I18nTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\I18n;
  4. class I18nMock extends I18n
  5. {
  6. public static function resetAvailableLanguages()
  7. {
  8. self::$_availableLanguages = array();
  9. }
  10. public static function resetPath($path = '')
  11. {
  12. self::$_path = $path;
  13. }
  14. public static function getPath($file = '')
  15. {
  16. return self::_getPath($file);
  17. }
  18. }
  19. class I18nTest extends TestCase
  20. {
  21. private $_translations = array();
  22. public function setUp(): void
  23. {
  24. /* Setup Routine */
  25. $this->_translations = json_decode(
  26. file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . 'de.json'),
  27. true
  28. );
  29. }
  30. public function tearDown(): void
  31. {
  32. unset($_COOKIE['lang'], $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  33. }
  34. public function testTranslationFallback()
  35. {
  36. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  37. $messageId = 'It does not matter if the message ID exists';
  38. I18n::loadTranslations();
  39. $this->assertEquals($messageId, I18n::_($messageId), 'fallback to en');
  40. I18n::getLanguageLabels();
  41. }
  42. public function testCookieLanguageDeDetection()
  43. {
  44. $_COOKIE['lang'] = 'de';
  45. I18n::loadTranslations();
  46. $this->assertEquals($_COOKIE['lang'], I18n::getLanguage(), 'browser language de');
  47. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
  48. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
  49. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
  50. }
  51. public function testBrowserLanguageDeDetection()
  52. {
  53. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-CH,de;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2,fr;q=0.0';
  54. I18n::loadTranslations();
  55. $this->assertEquals('de', I18n::getLanguage(), 'browser language de');
  56. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
  57. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
  58. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
  59. }
  60. public function testBrowserLanguageFrDetection()
  61. {
  62. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-CH,fr;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2,de;q=0.0';
  63. I18n::loadTranslations();
  64. $this->assertEquals('fr', I18n::getLanguage(), 'browser language fr');
  65. $this->assertEquals('0 heure', I18n::_('%d hours', 0), '0 hours in French');
  66. $this->assertEquals('1 heure', I18n::_('%d hours', 1), '1 hour in French');
  67. $this->assertEquals('2 heures', I18n::_('%d hours', 2), '2 hours in French');
  68. }
  69. public function testBrowserLanguageNoDetection()
  70. {
  71. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'no;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  72. I18n::loadTranslations();
  73. $this->assertEquals('no', I18n::getLanguage(), 'browser language no');
  74. $this->assertEquals('0 timer', I18n::_('%d hours', 0), '0 hours in Norwegian');
  75. $this->assertEquals('1 time', I18n::_('%d hours', 1), '1 hour in Norwegian');
  76. $this->assertEquals('2 timer', I18n::_('%d hours', 2), '2 hours in Norwegian');
  77. }
  78. public function testBrowserLanguageOcDetection()
  79. {
  80. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'oc;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  81. I18n::loadTranslations();
  82. $this->assertEquals('oc', I18n::getLanguage(), 'browser language oc');
  83. $this->assertEquals('0 ora', I18n::_('%d hours', 0), '0 hours in Occitan');
  84. $this->assertEquals('1 ora', I18n::_('%d hours', 1), '1 hour in Occitan');
  85. $this->assertEquals('2 oras', I18n::_('%d hours', 2), '2 hours in Occitan');
  86. }
  87. public function testBrowserLanguageZhDetection()
  88. {
  89. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'zh;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  90. I18n::loadTranslations();
  91. $this->assertEquals('zh', I18n::getLanguage(), 'browser language zh');
  92. $this->assertEquals('0 小时', I18n::_('%d hours', 0), '0 hours in Chinese');
  93. $this->assertEquals('1 小时', I18n::_('%d hours', 1), '1 hour in Chinese');
  94. $this->assertEquals('2 小时', I18n::_('%d hours', 2), '2 hours in Chinese');
  95. }
  96. public function testBrowserLanguagePlDetection()
  97. {
  98. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  99. I18n::loadTranslations();
  100. $this->assertEquals('pl', I18n::getLanguage(), 'browser language pl');
  101. $this->assertEquals('1 godzina', I18n::_('%d hours', 1), '1 hour in Polish');
  102. $this->assertEquals('2 godzina', I18n::_('%d hours', 2), '2 hours in Polish');
  103. $this->assertEquals('12 godzinę', I18n::_('%d hours', 12), '12 hours in Polish');
  104. $this->assertEquals('22 godzina', I18n::_('%d hours', 22), '22 hours in Polish');
  105. $this->assertEquals('1 minut', I18n::_('%d minutes', 1), '1 minute in Polish');
  106. $this->assertEquals('3 minut', I18n::_('%d minutes', 3), '3 minutes in Polish');
  107. $this->assertEquals('13 minut', I18n::_('%d minutes', 13), '13 minutes in Polish');
  108. $this->assertEquals('23 minut', I18n::_('%d minutes', 23), '23 minutes in Polish');
  109. }
  110. public function testBrowserLanguageRuDetection()
  111. {
  112. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  113. I18n::loadTranslations();
  114. $this->assertEquals('ru', I18n::getLanguage(), 'browser language ru');
  115. $this->assertEquals('1 минуту', I18n::_('%d minutes', 1), '1 minute in Russian');
  116. $this->assertEquals('3 минуты', I18n::_('%d minutes', 3), '3 minutes in Russian');
  117. $this->assertEquals('10 минут', I18n::_('%d minutes', 10), '10 minutes in Russian');
  118. $this->assertEquals('21 минуту', I18n::_('%d minutes', 21), '21 minutes in Russian');
  119. }
  120. public function testBrowserLanguageSlDetection()
  121. {
  122. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  123. I18n::loadTranslations();
  124. $this->assertEquals('sl', I18n::getLanguage(), 'browser language sl');
  125. $this->assertEquals('0 ura', I18n::_('%d hours', 0), '0 hours in Slowene');
  126. $this->assertEquals('1 uri', I18n::_('%d hours', 1), '1 hour in Slowene');
  127. $this->assertEquals('2 ure', I18n::_('%d hours', 2), '2 hours in Slowene');
  128. $this->assertEquals('3 ur', I18n::_('%d hours', 3), '3 hours in Slowene');
  129. $this->assertEquals('11 ura', I18n::_('%d hours', 11), '11 hours in Slowene');
  130. $this->assertEquals('101 uri', I18n::_('%d hours', 101), '101 hours in Slowene');
  131. $this->assertEquals('102 ure', I18n::_('%d hours', 102), '102 hours in Slowene');
  132. $this->assertEquals('104 ur', I18n::_('%d hours', 104), '104 hours in Slowene');
  133. }
  134. public function testBrowserLanguageCsDetection()
  135. {
  136. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'cs;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  137. I18n::loadTranslations();
  138. $this->assertEquals('cs', I18n::getLanguage(), 'browser language cs');
  139. $this->assertEquals('1 hodina', I18n::_('%d hours', 1), '1 hour in Czech');
  140. $this->assertEquals('2 hodiny', I18n::_('%d hours', 2), '2 hours in Czech');
  141. $this->assertEquals('5 minut', I18n::_('%d minutes', 5), '5 minutes in Czech');
  142. $this->assertEquals('14 minut', I18n::_('%d minutes', 14), '14 minutes in Czech');
  143. }
  144. public function testBrowserLanguageAnyDetection()
  145. {
  146. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  147. I18n::loadTranslations();
  148. $this->assertTrue(strlen(I18n::getLanguage()) >= 2, 'browser language any');
  149. }
  150. public function testVariableInjection()
  151. {
  152. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  153. I18n::loadTranslations();
  154. $this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
  155. }
  156. public function testHtmlEntityEncoding()
  157. {
  158. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  159. I18n::loadTranslations();
  160. $input = '&<>"\'/`=';
  161. $result = htmlspecialchars($input, ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED, 'UTF-8', false);
  162. $this->assertEquals($result, I18n::encode($input), 'encodes HTML entities');
  163. $this->assertEquals('<a>some ' . $result . ' + 1</a>', I18n::_('<a>some %s + %d</a>', $input, 1), 'encodes parameters in translations');
  164. $this->assertEquals($result . $result, I18n::_($input . '%s', $input), 'encodes message ID as well, when no link');
  165. }
  166. public function testFallbackAlwaysPresent()
  167. {
  168. $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_i18n';
  169. if (!is_dir($path)) {
  170. mkdir($path);
  171. }
  172. $languageIterator = new AppendIterator();
  173. $languageIterator->append(new GlobIterator(I18nMock::getPath('??.json')));
  174. $languageIterator->append(new GlobIterator(I18nMock::getPath('???.json'))); // for jbo
  175. $languageCount = 0;
  176. foreach ($languageIterator as $file) {
  177. ++$languageCount;
  178. $this->assertTrue(copy($file, $path . DIRECTORY_SEPARATOR . $file->getBasename()));
  179. }
  180. I18nMock::resetPath($path);
  181. $languagesDevelopment = I18nMock::getAvailableLanguages();
  182. $this->assertEquals($languageCount, count($languagesDevelopment), 'all copied languages detected');
  183. $this->assertTrue(in_array('en', $languagesDevelopment), 'English fallback present');
  184. unlink($path . DIRECTORY_SEPARATOR . 'en.json');
  185. I18nMock::resetAvailableLanguages();
  186. $languagesDeployed = I18nMock::getAvailableLanguages();
  187. $this->assertEquals($languageCount, count($languagesDeployed), 'all copied languages detected, plus fallback');
  188. $this->assertTrue(in_array('en', $languagesDeployed), 'English fallback still present');
  189. I18nMock::resetAvailableLanguages();
  190. I18nMock::resetPath();
  191. Helper::rmDir($path);
  192. }
  193. public function testMessageIdsExistInAllLanguages()
  194. {
  195. $messageIds = array();
  196. $languages = array();
  197. $dir = dir(PATH . 'i18n');
  198. while (false !== ($file = $dir->read())) {
  199. if (strlen($file) === 7) {
  200. $language = substr($file, 0, 2);
  201. $languageMessageIds = array_keys(
  202. json_decode(
  203. file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . $file),
  204. true
  205. )
  206. );
  207. $messageIds = array_unique(array_merge($messageIds, $languageMessageIds));
  208. $languages[$language] = $languageMessageIds;
  209. }
  210. }
  211. foreach ($messageIds as $messageId) {
  212. foreach (array_keys($languages) as $language) {
  213. // most languages don't translate the data size units, ignore those
  214. if ($messageId !== 'B' && strlen($messageId) !== 3 && strpos($messageId, 'B', 2) !== 2) {
  215. $this->assertContains(
  216. $messageId,
  217. $languages[$language],
  218. "message ID '$messageId' exists in translation file $language.json"
  219. );
  220. }
  221. }
  222. }
  223. }
  224. }