_engine = $string; } public function engine() { return $this->_engine; } } /** * StringMock class * */ class StringMock { } /** * TextHelperTest class * */ class TextHelperTest extends TestCase { /** * setUp method * * @return void */ public function setUp() { parent::setUp(); $this->View = new View(); $this->Text = new TextHelper($this->View); $this->_appNamespace = Configure::read('App.namespace'); Configure::write('App.namespace', 'TestApp'); } /** * tearDown method * * @return void */ public function tearDown() { unset($this->Text, $this->View); Configure::write('App.namespace', $this->_appNamespace); parent::tearDown(); } /** * test String class methods are called correctly * * @return void */ public function testTextHelperProxyMethodCalls() { $methods = [ 'stripLinks', 'excerpt', 'toList' ]; $String = $this->getMock(__NAMESPACE__ . '\StringMock', $methods); $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']); $Text->attach($String); foreach ($methods as $method) { $String->expects($this->at(0))->method($method); $Text->{$method}('who', 'what', 'when', 'where', 'how'); } $methods = [ 'highlight', 'truncate' ]; $String = $this->getMock(__NAMESPACE__ . '\StringMock', $methods); $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']); $Text->attach($String); foreach ($methods as $method) { $String->expects($this->at(0))->method($method); $Text->{$method}('who', ['what']); } $methods = [ 'tail' ]; $String = $this->getMock(__NAMESPACE__ . '\StringMock', $methods); $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']); $Text->attach($String); foreach ($methods as $method) { $String->expects($this->at(0))->method($method); $Text->{$method}('who', 1, ['what']); } } /** * test engine override * * @return void */ public function testEngineOverride() { $Text = new TextHelperTestObject($this->View, ['engine' => 'TestAppEngine']); $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Text->engine()); Plugin::load('TestPlugin'); $Text = new TextHelperTestObject($this->View, ['engine' => 'TestPlugin.TestPluginEngine']); $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Text->engine()); Plugin::unload('TestPlugin'); } /** * testAutoLink method * * @return void */ public function testAutoLink() { $text = 'The AWWWARD show happened today'; $result = $this->Text->autoLink($text); $this->assertEquals($text, $result); $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, ['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, ['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, ['escape' => false]); $this->assertEquals($expected, $result); $text = 'test test'; $expected = 'test test'; $result = $this->Text->autoLink($text, ['escape' => false]); $this->assertEquals($expected, $result); } /** * Data provider for autoLinking * * @return array */ public static function autoLinkProvider() { return [ [ 'This is a test text', 'This is a test text', ], [ 'This is a test that includes (www.cakephp.org)', 'This is a test that includes (www.cakephp.org)', ], [ 'This is a test that includes www.cakephp.org:8080', 'This is a test that includes www.cakephp.org:8080', ], [ 'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment', 'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment', ], [ 'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment', 'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment', ], [ 'This is a test that includes http://example.com/test.php?foo=bar text', 'This is a test that includes http://example.com/test.php?foo=bar text', ], [ 'This is a test that includes www.example.com/test.php?foo=bar text', 'This is a test that includes www.example.com/test.php?foo=bar text', ], [ 'Text with a partial www.cakephp.org URL', 'Text with a partial www.cakephp.org URL', ], [ 'Text with a partial WWW.cakephp.org URL', 'Text with a partial WWW.cakephp.org URL', ], [ 'Text with a partial WWW.cakephp.org ©, URL', 'Text with a partial WWW.cakephp.org &copy, URL', ], [ 'Text with a url www.cot.ag/cuIb2Q and more', 'Text with a url www.cot.ag/cuIb2Q and more', ], [ 'Text with a url http://www.does--not--work.com and more', 'Text with a url http://www.does--not--work.com and more', ], [ 'Text with a url http://www.not--work.com and more', 'Text with a url http://www.not--work.com and more', ], [ 'Text with a url http://www.sub_domain.domain.pl and more', 'Text with a url http://www.sub_domain.domain.pl and more', ], [ 'Text with a partial www.küchenschöhn-not-working.de URL', 'Text with a partial www.küchenschöhn-not-working.de URL' ], [ 'Text with a partial http://www.küchenschöhn-not-working.de URL', 'Text with a partial http://www.küchenschöhn-not-working.de URL' ], ]; } /** * testAutoLinkUrls method * * @dataProvider autoLinkProvider * @return void */ public function testAutoLinkUrls($text, $expected) { $result = $this->Text->autoLinkUrls($text); $this->assertEquals($expected, $result); } /** * Test the options for autoLinkUrls * * @return void */ public function testAutoLinkUrlsOptions() { $text = 'Text with a partial www.cakephp.org URL'; $expected = 'Text with a partial www.cakephp.org URL'; $result = $this->Text->autoLinkUrls($text, ['class' => 'link']); $this->assertRegExp('#^' . $expected . '$#', $result); $text = 'Text with a partial WWW.cakephp.org © URL'; $expected = 'Text with a partial WWW.cakephp.org © URL'; $result = $this->Text->autoLinkUrls($text, ['escape' => false]); $this->assertRegExp('#^' . $expected . '$#', $result); } /** * Test autoLinkUrls with the escape option. * * @return void */ public function testAutoLinkUrlsEscape() { $text = 'Text with a partial http://www.example.com link'; $expected = 'Text with a partial http://www.example.com link'; $result = $this->Text->autoLinkUrls($text, ['escape' => false]); $this->assertEquals($expected, $result); $text = 'Text with a partial www.example.com link'; $expected = 'Text with a partial www.example.com link'; $result = $this->Text->autoLinkUrls($text, ['escape' => false]); $this->assertEquals($expected, $result); $text = 'Text with a partial link link'; $expected = 'Text with a partial link link'; $result = $this->Text->autoLinkUrls($text, ['escape' => false]); $this->assertEquals($expected, $result); $text = 'Text with a partial