prevent regression around presence or absence of en.json

it gets excluded in the release archive, it's absence should not make
any difference
This commit is contained in:
El RIDO 2023-12-18 21:45:26 +01:00
parent 0a7a341fda
commit d49be80ffb
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
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();