TextHelperTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\View\Helper\TextHelper;
  21. use Cake\View\View;
  22. /**
  23. * Class TextHelperTestObject
  24. *
  25. */
  26. class TextHelperTestObject extends TextHelper
  27. {
  28. public function attach(StringMock $string)
  29. {
  30. $this->_engine = $string;
  31. }
  32. public function engine()
  33. {
  34. return $this->_engine;
  35. }
  36. }
  37. /**
  38. * StringMock class
  39. *
  40. */
  41. class StringMock
  42. {
  43. }
  44. /**
  45. * TextHelperTest class
  46. *
  47. */
  48. class TextHelperTest extends TestCase
  49. {
  50. /**
  51. * setUp method
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. parent::setUp();
  58. $this->View = new View();
  59. $this->Text = new TextHelper($this->View);
  60. $this->_appNamespace = Configure::read('App.namespace');
  61. Configure::write('App.namespace', 'TestApp');
  62. }
  63. /**
  64. * tearDown method
  65. *
  66. * @return void
  67. */
  68. public function tearDown()
  69. {
  70. unset($this->Text, $this->View);
  71. Configure::write('App.namespace', $this->_appNamespace);
  72. parent::tearDown();
  73. }
  74. /**
  75. * test String class methods are called correctly
  76. *
  77. * @return void
  78. */
  79. public function testTextHelperProxyMethodCalls()
  80. {
  81. $methods = [
  82. 'stripLinks', 'excerpt', 'toList'
  83. ];
  84. $String = $this->getMock(__NAMESPACE__ . '\StringMock', $methods);
  85. $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']);
  86. $Text->attach($String);
  87. foreach ($methods as $method) {
  88. $String->expects($this->at(0))->method($method);
  89. $Text->{$method}('who', 'what', 'when', 'where', 'how');
  90. }
  91. $methods = [
  92. 'highlight', 'truncate'
  93. ];
  94. $String = $this->getMock(__NAMESPACE__ . '\StringMock', $methods);
  95. $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']);
  96. $Text->attach($String);
  97. foreach ($methods as $method) {
  98. $String->expects($this->at(0))->method($method);
  99. $Text->{$method}('who', ['what']);
  100. }
  101. $methods = [
  102. 'tail'
  103. ];
  104. $String = $this->getMock(__NAMESPACE__ . '\StringMock', $methods);
  105. $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']);
  106. $Text->attach($String);
  107. foreach ($methods as $method) {
  108. $String->expects($this->at(0))->method($method);
  109. $Text->{$method}('who', 1, ['what']);
  110. }
  111. }
  112. /**
  113. * test engine override
  114. *
  115. * @return void
  116. */
  117. public function testEngineOverride()
  118. {
  119. $Text = new TextHelperTestObject($this->View, ['engine' => 'TestAppEngine']);
  120. $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Text->engine());
  121. Plugin::load('TestPlugin');
  122. $Text = new TextHelperTestObject($this->View, ['engine' => 'TestPlugin.TestPluginEngine']);
  123. $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Text->engine());
  124. Plugin::unload('TestPlugin');
  125. }
  126. /**
  127. * testAutoLink method
  128. *
  129. * @return void
  130. */
  131. public function testAutoLink()
  132. {
  133. $text = 'This is a test text';
  134. $expected = 'This is a test text';
  135. $result = $this->Text->autoLink($text);
  136. $this->assertEquals($expected, $result);
  137. $text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
  138. $result = $this->Text->autoLink($text);
  139. $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';
  140. $this->assertRegExp('#^' . $expected . '$#', $result);
  141. $text = 'This is a test text with URL http://www.cakephp.org';
  142. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  143. $result = $this->Text->autoLink($text);
  144. $this->assertEquals($expected, $result);
  145. $text = 'This is a test text with URL http://www.cakephp.org and some more text';
  146. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
  147. $result = $this->Text->autoLink($text);
  148. $this->assertEquals($expected, $result);
  149. $text = "This is a test text with URL http://www.cakephp.org\tand some more text";
  150. $expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
  151. $result = $this->Text->autoLink($text);
  152. $this->assertEquals($expected, $result);
  153. $text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
  154. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
  155. $result = $this->Text->autoLink($text);
  156. $this->assertEquals($expected, $result);
  157. }
  158. /**
  159. * Test mixing URLs and Email addresses in one confusing string.
  160. *
  161. * @return void
  162. */
  163. public function testAutoLinkMixed()
  164. {
  165. $text = 'Text with a url/email http://example.com/store?email=mark@example.com and email.';
  166. $expected = 'Text with a url/email <a href="http://example.com/store?email=mark@example.com">' .
  167. 'http://example.com/store?email=mark@example.com</a> and email.';
  168. $result = $this->Text->autoLink($text);
  169. $this->assertEquals($expected, $result);
  170. }
  171. /**
  172. * test autoLink() and options.
  173. *
  174. * @return void
  175. */
  176. public function testAutoLinkOptions()
  177. {
  178. $text = 'This is a test text with URL http://www.cakephp.org';
  179. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
  180. $result = $this->Text->autoLink($text, ['class' => 'link']);
  181. $this->assertEquals($expected, $result);
  182. $text = 'This is a test text with URL http://www.cakephp.org';
  183. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
  184. $result = $this->Text->autoLink($text, ['class' => 'link', 'id' => 'MyLink']);
  185. $this->assertEquals($expected, $result);
  186. }
  187. /**
  188. * Test escaping for autoLink
  189. *
  190. * @return void
  191. */
  192. public function testAutoLinkEscape()
  193. {
  194. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  195. $expected = 'This is a &lt;b&gt;test&lt;/b&gt; text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  196. $result = $this->Text->autoLink($text);
  197. $this->assertEquals($expected, $result);
  198. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  199. $expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  200. $result = $this->Text->autoLink($text, ['escape' => false]);
  201. $this->assertEquals($expected, $result);
  202. $text = 'test <ul>
  203. <li>lorem: http://example.org?some</li>
  204. <li>ipsum: http://othersite.com/abc</li>
  205. </ul> test';
  206. $expected = 'test <ul>
  207. <li>lorem: <a href="http://example.org?some">http://example.org?some</a></li>
  208. <li>ipsum: <a href="http://othersite.com/abc">http://othersite.com/abc</a></li>
  209. </ul> test';
  210. $result = $this->Text->autoLink($text, ['escape' => false]);
  211. $this->assertEquals($expected, $result);
  212. }
  213. /**
  214. * Data provider for autoLinking
  215. *
  216. * @return array
  217. */
  218. public static function autoLinkProvider()
  219. {
  220. return [
  221. [
  222. 'This is a test text',
  223. 'This is a test text',
  224. ],
  225. [
  226. 'This is a test that includes (www.cakephp.org)',
  227. 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)',
  228. ],
  229. [
  230. 'This is a test that includes www.cakephp.org:8080',
  231. 'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>',
  232. ],
  233. [
  234. 'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  235. '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>',
  236. ],
  237. [
  238. 'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  239. 'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
  240. ],
  241. [
  242. 'This is a test that includes http://example.com/test.php?foo=bar text',
  243. 'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text',
  244. ],
  245. [
  246. 'This is a test that includes www.example.com/test.php?foo=bar text',
  247. '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',
  248. ],
  249. [
  250. 'Text with a partial www.cakephp.org URL',
  251. 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL',
  252. ],
  253. [
  254. 'Text with a partial WWW.cakephp.org URL',
  255. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL',
  256. ],
  257. [
  258. 'Text with a partial WWW.cakephp.org &copy, URL',
  259. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &amp;copy, URL',
  260. ],
  261. [
  262. 'Text with a url www.cot.ag/cuIb2Q and more',
  263. 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more',
  264. ],
  265. [
  266. 'Text with a url http://www.does--not--work.com and more',
  267. 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more',
  268. ],
  269. [
  270. 'Text with a url http://www.not--work.com and more',
  271. 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more',
  272. ],
  273. [
  274. 'Text with a url http://www.sub_domain.domain.pl and more',
  275. 'Text with a url <a href="http://www.sub_domain.domain.pl">http://www.sub_domain.domain.pl</a> and more',
  276. ],
  277. [
  278. 'Text with a partial www.küchenschöhn-not-working.de URL',
  279. 'Text with a partial <a href="http://www.küchenschöhn-not-working.de">www.küchenschöhn-not-working.de</a> URL'
  280. ],
  281. [
  282. 'Text with a partial http://www.küchenschöhn-not-working.de URL',
  283. 'Text with a partial <a href="http://www.küchenschöhn-not-working.de">http://www.küchenschöhn-not-working.de</a> URL'
  284. ],
  285. ];
  286. }
  287. /**
  288. * testAutoLinkUrls method
  289. *
  290. * @dataProvider autoLinkProvider
  291. * @return void
  292. */
  293. public function testAutoLinkUrls($text, $expected)
  294. {
  295. $result = $this->Text->autoLinkUrls($text);
  296. $this->assertEquals($expected, $result);
  297. }
  298. /**
  299. * Test the options for autoLinkUrls
  300. *
  301. * @return void
  302. */
  303. public function testAutoLinkUrlsOptions()
  304. {
  305. $text = 'Text with a partial www.cakephp.org URL';
  306. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  307. $result = $this->Text->autoLinkUrls($text, ['class' => 'link']);
  308. $this->assertRegExp('#^' . $expected . '$#', $result);
  309. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  310. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  311. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  312. $this->assertRegExp('#^' . $expected . '$#', $result);
  313. }
  314. /**
  315. * Test autoLinkUrls with the escape option.
  316. *
  317. * @return void
  318. */
  319. public function testAutoLinkUrlsEscape()
  320. {
  321. $text = 'Text with a partial <a href="http://www.example.com">http://www.example.com</a> link';
  322. $expected = 'Text with a partial <a href="http://www.example.com">http://www.example.com</a> link';
  323. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  324. $this->assertEquals($expected, $result);
  325. $text = 'Text with a partial <a href="http://www.example.com">www.example.com</a> link';
  326. $expected = 'Text with a partial <a href="http://www.example.com">www.example.com</a> link';
  327. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  328. $this->assertEquals($expected, $result);
  329. $text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  330. $expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  331. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  332. $this->assertEquals($expected, $result);
  333. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  334. $expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  335. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  336. $this->assertEquals($expected, $result);
  337. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  338. $expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
  339. $result = $this->Text->autoLinkUrls($text, ['escape' => true]);
  340. $this->assertEquals($expected, $result);
  341. $text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  342. $expected = 'Text with a url &lt;a href=&quot;http://www.not-working-www.com&quot;&gt;www.not-working-www.com&lt;/a&gt; and more';
  343. $result = $this->Text->autoLinkUrls($text, ['escape' => true]);
  344. $this->assertEquals($expected, $result);
  345. $text = 'Text with a url www.not-working-www.com and more';
  346. $expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  347. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  348. $this->assertEquals($expected, $result);
  349. $text = 'Text with a url http://www.not-working-www.com and more';
  350. $expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
  351. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  352. $this->assertEquals($expected, $result);
  353. $text = 'Text with a url http://www.www.not-working-www.com and more';
  354. $expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
  355. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  356. $this->assertEquals($expected, $result);
  357. }
  358. /**
  359. * Test autoLinkUrls with query strings.
  360. *
  361. * @return void
  362. */
  363. public function testAutoLinkUrlsQueryString()
  364. {
  365. $text = 'Text with a partial http://www.cakephp.org?product_id=123&foo=bar link';
  366. $expected = 'Text with a partial <a href="http://www.cakephp.org?product_id=123&amp;foo=bar">http://www.cakephp.org?product_id=123&amp;foo=bar</a> link';
  367. $result = $this->Text->autoLinkUrls($text);
  368. $this->assertEquals($expected, $result);
  369. }
  370. /**
  371. * Data provider for autoLinkEmail.
  372. *
  373. * @return void
  374. */
  375. public function autoLinkEmailProvider()
  376. {
  377. return [
  378. [
  379. 'This is a test text',
  380. 'This is a test text',
  381. ],
  382. [
  383. 'email@example.com address',
  384. '<a href="mailto:email@example.com">email@example.com</a> address',
  385. ],
  386. [
  387. 'email@example.com address',
  388. '<a href="mailto:email@example.com">email@example.com</a> address',
  389. ],
  390. [
  391. '(email@example.com) address',
  392. '(<a href="mailto:email@example.com">email@example.com</a>) address',
  393. ],
  394. [
  395. 'Text with email@example.com address',
  396. 'Text with <a href="mailto:email@example.com">email@example.com</a> address',
  397. ],
  398. [
  399. "Text with o'hare._-bob@example.com address",
  400. 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address',
  401. ],
  402. [
  403. 'Text with düsentrieb@küchenschöhn-not-working.de address',
  404. 'Text with <a href="mailto:düsentrieb@küchenschöhn-not-working.de">düsentrieb@küchenschöhn-not-working.de</a> address',
  405. ],
  406. [
  407. 'Text with me@subdomain.küchenschöhn.de address',
  408. 'Text with <a href="mailto:me@subdomain.küchenschöhn.de">me@subdomain.küchenschöhn.de</a> address',
  409. ],
  410. [
  411. 'Text with email@example.com address',
  412. 'Text with <a href="mailto:email@example.com" class="link">email@example.com</a> address',
  413. ['class' => 'link'],
  414. ],
  415. [
  416. '<p>mark@example.com</p>',
  417. '<p><a href="mailto:mark@example.com">mark@example.com</a></p>',
  418. ['escape' => false]
  419. ],
  420. [
  421. 'Some&nbsp;mark@example.com&nbsp;Text',
  422. 'Some&nbsp;<a href="mailto:mark@example.com">mark@example.com</a>&nbsp;Text',
  423. ['escape' => false]
  424. ],
  425. ];
  426. }
  427. /**
  428. * testAutoLinkEmails method
  429. *
  430. * @param string $text The text to link
  431. * @param string $expected The expected results.
  432. * @dataProvider autoLinkEmailProvider
  433. * @return void
  434. */
  435. public function testAutoLinkEmails($text, $expected, $attrs = [])
  436. {
  437. $result = $this->Text->autoLinkEmails($text, $attrs);
  438. $this->assertEquals($expected, $result);
  439. }
  440. /**
  441. * test invalid email addresses.
  442. *
  443. * @return void
  444. */
  445. public function testAutoLinkEmailInvalid()
  446. {
  447. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  448. $expected = 'this is a myaddress@gmx-de test';
  449. $this->assertEquals($expected, $result);
  450. }
  451. /**
  452. * testAutoParagraph method
  453. *
  454. * @return void
  455. */
  456. public function testAutoParagraph()
  457. {
  458. $text = 'This is a test text';
  459. $expected = <<<TEXT
  460. <p>This is a test text</p>
  461. TEXT;
  462. $result = $this->Text->autoParagraph($text);
  463. $text = 'This is a <br/> <BR> test text';
  464. $expected = <<<TEXT
  465. <p>This is a </p>
  466. <p> test text</p>
  467. TEXT;
  468. $result = $this->Text->autoParagraph($text);
  469. $this->assertTextEquals($expected, $result);
  470. $result = $this->Text->autoParagraph($text);
  471. $text = 'This is a <BR id="test"/><br class="test"> test text';
  472. $expected = <<<TEXT
  473. <p>This is a </p>
  474. <p> test text</p>
  475. TEXT;
  476. $result = $this->Text->autoParagraph($text);
  477. $this->assertTextEquals($expected, $result);
  478. $text = <<<TEXT
  479. This is a test text.
  480. This is a line return.
  481. TEXT;
  482. $expected = <<<TEXT
  483. <p>This is a test text.<br />
  484. This is a line return.</p>
  485. TEXT;
  486. $result = $this->Text->autoParagraph($text);
  487. $this->assertTextEquals($expected, $result);
  488. $text = <<<TEXT
  489. This is a test text.
  490. This is a new line.
  491. TEXT;
  492. $expected = <<<TEXT
  493. <p>This is a test text.</p>
  494. <p>This is a new line.</p>
  495. TEXT;
  496. $result = $this->Text->autoParagraph($text);
  497. $this->assertTextEquals($expected, $result);
  498. }
  499. }