TextExtHelperTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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, array());
  17. $this->assertEquals($result, $expected);
  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, array('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($result, $expected);
  38. }
  39. public function testStripProtocol() {
  40. $urls = array(
  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($is, $expected);
  47. }
  48. }
  49. public function testAutoLinkUrls() {
  50. $texts = array(
  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($is, $expected);
  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, array('maxLength'=>12));
  61. $this->assertEquals($result, $expected);
  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, array('stripProtocol'=>true));
  65. $this->assertEquals($result, $expected);
  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, array('stripProtocol'=>false, 'maxLength'=>0));
  69. $this->assertEquals($result, $expected);
  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($result, $expected);
  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, array('maxLength'=>20), array('escape'=>false));
  79. $this->assertEquals($result, $expected);
  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($result, $expected);
  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, array('maxLength'=>30));
  90. $this->assertEquals($result, $expected);
  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($result, $expected);
  98. }
  99. /**
  100. * combined (emails + urls)
  101. * 2011-04-03 ms
  102. */
  103. public function testAutoLink() {
  104. $text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and some email@domain.com more';
  105. $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';
  106. $result = $this->Text->autoLink($text);
  107. //pr(h($text));
  108. $this->assertEquals($result, $expected);
  109. }
  110. /* from cake */
  111. /**
  112. * test invalid email addresses.
  113. *
  114. * @return void
  115. */
  116. public function testAutoLinkEmailInvalid() {
  117. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  118. $expected = 'this is a myaddress@gmx-de test';
  119. $this->assertEquals($expected, $result);
  120. $result = $this->Text->autoLink('this is a myaddress@gmx-de test');
  121. $expected = 'this is a myaddress@gmx-de test';
  122. $this->assertEquals($expected, $result);
  123. }
  124. public function testAutoLinkUrlsWithCakeTests() {
  125. $text = 'This is a test text';
  126. $expected = 'This is a test text';
  127. $result = $this->Text->autoLinkUrls($text);
  128. $this->assertEquals($result, $expected);
  129. $text = 'This is a test that includes (www.cakephp.org)';
  130. $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
  131. $result = $this->Text->autoLinkUrls($text);
  132. $this->assertEquals($result, $expected);
  133. $text = 'Text with a partial www.cakephp.org URL';
  134. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
  135. $result = $this->Text->autoLinkUrls($text);
  136. $this->assertRegExp('#^' . $expected . '$#', $result);
  137. $text = 'Text with a partial www.cakephp.org URL';
  138. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  139. $result = $this->Text->autoLinkUrls($text, array(), array('class' => 'link'));
  140. $this->assertRegExp('#^' . $expected . '$#', $result);
  141. $text = 'Text with a partial WWW.cakephp.org URL';
  142. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> URL';
  143. $result = $this->Text->autoLinkUrls($text);
  144. $this->assertRegExp('#^' . $expected . '$#', $result);
  145. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  146. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  147. $result = $this->Text->autoLinkUrls($text, array('escape' => false), array('escape' => false));
  148. $this->assertRegExp('#^' . $expected . '$#', $result);
  149. $text = 'Text with a url www.cot.ag/cuIb2Q and more';
  150. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more';
  151. $result = $this->Text->autoLinkUrls($text);
  152. $this->assertEquals($result, $expected);
  153. }
  154. /**
  155. * test minimizeUrl
  156. *
  157. * @access public
  158. * @return void
  159. * 2009-03-11 ms
  160. */
  161. public function testMinimizeUrl() {
  162. $url = 'http://www.test.de';
  163. $this->assertEquals($url, $this->Text->minimizeUrl($url,20));
  164. $url = 'http://www.test.de';
  165. $this->assertEquals($url, $this->Text->minimizeUrl($url,18));
  166. $url = 'http://www.test.de';
  167. $this->assertEquals('www.test.de', $this->Text->minimizeUrl($url,17));
  168. $url = 'http://www.testpage.de';
  169. $this->assertEquals('ww&#8230;ge.de', $this->Text->minimizeUrl($url,10));
  170. $url = 'http://www.testpage.de';
  171. $this->assertEquals('ww...ge.de', $this->Text->minimizeUrl($url,10, array('placeholder'=>'...')));
  172. # without full http://
  173. $url = 'www.testpage.de';
  174. $this->assertEquals($url, $this->Text->minimizeUrl($url,15));
  175. $url = 'www.testpage.de';
  176. $this->assertEquals('www.te&#8230;ge.de', $this->Text->minimizeUrl($url,14));
  177. }
  178. /**
  179. * test shortenText
  180. *
  181. * @access public
  182. * @return void
  183. * 2009-03-11 ms
  184. */
  185. public function testShortenText() {
  186. }
  187. }