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 12:31:21 -04:00
* PHP 5
2008-05-30 11:40:08 +00:00
*
2010-05-18 22:15:13 -03:00
* CakePHP ( tm ) Tests < http :// book . cakephp . org / view / 1196 / Testing >
2011-05-29 17:31:39 -04:00
* Copyright 2005 - 2011 , Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
2010-10-03 12:31:21 -04:00
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
2008-05-30 11:40:08 +00:00
*
2011-05-29 17:31:39 -04:00
* @ copyright Copyright 2005 - 2011 , Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2010-05-18 22:15:13 -03:00
* @ link http :// book . cakephp . org / view / 1196 / Testing CakePHP ( tm ) Tests
2011-07-26 01:46:14 -04:30
* @ package Cake . Test . Case . View . Helper
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0.4206
2010-10-03 12:27:27 -04:00
* @ license MIT License ( http :// www . opensource . org / licenses / mit - license . php )
2008-05-30 11:40:08 +00:00
*/
2010-12-11 01:17:55 -04:30
2010-12-10 01:53:27 -04:30
App :: uses ( 'View' , 'View' );
2010-12-11 01:17:55 -04:30
App :: uses ( 'TextHelper' , 'View/Helper' );
2009-07-24 21:18:37 +02:00
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 01:46:14 -04:30
* @ 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 21:18:37 +02: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 22:02:32 +02:00
public function setUp () {
2010-07-03 14:41:34 -04:00
$controller = null ;
$this -> View = new View ( $controller );
$this -> Text = new TextHelper ( $this -> View );
2008-05-30 11:40:08 +00:00
}
2009-07-24 21:18:37 +02:00
2009-03-18 17:55:58 +00:00
/**
* tearDown method
*
* @ return void
*/
2011-05-30 22:02:32 +02:00
public function tearDown () {
2010-07-03 14:41:34 -04:00
unset ( $this -> View , $this -> Text );
2009-03-18 17:55:58 +00:00
}
2009-07-24 21:18:37 +02:00
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 22:02:32 +02: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-15 16:07:56 -08: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-15 16:07:56 -08:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2010-04-13 15:33:24 -03: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-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2010-04-13 15:33:24 -03: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-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2010-04-13 15:33:24 -03: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-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2010-04-13 15:33:24 -03: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-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2010-04-13 15:33:24 -03: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-11-30 23:21:31 -08:00
$result = $this -> Text -> autoLink ( $text , array ( 'class' => 'link' ));
2011-11-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2010-04-13 15:33:24 -03: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-11-30 23:21:31 -08:00
$result = $this -> Text -> autoLink ( $text , array ( 'class' => 'link' , 'id' => 'MyLink' ));
2011-11-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
}
2009-07-24 21:18:37 +02:00
2011-10-29 12:08:43 -04: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 20:54:05 +07:00
$this -> assertEquals ( $expected , $result );
2011-10-29 12:08:43 -04: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 20:54:05 +07:00
$this -> assertEquals ( $expected , $result );
2011-10-29 12:08:43 -04:00
}
2008-06-02 19:22:55 +00:00
/**
* testAutoLinkUrls method
2008-11-08 00:07:17 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 22:02:32 +02:00
public function testAutoLinkUrls () {
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-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$text = 'This is a test that includes (www.cakephp.org)' ;
$expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)' ;
$result = $this -> Text -> autoLinkUrls ( $text );
2011-11-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
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*>www.cakephp.org</a> URL' ;
$result = $this -> Text -> autoLinkUrls ( $text );
2011-11-15 16:07:56 -08:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
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-15 16:07:56 -08:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2008-05-30 11:40:08 +00:00
$text = 'Text with a partial WWW.cakephp.org URL' ;
2010-06-21 21:17:37 -04: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 );
2011-11-15 16:07:56 -08:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2008-05-30 11:40:08 +00:00
$text = 'Text with a partial WWW.cakephp.org © URL' ;
2010-06-21 21:17:37 -04: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-15 16:07:56 -08:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2008-05-30 11:40:08 +00:00
2010-06-21 21:17:37 -04:00
$text = 'Text with a url www.cot.ag/cuIb2Q and more' ;
$expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more' ;
$result = $this -> Text -> autoLinkUrls ( $text );
2011-11-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2011-05-17 00:49:00 +02:00
2011-01-18 18:56:35 -05:00
$text = 'Text with a url http://www.does--not--work.com and more' ;
$expected = 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more' ;
$result = $this -> Text -> autoLinkUrls ( $text );
2011-11-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2011-05-17 00:49:00 +02:00
2011-01-18 18:56:35 -05:00
$text = 'Text with a url http://www.not--work.com and more' ;
$expected = 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more' ;
$result = $this -> Text -> autoLinkUrls ( $text );
2011-11-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2011-10-29 12:08:43 -04:00
}
2011-10-10 15:35:39 -04:00
2011-10-29 12:08:43 -04:00
/**
* Test autoLinkUrls with the escape option .
*
* @ return void
*/
public function testAutoLinkUrlsEscape () {
2011-10-10 15:35:39 -04: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-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2011-10-10 15:35:39 -04: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-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2011-10-29 12:08:43 -04: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-16 21:31:16 -05:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
}
2009-07-24 21:18:37 +02:00
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 22:02:32 +02: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-15 16:07:56 -08: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-15 16:07:56 -08:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2011-05-17 00:49:00 +02:00
2011-01-17 11:11:30 -05: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-15 16:07:56 -08: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-15 16:07:56 -08:00
$this -> assertRegExp ( '#^' . $expected . '$#' , $result );
2008-05-30 11:40:08 +00:00
}
2009-07-24 21:18:37 +02:00
2011-03-27 19:09:46 -04:00
/**
* test invalid email addresses .
*
* @ return void
*/
2011-05-30 22:02:32 +02:00
public function testAutoLinkEmailInvalid () {
2011-03-27 19:09:46 -04:00
$result = $this -> Text -> autoLinkEmails ( 'this is a myaddress@gmx-de test' );
$expected = 'this is a myaddress@gmx-de test' ;
2011-11-15 16:07:56 -08:00
$this -> assertEquals ( $expected , $result );
2011-03-27 19:09:46 -04:00
}
2008-05-30 11:40:08 +00:00
}