TextHelperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\View\View;
  4. use Tools\TestSuite\TestCase;
  5. use Tools\Utility\Text;
  6. use Tools\View\Helper\TextHelper;
  7. class TextHelperTest extends TestCase {
  8. /**
  9. * @var \Tools\View\Helper\TextHelper|\Tools\Utility\Text
  10. */
  11. public $Text;
  12. /**
  13. * @return void
  14. */
  15. public function setUp() {
  16. parent::setUp();
  17. $this->Text = new TextHelper(new View(null));
  18. }
  19. /**
  20. * Test calling Utility.Text class
  21. *
  22. * @return void
  23. */
  24. public function testParentCall() {
  25. $result = $this->Text->abbreviate('FooBar');
  26. $this->assertSame('FooBar', $result);
  27. }
  28. /**
  29. * TextExtHelperTest::testAutoLinkEmails()
  30. *
  31. * @return void
  32. */
  33. public function testAutoLinkEmails() {
  34. $text = 'Text with a url euro@euro.de and more';
  35. $expected = 'Text with a url <a href="mailto:euro@euro.de">euro@euro.de</a> and more';
  36. $result = $this->Text->autoLinkEmails($text, []);
  37. $this->assertEquals($expected, $result);
  38. $text = 'Text with a url euro@euro.de and more';
  39. $expected = 'Text with a url <script language=javascript><!--
  40. 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"\'+ \'>\');
  41. //--></script>
  42. e&#117;&#x72;o<span>@</span>e&#x75;&#x72;&#111;&#x2e;&#x64;&#x65;
  43. <script language=javascript><!--
  44. document.write(\'</a>\');
  45. //--></script> and more\'';
  46. $result = $this->Text->autoLinkEmails($text, ['obfuscate' => true]);
  47. //pr($text);
  48. //echo $result;
  49. //pr(h($result));
  50. $this->assertNotEquals($result, $text);
  51. }
  52. /**
  53. * TextExtHelperTest::testAutoLinkEmailsWithHtmlOrDangerousStrings()
  54. *
  55. * @return void
  56. */
  57. public function testAutoLinkEmailsWithHtmlOrDangerousStrings() {
  58. $text = 'Text <i>with a email</i> euro@euro.de and more';
  59. $expected = 'Text &lt;i&gt;with a email&lt;/i&gt; <a href="mailto:euro@euro.de">euro@euro.de</a> and more';
  60. $result = $this->Text->autoLinkEmails($text);
  61. //pr(h($text));
  62. $this->assertEquals($expected, $result);
  63. }
  64. /**
  65. * TextExtHelperTest::testStripProtocol()
  66. *
  67. * @return void
  68. */
  69. public function testStripProtocol() {
  70. $urls = [
  71. 'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
  72. 'www.cakephp.org' => 'www.cakephp.org'
  73. ];
  74. foreach ($urls as $url => $expected) {
  75. $is = $this->Text->stripProtocol($url);
  76. $this->assertEquals($expected, $is);
  77. }
  78. }
  79. /**
  80. * TextExtHelperTest::testAutoLinkUrls()
  81. *
  82. * @return void
  83. */
  84. public function testAutoLinkUrls() {
  85. $this->skipIf(true, '//TODO: Port from 2.x');
  86. $texts = [
  87. 'text http://www.cakephp.org/bla/bla some more text' => '',
  88. 'This is a test text with URL http://www.cakephp.org\tand some more text' => 'This is a test text with URL http://www.cakephp.org\tand some more text'
  89. ];
  90. foreach ($texts as $text => $expected) {
  91. //$is = $this->Text->stripProtocol($url);
  92. //$this->assertEquals($expected, $is);
  93. }
  94. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekr and more';
  95. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekr">www.cot.ag/c...</a> and more';
  96. $result = $this->Text->autoLinkUrls($text, ['maxLength' => 12]);
  97. $this->assertEquals($expected, $result);
  98. $text = 'Text with a url http://www.cot.ag/cuIb2Q/eru and more';
  99. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eru">www.cot.ag/cuIb2Q/eru</a> and more';
  100. $result = $this->Text->autoLinkUrls($text, ['stripProtocol' => true]);
  101. $this->assertEquals($expected, $result);
  102. $text = 'Text with a url http://www.cot.ag/cuIb2Q/eruierieriu-erjekr and more';
  103. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekr">http://www.cot.ag/cuIb2Q/eruierieriu-erjekr</a> and more';
  104. $result = $this->Text->autoLinkUrls($text, ['stripProtocol' => false, 'maxLength' => 0]);
  105. $this->assertEquals($expected, $result);
  106. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer-werwerwe-werwerwer-werwerdfrffsd-werwer and more';
  107. $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';
  108. $result = $this->Text->autoLinkUrls($text);
  109. $this->assertEquals($expected, $result);
  110. }
  111. /**
  112. * TextExtHelperTest::testAutoLinkUrlsWithEscapeFalse()
  113. *
  114. * @return void
  115. */
  116. public function testAutoLinkUrlsWithEscapeFalse() {
  117. $this->skipIf(true, '//TODO: Port from 2.x');
  118. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer and more';
  119. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer">www.cot.ag/cuIb2Q/er...</a> and more';
  120. $result = $this->Text->autoLinkUrls($text, ['maxLength' => 20], ['escape' => false]);
  121. $this->assertEquals($expected, $result);
  122. // not yet working
  123. /*
  124. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer and more';
  125. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer">www.cot.ag/cuIb2Q/er&hellip;</a> and more';
  126. $result = $this->Text->autoLinkUrls($text, array('maxLength'=>20), array('escape'=>false, 'html'=>true));
  127. $this->assertEquals($expected, $result);
  128. */
  129. $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';
  130. $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';
  131. $result = $this->Text->autoLinkUrls($text, ['maxLength' => 30]);
  132. $this->assertEquals($expected, $result);
  133. }
  134. /**
  135. * TextExtHelperTest::testAutoLinkUrlsWithHtmlOrDangerousStrings()
  136. *
  137. * @return void
  138. */
  139. public function testAutoLinkUrlsWithHtmlOrDangerousStrings() {
  140. $text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and more';
  141. $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';
  142. $result = $this->Text->autoLinkUrls($text);
  143. //pr(h($text));
  144. $this->assertEquals($expected, $result);
  145. }
  146. /**
  147. * Combined (emails + urls)
  148. *
  149. * @return void
  150. */
  151. public function testAutoLink() {
  152. $text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and some email@domain.com more';
  153. $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';
  154. $result = $this->Text->autoLink($text);
  155. //pr(h($text));
  156. $this->assertEquals($expected, $result);
  157. // With umlauts
  158. $text = 'Text <i>with a url</i> www.äöü.ag?id=2&sub=3 link';
  159. $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';
  160. $result = $this->Text->autoLink($text);
  161. //pr(h($text));
  162. $this->assertEquals($expected, $result);
  163. }
  164. /* from cake */
  165. /**
  166. * Test invalid email addresses.
  167. *
  168. * @return void
  169. */
  170. public function testAutoLinkEmailInvalid() {
  171. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  172. $expected = 'this is a myaddress@gmx-de test';
  173. $this->assertEquals($expected, $result);
  174. $result = $this->Text->autoLink('this is a myaddress@gmx-de test');
  175. $expected = 'this is a myaddress@gmx-de test';
  176. $this->assertEquals($expected, $result);
  177. }
  178. /**
  179. * TextExtHelperTest::testAutoLinkUrlsWithCakeTests()
  180. *
  181. * @return void
  182. */
  183. public function testAutoLinkUrlsWithCakeTests() {
  184. $this->skipIf(true, '//TODO: Port from 2.x');
  185. $text = 'This is a test text';
  186. $expected = 'This is a test text';
  187. $result = $this->Text->autoLinkUrls($text);
  188. $this->assertEquals($expected, $result);
  189. $text = 'This is a test that includes (www.cakephp.org)';
  190. $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
  191. $result = $this->Text->autoLinkUrls($text);
  192. $this->assertEquals($expected, $result);
  193. $text = 'Text with a partial www.cakephp.org URL';
  194. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
  195. $result = $this->Text->autoLinkUrls($text);
  196. $this->assertRegExp('#^' . $expected . '$#', $result);
  197. $text = 'Text with a partial www.cakephp.org URL';
  198. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  199. $result = $this->Text->autoLinkUrls($text, [], ['class' => 'link']);
  200. $this->assertRegExp('#^' . $expected . '$#', $result);
  201. $text = 'Text with a partial WWW.cakephp.org URL';
  202. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> URL';
  203. $result = $this->Text->autoLinkUrls($text);
  204. $this->assertRegExp('#^' . $expected . '$#', $result);
  205. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  206. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  207. $result = $this->Text->autoLinkUrls($text, ['escape' => false], ['escape' => false]);
  208. $this->assertRegExp('#^' . $expected . '$#', $result);
  209. $text = 'Text with a url www.cot.ag/cuIb2Q and more';
  210. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more';
  211. $result = $this->Text->autoLinkUrls($text);
  212. $this->assertEquals($expected, $result);
  213. }
  214. /**
  215. * Test minimizeUrl
  216. *
  217. * @return void
  218. */
  219. public function testMinimizeUrl() {
  220. $url = 'http://www.test.de';
  221. $this->assertEquals($url, $this->Text->minimizeUrl($url, 20));
  222. $url = 'http://www.test.de';
  223. $this->assertEquals($url, $this->Text->minimizeUrl($url, 18));
  224. $url = 'http://www.test.de';
  225. $this->assertEquals('www.test.de', $this->Text->minimizeUrl($url, 17));
  226. $url = 'http://www.testpage.de';
  227. $this->assertEquals('ww&#8230;ge.de', $this->Text->minimizeUrl($url, 10));
  228. $url = 'http://www.testpage.de';
  229. $this->assertEquals('ww...ge.de', $this->Text->minimizeUrl($url, 10, ['placeholder' => '...']));
  230. // without full http://
  231. $url = 'www.testpage.de';
  232. $this->assertEquals($url, $this->Text->minimizeUrl($url, 15));
  233. $url = 'www.testpage.de';
  234. $this->assertEquals('www.te&#8230;ge.de', $this->Text->minimizeUrl($url, 14));
  235. }
  236. }