TextHelperTest.php 26 KB

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