I18nTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. use PrivateBin\I18n;
  3. class I18nTest extends PHPUnit_Framework_TestCase
  4. {
  5. private $_translations = array();
  6. public function setUp()
  7. {
  8. /* Setup Routine */
  9. $this->_translations = json_decode(
  10. file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . 'de.json'),
  11. true
  12. );
  13. }
  14. public function tearDown()
  15. {
  16. /* Tear Down Routine */
  17. }
  18. public function testTranslationFallback()
  19. {
  20. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  21. $messageId = 'It does not matter if the message ID exists';
  22. I18n::loadTranslations();
  23. $this->assertEquals($messageId, I18n::_($messageId), 'fallback to en');
  24. }
  25. public function testCookieLanguageDeDetection()
  26. {
  27. $_COOKIE['lang'] = 'de';
  28. I18n::loadTranslations();
  29. $this->assertEquals($this->_translations['en'], I18n::_('en'), 'browser language de');
  30. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in german');
  31. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in german');
  32. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in french');
  33. }
  34. public function testBrowserLanguageDeDetection()
  35. {
  36. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-CH,de;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  37. I18n::loadTranslations();
  38. $this->assertEquals($this->_translations['en'], I18n::_('en'), 'browser language de');
  39. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in german');
  40. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in german');
  41. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in french');
  42. }
  43. public function testBrowserLanguageFrDetection()
  44. {
  45. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-CH,fr;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  46. I18n::loadTranslations();
  47. $this->assertEquals('fr', I18n::_('en'), 'browser language fr');
  48. $this->assertEquals('0 heure', I18n::_('%d hours', 0), '0 hours in french');
  49. $this->assertEquals('1 heure', I18n::_('%d hours', 1), '1 hour in french');
  50. $this->assertEquals('2 heures', I18n::_('%d hours', 2), '2 hours in french');
  51. }
  52. public function testBrowserLanguagePlDetection()
  53. {
  54. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  55. I18n::loadTranslations();
  56. $this->assertEquals('pl', I18n::_('en'), 'browser language pl');
  57. $this->assertEquals('2 godzina', I18n::_('%d hours', 2), 'hours in polish');
  58. }
  59. public function testBrowserLanguageAnyDetection()
  60. {
  61. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  62. I18n::loadTranslations();
  63. $this->assertTrue(strlen(I18n::_('en')) == 2, 'browser language any');
  64. }
  65. public function testVariableInjection()
  66. {
  67. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  68. I18n::loadTranslations();
  69. $this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
  70. }
  71. }