TextHelperTest.php 26 KB

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