TextHelperTest.php 10 KB

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