Permalink
Browse files

Cleaning up test code formatting, adding fix for POST hash validation…

… of checkbox lists, fixes #5903, test cases added.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7951 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
nateabele committed Dec 23, 2008
1 parent f7a62bc commit 61850b1be16db091f05464bab6f0d3a4e733548d
@@ -799,7 +799,7 @@ function input($fieldName, $options = array()) {
/**
* Creates a checkbox input widget.
*
- * @param string $fieldNamem Name of a field, like this "Modelname.fieldname"
+ * @param string $fieldName Name of a field, like this "Modelname.fieldname"
* @param array $options Array of HTML attributes.
* 'value' - the value of the checkbox
* 'checked' - boolean indicate that this checkbox is checked.
@@ -1159,12 +1159,9 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
}
if (isset($attributes) && array_key_exists('multiple', $attributes)) {
- if ($attributes['multiple'] === 'checkbox') {
- $tag = $this->Html->tags['checkboxmultiplestart'];
- $style = 'checkbox';
- } else {
- $tag = $this->Html->tags['selectmultiplestart'];
- }
+ $style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
+ $template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
+ $tag = $this->Html->tags[$template];
$select[] = $this->hidden(null, array('value' => '', 'id' => null, 'secure' => false));
} else {
$tag = $this->Html->tags['selectstart'];
@@ -1177,18 +1174,19 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
);
}
$emptyMulti = (
- $showEmpty !== null && $showEmpty !== false &&
- !(empty($showEmpty) && (isset($attributes) && array_key_exists('multiple', $attributes)))
+ $showEmpty !== null && $showEmpty !== false && !(
+ empty($showEmpty) && (isset($attributes) &&
+ array_key_exists('multiple', $attributes))
+ )
);
if ($emptyMulti) {
- if ($showEmpty === true) {
- $showEmpty = '';
- }
+ $showEmpty = ($showEmpty === true) ? '' : $showEmpty;
$options = array_reverse($options, true);
$options[''] = $showEmpty;
$options = array_reverse($options, true);
}
+
$select = array_merge($select, $this->__selectOptions(
array_reverse($options, true),
$selected,
@@ -1197,11 +1195,8 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
array('escape' => $escapeOptions, 'style' => $style)
));
- if ($style == 'checkbox') {
- $select[] = $this->Html->tags['checkboxmultipleend'];
- } else {
- $select[] = $this->Html->tags['selectend'];
- }
+ $template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
+ $select[] = $this->Html->tags[$template];
return $this->output(implode("\n", $select));
}
/**
@@ -62,7 +62,7 @@ class HtmlHelper extends AppHelper {
'selectend' => '</select>',
'optiongroup' => '<optgroup label="%s"%s>',
'optiongroupend' => '</optgroup>',
- 'checkboxmultiplestart' => '',
+ 'checkboxmultiplestart' => ' ',
'checkboxmultipleend' => '',
'password' => '<input type="password" name="%s" %s/>',
'file' => '<input type="file" name="%s" %s/>',
@@ -501,6 +501,23 @@ function testValidatePost() {
);
$this->assertTrue($this->Controller->Security->validatePost($this->Controller));
}
+/**
+ * Tests validation of checkbox arrays
+ *
+ * @access public
+ * @return void
+ */
+ function testValidatePostArray() {
+ $this->Controller->Security->startup($this->Controller);
+ $key = $this->Controller->params['_Token']['key'];
+ $fields = 'f7d573650a295b94e0938d32b323fde775e5f32b%3An%3A0%3A%7B%7D';
+
+ $this->Controller->data = array(
+ 'Model' => array('multi_field' => array('1', '3')),
+ '_Token' => compact('key', 'fields')
+ );
+ $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
+ }
/**
* testValidatePostNoModel method
*
@@ -2477,7 +2477,10 @@ function testSelect() {
);
$this->assertTags($result, $expected);
- $result = $this->Form->select('Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array(), false);
+ $result = $this->Form->select(
+ 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
+ null, array(), false
+ );
$expected = array(
'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
array('option' => array('value' => 'first')),
@@ -2490,7 +2493,11 @@ function testSelect() {
);
$this->assertTags($result, $expected);
- $result = $this->Form->select('Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'), null, array('escape' => false), false);
+ $result = $this->Form->select(
+ 'Model.field',
+ array('first' => 'first "html" <chars>', 'second' => 'value'),
+ null, array('escape' => false), false
+ );
$expected = array(
'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
array('option' => array('value' => 'first')),
@@ -2596,10 +2603,18 @@ function testNestedSelect() {
* @return void
*/
function testSelectMultiple() {
- $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), null, array('multiple' => true));
+ $options = array('first', 'second', 'third');
+ $result = $this->Form->select(
+ 'Model.multi_field', $options, null, array('multiple' => true)
+ );
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''),
- 'select' => array('name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField', 'multiple' => 'multiple'),
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''
+ ),
+ 'select' => array(
+ 'name' => 'data[Model][multi_field][]',
+ 'id' => 'ModelMultiField', 'multiple' => 'multiple'
+ ),
array('option' => array('value' => '0')),
'first',
'/option',
@@ -2613,10 +2628,17 @@ function testSelectMultiple() {
);
$this->assertTags($result, $expected);
- $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), null, array('multiple' => 'multiple'));
+ $result = $this->Form->select(
+ 'Model.multi_field', $options, null, array('multiple' => 'multiple')
+ );
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''),
- 'select' => array('name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField', 'multiple' => 'multiple'),
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''
+ ),
+ 'select' => array(
+ 'name' => 'data[Model][multi_field][]',
+ 'id' => 'ModelMultiField', 'multiple' => 'multiple'
+ ),
array('option' => array('value' => '0')),
'first',
'/option',
@@ -2630,10 +2652,17 @@ function testSelectMultiple() {
);
$this->assertTags($result, $expected);
- $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array(0, 1), array('multiple' => true));
+ $result = $this->Form->select(
+ 'Model.multi_field', $options, array(0, 1), array('multiple' => true)
+ );
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''),
- 'select' => array('name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField', 'multiple' => 'multiple'),
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''
+ ),
+ 'select' => array(
+ 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
+ 'multiple' => 'multiple'
+ ),
array('option' => array('value' => '0', 'selected' => 'selected')),
'first',
'/option',
@@ -2648,8 +2677,6 @@ function testSelectMultiple() {
$this->assertTags($result, $expected);
}
/**
- * testSelectMultipleCheckboxes method
- *
* test generation of multi select elements in checkbox format
*
* @access public
@@ -2702,7 +2729,9 @@ function testSelectMultipleCheckboxes() {
array('multiple' => 'checkbox')
);
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''),
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''
+ ),
array('div' => array('class' => 'checkbox')),
array('input' => array(
'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
@@ -2733,18 +2762,45 @@ function testSelectMultipleCheckboxes() {
);
$this->assertTags($result, $expected);
- $result = $this->Form->select('Model.multi_field', array('1' => 'first'), null, array('multiple' => 'checkbox'));
+ $result = $this->Form->select(
+ 'Model.multi_field', array('1' => 'first'), null, array('multiple' => 'checkbox')
+ );
$expected = array(
- 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''),
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => ''
+ ),
array('div' => array('class' => 'checkbox')),
- array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
+ array('input' => array(
+ 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
+ 'value' => '1', 'id' => 'ModelMultiField1'
+ )),
array('label' => array('for' => 'ModelMultiField1')),
'first',
'/label',
'/div'
);
$this->assertTags($result, $expected);
}
+/**
+ * Checks the security hash array generated for multiple-input checkbox elements
+ *
+ * @access public
+ * @return void
+ */
+ function testSelectMultipleCheckboxSecurity() {
+ $this->Form->params['_Token']['key'] = 'testKey';
+ $this->assertEqual($this->Form->fields, array());
+
+ $result = $this->Form->select(
+ 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
+ null, array('multiple' => 'checkbox')
+ );
+ $this->assertEqual($this->Form->fields, array('Model.multi_field'));
+
+ $result = $this->Form->secure($this->Form->fields);
+ $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3An%3A0%3A%7B%7D';
+ $this->assertPattern('/"' . $key . '"/', $result);
+ }
/**
* testInputMultipleCheckboxes method
*

0 comments on commit 61850b1

Please sign in to comment.