Permalink
Browse files

Some refactoring and adding test for disabled rendering.

  • Loading branch information...
1 parent ba54a47 commit 5e25ee36f4c6a546cee4927e5b3b544612a5893f @markstory markstory committed Jan 7, 2014
Showing with 40 additions and 8 deletions.
  1. +14 −7 Cake/View/Input/SelectBox.php
  2. +26 −1 Test/TestCase/View/Input/SelectBoxTest.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);
@@ -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');
}

0 comments on commit 5e25ee3

Please sign in to comment.