TextHelperTest.php 10 KB

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