PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/tests/cases/libs/view/helpers/text.test.php

https://github.com/hardsshah/bookmarks
PHP | 328 lines | 209 code | 22 blank | 97 comment | 2 complexity | 6b995769a0ed3ba9c72cbd24dc0ffbae MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * TextHelperTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.view.helpers
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Helper', 'Text');
  28. /**
  29. * TextHelperTest class
  30. *
  31. * @package cake
  32. * @subpackage cake.tests.cases.libs.view.helpers
  33. */
  34. class TextHelperTest extends CakeTestCase {
  35. /**
  36. * helper property
  37. *
  38. * @var mixed null
  39. * @access public
  40. */
  41. var $helper = null;
  42. /**
  43. * setUp method
  44. *
  45. * @access public
  46. * @return void
  47. */
  48. function setUp() {
  49. $this->Text = new TextHelper();
  50. }
  51. /**
  52. * tearDown method
  53. *
  54. * @access public
  55. * @return void
  56. */
  57. function tearDown() {
  58. unset($this->Text);
  59. }
  60. /**
  61. * testTruncate method
  62. *
  63. * @access public
  64. * @return void
  65. */
  66. function testTruncate() {
  67. if (!isset($this->method)) {
  68. $this->method = 'truncate';
  69. }
  70. $m = $this->method;
  71. $text1 = 'The quick brown fox jumps over the lazy dog';
  72. $text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
  73. $text3 = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
  74. $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?';
  75. $text5 = '0<b>1<i>2<span class="myclass">3</span>4<u>5</u>6</i>7</b>8<b>9</b>0';
  76. $text6 = '<p><strong>Extra dates have been announced for this year\'s tour.</strong></p><p>Tickets for the new shows in</p>';
  77. $text7 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
  78. $text8 = 'Vive la R'.chr(195).chr(169).'publique de France';
  79. $this->assertIdentical($this->Text->{$m}($text1, 15), 'The quick br...');
  80. $this->assertIdentical($this->Text->{$m}($text1, 15, '...', false), 'The quick...');
  81. $this->assertIdentical($this->Text->{$m}($text1, 100), 'The quick brown fox jumps over the lazy dog');
  82. $this->assertIdentical($this->Text->{$m}($text2, 10, '...'), 'Heiz&ou...');
  83. $this->assertIdentical($this->Text->{$m}($text2, 10, '...', false), '...');
  84. $this->assertIdentical($this->Text->{$m}($text3, 20), '<b>&copy; 2005-20...');
  85. $this->assertIdentical($this->Text->{$m}($text4, 15), '<img src="my...');
  86. $this->assertIdentical($this->Text->{$m}($text5, 6, ''), '0<b>1<');
  87. $this->assertIdentical($this->Text->{$m}($text1, 15, array('ending' => '...', 'exact' => true, 'considerHtml' => true)), 'The quick br...');
  88. $this->assertIdentical($this->Text->{$m}($text1, 15, '...', true, true), 'The quick br...');
  89. $this->assertIdentical($this->Text->{$m}($text1, 15, '...', false, true), 'The quick...');
  90. $this->assertIdentical($this->Text->{$m}($text2, 10, '...', true, true), 'Heiz&ouml;lr...');
  91. $this->assertIdentical($this->Text->{$m}($text2, 10, '...', false, true), '...');
  92. $this->assertIdentical($this->Text->{$m}($text3, 20, '...', true, true), '<b>&copy; 2005-2007, Cake...</b>');
  93. $this->assertIdentical($this->Text->{$m}($text4, 15, '...', true, true), '<img src="mypic.jpg"> This image ...');
  94. $this->assertIdentical($this->Text->{$m}($text4, 45, '...', true, true), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But t...</b>');
  95. $this->assertIdentical($this->Text->{$m}($text4, 90, '...', true, 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...');
  96. $this->assertIdentical($this->Text->{$m}($text5, 6, '', true, true), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
  97. $this->assertIdentical($this->Text->{$m}($text5, 20, '', true, true), $text5);
  98. $this->assertIdentical($this->Text->{$m}($text6, 57, '...', false, true), "<p><strong>Extra dates have been announced for this year's...</strong></p>");
  99. $this->assertIdentical($this->Text->{$m}($text7, 255), $text7);
  100. $this->assertIdentical($this->Text->{$m}($text7, 15), 'El moño está...');
  101. $this->assertIdentical($this->Text->{$m}($text8, 15), 'Vive la R'.chr(195).chr(169).'pu...');
  102. if ($this->method == 'truncate') {
  103. $this->method = 'trim';
  104. $this->testTruncate();
  105. }
  106. }
  107. /**
  108. * testHighlight method
  109. *
  110. * @access public
  111. * @return void
  112. */
  113. function testHighlight() {
  114. $text = 'This is a test text';
  115. $phrases = array('This', 'text');
  116. $result = $this->Text->highlight($text, $phrases, '<b>\1</b>');
  117. $expected = '<b>This</b> is a test <b>text</b>';
  118. $this->assertEqual($expected, $result);
  119. $text = 'This is a test text';
  120. $phrases = null;
  121. $result = $this->Text->highlight($text, $phrases, '<b>\1</b>');
  122. $this->assertEqual($result, $text);
  123. $text = 'Ich saß in einem Café am Übergang';
  124. $expected = 'Ich <b>saß</b> in einem <b>Café</b> am <b>Übergang</b>';
  125. $phrases = array('saß', 'café', 'übergang');
  126. $result = $this->Text->highlight($text, $phrases, '<b>\1</b>');
  127. $this->assertEqual($result, $expected);
  128. }
  129. /**
  130. * testHighlightConsiderHtml method
  131. *
  132. * @access public
  133. * @return void
  134. */
  135. function testHighlightConsiderHtml() {
  136. $text1 = '<p>strongbow isn&rsquo;t real cider</p>';
  137. $text2 = '<p>strongbow <strong>isn&rsquo;t</strong> real cider</p>';
  138. $text3 = '<img src="what-a-strong-mouse.png" alt="What a strong mouse!" />';
  139. $this->assertEqual($this->Text->highlight($text1, 'strong', '<b>\1</b>', true), '<p><b>strong</b>bow isn&rsquo;t real cider</p>');
  140. $this->assertEqual($this->Text->highlight($text2, 'strong', '<b>\1</b>', true), '<p><b>strong</b>bow <strong>isn&rsquo;t</strong> real cider</p>');
  141. $this->assertEqual($this->Text->highlight($text3, 'strong', '<b>\1</b>', true), $text3);
  142. }
  143. /**
  144. * testStripLinks method
  145. *
  146. * @access public
  147. * @return void
  148. */
  149. function testStripLinks() {
  150. $text = 'This is a test text';
  151. $expected = 'This is a test text';
  152. $result = $this->Text->stripLinks($text);
  153. $this->assertEqual($expected, $result);
  154. $text = 'This is a <a href="#">test</a> text';
  155. $expected = 'This is a test text';
  156. $result = $this->Text->stripLinks($text);
  157. $this->assertEqual($expected, $result);
  158. $text = 'This <strong>is</strong> a <a href="#">test</a> <a href="#">text</a>';
  159. $expected = 'This <strong>is</strong> a test text';
  160. $result = $this->Text->stripLinks($text);
  161. $this->assertEqual($expected, $result);
  162. $text = 'This <strong>is</strong> a <a href="#">test</a> and <abbr>some</abbr> other <a href="#">text</a>';
  163. $expected = 'This <strong>is</strong> a test and <abbr>some</abbr> other text';
  164. $result = $this->Text->stripLinks($text);
  165. $this->assertEqual($expected, $result);
  166. }
  167. /**
  168. * testAutoLink method
  169. *
  170. * @access public
  171. * @return void
  172. */
  173. function testAutoLink() {
  174. $text = 'This is a test text';
  175. $expected = 'This is a test text';
  176. $result = $this->Text->autoLink($text);
  177. $this->assertEqual($expected, $result);
  178. $text = 'Text with a partial www.cakephp.org URL and test@cakephp.org email address';
  179. $result = $this->Text->autoLink($text);
  180. $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';
  181. $this->assertPattern('#^' . $expected . '$#', $result);
  182. }
  183. /**
  184. * testAutoLinkUrls method
  185. *
  186. * @access public
  187. * @return void
  188. */
  189. function testAutoLinkUrls() {
  190. $text = 'This is a test text';
  191. $expected = 'This is a test text';
  192. $result = $this->Text->autoLinkUrls($text);
  193. $this->assertEqual($expected, $result);
  194. $text = 'This is a test that includes (www.cakephp.org)';
  195. $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
  196. $result = $this->Text->autoLinkUrls($text);
  197. $this->assertEqual($expected, $result);
  198. $text = 'Text with a partial www.cakephp.org URL';
  199. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
  200. $result = $this->Text->autoLinkUrls($text);
  201. $this->assertPattern('#^' . $expected . '$#', $result);
  202. $text = 'Text with a partial www.cakephp.org URL';
  203. $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
  204. $result = $this->Text->autoLinkUrls($text, array('class' => 'link'));
  205. $this->assertPattern('#^' . $expected . '$#', $result);
  206. $text = 'Text with a partial WWW.cakephp.org URL';
  207. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>WWW.cakephp.org</a> URL';
  208. $result = $this->Text->autoLinkUrls($text);
  209. $this->assertPattern('#^' . $expected . '$#', $result);
  210. $text = 'Text with a partial WWW.cakephp.org &copy; URL';
  211. $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>WWW.cakephp.org</a> &copy; URL';
  212. $result = $this->Text->autoLinkUrls($text, array('escape' => false));
  213. $this->assertPattern('#^' . $expected . '$#', $result);
  214. }
  215. /**
  216. * testAutoLinkEmails method
  217. *
  218. * @access public
  219. * @return void
  220. */
  221. function testAutoLinkEmails() {
  222. $text = 'This is a test text';
  223. $expected = 'This is a test text';
  224. $result = $this->Text->autoLinkUrls($text);
  225. $this->assertEqual($expected, $result);
  226. $text = 'Text with email@example.com address';
  227. $expected = 'Text with <a href="mailto:email@example.com"\s*>email@example.com</a> address';
  228. $result = $this->Text->autoLinkEmails($text);
  229. $this->assertPattern('#^' . $expected . '$#', $result);
  230. $text = 'Text with email@example.com address';
  231. $expected = 'Text with <a href="mailto:email@example.com" \s*class="link">email@example.com</a> address';
  232. $result = $this->Text->autoLinkEmails($text, array('class' => 'link'));
  233. $this->assertPattern('#^' . $expected . '$#', $result);
  234. }
  235. /**
  236. * testHighlightCaseInsensitivity method
  237. *
  238. * @access public
  239. * @return void
  240. */
  241. function testHighlightCaseInsensitivity() {
  242. $text = 'This is a Test text';
  243. $expected = 'This is a <b>Test</b> text';
  244. $result = $this->Text->highlight($text, 'test', '<b>\1</b>');
  245. $this->assertEqual($expected, $result);
  246. $result = $this->Text->highlight($text, array('test'), '<b>\1</b>');
  247. $this->assertEqual($expected, $result);
  248. }
  249. /**
  250. * testExcerpt method
  251. *
  252. * @access public
  253. * @return void
  254. */
  255. function testExcerpt() {
  256. $text = 'This is a phrase with test text to play with';
  257. $expected = '...with test text...';
  258. $result = $this->Text->excerpt($text, 'test', 9, '...');
  259. $this->assertEqual($expected, $result);
  260. $expected = 'This is a...';
  261. $result = $this->Text->excerpt($text, 'not_found', 9, '...');
  262. $this->assertEqual($expected, $result);
  263. $expected = 'This is a phras...';
  264. $result = $this->Text->excerpt($text, null, 9, '...');
  265. $this->assertEqual($expected, $result);
  266. $expected = $text;
  267. $result = $this->Text->excerpt($text, null, 200, '...');
  268. $this->assertEqual($expected, $result);
  269. $expected = '...phrase...';
  270. $result = $this->Text->excerpt($text, 'phrase', 2, '...');
  271. $this->assertEqual($expected, $result);
  272. }
  273. /**
  274. * testExcerptCaseInsensitivity method
  275. *
  276. * @access public
  277. * @return void
  278. */
  279. function testExcerptCaseInsensitivity() {
  280. $text = 'This is a phrase with test text to play with';
  281. $expected = '...with test text...';
  282. $result = $this->Text->excerpt($text, 'TEST', 9, '...');
  283. $this->assertEqual($expected, $result);
  284. $expected = 'This is a...';
  285. $result = $this->Text->excerpt($text, 'NOT_FOUND', 9, '...');
  286. $this->assertEqual($expected, $result);
  287. }
  288. /**
  289. * testListGeneration method
  290. *
  291. * @access public
  292. * @return void
  293. */
  294. function testListGeneration() {
  295. $result = $this->Text->toList(array('Larry', 'Curly', 'Moe'));
  296. $this->assertEqual($result, 'Larry, Curly and Moe');
  297. $result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
  298. $this->assertEqual($result, 'Dusty, Lucky y Ned');
  299. }
  300. }
  301. ?>