Skip to content

Commit

Permalink
Some refactoring and adding test for disabled rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 7, 2014
1 parent ba54a47 commit 5e25ee3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
21 changes: 14 additions & 7 deletions Cake/View/Input/SelectBox.php
Expand Up @@ -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';
}

Expand All @@ -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);
Expand Down
27 changes: 26 additions & 1 deletion Test/TestCase/View/Input/SelectBoxTest.php
Expand Up @@ -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');
}

Expand Down

0 comments on commit 5e25ee3

Please sign in to comment.