TextHelperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * TextHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, 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-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html 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 [email protected] 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. * Data provider for autoLinking
  147. */
  148. public static function autoLinkProvider() {
  149. return array(
  150. array(
  151. 'This is a test text',
  152. 'This is a test text',
  153. ),
  154. array(
  155. 'This is a test that includes (www.cakephp.org)',
  156. 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)',
  157. ),
  158. array(
  159. 'This is a test that includes www.cakephp.org:8080',
  160. 'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>',
  161. ),
  162. array(
  163. 'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  164. 'This is a test that includes <a href="http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
  165. ),
  166. array(
  167. 'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  168. 'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
  169. ),
  170. array(
  171. 'This is a test that includes http://example.com/test.php?foo=bar text',
  172. 'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text',
  173. ),
  174. array(
  175. 'This is a test that includes www.example.com/test.php?foo=bar text',
  176. 'This is a test that includes <a href="http://www.example.com/test.php?foo=bar">www.example.com/test.php?foo=bar</a> text',
  177. ),
  178. array(
  179. 'Text with a partial www.cakephp.org URL',
  180. 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL',
  181. ),
  182. array(
  183. 'Text with a partial WWW.cakephp.org URL',
  184. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL',
  185. ),
  186. array(
  187. 'Text with a partial WWW.cakephp.org &copy, URL',
  188. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &amp;copy, URL',
  189. ),
  190. array(
  191. 'Text with a url www.cot.ag/cuIb2Q and more',
  192. 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more',
  193. ),
  194. array(
  195. 'Text with a url http://www.does--not--work.com and more',
  196. 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more',
  197. ),
  198. array(
  199. 'Text with a url http://www.not--work.com and more',
  200. 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more',
  201. ),
  202. );
  203. }
  204. /**
  205. * testAutoLinkUrls method
  206. *
  207. * @dataProvider autoLinkProvider
  208. * @return void
  209. */
  210. public function testAutoLinkUrls($text, $expected) {
  211. $result = $this->Text->autoLinkUrls($text);
  212. $this->assertEquals($expected, $result);
  213. }
  214. /**
  215. * Test the options for autoLinkUrls
  216. *
  217. * @return void
  218. */
  219. public function testAutoLinkUrlsOptions() {
  220. $text = 'Text with a partial www.cakephp.org URL';
  221. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  222. $result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
  223. $this->assertRegExp('#^' . $expected . '$#', $result);
  224. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  225. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  226. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  227. $this->assertRegExp('#^' . $expected . '$#', $result);
  228. }
  229. /**
  230. * Test autoLinkUrls with the escape option.
  231. *
  232. * @return void
  233. */
  234. public function testAutoLinkUrlsEscape() {
  235. $text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  236. $expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  237. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  238. $this->assertEquals($expected, $result);
  239. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  240. $expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  241. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  242. $this->assertEquals($expected, $result);
  243. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  244. $expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
  245. $result = $this->Text->autoLinkUrls($text, array('escape' => true));
  246. $this->assertEquals($expected, $result);
  247. $text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  248. $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';
  249. $result = $this->Text->autoLinkUrls($text);
  250. $this->assertEquals($expected, $result);
  251. $text = 'Text with a url www.not-working-www.com and more';
  252. $expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  253. $result = $this->Text->autoLinkUrls($text);
  254. $this->assertEquals($expected, $result);
  255. $text = 'Text with a url http://www.not-working-www.com and more';
  256. $expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
  257. $result = $this->Text->autoLinkUrls($text);
  258. $this->assertEquals($expected, $result);
  259. $text = 'Text with a url http://www.www.not-working-www.com and more';
  260. $expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
  261. $result = $this->Text->autoLinkUrls($text);
  262. $this->assertEquals($expected, $result);
  263. }
  264. /**
  265. * testAutoLinkEmails method
  266. *
  267. * @return void
  268. */
  269. public function testAutoLinkEmails() {
  270. $text = 'This is a test text';
  271. $expected = 'This is a test text';
  272. $result = $this->Text->autoLinkUrls($text);
  273. $this->assertEquals($expected, $result);
  274. $text = 'Text with [email protected] address';
  275. $expected = 'Text with <a href="mailto:[email protected]"\s*>[email protected]</a> address';
  276. $result = $this->Text->autoLinkEmails($text);
  277. $this->assertRegExp('#^' . $expected . '$#', $result);
  278. $text = "Text with o'[email protected] address";
  279. $expected = 'Text with <a href="mailto:o&#039;[email protected]">o&#039;[email protected]</a> address';
  280. $result = $this->Text->autoLinkEmails($text);
  281. $this->assertEquals($expected, $result);
  282. $text = 'Text with [email protected] address';
  283. $expected = 'Text with <a href="mailto:[email protected]" \s*class="link">[email protected]</a> address';
  284. $result = $this->Text->autoLinkEmails($text, array('class' => 'link'));
  285. $this->assertRegExp('#^' . $expected . '$#', $result);
  286. }
  287. /**
  288. * test invalid email addresses.
  289. *
  290. * @return void
  291. */
  292. public function testAutoLinkEmailInvalid() {
  293. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  294. $expected = 'this is a myaddress@gmx-de test';
  295. $this->assertEquals($expected, $result);
  296. }
  297. }