mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 14:28:27 +08:00
Fix to do not delete used message files with nested category names (#17021)
This commit is contained in:

committed by
Alexander Makarov

parent
b4cb42fb9d
commit
ec1809bed2
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
||||
2.0.16 under development
|
||||
------------------------
|
||||
|
||||
- Bug #17021: Fix to do not remove existing message category files in a subfolder (albertborsos)
|
||||
- Bug #16991: Removed usage of `utf8_encode()` from `Request::resolvePathInfo()` (GHopperMSK)
|
||||
- Bug #16974: Regular Expression Validator to include support for 'u' (UTF-8) modifier (Dzhuneyt)
|
||||
- Chg #16941: Set `yii\console\controllers\MigrateController::useTablePrefix` to true as default value (GHopperMSK)
|
||||
|
@ -669,7 +669,7 @@ EOD;
|
||||
}
|
||||
|
||||
if ($removeUnused) {
|
||||
$this->deleteUnusedPhpMessageFiles($dirName, array_keys($messages));
|
||||
$this->deleteUnusedPhpMessageFiles(array_keys($messages), $dirName);
|
||||
}
|
||||
}
|
||||
|
||||
@ -889,13 +889,16 @@ EOD;
|
||||
}
|
||||
}
|
||||
|
||||
private function deleteUnusedPhpMessageFiles($dirName, $existingCategories)
|
||||
private function deleteUnusedPhpMessageFiles($existingCategories, $dirName)
|
||||
{
|
||||
$messageFiles = FileHelper::findFiles($dirName);
|
||||
foreach ($messageFiles as $file) {
|
||||
$category = preg_replace('#\.php$#', '', basename($file));
|
||||
foreach ($messageFiles as $messageFile) {
|
||||
$categoryFileName = str_replace($dirName, '', $messageFile);
|
||||
$categoryFileName = ltrim($categoryFileName, DIRECTORY_SEPARATOR);
|
||||
$category = preg_replace('#\.php$#', '', $categoryFileName);
|
||||
|
||||
if (!in_array($category, $existingCategories, true)) {
|
||||
unlink($file);
|
||||
unlink($messageFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,26 +123,49 @@ class PHPMessageControllerTest extends BaseMessageControllerTest
|
||||
$this->assertEqualsWithoutLE($expected, $head);
|
||||
}
|
||||
|
||||
public function testDeleteUnusedMessageFiles()
|
||||
public function messageFileCategoriesDataProvider(){
|
||||
return [
|
||||
'removeUnused:false - unused category should not be removed - normal category' => ['test_delete_category', true, false, true],
|
||||
'removeUnused:false - unused category should not be removed - nested category' => ['nested/category', true, false, true],
|
||||
'removeUnused:false - unused category should not be removed - nested 3 level category' => ['multi-level/nested/category', true, false, true],
|
||||
|
||||
'removeUnused:false - used category should not be removed - normal category' => ['test_delete_category', false, false, true],
|
||||
'removeUnused:false - used category should not be removed - nested category' => ['nested/category', false, false, true],
|
||||
'removeUnused:false - used category should not be removed - nested 3 level category' => ['multi-level/nested/category', false, false, true],
|
||||
|
||||
'removeUnused:true - used category should not be removed - normal category' => ['test_delete_category', false, true, true],
|
||||
'removeUnused:true - used category should not be removed - nested category' => ['nested/category', false, true, true],
|
||||
'removeUnused:true - used category should not be removed - nested 3 level category' => ['multi-level/nested/category', false, true, true],
|
||||
|
||||
'removeUnused:true - unused category should be removed - normal category' => ['test_delete_category', true, true, false],
|
||||
'removeUnused:true - unused category should be removed - nested category' => ['nested/category', true, true, false],
|
||||
'removeUnused:true - unused category should be removed - nested 3 level category' => ['multi-level/nested/category', true, true, false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider messageFileCategoriesDataProvider
|
||||
*/
|
||||
public function testRemoveUnusedBehavior($category, $isUnused, $removeUnused, $isExpectedToExist)
|
||||
{
|
||||
$category = 'test_delete_category';
|
||||
$this->saveMessages(['test message' => 'test translation'], $category);
|
||||
$filePath = $this->getMessageFilePath($category);
|
||||
|
||||
$this->saveConfigFile($this->getConfig([
|
||||
'removeUnused' => false,
|
||||
'removeUnused' => $removeUnused,
|
||||
]));
|
||||
|
||||
if (!$isUnused) {
|
||||
$message = 'test message';
|
||||
$sourceFileContent = "Yii::t('{$category}', '{$message}');";
|
||||
$this->createSourceFile($sourceFileContent);
|
||||
}
|
||||
|
||||
$this->runMessageControllerAction('extract', [$this->configFileName]);
|
||||
$this->assertFileExists($filePath,
|
||||
'File with unused translations should not be deleted if "removeUnused" option is false');
|
||||
|
||||
|
||||
$this->saveConfigFile($this->getConfig([
|
||||
'removeUnused' => true,
|
||||
]));
|
||||
$this->runMessageControllerAction('extract', [$this->configFileName]);
|
||||
|
||||
$this->assertFileNotExists($filePath,
|
||||
'File with unused translations should be deleted if "removeUnused" option is true');
|
||||
if ($isExpectedToExist) {
|
||||
$this->assertFileExists($filePath);
|
||||
} else {
|
||||
$this->assertFileNotExists($filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user