register('test_shortcode', array('DBHTMLTextTest_Shortcode', 'handle_shortcode')); ShortcodeParser::set_active('htmltest'); } public function tearDown() { ShortcodeParser::set_active('default'); parent::tearDown(); } /** * Test {@link Text->LimitCharacters()} */ public function providerLimitCharacters() { // HTML characters are stripped safely return [ ['The little brown fox jumped over the lazy cow.', 'The little brown fox...'], ['
Short & Sweet
', 'Short & Sweet'], ['This text contains & in it', 'This text contains &...'], ]; } /** * Test {@link DBHTMLText->LimitCharacters()} * @dataProvider providerLimitCharacters * @param string $originalValue * @param string $expectedValue */ public function testLimitCharacters($originalValue, $expectedValue) { $textObj = DBField::create_field('HTMLFragment', $originalValue); $result = $textObj->obj('LimitCharacters')->forTemplate(); $this->assertEquals($expectedValue, $result); } /** * @return array */ public function providerLimitCharactersToClosestWord() { // HTML is converted safely to plain text return [ // Standard words limited, ellipsis added if truncated ['Lorem ipsum dolor sit amet
', 24, 'Lorem ipsum dolor sit...'], // Complete words less than the character limit don't get truncated, ellipsis not added ['Lorem ipsum
', 24, 'Lorem ipsum'], ['Lorem
', 24, 'Lorem'], ['', 24, ''], // No words produces nothing! // Special characters are encoded safely ['Nice & Easy', 24, 'Nice & Easy'], // HTML is safely converted to plain text ['Lorem ipsum dolor sit amet
', 24, 'Lorem ipsum dolor sit...'], ['Lorem ipsum dolor sit amet
', 24, 'Lorem ipsum dolor sit...'], ['Lorem ipsum
', 24, 'Lorem ipsum'], ['Lorem & ipsum dolor sit amet', 24, 'Lorem & ipsum dolor sit...'] ]; } /** * Test {@link DBHTMLText->LimitCharactersToClosestWord()} * @dataProvider providerLimitCharactersToClosestWord * * @param string $originalValue Raw string input * @param int $limit * @param string $expectedValue Expected template value */ public function testLimitCharactersToClosestWord($originalValue, $limit, $expectedValue) { $textObj = DBField::create_field('HTMLFragment', $originalValue); $result = $textObj->obj('LimitCharactersToClosestWord', [$limit])->forTemplate(); $this->assertEquals($expectedValue, $result); } public function providerSummary() { return [ [ 'Should strip tags, but leave text
', 50, 'Should strip tags, but leave text', ], [ // Line breaks are preserved 'Unclosed tags
should not phase it
Second paragraph
should not cause errors or appear in output
', 50, "Second paragraphSecond paragraph
should not cause errors or appear in output
', 50, "Second paragraphSecond paragraph
should not cause errors or appear in output
', 50, "Second paragraphexample text words hello
', 50, 'example text words hello', ], // Shorter limits [ 'A long paragraph should be cut off if limit is set
', 5, 'A long paragraph should be...', ], [ 'No matter how many tags are in it
', 5, 'No matter how many tags...', ], [ 'A sentence is. nicer than hard limits
', 5, 'A sentence is.', ], ]; } /** * @dataProvider providerSummary * @param string $originalValue * @param int $limit * @param string $expectedValue */ public function testSummary($originalValue, $limit, $expectedValue) { $textObj = DBField::create_field('HTMLFragment', $originalValue); $result = $textObj->obj('Summary', [$limit])->forTemplate(); $this->assertEquals($expectedValue, $result); } public function testSummaryEndings() { $cases = array( '...', ' -> more', '' ); $orig = 'Cut it off, cut it off
'; $match = 'Cut it off, cut'; foreach($cases as $add) { $textObj = DBField::create_field('HTMLFragment', $orig); $result = $textObj->obj('Summary', [4, $add])->forTemplate(); $this->assertEquals($match.Convert::raw2xml($add), $result); } } public function providerFirstSentence() { return [ // Same behaviour as DBTextTest ['', ''], ['First sentence.', 'First sentence.'], ['First sentence. Second sentence', 'First sentence.'], ['First sentence? Second sentence', 'First sentence?'], ['First sentence! Second sentence', 'First sentence!'], // DBHTHLText strips HTML first ['First sentence. Second sentence. Third sentence
', 'First sentence.'], ]; } /** * @dataProvider providerFirstSentence * @param string $originalValue * @param string $expectedValue */ public function testFirstSentence($originalValue, $expectedValue) { $textObj = DBField::create_field('HTMLFragment', $originalValue); $result = $textObj->obj('FirstSentence')->forTemplate(); $this->assertEquals($expectedValue, $result); } public function providerToPlain() { return [ [ 'Lots of HTML nested tags', 'Lots of HTML nested tags', ], [ '
Multi
Paragraph
Also has multilines.
Collapses
Excessive
Newlines
A dog ate a cat while looking at a Foobar
', 100, 'ate', // it should highlight 3 letters or more. 'A dog ate a cat while looking at a Foobar', ] ]; } /** * @dataProvider providerContextSummary * @param string $originalValue Input * @param int $limit Numer of characters * @param string $keywords Keywords to highlight * @param string $expectedValue Expected output (XML encoded safely) */ public function testContextSummary($originalValue, $limit, $keywords, $expectedValue) { $text = DBField::create_field('HTMLFragment', $originalValue); $result = $text->obj('ContextSummary', [$limit, $keywords])->forTemplate(); // it should highlight 3 letters or more. $this->assertEquals($expectedValue, $result); } public function testRAW() { $data = DBField::create_field('HTMLFragment', 'This & This'); $this->assertEquals('This & This', $data->RAW()); $data = DBField::create_field('HTMLFragment', 'This & This'); $this->assertEquals('This & This', $data->RAW()); } public function testXML() { $data = DBField::create_field('HTMLFragment', 'This & This'); $this->assertEquals('This & This', $data->XML()); $data = DBField::create_field('HTMLFragment', 'This & This'); $this->assertEquals('This & This', $data->XML()); } public function testHTML() { $data = DBField::create_field('HTMLFragment', 'This & This'); $this->assertEquals('This & This', $data->HTML()); $data = DBField::create_field('HTMLFragment', 'This & This'); $this->assertEquals('This & This', $data->HTML()); } public function testJS() { $data = DBField::create_field('HTMLText', '"this is & test"'); $this->assertEquals('\"this is \x26amp; test\"', $data->JS()); } public function testATT() { // HTML Fragment $data = DBField::create_field('HTMLFragment', '"this is a test"'); $this->assertEquals('"this is a test"', $data->ATT()); // HTML Text (passes shortcodes + tidy) $data = DBField::create_field('HTMLText', '"'); $this->assertEquals('"', $data->ATT()); } public function testShortcodesProcessed() { /** @var DBHTMLText $obj */ $obj = DBField::create_field( 'HTMLText', 'Some content [test_shortcode] with shortcode
' ); // Basic DBField methods process shortcodes $this->assertEquals( 'Some content shortcode content with shortcode', $obj->Plain() ); $this->assertEquals( 'Some content shortcode content with shortcode
', $obj->RAW() ); $this->assertEquals( '<p>Some content <strong>shortcode content</strong> with shortcode</p>', $obj->XML() ); $this->assertEquals( '<p>Some content <strong>shortcode content</strong> with shortcode</p>', $obj->HTML() ); // Test summary methods $this->assertEquals( 'Some content shortcode...', $obj->Summary(3) ); $this->assertEquals( 'Some content shortcode content with shortcode', $obj->LimitSentences(1) ); $this->assertEquals( 'Some content shortco...', $obj->LimitCharacters(20) ); } public function testParse() { // Test parse /** @var DBHTMLText $obj */ $obj = DBField::create_field( 'HTMLText', '[b]Some content[/b] [test_shortcode] with shortcode
' ); // BBCode strips HTML and applies own formatting $this->assertEquals( 'Some content shortcode content with shortcode', $obj->Parse('BBCodeParser')->forTemplate() ); } function testExists() { $h = new DBHTMLText(); $h->setValue(""); $this->assertFalse($h->exists()); $h->setValue(""); $this->assertFalse($h->exists()); $h->setValue(""); $this->assertFalse($h->exists()); $h->setValue(""); $this->assertFalse($h->exists()); $h->setValue(""); $this->assertFalse($h->exists()); $h->setValue("something"); $this->assertTrue($h->exists()); $h->setValue(""); $this->assertTrue($h->exists()); $h->setValue(""); $this->assertTrue($h->exists()); $h->setValue(""); $this->assertTrue($h->exists()); $h->setValue(""); $this->assertTrue($h->exists()); $h->setValue("