Skip to content
Browse files

Adding tests for select helper int/string agnosticism

  • Loading branch information...
2 parents 4e674f8 + bd06238 commit f2abe3b2c66b7d9893fa4deb1279df62da769c1d @toomuchpete toomuchpete committed with nateabele Apr 12, 2012
Showing with 51 additions and 3 deletions.
  1. +3 −3 template/helper/Form.php
  2. +48 −0 tests/cases/template/helper/FormTest.php
View
6 template/helper/Form.php
@@ -597,8 +597,7 @@ protected function _selectOptions(array $list, array $scope) {
}
$selected = (
(is_array($scope['value']) && in_array($value, $scope['value'])) ||
- ($scope['empty'] && empty($scope['value']) && $value === '') ||
- ($scope['value'] === (is_integer($value) ? (string) $value : $value) )
+ ((string) $scope['value'] === (string) $value)
);
$options = $selected ? array('selected' => true) : array();
$params = compact('value', 'title', 'options');
@@ -622,12 +621,13 @@ public function checkbox($name, array $options = array()) {
$defaults = array('value' => '1', 'hidden' => true);
$options += $defaults;
$default = $options['value'];
+ $key = $name;
$out = '';
list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
list($scope, $options) = $this->_options($defaults, $options);
- if (!isset($options['checked']) && ($bound = $this->binding($name)->data)) {
+ if (!isset($options['checked']) && ($bound = $this->binding($key)->data)) {
$options['checked'] = ($bound == $default);
}
if ($scope['hidden']) {
View
48 tests/cases/template/helper/FormTest.php
@@ -381,6 +381,18 @@ public function testCheckboxGeneration() {
'checked' => 'checked', 'id' => 'MockFormPostFoo'
))
));
+
+ $document = new Document(array('model' => $this->_model, 'data' => array('subdocument' => array('foo' => true))));
+ $this->form->create($document);
+
+ $result = $this->form->checkbox('subdocument.foo');
+ $this->assertTags($result, array(
+ array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'subdocument[foo]')),
+ array('input' => array(
+ 'type' => 'checkbox', 'value' => '1', 'name' => 'subdocument[foo]',
+ 'checked' => 'checked', 'id' => 'MockFormPostSubdocumentFoo'
+ ))
+ ));
}
public function testCustomCheckbox() {
@@ -600,6 +612,42 @@ public function testSelectGeneration() {
));
}
+ /**
+ * When trying to determine which option of a select box should be selected, we should be
+ * int/string agnostic because it all looks the same in HTML.
+ */
+ public function testSelectTypeAgnosticism() {
+ $taglist = array(
+ 'select' => array('name' => 'numbers', 'id' => 'Numbers'),
+ array('option' => array('value' => '0')),
+ 'Zero',
+ '/option',
+ array('option' => array('value' => '1', 'selected' => 'selected')),
+ 'One',
+ '/option',
+ array('option' => array('value' => '2')),
+ 'Two',
+ '/option',
+ '/select'
+ );
+
+ $result = $this->form->select(
+ 'numbers',
+ array(0 => 'Zero', 1 => 'One', 2 => 'Two'),
+ array('id' => 'Numbers', 'value' => '1')
+ );
+
+ $this->assertTags($result, $taglist);
+
+ $result = $this->form->select(
+ 'numbers',
+ array('0' => 'Zero', '1' => 'One', '2' => 'Two'),
+ array('id' => 'Numbers', 'value' => 1)
+ );
+
+ $this->assertTags($result, $taglist);
+ }
+
public function testSelectWithEmptyOption() {
$result = $this->form->select('numbers', array('zero', 'first', 'second'), array(
'empty' => true

0 comments on commit f2abe3b

Please sign in to comment.
Something went wrong with that request. Please try again.