TextTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. /**
  3. * Tests the kohana text class (Kohana_Text)
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.text
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. */
  12. class Kohana_TextTest extends Unittest_TestCase
  13. {
  14. /**
  15. * Sets up the test enviroment
  16. */
  17. // @codingStandardsIgnoreStart
  18. function setUp(): void
  19. // @codingStandardsIgnoreEnd
  20. {
  21. parent::setUp();
  22. Text::alternate();
  23. }
  24. /**
  25. * This test makes sure that auto_p returns an empty string if
  26. * an empty input was provided
  27. *
  28. * @test
  29. * @covers Text::auto_p
  30. */
  31. function test_auto_para_returns_empty_string_on_empty_input()
  32. {
  33. $this->assertSame('', Text::auto_p(''));
  34. }
  35. /**
  36. *
  37. * @return array Test Data
  38. */
  39. function provider_auto_para_does_not_enclose_html_tags_in_paragraphs()
  40. {
  41. return [
  42. [
  43. ['div'],
  44. '<div>Pick a plum of peppers</div>',
  45. ],
  46. [
  47. ['div'],
  48. '<div id="awesome">Tangas</div>',
  49. ],
  50. ];
  51. }
  52. /**
  53. * This test makes sure that auto_p doesn't enclose HTML tags
  54. * in paragraphs
  55. *
  56. * @test
  57. * @covers Text::auto_p
  58. * @dataProvider provider_auto_para_does_not_enclose_html_tags_in_paragraphs
  59. */
  60. function test_auto_para_does_not_enclose_html_tags_in_paragraphs(array $tags, $text)
  61. {
  62. $output = Text::auto_p($text);
  63. foreach ($tags as $tag)
  64. {
  65. $this->assertNotTag(
  66. ['tag' => $tag, 'ancestor' => ['tag' => 'p']],
  67. $output
  68. );
  69. }
  70. }
  71. /**
  72. * This test makes sure that auto_p surrounds a single line of text
  73. * with paragraph tags
  74. *
  75. * @test
  76. * @covers Text::auto_p
  77. */
  78. function test_auto_para_encloses_slot_in_paragraph()
  79. {
  80. $text = 'Pick a pinch of purple pepper';
  81. $this->assertSame('<p>'.$text.'</p>', Text::auto_p($text));
  82. }
  83. /**
  84. * Make sure that multiple new lines are replaced with paragraph tags
  85. *
  86. * @test
  87. * @covers Text::auto_p
  88. */
  89. public function test_auto_para_replaces_multiple_newlines_with_paragraph()
  90. {
  91. $this->assertSame(
  92. "<p>My name is john</p>\n\n<p>I'm a developer</p>",
  93. Text::auto_p("My name is john\n\n\n\nI'm a developer")
  94. );
  95. }
  96. /**
  97. * Data provider for test_limit_words
  98. *
  99. * @return array Array of test data
  100. */
  101. function provider_limit_words()
  102. {
  103. return [
  104. ['', '', 100, NULL],
  105. ['…', 'The rain in spain', -10, NULL],
  106. ['The rain…', 'The rain in spain', 2, NULL],
  107. ['The rain...', 'The rain in spain', 2, '...'],
  108. ];
  109. }
  110. /**
  111. *
  112. * @test
  113. * @dataProvider provider_limit_words
  114. */
  115. function test_limit_words($expected, $str, $limit, $end_char)
  116. {
  117. $this->assertSame($expected, Text::limit_words($str, $limit, $end_char));
  118. }
  119. /**
  120. * Provides test data for test_limit_chars()
  121. *
  122. * @return array Test data
  123. */
  124. function provider_limit_chars()
  125. {
  126. return [
  127. ['', '', 100, NULL, FALSE],
  128. ['…', 'BOO!', -42, NULL, FALSE],
  129. ['making php bet…', 'making php better for the sane', 14, NULL, FALSE],
  130. ['Garçon! Un café s.v.p.', 'Garçon! Un café s.v.p.', 50, '__', FALSE],
  131. ['Garçon!__', 'Garçon! Un café s.v.p.', 8, '__', FALSE],
  132. // @issue 3238
  133. ['making php…', 'making php better for the sane', 14, NULL, TRUE],
  134. ['Garçon!__', 'Garçon! Un café s.v.p.', 9, '__', TRUE],
  135. ['Garçon!__', 'Garçon! Un café s.v.p.', 7, '__', TRUE],
  136. ['__', 'Garçon! Un café s.v.p.', 5, '__', TRUE],
  137. ];
  138. }
  139. /**
  140. * Tests Text::limit_chars()
  141. *
  142. * @test
  143. * @dataProvider provider_limit_chars
  144. */
  145. function test_limit_chars($expected, $str, $limit, $end_char, $preserve_words)
  146. {
  147. $this->assertSame($expected, Text::limit_chars($str, $limit, $end_char, $preserve_words));
  148. }
  149. /**
  150. * Test Text::alternate()
  151. *
  152. * @test
  153. */
  154. function test_alternate_alternates_between_parameters()
  155. {
  156. list($val_a, $val_b, $val_c) = ['good', 'bad', 'ugly'];
  157. $this->assertSame('good', Text::alternate($val_a, $val_b, $val_c));
  158. $this->assertSame('bad', Text::alternate($val_a, $val_b, $val_c));
  159. $this->assertSame('ugly', Text::alternate($val_a, $val_b, $val_c));
  160. $this->assertSame('good', Text::alternate($val_a, $val_b, $val_c));
  161. }
  162. /**
  163. * Tests Text::alternate()
  164. *
  165. * @test
  166. * @covers Text::alternate
  167. */
  168. function test_alternate_resets_when_called_with_no_params_and_returns_empty_string()
  169. {
  170. list($val_a, $val_b, $val_c) = ['yes', 'no', 'maybe'];
  171. $this->assertSame('yes', Text::alternate($val_a, $val_b, $val_c));
  172. $this->assertSame('', Text::alternate());
  173. $this->assertSame('yes', Text::alternate($val_a, $val_b, $val_c));
  174. }
  175. /**
  176. * Provides test data for test_ucfirst
  177. *
  178. * @return array Test data
  179. */
  180. public function provider_ucfirst()
  181. {
  182. return [
  183. ['Content-Type', 'content-type', '-'],
  184. ['Բարեւ|Ձեզ', 'բարեւ|ձեզ', '|'],
  185. ];
  186. }
  187. /**
  188. * Covers Text::ucfirst()
  189. *
  190. * @test
  191. * @dataProvider provider_ucfirst
  192. */
  193. public function test_ucfirst($expected, $string, $delimiter)
  194. {
  195. $this->assertSame($expected, Text::ucfirst($string, $delimiter));
  196. }
  197. /**
  198. * Provides test data for test_reducde_slashes()
  199. *
  200. * @returns array Array of test data
  201. */
  202. function provider_reduce_slashes()
  203. {
  204. return [
  205. ['/', '//'],
  206. ['/google/php/kohana/', '//google/php//kohana//'],
  207. ];
  208. }
  209. /**
  210. * Covers Text::reduce_slashes()
  211. *
  212. * @test
  213. * @dataProvider provider_reduce_slashes
  214. */
  215. function test_reduce_slashes($expected, $str)
  216. {
  217. $this->assertSame($expected, Text::reduce_slashes($str));
  218. }
  219. /**
  220. * Provides test data for test_censor()
  221. *
  222. * @return array Test data
  223. */
  224. function provider_censor()
  225. {
  226. return [
  227. // If the replacement is 1 character long it should be repeated for the length of the removed word
  228. ["A donkey is also an ***", 'A donkey is also an ass', ['ass'], '*', TRUE],
  229. ["Cake### isn't nearly as good as kohana###", "CakePHP isn't nearly as good as kohanaphp", ['php'], '#', TRUE],
  230. // If it's > 1 then it's just replaced straight out
  231. ["If you're born out of wedlock you're a --expletive--", "If you're born out of wedlock you're a child", ['child'], '--expletive--', TRUE],
  232. ['class', 'class', ['ass'], '*', FALSE],
  233. ];
  234. }
  235. /**
  236. * Tests Text::censor
  237. *
  238. * @test
  239. * @dataProvider provider_censor
  240. */
  241. function test_censor($expected, $str, $badwords, $replacement, $replace_partial_words)
  242. {
  243. $this->assertSame($expected, Text::censor($str, $badwords, $replacement, $replace_partial_words));
  244. }
  245. /**
  246. * Provides test data for test_random
  247. *
  248. * @return array Test Data
  249. */
  250. function provider_random()
  251. {
  252. return [
  253. ['alnum', 8],
  254. ['alpha', 10],
  255. ['hexdec', 20],
  256. ['nozero', 5],
  257. ['numeric', 14],
  258. ['distinct', 12],
  259. ['aeiou', 4],
  260. ['‹¡›«¿»', 8], // UTF8 characters
  261. [NULL, 8], // Issue #3256
  262. ];
  263. }
  264. /**
  265. * Tests Text::random() as well as possible
  266. *
  267. * Obviously you can't compare a randomly generated string against a
  268. * pre-generated one and check that they are the same as this goes
  269. * against the whole ethos of random.
  270. *
  271. * This test just makes sure that the value returned is of the correct
  272. * values and length
  273. *
  274. * @test
  275. * @dataProvider provider_random
  276. */
  277. function test_random($type, $length)
  278. {
  279. if ($type === NULL)
  280. {
  281. $type = 'alnum';
  282. }
  283. $pool = (string) $type;
  284. switch ($pool)
  285. {
  286. case 'alnum':
  287. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  288. break;
  289. case 'alpha':
  290. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  291. break;
  292. case 'hexdec':
  293. $pool = '0123456789abcdef';
  294. break;
  295. case 'numeric':
  296. $pool = '0123456789';
  297. break;
  298. case 'nozero':
  299. $pool = '123456789';
  300. break;
  301. case 'distinct':
  302. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  303. break;
  304. }
  305. $this->assertRegExp('/^['.$pool.']{'.$length.'}$/u', Text::random($type, $length));
  306. }
  307. /**
  308. * Provides test data for test_similar
  309. *
  310. * @return array
  311. */
  312. function provider_similar()
  313. {
  314. return [
  315. // TODO: add some more cases
  316. ['foo', ['foobar', 'food', 'fooberry']],
  317. ];
  318. }
  319. /**
  320. * Tests Text::similar()
  321. *
  322. * @test
  323. * @dataProvider provider_similar
  324. * @covers Text::similar
  325. */
  326. function test_similar($expected, $words)
  327. {
  328. $this->assertSame($expected, Text::similar($words));
  329. }
  330. /**
  331. * Provides test data for test_bytes
  332. *
  333. * @return array
  334. */
  335. public function provider_bytes()
  336. {
  337. return [
  338. // TODO: cover the other units
  339. ['256.00 B', 256, NULL, NULL, TRUE],
  340. ['1.02 kB', 1024, NULL, NULL, TRUE],
  341. // In case you need to know the size of a floppy disk in petabytes
  342. ['0.00147 GB', 1.44 * 1000 * 1024, 'GB', '%01.5f %s', TRUE],
  343. // SI is the standard, but lets deviate slightly
  344. ['1.00 MiB', 1024 * 1024, 'MiB', NULL, FALSE],
  345. ];
  346. }
  347. /**
  348. * Tests Text::bytes()
  349. *
  350. * @test
  351. * @dataProvider provider_bytes
  352. */
  353. function test_bytes($expected, $bytes, $force_unit, $format, $si)
  354. {
  355. $this->assertSame($expected, Text::bytes($bytes, $force_unit, $format, $si));
  356. }
  357. /**
  358. * Provides test data for test_widont()
  359. *
  360. * @return array Test data
  361. */
  362. function provider_widont()
  363. {
  364. return [
  365. // A very simple widont test
  366. [
  367. 'A very simple&nbsp;test',
  368. 'A very simple test',
  369. ],
  370. // Single word items shouldn't be changed
  371. [
  372. 'Test',
  373. 'Test',
  374. ],
  375. // Single word after single space shouldn't be changed either
  376. [
  377. ' Test',
  378. ' Test',
  379. ],
  380. // Single word with HTML all around
  381. [
  382. '<ul><li><p>Test</p></li><ul>',
  383. '<ul><li><p>Test</p></li><ul>',
  384. ],
  385. // Single word after single space with HTML all around
  386. [
  387. '<ul><li><p> Test</p></li><ul>',
  388. '<ul><li><p> Test</p></li><ul>',
  389. ],
  390. // Widont with more than one paragraph
  391. [
  392. '<p>In a couple of&nbsp;paragraphs</p><p>paragraph&nbsp;two</p>',
  393. '<p>In a couple of paragraphs</p><p>paragraph two</p>',
  394. ],
  395. // a link inside a heading
  396. [
  397. '<h1><a href="#">In a link inside a&nbsp;heading </a></h1>',
  398. '<h1><a href="#">In a link inside a heading </a></h1>',
  399. ],
  400. // a link followed by text
  401. [
  402. '<h1><a href="#">In a link</a> followed by other&nbsp;text</h1>',
  403. '<h1><a href="#">In a link</a> followed by other text</h1>',
  404. ],
  405. // empty html, with no text inside
  406. [
  407. '<h1><a href="#"></a></h1>',
  408. '<h1><a href="#"></a></h1>',
  409. ],
  410. // apparently, we don't love DIVs
  411. [
  412. '<div>Divs get no love!</div>',
  413. '<div>Divs get no love!</div>',
  414. ],
  415. // we don't love PREs, either
  416. [
  417. '<pre>Neither do PREs</pre>',
  418. '<pre>Neither do PREs</pre>',
  419. ],
  420. // but we love DIVs with paragraphs
  421. [
  422. '<div><p>But divs with paragraphs&nbsp;do!</p></div>',
  423. '<div><p>But divs with paragraphs do!</p></div>',
  424. ],
  425. [
  426. 'No gain, no&nbsp;pain',
  427. 'No gain, no pain',
  428. ],
  429. [
  430. "spaces?what'rethey?",
  431. "spaces?what'rethey?",
  432. ],
  433. /*
  434. * // @issue 3499, with HTML at the end
  435. * array(
  436. * 'with HTML at the end &nbsp;<strong>Kohana</strong>',
  437. * 'with HTML at the end <strong>Kohana</strong>',
  438. * ),
  439. * // @issue 3499, with HTML with attributes at the end
  440. * array(
  441. * 'with HTML at the end:&nbsp;<a href="#" title="Kohana">Kohana</a>',
  442. * 'with HTML at the end: <a href="#" title="Kohana">Kohana</a>',
  443. * ),
  444. */
  445. [
  446. '',
  447. '',
  448. ],
  449. ];
  450. }
  451. /**
  452. * Tests Text::widont()
  453. *
  454. * @test
  455. * @dataProvider provider_widont
  456. */
  457. function test_widont($expected, $string)
  458. {
  459. $this->assertSame($expected, Text::widont($string));
  460. }
  461. /**
  462. * This checks that auto_link_emails() respects word boundaries and does not
  463. * just blindly replace all occurences of the email address in the text.
  464. *
  465. * In the sample below the algorithm was replacing all occurences of voorzitter@xxxx.com
  466. * inc the copy in the second list item.
  467. *
  468. * It was updated in 6c199366efc1115545ba13108b876acc66c54b2d to respect word boundaries
  469. *
  470. * @test
  471. * @covers Text::auto_link_emails
  472. * @ticket 2772
  473. */
  474. function test_auto_link_emails_respects_word_boundaries()
  475. {
  476. $original = '<ul>
  477. <li>voorzitter@xxxx.com</li>
  478. <li>vicevoorzitter@xxxx.com</li>
  479. </ul>';
  480. $this->assertFalse(strpos('vice', Text::auto_link_emails($original)));
  481. }
  482. /**
  483. * Provides some test data for test_number()
  484. *
  485. * @return array
  486. */
  487. public function provider_number()
  488. {
  489. return [
  490. ['one', 1],
  491. ['twenty-three', 23],
  492. ['forty-two', 42],
  493. ['five million, six hundred and thirty-two', 5000632],
  494. ['five million, six hundred and thirty', 5000630],
  495. ['nine hundred million', 900000000],
  496. ['thirty-seven thousand', 37000],
  497. ['one thousand and twenty-four', 1024],
  498. ];
  499. }
  500. /**
  501. * Checks that Text::number formats a number into english text
  502. *
  503. * @test
  504. * @dataProvider provider_number
  505. */
  506. public function test_number($expected, $number)
  507. {
  508. $this->assertSame($expected, Text::number($number));
  509. }
  510. /**
  511. * Provides test data for test_auto_link_urls()
  512. *
  513. * @return array
  514. */
  515. public function provider_auto_link_urls()
  516. {
  517. return [
  518. // First we try with the really obvious url
  519. [
  520. 'Some random text <a href="http://www.google.com">http://www.google.com</a>',
  521. 'Some random text http://www.google.com',
  522. ],
  523. // Then we try with varying urls
  524. [
  525. 'Some random <a href="http://www.google.com">www.google.com</a>',
  526. 'Some random www.google.com',
  527. ],
  528. [
  529. 'Some random google.com',
  530. 'Some random google.com',
  531. ],
  532. // Check that it doesn't link urls in a href
  533. [
  534. 'Look at me <a href="http://google.com">Awesome stuff</a>',
  535. 'Look at me <a href="http://google.com">Awesome stuff</a>',
  536. ],
  537. [
  538. 'Look at me <a href="http://www.google.com">http://www.google.com</a>',
  539. 'Look at me <a href="http://www.google.com">http://www.google.com</a>',
  540. ],
  541. // Punctuation at the end of the URL
  542. [
  543. 'Wow <a href="http://www.google.com">http://www.google.com</a>!',
  544. 'Wow http://www.google.com!',
  545. ],
  546. [
  547. 'Zomg <a href="http://www.google.com">www.google.com</a>!',
  548. 'Zomg www.google.com!',
  549. ],
  550. [
  551. 'Well this, <a href="http://www.google.com">www.google.com</a>, is cool',
  552. 'Well this, www.google.com, is cool',
  553. ],
  554. // @issue 3190
  555. [
  556. '<a href="http://www.google.com/">www.google.com</a>',
  557. '<a href="http://www.google.com/">www.google.com</a>',
  558. ],
  559. [
  560. '<a href="http://www.google.com/">www.google.com</a> <a href="http://www.google.com/">http://www.google.com/</a>',
  561. '<a href="http://www.google.com/">www.google.com</a> http://www.google.com/',
  562. ],
  563. // @issue 3436
  564. [
  565. '<strong><a href="http://www.google.com/">http://www.google.com/</a></strong>',
  566. '<strong>http://www.google.com/</strong>',
  567. ],
  568. // @issue 4208, URLs with a path
  569. [
  570. 'Foobar <a href="http://www.google.com/analytics">www.google.com/analytics</a> cake',
  571. 'Foobar www.google.com/analytics cake',
  572. ],
  573. [
  574. 'Look at this <a href="http://www.google.com/analytics">www.google.com/analytics</a>!',
  575. 'Look at this www.google.com/analytics!',
  576. ],
  577. [
  578. 'Path <a href="http://www.google.com/analytics">http://www.google.com/analytics</a> works?',
  579. 'Path http://www.google.com/analytics works?',
  580. ],
  581. [
  582. 'Path <a href="http://www.google.com/analytics">http://www.google.com/analytics</a>',
  583. 'Path http://www.google.com/analytics',
  584. ],
  585. [
  586. 'Path <a href="http://www.google.com/analytics">www.google.com/analytics</a>',
  587. 'Path www.google.com/analytics',
  588. ],
  589. ];
  590. }
  591. /**
  592. * Runs tests for Test::auto_link_urls
  593. *
  594. * @test
  595. * @dataProvider provider_auto_link_urls
  596. */
  597. public function test_auto_link_urls($expected, $text)
  598. {
  599. $this->assertSame($expected, Text::auto_link_urls($text));
  600. }
  601. /**
  602. * Provides test data for test_auto_link_emails()
  603. *
  604. * @return array
  605. */
  606. public function provider_auto_link_emails()
  607. {
  608. return [
  609. // @issue 3162
  610. [
  611. '<span class="broken"><a href="mailto:info@test.com">info@test.com</a></span>',
  612. '<span class="broken">info@test.com</span>',
  613. ],
  614. [
  615. '<a href="mailto:info@test.com">info@test.com</a>',
  616. '<a href="mailto:info@test.com">info@test.com</a>',
  617. ],
  618. // @issue 3189
  619. [
  620. '<a href="mailto:email@address.com">email@address.com</a> <a href="mailto:email@address.com">email@address.com</a>',
  621. '<a href="mailto:email@address.com">email@address.com</a> email@address.com',
  622. ],
  623. ];
  624. }
  625. /**
  626. * Runs tests for Test::auto_link_emails
  627. *
  628. * @test
  629. * @dataProvider provider_auto_link_emails
  630. */
  631. public function test_auto_link_emails($expected, $text)
  632. {
  633. // Use html_entity_decode because emails will be randomly encoded by HTML::mailto
  634. $this->assertSame($expected, html_entity_decode(Text::auto_link_emails($text)));
  635. }
  636. /**
  637. * Provides test data for test_auto_link
  638. *
  639. * @return array Test data
  640. */
  641. public function provider_auto_link()
  642. {
  643. return [
  644. [
  645. 'Hi there, my site is kohanaframework.org and you can email me at nobody@kohanaframework.org',
  646. ['kohanaframework.org'],
  647. ],
  648. [
  649. 'Hi my.domain.com@domain.com you came from',
  650. FALSE,
  651. ['my.domain.com@domain.com'],
  652. ],
  653. ];
  654. }
  655. /**
  656. * Tests Text::auto_link()
  657. *
  658. * @test
  659. * @dataProvider provider_auto_link
  660. */
  661. public function test_auto_link($text, $urls = [], $emails = [])
  662. {
  663. $linked_text = Text::auto_link($text);
  664. if ($urls === FALSE)
  665. {
  666. $this->assertNotContains('http://', $linked_text);
  667. }
  668. elseif (count($urls))
  669. {
  670. foreach ($urls as $url)
  671. {
  672. // Assert that all the urls have been caught by text auto_link_urls()
  673. $this->assertContains(Text::auto_link_urls($url), $linked_text);
  674. }
  675. }
  676. foreach ($emails as $email)
  677. {
  678. $this->assertContains('&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email, $linked_text);
  679. }
  680. }
  681. public function provider_user_agents()
  682. {
  683. return [
  684. [
  685. "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36",
  686. [
  687. 'browser' => 'Chrome',
  688. 'version' => '37.0.2049.0',
  689. 'platform' => "Windows 8.1"
  690. ]
  691. ],
  692. [
  693. "Mozilla/5.0 (Macintosh; U; Mac OS X 10_6_1; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/ Safari/530.5",
  694. [
  695. 'browser' => 'Chrome',
  696. 'version' => '530.5',
  697. 'platform' => "Mac OS X"
  698. ]
  699. ],
  700. [
  701. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
  702. [
  703. 'browser' => 'Safari',
  704. 'version' => '534.57.2',
  705. 'platform' => 'Mac OS X'
  706. ]
  707. ],
  708. [
  709. "Lynx/2.8.8dev.3 libwww-FM/2.14 SSL-MM/1.4.1",
  710. [
  711. 'browser' => 'Lynx',
  712. 'version' => '2.8.8dev.3',
  713. 'platform' => false
  714. ]
  715. ]
  716. ];
  717. }
  718. /**
  719. * Tests Text::user_agent
  720. *
  721. * @dataProvider provider_user_agents
  722. * @group current
  723. */
  724. public function test_user_agent_returns_correct_browser($userAgent, $expectedData)
  725. {
  726. $browser = Text::user_agent($userAgent, 'browser');
  727. $this->assertEquals($expectedData['browser'], $browser);
  728. }
  729. /**
  730. * Tests Text::user_agent
  731. *
  732. * @dataProvider provider_user_agents
  733. * @test
  734. */
  735. public function test_user_agent_returns_correct_version($userAgent, $expectedData)
  736. {
  737. $version = Text::user_agent($userAgent, 'version');
  738. $this->assertEquals($expectedData['version'], $version);
  739. }
  740. /**
  741. * Tests Text::user_agent
  742. * @test
  743. */
  744. public function test_user_agent_recognizes_robots()
  745. {
  746. $bot = Text::user_agent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'robot');
  747. $this->assertEquals('Googlebot', $bot);
  748. }
  749. /**
  750. * Tests Text::user_agent
  751. *
  752. * @dataProvider provider_user_agents
  753. * @test
  754. */
  755. public function test_user_agent_returns_correct_platform($userAgent, $expectedData)
  756. {
  757. $platform = Text::user_agent($userAgent, 'platform');
  758. $this->assertEquals($expectedData['platform'], $platform);
  759. }
  760. /**
  761. * Tests Text::user_agent
  762. * @test
  763. */
  764. public function test_user_agent_accepts_array()
  765. {
  766. $agent_info = Text::user_agent(
  767. 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 '.
  768. '(KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36',
  769. ['browser', 'version', 'platform']);
  770. $this->assertArrayHasKey('browser', $agent_info);
  771. $this->assertArrayHasKey('version', $agent_info);
  772. $this->assertArrayHasKey('platform', $agent_info);
  773. }
  774. }