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
/**
* 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...'
);
foreach ( $cases as $originalValue => $expectedValue ) {
$textObj = new Text ( 'Test' );
$textObj -> setValue ( $originalValue );
$this -> assertEquals ( $expectedValue , $textObj -> LimitCharacters ());
}
}
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...' ,
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!
/* 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
);
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 \n Blah Blah Blah " => " Stuff \n Blah 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
);
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
}
2009-02-12 10:24:33 +01: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.' ,
'<p>First sentence. <em class="dummyClass">Second sentence</em>. Third sentence</p>' => 'First sentence. Second sentence.'
);
foreach ( $cases as $originalValue => $expectedValue ) {
$textObj = new Text ( 'Test' );
$textObj -> setValue ( $originalValue );
$this -> assertEquals ( $expectedValue , $textObj -> LimitSentences ( 2 ));
}
}
2009-08-24 08:16:56 +02:00
/**
* Test { @ link Text -> BigSummary ()}
*/
2012-09-19 12:07:39 +02:00
public function testBigSummary () {
2009-08-24 08:16:56 +02:00
$cases = array (
'This text has multiple sentences. Big Summary uses this to split sentences up.' => 'This text has multiple...' ,
'This text does not have multiple sentences' => 'This text does not...' ,
'Very short' => 'Very short' ,
'' => ''
);
foreach ( $cases as $originalValue => $expectedValue ) {
2012-03-27 06:57:42 +02:00
$textObj = DBField :: create_field ( 'Text' , $originalValue );
2009-08-24 08:16:56 +02:00
$this -> assertEquals ( $expectedValue , $textObj -> BigSummary ( 4 ));
}
}
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' ;
2010-10-15 05:44:24 +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' ;
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' );
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 );
$this -> assertEquals (
2010-10-15 05:00:12 +02:00
'This is <span class="highlight">some</span> <span class="highlight">test</span> text. <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 )
);
2010-10-15 05:44:24 +02:00
$textObj -> setValue ( $testString3 );
// 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 )
);
// 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 )
);
2010-10-15 04:59:46 +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' );
}
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"' );
}
2012-03-24 04:04:52 +01:00
}