From 5e25ee36f4c6a546cee4927e5b3b544612a5893f Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 6 Jan 2014 23:23:10 -0500 Subject: [PATCH] Some refactoring and adding test for disabled rendering. --- Cake/View/Input/SelectBox.php | 21 +++++++++++------ Test/TestCase/View/Input/SelectBoxTest.php | 27 +++++++++++++++++++++- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Cake/View/Input/SelectBox.php b/Cake/View/Input/SelectBox.php index 53b7c5c68d3..899a3398741 100644 --- a/Cake/View/Input/SelectBox.php +++ b/Cake/View/Input/SelectBox.php @@ -88,13 +88,8 @@ protected function _renderOptions($data) { foreach ($options as $key => $val) { $template = 'option'; - $strict = !is_numeric($key); - - if ( - isset($selected) && - (!$selectedArray && (string)$key === (string)$selected) || - ($selectedArray && in_array((string)$key, $selected, $strict)) - ) { + $isSelected = $this->_isSelected($key, $selected); + if ($isSelected) { $template = 'optionSelected'; } @@ -106,6 +101,18 @@ protected function _renderOptions($data) { return $out; } + protected function _isSelected($key, $selected) { + if ($selected === null) { + return false; + } + $isArray = is_array($selected); + if (!$isArray) { + return (string)$key === (string)$selected; + } + $strict = !is_numeric($key); + return in_array((string)$key, $selected, $strict); + } + protected function _parseAttributes($options, $exclude = null) { $insertBefore = ' '; $options = (array)$options + array('escape' => true); diff --git a/Test/TestCase/View/Input/SelectBoxTest.php b/Test/TestCase/View/Input/SelectBoxTest.php index 7edda3c492b..3759fa66f14 100644 --- a/Test/TestCase/View/Input/SelectBoxTest.php +++ b/Test/TestCase/View/Input/SelectBoxTest.php @@ -200,11 +200,36 @@ public function testRenderOptionGroupsSelected() { } /** - * test rendering a disabled element + * test rendering a totally disabled element * * @return void */ public function testRenderDisabled() { + $select = new SelectBox($this->templates); + $data = [ + 'disabled' => true, + 'name' => 'Birds[name]', + 'options' => ['a' => 'Albatross', 'b' => 'Budgie'] + ]; + $result = $select->render($data); + $expected = [ + 'select' => [ + 'name' => 'Birds[name]', + 'disabled' => 'disabled', + ], + ['option' => ['value' => 'a']], 'Albatross', '/option', + ['option' => ['value' => 'b']], 'Budgie', '/option', + '/select' + ]; + $this->assertTags($result, $expected); + } + +/** + * test rendering a disabled element + * + * @return void + */ + public function testRenderDisabledMultiple() { $this->markTestIncomplete('Not done'); }