TextHelperTest.php 11 KB

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