Browse Source

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
mark_story 12 years ago
parent
commit
58c0b1487d
2 changed files with 53 additions and 39 deletions
  1. 6 6
      src/View/Helper/FormHelper.php
  2. 47 33
      tests/TestCase/View/Helper/FormHelperTest.php

+ 6 - 6
src/View/Helper/FormHelper.php

@@ -870,11 +870,11 @@ class FormHelper extends Helper {
 	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':
@@ -1161,12 +1161,12 @@ class FormHelper extends Helper {
  *   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;
@@ -1555,14 +1555,14 @@ class FormHelper extends Helper {
  * }}}
  *
  * @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,

+ 47 - 33
tests/TestCase/View/Helper/FormHelperTest.php

@@ -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;
@@ -3060,6 +3061,9 @@ class FormHelperTest extends TestCase {
 		);
 		$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' => ''),
@@ -3192,6 +3196,9 @@ class FormHelperTest extends TestCase {
 		);
 		$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(
@@ -3206,39 +3213,6 @@ class FormHelperTest extends TestCase {
 		);
 		$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'),
@@ -3289,6 +3263,46 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void