2009-02-12 10:24:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-02-12 10:24:33 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class TextTest extends SapphireTest {
|
2009-02-12 10:38:43 +01: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
|
|
|
/**
|
|
|
|
* Test {@link Text->LimitCharacters()}
|
|
|
|
*/
|
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...',
|
|
|
|
'<p>This is some text in a paragraph.</p>' => '<p>This is some text...'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-12 10:38:43 +01:00
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new Text('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->LimitCharacters());
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-10-17 11:41:28 +02:00
|
|
|
/**
|
|
|
|
* Test {@link Text->LimitCharactersToClosestWord()}
|
|
|
|
*/
|
|
|
|
public function testLimitCharactersToClosestWord() {
|
|
|
|
$cases = array(
|
|
|
|
/* Standard words limited, ellipsis added if truncated */
|
|
|
|
'Lorem ipsum dolor sit amet' => 'Lorem ipsum dolor sit...',
|
|
|
|
|
|
|
|
/* Complete words less than the character limit don't get truncated, ellipsis not added */
|
|
|
|
'Lorem ipsum' => 'Lorem ipsum',
|
|
|
|
'Lorem' => 'Lorem',
|
|
|
|
'' => '', // No words produces nothing!
|
|
|
|
|
|
|
|
/* HTML tags get stripped out, leaving the raw text */
|
|
|
|
'<p>Lorem ipsum dolor sit amet</p>' => 'Lorem ipsum dolor sit...',
|
|
|
|
'<p><span>Lorem ipsum dolor sit amet</span></p>' => 'Lorem ipsum dolor sit...',
|
|
|
|
'<p>Lorem ipsum</p>' => 'Lorem ipsum',
|
|
|
|
|
|
|
|
/* HTML entities are treated as a single character */
|
|
|
|
'Lorem & ipsum dolor sit amet' => 'Lorem & ipsum dolor...'
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new Text('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->LimitCharactersToClosestWord(24));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-12 10:24:33 +01:00
|
|
|
/**
|
|
|
|
* Test {@link Text->LimitWordCount()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLimitWordCount() {
|
2009-02-12 10:24:33 +01:00
|
|
|
$cases = array(
|
|
|
|
/* Standard words limited, ellipsis added if truncated */
|
|
|
|
'The little brown fox jumped over the lazy cow.' => 'The little brown...',
|
2009-02-12 10:30:56 +01:00
|
|
|
' This text has white space around the ends ' => 'This text has...',
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-12 10:24:33 +01:00
|
|
|
/* Words less than the limt word count don't get truncated, ellipsis not added */
|
|
|
|
'Two words' => 'Two words', // Two words shouldn't have an ellipsis
|
|
|
|
'One' => 'One', // Neither should one word
|
|
|
|
'' => '', // No words produces nothing!
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-12 10:24:33 +01:00
|
|
|
/* HTML tags get stripped out, leaving the raw text */
|
|
|
|
'<p>Text inside a paragraph tag should also work</p>' => 'Text inside a...',
|
2009-02-12 10:30:56 +01:00
|
|
|
'<p><span>Text nested inside another tag should also work</span></p>' => 'Text nested inside...',
|
|
|
|
'<p>Two words</p>' => 'Two words'
|
2009-02-12 10:24:33 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-12 10:24:33 +01:00
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new Text('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->LimitWordCount(3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test {@link Text->LimitWordCountXML()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLimitWordCountXML() {
|
2009-02-12 10:24:33 +01:00
|
|
|
$cases = array(
|
2009-02-12 10:30:56 +01:00
|
|
|
'<p>Stuff & stuff</p>' => 'Stuff &...',
|
2010-12-16 23:55:17 +01:00
|
|
|
"Stuff\nBlah Blah Blah" => "Stuff\nBlah Blah...",
|
2009-02-12 10:30:56 +01:00
|
|
|
"Stuff<Blah Blah" => "Stuff<Blah Blah",
|
|
|
|
"Stuff>Blah Blah" => "Stuff>Blah Blah"
|
2009-02-12 10:24:33 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-12 10:24:33 +01:00
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new Text('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->LimitWordCountXML(3));
|
|
|
|
}
|
2009-02-12 10:30:56 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-09 08:14:04 +02:00
|
|
|
/**
|
|
|
|
* Test {@link Text->LimitSentences()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLimitSentences() {
|
2009-07-09 08:14:04 +02:00
|
|
|
$cases = array(
|
|
|
|
'' => '',
|
|
|
|
'First sentence.' => 'First sentence.',
|
|
|
|
'First sentence. Second sentence' => 'First sentence. Second sentence.',
|
|
|
|
'<p>First sentence.</p>' => 'First sentence.',
|
|
|
|
'<p>First sentence. Second sentence. Third sentence</p>' => 'First sentence. Second sentence.',
|
|
|
|
'<p>First sentence. <em>Second sentence</em>. Third sentence</p>' => 'First sentence. Second sentence.',
|
2012-09-26 23:34:00 +02:00
|
|
|
'<p>First sentence. <em class="dummyClass">Second sentence</em>. Third sentence</p>'
|
|
|
|
=> 'First sentence. Second sentence.'
|
2009-07-09 08:14:04 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-09 08:14:04 +02:00
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new Text('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->LimitSentences(2));
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 02:57:12 +02:00
|
|
|
|
|
|
|
public function testFirstSentance() {
|
|
|
|
$cases = array(
|
|
|
|
'' => '',
|
|
|
|
'First sentence.' => 'First sentence.',
|
|
|
|
'First sentence. Second sentence' => 'First sentence.',
|
|
|
|
'First sentence? Second sentence' => 'First sentence?',
|
|
|
|
'First sentence! Second sentence' => 'First sentence!',
|
|
|
|
'<p>First sentence.</p>' => 'First sentence.',
|
|
|
|
'<p>First sentence. Second sentence. Third sentence</p>' => 'First sentence.',
|
|
|
|
'<p>First sentence. <em>Second sentence</em>. Third sentence</p>' => 'First sentence.',
|
|
|
|
'<p>First sentence. <em class="dummyClass">Second sentence</em>. Third sentence</p>'
|
|
|
|
=> 'First sentence.'
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = new Text('Test');
|
|
|
|
$textObj->setValue($originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->FirstSentence());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-24 08:16:56 +02:00
|
|
|
/**
|
|
|
|
* Test {@link Text->BigSummary()}
|
|
|
|
*/
|
2014-10-01 05:02:28 +02:00
|
|
|
public function testBigSummaryPlain() {
|
2009-08-24 08:16:56 +02:00
|
|
|
$cases = array(
|
2014-10-01 05:02:28 +02:00
|
|
|
'<p>This text has multiple sentences. Big Summary uses this to split sentences up.</p>'
|
2012-09-26 23:34:00 +02:00
|
|
|
=> 'This text has multiple...',
|
2009-08-24 08:16:56 +02:00
|
|
|
'This text does not have multiple sentences' => 'This text does not...',
|
|
|
|
'Very short' => 'Very short',
|
|
|
|
'' => ''
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-24 08:16:56 +02:00
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
2012-03-27 06:57:42 +02:00
|
|
|
$textObj = DBField::create_field('Text', $originalValue);
|
2014-10-01 05:02:28 +02:00
|
|
|
$this->assertEquals($expectedValue, $textObj->BigSummary(4, true));
|
2009-08-24 08:16:56 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-01 05:02:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test {@link Text->BigSummary()}
|
|
|
|
*/
|
|
|
|
public function testBigSummary() {
|
|
|
|
$cases = array(
|
|
|
|
'<strong>This</strong> text has multiple sentences. Big Summary uses this to split sentences up.</p>'
|
|
|
|
=> '<strong>This</strong> text has multiple...',
|
|
|
|
'This text does not have multiple sentences' => 'This text does not...',
|
|
|
|
'Very short' => 'Very short',
|
|
|
|
'' => ''
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach($cases as $originalValue => $expectedValue) {
|
|
|
|
$textObj = DBField::create_field('Text', $originalValue);
|
|
|
|
$this->assertEquals($expectedValue, $textObj->BigSummary(4, false));
|
2009-08-24 08:16:56 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testContextSummary() {
|
2009-09-18 04:53:46 +02:00
|
|
|
$testString1 = '<p>This is some text. It is a test</p>';
|
|
|
|
$testKeywords1 = 'test';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-18 04:53:46 +02:00
|
|
|
$testString2 = '<p>This is some test text. Test test what if you have multiple keywords.</p>';
|
|
|
|
$testKeywords2 = 'some test';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:44:24 +02:00
|
|
|
$testString3 = '<p>A dog ate a cat while looking at a Foobar</p>';
|
|
|
|
$testKeyword3 = 'a';
|
|
|
|
$testKeyword3a = 'ate';
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$textObj = DBField::create_field('Text', $testString1, 'Text');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-18 04:53:46 +02:00
|
|
|
$this->assertEquals(
|
2010-10-15 05:00:12 +02:00
|
|
|
'... text. It is a <span class="highlight">test</span>...',
|
2009-09-18 04:53:46 +02:00
|
|
|
$textObj->ContextSummary(20, $testKeywords1)
|
|
|
|
);
|
2010-10-15 05:00:12 +02:00
|
|
|
|
2009-09-18 04:53:46 +02:00
|
|
|
$textObj->setValue($testString2);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-18 04:47:18 +01:00
|
|
|
// test that it preserves case
|
2009-09-18 04:53:46 +02:00
|
|
|
$this->assertEquals(
|
2012-09-26 23:34:00 +02:00
|
|
|
'This is <span class="highlight">some</span> <span class="highlight">test</span> text.'
|
2016-12-18 04:47:18 +01:00
|
|
|
. ' <span class="highlight">Test</span> <span class="highlight">test</span> what if you have...',
|
2009-09-18 04:53:46 +02:00
|
|
|
$textObj->ContextSummary(50, $testKeywords2)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:44:24 +02:00
|
|
|
$textObj->setValue($testString3);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:44:24 +02:00
|
|
|
// test that it does not highlight too much (eg every a)
|
|
|
|
$this->assertEquals(
|
|
|
|
'A dog ate a cat while looking at a Foobar',
|
|
|
|
$textObj->ContextSummary(100, $testKeyword3)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:44:24 +02:00
|
|
|
// it should highlight 3 letters or more.
|
|
|
|
$this->assertEquals(
|
|
|
|
'A dog <span class="highlight">ate</span> a cat while looking at a Foobar',
|
|
|
|
$textObj->ContextSummary(100, $testKeyword3a)
|
|
|
|
);
|
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('Text', '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('Text', '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('Text', '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('Text', '"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('Text', '"this is a test"');
|
2012-01-31 01:07:24 +01:00
|
|
|
$this->assertEquals($data->ATT(), '"this is a test"');
|
|
|
|
}
|
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('This is a Test with non-breaking space!', ENT_COMPAT, 'UTF-8');
|
|
|
|
|
|
|
|
$textObj = new Text('Test');
|
|
|
|
$textObj->setValue($problematicText);
|
|
|
|
|
|
|
|
$this->assertTrue(mb_check_encoding($textObj->FirstSentence(), 'UTF-8'));
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|