TextExtHelperTest.php 9.9 KB

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