TextHelperTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\View\Helper\TextHelper;
  20. use Cake\View\View;
  21. /**
  22. * TextHelperTestObject
  23. */
  24. class TextHelperTestObject extends TextHelper
  25. {
  26. public function attach(StringMock $string)
  27. {
  28. $this->_engine = $string;
  29. }
  30. public function engine()
  31. {
  32. return $this->_engine;
  33. }
  34. }
  35. /**
  36. * StringMock class
  37. */
  38. class StringMock
  39. {
  40. }
  41. /**
  42. * TextHelperTest class
  43. */
  44. class TextHelperTest extends TestCase
  45. {
  46. /**
  47. * @var \Cake\View\Helper\TextHelper
  48. */
  49. public $Text;
  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. static::setAppNamespace();
  62. }
  63. /**
  64. * tearDown method
  65. *
  66. * @return void
  67. */
  68. public function tearDown()
  69. {
  70. unset($this->Text, $this->View);
  71. static::setAppNamespace($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->getMockBuilder(__NAMESPACE__ . '\StringMock')
  85. ->setMethods($methods)
  86. ->getMock();
  87. $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']);
  88. $Text->attach($String);
  89. foreach ($methods as $method) {
  90. $String->expects($this->at(0))->method($method);
  91. $Text->{$method}('who', 'what', 'when', 'where', 'how');
  92. }
  93. $methods = [
  94. 'highlight', 'truncate'
  95. ];
  96. $String = $this->getMockBuilder(__NAMESPACE__ . '\StringMock')
  97. ->setMethods($methods)
  98. ->getMock();
  99. $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']);
  100. $Text->attach($String);
  101. foreach ($methods as $method) {
  102. $String->expects($this->at(0))->method($method);
  103. $Text->{$method}('who', ['what']);
  104. }
  105. $methods = [
  106. 'tail'
  107. ];
  108. $String = $this->getMockBuilder(__NAMESPACE__ . '\StringMock')
  109. ->setMethods($methods)
  110. ->getMock();
  111. $Text = new TextHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\StringMock']);
  112. $Text->attach($String);
  113. foreach ($methods as $method) {
  114. $String->expects($this->at(0))->method($method);
  115. $Text->{$method}('who', 1, ['what']);
  116. }
  117. }
  118. /**
  119. * test engine override
  120. *
  121. * @return void
  122. */
  123. public function testEngineOverride()
  124. {
  125. $Text = new TextHelperTestObject($this->View, ['engine' => 'TestAppEngine']);
  126. $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Text->engine());
  127. Plugin::load('TestPlugin');
  128. $Text = new TextHelperTestObject($this->View, ['engine' => 'TestPlugin.TestPluginEngine']);
  129. $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Text->engine());
  130. Plugin::unload('TestPlugin');
  131. }
  132. /**
  133. * testAutoLink method
  134. *
  135. * @return void
  136. */
  137. public function testAutoLink()
  138. {
  139. $text = 'The AWWWARD show happened today';
  140. $result = $this->Text->autoLink($text);
  141. $this->assertEquals($text, $result);
  142. $text = 'This is a test text';
  143. $expected = 'This is a test text';
  144. $result = $this->Text->autoLink($text);
  145. $this->assertEquals($expected, $result);
  146. $text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
  147. $result = $this->Text->autoLink($text);
  148. $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';
  149. $this->assertRegExp('#^' . $expected . '$#', $result);
  150. $text = 'This is a test text with URL http://www.cakephp.org';
  151. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  152. $result = $this->Text->autoLink($text);
  153. $this->assertEquals($expected, $result);
  154. $text = 'This is a test text with URL http://www.cakephp.org and some more text';
  155. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
  156. $result = $this->Text->autoLink($text);
  157. $this->assertEquals($expected, $result);
  158. $text = "This is a test text with URL http://www.cakephp.org\tand some more text";
  159. $expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
  160. $result = $this->Text->autoLink($text);
  161. $this->assertEquals($expected, $result);
  162. $text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
  163. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
  164. $result = $this->Text->autoLink($text);
  165. $this->assertEquals($expected, $result);
  166. $text = 'This is a test text with URL (http://www.cakephp.org/page/4) in brackets';
  167. $expected = 'This is a test text with URL (<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>) in brackets';
  168. $result = $this->Text->autoLink($text);
  169. $this->assertEquals($expected, $result);
  170. $text = 'This is a test text with URL [http://www.cakephp.org/page/4] in square brackets';
  171. $expected = 'This is a test text with URL [<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>] in square brackets';
  172. $result = $this->Text->autoLink($text);
  173. $this->assertEquals($expected, $result);
  174. $text = 'This is a test text with URL [http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3] in square brackets';
  175. $expected = 'This is a test text with URL [<a href="http://www.example.com?aParam[]=value1&amp;aParam[]=value2&amp;aParam[]=value3">http://www.example.com?aParam[]=value1&amp;aParam[]=value2&amp;aParam[]=value3</a>] in square brackets';
  176. $result = $this->Text->autoLink($text);
  177. $this->assertEquals($expected, $result);
  178. $text = 'This is a test text with URL ;http://www.cakephp.org/page/4; semi-colon';
  179. $expected = 'This is a test text with URL ;<a href="http://www.cakephp.org/page/4">http://www.cakephp.org/page/4</a>; semi-colon';
  180. $result = $this->Text->autoLink($text);
  181. $this->assertEquals($expected, $result);
  182. $text = 'This is a test text with URL (http://www.cakephp.org/page/4/other(thing)) brackets';
  183. $expected = 'This is a test text with URL (<a href="http://www.cakephp.org/page/4/other(thing)">http://www.cakephp.org/page/4/other(thing)</a>) brackets';
  184. $result = $this->Text->autoLink($text);
  185. $this->assertEquals($expected, $result);
  186. }
  187. /**
  188. * Test mixing URLs and Email addresses in one confusing string.
  189. *
  190. * @return void
  191. */
  192. public function testAutoLinkMixed()
  193. {
  194. $text = 'Text with a url/email http://example.com/store?email=mark@example.com and email.';
  195. $expected = 'Text with a url/email <a href="http://example.com/store?email=mark@example.com">' .
  196. 'http://example.com/store?email=mark@example.com</a> and email.';
  197. $result = $this->Text->autoLink($text);
  198. $this->assertEquals($expected, $result);
  199. }
  200. /**
  201. * test autoLink() and options.
  202. *
  203. * @return void
  204. */
  205. public function testAutoLinkOptions()
  206. {
  207. $text = 'This is a test text with URL http://www.cakephp.org';
  208. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
  209. $result = $this->Text->autoLink($text, ['class' => 'link']);
  210. $this->assertEquals($expected, $result);
  211. $text = 'This is a test text with URL http://www.cakephp.org';
  212. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
  213. $result = $this->Text->autoLink($text, ['class' => 'link', 'id' => 'MyLink']);
  214. $this->assertEquals($expected, $result);
  215. }
  216. /**
  217. * Test escaping for autoLink
  218. *
  219. * @return void
  220. */
  221. public function testAutoLinkEscape()
  222. {
  223. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  224. $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>';
  225. $result = $this->Text->autoLink($text);
  226. $this->assertEquals($expected, $result);
  227. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  228. $expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  229. $result = $this->Text->autoLink($text, ['escape' => false]);
  230. $this->assertEquals($expected, $result);
  231. $text = 'test <ul>
  232. <li>lorem: http://example.org?some</li>
  233. <li>ipsum: http://othersite.com/abc</li>
  234. </ul> test';
  235. $expected = 'test <ul>
  236. <li>lorem: <a href="http://example.org?some">http://example.org?some</a></li>
  237. <li>ipsum: <a href="http://othersite.com/abc">http://othersite.com/abc</a></li>
  238. </ul> test';
  239. $result = $this->Text->autoLink($text, ['escape' => false]);
  240. $this->assertEquals($expected, $result);
  241. }
  242. /**
  243. * Data provider for autoLinking
  244. *
  245. * @return array
  246. */
  247. public static function autoLinkProvider()
  248. {
  249. return [
  250. [
  251. 'This is a test text',
  252. 'This is a test text',
  253. ],
  254. [
  255. 'This is a test that includes (www.cakephp.org)',
  256. 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)',
  257. ],
  258. [
  259. 'This is a test that includes www.cakephp.org:8080',
  260. 'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>',
  261. ],
  262. [
  263. 'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  264. '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>',
  265. ],
  266. [
  267. 'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  268. 'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
  269. ],
  270. [
  271. 'This is a test that includes Http://example.com/test.php?foo=bar text',
  272. 'This is a test that includes <a href="Http://example.com/test.php?foo=bar">Http://example.com/test.php?foo=bar</a> text',
  273. ],
  274. [
  275. 'This is a test that includes http://example.com/test.php?foo=bar text',
  276. 'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text',
  277. ],
  278. [
  279. 'This is a test that includes www.example.com/test.php?foo=bar text',
  280. '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',
  281. ],
  282. [
  283. 'Text with a partial www.cakephp.org URL',
  284. 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL',
  285. ],
  286. [
  287. 'Text with a partial WWW.cakephp.org URL',
  288. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL',
  289. ],
  290. [
  291. 'Text with a partial WWW.cakephp.org &copy, URL',
  292. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &amp;copy, URL',
  293. ],
  294. [
  295. 'Text with a url www.cot.ag/cuIb2Q and more',
  296. 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more',
  297. ],
  298. [
  299. 'Text with a url http://www.does--not--work.com and more',
  300. 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more',
  301. ],
  302. [
  303. 'Text with a url http://www.not--work.com and more',
  304. 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more',
  305. ],
  306. [
  307. 'Text with a url http://www.sub_domain.domain.pl and more',
  308. 'Text with a url <a href="http://www.sub_domain.domain.pl">http://www.sub_domain.domain.pl</a> and more',
  309. ],
  310. [
  311. 'Text with a partial www.küchenschöhn-not-working.de URL',
  312. 'Text with a partial <a href="http://www.küchenschöhn-not-working.de">www.küchenschöhn-not-working.de</a> URL'
  313. ],
  314. [
  315. 'Text with a partial http://www.küchenschöhn-not-working.de URL',
  316. 'Text with a partial <a href="http://www.küchenschöhn-not-working.de">http://www.küchenschöhn-not-working.de</a> URL'
  317. ],
  318. [
  319. "Text with partial www.cakephp.org\r\nwww.cakephp.org urls and CRLF",
  320. "Text with partial <a href=\"http://www.cakephp.org\">www.cakephp.org</a>\r\n<a href=\"http://www.cakephp.org\">www.cakephp.org</a> urls and CRLF"
  321. ],
  322. [
  323. 'https://nl.wikipedia.org/wiki/Exploit_(computerbeveiliging)',
  324. '<a href="https://nl.wikipedia.org/wiki/Exploit_(computerbeveiliging)">https://nl.wikipedia.org/wiki/Exploit_(computerbeveiliging)</a>'
  325. ],
  326. [
  327. 'http://dev.local/threads/search?search_string=this+is+a+test',
  328. '<a href="http://dev.local/threads/search?search_string=this+is+a+test">http://dev.local/threads/search?search_string=this+is+a+test</a>'
  329. ],
  330. [
  331. 'http://www.ad.nl/show/giel-beelen-heeft-weinig-moeite-met-rijontzegging~acd8b6ed',
  332. '<a href="http://www.ad.nl/show/giel-beelen-heeft-weinig-moeite-met-rijontzegging~acd8b6ed">http://www.ad.nl/show/giel-beelen-heeft-weinig-moeite-met-rijontzegging~acd8b6ed</a>'
  333. ],
  334. [
  335. 'https://sevvlor.com/page%20not%20found',
  336. '<a href="https://sevvlor.com/page%20not%20found">https://sevvlor.com/page%20not%20found</a>'
  337. ],
  338. [
  339. 'https://fakedomain.ext/path/#!topic/test',
  340. '<a href="https://fakedomain.ext/path/#!topic/test">https://fakedomain.ext/path/#!topic/test</a>'
  341. ],
  342. [
  343. 'https://fakedomain.ext/path/#!topic/test;other;tag',
  344. '<a href="https://fakedomain.ext/path/#!topic/test;other;tag">https://fakedomain.ext/path/#!topic/test;other;tag</a>'
  345. ],
  346. [
  347. 'This is text,https://fakedomain.ext/path/#!topic/test,tag, with a comma',
  348. 'This is text,<a href="https://fakedomain.ext/path/#!topic/test,tag">https://fakedomain.ext/path/#!topic/test,tag</a>, with a comma'
  349. ],
  350. [
  351. 'This is text https://fakedomain.ext/path/#!topic/path!',
  352. 'This is text <a href="https://fakedomain.ext/path/#!topic/path">https://fakedomain.ext/path/#!topic/path</a>!'
  353. ]
  354. ];
  355. }
  356. /**
  357. * testAutoLinkUrls method
  358. *
  359. * @dataProvider autoLinkProvider
  360. * @return void
  361. */
  362. public function testAutoLinkUrls($text, $expected)
  363. {
  364. $result = $this->Text->autoLinkUrls($text);
  365. $this->assertEquals($expected, $result);
  366. }
  367. /**
  368. * Test the options for autoLinkUrls
  369. *
  370. * @return void
  371. */
  372. public function testAutoLinkUrlsOptions()
  373. {
  374. $text = 'Text with a partial www.cakephp.org URL';
  375. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  376. $result = $this->Text->autoLinkUrls($text, ['class' => 'link']);
  377. $this->assertRegExp('#^' . $expected . '$#', $result);
  378. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  379. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  380. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  381. $this->assertRegExp('#^' . $expected . '$#', $result);
  382. }
  383. /**
  384. * Test autoLinkUrls with the escape option.
  385. *
  386. * @return void
  387. */
  388. public function testAutoLinkUrlsEscape()
  389. {
  390. $text = 'Text with a partial <a href="http://www.example.com">http://www.example.com</a> link';
  391. $expected = 'Text with a partial <a href="http://www.example.com">http://www.example.com</a> link';
  392. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  393. $this->assertEquals($expected, $result);
  394. $text = 'Text with a partial <a href="http://www.example.com">www.example.com</a> link';
  395. $expected = 'Text with a partial <a href="http://www.example.com">www.example.com</a> link';
  396. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  397. $this->assertEquals($expected, $result);
  398. $text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  399. $expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  400. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  401. $this->assertEquals($expected, $result);
  402. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  403. $expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  404. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  405. $this->assertEquals($expected, $result);
  406. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  407. $expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
  408. $result = $this->Text->autoLinkUrls($text, ['escape' => true]);
  409. $this->assertEquals($expected, $result);
  410. $text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  411. $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';
  412. $result = $this->Text->autoLinkUrls($text, ['escape' => true]);
  413. $this->assertEquals($expected, $result);
  414. $text = 'Text with a url www.not-working-www.com and more';
  415. $expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  416. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  417. $this->assertEquals($expected, $result);
  418. $text = 'Text with a url http://www.not-working-www.com and more';
  419. $expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
  420. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  421. $this->assertEquals($expected, $result);
  422. $text = 'Text with a url http://www.www.not-working-www.com and more';
  423. $expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
  424. $result = $this->Text->autoLinkUrls($text, ['escape' => false]);
  425. $this->assertEquals($expected, $result);
  426. }
  427. /**
  428. * Test autoLinkUrls with query strings.
  429. *
  430. * @return void
  431. */
  432. public function testAutoLinkUrlsQueryString()
  433. {
  434. $text = 'Text with a partial http://www.cakephp.org?product_id=123&foo=bar link';
  435. $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';
  436. $result = $this->Text->autoLinkUrls($text);
  437. $this->assertEquals($expected, $result);
  438. }
  439. /**
  440. * Data provider for autoLinkEmail.
  441. *
  442. * @return array
  443. */
  444. public function autoLinkEmailProvider()
  445. {
  446. return [
  447. [
  448. 'This is a test text',
  449. 'This is a test text',
  450. ],
  451. [
  452. 'email@example.com address',
  453. '<a href="mailto:email@example.com">email@example.com</a> address',
  454. ],
  455. [
  456. 'email@example.com address',
  457. '<a href="mailto:email@example.com">email@example.com</a> address',
  458. ],
  459. [
  460. '(email@example.com) address',
  461. '(<a href="mailto:email@example.com">email@example.com</a>) address',
  462. ],
  463. [
  464. 'Text with email@example.com address',
  465. 'Text with <a href="mailto:email@example.com">email@example.com</a> address',
  466. ],
  467. [
  468. "Text with o'hare._-bob@example.com address",
  469. 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address',
  470. ],
  471. [
  472. 'Text with düsentrieb@küchenschöhn-not-working.de address',
  473. 'Text with <a href="mailto:düsentrieb@küchenschöhn-not-working.de">düsentrieb@küchenschöhn-not-working.de</a> address',
  474. ],
  475. [
  476. 'Text with me@subdomain.küchenschöhn.de address',
  477. 'Text with <a href="mailto:me@subdomain.küchenschöhn.de">me@subdomain.küchenschöhn.de</a> address',
  478. ],
  479. [
  480. 'Text with email@example.com address',
  481. 'Text with <a href="mailto:email@example.com" class="link">email@example.com</a> address',
  482. ['class' => 'link'],
  483. ],
  484. [
  485. '<p>mark@example.com</p>',
  486. '<p><a href="mailto:mark@example.com">mark@example.com</a></p>',
  487. ['escape' => false]
  488. ],
  489. [
  490. 'Some&nbsp;mark@example.com&nbsp;Text',
  491. 'Some&nbsp;<a href="mailto:mark@example.com">mark@example.com</a>&nbsp;Text',
  492. ['escape' => false]
  493. ],
  494. ];
  495. }
  496. /**
  497. * testAutoLinkEmails method
  498. *
  499. * @param string $text The text to link
  500. * @param string $expected The expected results.
  501. * @dataProvider autoLinkEmailProvider
  502. * @return void
  503. */
  504. public function testAutoLinkEmails($text, $expected, $attrs = [])
  505. {
  506. $result = $this->Text->autoLinkEmails($text, $attrs);
  507. $this->assertEquals($expected, $result);
  508. }
  509. /**
  510. * test invalid email addresses.
  511. *
  512. * @return void
  513. */
  514. public function testAutoLinkEmailInvalid()
  515. {
  516. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  517. $expected = 'this is a myaddress@gmx-de test';
  518. $this->assertEquals($expected, $result);
  519. }
  520. /**
  521. * testAutoParagraph method
  522. *
  523. * @return void
  524. */
  525. public function testAutoParagraph()
  526. {
  527. $text = 'This is a test text';
  528. $expected = <<<TEXT
  529. <p>This is a test text</p>
  530. TEXT;
  531. $result = $this->Text->autoParagraph($text);
  532. $text = 'This is a <br/> <BR> test text';
  533. $expected = <<<TEXT
  534. <p>This is a </p>
  535. <p> test text</p>
  536. TEXT;
  537. $result = $this->Text->autoParagraph($text);
  538. $this->assertTextEquals($expected, $result);
  539. $result = $this->Text->autoParagraph($text);
  540. $text = 'This is a <BR id="test"/><br class="test"> test text';
  541. $expected = <<<TEXT
  542. <p>This is a </p>
  543. <p> test text</p>
  544. TEXT;
  545. $result = $this->Text->autoParagraph($text);
  546. $this->assertTextEquals($expected, $result);
  547. $text = <<<TEXT
  548. This is a test text.
  549. This is a line return.
  550. TEXT;
  551. $expected = <<<TEXT
  552. <p>This is a test text.<br />
  553. This is a line return.</p>
  554. TEXT;
  555. $result = $this->Text->autoParagraph($text);
  556. $this->assertTextEquals($expected, $result);
  557. $text = <<<TEXT
  558. This is a test text.
  559. This is a new line.
  560. TEXT;
  561. $expected = <<<TEXT
  562. <p>This is a test text.</p>
  563. <p>This is a new line.</p>
  564. TEXT;
  565. $result = $this->Text->autoParagraph($text);
  566. $this->assertTextEquals($expected, $result);
  567. }
  568. }