* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice * * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests * @package Cake.Test.Case.View.Helper * @since CakePHP(tm) v 1.2.0.4206 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ App::uses('View', 'View'); App::uses('TextHelper', 'View/Helper'); /** * Class TextHelperTestObject * * @package Cake.Test.Case.View.Helper */ class TextHelperTestObject extends TextHelper { public function attach(CakeTextMock $string) { $this->_engine = $string; } public function engine() { return $this->_engine; } } /** * CakeTextMock class * * @package Cake.Test.Case.View.Helper */ class CakeTextMock { } /** * TextHelperTest class * * @package Cake.Test.Case.View.Helper */ class TextHelperTest extends CakeTestCase { /** * setUp method * * @return void */ public function setUp() { parent::setUp(); $this->View = new View(null); $this->Text = new TextHelper($this->View); } /** * tearDown method * * @return void */ public function tearDown() { unset($this->View); parent::tearDown(); } /** * test String class methods are called correctly * * @return void */ public function testTextHelperProxyMethodCalls() { $methods = array( 'highlight', 'stripLinks', 'truncate', 'tail', 'excerpt', 'toList', ); $CakeText = $this->getMock('CakeTextMock', $methods); $Text = new TextHelperTestObject($this->View, array('engine' => 'CakeTextMock')); $Text->attach($CakeText); foreach ($methods as $method) { $CakeText->expects($this->at(0))->method($method); $Text->{$method}('who', 'what', 'when', 'where', 'how'); } } /** * test engine override * * @return void */ public function testEngineOverride() { App::build(array( 'Utility' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS) ), App::REGISTER); $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) )); CakePlugin::load('TestPlugin'); $Text = new TextHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine')); $this->assertInstanceOf('TestPluginEngine', $Text->engine()); CakePlugin::unload('TestPlugin'); } /** * testAutoLink method * * @return void */ public function testAutoLink() { $text = 'This is a test text'; $expected = 'This is a test text'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $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 www.cakephp.org URL and test@cakephp\.org email address'; $this->assertRegExp('#^' . $expected . '$#', $result); $text = 'This is a test text with URL http://www.cakephp.org'; $expected = 'This is a test text with URL http://www.cakephp.org'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = 'This is a test text with URL http://www.cakephp.org and some more text'; $expected = 'This is a test text with URL http://www.cakephp.org and some more text'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = "This is a test text with URL http://www.cakephp.org\tand some more text"; $expected = "This is a test text with URL http://www.cakephp.org\tand some more text"; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = 'This is a test text with URL http://www.cakephp.org(and some more text)'; $expected = 'This is a test text with URL http://www.cakephp.org(and some more text)'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = 'This is a test text with URL (http://www.cakephp.org/page/4) in brackets'; $expected = 'This is a test text with URL (http://www.cakephp.org/page/4) in brackets'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = 'This is a test text with URL [http://www.cakephp.org/page/4] in square brackets'; $expected = 'This is a test text with URL [http://www.cakephp.org/page/4] in square brackets'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = 'This is a test text with URL [http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3] in square brackets'; $expected = 'This is a test text with URL [http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3] in square brackets'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = 'This is a test text with URL ;http://www.cakephp.org/page/4; semi-colon'; $expected = 'This is a test text with URL ;http://www.cakephp.org/page/4; semi-colon'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = 'This is a test text with URL (http://www.cakephp.org/page/4/other(thing)) brackets'; $expected = 'This is a test text with URL (http://www.cakephp.org/page/4/other(thing)) brackets'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); } /** * Test mixing URLs and Email addresses in one confusing string. * * @return void */ public function testAutoLinkMixed() { $text = 'Text with a url/email http://example.com/store?email=mark@example.com and email.'; $expected = 'Text with a url/email ' . 'http://example.com/store?email=mark@example.com and email.'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); } /** * test autoLink() and options. * * @return void */ public function testAutoLinkOptions() { $text = 'This is a test text with URL http://www.cakephp.org'; $expected = 'This is a test text with URL http://www.cakephp.org'; $result = $this->Text->autoLink($text, array('class' => 'link')); $this->assertEquals($expected, $result); $text = 'This is a test text with URL http://www.cakephp.org'; $expected = 'This is a test text with URL http://www.cakephp.org'; $result = $this->Text->autoLink($text, array('class' => 'link', 'id' => 'MyLink')); $this->assertEquals($expected, $result); } /** * Test escaping for autoLink * * @return void */ public function testAutoLinkEscape() { $text = 'This is a test text with URL http://www.cakephp.org'; $expected = 'This is a <b>test</b> text with URL http://www.cakephp.org'; $result = $this->Text->autoLink($text); $this->assertEquals($expected, $result); $text = 'This is a test text with URL http://www.cakephp.org'; $expected = 'This is a test text with URL http://www.cakephp.org'; $result = $this->Text->autoLink($text, array('escape' => false)); $this->assertEquals($expected, $result); $text = 'test
mark@example.com
', '', array('escape' => false) ), array( 'Some mark@example.com Text', 'Some mark@example.com Text', array('escape' => false) ), ); } /** * testAutoLinkEmails method * * @param string $text The text to link * @param string $expected The expected results. * @dataProvider autoLinkEmailProvider * @return void */ public function testAutoLinkEmails($text, $expected, $attrs = array()) { $result = $this->Text->autoLinkEmails($text, $attrs); $this->assertEquals($expected, $result); } /** * test invalid email addresses. * * @return void */ public function testAutoLinkEmailInvalid() { $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test'); $expected = 'this is a myaddress@gmx-de test'; $this->assertEquals($expected, $result); } /** * testAutoParagraph method * * @return void */ public function testAutoParagraph() { $text = 'This is a test text'; $expected = <<test text
TEXT; $result = $this->Text->autoParagraph($text); $this->assertTextEquals($expected, $result); $result = $this->Text->autoParagraph($text); $text = 'This is atest text
TEXT; $result = $this->Text->autoParagraph($text); $this->assertTextEquals($expected, $result); $text = <<This is a new line.
TEXT; $result = $this->Text->autoParagraph($text); $this->assertTextEquals($expected, $result); } }