Skip to content

Commit

Permalink
Fix up text() and password() tests.
Browse files Browse the repository at this point in the history
Restore 'default' key feature.
  • Loading branch information
markstory committed Feb 14, 2014
1 parent a1a3122 commit 09c3dea
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
10 changes: 8 additions & 2 deletions src/View/Helper/FormHelper.php
Expand Up @@ -1539,7 +1539,7 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
* @throws Cake\Error\Exception When there are no params for the method call.
*/
public function __call($method, $params) {
$options = array();
$options = [];
if (empty($params)) {
throw new Error\Exception(sprintf('Missing field name for FormHelper::%s', $method));
}
Expand All @@ -1550,7 +1550,7 @@ public function __call($method, $params) {
$options['type'] = $method;
}
$options = $this->_initInputField($params[0], $options);
return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => null)));
return $this->widget($options['type'], $options);
}

/**
Expand Down Expand Up @@ -2861,13 +2861,19 @@ protected function _initInputField($field, $options = []) {
$first = array_shift($parts);
$options['name'] = $first . ($parts ? '[' . implode('][', $parts) . ']' : '');
}

if (isset($options['value']) && !isset($options['val'])) {
$options['val'] = $options['value'];
unset($options['value']);
}
if (!isset($options['val'])) {
$options['val'] = $context->val($field);
}
if (!isset($options['val']) && isset($options['default'])) {
$options['val'] = $options['default'];
}
unset($options['default']);

$options += (array)$context->attributes($field);

if ($context->hasError($field)) {
Expand Down
82 changes: 54 additions & 28 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -1280,12 +1280,14 @@ public function testFormSecurityFields() {
*/
public function testTextFieldGenerationForFloats() {
$this->markTestIncomplete('Need to revisit once models work again.');
$model->setSchema(array('foo' => array(
'type' => 'float',
'null' => false,
'default' => null,
'length' => 10
)));
$this->article['schema'] = [
'foo' => [
'type' => 'float',
'null' => false,
'default' => null,
'length' => 10
]
];

$this->Form->create('Contact');
$result = $this->Form->input('foo');
Expand Down Expand Up @@ -3299,10 +3301,9 @@ public function testInputCheckboxWithDisabledElements() {
* @return void
*/
public function testInputWithLeadingInteger() {
$this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->text('0.Node.title');
$expected = array(
'input' => array('name' => '0[Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
'input' => array('name' => '0[Node][title]', 'type' => 'text')
);
$this->assertTags($result, $expected);
}
Expand Down Expand Up @@ -3990,30 +3991,49 @@ public function testLabel() {
* @return void
*/
public function testTextbox() {
$this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->text('Model.field');
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'ModelField')));
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));

$result = $this->Form->text('Model.field', array('type' => 'password'));
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]', 'id' => 'ModelField')));
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));

$result = $this->Form->text('Model.field', array('id' => 'theID'));
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
}

/**
* Test that text() hooks up with request data and error fields.
*
* @return void
*/
public function testTextBoxDataAndError() {
$this->article['errors'] = [
'Contact' => ['text' => 'wrong']
];
$this->Form->create($this->article);

$this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
$result = $this->Form->text('Model.text');
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
$expected = [
'input' => [
'type' => 'text',
'name' => 'Model[text]',
'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
]
];
$this->assertTags($result, $expected);

$Contact->validationErrors['text'] = array(true);
$this->Form->request->data['Contact']['text'] = 'test';
$result = $this->Form->text('Contact.text', array('id' => 'theID'));
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Contact[text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));

$this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
$result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
$expected = array(
'input' => array('type' => 'text', 'name' => 'Model[0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
);
$result = $this->Form->text('Contact.text', ['id' => 'theID']);
$expected = [
'input' => [
'type' => 'text',
'name' => 'Contact[text]',
'value' => 'test',
'id' => 'theID',
'class' => 'form-error'
]
];
$this->assertTags($result, $expected);
}

Expand All @@ -4024,15 +4044,16 @@ public function testTextbox() {
*
* @return void
*/
public function testDefaultValue() {
$this->markTestIncomplete('Need to revisit once models work again.');
public function testTextDefaultValue() {
$this->Form->request->data['Model']['field'] = 'test';
$result = $this->Form->text('Model.field', array('default' => 'default value'));
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'test', 'id' => 'ModelField')));
$expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
$this->assertTags($result, $expected);

unset($this->Form->request->data['Model']['field']);
$result = $this->Form->text('Model.field', array('default' => 'default value'));
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'default value', 'id' => 'ModelField')));
$expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
$this->assertTags($result, $expected);
}

/**
Expand Down Expand Up @@ -4190,11 +4211,16 @@ public function testInputErrorEscape() {
* @return void
*/
public function testPassword() {
$this->markTestIncomplete('Need to revisit once models work again.');
$this->article['errors'] = [
'Contact' => [
'passwd' => 1
]
];
$this->Form->create($this->article);

$result = $this->Form->password('Contact.field');
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]', 'id' => 'ContactField')));
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));

$Contact->validationErrors['passwd'] = 1;
$this->Form->request->data['Contact']['passwd'] = 'test';
$result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
Expand Down

0 comments on commit 09c3dea

Please sign in to comment.