2008-05-30 11:40:08 +00:00
< ? php
/**
2009-03-18 17:55:58 +00:00
* TextHelperTest file
2008-05-30 11:40:08 +00:00
*
2010-10-03 16:31:21 +00:00
* PHP 5
2008-05-30 11:40:08 +00:00
*
2012-04-27 02:49:18 +00:00
* CakePHP ( tm ) Tests < http :// book . cakephp . org / 2.0 / en / development / testing . html >
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
*
2010-10-03 16:31:21 +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
2010-10-03 16:31:21 +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 . View . Helper
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0.4206
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
*/
2010-12-11 05:47:55 +00:00
2010-12-10 06:23:27 +00:00
App :: uses ( 'View' , 'View' );
2010-12-11 05:47:55 +00:00
App :: uses ( 'TextHelper' , 'View/Helper' );
2009-07-24 19:18:37 +00:00
2013-05-30 22:11:14 +00:00
/**
* Class TextHelperTestObject
*
* @ package Cake . Test . Case . View . Helper
*/
2012-02-06 14:09:04 +00:00
class TextHelperTestObject extends TextHelper {
2012-02-13 02:12:29 +00:00
public function attach ( StringMock $string ) {
2012-02-14 16:14:18 +00:00
$this -> _engine = $string ;
2012-02-06 14:09:04 +00:00
}
2012-02-14 15:56:48 +00:00
public function engine () {
2012-02-14 16:14:18 +00:00
return $this -> _engine ;
2012-02-14 15:56:48 +00:00
}
2012-02-06 14:09:04 +00:00
}
2012-02-13 02:12:29 +00:00
/**
* StringMock class
2013-05-30 22:11:14 +00:00
*
* @ package Cake . Test . Case . View . Helper
2012-02-13 02:12:29 +00:00
*/
class StringMock {
}
2008-05-30 11:40:08 +00:00
/**
2009-03-18 17:55:58 +00:00
* TextHelperTest class
2008-05-30 11:40:08 +00:00
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . View . Helper
2008-05-30 11:40:08 +00:00
*/
2009-03-18 17:55:58 +00:00
class TextHelperTest extends CakeTestCase {
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* setUp method
2008-11-08 00:07:17 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function setUp () {
2012-02-13 02:12:29 +00:00
parent :: setUp ();
$this -> View = new View ( null );
2010-07-03 18:41:34 +00:00
$this -> Text = new TextHelper ( $this -> View );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2009-03-18 17:55:58 +00:00
/**
* tearDown method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function tearDown () {
2012-02-13 02:12:29 +00:00
unset ( $this -> View );
parent :: tearDown ();
2009-03-18 17:55:58 +00:00
}
2009-07-24 19:18:37 +00:00
2012-02-06 14:09:04 +00:00
/**
* test String class methods are called correctly
*/
public function testTextHelperProxyMethodCalls () {
$methods = array (
'highlight' , 'stripLinks' , 'truncate' , 'excerpt' , 'toList' ,
);
2012-02-13 02:12:29 +00:00
$String = $this -> getMock ( 'StringMock' , $methods );
$Text = new TextHelperTestObject ( $this -> View , array ( 'engine' => 'StringMock' ));
$Text -> attach ( $String );
2012-02-06 14:09:04 +00:00
foreach ( $methods as $method ) {
2012-02-13 02:12:29 +00:00
$String -> expects ( $this -> at ( 0 )) -> method ( $method );
$Text -> { $method }( 'who' , 'what' , 'when' , 'where' , 'how' );
2012-02-06 14:09:04 +00:00
}
}
2012-02-14 15:56:48 +00:00
/**
* test engine override
*/
public function testEngineOverride () {
App :: build ( array (
'Utility' => array ( CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS )
2012-02-18 12:04:54 +00:00
), App :: REGISTER );
2012-02-14 15:56:48 +00:00
$Text = new TextHelperTestObject ( $this -> View , array ( 'engine' => 'TestAppEngine' ));
$this -> assertInstanceOf ( 'TestAppEngine' , $Text -> engine ());
App :: build ( array (
'Plugin' => array ( CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS )
2012-02-18 12:04:54 +00:00
));
2012-02-14 15:56:48 +00:00
CakePlugin :: load ( 'TestPlugin' );
$Text = new TextHelperTestObject ( $this -> View , array ( 'engine' => 'TestPlugin.TestPluginEngine' ));
$this -> assertInstanceOf ( 'TestPluginEngine' , $Text -> engine ());
CakePlugin :: unload ( 'TestPlugin' );
}
2008-06-02 19:22:55 +00:00
/**
* testAutoLink method
2008-11-08 00:07:17 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAutoLink () {
2008-05-30 11:40:08 +00:00
$text = 'This is a test text' ;
$expected = 'This is a test text' ;
$result = $this -> Text -> autoLink ( $text );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address' ;
$result = $this -> Text -> autoLink ( $text );
$expected = 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL and <a href="mailto:test@cakephp\.org">test@cakephp\.org</a> email address' ;
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2010-04-13 18:33:24 +00:00
$text = 'This is a test text with URL http://www.cakephp.org' ;
$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>' ;
$result = $this -> Text -> autoLink ( $text );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-04-13 18:33:24 +00:00
$text = 'This is a test text with URL http://www.cakephp.org and some more text' ;
$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text' ;
$result = $this -> Text -> autoLink ( $text );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-04-13 18:33:24 +00:00
$text = " This is a test text with URL http://www.cakephp.org \t and some more text " ;
$expected = " This is a test text with URL <a href= \" http://www.cakephp.org \" >http://www.cakephp.org</a> \t and some more text " ;
$result = $this -> Text -> autoLink ( $text );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-04-13 18:33:24 +00:00
$text = 'This is a test text with URL http://www.cakephp.org(and some more text)' ;
$expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)' ;
$result = $this -> Text -> autoLink ( $text );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-04-13 18:33:24 +00:00
$text = 'This is a test text with URL http://www.cakephp.org' ;
$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>' ;
2011-12-01 07:21:31 +00:00
$result = $this -> Text -> autoLink ( $text , array ( 'class' => 'link' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-04-13 18:33:24 +00:00
$text = 'This is a test text with URL http://www.cakephp.org' ;
$expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>' ;
2011-12-01 07:21:31 +00:00
$result = $this -> Text -> autoLink ( $text , array ( 'class' => 'link' , 'id' => 'MyLink' ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2011-10-29 16:08:43 +00:00
/**
* Test escaping for autoLink
*
* @ return void
*/
public function testAutoLinkEscape () {
$text = 'This is a <b>test</b> text with URL http://www.cakephp.org' ;
$expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>' ;
$result = $this -> Text -> autoLink ( $text );
2011-12-13 13:54:05 +00:00
$this -> assertEquals ( $expected , $result );
2011-10-29 16:08:43 +00:00
$text = 'This is a <b>test</b> text with URL http://www.cakephp.org' ;
$expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>' ;
$result = $this -> Text -> autoLink ( $text , array ( 'escape' => false ));
2011-12-13 13:54:05 +00:00
$this -> assertEquals ( $expected , $result );
2013-06-16 02:01:29 +00:00
$text = ' test < ul >
< li > lorem : http :// example . org ? some </ li >
< li > ipsum : http :// othersite . com / abc </ li >
</ ul > test ' ;
$expected = ' test < ul >
< li > lorem : < a href = " http://example.org?some " > http :// example . org ? some </ a ></ li >
< li > ipsum : < a href = " http://othersite.com/abc " > http :// othersite . com / abc </ a ></ li >
</ ul > test ' ;
$result = $this -> Text -> autoLink ( $text , array ( 'escape' => false ));
$this -> assertEquals ( $expected , $result );
2011-10-29 16:08:43 +00:00
}
2012-10-11 12:25:15 +00:00
/**
* Data provider for autoLinking
*/
public static function autoLinkProvider () {
return array (
array (
'This is a test text' ,
'This is a test text' ,
),
array (
'This is a test that includes (www.cakephp.org)' ,
'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)' ,
),
array (
'This is a test that includes www.cakephp.org:8080' ,
'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>' ,
),
array (
'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment' ,
'This is a test that includes <a href="http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>' ,
),
array (
'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment' ,
'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>' ,
),
array (
'This is a test that includes http://example.com/test.php?foo=bar text' ,
'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text' ,
),
array (
'This is a test that includes www.example.com/test.php?foo=bar text' ,
'This is a test that includes <a href="http://www.example.com/test.php?foo=bar">www.example.com/test.php?foo=bar</a> text' ,
),
array (
'Text with a partial www.cakephp.org URL' ,
'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL' ,
),
array (
'Text with a partial WWW.cakephp.org URL' ,
'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL' ,
),
array (
'Text with a partial WWW.cakephp.org ©, URL' ,
'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &copy, URL' ,
),
array (
'Text with a url www.cot.ag/cuIb2Q and more' ,
'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more' ,
),
array (
'Text with a url http://www.does--not--work.com and more' ,
'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more' ,
),
array (
'Text with a url http://www.not--work.com and more' ,
'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more' ,
),
2013-09-27 12:48:57 +00:00
array (
'Text with a partial www.küchenschöhn-not-working.de URL' ,
'Text with a partial <a href="http://www.küchenschöhn-not-working.de">www.küchenschöhn-not-working.de</a> URL'
),
array (
'Text with a partial http://www.küchenschöhn-not-working.de URL' ,
'Text with a partial <a href="http://www.küchenschöhn-not-working.de">http://www.küchenschöhn-not-working.de</a> URL'
)
2012-10-11 12:25:15 +00:00
);
}
2008-06-02 19:22:55 +00:00
/**
* testAutoLinkUrls method
2008-11-08 00:07:17 +00:00
*
2012-10-11 12:25:15 +00:00
* @ dataProvider autoLinkProvider
2008-06-02 19:22:55 +00:00
* @ return void
*/
2012-10-11 12:25:15 +00:00
public function testAutoLinkUrls ( $text , $expected ) {
2012-10-11 12:16:51 +00:00
$result = $this -> Text -> autoLinkUrls ( $text );
$this -> assertEquals ( $expected , $result );
2012-10-11 12:25:15 +00:00
}
2012-10-11 12:16:51 +00:00
2012-10-11 12:25:15 +00:00
/**
* Test the options for autoLinkUrls
*
* @ return void
*/
public function testAutoLinkUrlsOptions () {
2008-05-30 11:40:08 +00:00
$text = 'Text with a partial www.cakephp.org URL' ;
$expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL' ;
$result = $this -> Text -> autoLinkUrls ( $text , array ( 'class' => 'link' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2008-05-30 11:40:08 +00:00
$text = 'Text with a partial WWW.cakephp.org © URL' ;
2010-06-22 01:17:37 +00:00
$expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> © URL' ;
2008-05-30 11:40:08 +00:00
$result = $this -> Text -> autoLinkUrls ( $text , array ( 'escape' => false ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2011-10-29 16:08:43 +00:00
}
2011-10-10 19:35:39 +00:00
2011-10-29 16:08:43 +00:00
/**
* Test autoLinkUrls with the escape option .
*
* @ return void
*/
public function testAutoLinkUrlsEscape () {
2011-10-10 19:35:39 +00:00
$text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link' ;
$expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link' ;
$result = $this -> Text -> autoLinkUrls ( $text , array ( 'escape' => false ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2011-10-10 19:35:39 +00:00
$text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link' ;
$expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link' ;
$result = $this -> Text -> autoLinkUrls ( $text , array ( 'escape' => false ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2011-10-29 16:08:43 +00:00
$text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link' ;
$expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link' ;
$result = $this -> Text -> autoLinkUrls ( $text , array ( 'escape' => true ));
2011-11-17 02:31:16 +00:00
$this -> assertEquals ( $expected , $result );
2012-02-15 02:55:21 +00:00
2012-02-14 13:08:37 +00:00
$text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more' ;
2012-02-15 02:55:21 +00:00
$expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more' ;
2012-02-14 13:08:37 +00:00
$result = $this -> Text -> autoLinkUrls ( $text );
$this -> assertEquals ( $expected , $result );
$text = 'Text with a url www.not-working-www.com and more' ;
$expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more' ;
$result = $this -> Text -> autoLinkUrls ( $text );
$this -> assertEquals ( $expected , $result );
$text = 'Text with a url http://www.not-working-www.com and more' ;
$expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more' ;
$result = $this -> Text -> autoLinkUrls ( $text );
$this -> assertEquals ( $expected , $result );
$text = 'Text with a url http://www.www.not-working-www.com and more' ;
$expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more' ;
$result = $this -> Text -> autoLinkUrls ( $text );
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2013-04-30 21:02:07 +00:00
/**
* Test autoLinkUrls with query strings .
*
* @ return void
*/
public function testAutoLinkUrlsQueryString () {
$text = 'Text with a partial http://www.cakephp.org?product_id=123&foo=bar link' ;
$expected = 'Text with a partial <a href="http://www.cakephp.org?product_id=123&foo=bar">http://www.cakephp.org?product_id=123&foo=bar</a> link' ;
$result = $this -> Text -> autoLinkUrls ( $text );
$this -> assertEquals ( $expected , $result );
}
2008-06-02 19:22:55 +00:00
/**
* testAutoLinkEmails method
2008-11-08 00:07:17 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAutoLinkEmails () {
2008-05-30 11:40:08 +00:00
$text = 'This is a test text' ;
$expected = 'This is a test text' ;
$result = $this -> Text -> autoLinkUrls ( $text );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$text = 'Text with email@example.com address' ;
$expected = 'Text with <a href="mailto:email@example.com"\s*>email@example.com</a> address' ;
$result = $this -> Text -> autoLinkEmails ( $text );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2011-05-16 22:49:00 +00:00
2011-01-17 16:11:30 +00:00
$text = " Text with o'hare._-bob@example.com address " ;
$expected = 'Text with <a href="mailto:o'hare._-bob@example.com">o'hare._-bob@example.com</a> address' ;
$result = $this -> Text -> autoLinkEmails ( $text );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$text = 'Text with email@example.com address' ;
$expected = 'Text with <a href="mailto:email@example.com" \s*class="link">email@example.com</a> address' ;
$result = $this -> Text -> autoLinkEmails ( $text , array ( 'class' => 'link' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2013-09-27 12:56:53 +00:00
$text = 'Text with düsentrieb@küchenschöhn-not-working.de address' ;
$expected = 'Text with <a href="mailto:düsentrieb@küchenschöhn-not-working.de">düsentrieb@küchenschöhn-not-working.de</a> address' ;
$result = $this -> Text -> autoLinkEmails ( $text );
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2013-09-27 13:01:46 +00:00
$text = 'Text with me@subdomain.küchenschöhn.de address' ;
$expected = 'Text with <a href="mailto:me@subdomain.küchenschöhn.de">me@subdomain.küchenschöhn.de</a> address' ;
$result = $this -> Text -> autoLinkEmails ( $text );
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2011-03-27 23:09:46 +00:00
/**
* test invalid email addresses .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAutoLinkEmailInvalid () {
2011-03-27 23:09:46 +00:00
$result = $this -> Text -> autoLinkEmails ( 'this is a myaddress@gmx-de test' );
$expected = 'this is a myaddress@gmx-de test' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2011-03-27 23:09:46 +00:00
}
2012-12-14 18:58:23 +00:00
/**
* testAutoParagraph method
*
* @ return void
*/
public function testAutoParagraph () {
$text = 'This is a test text' ;
$expected = <<< TEXT
< p > This is a test text </ p >
TEXT ;
2013-03-31 20:46:52 +00:00
$result = $this -> Text -> autoParagraph ( $text );
$text = 'This is a <br/> <BR> test text' ;
$expected = <<< TEXT
2012-12-14 18:58:23 +00:00
< p > This is a </ p >
< p > test text </ p >
TEXT ;
2013-03-31 20:46:52 +00:00
$result = $this -> Text -> autoParagraph ( $text );
2013-09-27 12:48:57 +00:00
$this -> assertTextEquals ( $expected , $result );
2013-03-31 20:46:52 +00:00
$result = $this -> Text -> autoParagraph ( $text );
$text = 'This is a <BR id="test"/><br class="test"> test text' ;
$expected = <<< TEXT
2012-12-14 18:58:23 +00:00
< p > This is a </ p >
< p > test text </ p >
TEXT ;
2013-03-31 20:46:52 +00:00
$result = $this -> Text -> autoParagraph ( $text );
2013-09-27 12:48:57 +00:00
$this -> assertTextEquals ( $expected , $result );
2012-12-14 18:58:23 +00:00
$text = <<< TEXT
This is a test text .
This is a line return .
TEXT ;
$expected = <<< TEXT
< p > This is a test text .< br />
This is a line return .</ p >
TEXT ;
$result = $this -> Text -> autoParagraph ( $text );
2013-09-27 12:48:57 +00:00
$this -> assertTextEquals ( $expected , $result );
2012-12-14 18:58:23 +00:00
$text = <<< TEXT
This is a test text .
This is a new line .
TEXT ;
$expected = <<< TEXT
< p > This is a test text .</ p >
< p > This is a new line .</ p >
TEXT ;
$result = $this -> Text -> autoParagraph ( $text );
2013-09-27 12:48:57 +00:00
$this -> assertTextEquals ( $expected , $result );
2012-12-14 18:58:23 +00:00
}
2008-05-30 11:40:08 +00:00
}