2008-05-30 11:40:08 +00:00
< ? php
/**
2009-03-18 17:55:58 +00:00
* StringTest file
2008-05-30 11:40:08 +00:00
*
2009-05-01 21:05:46 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// cakephp . org )
2013-02-08 11:59:49 +00:00
* Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
2009-05-01 21:05:46 +00:00
* Licensed under The MIT License
2013-02-08 12:22:51 +00:00
* For full copyright and license information , please see the LICENSE . txt
2009-05-01 21:05:46 +00:00
* Redistributions of files must retain the above copyright notice .
2008-05-30 11:40:08 +00:00
*
2013-02-08 11:59:49 +00:00
* @ copyright Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2012-04-27 02:49:18 +00:00
* @ link http :// book . cakephp . org / 2.0 / en / development / testing . html CakePHP ( tm ) Tests
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Utility
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0.5432
2013-05-30 22:11:14 +00:00
* @ license http :// www . opensource . org / licenses / mit - license . php MIT License
2008-05-30 11:40:08 +00:00
*/
2013-05-30 22:11:14 +00:00
2010-12-10 06:23:27 +00:00
App :: uses ( 'String' , 'Utility' );
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-03-18 17:55:58 +00:00
* StringTest class
2008-05-30 11:40:08 +00:00
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Utility
2008-05-30 11:40:08 +00:00
*/
2008-07-21 02:40:58 +00:00
class StringTest extends CakeTestCase {
2009-05-01 21:05:46 +00:00
2012-02-06 10:35:19 +00:00
public function setUp () {
parent :: setUp ();
$this -> Text = new String ();
}
public function tearDown () {
parent :: tearDown ();
unset ( $this -> Text );
}
2008-06-02 19:22:55 +00:00
/**
* testUuidGeneration method
2008-07-05 14:31:22 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testUuidGeneration () {
2008-05-30 11:40:08 +00:00
$result = String :: uuid ();
2009-03-09 20:03:02 +00:00
$pattern = " /^[a-f0-9] { 8}-[a-f0-9] { 4}-[a-f0-9] { 4}-[a-f0-9] { 4}-[a-f0-9] { 12} $ / " ;
2012-03-14 02:59:20 +00:00
$match = ( bool ) preg_match ( $pattern , $result );
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( $match );
}
2009-05-01 21:05:46 +00:00
2008-06-02 19:22:55 +00:00
/**
* testMultipleUuidGeneration method
2008-07-05 14:31:22 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testMultipleUuidGeneration () {
2008-05-30 11:40:08 +00:00
$check = array ();
2008-07-05 14:31:22 +00:00
$count = mt_rand ( 10 , 1000 );
2009-03-09 20:03:02 +00:00
$pattern = " /^[a-f0-9] { 8}-[a-f0-9] { 4}-[a-f0-9] { 4}-[a-f0-9] { 4}-[a-f0-9] { 12} $ / " ;
2011-11-30 15:44:11 +00:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
2008-05-30 11:40:08 +00:00
$result = String :: uuid ();
2012-03-14 02:59:20 +00:00
$match = ( bool ) preg_match ( $pattern , $result );
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( $match );
$this -> assertFalse ( in_array ( $result , $check ));
$check [] = $result ;
}
}
2009-05-01 21:05:46 +00:00
2008-06-02 19:22:55 +00:00
/**
* testInsert method
2008-07-05 14:31:22 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testInsert () {
2009-11-03 23:21:01 +00:00
$string = 'some string' ;
$expected = 'some string' ;
$result = String :: insert ( $string , array ());
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-11-03 23:21:01 +00:00
2008-05-30 11:40:08 +00:00
$string = '2 + 2 = :sum. Cake is :adjective.' ;
$expected = '2 + 2 = 4. Cake is yummy.' ;
$result = String :: insert ( $string , array ( 'sum' => '4' , 'adjective' => 'yummy' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '2 + 2 = %sum. Cake is %adjective.' ;
$result = String :: insert ( $string , array ( 'sum' => '4' , 'adjective' => 'yummy' ), array ( 'before' => '%' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '2 + 2 = 2sum2. Cake is 9adjective9.' ;
$result = String :: insert ( $string , array ( 'sum' => '4' , 'adjective' => 'yummy' ), array ( 'format' => '/([\d])%s\\1/' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '2 + 2 = 12sum21. Cake is 23adjective45.' ;
$expected = '2 + 2 = 4. Cake is 23adjective45.' ;
$result = String :: insert ( $string , array ( 'sum' => '4' , 'adjective' => 'yummy' ), array ( 'format' => '/([\d])([\d])%s\\2\\1/' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
2009-04-09 13:15:43 +00:00
$string = ':web :web_site' ;
$expected = 'www http' ;
$result = String :: insert ( $string , array ( 'web' => 'www' , 'web_site' => 'http' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-04-09 13:15:43 +00:00
2008-05-30 11:40:08 +00:00
$string = '2 + 2 = <sum. Cake is <adjective>.' ;
$expected = '2 + 2 = <sum. Cake is yummy.' ;
$result = String :: insert ( $string , array ( 'sum' => '4' , 'adjective' => 'yummy' ), array ( 'before' => '<' , 'after' => '>' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '2 + 2 = \:sum. Cake is :adjective.' ;
$expected = '2 + 2 = :sum. Cake is yummy.' ;
$result = String :: insert ( $string , array ( 'sum' => '4' , 'adjective' => 'yummy' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '2 + 2 = !:sum. Cake is :adjective.' ;
$result = String :: insert ( $string , array ( 'sum' => '4' , 'adjective' => 'yummy' ), array ( 'escape' => '!' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '2 + 2 = \%sum. Cake is %adjective.' ;
$expected = '2 + 2 = %sum. Cake is yummy.' ;
$result = String :: insert ( $string , array ( 'sum' => '4' , 'adjective' => 'yummy' ), array ( 'before' => '%' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = ':a :b \:a :a' ;
$expected = '1 2 :a 1' ;
$result = String :: insert ( $string , array ( 'a' => 1 , 'b' => 2 ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = ':a :b :c' ;
$expected = '2 3' ;
$result = String :: insert ( $string , array ( 'b' => 2 , 'c' => 3 ), array ( 'clean' => true ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = ':a :b :c' ;
$expected = '1 3' ;
$result = String :: insert ( $string , array ( 'a' => 1 , 'c' => 3 ), array ( 'clean' => true ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = ':a :b :c' ;
$expected = '2 3' ;
$result = String :: insert ( $string , array ( 'b' => 2 , 'c' => 3 ), array ( 'clean' => true ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = ':a, :b and :c' ;
$expected = '2 and 3' ;
$result = String :: insert ( $string , array ( 'b' => 2 , 'c' => 3 ), array ( 'clean' => true ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '":a, :b and :c"' ;
$expected = '"1, 2"' ;
$result = String :: insert ( $string , array ( 'a' => 1 , 'b' => 2 ), array ( 'clean' => true ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '"${a}, ${b} and ${c}"' ;
$expected = '"1, 2"' ;
$result = String :: insert ( $string , array ( 'a' => 1 , 'b' => 2 ), array ( 'before' => '${' , 'after' => '}' , 'clean' => true ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '<img src=":src" alt=":alt" class="foo :extra bar"/>' ;
$expected = '<img src="foo" class="foo bar"/>' ;
$result = String :: insert ( $string , array ( 'src' => 'foo' ), array ( 'clean' => 'html' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '<img src=":src" class=":no :extra"/>' ;
$expected = '<img src="foo"/>' ;
$result = String :: insert ( $string , array ( 'src' => 'foo' ), array ( 'clean' => 'html' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$string = '<img src=":src" class=":no :extra"/>' ;
$expected = '<img src="foo" class="bar"/>' ;
$result = String :: insert ( $string , array ( 'src' => 'foo' , 'extra' => 'bar' ), array ( 'clean' => 'html' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-31 12:36:38 +00:00
$result = String :: insert ( " this is a ? string " , " test " );
$expected = " this is a test string " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-07-05 14:31:22 +00:00
2008-07-03 02:19:19 +00:00
$result = String :: insert ( " this is a ? string with a ? ? ? " , array ( 'long' , 'few?' , 'params' , 'you know' ));
$expected = " this is a long string with a few? params you know " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-07-07 23:24:39 +00:00
2013-08-07 23:03:21 +00:00
$result = String :: insert ( 'update saved_urls set url = :url where id = :id' , array ( 'url' => 'http://www.testurl.com/param1:url/param2:id' , 'id' => 1 ));
2008-07-07 23:24:39 +00:00
$expected = " update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-07-07 23:24:39 +00:00
2008-11-08 02:58:37 +00:00
$result = String :: insert ( 'update saved_urls set url = :url where id = :id' , array ( 'id' => 1 , 'url' => 'http://www.testurl.com/param1:url/param2:id' ));
2008-07-07 23:24:39 +00:00
$expected = " update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-08 02:58:37 +00:00
$result = String :: insert ( ':me cake. :subject :verb fantastic.' , array ( 'me' => 'I :verb' , 'subject' => 'cake' , 'verb' => 'is' ));
2008-07-07 23:24:39 +00:00
$expected = " I :verb cake. cake is fantastic. " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-08 02:58:37 +00:00
2008-10-18 23:56:41 +00:00
$result = String :: insert ( ':I.am: :not.yet: passing.' , array ( 'I.am' => 'We are' ), array ( 'before' => ':' , 'after' => ':' , 'clean' => array ( 'replacement' => ' of course' , 'method' => 'text' )));
$expected = " We are of course passing. " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-07-07 23:24:39 +00:00
2009-03-09 20:03:02 +00:00
$result = String :: insert (
':I.am: :not.yet: passing.' ,
array ( 'I.am' => 'We are' ),
array ( 'before' => ':' , 'after' => ':' , 'clean' => true )
);
2008-10-18 23:56:41 +00:00
$expected = " We are passing. " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-03-09 20:03:02 +00:00
$result = String :: insert ( '?-pended result' , array ( 'Pre' ));
$expected = " Pre-pended result " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-08-03 08:10:14 +00:00
$string = 'switching :timeout / :timeout_count' ;
$expected = 'switching 5 / 10' ;
$result = String :: insert ( $string , array ( 'timeout' => 5 , 'timeout_count' => 10 ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-08-03 08:10:14 +00:00
$string = 'switching :timeout / :timeout_count' ;
$expected = 'switching 5 / 10' ;
$result = String :: insert ( $string , array ( 'timeout_count' => 10 , 'timeout' => 5 ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-08-03 08:10:14 +00:00
$string = 'switching :timeout_count by :timeout' ;
$expected = 'switching 10 by 5' ;
$result = String :: insert ( $string , array ( 'timeout' => 5 , 'timeout_count' => 10 ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-08-03 08:10:14 +00:00
$string = 'switching :timeout_count by :timeout' ;
$expected = 'switching 10 by 5' ;
$result = String :: insert ( $string , array ( 'timeout_count' => 10 , 'timeout' => 5 ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-10-18 23:56:41 +00:00
}
2009-05-01 21:05:46 +00:00
2008-10-18 23:56:41 +00:00
/**
* test Clean Insert
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2011-05-30 20:02:32 +00:00
public function testCleanInsert () {
2009-05-01 21:05:46 +00:00
$result = String :: cleanInsert ( ':incomplete' , array (
'clean' => true , 'before' => ':' , 'after' => ''
));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '' , $result );
2008-11-08 02:58:37 +00:00
2008-10-18 23:56:41 +00:00
$result = String :: cleanInsert ( ':incomplete' , array (
2008-11-08 02:58:37 +00:00
'clean' => array ( 'method' => 'text' , 'replacement' => 'complete' ),
2008-10-18 23:56:41 +00:00
'before' => ':' , 'after' => '' )
);
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'complete' , $result );
2008-11-08 02:58:37 +00:00
2009-05-01 21:05:46 +00:00
$result = String :: cleanInsert ( ':in.complete' , array (
'clean' => true , 'before' => ':' , 'after' => ''
));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '' , $result );
2008-11-08 02:58:37 +00:00
2009-05-01 21:05:46 +00:00
$result = String :: cleanInsert ( ':in.complete and' , array (
'clean' => true , 'before' => ':' , 'after' => '' )
);
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '' , $result );
2008-11-08 02:58:37 +00:00
2009-05-01 21:05:46 +00:00
$result = String :: cleanInsert ( ':in.complete or stuff' , array (
'clean' => true , 'before' => ':' , 'after' => ''
));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'stuff' , $result );
2008-11-08 02:58:37 +00:00
2009-05-01 21:05:46 +00:00
$result = String :: cleanInsert (
'<p class=":missing" id=":missing">Text here</p>' ,
array ( 'clean' => 'html' , 'before' => ':' , 'after' => '' )
);
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '<p>Text here</p>' , $result );
2008-05-30 11:40:08 +00:00
}
2009-05-01 21:05:46 +00:00
/**
* Tests that non - insertable variables ( i . e . arrays ) are skipped when used as values in
* String :: insert () .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAutoIgnoreBadInsertData () {
2009-05-01 21:05:46 +00:00
$data = array ( 'foo' => 'alpha' , 'bar' => 'beta' , 'fale' => array ());
$result = String :: insert ( '(:foo > :bar || :fale!)' , $data , array ( 'clean' => 'text' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '(alpha > beta || !)' , $result );
2009-05-01 21:05:46 +00:00
}
2008-07-03 02:19:19 +00:00
/**
2008-06-02 19:22:55 +00:00
* testTokenize method
2008-07-05 14:31:22 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testTokenize () {
2008-05-30 11:40:08 +00:00
$result = String :: tokenize ( 'A,(short,boring test)' );
$expected = array ( 'A' , '(short,boring test)' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$result = String :: tokenize ( 'A,(short,more interesting( test)' );
$expected = array ( 'A' , '(short,more interesting( test)' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$result = String :: tokenize ( 'A,(short,very interesting( test))' );
$expected = array ( 'A' , '(short,very interesting( test))' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$result = String :: tokenize ( '"single tag"' , ' ' , '"' , '"' );
$expected = array ( '"single tag"' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-07-05 14:31:22 +00:00
2008-05-30 11:40:08 +00:00
$result = String :: tokenize ( 'tagA "single tag" tagB' , ' ' , '"' , '"' );
$expected = array ( 'tagA' , '"single tag"' , 'tagB' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2013-12-19 14:58:08 +00:00
$result = String :: tokenize ( '' );
$expected = array ();
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
}
2011-05-16 22:49:00 +00:00
2011-05-30 20:02:32 +00:00
public function testReplaceWithQuestionMarkInString () {
2009-10-13 15:15:17 +00:00
$string = ':a, :b and :c?' ;
$expected = '2 and 3?' ;
$result = String :: insert ( $string , array ( 'b' => 2 , 'c' => 3 ), array ( 'clean' => true ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-10-13 15:15:17 +00:00
}
2010-10-15 02:32:56 +00:00
2013-10-01 17:11:44 +00:00
/**
* test that wordWrap () works the same as built - in wordwrap function
*
* @ dataProvider wordWrapProvider
* @ return void
*/
public function testWordWrap ( $text , $width , $break = " \n " , $cut = false ) {
$result = String :: wordWrap ( $text , $width , $break , $cut );
$expected = wordwrap ( $text , $width , $break , $cut );
$this -> assertTextEquals ( $expected , $result , 'Text not wrapped same as built-in function.' );
}
/**
* data provider for testWordWrap method
*
* @ return array
*/
public function wordWrapProvider () {
return array (
array (
'The quick brown fox jumped over the lazy dog.' ,
33
),
array (
'A very long woooooooooooord.' ,
8
),
array (
'A very long woooooooooooord. Right.' ,
8
),
);
}
/**
* test that wordWrap () properly handle unicode strings .
*
* @ return void
*/
public function testWordWrapUnicodeAware () {
$text = 'Н о вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.' ;
$result = String :: wordWrap ( $text , 33 , " \n " , true );
$expected = <<< TEXT
Н о вим омниюм факёльиси элыктрам ,
мюнырэ лэгыры векж ыт . Выльёт квю
андо нюмквуам ты кюм . Зыд эю рыбю
м .
TEXT ;
$this -> assertTextEquals ( $expected , $result , 'Text not wrapped.' );
$text = 'Н о вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.' ;
$result = String :: wordWrap ( $text , 33 , " \n " );
$expected = <<< TEXT
Н о вим омниюм факёльиси элыктрам ,
мюнырэ лэгыры векж ыт . Выльёт
квюандо нюмквуам ты кюм . Зыд эю
рыбюм .
TEXT ;
$this -> assertTextEquals ( $expected , $result , 'Text not wrapped.' );
}
2010-10-15 02:32:56 +00:00
/**
* test wrap method .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testWrap () {
2010-10-15 02:32:56 +00:00
$text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.' ;
$result = String :: wrap ( $text , 33 );
$expected = <<< TEXT
This is the song that never ends .
This is the song that never ends .
This is the song that never ends .
TEXT ;
2012-01-25 02:12:26 +00:00
$this -> assertTextEquals ( $expected , $result , 'Text not wrapped.' );
2010-10-15 02:32:56 +00:00
$result = String :: wrap ( $text , array ( 'width' => 20 , 'wordWrap' => false ));
2013-08-07 23:14:31 +00:00
$expected = 'This is the song th' . " \n " .
'at never ends. This' . " \n " .
' is the song that n' . " \n " .
'ever ends. This is ' . " \n " .
'the song that never' . " \n " .
' ends.' ;
2012-01-25 02:12:26 +00:00
$this -> assertTextEquals ( $expected , $result , 'Text not wrapped.' );
2013-10-01 17:11:44 +00:00
$text = 'Н о вим омниюм факёльиси элыктрам, мюнырэ лэгыры векж ыт. Выльёт квюандо нюмквуам ты кюм. Зыд эю рыбюм.' ;
$result = String :: wrap ( $text , 33 );
$expected = <<< TEXT
Н о вим омниюм факёльиси элыктрам ,
мюнырэ лэгыры векж ыт . Выльёт
квюандо нюмквуам ты кюм . Зыд эю
рыбюм .
TEXT ;
$this -> assertTextEquals ( $expected , $result , 'Text not wrapped.' );
2010-10-15 02:32:56 +00:00
}
2010-10-16 02:46:03 +00:00
/**
* test wrap () indenting
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testWrapIndent () {
2010-10-16 02:46:03 +00:00
$text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.' ;
$result = String :: wrap ( $text , array ( 'width' => 33 , 'indent' => " \t " , 'indentAt' => 1 ));
$expected = <<< TEXT
This is the song that never ends .
This is the song that never ends .
This is the song that never ends .
TEXT ;
2012-01-25 02:12:26 +00:00
$this -> assertTextEquals ( $expected , $result );
2010-10-16 02:46:03 +00:00
}
2012-02-06 10:35:19 +00:00
/**
* testTruncate method
*
* @ return void
*/
public function testTruncate () {
$text1 = 'The quick brown fox jumps over the lazy dog' ;
$text2 = 'Heizölrückstoßabdämpfung' ;
$text3 = '<b>© 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener' ;
$text4 = '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great, or?' ;
$text5 = '0<b>1<i>2<span class="myclass">3</span>4<u>5</u>6</i>7</b>8<b>9</b>0' ;
$text6 = '<p><strong>Extra dates have been announced for this year\'s tour.</strong></p><p>Tickets for the new shows in</p>' ;
$text7 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?' ;
$text8 = 'Vive la R' . chr ( 195 ) . chr ( 169 ) . 'publique de France' ;
$text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь' ;
2012-02-23 11:17:58 +00:00
$text10 = 'http://example.com/something/foo:bar' ;
2012-02-06 10:35:19 +00:00
2012-11-10 03:32:29 +00:00
$elipsis = " \xe2 \x80 \xa6 " ;
2012-02-06 10:35:19 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text1 , 15 ), 'The quick br...' );
$this -> assertSame ( $this -> Text -> truncate ( $text1 , 15 , array ( 'exact' => false )), 'The quick...' );
$this -> assertSame ( $this -> Text -> truncate ( $text1 , 100 ), 'The quick brown fox jumps over the lazy dog' );
$this -> assertSame ( $this -> Text -> truncate ( $text2 , 10 ), 'Heiz&ou...' );
$this -> assertSame ( $this -> Text -> truncate ( $text2 , 10 , array ( 'exact' => false )), '...' );
$this -> assertSame ( $this -> Text -> truncate ( $text3 , 20 ), '<b>© 2005-20...' );
$this -> assertSame ( $this -> Text -> truncate ( $text4 , 15 ), '<img src="my...' );
2012-08-04 17:58:23 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text5 , 6 , array ( 'ellipsis' => '' )), '0<b>1<' );
2012-11-10 03:32:29 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text1 , 15 , array ( 'html' => true )), 'The quick brow' . $elipsis );
$this -> assertSame ( $this -> Text -> truncate ( $text1 , 15 , array ( 'exact' => false , 'html' => true )), 'The quick' . $elipsis );
$this -> assertSame ( $this -> Text -> truncate ( $text2 , 10 , array ( 'html' => true )), 'Heizölrüc' . $elipsis );
$this -> assertSame ( $this -> Text -> truncate ( $text2 , 10 , array ( 'exact' => false , 'html' => true )), $elipsis );
$this -> assertSame ( $this -> Text -> truncate ( $text3 , 20 , array ( 'html' => true )), '<b>© 2005-2007, Cake S' . $elipsis . '</b>' );
$this -> assertSame ( $this -> Text -> truncate ( $text4 , 15 , array ( 'html' => true )), '<img src="mypic.jpg"> This image ta' . $elipsis );
$this -> assertSame ( $this -> Text -> truncate ( $text4 , 45 , array ( 'html' => true )), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the' . $elipsis . '</b>' );
$this -> assertSame ( $this -> Text -> truncate ( $text4 , 90 , array ( 'html' => true )), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great,' . $elipsis );
2012-08-04 17:58:23 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text5 , 6 , array ( 'ellipsis' => '' , 'html' => true )), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>' );
$this -> assertSame ( $this -> Text -> truncate ( $text5 , 20 , array ( 'ellipsis' => '' , 'html' => true )), $text5 );
2012-11-10 03:32:29 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text6 , 57 , array ( 'exact' => false , 'html' => true )), " <p><strong>Extra dates have been announced for this year's " . $elipsis . " </strong></p> " );
2012-02-06 10:35:19 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text7 , 255 ), $text7 );
$this -> assertSame ( $this -> Text -> truncate ( $text7 , 15 ), 'El moño está...' );
2012-03-14 02:59:20 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text8 , 15 ), 'Vive la R' . chr ( 195 ) . chr ( 169 ) . 'pu...' );
2012-02-06 10:35:19 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text9 , 10 ), 'НОПРСТУ...' );
2012-02-23 11:17:58 +00:00
$this -> assertSame ( $this -> Text -> truncate ( $text10 , 30 ), 'http://example.com/somethin...' );
2012-02-06 10:35:19 +00:00
$text = '<p><span style="font-size: medium;"><a>Iamatestwithnospacesandhtml</a></span></p>' ;
$result = $this -> Text -> truncate ( $text , 10 , array (
2012-08-04 17:58:23 +00:00
'ellipsis' => '...' ,
2012-02-06 10:35:19 +00:00
'exact' => false ,
'html' => true
));
$expected = '<p><span style="font-size: medium;"><a>...</a></span></p>' ;
$this -> assertEquals ( $expected , $result );
$text = ' < p >< span style = " font-size: medium; " > El biógrafo de Steve Jobs , Walter
Isaacson , explica porqué Jobs le pidió que le hiciera su biografía en
este artículo de El País .</ span ></ p >
< p >< span style = " font-size: medium; " >< span style = " font-size:
large ; " >Por qué Steve era distinto.</span></span></p>
< p >< span style = " font-size: medium; " >< a href = " http://www.elpais.com/
articulo / primer / plano / Steve / era / distinto / elpepueconeg /
20111009 elpneglse_4 / Tes " >http://www.elpais.com/articulo/primer/plano/
Steve / era / distinto / elpepueconeg / 20111009 elpneglse_4 / Tes </ a ></ span ></ p >
< p >< span style = " font-size: medium; " > Ya se ha publicado la biografía de
Steve Jobs escrita por Walter Isaacson " <strong>Steve Jobs by Walter
Isaacson </ strong > " , aquí os dejamos la dirección de amazon donde
podeís adquirirla .</ span ></ p >
< p >< span style = " font-size: medium; " >< a > http :// www . amazon . com / Steve -
Jobs - Walter - Isaacson / dp / 1451648537 </ a ></ span ></ p > ' ;
$result = $this -> Text -> truncate ( $text , 500 , array (
2012-08-04 17:58:23 +00:00
'ellipsis' => '... ' ,
2012-02-06 10:35:19 +00:00
'exact' => false ,
'html' => true
));
$expected = ' < p >< span style = " font-size: medium; " > El biógrafo de Steve Jobs , Walter
Isaacson , explica porqué Jobs le pidió que le hiciera su biografía en
este artículo de El País .</ span ></ p >
< p >< span style = " font-size: medium; " >< span style = " font-size:
large ; " >Por qué Steve era distinto.</span></span></p>
< p >< span style = " font-size: medium; " >< a href = " http://www.elpais.com/
articulo / primer / plano / Steve / era / distinto / elpepueconeg /
20111009 elpneglse_4 / Tes " >http://www.elpais.com/articulo/primer/plano/
Steve / era / distinto / elpepueconeg / 20111009 elpneglse_4 / Tes </ a ></ span ></ p >
< p >< span style = " font-size: medium; " > Ya se ha publicado la biografía de
Steve Jobs escrita por Walter Isaacson " <strong>Steve Jobs by Walter
Isaacson </ strong > " , aquí os dejamos la dirección de amazon donde
podeís adquirirla .</ span ></ p >
< p >< span style = " font-size: medium; " >< a >... </ a ></ span ></ p > ' ;
$this -> assertEquals ( $expected , $result );
2012-08-04 17:58:23 +00:00
// test deprecated `ending` (`ellipsis` taking precedence if both are defined)
$result = $this -> Text -> truncate ( $text1 , 31 , array (
'ending' => '.' ,
'exact' => false ,
));
$expected = 'The quick brown fox jumps.' ;
$this -> assertEquals ( $expected , $result );
$result = $this -> Text -> truncate ( $text1 , 31 , array (
'ellipsis' => '..' ,
'ending' => '.' ,
'exact' => false ,
));
$expected = 'The quick brown fox jumps..' ;
$this -> assertEquals ( $expected , $result );
2012-02-06 10:35:19 +00:00
}
2012-08-17 11:57:47 +00:00
/**
* testTruncate method with non utf8 sites
*
* @ return void
*/
public function testTruncateLegacy () {
Configure :: write ( 'App.encoding' , 'ISO-8859-1' );
$text = '<b>© 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener' ;
$result = $this -> Text -> truncate ( $text , 31 , array (
2012-08-17 12:02:20 +00:00
'html' => true ,
2012-08-17 11:57:47 +00:00
'exact' => false ,
));
2012-08-17 12:02:20 +00:00
$expected = '<b>© 2005-2007, Cake Software...</b>' ;
2012-08-17 11:57:47 +00:00
$this -> assertEquals ( $expected , $result );
2012-08-27 02:17:36 +00:00
2012-08-17 11:57:47 +00:00
$result = $this -> Text -> truncate ( $text , 31 , array (
2012-08-17 12:02:20 +00:00
'html' => true ,
2012-08-17 11:57:47 +00:00
'exact' => true ,
));
2012-08-17 12:02:20 +00:00
$expected = '<b>© 2005-2007, Cake Software F...</b>' ;
2012-08-17 11:57:47 +00:00
$this -> assertEquals ( $expected , $result );
}
2012-08-02 11:21:31 +00:00
/**
* testTail method
*
* @ return void
*/
public function testTail () {
$text1 = 'The quick brown fox jumps over the lazy dog' ;
$text2 = 'Heizölrückstoßabdämpfung' ;
$text3 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?' ;
$text4 = 'Vive la R' . chr ( 195 ) . chr ( 169 ) . 'publique de France' ;
$text5 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь' ;
$result = $this -> Text -> tail ( $text1 , 13 );
$this -> assertEquals ( '...e lazy dog' , $result );
$result = $this -> Text -> tail ( $text1 , 13 , array ( 'exact' => false ));
$this -> assertEquals ( '...lazy dog' , $result );
$result = $this -> Text -> tail ( $text1 , 100 );
$this -> assertEquals ( 'The quick brown fox jumps over the lazy dog' , $result );
$result = $this -> Text -> tail ( $text2 , 10 );
$this -> assertEquals ( '...;mpfung' , $result );
$result = $this -> Text -> tail ( $text2 , 10 , array ( 'exact' => false ));
$this -> assertEquals ( '...' , $result );
$result = $this -> Text -> tail ( $text3 , 255 );
$this -> assertEquals ( $text3 , $result );
$result = $this -> Text -> tail ( $text3 , 21 );
$this -> assertEquals ( '...á dicho la verdad?' , $result );
$result = $this -> Text -> tail ( $text4 , 25 );
$this -> assertEquals ( '...a R' . chr ( 195 ) . chr ( 169 ) . 'publique de France' , $result );
$result = $this -> Text -> tail ( $text5 , 10 );
$this -> assertEquals ( '...цчшщъыь' , $result );
2012-08-04 17:58:23 +00:00
$result = $this -> Text -> tail ( $text5 , 6 , array ( 'ellipsis' => '' ));
$this -> assertEquals ( 'чшщъыь' , $result );
2012-08-02 11:21:31 +00:00
}
2012-02-06 10:35:19 +00:00
/**
* testHighlight method
*
* @ return void
*/
public function testHighlight () {
$text = 'This is a test text' ;
$phrases = array ( 'This' , 'text' );
$result = $this -> Text -> highlight ( $text , $phrases , array ( 'format' => '<b>\1</b>' ));
$expected = '<b>This</b> is a test <b>text</b>' ;
$this -> assertEquals ( $expected , $result );
2012-04-07 02:56:01 +00:00
2012-03-16 21:20:00 +00:00
$phrases = array ( 'is' , 'text' );
$result = $this -> Text -> highlight ( $text , $phrases , array ( 'format' => '<b>\1</b>' , 'regex' => " | \ b%s \ b|iu " ));
$expected = 'This <b>is</b> a test <b>text</b>' ;
$this -> assertEquals ( $expected , $result );
2012-02-06 10:35:19 +00:00
$text = 'This is a test text' ;
$phrases = null ;
$result = $this -> Text -> highlight ( $text , $phrases , array ( 'format' => '<b>\1</b>' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $text , $result );
2012-02-06 10:35:19 +00:00
$text = 'This is a (test) text' ;
$phrases = '(test' ;
$result = $this -> Text -> highlight ( $text , $phrases , array ( 'format' => '<b>\1</b>' ));
$this -> assertEquals ( 'This is a <b>(test</b>) text' , $result );
$text = 'Ich saß in einem Café am Übergang' ;
$expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>' ;
$phrases = array ( 'saß' , 'café' , 'übergang' );
$result = $this -> Text -> highlight ( $text , $phrases , array ( 'format' => '<b>\1</b>' ));
$this -> assertEquals ( $expected , $result );
}
/**
* testHighlightHtml method
*
* @ return void
*/
public function testHighlightHtml () {
$text1 = '<p>strongbow isn’t real cider</p>' ;
$text2 = '<p>strongbow <strong>isn’t</strong> real cider</p>' ;
$text3 = '<img src="what-a-strong-mouse.png" alt="What a strong mouse!" />' ;
$text4 = 'What a strong mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />' ;
$options = array ( 'format' => '<b>\1</b>' , 'html' => true );
$expected = '<p><b>strong</b>bow isn’t real cider</p>' ;
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $expected , $this -> Text -> highlight ( $text1 , 'strong' , $options ));
2012-02-06 10:35:19 +00:00
$expected = '<p><b>strong</b>bow <strong>isn’t</strong> real cider</p>' ;
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $expected , $this -> Text -> highlight ( $text2 , 'strong' , $options ));
2012-02-06 10:35:19 +00:00
$this -> assertEquals ( $this -> Text -> highlight ( $text3 , 'strong' , $options ), $text3 );
$this -> assertEquals ( $this -> Text -> highlight ( $text3 , array ( 'strong' , 'what' ), $options ), $text3 );
$expected = '<b>What</b> a <b>strong</b> mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />' ;
2014-01-09 23:33:27 +00:00
$this -> assertEquals ( $expected , $this -> Text -> highlight ( $text4 , array ( 'strong' , 'what' ), $options ));
2012-02-06 10:35:19 +00:00
}
/**
* testHighlightMulti method
*
* @ return void
*/
public function testHighlightMulti () {
$text = 'This is a test text' ;
$phrases = array ( 'This' , 'text' );
$result = $this -> Text -> highlight ( $text , $phrases , array ( 'format' => array ( '<b>\1</b>' , '<em>\1</em>' )));
$expected = '<b>This</b> is a test <em>text</em>' ;
$this -> assertEquals ( $expected , $result );
}
/**
* testStripLinks method
*
* @ return void
*/
public function testStripLinks () {
$text = 'This is a test text' ;
$expected = 'This is a test text' ;
$result = $this -> Text -> stripLinks ( $text );
$this -> assertEquals ( $expected , $result );
$text = 'This is a <a href="#">test</a> text' ;
$expected = 'This is a test text' ;
$result = $this -> Text -> stripLinks ( $text );
$this -> assertEquals ( $expected , $result );
$text = 'This <strong>is</strong> a <a href="#">test</a> <a href="#">text</a>' ;
$expected = 'This <strong>is</strong> a test text' ;
$result = $this -> Text -> stripLinks ( $text );
$this -> assertEquals ( $expected , $result );
$text = 'This <strong>is</strong> a <a href="#">test</a> and <abbr>some</abbr> other <a href="#">text</a>' ;
$expected = 'This <strong>is</strong> a test and <abbr>some</abbr> other text' ;
$result = $this -> Text -> stripLinks ( $text );
$this -> assertEquals ( $expected , $result );
}
/**
* testHighlightCaseInsensitivity method
*
* @ return void
*/
public function testHighlightCaseInsensitivity () {
$text = 'This is a Test text' ;
$expected = 'This is a <b>Test</b> text' ;
$result = $this -> Text -> highlight ( $text , 'test' , array ( 'format' => '<b>\1</b>' ));
$this -> assertEquals ( $expected , $result );
$result = $this -> Text -> highlight ( $text , array ( 'test' ), array ( 'format' => '<b>\1</b>' ));
$this -> assertEquals ( $expected , $result );
}
/**
* testExcerpt method
*
* @ return void
*/
public function testExcerpt () {
$text = 'This is a phrase with test text to play with' ;
$expected = '...ase with test text to ...' ;
$result = $this -> Text -> excerpt ( $text , 'test' , 9 , '...' );
$this -> assertEquals ( $expected , $result );
$expected = 'This is a...' ;
$result = $this -> Text -> excerpt ( $text , 'not_found' , 9 , '...' );
$this -> assertEquals ( $expected , $result );
$expected = 'This is a phras...' ;
$result = $this -> Text -> excerpt ( $text , null , 9 , '...' );
$this -> assertEquals ( $expected , $result );
$expected = $text ;
$result = $this -> Text -> excerpt ( $text , null , 200 , '...' );
$this -> assertEquals ( $expected , $result );
$expected = '...a phrase w...' ;
$result = $this -> Text -> excerpt ( $text , 'phrase' , 2 , '...' );
$this -> assertEquals ( $expected , $result );
$phrase = 'This is a phrase with test text' ;
$expected = $text ;
$result = $this -> Text -> excerpt ( $text , $phrase , 13 , '...' );
$this -> assertEquals ( $expected , $result );
$text = 'aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa' ;
$phrase = 'bbbbbbbb' ;
$result = $this -> Text -> excerpt ( $text , $phrase , 10 );
$expected = '...aaaaaaaaaabbbbbbbbaaaaaaaaaa...' ;
$this -> assertEquals ( $expected , $result );
}
/**
* testExcerptCaseInsensitivity method
*
* @ return void
*/
public function testExcerptCaseInsensitivity () {
$text = 'This is a phrase with test text to play with' ;
$expected = '...ase with test text to ...' ;
$result = $this -> Text -> excerpt ( $text , 'TEST' , 9 , '...' );
$this -> assertEquals ( $expected , $result );
$expected = 'This is a...' ;
$result = $this -> Text -> excerpt ( $text , 'NOT_FOUND' , 9 , '...' );
$this -> assertEquals ( $expected , $result );
}
/**
* testListGeneration method
*
* @ return void
*/
public function testListGeneration () {
$result = $this -> Text -> toList ( array ());
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '' , $result );
2012-02-06 10:35:19 +00:00
$result = $this -> Text -> toList ( array ( 'One' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'One' , $result );
2012-02-06 10:35:19 +00:00
$result = $this -> Text -> toList ( array ( 'Larry' , 'Curly' , 'Moe' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'Larry, Curly and Moe' , $result );
2012-02-06 10:35:19 +00:00
$result = $this -> Text -> toList ( array ( 'Dusty' , 'Lucky' , 'Ned' ), 'y' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'Dusty, Lucky y Ned' , $result );
2012-02-06 10:35:19 +00:00
$result = $this -> Text -> toList ( array ( 1 => 'Dusty' , 2 => 'Lucky' , 3 => 'Ned' ), 'y' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'Dusty, Lucky y Ned' , $result );
2012-02-06 10:35:19 +00:00
$result = $this -> Text -> toList ( array ( 1 => 'Dusty' , 2 => 'Lucky' , 3 => 'Ned' ), 'and' , ' + ' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'Dusty + Lucky and Ned' , $result );
2012-02-06 10:35:19 +00:00
$result = $this -> Text -> toList ( array ( 'name1' => 'Dusty' , 'name2' => 'Lucky' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'Dusty and Lucky' , $result );
2012-02-06 10:35:19 +00:00
$result = $this -> Text -> toList ( array ( 'test_0' => 'banana' , 'test_1' => 'apple' , 'test_2' => 'lemon' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'banana, apple and lemon' , $result );
2012-02-06 10:35:19 +00:00
}
2008-05-30 11:40:08 +00:00
}