From ec82ec4cd78021fbc40fee56f0742336d3341ff5 Mon Sep 17 00:00:00 2001 From: Vitaliy Pikalov Date: Thu, 19 Nov 2026 21:18:27 +0600 Subject: [PATCH 1/2] Fix #20694: Use the given `$actionID` in `yii\console\Controller::getOptionValues()` `getOptionValues()` ignored its `$actionID` argument and always resolved options for the current action, so it returned wrong values for any other action id and failed when no action was running. Co-Authored-By: Claude Opus 5.5 --- framework/CHANGELOG.md | 1 + framework/console/Controller.php | 5 +-- tests/framework/console/ControllerTest.php | 51 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index f984149604c..8c7837116a3 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -159,6 +159,7 @@ Yii Framework 2 Change Log - Enh #21060: Add PSR-3 logging interoperability with `yii\log\PsrLogger` and `yii\log\PsrTarget` (terabytesoftw) - Enh #21061: Add precise PHPStan array shapes for logger messages, profiling timings, and target filtering; remove an unreachable dispatcher exception branch (terabytesoftw) - Bug #21162: Fix standalone actions: run sub-module hooks, short `Action::$id`, opt-in `actionNamespace`, exact `actionMap` routes (terabytesoftw) +- Bug #20694: Fix `yii\console\Controller::getOptionValues()` to return the option values of the given `$actionID` instead of the current action (hrompik) 2.0.56 under development ------------------------ diff --git a/framework/console/Controller.php b/framework/console/Controller.php index 8f33b23512f..8bdec6548a3 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -471,14 +471,13 @@ public function optionAliases() * Returns properties corresponding to the options for the action id * Child classes may override this method to specify possible properties. * - * @param string $actionID the action id of the current request + * @param string $actionID the action id to return the option values for * @return array properties corresponding to the options for the action */ public function getOptionValues($actionID) { - // $actionId might be used in subclasses to provide properties specific to action id $properties = []; - foreach ($this->options($this->action->id) as $property) { + foreach ($this->options($actionID) as $property) { $properties[$property] = $this->$property; } diff --git a/tests/framework/console/ControllerTest.php b/tests/framework/console/ControllerTest.php index 76dc8f149b4..7b765b21198 100644 --- a/tests/framework/console/ControllerTest.php +++ b/tests/framework/console/ControllerTest.php @@ -486,4 +486,55 @@ public function testGetActionHelpSummaryOnNull(): void $helpSummary = $controller->getActionHelpSummary(null); $this->assertEquals($controller->ansiFormat('Action not found.', Console::FG_RED), $helpSummary); } + + /** + * @see https://github.com/yiisoft/yii2/issues/20694 + */ + public function testGetOptionValuesUsesGivenActionId(): void + { + $controller = $this->createControllerWithActionSpecificOption(); + $controller->action = new InlineAction('index', $controller, 'actionIndex'); + + self::assertSame( + ['color', 'interactive', 'help', 'silentExitOnException'], + array_keys($controller->getOptionValues('index')), + 'Options of the given action should be returned.', + ); + self::assertSame( + ['color', 'interactive', 'help', 'silentExitOnException', 'dryRun'], + array_keys($controller->getOptionValues('deploy')), + 'Options of the given action should be returned, not those of the current action.', + ); + } + + /** + * @see https://github.com/yiisoft/yii2/issues/20694 + */ + public function testGetOptionValuesWithoutCurrentAction(): void + { + $controller = $this->createControllerWithActionSpecificOption(dryRun: true); + + self::assertNull($controller->action); + self::assertTrue( + $controller->getOptionValues('deploy')['dryRun'], + 'Option values should be available before an action is run.', + ); + } + + private function createControllerWithActionSpecificOption(bool $dryRun = false): Controller + { + $controller = new class('fake', Yii::$app) extends Controller { + public bool $dryRun = false; + + public function options($actionID) + { + $options = parent::options($actionID); + + return $actionID === 'deploy' ? [...$options, 'dryRun'] : $options; + } + }; + $controller->dryRun = $dryRun; + + return $controller; + } } From aa1c3613fd2ddc1b8d91f3e01451402f064081c4 Mon Sep 17 00:00:00 2001 From: Vitaliy Pikalov Date: Thu, 19 Nov 2026 22:49:45 +0600 Subject: [PATCH 2/2] Use `defaultAction` for an empty `$actionID` in `yii\console\Controller::getOptionValues()` An empty action ID refers to the default action, the same way as in `runAction()`. Co-Authored-By: Claude Opus 5.5 --- framework/console/Controller.php | 5 +++-- tests/framework/console/ControllerTest.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/framework/console/Controller.php b/framework/console/Controller.php index 8bdec6548a3..51400a258f1 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -471,13 +471,14 @@ public function optionAliases() * Returns properties corresponding to the options for the action id * Child classes may override this method to specify possible properties. * - * @param string $actionID the action id to return the option values for + * @param string $actionID the action id to return the option values for. + * If it is empty, the option values of [[defaultAction]] are returned. * @return array properties corresponding to the options for the action */ public function getOptionValues($actionID) { $properties = []; - foreach ($this->options($actionID) as $property) { + foreach ($this->options($actionID === '' ? $this->defaultAction : $actionID) as $property) { $properties[$property] = $this->$property; } diff --git a/tests/framework/console/ControllerTest.php b/tests/framework/console/ControllerTest.php index 7b765b21198..deba6b8ad2e 100644 --- a/tests/framework/console/ControllerTest.php +++ b/tests/framework/console/ControllerTest.php @@ -521,6 +521,20 @@ public function testGetOptionValuesWithoutCurrentAction(): void ); } + /** + * @see https://github.com/yiisoft/yii2/issues/20694 + */ + public function testGetOptionValuesForEmptyActionIdUsesDefaultAction(): void + { + $controller = $this->createControllerWithActionSpecificOption(dryRun: true); + $controller->defaultAction = 'deploy'; + + self::assertTrue( + $controller->getOptionValues('')['dryRun'], + 'Options of the default action should be returned for an empty action ID.', + ); + } + private function createControllerWithActionSpecificOption(bool $dryRun = false): Controller { $controller = new class('fake', Yii::$app) extends Controller {