TextHelperTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('View', 'View');
  21. App::uses('TextHelper', 'View/Helper');
  22. class TextHelperTestObject extends TextHelper {
  23. public function attach(StringMock $string) {
  24. $this->_engine = $string;
  25. }
  26. public function engine() {
  27. return $this->_engine;
  28. }
  29. }
  30. /**
  31. * StringMock class
  32. */
  33. class StringMock {
  34. }
  35. /**
  36. * TextHelperTest class
  37. *
  38. * @package Cake.Test.Case.View.Helper
  39. */
  40. class TextHelperTest extends CakeTestCase {
  41. /**
  42. * setUp method
  43. *
  44. * @return void
  45. */
  46. public function setUp() {
  47. parent::setUp();
  48. $this->View = new View(null);
  49. $this->Text = new TextHelper($this->View);
  50. }
  51. /**
  52. * tearDown method
  53. *
  54. * @return void
  55. */
  56. public function tearDown() {
  57. unset($this->View);
  58. parent::tearDown();
  59. }
  60. /**
  61. * test String class methods are called correctly
  62. */
  63. public function testTextHelperProxyMethodCalls() {
  64. $methods = array(
  65. 'highlight', 'stripLinks', 'truncate', 'excerpt', 'toList',
  66. );
  67. $String = $this->getMock('StringMock', $methods);
  68. $Text = new TextHelperTestObject($this->View, array('engine' => 'StringMock'));
  69. $Text->attach($String);
  70. foreach ($methods as $method) {
  71. $String->expects($this->at(0))->method($method);
  72. $Text->{$method}('who', 'what', 'when', 'where', 'how');
  73. }
  74. }
  75. /**
  76. * test engine override
  77. */
  78. public function testEngineOverride() {
  79. App::build(array(
  80. 'Utility' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS)
  81. ), App::REGISTER);
  82. $Text = new TextHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
  83. $this->assertInstanceOf('TestAppEngine', $Text->engine());
  84. App::build(array(
  85. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  86. ));
  87. CakePlugin::load('TestPlugin');
  88. $Text = new TextHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
  89. $this->assertInstanceOf('TestPluginEngine', $Text->engine());
  90. CakePlugin::unload('TestPlugin');
  91. }
  92. /**
  93. * testAutoLink method
  94. *
  95. * @return void
  96. */
  97. public function testAutoLink() {
  98. $text = 'This is a test text';
  99. $expected = 'This is a test text';
  100. $result = $this->Text->autoLink($text);
  101. $this->assertEquals($expected, $result);
  102. $text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
  103. $result = $this->Text->autoLink($text);
  104. $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';
  105. $this->assertRegExp('#^' . $expected . '$#', $result);
  106. $text = 'This is a test text with URL http://www.cakephp.org';
  107. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  108. $result = $this->Text->autoLink($text);
  109. $this->assertEquals($expected, $result);
  110. $text = 'This is a test text with URL http://www.cakephp.org and some more text';
  111. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
  112. $result = $this->Text->autoLink($text);
  113. $this->assertEquals($expected, $result);
  114. $text = "This is a test text with URL http://www.cakephp.org\tand some more text";
  115. $expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
  116. $result = $this->Text->autoLink($text);
  117. $this->assertEquals($expected, $result);
  118. $text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
  119. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
  120. $result = $this->Text->autoLink($text);
  121. $this->assertEquals($expected, $result);
  122. $text = 'This is a test text with URL http://www.cakephp.org';
  123. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
  124. $result = $this->Text->autoLink($text, array('class' => 'link'));
  125. $this->assertEquals($expected, $result);
  126. $text = 'This is a test text with URL http://www.cakephp.org';
  127. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
  128. $result = $this->Text->autoLink($text, array('class' => 'link', 'id' => 'MyLink'));
  129. $this->assertEquals($expected, $result);
  130. }
  131. /**
  132. * Test escaping for autoLink
  133. *
  134. * @return void
  135. */
  136. public function testAutoLinkEscape() {
  137. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  138. $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>';
  139. $result = $this->Text->autoLink($text);
  140. $this->assertEquals($expected, $result);
  141. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  142. $expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  143. $result = $this->Text->autoLink($text, array('escape' => false));
  144. $this->assertEquals($expected, $result);
  145. }
  146. /**
  147. * Data provider for autoLinking
  148. */
  149. public static function autoLinkProvider() {
  150. return array(
  151. array(
  152. 'This is a test text',
  153. 'This is a test text',
  154. ),
  155. array(
  156. 'This is a test that includes (www.cakephp.org)',
  157. 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)',
  158. ),
  159. array(
  160. 'This is a test that includes www.cakephp.org:8080',
  161. 'This is a test that includes <a href="http://www.cakephp.org:8080">www.cakephp.org:8080</a>',
  162. ),
  163. array(
  164. 'This is a test that includes http://de.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  165. '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>',
  166. ),
  167. array(
  168. 'This is a test that includes www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment',
  169. 'This is a test that includes <a href="http://www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment">www.wikipedia.org/wiki/Kanton_(Schweiz)#fragment</a>',
  170. ),
  171. array(
  172. 'This is a test that includes http://example.com/test.php?foo=bar text',
  173. 'This is a test that includes <a href="http://example.com/test.php?foo=bar">http://example.com/test.php?foo=bar</a> text',
  174. ),
  175. array(
  176. 'This is a test that includes www.example.com/test.php?foo=bar text',
  177. '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',
  178. ),
  179. array(
  180. 'Text with a partial www.cakephp.org URL',
  181. 'Text with a partial <a href="http://www.cakephp.org">www.cakephp.org</a> URL',
  182. ),
  183. array(
  184. 'Text with a partial WWW.cakephp.org URL',
  185. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> URL',
  186. ),
  187. array(
  188. 'Text with a partial WWW.cakephp.org &copy, URL',
  189. 'Text with a partial <a href="http://WWW.cakephp.org">WWW.cakephp.org</a> &amp;copy, URL',
  190. ),
  191. array(
  192. 'Text with a url www.cot.ag/cuIb2Q and more',
  193. 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more',
  194. ),
  195. array(
  196. 'Text with a url http://www.does--not--work.com and more',
  197. 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more',
  198. ),
  199. array(
  200. 'Text with a url http://www.not--work.com and more',
  201. 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more',
  202. ),
  203. );
  204. }
  205. /**
  206. * testAutoLinkUrls method
  207. *
  208. * @dataProvider autoLinkProvider
  209. * @return void
  210. */
  211. public function testAutoLinkUrls($text, $expected) {
  212. $result = $this->Text->autoLinkUrls($text);
  213. $this->assertEquals($expected, $result);
  214. }
  215. /**
  216. * Test the options for autoLinkUrls
  217. *
  218. * @return void
  219. */
  220. public function testAutoLinkUrlsOptions() {
  221. $text = 'Text with a partial www.cakephp.org URL';
  222. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  223. $result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
  224. $this->assertRegExp('#^' . $expected . '$#', $result);
  225. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  226. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  227. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  228. $this->assertRegExp('#^' . $expected . '$#', $result);
  229. }
  230. /**
  231. * Test autoLinkUrls with the escape option.
  232. *
  233. * @return void
  234. */
  235. public function testAutoLinkUrlsEscape() {
  236. $text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  237. $expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  238. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  239. $this->assertEquals($expected, $result);
  240. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  241. $expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  242. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  243. $this->assertEquals($expected, $result);
  244. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  245. $expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
  246. $result = $this->Text->autoLinkUrls($text, array('escape' => true));
  247. $this->assertEquals($expected, $result);
  248. $text = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  249. $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';
  250. $result = $this->Text->autoLinkUrls($text);
  251. $this->assertEquals($expected, $result);
  252. $text = 'Text with a url www.not-working-www.com and more';
  253. $expected = 'Text with a url <a href="http://www.not-working-www.com">www.not-working-www.com</a> and more';
  254. $result = $this->Text->autoLinkUrls($text);
  255. $this->assertEquals($expected, $result);
  256. $text = 'Text with a url http://www.not-working-www.com and more';
  257. $expected = 'Text with a url <a href="http://www.not-working-www.com">http://www.not-working-www.com</a> and more';
  258. $result = $this->Text->autoLinkUrls($text);
  259. $this->assertEquals($expected, $result);
  260. $text = 'Text with a url http://www.www.not-working-www.com and more';
  261. $expected = 'Text with a url <a href="http://www.www.not-working-www.com">http://www.www.not-working-www.com</a> and more';
  262. $result = $this->Text->autoLinkUrls($text);
  263. $this->assertEquals($expected, $result);
  264. }
  265. /**
  266. * testAutoLinkEmails method
  267. *
  268. * @return void
  269. */
  270. public function testAutoLinkEmails() {
  271. $text = 'This is a test text';
  272. $expected = 'This is a test text';
  273. $result = $this->Text->autoLinkUrls($text);
  274. $this->assertEquals($expected, $result);
  275. $text = 'Text with email@example.com address';
  276. $expected = 'Text with <a href="mailto:email@example.com"\s*>email@example.com</a> address';
  277. $result = $this->Text->autoLinkEmails($text);
  278. $this->assertRegExp('#^' . $expected . '$#', $result);
  279. $text = "Text with o'hare._-bob@example.com address";
  280. $expected = 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address';
  281. $result = $this->Text->autoLinkEmails($text);
  282. $this->assertEquals($expected, $result);
  283. $text = 'Text with email@example.com address';
  284. $expected = 'Text with <a href="mailto:email@example.com" \s*class="link">email@example.com</a> address';
  285. $result = $this->Text->autoLinkEmails($text, array('class' => 'link'));
  286. $this->assertRegExp('#^' . $expected . '$#', $result);
  287. }
  288. /**
  289. * test invalid email addresses.
  290. *
  291. * @return void
  292. */
  293. public function testAutoLinkEmailInvalid() {
  294. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  295. $expected = 'this is a myaddress@gmx-de test';
  296. $this->assertEquals($expected, $result);
  297. }
  298. /**
  299. * testAutoParagraph method
  300. *
  301. * @return void
  302. */
  303. public function testAutoParagraph() {
  304. $text = 'This is a test text';
  305. $expected = <<<TEXT
  306. <p>This is a test text</p>
  307. TEXT;
  308. $result = $this->Text->autoParagraph($text);
  309. $text = 'This is a <br/> <BR> test text';
  310. $expected = <<<TEXT
  311. <p>This is a </p>
  312. <p> test text</p>
  313. TEXT;
  314. $result = $this->Text->autoParagraph($text);
  315. $this->assertEquals($expected, $result);
  316. $result = $this->Text->autoParagraph($text);
  317. $text = 'This is a <BR id="test"/><br class="test"> test text';
  318. $expected = <<<TEXT
  319. <p>This is a </p>
  320. <p> test text</p>
  321. TEXT;
  322. $result = $this->Text->autoParagraph($text);
  323. $this->assertEquals($expected, $result);
  324. $text = <<<TEXT
  325. This is a test text.
  326. This is a line return.
  327. TEXT;
  328. $expected = <<<TEXT
  329. <p>This is a test text.<br />
  330. This is a line return.</p>
  331. TEXT;
  332. $result = $this->Text->autoParagraph($text);
  333. $this->assertEquals($expected, $result);
  334. $text = <<<TEXT
  335. This is a test text.
  336. This is a new line.
  337. TEXT;
  338. $expected = <<<TEXT
  339. <p>This is a test text.</p>
  340. <p>This is a new line.</p>
  341. TEXT;
  342. $result = $this->Text->autoParagraph($text);
  343. $this->assertEquals($expected, $result);
  344. }
  345. }