From 4de66f8205fe74d643b010a922a093ce390a5bb2 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Tue, 24 Mar 2020 12:55:35 +0300 Subject: [PATCH] Fix #16145: Fix `Html` helper `checkboxList()`, `radioList()`, `renderSelectOptions()`, `dropDownList()`, `listBox()` methods to work properly with traversable selection --- framework/CHANGELOG.md | 1 + framework/helpers/BaseHtml.php | 6 ++-- tests/framework/helpers/HtmlTest.php | 51 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 9ce11461fa..dcfd9c755f 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.33 under development ------------------------ +- Bug #16145: Fix `Html` helper `checkboxList()`, `radioList()`, `renderSelectOptions()`, `dropDownList()`, `listBox()` methods to work properly with traversable selection (samdark) - Bug #17797: Fix for `activeListInput` options (alex-code) - Bug #16092: Fix duplicate joins in usage of `joinWith` (germanow) - Bug #17679: Fix Oracle exception "ORA-01461: can bind a LONG value only for insert into a LONG column" when inserting 4k+ string (vinpel, 243083df) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index e29b869bc4..7d3862a3d9 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -954,7 +954,7 @@ class BaseHtml $name .= '[]'; } if (ArrayHelper::isTraversable($selection)) { - $selection = array_map('strval', (array)$selection); + $selection = array_map('strval', ArrayHelper::toArray($selection)); } $formatter = ArrayHelper::remove($options, 'item'); @@ -1041,7 +1041,7 @@ class BaseHtml public static function radioList($name, $selection = null, $items = [], $options = []) { if (ArrayHelper::isTraversable($selection)) { - $selection = array_map('strval', (array)$selection); + $selection = array_map('strval', ArrayHelper::toArray($selection)); } $formatter = ArrayHelper::remove($options, 'item'); @@ -1854,7 +1854,7 @@ class BaseHtml public static function renderSelectOptions($selection, $items, &$tagOptions = []) { if (ArrayHelper::isTraversable($selection)) { - $selection = array_map('strval', (array)$selection); + $selection = array_map('strval', ArrayHelper::toArray($selection)); } $lines = []; diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php index f9dbf58383..4e2e3a9d0a 100644 --- a/tests/framework/helpers/HtmlTest.php +++ b/tests/framework/helpers/HtmlTest.php @@ -9,6 +9,7 @@ namespace yiiunit\framework\helpers; use Yii; use yii\base\DynamicModel; +use yii\db\ArrayExpression; use yii\helpers\Html; use yii\helpers\Url; use yiiunit\TestCase; @@ -769,6 +770,56 @@ EOD; ])); } + public function testRadioListWithArrayExpression() + { + $selection = new ArrayExpression(['first']); + + $output = Html::radioList( + 'test', + $selection, + [ + 'first' => 'first', + 'second' => 'second' + ] + ); + + $this->assertEqualsWithoutLE('
+
', $output); + } + + public function testCheckboxListWithArrayExpression() + { + $selection = new ArrayExpression(['first']); + + $output = Html::checkboxList( + 'test', + $selection, + [ + 'first' => 'first', + 'second' => 'second' + ] + ); + + $this->assertEqualsWithoutLE('
+
', $output); + } + + public function testRenderSelectOptionsWithArrayExpression() + { + $selection = new ArrayExpression(['first']); + + $output = Html::renderSelectOptions( + $selection, + [ + 'first' => 'first', + 'second' => 'second' + ] + ); + + $this->assertEqualsWithoutLE(' +', $output); + } + public function testRadioList() { $this->assertEquals('
', Html::radioList('test'));