TextExtHelperTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $this->Text = new TextExtHelper(new View(null));
  8. }
  9. public function testObject() {
  10. $this->assertTrue(is_a($this->Text, 'TextExtHelper'));
  11. }
  12. public function testAutoLinkEmails() {
  13. $text = 'Text with a url euro@euro.de and more';
  14. $expected = 'Text with a url <a href="mailto:euro@euro.de">euro@euro.de</a> and more';
  15. $result = $this->Text->autoLinkEmails($text, array());
  16. $this->assertEquals($result, $expected);
  17. $text = 'Text with a url euro@euro.de and more';
  18. $expected = 'Text with a url <script language=javascript><!--
  19. 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"\'+ \'>\');
  20. //--></script>
  21. e&#117;&#x72;o<span>@</span>e&#x75;&#x72;&#111;&#x2e;&#x64;&#x65;
  22. <script language=javascript><!--
  23. document.write(\'</a>\');
  24. //--></script> and more\'';
  25. $result = $this->Text->autoLinkEmails($text, array('obfuscate'=>true));
  26. pr($text);
  27. echo $result;
  28. pr(h($result));
  29. $this->assertNotEqual($result, $text);
  30. }
  31. public function testAutoLinkEmailsWithHtmlOrDangerousStrings() {
  32. $text = 'Text <i>with a email</i> euro@euro.de and more';
  33. $expected = 'Text &lt;i&gt;with a email&lt;/i&gt; <a href="mailto:euro@euro.de">euro@euro.de</a> and more';
  34. $result = $this->Text->autoLinkEmails($text);
  35. //pr(h($text));
  36. $this->assertEquals($result, $expected);
  37. }
  38. public function testStripProtocol() {
  39. $urls = array(
  40. 'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
  41. 'www.cakephp.org' => 'www.cakephp.org'
  42. );
  43. foreach ($urls as $url => $expected) {
  44. $is = $this->Text->stripProtocol($url);
  45. $this->assertEquals($is, $expected);
  46. }
  47. }
  48. public function testAutoLinkUrls() {
  49. $texts = array(
  50. 'text http://www.cakephp.org/bla/bla some more text' => '',
  51. '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'
  52. );
  53. foreach ($texts as $text => $expected) {
  54. //$is = $this->Text->stripProtocol($url);
  55. //$this->assertEquals($is, $expected);
  56. }
  57. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekr and more';
  58. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekr">www.cot.ag/c...</a> and more';
  59. $result = $this->Text->autoLinkUrls($text, array('maxLength'=>12));
  60. $this->assertEquals($result, $expected);
  61. $text = 'Text with a url http://www.cot.ag/cuIb2Q/eru and more';
  62. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eru">www.cot.ag/cuIb2Q/eru</a> and more';
  63. $result = $this->Text->autoLinkUrls($text, array('stripProtocol'=>true));
  64. $this->assertEquals($result, $expected);
  65. $text = 'Text with a url http://www.cot.ag/cuIb2Q/eruierieriu-erjekr and more';
  66. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekr">http://www.cot.ag/cuIb2Q/eruierieriu-erjekr</a> and more';
  67. $result = $this->Text->autoLinkUrls($text, array('stripProtocol'=>false, 'maxLength'=>0));
  68. $this->assertEquals($result, $expected);
  69. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer-werwerwe-werwerwer-werwerdfrffsd-werwer and more';
  70. $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';
  71. $result = $this->Text->autoLinkUrls($text);
  72. $this->assertEquals($result, $expected);
  73. }
  74. public function testAutoLinkUrlsWithEscapeFalse() {
  75. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer and more';
  76. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer">www.cot.ag/cuIb2Q/er...</a> and more';
  77. $result = $this->Text->autoLinkUrls($text, array('maxLength'=>20), array('escape'=>false));
  78. $this->assertEquals($result, $expected);
  79. # not yet working
  80. /*
  81. $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer and more';
  82. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer">www.cot.ag/cuIb2Q/er&hellip;</a> and more';
  83. $result = $this->Text->autoLinkUrls($text, array('maxLength'=>20), array('escape'=>false, 'html'=>true));
  84. $this->assertEquals($result, $expected);
  85. */
  86. $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';
  87. $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';
  88. $result = $this->Text->autoLinkUrls($text, array('maxLength'=>30));
  89. $this->assertEquals($result, $expected);
  90. }
  91. public function testAutoLinkUrlsWithHtmlOrDangerousStrings() {
  92. $text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and more';
  93. $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';
  94. $result = $this->Text->autoLinkUrls($text);
  95. //pr(h($text));
  96. $this->assertEquals($result, $expected);
  97. }
  98. /**
  99. * combined (emails + urls)
  100. * 2011-04-03 ms
  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($result, $expected);
  108. }
  109. /* from cake */
  110. /**
  111. * test invalid email addresses.
  112. *
  113. * @return void
  114. */
  115. public function testAutoLinkEmailInvalid() {
  116. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  117. $expected = 'this is a myaddress@gmx-de test';
  118. $this->assertEquals($expected, $result);
  119. $result = $this->Text->autoLink('this is a myaddress@gmx-de test');
  120. $expected = 'this is a myaddress@gmx-de test';
  121. $this->assertEquals($expected, $result);
  122. }
  123. public function testAutoLinkUrlsWithCakeTests() {
  124. $text = 'This is a test text';
  125. $expected = 'This is a test text';
  126. $result = $this->Text->autoLinkUrls($text);
  127. $this->assertEquals($result, $expected);
  128. $text = 'This is a test that includes (www.cakephp.org)';
  129. $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
  130. $result = $this->Text->autoLinkUrls($text);
  131. $this->assertEquals($result, $expected);
  132. $text = 'Text with a partial www.cakephp.org URL';
  133. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
  134. $result = $this->Text->autoLinkUrls($text);
  135. $this->assertRegExp('#^' . $expected . '$#', $result);
  136. $text = 'Text with a partial www.cakephp.org URL';
  137. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  138. $result = $this->Text->autoLinkUrls($text, array(), array('class' => 'link'));
  139. $this->assertRegExp('#^' . $expected . '$#', $result);
  140. $text = 'Text with a partial WWW.cakephp.org URL';
  141. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> URL';
  142. $result = $this->Text->autoLinkUrls($text);
  143. $this->assertRegExp('#^' . $expected . '$#', $result);
  144. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  145. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  146. $result = $this->Text->autoLinkUrls($text, array('escape' => false), array('escape' => false));
  147. $this->assertRegExp('#^' . $expected . '$#', $result);
  148. $text = 'Text with a url www.cot.ag/cuIb2Q and more';
  149. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more';
  150. $result = $this->Text->autoLinkUrls($text);
  151. $this->assertEquals($result, $expected);
  152. }
  153. }