From 6bde69aa966adcf048fa87fcac0baf152b1c11d7 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 6 Oct 2017 22:24:38 +0200 Subject: [PATCH] Fixes #14016: Fixed empty messages marked as unused in PHP and PO sources when extracted with message command when `markUnused` is `false` --- framework/CHANGELOG.md | 1 + .../console/controllers/MessageController.php | 4 +-- .../controllers/BaseMessageControllerTest.php | 35 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index b1aa33e100..6c75b1b62f 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.13 under development ------------------------ +- Bug #14016: Fixed empty messages marked as unused in PHP and PO sources when extracted with message command when `markUnused` is `false` (samdark) - Enh #9438: `yii\web\DbSession` now relies on error handler to display errors (samdark) - Bug #6226: Fix fatal symlink error when publishing in multi threaded environments (dynasource) - Enh #13486: Use DI container to instantiate cookies in order to be able to set defaults (samdark) diff --git a/framework/console/controllers/MessageController.php b/framework/console/controllers/MessageController.php index da578dafcf..730b6594ef 100644 --- a/framework/console/controllers/MessageController.php +++ b/framework/console/controllers/MessageController.php @@ -702,7 +702,7 @@ EOD; ksort($existingMessages); foreach ($existingMessages as $message => $translation) { if (!$removeUnused && !isset($merged[$message]) && !isset($todo[$message])) { - if (!empty($translation) && (!$markUnused || (strncmp($translation, '@@', 2) === 0 && substr_compare($translation, '@@', -2, 2) === 0))) { + if (!$markUnused || (!empty($translation) && (strncmp($translation, '@@', 2) === 0 && substr_compare($translation, '@@', -2, 2) === 0))) { $todo[$message] = $translation; } else { $todo[$message] = '@@' . $translation . '@@'; @@ -804,7 +804,7 @@ EOD; // add obsolete unused messages foreach ($existingMessages as $message => $translation) { if (!$removeUnused && !isset($merged[$category . chr(4) . $message]) && !isset($todos[$category . chr(4) . $message])) { - if (!empty($translation) && (!$markUnused || (substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@'))) { + if (!$markUnused || (!empty($translation) && ((substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@')))) { $todos[$category . chr(4) . $message] = $translation; } else { $todos[$category . chr(4) . $message] = '@@' . $translation . '@@'; diff --git a/tests/framework/console/controllers/BaseMessageControllerTest.php b/tests/framework/console/controllers/BaseMessageControllerTest.php index c67110b8d0..cdc42e3e61 100644 --- a/tests/framework/console/controllers/BaseMessageControllerTest.php +++ b/tests/framework/console/controllers/BaseMessageControllerTest.php @@ -478,6 +478,41 @@ abstract class BaseMessageControllerTest extends TestCase $this->assertArrayHasKey($mainMessage, $messages, "\"$mainMessage\" is missing in translation file. Command output:\n\n" . $out); } + + /** + * @see https://github.com/yiisoft/yii2/issues/14016 + */ + public function testShouldNotMarkUnused() + { + $category = 'my'; + + $key1 = 'key1'; + $key2 = 'key2'; + + $this->saveMessages( + [ + $key1 => '', + $key2 => '', + ], + $category + ); + + $sourceFileContent = 'Yii::t("my", "test");'; + $this->createSourceFile($sourceFileContent); + + $this->saveConfigFile($this->getConfig(['markUnused' => false])); + $out = $this->runMessageControllerAction('extract', [$this->configFileName]); + $messages = $this->loadMessages($category); + + $this->assertArrayHasKey($key1, $messages, "$key1 isn't there. Command output:\n\n" . $out); + $this->assertArrayHasKey($key2, $messages, "$key2 isn't there. Command output:\n\n" . $out); + + $value1 = $messages[$key1]; + $value2 = $messages[$key2]; + + $this->assertEquals('', $value1, "Message at $key1 should be empty but it is $value1. Command output:\n\n" . $out); + $this->assertEquals('', $value2, "Message at $key2 should be empty but it is $value2. Command output:\n\n" . $out); + } } class MessageControllerMock extends MessageController