Merge pull request #1220 from PrivateBin/en-default-test

prevent regression around presence or absence of en.json
This commit is contained in:
El RIDO 2023-12-23 13:06:05 +01:00 committed by GitHub
commit 2985305dbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 2 deletions

View File

@ -315,10 +315,10 @@ class I18n
*/ */
protected static function _getPath($file = '') protected static function _getPath($file = '')
{ {
if (strlen(self::$_path) == 0) { if (empty(self::$_path)) {
self::$_path = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'i18n'; self::$_path = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'i18n';
} }
return self::$_path . (strlen($file) ? DIRECTORY_SEPARATOR . $file : ''); return self::$_path . (empty($file) ? '' : DIRECTORY_SEPARATOR . $file);
} }
/** /**

View File

@ -3,6 +3,24 @@
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PrivateBin\I18n; 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 class I18nTest extends TestCase
{ {
private $_translations = array(); 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'); $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() public function testMessageIdsExistInAllLanguages()
{ {
$messageIds = array(); $messageIds = array();