TextTest.php 19 KB

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