TextHelperTest.php 14 KB

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