diff --git a/Cake/View/Input/SelectBox.php b/Cake/View/Input/SelectBox.php index 649bcd963ec..9e388311fb5 100644 --- a/Cake/View/Input/SelectBox.php +++ b/Cake/View/Input/SelectBox.php @@ -72,17 +72,24 @@ public function render($data) { protected function _renderOptions($data) { $out = []; + if (!isset($data['options'])) { + $data['options'] = []; + } + $options = $data['options']; + if (!empty($data['empty'])) { - // TODO + $value = $data['empty'] === true ? '' : $data['empty']; + $empty = ['' => $value]; + $options = $empty + $options; } - if (empty($data['options'])) { + if (empty($options)) { return $out; } $selected = isset($data['value']) ? $data['value'] : null; $selectedArray = is_array($selected); - foreach ($data['options'] as $key => $val) { + foreach ($options as $key => $val) { $template = 'option'; $strict = !is_numeric($key); diff --git a/Test/TestCase/View/Input/SelectBoxTest.php b/Test/TestCase/View/Input/SelectBoxTest.php index 225631763c7..4f4b1f27b70 100644 --- a/Test/TestCase/View/Input/SelectBoxTest.php +++ b/Test/TestCase/View/Input/SelectBoxTest.php @@ -87,7 +87,41 @@ public function testRenderSimple() { * @return void */ public function testRenderSelected() { - $this->markTestIncomplete('Not done'); + $context = new Context(); + $select = new SelectBox($this->templates, $context); + $data = [ + 'id' => 'BirdName', + 'name' => 'Birds[name]', + 'value' => '1', + 'options' => [ + 1 => 'one', + '1x' => 'one x', + '2' => 'two', + '2x' => 'two x', + ] + ]; + $result = $select->render($data); + $expected = [ + 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'], + ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option', + ['option' => ['value' => '1x']], 'one x', '/option', + ['option' => ['value' => '2']], 'two', '/option', + ['option' => ['value' => '2x']], 'two x', '/option', + '/select' + ]; + $this->assertTags($result, $expected); + + $data['value'] = 2; + $result = $select->render($data); + $expected = [ + 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'], + ['option' => ['value' => '1']], 'one', '/option', + ['option' => ['value' => '1x']], 'one x', '/option', + ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option', + ['option' => ['value' => '2x']], 'two x', '/option', + '/select' + ]; + $this->assertTags($result, $expected); } /** @@ -141,7 +175,45 @@ public function testRenderDisabled() { * @return void */ public function testRenderEmptyOption() { - $this->markTestIncomplete('Not done'); + $select = new SelectBox($this->templates, $context); + $data = [ + 'id' => 'BirdName', + 'name' => 'Birds[name]', + 'empty' => true, + 'options' => ['a' => 'Albatross', 'b' => 'Budgie'] + ]; + $result = $select->render($data); + $expected = [ + 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'], + ['option' => ['value' => '']], '/option', + ['option' => ['value' => 'a']], 'Albatross', '/option', + ['option' => ['value' => 'b']], 'Budgie', '/option', + '/select' + ]; + $this->assertTags($result, $expected); + + $data['empty'] = 'empty'; + $result = $select->render($data); + $expected = [ + 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'], + ['option' => ['value' => '']], 'empty', '/option', + ['option' => ['value' => 'a']], 'Albatross', '/option', + ['option' => ['value' => 'b']], 'Budgie', '/option', + '/select' + ]; + $this->assertTags($result, $expected); + + $data['empty'] = 'empty'; + $data['value'] = ''; + $result = $select->render($data); + $expected = [ + 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'], + ['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option', + ['option' => ['value' => 'a']], 'Albatross', '/option', + ['option' => ['value' => 'b']], 'Budgie', '/option', + '/select' + ]; + $this->assertTags($result, $expected); } }