diff --git a/lib/I18n.php b/lib/I18n.php index 3fed6754..22142c00 100644 --- a/lib/I18n.php +++ b/lib/I18n.php @@ -315,10 +315,10 @@ class I18n */ protected static function _getPath($file = '') { - if (strlen(self::$_path) == 0) { + if (empty(self::$_path)) { self::$_path = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'i18n'; } - return self::$_path . (strlen($file) ? DIRECTORY_SEPARATOR . $file : ''); + return self::$_path . (empty($file) ? '' : DIRECTORY_SEPARATOR . $file); } /** diff --git a/tst/I18nTest.php b/tst/I18nTest.php index e53c36d0..e606a8b3 100644 --- a/tst/I18nTest.php +++ b/tst/I18nTest.php @@ -3,6 +3,24 @@ use PHPUnit\Framework\TestCase; use PrivateBin\I18n; +class I18nMock extends I18n +{ + public static function resetAvailableLanguages() + { + self::$_availableLanguages = array(); + } + + public static function resetPath($path = '') + { + self::$_path = $path; + } + + public static function getPath($file = '') + { + return self::_getPath($file); + } +} + class I18nTest extends TestCase { private $_translations = array(); @@ -167,6 +185,38 @@ class I18nTest extends TestCase $this->assertEquals($result . $result, I18n::_($input . '%s', $input), 'encodes message ID as well, when no link'); } + public function testFallbackAlwaysPresent() + { + $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_i18n'; + if (!is_dir($path)) { + mkdir($path); + } + + $languageIterator = new AppendIterator(); + $languageIterator->append(new GlobIterator(I18nMock::getPath('??.json'))); + $languageIterator->append(new GlobIterator(I18nMock::getPath('???.json'))); // for jbo + $languageCount = 0; + foreach ($languageIterator as $file) { + ++$languageCount; + $this->assertTrue(copy($file, $path . DIRECTORY_SEPARATOR . $file->getBasename())); + } + + I18nMock::resetPath($path); + $languagesDevelopment = I18nMock::getAvailableLanguages(); + $this->assertEquals($languageCount, count($languagesDevelopment), 'all copied languages detected'); + $this->assertTrue(in_array('en', $languagesDevelopment), 'English fallback present'); + + unlink($path . DIRECTORY_SEPARATOR . 'en.json'); + I18nMock::resetAvailableLanguages(); + $languagesDeployed = I18nMock::getAvailableLanguages(); + $this->assertEquals($languageCount, count($languagesDeployed), 'all copied languages detected, plus fallback'); + $this->assertTrue(in_array('en', $languagesDeployed), 'English fallback still present'); + + I18nMock::resetAvailableLanguages(); + I18nMock::resetPath(); + Helper::rmDir($path); + } + public function testMessageIdsExistInAllLanguages() { $messageIds = array();