2009-02-12 10:38:43 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-02-12 10:38:43 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class HTMLTextTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-07-06 16:16:39 +02:00
|
|
|
private $previousLocaleSetting = null;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
// clear the previous locale setting
|
|
|
|
$this->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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-12 10:38:43 +01:00
|
|
|
/**
|
2009-02-12 10:40:14 +01:00
|
|
|
* Test {@link HTMLText->LimitCharacters()}
|
2009-02-12 10:38:43 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLimitCharacters() {
|
2009-02-12 10:38:43 +01:00
|
|
|
$cases = array(
|
|
|
|
'The little brown fox jumped over the lazy cow.' => 'The little brown fox...',
|
2010-11-29 04:18:02 +01:00
|
|
|
'<p>This is some text in a paragraph.</p>' => 'This is some text in...',
|
|
|
|
'This text contains & in it' => 'This text contains &...'
|
2009-02-12 10:38:43 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-12 10:38:43 +01:00
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new HTMLText('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->LimitCharacters());
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSummaryBasics() {
|
2009-06-17 13:36:49 +02:00
|
|
|
$cases = array(
|
|
|
|
'<h1>Should not take header</h1><p>Should take paragraph</p>' => 'Should take paragraph',
|
|
|
|
'<p>Should strip <b>tags, but leave</b> text</p>' => 'Should strip tags, but leave text',
|
|
|
|
'<p>Unclosed tags <br>should not phase it</p>' => 'Unclosed tags should not phase it',
|
2011-03-21 22:29:48 +01:00
|
|
|
'<p>Second paragraph</p><p>should not cause errors or appear in output</p>' => 'Second paragraph',
|
2014-08-15 08:53:05 +02:00
|
|
|
'<img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>'
|
2012-09-26 23:34:00 +02:00
|
|
|
=> 'Second paragraph',
|
|
|
|
' <img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>'
|
|
|
|
=> 'Second paragraph',
|
|
|
|
'<p><img src="remove me">example <img src="include me">text words hello<img src="hello"></p>'
|
|
|
|
=> 'example text words hello',
|
2009-06-17 13:36:49 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new HTMLText('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->Summary());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSummaryLimits() {
|
2009-06-17 13:36:49 +02:00
|
|
|
$cases = array(
|
|
|
|
'<p>A long paragraph should be cut off if limit is set</p>' => 'A long paragraph should be...',
|
|
|
|
'<p>No matter <i>how many <b>tags</b></i> are in it</p>' => 'No matter how many tags...',
|
|
|
|
'<p>A sentence is. nicer than hard limits</p>' => 'A sentence is.',
|
|
|
|
'<p>But not. If it\'s too short</p>' => 'But not. If it\'s too...'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new HTMLText('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->Summary(5, 3, '...'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSummaryEndings() {
|
2009-06-17 13:36:49 +02:00
|
|
|
$cases = array(
|
|
|
|
'...', ' -> more', ''
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
$orig = '<p>Cut it off, cut it off</p>';
|
|
|
|
$match = 'Cut it off, cut';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
foreach($cases as $add) {
|
|
|
|
$textObj = new HTMLText();
|
|
|
|
$textObj->setValue($orig);
|
|
|
|
$this->assertEquals($match.$add, $textObj->Summary(4, 0, $add));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSummaryFlexTooBigShouldNotCauseError() {
|
2009-06-17 13:36:49 +02:00
|
|
|
$orig = '<p>Cut it off, cut it off</p>';
|
|
|
|
$match = 'Cut it off, cut';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
$textObj = new HTMLText();
|
|
|
|
$textObj->setValue($orig);
|
|
|
|
$this->assertEquals($match, $textObj->Summary(4, 10, ''));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSummaryInvalidHTML() {
|
2009-06-17 13:36:49 +02:00
|
|
|
$cases = array(
|
2012-09-26 23:34:00 +02:00
|
|
|
'It\'s got a <p<> tag, but<p junk true>This doesn\'t <a id="boo">make</b class="wa"> < ><any< sense</p>'
|
|
|
|
=> 'This doesn\'t make any',
|
2009-06-17 13:36:49 +02:00
|
|
|
'This doesn\'t <a style="much horray= true>even</b> < ><have< a <i>p tag' => 'This doesn\'t even have'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
foreach($cases as $orig => $match) {
|
|
|
|
$textObj = new HTMLText();
|
|
|
|
$textObj->setValue($orig);
|
|
|
|
$this->assertEquals($match, $textObj->Summary(4, 0, ''));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFirstSentence() {
|
2009-06-17 13:36:49 +02:00
|
|
|
$many = str_repeat('many ', 100);
|
|
|
|
$cases = array(
|
|
|
|
'<h1>should ignore</h1><p>First sentence. Second sentence.</p>' => 'First sentence.',
|
|
|
|
'<h1>should ignore</h1><p>First Mr. sentence. Second sentence.</p>' => 'First Mr. sentence.',
|
2012-09-26 23:34:00 +02:00
|
|
|
"<h1>should ignore</h1><p>Sentence with {$many}words. Second sentence.</p>"
|
|
|
|
=> "Sentence with {$many}words.",
|
2013-08-21 08:54:05 +02:00
|
|
|
'<p>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.</p>'
|
2013-05-01 06:03:58 +02:00
|
|
|
=> 'This classic picture book features a repetitive format that lends itself to audience interaction.'
|
2009-06-17 13:36:49 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
foreach($cases as $orig => $match) {
|
|
|
|
$textObj = new HTMLText();
|
|
|
|
$textObj->setValue($orig);
|
|
|
|
$this->assertEquals($match, $textObj->FirstSentence());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-01-31 01:07:24 +01:00
|
|
|
|
|
|
|
public function testRAW() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$data = DBField::create_field('HTMLText', 'This & This');
|
2012-01-31 01:07:24 +01:00
|
|
|
$this->assertEquals($data->RAW(), 'This & This');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$data = DBField::create_field('HTMLText', 'This & This');
|
2012-01-31 01:07:24 +01:00
|
|
|
$this->assertEquals($data->RAW(), 'This & This');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-31 01:07:24 +01:00
|
|
|
public function testXML() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$data = DBField::create_field('HTMLText', 'This & This');
|
2012-01-31 01:07:24 +01:00
|
|
|
$this->assertEquals($data->XML(), 'This & This');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHTML() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$data = DBField::create_field('HTMLText', 'This & This');
|
2012-01-31 01:07:24 +01:00
|
|
|
$this->assertEquals($data->HTML(), 'This & This');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testJS() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$data = DBField::create_field('HTMLText', '"this is a test"');
|
2012-01-31 01:07:24 +01:00
|
|
|
$this->assertEquals($data->JS(), '\"this is a test\"');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testATT() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$data = DBField::create_field('HTMLText', '"this is a test"');
|
2012-01-31 01:07:24 +01:00
|
|
|
$this->assertEquals($data->ATT(), '"this is a test"');
|
|
|
|
}
|
2011-12-12 06:44:47 +01:00
|
|
|
|
|
|
|
function testExists() {
|
|
|
|
$h = new HTMLText;
|
|
|
|
$h->setValue("");
|
|
|
|
$this->assertFalse($h->exists());
|
|
|
|
$h->setValue("<p></p>");
|
|
|
|
$this->assertFalse($h->exists());
|
|
|
|
$h->setValue("<p> </p>");
|
|
|
|
$this->assertFalse($h->exists());
|
|
|
|
$h->setValue("<h2/>");
|
|
|
|
$this->assertFalse($h->exists());
|
|
|
|
$h->setValue("<h2></h2>");
|
|
|
|
$this->assertFalse($h->exists());
|
|
|
|
|
|
|
|
$h->setValue("something");
|
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
$h->setValue("<img src=\"dummy.png\">");
|
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
$h->setValue("<img src=\"dummy.png\"><img src=\"dummy.png\">");
|
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
$h->setValue("<p><img src=\"dummy.png\"></p>");
|
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
|
|
|
|
$h->setValue("<iframe src=\"http://www.google.com\"></iframe>");
|
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
$h->setValue("<embed src=\"test.swf\">");
|
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
$h->setValue("<object width=\"400\" height=\"400\" data=\"test.swf\"></object>");
|
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
|
|
|
|
|
|
|
|
$h->setValue("<p>test</p>");
|
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
}
|
2014-02-16 23:22:26 +01:00
|
|
|
|
|
|
|
function testWhitelist() {
|
|
|
|
$textObj = new HTMLText('Test', 'meta,link');
|
|
|
|
$this->assertEquals(
|
2014-04-09 04:48:02 +02:00
|
|
|
'<meta content="Keep"><link href="Also Keep">',
|
|
|
|
$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Remove Text'),
|
|
|
|
'Removes any elements not in whitelist excluding text elements'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-09 04:48:02 +02:00
|
|
|
$textObj = new HTMLText('Test', 'meta,link,text()');
|
|
|
|
$this->assertEquals(
|
|
|
|
'<meta content="Keep"><link href="Also Keep">Keep Text',
|
|
|
|
$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Keep Text'),
|
|
|
|
'Removes any elements not in whitelist including text elements'
|
2014-02-16 23:22:26 +01:00
|
|
|
);
|
|
|
|
}
|
2016-06-30 16:46:23 +02:00
|
|
|
|
|
|
|
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', '<p>[shortcode]</p>');
|
|
|
|
$this->assertEquals('<p>replaced</p>', $field->RAW());
|
|
|
|
$this->assertEquals('<p>replaced</p>', (string)$field);
|
|
|
|
|
|
|
|
$field->setOptions(array(
|
|
|
|
'shortcodes' => false,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals('<p>[shortcode]</p>', $field->RAW());
|
|
|
|
$this->assertEquals('<p>[shortcode]</p>', (string)$field);
|
|
|
|
|
|
|
|
|
2016-06-30 16:54:44 +02:00
|
|
|
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. <a href="home">home</a>';
|
|
|
|
});
|
|
|
|
ShortcodeParser::set_active('HTMLTextTest');
|
|
|
|
/** @var HTMLText $field */
|
|
|
|
$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>');
|
|
|
|
|
|
|
|
$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(
|
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>',
|
|
|
|
$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(
|
|
|
|
'<p>replaced short code with this. <a href="home">home</a></p>',
|
|
|
|
$field->LowerCase()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<P>REPLACED SHORT CODE WITH THIS. <A HREF="HOME">HOME</A></P>',
|
|
|
|
$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(
|
|
|
|
'<p>Replaced short code with this. <a href="http://example.com/home">home</a></p>',
|
|
|
|
$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 <span class="highlight">short</span> <span class="highlight">code</span> with this. home',
|
|
|
|
$field->ContextSummary(500, 'short code')
|
|
|
|
);
|
|
|
|
|
2016-06-30 16:46:23 +02:00
|
|
|
ShortcodeParser::set_active('default');
|
|
|
|
}
|
2017-07-06 16:16:39 +02:00
|
|
|
|
|
|
|
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('<p>This is a Test with non-breaking space!</p>', 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'));
|
|
|
|
}
|
2009-02-12 10:38:43 +01:00
|
|
|
}
|