PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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