Skip to content

Commit 58c0b14

Browse files
committed
Add tests for non array versions of options.
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
1 parent c0dae5b commit 58c0b14

File tree

2 files changed

+53
-39
lines changed

2 files changed

+53
-39
lines changed

src/View/Helper/FormHelper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,11 @@ public function input($fieldName, array $options = []) {
870870
protected function _getInput($fieldName, $options) {
871871
switch ($options['type']) {
872872
case 'select':
873-
$opts = (array)$options['options'];
873+
$opts = $options['options'];
874874
unset($options['options']);
875875
return $this->select($fieldName, $opts, $options);
876876
case 'radio':
877-
$opts = (array)$options['options'];
877+
$opts = $options['options'];
878878
unset($options['options']);
879879
return $this->radio($fieldName, $opts, $options);
880880
case 'url':
@@ -1161,12 +1161,12 @@ public function checkbox($fieldName, array $options = []) {
11611161
* the radio label will be 'empty'. Set this option to a string to control the label value.
11621162
*
11631163
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
1164-
* @param array $options Radio button options array.
1164+
* @param array|\Traversable $options Radio button options array.
11651165
* @param array $attributes Array of HTML attributes, and special attributes above.
11661166
* @return string Completed radio widget set.
11671167
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
11681168
*/
1169-
public function radio($fieldName, array $options = [], array $attributes = []) {
1169+
public function radio($fieldName, $options = [], array $attributes = []) {
11701170
$attributes = $this->_initInputField($fieldName, $attributes);
11711171

11721172
$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
@@ -1555,14 +1555,14 @@ public function submit($caption = null, array $options = []) {
15551555
* }}}
15561556
*
15571557
* @param string $fieldName Name attribute of the SELECT
1558-
* @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
1558+
* @param array|\Traversable $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
15591559
* SELECT element
15601560
* @param array $attributes The HTML attributes of the select element.
15611561
* @return string Formatted SELECT element
15621562
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
15631563
* @see \Cake\View\Helper\FormHelper::multiCheckbox() for creating multiple checkboxes.
15641564
*/
1565-
public function select($fieldName, array $options = [], array $attributes = []) {
1565+
public function select($fieldName, $options = [], array $attributes = []) {
15661566
$attributes += [
15671567
'disabled' => null,
15681568
'escape' => true,

tests/TestCase/View/Helper/FormHelperTest.php

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
namespace Cake\Test\TestCase\View\Helper;
1616

17+
use Cake\Collection\Collection;
1718
use Cake\Controller\Controller;
1819
use Cake\Core\App;
1920
use Cake\Core\Configure;
@@ -3060,6 +3061,9 @@ public function testRadio() {
30603061
);
30613062
$this->assertTags($result, $expected);
30623063

3064+
$result = $this->Form->radio('Model.field', new Collection(['option A']));
3065+
$this->assertTags($result, $expected);
3066+
30633067
$result = $this->Form->radio('Model.field', array('option A', 'option B'));
30643068
$expected = array(
30653069
'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
@@ -3192,6 +3196,9 @@ public function testSelect() {
31923196
);
31933197
$this->assertTags($result, $expected);
31943198

3199+
$result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
3200+
$this->assertTags($result, $expected);
3201+
31953202
$this->Form->request->data = array();
31963203
$result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
31973204
$expected = array(
@@ -3206,39 +3213,6 @@ public function testSelect() {
32063213
);
32073214
$this->assertTags($result, $expected);
32083215

3209-
$result = $this->Form->select(
3210-
'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
3211-
array('empty' => false)
3212-
);
3213-
$expected = array(
3214-
'select' => array('name' => 'Model[field]'),
3215-
array('option' => array('value' => 'first')),
3216-
'first &quot;html&quot; &lt;chars&gt;',
3217-
'/option',
3218-
array('option' => array('value' => 'second')),
3219-
'value',
3220-
'/option',
3221-
'/select'
3222-
);
3223-
$this->assertTags($result, $expected);
3224-
3225-
$result = $this->Form->select(
3226-
'Model.field',
3227-
array('first' => 'first "html" <chars>', 'second' => 'value'),
3228-
array('escape' => false, 'empty' => false)
3229-
);
3230-
$expected = array(
3231-
'select' => array('name' => 'Model[field]'),
3232-
array('option' => array('value' => 'first')),
3233-
'first "html" <chars>',
3234-
'/option',
3235-
array('option' => array('value' => 'second')),
3236-
'value',
3237-
'/option',
3238-
'/select'
3239-
);
3240-
$this->assertTags($result, $expected);
3241-
32423216
$options = array(
32433217
array('value' => 'first', 'text' => 'First'),
32443218
array('value' => 'first', 'text' => 'Another First'),
@@ -3288,6 +3262,46 @@ public function testSelect() {
32883262
$this->assertTags($result, $expected);
32893263
}
32903264

3265+
/**
3266+
* Test that select() escapes HTML.
3267+
*
3268+
* @return void
3269+
*/
3270+
public function testSelectEscapeHtml() {
3271+
$result = $this->Form->select(
3272+
'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
3273+
array('empty' => false)
3274+
);
3275+
$expected = array(
3276+
'select' => array('name' => 'Model[field]'),
3277+
array('option' => array('value' => 'first')),
3278+
'first &quot;html&quot; &lt;chars&gt;',
3279+
'/option',
3280+
array('option' => array('value' => 'second')),
3281+
'value',
3282+
'/option',
3283+
'/select'
3284+
);
3285+
$this->assertTags($result, $expected);
3286+
3287+
$result = $this->Form->select(
3288+
'Model.field',
3289+
array('first' => 'first "html" <chars>', 'second' => 'value'),
3290+
array('escape' => false, 'empty' => false)
3291+
);
3292+
$expected = array(
3293+
'select' => array('name' => 'Model[field]'),
3294+
array('option' => array('value' => 'first')),
3295+
'first "html" <chars>',
3296+
'/option',
3297+
array('option' => array('value' => 'second')),
3298+
'value',
3299+
'/option',
3300+
'/select'
3301+
);
3302+
$this->assertTags($result, $expected);
3303+
}
3304+
32913305
/**
32923306
* test select() with required and disabled attributes.
32933307
*

0 commit comments

Comments
 (0)