TextHelperTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\View\View;
  4. use Shim\TestSuite\TestCase;
  5. use Tools\View\Helper\TextHelper;
  6. class TextHelperTest extends TestCase {
  7. /**
  8. * @var \Tools\View\Helper\TextHelper
  9. */
  10. protected $Text;
  11. /**
  12. * @return void
  13. */
  14. public function setUp(): void {
  15. parent::setUp();
  16. $this->Text = new TextHelper(new View(null));
  17. }
  18. /**
  19. * Test calling Utility.Text class
  20. *
  21. * @return void
  22. */
  23. public function testParentCall() {
  24. $result = $this->Text->abbreviate('FooBar');
  25. $this->assertSame('FooBar', $result);
  26. }
  27. /**
  28. * @return void
  29. */
  30. public function testAutoLinkEmails() {
  31. $text = 'Text with a url euro@euro.de and more';
  32. $expected = 'Text with a url <a href="mailto:euro@euro.de">euro@euro.de</a> and more';
  33. $result = $this->Text->autoLinkEmails($text, []);
  34. $this->assertEquals($expected, $result);
  35. $text = 'Text with a url euro@euro.de and more';
  36. $expected = 'Text with a url <script language=javascript><!--
  37. document.write(\'<a\'+ \' hre\'+ \'f="ma\'+ \'ilto:\'+ \'eu\'+ \'ro@\'+ \'euro\'+ \'.d\'+ \'e"\'+ \' t\'+ \'itle\'+ \'="\'+ \'Für \'+ \'den\'+ \' G\'+ \'ebra\'+ \'uch\'+ \' eines\'+ \' exte\'+ \'rn\'+ \'en E-\'+ \'Mail-P\'+ \'rogra\'+ \'mms"\'+ \' cl\'+ \'ass="e\'+ \'mail"\'+ \'>\');
  38. //--></script>
  39. e&#117;&#x72;o<span>@</span>e&#x75;&#x72;&#111;&#x2e;&#x64;&#x65;
  40. <script language=javascript><!--
  41. document.write(\'</a>\');
  42. //--></script> and more\'';
  43. $result = $this->Text->autoLinkEmails($text, ['obfuscate' => true]);
  44. //pr($text);
  45. //echo $result;
  46. //pr(h($result));
  47. $this->assertNotEquals($result, $text);
  48. }
  49. /**
  50. * @return void
  51. */
  52. public function testAutoLinkEmailsWithHtmlOrDangerousStrings() {
  53. $text = 'Text <i>with a email</i> euro@euro.de and more';
  54. $expected = 'Text &lt;i&gt;with a email&lt;/i&gt; <a href="mailto:euro@euro.de">euro@euro.de</a> and more';
  55. $result = $this->Text->autoLinkEmails($text);
  56. //pr(h($text));
  57. $this->assertEquals($expected, $result);
  58. }
  59. /**
  60. * @return void
  61. */
  62. public function testStripProtocol(): void {
  63. $urls = [
  64. 'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
  65. 'https://www.cakephp.org/' => 'www.cakephp.org/',
  66. ];
  67. foreach ($urls as $url => $expected) {
  68. $is = $this->Text->stripProtocol($url);
  69. $this->assertEquals($expected, $is);
  70. }
  71. }
  72. /**
  73. * @return void
  74. */
  75. public function testAutoLinkUrls() {
  76. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekr and more';
  77. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekr">www.cot.ag/c…</a> and more';
  78. $result = $this->Text->autoLinkUrls($text, ['maxLength' => 12]);
  79. $this->assertEquals($expected, $result);
  80. $text = 'Text with a url http://www.cot.ag/cuIb2Q/eru and more';
  81. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eru">www.cot.ag/cuIb2Q/eru</a> and more';
  82. $result = $this->Text->autoLinkUrls($text, ['stripProtocol' => true]);
  83. $this->assertEquals($expected, $result);
  84. $text = 'Text with a url http://www.cot.ag/cuIb2Q/eruierieriu-erjekr and more';
  85. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekr">http://www.cot.ag/cuIb2Q/eruierieriu-erjekr</a> and more';
  86. $result = $this->Text->autoLinkUrls($text, ['stripProtocol' => false, 'maxLength' => 0]);
  87. $this->assertEquals($expected, $result);
  88. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer-werwerwe-werwerwer-werwerdfrffsd-werwer and more';
  89. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer-werwerwe-werwerwer-werwerdfrffsd-werwer">www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-w…</a> and more';
  90. $result = $this->Text->autoLinkUrls($text);
  91. $this->assertEquals($expected, $result);
  92. }
  93. /**
  94. * @return void
  95. */
  96. public function testAutoLinkUrlsWithEscapeFalse() {
  97. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer and more';
  98. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer">www.cot.ag/cuIb2Q/er…</a> and more';
  99. $result = $this->Text->autoLinkUrls($text, ['maxLength' => 20], ['escape' => false]);
  100. $this->assertEquals($expected, $result);
  101. // not yet working
  102. /*
  103. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer and more';
  104. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer">www.cot.ag/cuIb2Q/er&hellip;</a> and more';
  105. $result = $this->Text->autoLinkUrls($text, array('maxLength'=>20), array('escape'=>false, 'html'=>true));
  106. $this->assertEquals($expected, $result);
  107. */
  108. $text = '<h3>google<h3> a http://maps.google.de/maps?f=d&source=s_d&saddr=m%C3%BCnchen&daddr=Berlin&hl=de&geocode=FXaL3gIdGrOwACnZX4yj-XWeRzF9mLF9SrgMAQ%3BFY1xIQMdSKTMACkBWQM_N06oRzFwO15bRiAhBA&mra=ls&sll=52.532932,13.41156&sspn=0.77021,2.348328&g=berlin&ie=UTF8&t=h&z=6 link';
  109. $expected = '&lt;h3&gt;google&lt;h3&gt; a <a href="http://maps.google.de/maps?f=d&amp;source=s_d&amp;saddr=m%C3%BCnchen&amp;daddr=Berlin&amp;hl=de&amp;geocode=FXaL3gIdGrOwACnZX4yj-XWeRzF9mLF9SrgMAQ%3BFY1xIQMdSKTMACkBWQM_N06oRzFwO15bRiAhBA&amp;mra=ls&amp;sll=52.532932,13.41156&amp;sspn=0.77021,2.348328&amp;g=berlin&amp;ie=UTF8&amp;t=h&amp;z=6">maps.google.de/maps?f=d&amp;source…</a> link';
  110. $result = $this->Text->autoLinkUrls($text, ['maxLength' => 30]);
  111. $this->assertEquals($expected, $result);
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function testAutoLinkUrlsWithHtmlOrDangerousStrings() {
  117. $text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and more';
  118. $expected = 'Text &lt;i&gt;with a url&lt;/i&gt; <a href="http://www.cot.ag?id=2&amp;sub=3">www.cot.ag?id=2&amp;sub=3</a> and more';
  119. $result = $this->Text->autoLinkUrls($text);
  120. $this->assertEquals($expected, $result);
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function testAutoLinkUrlsWithCallback() {
  126. $this->loadRoutes();
  127. $text = 'Text www.domain.test?id=2&sub=3 and more';
  128. $that = $this->Text;
  129. $callable = function (string $link, string $url, array $options) use ($that) {
  130. $linkOptions = $options;
  131. $linkOptions['target'] = '_blank';
  132. $linkOptions['rel'] = 'nofollow';
  133. $url = ['controller' => 'Outbound', 'action' => 'redirect', '?' => ['url' => $url]];
  134. return $that->Html->link($that->prepareLinkName($link, $options), $url, $linkOptions);
  135. };
  136. $result = $this->Text->autoLinkUrls($text, ['callable' => $callable]);
  137. $expected = 'Text <a href="/outbound/redirect?url=http%3A%2F%2Fwww.domain.test%3Fid%3D2%26sub%3D3" target="_blank" rel="nofollow">www.domain.test?id=2&amp;sub=3</a> and more';
  138. $this->assertEquals($expected, $result);
  139. }
  140. /**
  141. * Combined (emails + urls)
  142. *
  143. * @return void
  144. */
  145. public function testAutoLink() {
  146. $text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and some email@domain.com more';
  147. $expected = 'Text &lt;i&gt;with a url&lt;/i&gt; <a href="http://www.cot.ag?id=2&amp;sub=3">www.cot.ag?id=2&amp;sub=3</a> and some <a href="mailto:email@domain.com">email@domain.com</a> more';
  148. $result = $this->Text->autoLink($text);
  149. //pr(h($text));
  150. $this->assertEquals($expected, $result);
  151. // With umlauts
  152. $text = 'Text <i>with a url</i> www.äöü.ag?id=2&sub=3 link';
  153. $expected = 'Text &lt;i&gt;with a url&lt;/i&gt; <a href="http://www.äöü.ag?id=2&amp;sub=3">www.äöü.ag?id=2&amp;sub=3</a> link';
  154. $result = $this->Text->autoLink($text);
  155. //pr(h($text));
  156. $this->assertEquals($expected, $result);
  157. }
  158. /**
  159. * Test invalid email addresses.
  160. *
  161. * @return void
  162. */
  163. public function testAutoLinkEmailInvalid() {
  164. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  165. $expected = 'this is a myaddress@gmx-de test';
  166. $this->assertEquals($expected, $result);
  167. $result = $this->Text->autoLink('this is a myaddress@gmx-de test');
  168. $expected = 'this is a myaddress@gmx-de test';
  169. $this->assertEquals($expected, $result);
  170. }
  171. /**
  172. * @return void
  173. */
  174. public function testAutoLinkUrlsWithCakeTests() {
  175. $text = 'This is a test text';
  176. $expected = 'This is a test text';
  177. $result = $this->Text->autoLinkUrls($text);
  178. $this->assertEquals($expected, $result);
  179. $text = 'This is a test that includes (www.cakephp.org)';
  180. $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
  181. $result = $this->Text->autoLinkUrls($text);
  182. $this->assertEquals($expected, $result);
  183. $text = 'Text with a partial www.cakephp.org URL';
  184. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
  185. $result = $this->Text->autoLinkUrls($text);
  186. $this->assertMatchesRegularExpression('#^' . $expected . '$#', $result);
  187. $text = 'Text with a partial www.cakephp.org URL';
  188. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  189. $result = $this->Text->autoLinkUrls($text, ['class' => 'link']);
  190. $this->assertMatchesRegularExpression('#^' . $expected . '$#', $result);
  191. $text = 'Text with a partial WWW.cakephp.org URL';
  192. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> URL';
  193. $result = $this->Text->autoLinkUrls($text);
  194. $this->assertMatchesRegularExpression('#^' . $expected . '$#', $result);
  195. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  196. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  197. $result = $this->Text->autoLinkUrls($text, ['escape' => false, 'escape' => false]);
  198. $this->assertMatchesRegularExpression('#^' . $expected . '$#', $result);
  199. $text = 'Text with a url www.cot.ag/cuIb2Q and more';
  200. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more';
  201. $result = $this->Text->autoLinkUrls($text);
  202. $this->assertEquals($expected, $result);
  203. }
  204. /**
  205. * Test minimizeUrl
  206. *
  207. * @return void
  208. */
  209. public function testMinimizeUrl() {
  210. $url = 'http://www.test.de';
  211. $this->assertEquals($url, $this->Text->minimizeUrl($url, 20));
  212. $url = 'http://www.test.de';
  213. $this->assertEquals($url, $this->Text->minimizeUrl($url, 18));
  214. $url = 'http://www.test.de';
  215. $this->assertEquals('www.test.de', $this->Text->minimizeUrl($url, 17));
  216. $url = 'http://www.testpage.de';
  217. $this->assertEquals('ww&#8230;ge.de', $this->Text->minimizeUrl($url, 10));
  218. $url = 'http://www.testpage.de';
  219. $this->assertEquals('ww...ge.de', $this->Text->minimizeUrl($url, 10, ['placeholder' => '...']));
  220. // without full http://
  221. $url = 'www.testpage.de';
  222. $this->assertEquals($url, $this->Text->minimizeUrl($url, 15));
  223. $url = 'www.testpage.de';
  224. $this->assertEquals('www.te&#8230;ge.de', $this->Text->minimizeUrl($url, 14));
  225. }
  226. }