Skip to content

Commit

Permalink
Add tests for non array versions of options.
Browse files Browse the repository at this point in the history
Both radio() and select() need to accept Collection/Traversable objects.
Failing to do so makes FormHelper sad when an ORM\Query is passed in.

Refs #3317
  • Loading branch information
markstory committed Apr 15, 2014
1 parent c0dae5b commit 58c0b14
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/View/Helper/FormHelper.php
Expand Up @@ -870,11 +870,11 @@ public function input($fieldName, array $options = []) {
protected function _getInput($fieldName, $options) {
switch ($options['type']) {
case 'select':
$opts = (array)$options['options'];
$opts = $options['options'];
unset($options['options']);
return $this->select($fieldName, $opts, $options);
case 'radio':
$opts = (array)$options['options'];
$opts = $options['options'];
unset($options['options']);
return $this->radio($fieldName, $opts, $options);
case 'url':
Expand Down Expand Up @@ -1161,12 +1161,12 @@ public function checkbox($fieldName, array $options = []) {
* the radio label will be 'empty'. Set this option to a string to control the label value.
*
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
* @param array $options Radio button options array.
* @param array|\Traversable $options Radio button options array.
* @param array $attributes Array of HTML attributes, and special attributes above.
* @return string Completed radio widget set.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
*/
public function radio($fieldName, array $options = [], array $attributes = []) {
public function radio($fieldName, $options = [], array $attributes = []) {
$attributes = $this->_initInputField($fieldName, $attributes);

$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
Expand Down Expand Up @@ -1555,14 +1555,14 @@ public function submit($caption = null, array $options = []) {
* }}}
*
* @param string $fieldName Name attribute of the SELECT
* @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
* @param array|\Traversable $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
* SELECT element
* @param array $attributes The HTML attributes of the select element.
* @return string Formatted SELECT element
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
* @see \Cake\View\Helper\FormHelper::multiCheckbox() for creating multiple checkboxes.
*/
public function select($fieldName, array $options = [], array $attributes = []) {
public function select($fieldName, $options = [], array $attributes = []) {
$attributes += [
'disabled' => null,
'escape' => true,
Expand Down
80 changes: 47 additions & 33 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\View\Helper;

use Cake\Collection\Collection;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
Expand Down Expand Up @@ -3060,6 +3061,9 @@ public function testRadio() {
);
$this->assertTags($result, $expected);

$result = $this->Form->radio('Model.field', new Collection(['option A']));
$this->assertTags($result, $expected);

$result = $this->Form->radio('Model.field', array('option A', 'option B'));
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
Expand Down Expand Up @@ -3192,6 +3196,9 @@ public function testSelect() {
);
$this->assertTags($result, $expected);

$result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
$this->assertTags($result, $expected);

$this->Form->request->data = array();
$result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
$expected = array(
Expand All @@ -3206,39 +3213,6 @@ public function testSelect() {
);
$this->assertTags($result, $expected);

$result = $this->Form->select(
'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
array('empty' => false)
);
$expected = array(
'select' => array('name' => 'Model[field]'),
array('option' => array('value' => 'first')),
'first &quot;html&quot; &lt;chars&gt;',
'/option',
array('option' => array('value' => 'second')),
'value',
'/option',
'/select'
);
$this->assertTags($result, $expected);

$result = $this->Form->select(
'Model.field',
array('first' => 'first "html" <chars>', 'second' => 'value'),
array('escape' => false, 'empty' => false)
);
$expected = array(
'select' => array('name' => 'Model[field]'),
array('option' => array('value' => 'first')),
'first "html" <chars>',
'/option',
array('option' => array('value' => 'second')),
'value',
'/option',
'/select'
);
$this->assertTags($result, $expected);

$options = array(
array('value' => 'first', 'text' => 'First'),
array('value' => 'first', 'text' => 'Another First'),
Expand Down Expand Up @@ -3288,6 +3262,46 @@ public function testSelect() {
$this->assertTags($result, $expected);
}

/**
* Test that select() escapes HTML.
*
* @return void
*/
public function testSelectEscapeHtml() {
$result = $this->Form->select(
'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
array('empty' => false)
);
$expected = array(
'select' => array('name' => 'Model[field]'),
array('option' => array('value' => 'first')),
'first &quot;html&quot; &lt;chars&gt;',
'/option',
array('option' => array('value' => 'second')),
'value',
'/option',
'/select'
);
$this->assertTags($result, $expected);

$result = $this->Form->select(
'Model.field',
array('first' => 'first "html" <chars>', 'second' => 'value'),
array('escape' => false, 'empty' => false)
);
$expected = array(
'select' => array('name' => 'Model[field]'),
array('option' => array('value' => 'first')),
'first "html" <chars>',
'/option',
array('option' => array('value' => 'second')),
'value',
'/option',
'/select'
);
$this->assertTags($result, $expected);
}

/**
* test select() with required and disabled attributes.
*
Expand Down

0 comments on commit 58c0b14

Please sign in to comment.