TextHelperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * TextHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('View', 'View');
  20. App::uses('TextHelper', 'View/Helper');
  21. class TextHelperTestObject extends TextHelper {
  22. public function attach(StringMock $string) {
  23. $this->_engine = $string;
  24. }
  25. public function engine() {
  26. return $this->_engine;
  27. }
  28. }
  29. /**
  30. * StringMock class
  31. */
  32. class StringMock {
  33. }
  34. /**
  35. * TextHelperTest class
  36. *
  37. * @package Cake.Test.Case.View.Helper
  38. */
  39. class TextHelperTest extends CakeTestCase {
  40. /**
  41. * setUp method
  42. *
  43. * @return void
  44. */
  45. public function setUp() {
  46. parent::setUp();
  47. $this->View = new View(null);
  48. $this->Text = new TextHelper($this->View);
  49. }
  50. /**
  51. * tearDown method
  52. *
  53. * @return void
  54. */
  55. public function tearDown() {
  56. unset($this->View);
  57. parent::tearDown();
  58. }
  59. /**
  60. * test String class methods are called correctly
  61. */
  62. public function testTextHelperProxyMethodCalls() {
  63. $methods = array(
  64. 'highlight', 'stripLinks', 'truncate', 'excerpt', 'toList',
  65. );
  66. $String = $this->getMock('StringMock', $methods);
  67. $Text = new TextHelperTestObject($this->View, array('engine' => 'StringMock'));
  68. $Text->attach($String);
  69. foreach ($methods as $method) {
  70. $String->expects($this->at(0))->method($method);
  71. $Text->{$method}('who', 'what', 'when', 'where', 'how');
  72. }
  73. }
  74. /**
  75. * test engine override
  76. */
  77. public function testEngineOverride() {
  78. App::build(array(
  79. 'Utility' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS)
  80. ), App::REGISTER);
  81. $Text = new TextHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
  82. $this->assertInstanceOf('TestAppEngine', $Text->engine());
  83. App::build(array(
  84. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  85. ));
  86. CakePlugin::load('TestPlugin');
  87. $Text = new TextHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
  88. $this->assertInstanceOf('TestPluginEngine', $Text->engine());
  89. CakePlugin::unload('TestPlugin');
  90. }
  91. /**
  92. * testAutoLink method
  93. *
  94. * @return void
  95. */
  96. public function testAutoLink() {
  97. $text = 'This is a test text';
  98. $expected = 'This is a test text';
  99. $result = $this->Text->autoLink($text);
  100. $this->assertEquals($expected, $result);
  101. $text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
  102. $result = $this->Text->autoLink($text);
  103. $expected = 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL and <a href="mailto:test@cakephp\.org">test@cakephp\.org</a> email address';
  104. $this->assertRegExp('#^' . $expected . '$#', $result);
  105. $text = 'This is a test text with URL http://www.cakephp.org';
  106. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  107. $result = $this->Text->autoLink($text);
  108. $this->assertEquals($expected, $result);
  109. $text = 'This is a test text with URL http://www.cakephp.org and some more text';
  110. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
  111. $result = $this->Text->autoLink($text);
  112. $this->assertEquals($expected, $result);
  113. $text = "This is a test text with URL http://www.cakephp.org\tand some more text";
  114. $expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
  115. $result = $this->Text->autoLink($text);
  116. $this->assertEquals($expected, $result);
  117. $text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
  118. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
  119. $result = $this->Text->autoLink($text);
  120. $this->assertEquals($expected, $result);
  121. $text = 'This is a test text with URL http://www.cakephp.org';
  122. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
  123. $result = $this->Text->autoLink($text, array('class' => 'link'));
  124. $this->assertEquals($expected, $result);
  125. $text = 'This is a test text with URL http://www.cakephp.org';
  126. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
  127. $result = $this->Text->autoLink($text, array('class' => 'link', 'id' => 'MyLink'));
  128. $this->assertEquals($expected, $result);
  129. }
  130. /**
  131. * Test escaping for autoLink
  132. *
  133. * @return void
  134. */
  135. public function testAutoLinkEscape() {
  136. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  137. $expected = 'This is a &lt;b&gt;test&lt;/b&gt; text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  138. $result = $this->Text->autoLink($text);
  139. $this->assertEquals($expected, $result);
  140. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  141. $expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  142. $result = $this->Text->autoLink($text, array('escape' => false));
  143. $this->assertEquals($expected, $result);
  144. }
  145. /**
  146. * testAutoLinkUrls method
  147. *
  148. * @return void
  149. */
  150. public function testAutoLinkUrls() {
  151. $text = 'This is a test text';
  152. $expected = 'This is a test text';
  153. $result = $this->Text->autoLinkUrls($text);
  154. $this->assertEquals($expected, $result);
  155. $text = 'This is a test that includes (www.cakephp.org)';
  156. $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
  157. $result = $this->Text->autoLinkUrls($text);
  158. $this->assertEquals($expected, $result);
  159. $text = 'Text with a partial www.cakephp.org URL';
  160. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
  161. $result = $this->Text->autoLinkUrls($text);
  162. $this->assertRegExp('#^' . $expected . '$#', $result);
  163. $text = 'Text with a partial www.cakephp.org URL';
  164. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  165. $result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
  166. $this->assertRegExp('#^' . $expected . '$#', $result);
  167. $text = 'Text with a partial WWW.cakephp.org URL';
  168. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> URL';
  169. $result = $this->Text->autoLinkUrls($text);
  170. $this->assertRegExp('#^' . $expected . '$#', $result);
  171. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  172. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  173. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  174. $this->assertRegExp('#^' . $expected . '$#', $result);
  175. $text = 'Text with a url www.cot.ag/cuIb2Q and more';
  176. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more';
  177. $result = $this->Text->autoLinkUrls($text);
  178. $this->assertEquals($expected, $result);
  179. $text = 'Text with a url http://www.does--not--work.com and more';
  180. $expected = 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more';
  181. $result = $this->Text->autoLinkUrls($text);
  182. $this->assertEquals($expected, $result);
  183. $text = 'Text with a url http://www.not--work.com and more';
  184. $expected = 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more';
  185. $result = $this->Text->autoLinkUrls($text);
  186. $this->assertEquals($expected, $result);
  187. }
  188. /**
  189. * Test autoLinkUrls with the escape option.
  190. *
  191. * @return void
  192. */
  193. public function testAutoLinkUrlsEscape() {
  194. $text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  195. $expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  196. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  197. $this->assertEquals($expected, $result);
  198. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  199. $expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  200. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  201. $this->assertEquals($expected, $result);
  202. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  203. $expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
  204. $result = $this->Text->autoLinkUrls($text, array('escape' => true));
  205. $this->assertEquals($expected, $result);
  206. $text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  207. $expected = 'Text with a url &lt;a href=&quot;http://www.not-working-www.com&quot;&gt;www.not-working-www.com&lt;/a&gt; and more';
  208. $result = $this->Text->autoLinkUrls($text);
  209. $this->assertEquals($expected, $result);
  210. $text = 'Text with a url www.not-working-www.com and more';
  211. $expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  212. $result = $this->Text->autoLinkUrls($text);
  213. $this->assertEquals($expected, $result);
  214. $text = 'Text with a url http://www.not-working-www.com and more';
  215. $expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
  216. $result = $this->Text->autoLinkUrls($text);
  217. $this->assertEquals($expected, $result);
  218. $text = 'Text with a url http://www.www.not-working-www.com and more';
  219. $expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
  220. $result = $this->Text->autoLinkUrls($text);
  221. $this->assertEquals($expected, $result);
  222. }
  223. /**
  224. * testAutoLinkEmails method
  225. *
  226. * @return void
  227. */
  228. public function testAutoLinkEmails() {
  229. $text = 'This is a test text';
  230. $expected = 'This is a test text';
  231. $result = $this->Text->autoLinkUrls($text);
  232. $this->assertEquals($expected, $result);
  233. $text = 'Text with email@example.com address';
  234. $expected = 'Text with <a href="mailto:email@example.com"\s*>email@example.com</a> address';
  235. $result = $this->Text->autoLinkEmails($text);
  236. $this->assertRegExp('#^' . $expected . '$#', $result);
  237. $text = "Text with o'hare._-bob@example.com address";
  238. $expected = 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address';
  239. $result = $this->Text->autoLinkEmails($text);
  240. $this->assertEquals($expected, $result);
  241. $text = 'Text with email@example.com address';
  242. $expected = 'Text with <a href="mailto:email@example.com" \s*class="link">email@example.com</a> address';
  243. $result = $this->Text->autoLinkEmails($text, array('class' => 'link'));
  244. $this->assertRegExp('#^' . $expected . '$#', $result);
  245. }
  246. /**
  247. * test invalid email addresses.
  248. *
  249. * @return void
  250. */
  251. public function testAutoLinkEmailInvalid() {
  252. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  253. $expected = 'this is a myaddress@gmx-de test';
  254. $this->assertEquals($expected, $result);
  255. }
  256. }