previousLocaleSetting = null; } public function tearDown() { parent::tearDown(); // If a test sets the locale, reset it on teardown if ($this->previousLocaleSetting) { setlocale(LC_CTYPE, $this->previousLocaleSetting); } } /** * Test {@link HTMLText->LimitCharacters()} */ public function testLimitCharacters() { $cases = array( 'The little brown fox jumped over the lazy cow.' => 'The little brown fox...', '

This is some text in a paragraph.

' => 'This is some text in...', 'This text contains & in it' => 'This text contains &...' ); foreach($cases as $originalValue => $expectedValue) { $textObj = new HTMLText('Test'); $textObj->setValue($originalValue); $this->assertEquals($expectedValue, $textObj->LimitCharacters()); } } public function testSummaryBasics() { $cases = array( '

Should not take header

Should take paragraph

' => 'Should take paragraph', '

Should strip tags, but leave text

' => 'Should strip tags, but leave text', '

Unclosed tags
should not phase it

' => 'Unclosed tags should not phase it', '

Second paragraph

should not cause errors or appear in output

' => 'Second paragraph', '

Second paragraph

should not cause errors or appear in output

' => 'Second paragraph', '

Second paragraph

should not cause errors or appear in output

' => 'Second paragraph', '

example text words hello

' => 'example text words hello', ); foreach($cases as $originalValue => $expectedValue) { $textObj = new HTMLText('Test'); $textObj->setValue($originalValue); $this->assertEquals($expectedValue, $textObj->Summary()); } } public function testSummaryLimits() { $cases = array( '

A long paragraph should be cut off if limit is set

' => 'A long paragraph should be...', '

No matter how many tags are in it

' => 'No matter how many tags...', '

A sentence is. nicer than hard limits

' => 'A sentence is.', '

But not. If it\'s too short

' => 'But not. If it\'s too...' ); foreach($cases as $originalValue => $expectedValue) { $textObj = new HTMLText('Test'); $textObj->setValue($originalValue); $this->assertEquals($expectedValue, $textObj->Summary(5, 3, '...')); } } public function testSummaryEndings() { $cases = array( '...', ' -> more', '' ); $orig = '

Cut it off, cut it off

'; $match = 'Cut it off, cut'; foreach($cases as $add) { $textObj = new HTMLText(); $textObj->setValue($orig); $this->assertEquals($match.$add, $textObj->Summary(4, 0, $add)); } } public function testSummaryFlexTooBigShouldNotCauseError() { $orig = '

Cut it off, cut it off

'; $match = 'Cut it off, cut'; $textObj = new HTMLText(); $textObj->setValue($orig); $this->assertEquals($match, $textObj->Summary(4, 10, '')); } public function testSummaryInvalidHTML() { $cases = array( 'It\'s got a tag, but

This doesn\'t make < >' => 'This doesn\'t make any', 'This doesn\'t should ignore

Sentence with {$many}words. Second sentence.

" => "Sentence with {$many}words.", '

This classic picture book features a repetitive format that lends itself to audience interaction.'. '  Illustrator Eric Carle submitted new, bolder artwork for the 25th anniversary edition.

' => 'This classic picture book features a repetitive format that lends itself to audience interaction.' ); foreach($cases as $orig => $match) { $textObj = new HTMLText(); $textObj->setValue($orig); $this->assertEquals($match, $textObj->FirstSentence()); } } public function testRAW() { $data = DBField::create_field('HTMLText', 'This & This'); $this->assertEquals($data->RAW(), 'This & This'); $data = DBField::create_field('HTMLText', 'This & This'); $this->assertEquals($data->RAW(), 'This & This'); } public function testXML() { $data = DBField::create_field('HTMLText', 'This & This'); $this->assertEquals($data->XML(), 'This & This'); } public function testHTML() { $data = DBField::create_field('HTMLText', 'This & This'); $this->assertEquals($data->HTML(), 'This & This'); } public function testJS() { $data = DBField::create_field('HTMLText', '"this is a test"'); $this->assertEquals($data->JS(), '\"this is a test\"'); } public function testATT() { $data = DBField::create_field('HTMLText', '"this is a test"'); $this->assertEquals($data->ATT(), '"this is a test"'); } function testExists() { $h = new HTMLText; $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(""); $this->assertTrue($h->exists()); $h->setValue(""); $this->assertTrue($h->exists()); $h->setValue("

test

"); $this->assertTrue($h->exists()); } function testWhitelist() { $textObj = new HTMLText('Test', 'meta,link'); $this->assertEquals( '', $textObj->whitelistContent('

Remove

Remove Text'), 'Removes any elements not in whitelist excluding text elements' ); $textObj = new HTMLText('Test', 'meta,link,text()'); $this->assertEquals( 'Keep Text', $textObj->whitelistContent('

Remove

Keep Text'), 'Removes any elements not in whitelist including text elements' ); } public function testShortCodeParsedInRAW() { $parser = ShortcodeParser::get('HTMLTextTest'); $parser->register('shortcode', function($arguments, $content, $parser, $tagName, $extra) { return 'replaced'; }); ShortcodeParser::set_active('HTMLTextTest'); /** @var HTMLText $field */ $field = DBField::create_field('HTMLText', '

[shortcode]

'); $this->assertEquals('

replaced

', $field->RAW()); $this->assertEquals('

replaced

', (string)$field); $field->setOptions(array( 'shortcodes' => false, )); $this->assertEquals('

[shortcode]

', $field->RAW()); $this->assertEquals('

[shortcode]

', (string)$field); ShortcodeParser::set_active('default'); } public function testShortCodeParsedInTemplateHelpers() { $parser = ShortcodeParser::get('HTMLTextTest'); $parser->register('shortcode', function($arguments, $content, $parser, $tagName, $extra) { return 'Replaced short code with this.
home'; }); ShortcodeParser::set_active('HTMLTextTest'); /** @var HTMLText $field */ $field = DBField::create_field('HTMLText', '

[shortcode]

'); $this->assertEquals( '<p>Replaced short code with this. <a href="home">home</a></p>', $field->HTMLATT() ); $this->assertEquals( '%3Cp%3EReplaced+short+code+with+this.+%3Ca+href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E', $field->URLATT() ); $this->assertEquals( '%3Cp%3EReplaced%20short%20code%20with%20this.%20%3Ca%20href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E', $field->RAWURLATT() ); $this->assertEquals( '<p>Replaced short code with this. <a href="home">home</a></p>', $field->ATT() ); $this->assertEquals( '

Replaced short code with this. home

', $field->RAW() ); $this->assertEquals( '\x3cp\x3eReplaced short code with this. \x3ca href=\"home\"\x3ehome\x3c/a\x3e\x3c/p\x3e', $field->JS() ); $this->assertEquals( '<p>Replaced short code with this. <a href="home">home</a></p>', $field->HTML() ); $this->assertEquals( '<p>Replaced short code with this. <a href="home">home</a></p>', $field->XML() ); $this->assertEquals( 'Repl...', $field->LimitCharacters(4, '...') ); $this->assertEquals( 'Replaced...', $field->LimitCharactersToClosestWord(10, '...') ); $this->assertEquals( 'Replaced...', $field->LimitWordCount(1, '...') ); $this->assertEquals( '

replaced short code with this. home

', $field->LowerCase() ); $this->assertEquals( '

REPLACED SHORT CODE WITH THIS. HOME

', $field->UpperCase() ); $this->assertEquals( 'Replaced short code with this. home', $field->NoHTML() ); Config::nest(); Config::inst()->update('Director', 'alternate_base_url', 'http://example.com/'); $this->assertEquals( '

Replaced short code with this. home

', $field->AbsoluteLinks() ); Config::unnest(); $this->assertEquals( 'Replaced short code with this.', $field->LimitSentences(1) ); $this->assertEquals( 'Replaced short code with this.', $field->FirstSentence() ); $this->assertEquals( 'Replaced short...', $field->Summary(2) ); $this->assertEquals( 'Replaced short code with...', $field->BigSummary(4) ); $this->assertEquals( 'Replaced short code with this. home[home]', $field->FirstParagraph() ); $this->assertEquals( 'Replaced short code with this. home', $field->ContextSummary(500, 'short code') ); ShortcodeParser::set_active('default'); } public function testValidUtf8() { // Install a UTF-8 locale $this->previousLocaleSetting = setlocale(LC_CTYPE, 0); $locales = array('en_US.UTF-8', 'en_NZ.UTF-8', 'de_DE.UTF-8'); $localeInstalled = false; foreach ($locales as $locale) { if ($localeInstalled = setlocale(LC_CTYPE, $locale)) { break; } } // If the system doesn't have any of the UTF-8 locales, exit early if ($localeInstalled === false) { $this->markTestIncomplete('Unable to run this test because of missing locale!'); return; } $problematicText = html_entity_decode('

This is a Test with non-breaking space!

', ENT_COMPAT, 'UTF-8'); $textObj = new HTMLText('Test'); $textObj->setValue($problematicText); $this->assertTrue(mb_check_encoding($textObj->FirstSentence(), 'UTF-8')); $this->assertTrue(mb_check_encoding($textObj->Summary(), 'UTF-8')); } }