Skip to content

Commit

Permalink
Unskip a secured form test.
Browse files Browse the repository at this point in the history
Fix issues with step and hidden fields as there were some problems.
  • Loading branch information
markstory committed Feb 28, 2014
1 parent a8e92b9 commit 1a9b989
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/View/Helper/FormHelper.php
Expand Up @@ -1012,8 +1012,8 @@ protected function _magicOptions($fieldName, $options, $allowOverride) {
$fieldDef = $context->attributes($fieldName);

if ($options['type'] === 'number' && !isset($options['step'])) {
if ($type === 'decimal') {
$decimalPlaces = substr($fieldDef['length'], strpos($fieldDef['length'], ',') + 1);
if ($type === 'decimal' && isset($fieldDef['precision'])) {
$decimalPlaces = $fieldDef['precision'];
$options['step'] = sprintf('%.' . $decimalPlaces . 'F', pow(10, -1 * $decimalPlaces));
} elseif ($type === 'float') {
$options['step'] = 'any';
Expand Down Expand Up @@ -1271,7 +1271,7 @@ public function hidden($fieldName, $options = array()) {
));

if ($secure === true) {
$this->_secure(true, $this->_secureFieldName($options), $options['val']);
$this->_secure(true, $this->_secureFieldName($options), (string)$options['val']);
}

$options['type'] = 'hidden';
Expand Down
83 changes: 48 additions & 35 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -1610,24 +1610,29 @@ public function testFormSecureWithCustomNameAttribute() {
* @return void
*/
public function testFormSecuredInput() {
$this->markTestIncomplete('Need to revisit once models work again.');
$this->Form->request->params['_csrfToken'] = 'testKey';
$this->Form->request->params['_Token'] = 'stuff';
$this->article['schema'] = [
'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
];

$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
$result = $this->Form->create($this->article, array('url' => '/articles/add'));
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
'form' => array('method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding),
'div' => array('style' => 'display:none;'),
array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
array('input' => array(
'type' => 'hidden', 'name' => '_csrfToken',
'value' => 'testKey', 'id' => 'preg:/Token\d+/'
'type' => 'hidden',
'name' => '_csrfToken',
'value' => 'testKey'
)),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('ValidateUser.ratio');
$result = $this->Form->input('ratio');
$expected = array(
'div' => array('class'),
'label' => array('for'),
Expand All @@ -1638,7 +1643,7 @@ public function testFormSecuredInput() {
);
$this->assertTags($result, $expected);

$result = $this->Form->input('ValidateUser.population');
$result = $this->Form->input('population');
$expected = array(
'div' => array('class'),
'label' => array('for'),
Expand All @@ -1649,61 +1654,67 @@ public function testFormSecuredInput() {
);
$this->assertTags($result, $expected);

$result = $this->Form->input('UserForm.published', array('type' => 'text'));
$result = $this->Form->input('published', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'UserFormPublished'),
'label' => array('for' => 'published'),
'Published',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'UserForm[published]',
'id' => 'UserFormPublished'
'type' => 'text',
'name' => 'published',
'id' => 'published'
)),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('UserForm.other', array('type' => 'text'));
$result = $this->Form->input('other', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'UserFormOther'),
'label' => array('for' => 'other'),
'Other',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'UserForm[other]',
'id' => 'UserFormOther'
'type' => 'text',
'name' => 'other',
'id',
)),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->hidden('UserForm.stuff');
$result = $this->Form->hidden('stuff');
$expected = array(
'input' => array(
'type' => 'hidden', 'name' => 'UserForm[stuff]',
'id' => 'UserFormStuff'
'type' => 'hidden',
'name' => 'stuff',
));
$this->assertTags($result, $expected);

$result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
$result = $this->Form->hidden('hidden', array('value' => '0'));
$expected = array('input' => array(
'type' => 'hidden', 'name' => 'UserForm[hidden]',
'value' => '0', 'id' => 'UserFormHidden'
'type' => 'hidden',
'name' => 'hidden',
'value' => '0'
));
$this->assertTags($result, $expected);

$result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
$result = $this->Form->input('something', array('type' => 'checkbox'));
$expected = array(
'div' => array('class' => 'input checkbox'),
array('input' => array(
'type' => 'hidden', 'name' => 'UserForm[something]',
'value' => '0', 'id' => 'UserFormSomething_'
'type' => 'hidden',
'name' => 'something',
'value' => '0'
)),
array('input' => array(
'type' => 'checkbox', 'name' => 'UserForm[something]',
'value' => '1', 'id' => 'UserFormSomething'
'type' => 'checkbox',
'name' => 'something',
'value' => '1',
'id' => 'something'
)),
'label' => array('for' => 'UserFormSomething'),
'label' => array('for' => 'something'),
'Something',
'/label',
'/div'
Expand All @@ -1712,23 +1723,25 @@ public function testFormSecuredInput() {

$result = $this->Form->fields;
$expected = array(
'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
'UserForm.hidden' => '0', 'UserForm.something'
'ratio', 'population', 'published', 'other',
'stuff' => '',
'hidden' => '0',
'something'
);
$this->assertEquals($expected, $result);

$hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';

$result = $this->Form->secure($this->Form->fields);
$expected = array(
'div' => array('style' => 'display:none;'),
array('input' => array(
'type' => 'hidden', 'name' => '_Token[fields]',
'value' => $hash
'type' => 'hidden',
'name' => '_Token[fields]',
'value'
)),
array('input' => array(
'type' => 'hidden', 'name' => '_Token[unlocked]',
'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
'type' => 'hidden',
'name' => '_Token[unlocked]',
'value' => ''
)),
'/div'
);
Expand Down

0 comments on commit 1a9b989

Please sign in to comment.