I18nTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\I18n;
  4. use PrivateBin\Json;
  5. class I18nMock extends I18n
  6. {
  7. public static function resetAvailableLanguages()
  8. {
  9. self::$_availableLanguages = [];
  10. }
  11. public static function resetPath($path = '')
  12. {
  13. self::$_path = $path;
  14. }
  15. public static function getPath($file = '')
  16. {
  17. return self::_getPath($file);
  18. }
  19. }
  20. class I18nTest extends TestCase
  21. {
  22. private $_translations = [];
  23. public function setUp(): void
  24. {
  25. /* Setup Routine */
  26. $this->_translations = json_decode(
  27. file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . 'de.json'),
  28. true
  29. );
  30. }
  31. public function tearDown(): void
  32. {
  33. unset($_COOKIE['lang'], $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  34. I18n::loadTranslations();
  35. }
  36. public function testTranslationFallback()
  37. {
  38. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  39. $messageId = 'It does not matter if the message ID exists';
  40. I18n::loadTranslations();
  41. $this->assertEquals($messageId, I18n::_($messageId), 'fallback to en');
  42. I18n::getLanguageLabels();
  43. }
  44. public function testCookieLanguageDeDetection()
  45. {
  46. $_COOKIE['lang'] = 'de';
  47. I18n::loadTranslations();
  48. $this->assertEquals($_COOKIE['lang'], I18n::getLanguage(), 'browser language de');
  49. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
  50. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
  51. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
  52. }
  53. public function testBrowserLanguageDeDetection()
  54. {
  55. $_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';
  56. I18n::loadTranslations();
  57. $this->assertEquals('de', I18n::getLanguage(), 'browser language de');
  58. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
  59. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
  60. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
  61. }
  62. public function testBrowserLanguageFrDetection()
  63. {
  64. $_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';
  65. I18n::loadTranslations();
  66. $this->assertEquals('fr', I18n::getLanguage(), 'browser language fr');
  67. $this->assertEquals('0 heure', I18n::_('%d hours', 0), '0 hours in French');
  68. $this->assertEquals('1 heure', I18n::_('%d hours', 1), '1 hour in French');
  69. $this->assertEquals('2 heures', I18n::_('%d hours', 2), '2 hours in French');
  70. }
  71. public function testEqualQualityPreservesBrowserOrder()
  72. {
  73. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr;q=0.8,de;q=0.8';
  74. I18n::loadTranslations();
  75. $this->assertEquals('fr', I18n::getLanguage());
  76. }
  77. public function testBrowserLanguageNoDetection()
  78. {
  79. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'no;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  80. I18n::loadTranslations();
  81. $this->assertEquals('no', I18n::getLanguage(), 'browser language no');
  82. $this->assertEquals('0 timer', I18n::_('%d hours', 0), '0 hours in Norwegian');
  83. $this->assertEquals('1 time', I18n::_('%d hours', 1), '1 hour in Norwegian');
  84. $this->assertEquals('2 timer', I18n::_('%d hours', 2), '2 hours in Norwegian');
  85. }
  86. public function testBrowserLanguageOcDetection()
  87. {
  88. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'oc;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  89. I18n::loadTranslations();
  90. $this->assertEquals('oc', I18n::getLanguage(), 'browser language oc');
  91. $this->assertEquals('0 ora', I18n::_('%d hours', 0), '0 hours in Occitan');
  92. $this->assertEquals('1 ora', I18n::_('%d hours', 1), '1 hour in Occitan');
  93. $this->assertEquals('2 oras', I18n::_('%d hours', 2), '2 hours in Occitan');
  94. }
  95. public function testBrowserLanguageZhDetection()
  96. {
  97. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'zh;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  98. I18n::loadTranslations();
  99. $this->assertEquals('zh', I18n::getLanguage(), 'browser language zh');
  100. $this->assertEquals('0 小时', I18n::_('%d hours', 0), '0 hours in Chinese');
  101. $this->assertEquals('1 小时', I18n::_('%d hours', 1), '1 hour in Chinese');
  102. $this->assertEquals('2 小时', I18n::_('%d hours', 2), '2 hours in Chinese');
  103. }
  104. public function testBrowserLanguagePlDetection()
  105. {
  106. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  107. I18n::loadTranslations();
  108. $this->assertEquals('pl', I18n::getLanguage(), 'browser language pl');
  109. $this->assertEquals('1 godzina', I18n::_('%d hours', 1), '1 hour in Polish');
  110. $this->assertEquals('2 godziny', I18n::_('%d hours', 2), '2 hours in Polish');
  111. $this->assertEquals('12 godzin', I18n::_('%d hours', 12), '12 hours in Polish');
  112. $this->assertEquals('22 godziny', I18n::_('%d hours', 22), '22 hours in Polish');
  113. $this->assertEquals('1 minuta', I18n::_('%d minutes', 1), '1 minute in Polish');
  114. $this->assertEquals('3 minuty', I18n::_('%d minutes', 3), '3 minutes in Polish');
  115. $this->assertEquals('13 minut', I18n::_('%d minutes', 13), '13 minutes in Polish');
  116. $this->assertEquals('23 minuty', I18n::_('%d minutes', 23), '23 minutes in Polish');
  117. }
  118. public function testBrowserLanguageRuDetection()
  119. {
  120. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  121. I18n::loadTranslations();
  122. $this->assertEquals('ru', I18n::getLanguage(), 'browser language ru');
  123. $this->assertEquals('1 минуту', I18n::_('%d minutes', 1), '1 minute in Russian');
  124. $this->assertEquals('3 минуты', I18n::_('%d minutes', 3), '3 minutes in Russian');
  125. $this->assertEquals('10 минут', I18n::_('%d minutes', 10), '10 minutes in Russian');
  126. $this->assertEquals('21 минуту', I18n::_('%d minutes', 21), '21 minutes in Russian');
  127. }
  128. public function testBrowserLanguageSlDetection()
  129. {
  130. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  131. I18n::loadTranslations();
  132. $this->assertEquals('sl', I18n::getLanguage(), 'browser language sl');
  133. $this->assertEquals('0 ura', I18n::_('%d hours', 0), '0 hours in Slovene');
  134. $this->assertEquals('1 uri', I18n::_('%d hours', 1), '1 hour in Slovene');
  135. $this->assertEquals('2 ure', I18n::_('%d hours', 2), '2 hours in Slovene');
  136. $this->assertEquals('3 ur', I18n::_('%d hours', 3), '3 hours in Slovene');
  137. $this->assertEquals('11 ura', I18n::_('%d hours', 11), '11 hours in Slovene');
  138. $this->assertEquals('101 uri', I18n::_('%d hours', 101), '101 hours in Slovene');
  139. $this->assertEquals('102 ure', I18n::_('%d hours', 102), '102 hours in Slovene');
  140. $this->assertEquals('104 ur', I18n::_('%d hours', 104), '104 hours in Slovene');
  141. }
  142. public function testBrowserLanguageCsDetection()
  143. {
  144. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'cs;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  145. I18n::loadTranslations();
  146. $this->assertEquals('cs', I18n::getLanguage(), 'browser language cs');
  147. $this->assertEquals('1 hodina', I18n::_('%d hours', 1), '1 hour in Czech');
  148. $this->assertEquals('2 hodiny', I18n::_('%d hours', 2), '2 hours in Czech');
  149. $this->assertEquals('5 minut', I18n::_('%d minutes', 5), '5 minutes in Czech');
  150. $this->assertEquals('14 minut', I18n::_('%d minutes', 14), '14 minutes in Czech');
  151. }
  152. public function testInvalidBrowserLanguageQualityIsIgnored()
  153. {
  154. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de;q=2,en;q=0.8';
  155. $this->assertSame(['0.8' => ['en']], I18n::getBrowserLanguages());
  156. }
  157. public function testBrowserLanguageAnyDetection()
  158. {
  159. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  160. I18n::loadTranslations();
  161. $this->assertTrue(strlen(I18n::getLanguage()) >= 2, 'browser language any');
  162. }
  163. public function testVariableInjection()
  164. {
  165. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  166. I18n::loadTranslations();
  167. $this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
  168. }
  169. public function testHtmlEntityEncoding()
  170. {
  171. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  172. I18n::loadTranslations();
  173. $input = '&<>"\'/`=';
  174. $result = htmlspecialchars($input, ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED, 'UTF-8', false);
  175. $this->assertEquals($result, I18n::encode($input), 'encodes HTML entities');
  176. $this->assertEquals('<a>some ' . $result . ' + 1</a>', I18n::_('<a>some %s + %d</a>', $input, 1), 'encodes parameters in translations');
  177. // Message ID should NOT be encoded (it comes from trusted source), only the parameter should be
  178. $this->assertEquals($input . $result, I18n::_($input . '%s', $input), 'encodes only parameters, not message ID');
  179. }
  180. public function testApostropheEncodngInMessage()
  181. {
  182. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr';
  183. I18n::loadTranslations();
  184. // For example, the French translation should not have the apostrophe encoded
  185. // See https://github.com/PrivateBin/PrivateBin/issues/1712
  186. $message = I18n::_('Document does not exist, has expired or has been deleted.');
  187. $this->assertFalse(strpos($message, '&apos;') !== false, 'French apostrophe should not be encoded in translation message');
  188. $this->assertTrue(strpos($message, "n'existe") !== false, 'French apostrophe should be present as literal character');
  189. }
  190. public function testFallbackAlwaysPresent()
  191. {
  192. $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_i18n';
  193. if (!is_dir($path)) {
  194. mkdir($path);
  195. }
  196. $languageIterator = new AppendIterator();
  197. $languageIterator->append(new GlobIterator(I18nMock::getPath('??.json')));
  198. $languageIterator->append(new GlobIterator(I18nMock::getPath('???.json'))); // for jbo
  199. $languageCount = 0;
  200. foreach ($languageIterator as $file) {
  201. ++$languageCount;
  202. $this->assertTrue(copy($file->getPathname(), $path . DIRECTORY_SEPARATOR . $file->getBasename()));
  203. }
  204. I18nMock::resetPath($path);
  205. $languagesDevelopment = I18nMock::getAvailableLanguages();
  206. $this->assertEquals($languageCount, count($languagesDevelopment), 'all copied languages detected');
  207. $this->assertTrue(in_array('en', $languagesDevelopment), 'English fallback present');
  208. unlink($path . DIRECTORY_SEPARATOR . 'en.json');
  209. I18nMock::resetAvailableLanguages();
  210. $languagesDeployed = I18nMock::getAvailableLanguages();
  211. $this->assertEquals($languageCount, count($languagesDeployed), 'all copied languages detected, plus fallback');
  212. $this->assertTrue(in_array('en', $languagesDeployed), 'English fallback still present');
  213. I18nMock::resetAvailableLanguages();
  214. I18nMock::resetPath();
  215. Helper::rmDir($path);
  216. }
  217. public function testGetCopyHotkey()
  218. {
  219. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)';
  220. $this->assertEquals('Cmd', I18n::getCopyHotkey(), 'returns Cmd on macOS');
  221. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';
  222. $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl on Windows');
  223. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (X11; Linux x86_64)';
  224. $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl on Linux');
  225. unset($_SERVER['HTTP_USER_AGENT']);
  226. $this->assertEquals('Ctrl', I18n::getCopyHotkey(), 'returns Ctrl when user agent absent');
  227. }
  228. public function testMessageIdsExistInAllLanguages()
  229. {
  230. $messageIds = [];
  231. $languages = [];
  232. foreach (new DirectoryIterator(PATH . 'i18n') as $file) {
  233. $fileNameLength = strlen($file->getFilename());
  234. if ($fileNameLength === 7) { // xx.json
  235. $language = substr($file->getFilename(), 0, 2);
  236. } elseif ($fileNameLength === 8) { // jbo.json
  237. $language = substr($file->getFilename(), 0, 3);
  238. } else {
  239. continue;
  240. }
  241. $languageJson = file_get_contents($file->getPathname());
  242. $languageMessageIds = array_keys(Json::decode($languageJson));
  243. $messageIds = array_unique(array_merge($messageIds, $languageMessageIds));
  244. $languages[$language] = $languageMessageIds;
  245. }
  246. foreach ($messageIds as $messageId) {
  247. foreach (array_keys($languages) as $language) {
  248. // most languages don't translate the data size units, ignore those
  249. if ($messageId !== 'B' && strlen($messageId) !== 3 && strpos($messageId, 'B', 2) !== 2) {
  250. $this->assertContains(
  251. $messageId,
  252. $languages[$language],
  253. "message ID '$messageId' exists in translation file $language.json"
  254. );
  255. }
  256. }
  257. }
  258. }
  259. }