TextHelperTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. /**
  22. * TextHelperTest class
  23. *
  24. * @package Cake.Test.Case.View.Helper
  25. */
  26. class TextHelperTest extends CakeTestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. $controller = null;
  34. $this->View = new View($controller);
  35. $this->Text = new TextHelper($this->View);
  36. }
  37. /**
  38. * tearDown method
  39. *
  40. * @return void
  41. */
  42. public function tearDown() {
  43. unset($this->View, $this->Text);
  44. }
  45. /**
  46. * testTruncate method
  47. *
  48. * @return void
  49. */
  50. public function testTruncate() {
  51. $text1 = 'The quick brown fox jumps over the lazy dog';
  52. $text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
  53. $text3 = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
  54. $text4 = '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great, or?';
  55. $text5 = '0<b>1<i>2<span class="myclass">3</span>4<u>5</u>6</i>7</b>8<b>9</b>0';
  56. $text6 = '<p><strong>Extra dates have been announced for this year\'s tour.</strong></p><p>Tickets for the new shows in</p>';
  57. $text7 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
  58. $text8 = 'Vive la R'.chr(195).chr(169).'publique de France';
  59. $text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
  60. $this->assertSame($this->Text->truncate($text1, 15), 'The quick br...');
  61. $this->assertSame($this->Text->truncate($text1, 15, array('exact' => false)), 'The quick...');
  62. $this->assertSame($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
  63. $this->assertSame($this->Text->truncate($text2, 10), 'Heiz&ou...');
  64. $this->assertSame($this->Text->truncate($text2, 10, array('exact' => false)), '...');
  65. $this->assertSame($this->Text->truncate($text3, 20), '<b>&copy; 2005-20...');
  66. $this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
  67. $this->assertSame($this->Text->truncate($text5, 6, array('ending' => '')), '0<b>1<');
  68. $this->assertSame($this->Text->truncate($text1, 15, array('html' => true)), 'The quick br...');
  69. $this->assertSame($this->Text->truncate($text1, 15, array('exact' => false, 'html' => true)), 'The quick...');
  70. $this->assertSame($this->Text->truncate($text2, 10, array('html' => true)), 'Heiz&ouml;lr...');
  71. $this->assertSame($this->Text->truncate($text2, 10, array('exact' => false, 'html' => true)), '...');
  72. $this->assertSame($this->Text->truncate($text3, 20, array('html' => true)), '<b>&copy; 2005-2007, Cake...</b>');
  73. $this->assertSame($this->Text->truncate($text4, 15, array('html' => true)), '<img src="mypic.jpg"> This image ...');
  74. $this->assertSame($this->Text->truncate($text4, 45, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But t...</b>');
  75. $this->assertSame($this->Text->truncate($text4, 90, array('html' => true)), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Grea...');
  76. $this->assertSame($this->Text->truncate($text5, 6, array('ending' => '', 'html' => true)), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
  77. $this->assertSame($this->Text->truncate($text5, 20, array('ending' => '', 'html' => true)), $text5);
  78. $this->assertSame($this->Text->truncate($text6, 57, array('exact' => false, 'html' => true)), "<p><strong>Extra dates have been announced for this year's...</strong></p>");
  79. $this->assertSame($this->Text->truncate($text7, 255), $text7);
  80. $this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
  81. $this->assertSame($this->Text->truncate($text8, 15), 'Vive la R'.chr(195).chr(169).'pu...');
  82. $this->assertSame($this->Text->truncate($text9, 10), 'НОПРСТУ...');
  83. }
  84. /**
  85. * testHighlight method
  86. *
  87. * @return void
  88. */
  89. public function testHighlight() {
  90. $text = 'This is a test text';
  91. $phrases = array('This', 'text');
  92. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
  93. $expected = '<b>This</b> is a test <b>text</b>';
  94. $this->assertEquals($expected, $result);
  95. $text = 'This is a test text';
  96. $phrases = null;
  97. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
  98. $this->assertEquals($result, $text);
  99. $text = 'This is a (test) text';
  100. $phrases = '(test';
  101. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
  102. $this->assertEquals('This is a <b>(test</b>) text', $result);
  103. $text = 'Ich saß in einem Café am Übergang';
  104. $expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
  105. $phrases = array('saß', 'café', 'übergang');
  106. $result = $this->Text->highlight($text, $phrases, array('format' => '<b>\1</b>'));
  107. $this->assertEquals($expected, $result);
  108. }
  109. /**
  110. * testHighlightHtml method
  111. *
  112. * @return void
  113. */
  114. public function testHighlightHtml() {
  115. $text1 = '<p>strongbow isn&rsquo;t real cider</p>';
  116. $text2 = '<p>strongbow <strong>isn&rsquo;t</strong> real cider</p>';
  117. $text3 = '<img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
  118. $text4 = 'What a strong mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
  119. $options = array('format' => '<b>\1</b>', 'html' => true);
  120. $expected = '<p><b>strong</b>bow isn&rsquo;t real cider</p>';
  121. $this->assertEquals($this->Text->highlight($text1, 'strong', $options), $expected);
  122. $expected = '<p><b>strong</b>bow <strong>isn&rsquo;t</strong> real cider</p>';
  123. $this->assertEquals($this->Text->highlight($text2, 'strong', $options), $expected);
  124. $this->assertEquals($this->Text->highlight($text3, 'strong', $options), $text3);
  125. $this->assertEquals($this->Text->highlight($text3, array('strong', 'what'), $options), $text3);
  126. $expected = '<b>What</b> a <b>strong</b> mouse: <img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
  127. $this->assertEquals($this->Text->highlight($text4, array('strong', 'what'), $options), $expected);
  128. }
  129. /**
  130. * testHighlightMulti method
  131. *
  132. * @return void
  133. */
  134. public function testHighlightMulti() {
  135. $text = 'This is a test text';
  136. $phrases = array('This', 'text');
  137. $result = $this->Text->highlight($text, $phrases, array('format' => array('<b>\1</b>', '<em>\1</em>')));
  138. $expected = '<b>This</b> is a test <em>text</em>';
  139. $this->assertEquals($expected, $result);
  140. }
  141. /**
  142. * testStripLinks method
  143. *
  144. * @return void
  145. */
  146. public function testStripLinks() {
  147. $text = 'This is a test text';
  148. $expected = 'This is a test text';
  149. $result = $this->Text->stripLinks($text);
  150. $this->assertEquals($expected, $result);
  151. $text = 'This is a <a href="#">test</a> text';
  152. $expected = 'This is a test text';
  153. $result = $this->Text->stripLinks($text);
  154. $this->assertEquals($expected, $result);
  155. $text = 'This <strong>is</strong> a <a href="#">test</a> <a href="#">text</a>';
  156. $expected = 'This <strong>is</strong> a test text';
  157. $result = $this->Text->stripLinks($text);
  158. $this->assertEquals($expected, $result);
  159. $text = 'This <strong>is</strong> a <a href="#">test</a> and <abbr>some</abbr> other <a href="#">text</a>';
  160. $expected = 'This <strong>is</strong> a test and <abbr>some</abbr> other text';
  161. $result = $this->Text->stripLinks($text);
  162. $this->assertEquals($expected, $result);
  163. }
  164. /**
  165. * testAutoLink method
  166. *
  167. * @return void
  168. */
  169. public function testAutoLink() {
  170. $text = 'This is a test text';
  171. $expected = 'This is a test text';
  172. $result = $this->Text->autoLink($text);
  173. $this->assertEquals($expected, $result);
  174. $text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
  175. $result = $this->Text->autoLink($text);
  176. $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';
  177. $this->assertRegExp('#^' . $expected . '$#', $result);
  178. $text = 'This is a test text with URL http://www.cakephp.org';
  179. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  180. $result = $this->Text->autoLink($text);
  181. $this->assertEquals($expected, $result);
  182. $text = 'This is a test text with URL http://www.cakephp.org and some more text';
  183. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a> and some more text';
  184. $result = $this->Text->autoLink($text);
  185. $this->assertEquals($expected, $result);
  186. $text = "This is a test text with URL http://www.cakephp.org\tand some more text";
  187. $expected = "This is a test text with URL <a href=\"http://www.cakephp.org\">http://www.cakephp.org</a>\tand some more text";
  188. $result = $this->Text->autoLink($text);
  189. $this->assertEquals($expected, $result);
  190. $text = 'This is a test text with URL http://www.cakephp.org(and some more text)';
  191. $expected = 'This is a test text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>(and some more text)';
  192. $result = $this->Text->autoLink($text);
  193. $this->assertEquals($expected, $result);
  194. $text = 'This is a test text with URL http://www.cakephp.org';
  195. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link">http://www.cakephp.org</a>';
  196. $result = $this->Text->autoLink($text, array('class' => 'link'));
  197. $this->assertEquals($expected, $result);
  198. $text = 'This is a test text with URL http://www.cakephp.org';
  199. $expected = 'This is a test text with URL <a href="http://www.cakephp.org" class="link" id="MyLink">http://www.cakephp.org</a>';
  200. $result = $this->Text->autoLink($text, array('class' => 'link', 'id' => 'MyLink'));
  201. $this->assertEquals($expected, $result);
  202. }
  203. /**
  204. * Test escaping for autoLink
  205. *
  206. * @return void
  207. */
  208. public function testAutoLinkEscape() {
  209. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  210. $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>';
  211. $result = $this->Text->autoLink($text);
  212. $this->assertEquals($expected, $result);
  213. $text = 'This is a <b>test</b> text with URL http://www.cakephp.org';
  214. $expected = 'This is a <b>test</b> text with URL <a href="http://www.cakephp.org">http://www.cakephp.org</a>';
  215. $result = $this->Text->autoLink($text, array('escape' => false));
  216. $this->assertEquals($expected, $result);
  217. }
  218. /**
  219. * testAutoLinkUrls method
  220. *
  221. * @return void
  222. */
  223. public function testAutoLinkUrls() {
  224. $text = 'This is a test text';
  225. $expected = 'This is a test text';
  226. $result = $this->Text->autoLinkUrls($text);
  227. $this->assertEquals($expected, $result);
  228. $text = 'This is a test that includes (www.cakephp.org)';
  229. $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
  230. $result = $this->Text->autoLinkUrls($text);
  231. $this->assertEquals($expected, $result);
  232. $text = 'Text with a partial www.cakephp.org URL';
  233. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
  234. $result = $this->Text->autoLinkUrls($text);
  235. $this->assertRegExp('#^' . $expected . '$#', $result);
  236. $text = 'Text with a partial www.cakephp.org URL';
  237. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  238. $result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
  239. $this->assertRegExp('#^' . $expected . '$#', $result);
  240. $text = 'Text with a partial WWW.cakephp.org URL';
  241. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> URL';
  242. $result = $this->Text->autoLinkUrls($text);
  243. $this->assertRegExp('#^' . $expected . '$#', $result);
  244. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  245. $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  246. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  247. $this->assertRegExp('#^' . $expected . '$#', $result);
  248. $text = 'Text with a url www.cot.ag/cuIb2Q and more';
  249. $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more';
  250. $result = $this->Text->autoLinkUrls($text);
  251. $this->assertEquals($expected, $result);
  252. $text = 'Text with a url http://www.does--not--work.com and more';
  253. $expected = 'Text with a url <a href="http://www.does--not--work.com">http://www.does--not--work.com</a> and more';
  254. $result = $this->Text->autoLinkUrls($text);
  255. $this->assertEquals($expected, $result);
  256. $text = 'Text with a url http://www.not--work.com and more';
  257. $expected = 'Text with a url <a href="http://www.not--work.com">http://www.not--work.com</a> and more';
  258. $result = $this->Text->autoLinkUrls($text);
  259. $this->assertEquals($expected, $result);
  260. }
  261. /**
  262. * Test autoLinkUrls with the escape option.
  263. *
  264. * @return void
  265. */
  266. public function testAutoLinkUrlsEscape() {
  267. $text = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  268. $expected = 'Text with a partial <a href="http://www.cakephp.org">link</a> link';
  269. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  270. $this->assertEquals($expected, $result);
  271. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  272. $expected = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  273. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  274. $this->assertEquals($expected, $result);
  275. $text = 'Text with a partial <iframe src="http://www.cakephp.org" /> link';
  276. $expected = 'Text with a partial &lt;iframe src=&quot;http://www.cakephp.org&quot; /&gt; link';
  277. $result = $this->Text->autoLinkUrls($text, array('escape' => true));
  278. $this->assertEquals($expected, $result);
  279. }
  280. /**
  281. * testAutoLinkEmails method
  282. *
  283. * @return void
  284. */
  285. public function testAutoLinkEmails() {
  286. $text = 'This is a test text';
  287. $expected = 'This is a test text';
  288. $result = $this->Text->autoLinkUrls($text);
  289. $this->assertEquals($expected, $result);
  290. $text = 'Text with email@example.com address';
  291. $expected = 'Text with <a href="mailto:email@example.com"\s*>email@example.com</a> address';
  292. $result = $this->Text->autoLinkEmails($text);
  293. $this->assertRegExp('#^' . $expected . '$#', $result);
  294. $text = "Text with o'hare._-bob@example.com address";
  295. $expected = 'Text with <a href="mailto:o&#039;hare._-bob@example.com">o&#039;hare._-bob@example.com</a> address';
  296. $result = $this->Text->autoLinkEmails($text);
  297. $this->assertEquals($expected, $result);
  298. $text = 'Text with email@example.com address';
  299. $expected = 'Text with <a href="mailto:email@example.com" \s*class="link">email@example.com</a> address';
  300. $result = $this->Text->autoLinkEmails($text, array('class' => 'link'));
  301. $this->assertRegExp('#^' . $expected . '$#', $result);
  302. }
  303. /**
  304. * test invalid email addresses.
  305. *
  306. * @return void
  307. */
  308. public function testAutoLinkEmailInvalid() {
  309. $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
  310. $expected = 'this is a myaddress@gmx-de test';
  311. $this->assertEquals($expected, $result);
  312. }
  313. /**
  314. * testHighlightCaseInsensitivity method
  315. *
  316. * @return void
  317. */
  318. public function testHighlightCaseInsensitivity() {
  319. $text = 'This is a Test text';
  320. $expected = 'This is a <b>Test</b> text';
  321. $result = $this->Text->highlight($text, 'test', array('format' => '<b>\1</b>'));
  322. $this->assertEquals($expected, $result);
  323. $result = $this->Text->highlight($text, array('test'), array('format' => '<b>\1</b>'));
  324. $this->assertEquals($expected, $result);
  325. }
  326. /**
  327. * testExcerpt method
  328. *
  329. * @return void
  330. */
  331. public function testExcerpt() {
  332. $text = 'This is a phrase with test text to play with';
  333. $expected = '...ase with test text to ...';
  334. $result = $this->Text->excerpt($text, 'test', 9, '...');
  335. $this->assertEquals($expected, $result);
  336. $expected = 'This is a...';
  337. $result = $this->Text->excerpt($text, 'not_found', 9, '...');
  338. $this->assertEquals($expected, $result);
  339. $expected = 'This is a phras...';
  340. $result = $this->Text->excerpt($text, null, 9, '...');
  341. $this->assertEquals($expected, $result);
  342. $expected = $text;
  343. $result = $this->Text->excerpt($text, null, 200, '...');
  344. $this->assertEquals($expected, $result);
  345. $expected = '...a phrase w...';
  346. $result = $this->Text->excerpt($text, 'phrase', 2, '...');
  347. $this->assertEquals($expected, $result);
  348. $phrase = 'This is a phrase with test text';
  349. $expected = $text;
  350. $result = $this->Text->excerpt($text, $phrase, 13, '...');
  351. $this->assertEquals($expected, $result);
  352. $text = 'aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa';
  353. $phrase = 'bbbbbbbb';
  354. $result = $this->Text->excerpt($text, $phrase, 10);
  355. $expected = '...aaaaaaaaaabbbbbbbbaaaaaaaaaa...';
  356. $this->assertEquals($expected, $result);
  357. }
  358. /**
  359. * testExcerptCaseInsensitivity method
  360. *
  361. * @return void
  362. */
  363. public function testExcerptCaseInsensitivity() {
  364. $text = 'This is a phrase with test text to play with';
  365. $expected = '...ase with test text to ...';
  366. $result = $this->Text->excerpt($text, 'TEST', 9, '...');
  367. $this->assertEquals($expected, $result);
  368. $expected = 'This is a...';
  369. $result = $this->Text->excerpt($text, 'NOT_FOUND', 9, '...');
  370. $this->assertEquals($expected, $result);
  371. }
  372. /**
  373. * testListGeneration method
  374. *
  375. * @return void
  376. */
  377. public function testListGeneration() {
  378. $result = $this->Text->toList(array());
  379. $this->assertEquals($result, '');
  380. $result = $this->Text->toList(array('One'));
  381. $this->assertEquals($result, 'One');
  382. $result = $this->Text->toList(array('Larry', 'Curly', 'Moe'));
  383. $this->assertEquals($result, 'Larry, Curly and Moe');
  384. $result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
  385. $this->assertEquals($result, 'Dusty, Lucky y Ned');
  386. $result = $this->Text->toList(array(1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
  387. $this->assertEquals($result, 'Dusty, Lucky y Ned');
  388. $result = $this->Text->toList(array(1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'and', ' + ');
  389. $this->assertEquals($result, 'Dusty + Lucky and Ned');
  390. $result = $this->Text->toList(array('name1' => 'Dusty', 'name2' => 'Lucky'));
  391. $this->assertEquals($result, 'Dusty and Lucky');
  392. $result = $this->Text->toList(array('test_0' => 'banana', 'test_1' => 'apple', 'test_2' => 'lemon'));
  393. $this->assertEquals($result, 'banana, apple and lemon');
  394. }
  395. }