PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/tests/kohana/TextTest.php

https://bitbucket.org/seyar/kinda.local
PHP | 596 lines | 321 code | 57 blank | 218 comment | 3 complexity | 0f6e29bf97cfd5d7c1423d0c3d124e4e MD5 | raw file
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests the kohana text class (Kohana_Text)
  4. *
  5. * @group kohana
  6. */
  7. Class Kohana_TextTest extends Kohana_Unittest_TestCase
  8. {
  9. /**
  10. * Sets up the test enviroment
  11. */
  12. public function setUp()
  13. {
  14. parent::setUp();
  15. Text::alternate();
  16. }
  17. /**
  18. * This test makes sure that auto_p returns an empty string if
  19. * an empty input was provided
  20. *
  21. * @test
  22. * @covers Text::auto_p
  23. */
  24. public function test_auto_para_returns_empty_string_on_empty_input()
  25. {
  26. $this->assertSame('', Text::auto_p(''));
  27. }
  28. /**
  29. *
  30. * @return array Test Data
  31. */
  32. public function provider_auto_para_does_not_enclose_html_tags_in_paragraphs()
  33. {
  34. return array(
  35. array(
  36. array('div'),
  37. '<div>Pick a plum of peppers</div>',
  38. ),
  39. array(
  40. array('div'),
  41. '<div id="awesome">Tangas</div>',
  42. ),
  43. );
  44. }
  45. /**
  46. * This test makes sure that auto_p doesn't enclose HTML tags
  47. * in paragraphs
  48. *
  49. * @test
  50. * @covers Text::auto_p
  51. * @dataProvider provider_auto_para_does_not_enclose_html_tags_in_paragraphs
  52. */
  53. public function test_auto_para_does_not_enclose_html_tags_in_paragraphs(array $tags, $text)
  54. {
  55. $output = Text::auto_p($text);
  56. foreach($tags as $tag)
  57. {
  58. $this->assertNotTag(
  59. array('tag' => $tag, 'ancestor' => array('tag' => 'p')),
  60. $output
  61. );
  62. }
  63. }
  64. /**
  65. * This test makes sure that auto_p surrounds a single line of text
  66. * with paragraph tags
  67. *
  68. * @test
  69. * @covers Text::auto_p
  70. */
  71. public function test_auto_para_encloses_slot_in_paragraph()
  72. {
  73. $text = 'Pick a pinch of purple pepper';
  74. $this->assertSame('<p>'.$text.'</p>', Text::auto_p($text));
  75. }
  76. /**
  77. * Make sure that multiple new lines are replaced with paragraph tags
  78. *
  79. * @test
  80. * @covers Text::auto_p
  81. */
  82. public function test_auto_para_replaces_multiple_newlines_with_paragraph()
  83. {
  84. $this->assertSame(
  85. "<p>My name is john</p>\n\n<p>I'm a developer</p>",
  86. Text::auto_p("My name is john\n\n\n\nI'm a developer")
  87. );
  88. }
  89. /**
  90. * Data provider for test_limit_words
  91. *
  92. * @return array Array of test data
  93. */
  94. public function provider_limit_words()
  95. {
  96. return array
  97. (
  98. array('', '', 100, NULL),
  99. array('…', 'The rain in spain', -10, NULL),
  100. array('The rain…', 'The rain in spain', 2, NULL),
  101. array('The rain...', 'The rain in spain', 2, '...'),
  102. );
  103. }
  104. /**
  105. *
  106. * @test
  107. * @dataProvider provider_limit_words
  108. */
  109. public function test_limit_words($expected, $str, $limit, $end_char)
  110. {
  111. $this->assertSame($expected, Text::limit_words($str, $limit, $end_char));
  112. }
  113. /**
  114. * Provides test data for test_limit_chars()
  115. *
  116. * @return array Test data
  117. */
  118. public function provider_limit_chars()
  119. {
  120. return array
  121. (
  122. array('', '', 100, NULL, FALSE),
  123. array('…', 'BOO!', -42, NULL, FALSE),
  124. array('making php bet…', 'making php better for the sane', 14, NULL, FALSE),
  125. array('Garçon! Un café s.v.p.', 'Garçon! Un café s.v.p.', 50, '__', FALSE),
  126. array('Garçon!__', 'Garçon! Un café s.v.p.', 8, '__', FALSE),
  127. // @issue 3238
  128. array('making php…', 'making php better for the sane', 14, NULL, TRUE),
  129. array('Garçon!__', 'Garçon! Un café s.v.p.', 9, '__', TRUE),
  130. array('Garçon!__', 'Garçon! Un café s.v.p.', 7, '__', TRUE),
  131. array('__', 'Garçon! Un café s.v.p.', 5, '__', TRUE),
  132. );
  133. }
  134. /**
  135. * Tests Text::limit_chars()
  136. *
  137. * @test
  138. * @dataProvider provider_limit_chars
  139. */
  140. public function test_limit_chars($expected, $str, $limit, $end_char, $preserve_words)
  141. {
  142. $this->assertSame($expected, Text::limit_chars($str, $limit, $end_char, $preserve_words));
  143. }
  144. /**
  145. * Test Text::alternate()
  146. *
  147. * @test
  148. */
  149. public function test_alternate_alternates_between_parameters()
  150. {
  151. list($val_a, $val_b, $val_c) = array('good', 'bad', 'ugly');
  152. $this->assertSame('good', Text::alternate($val_a, $val_b, $val_c));
  153. $this->assertSame('bad', Text::alternate($val_a, $val_b, $val_c));
  154. $this->assertSame('ugly', Text::alternate($val_a, $val_b, $val_c));
  155. $this->assertSame('good', Text::alternate($val_a, $val_b, $val_c));
  156. }
  157. /**
  158. * Tests Text::alternate()
  159. *
  160. * @test
  161. * @covers Text::alternate
  162. */
  163. public function test_alternate_resets_when_called_with_no_params_and_returns_empty_string()
  164. {
  165. list($val_a, $val_b, $val_c) = array('yes', 'no', 'maybe');
  166. $this->assertSame('yes', Text::alternate($val_a, $val_b, $val_c));
  167. $this->assertSame('', Text::alternate());
  168. $this->assertSame('yes', Text::alternate($val_a, $val_b, $val_c));
  169. }
  170. /**
  171. * Provides test data for test_reducde_slashes()
  172. *
  173. * @returns array Array of test data
  174. */
  175. public function provider_reduce_slashes()
  176. {
  177. return array
  178. (
  179. array('/', '//'),
  180. array('/google/php/kohana/', '//google/php//kohana//'),
  181. );
  182. }
  183. /**
  184. * Covers Text::reduce_slashes()
  185. *
  186. * @test
  187. * @dataProvider provider_reduce_slashes
  188. */
  189. public function test_reduce_slashes($expected, $str)
  190. {
  191. $this->assertSame($expected, Text::reduce_slashes($str));
  192. }
  193. /**
  194. * Provides test data for test_censor()
  195. *
  196. * @return array Test data
  197. */
  198. public function provider_censor()
  199. {
  200. return array
  201. (
  202. // If the replacement is 1 character long it should be repeated for the length of the removed word
  203. array("A donkey is also an ***", 'A donkey is also an ass', array('ass'), '*', TRUE),
  204. array("Cake### isn't nearly as good as kohana###", "CakePHP isn't nearly as good as kohanaphp", array('php'), '#', TRUE),
  205. // If it's > 1 then it's just replaced straight out
  206. array("If you're born out of wedlock you're a --expletive--", "If you're born out of wedlock you're a child", array('child'), '--expletive--', TRUE),
  207. array('class', 'class', array('ass'), '*', FALSE),
  208. );
  209. }
  210. /**
  211. * Tests Text::censor
  212. *
  213. * @test
  214. * @dataProvider provider_censor
  215. */
  216. public function test_censor($expected, $str, $badwords, $replacement, $replace_partial_words)
  217. {
  218. $this->assertSame($expected, Text::censor($str, $badwords, $replacement, $replace_partial_words));
  219. }
  220. /**
  221. * Provides test data for test_random
  222. *
  223. * @return array Test Data
  224. */
  225. public function provider_random()
  226. {
  227. return array(
  228. array('alnum', 8),
  229. array('alpha', 10),
  230. array('hexdec', 20),
  231. array('nozero', 5),
  232. array('numeric', 14),
  233. array('distinct', 12),
  234. array('aeiou', 4),
  235. array('‹¡›«¿»', 8), // UTF8 characters
  236. array(NULL, 8), // Issue #3256
  237. );
  238. }
  239. /**
  240. * Tests Text::random() as well as possible
  241. *
  242. * Obviously you can't compare a randomly generated string against a
  243. * pre-generated one and check that they are the same as this goes
  244. * against the whole ethos of random.
  245. *
  246. * This test just makes sure that the value returned is of the correct
  247. * values and length
  248. *
  249. * @test
  250. * @dataProvider provider_random
  251. */
  252. public function test_random($type, $length)
  253. {
  254. if ($type === NULL)
  255. {
  256. $type = 'alnum';
  257. }
  258. $pool = (string) $type;
  259. switch ($pool)
  260. {
  261. case 'alnum':
  262. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  263. break;
  264. case 'alpha':
  265. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  266. break;
  267. case 'hexdec':
  268. $pool = '0123456789abcdef';
  269. break;
  270. case 'numeric':
  271. $pool = '0123456789';
  272. break;
  273. case 'nozero':
  274. $pool = '123456789';
  275. break;
  276. case 'distinct':
  277. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  278. break;
  279. }
  280. $this->assertRegExp('/^['.$pool.']{'.$length.'}$/u', Text::random($type, $length));
  281. }
  282. /**
  283. * Provides test data for test_similar
  284. *
  285. * @return array
  286. */
  287. public function provider_similar()
  288. {
  289. return array
  290. (
  291. // TODO: add some more cases
  292. array('foo', array('foobar', 'food', 'fooberry')),
  293. );
  294. }
  295. /**
  296. * Tests Text::similar()
  297. *
  298. * @test
  299. * @dataProvider provider_similar
  300. * @covers Text::similar
  301. */
  302. public function test_similar($expected, $words)
  303. {
  304. $this->assertSame($expected, Text::similar($words));
  305. }
  306. /**
  307. * Provides test data for test_bytes
  308. *
  309. * @return array
  310. */
  311. public function provider_bytes()
  312. {
  313. return array
  314. (
  315. // TODO: cover the other units
  316. array('256.00 B', 256, NULL, NULL, TRUE),
  317. array('1.02 kB', 1024, NULL, NULL, TRUE),
  318. // In case you need to know the size of a floppy disk in petabytes
  319. array('0.00147 GB', 1.44 * 1000 * 1024, 'GB', '%01.5f %s', TRUE),
  320. // SI is the standard, but lets deviate slightly
  321. array('1.00 MiB', 1024 * 1024, 'MiB', NULL, FALSE),
  322. );
  323. }
  324. /**
  325. * Tests Text::bytes()
  326. *
  327. * @test
  328. * @dataProvider provider_bytes
  329. */
  330. public function test_bytes($expected, $bytes, $force_unit, $format, $si)
  331. {
  332. $this->assertSame($expected, Text::bytes($bytes, $force_unit, $format, $si));
  333. }
  334. /**
  335. * Provides test data for test_widont()
  336. *
  337. * @return array Test data
  338. */
  339. public function provider_widont()
  340. {
  341. return array
  342. (
  343. array('No gain, no&nbsp;pain', 'No gain, no pain'),
  344. array("spaces?what'rethey?", "spaces?what'rethey?"),
  345. array('', ''),
  346. );
  347. }
  348. /**
  349. * Tests Text::widont()
  350. *
  351. * @test
  352. * @dataProvider provider_widont
  353. */
  354. public function test_widont($expected, $string)
  355. {
  356. $this->assertSame($expected, Text::widont($string));
  357. }
  358. /**
  359. * This checks that auto_link_emails() respects word boundaries and does not
  360. * just blindly replace all occurences of the email address in the text.
  361. *
  362. * In the sample below the algorithm was replacing all occurences of voorzitter@xxxx.com
  363. * inc the copy in the second list item.
  364. *
  365. * It was updated in 6c199366efc1115545ba13108b876acc66c54b2d to respect word boundaries
  366. *
  367. * @test
  368. * @covers Text::auto_link_emails
  369. * @ticket 2772
  370. */
  371. public function test_auto_link_emails_respects_word_boundaries()
  372. {
  373. $original = '<ul>
  374. <li>voorzitter@xxxx.com</li>
  375. <li>vicevoorzitter@xxxx.com</li>
  376. </ul>';
  377. $this->assertFalse(strpos('vice', Text::auto_link_emails($original)));
  378. }
  379. /**
  380. * Provides some test data for test_number()
  381. *
  382. * @return array
  383. */
  384. public function provider_number()
  385. {
  386. return array(
  387. array('one', 1),
  388. array('twenty-three', 23),
  389. array('fourty-two', 42),
  390. array('five million, six hundred and thirty-two', 5000632),
  391. array('five million, six hundred and thirty', 5000630),
  392. array('nine hundred million', 900000000),
  393. array('thirty-seven thousand', 37000),
  394. array('one thousand and twenty-four', 1024),
  395. );
  396. }
  397. /**
  398. * Checks that Text::number formats a number into english text
  399. *
  400. * @test
  401. * @dataProvider provider_number
  402. */
  403. public function test_number($expected, $number)
  404. {
  405. $this->assertSame($expected, Text::number($number));
  406. }
  407. /**
  408. * Provides test data for test_auto_link_urls()
  409. *
  410. * @return array
  411. */
  412. public function provider_auto_link_urls()
  413. {
  414. return array(
  415. // First we try with the really obvious url
  416. array(
  417. 'Some random text <a href="http://www.google.com">http://www.google.com</a>',
  418. 'Some random text http://www.google.com',
  419. ),
  420. // Then we try with varying urls
  421. array(
  422. 'Some random <a href="http://www.google.com">www.google.com</a>',
  423. 'Some random www.google.com',
  424. ),
  425. array(
  426. 'Some random google.com',
  427. 'Some random google.com',
  428. ),
  429. // Check that it doesn't link urls in a href
  430. array(
  431. 'Look at me <a href="http://google.com">Awesome stuff</a>',
  432. 'Look at me <a href="http://google.com">Awesome stuff</a>',
  433. ),
  434. array(
  435. 'Look at me <a href="http://www.google.com">http://www.google.com</a>',
  436. 'Look at me <a href="http://www.google.com">http://www.google.com</a>',
  437. ),
  438. // @issue 3190
  439. array(
  440. '<a href="http://www.google.com/">www.google.com</a>',
  441. '<a href="http://www.google.com/">www.google.com</a>',
  442. ),
  443. array(
  444. '<a href="http://www.google.com/">www.google.com</a> <a href="http://www.google.com/">http://www.google.com/</a>',
  445. '<a href="http://www.google.com/">www.google.com</a> http://www.google.com/',
  446. ),
  447. );
  448. }
  449. /**
  450. * Runs tests for Test::auto_link_urls
  451. *
  452. * @test
  453. * @dataProvider provider_auto_link_urls
  454. */
  455. public function test_auto_link_urls($expected, $text)
  456. {
  457. $this->assertSame($expected, Text::auto_link_urls($text));
  458. }
  459. /**
  460. * Provides test data for test_auto_link_emails()
  461. *
  462. * @return array
  463. */
  464. public function provider_auto_link_emails()
  465. {
  466. return array(
  467. // @issue 3162
  468. array(
  469. '<span class="broken"><a href="mailto:info@test.com">info@test.com</a></span>',
  470. '<span class="broken">info@test.com</span>',
  471. ),
  472. array(
  473. '<a href="mailto:info@test.com">info@test.com</a>',
  474. '<a href="mailto:info@test.com">info@test.com</a>',
  475. ),
  476. // @issue 3189
  477. array(
  478. '<a href="mailto:email@address.com">email@address.com</a> <a href="mailto:email@address.com">email@address.com</a>',
  479. '<a href="mailto:email@address.com">email@address.com</a> email@address.com',
  480. ),
  481. );
  482. }
  483. /**
  484. * Runs tests for Test::auto_link_emails
  485. *
  486. * @test
  487. * @dataProvider provider_auto_link_emails
  488. */
  489. public function test_auto_link_emails($expected, $text)
  490. {
  491. // Use html_entity_decode because emails will be randomly encoded by HTML::mailto
  492. $this->assertSame($expected, html_entity_decode(Text::auto_link_emails($text)));
  493. }
  494. /**
  495. * Provides test data for test_auto_link
  496. *
  497. * @return array Test data
  498. */
  499. public function provider_auto_link()
  500. {
  501. return array(
  502. array(
  503. 'Hi there, my site is kohanaframework.org and you can email me at nobody@kohanaframework.org',
  504. array('kohanaframework.org'),
  505. ),
  506. array(
  507. 'Hi my.domain.com@domain.com you came from',
  508. FALSE,
  509. array('my.domain.com@domain.com'),
  510. ),
  511. );
  512. }
  513. /**
  514. * Tests Text::auto_link()
  515. *
  516. * @test
  517. * @dataProvider provider_auto_link
  518. */
  519. public function test_auto_link($text, $urls = array(), $emails = array())
  520. {
  521. $linked_text = Text::auto_link($text);
  522. if($urls === FALSE)
  523. {
  524. $this->assertNotContains('http://', $linked_text);
  525. }
  526. elseif(count($urls))
  527. {
  528. foreach($urls as $url)
  529. {
  530. // Assert that all the urls have been caught by text auto_link_urls()
  531. $this->assertContains(Text::auto_link_urls($url), $linked_text);
  532. }
  533. }
  534. foreach($emails as $email)
  535. {
  536. $this->assertNotContains($email, $linked_text);
  537. }
  538. }
  539. }