Skip to content

Commit

Permalink
Updating more tests on FormHelperTest case
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 14, 2011
1 parent ba012ed commit 4ed46cf
Showing 1 changed file with 65 additions and 58 deletions.
123 changes: 65 additions & 58 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -2700,10 +2700,11 @@ public function testTextbox() {
$result = $this->Form->text('Model.text');
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test <strong>HTML</strong> values', 'id' => 'ModelText')));

$this->Form->validationErrors['Model']['text'] = 1;
$this->Form->request->data['Model']['text'] = 'test';
$result = $this->Form->text('Model.text', array('id' => 'theID'));
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
$Contact = ClassRegistry::getObject('Contact');
$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' => 'data[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'));
Expand Down Expand Up @@ -2766,44 +2767,45 @@ public function testCheckboxDefaultValue() {
* @return void
*/
public function testError() {
$this->Form->validationErrors['Model']['field'] = array(1);
$result = $this->Form->error('Model.field');
$Contact = ClassRegistry::getObject('Contact');
$Contact->validationErrors['field'] = array(1);
$result = $this->Form->error('Contact.field');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));

$result = $this->Form->error('Model.field', null, array('wrap' => false));
$result = $this->Form->error('Contact.field', null, array('wrap' => false));
$this->assertEqual($result, 'Error in field Field');

$this->Form->validationErrors['Model']['field'] = array("This field contains invalid input");
$result = $this->Form->error('Model.field', null, array('wrap' => false));
$Contact->validationErrors['field'] = array("This field contains invalid input");
$result = $this->Form->error('Contact.field', null, array('wrap' => false));
$this->assertEqual($result, 'This field contains invalid input');

$this->Form->validationErrors['Model']['field'] = array("This field contains invalid input");
$result = $this->Form->error('Model.field', null, array('wrap' => 'span'));
$Contact->validationErrors['field'] = array("This field contains invalid input");
$result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
$this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));

$result = $this->Form->error('Model.field', 'There is an error fool!', array('wrap' => 'span'));
$result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
$this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));

$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false));
$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
$this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');

$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
$this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');

$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
$this->assertEqual($result, '<strong>Badness!</strong>');

$this->Form->validationErrors['Model']['field'] = array("email");
$result = $this->Form->error('Model.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
$Contact->validationErrors['field'] = array("email");
$result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
$expected = array(
'div' => array('class' => 'field-error'),
'No good!',
'/div'
);
$this->assertTags($result, $expected);

$this->Form->validationErrors['Model']['field'] = array('notEmpty', 'email', 'Something else');
$result = $this->Form->error('Model.field', array(
$Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
$result = $this->Form->error('Contact.field', array(
'notEmpty' => 'Cannot be empty',
'email' => 'No good!'
));
Expand All @@ -2819,9 +2821,9 @@ public function testError() {
$this->assertTags($result, $expected);

/** Testing error messages list options **/
$this->Form->validationErrors['Model']['field'] = array('notEmpty', 'email');
$Contact->validationErrors['field'] = array('notEmpty', 'email');

$result = $this->Form->error('Model.field', null, array('listOptions' => 'ol'));
$result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
$expected = array(
'div' => array('class' => 'error-message'),
'ol' => array(),
Expand All @@ -2832,7 +2834,7 @@ public function testError() {
);
$this->assertTags($result, $expected);

$result = $this->Form->error('Model.field', null, array('listOptions' => array('tag' => 'ol')));
$result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
$expected = array(
'div' => array('class' => 'error-message'),
'ol' => array(),
Expand All @@ -2843,7 +2845,7 @@ public function testError() {
);
$this->assertTags($result, $expected);

$result = $this->Form->error('Model.field', null, array(
$result = $this->Form->error('Contact.field', null, array(
'listOptions' => array(
'class' => 'ul-class',
'itemOptions' => array(
Expand All @@ -2869,7 +2871,8 @@ public function testError() {
*/
public function testInputErrorEscape() {
$this->Form->create('ValidateProfile');
$this->Form->validationErrors['ValidateProfile']['city'] = array('required<br>');
$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
$ValidateProfile->validationErrors['city'] = array('required<br>');
$result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
$this->assertPattern('/required&lt;br&gt;/', $result);

Expand All @@ -2886,13 +2889,14 @@ public function testInputErrorEscape() {
* @return void
*/
public function testPassword() {
$result = $this->Form->password('Model.field');
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
$Contact = ClassRegistry::getObject('Contact');
$result = $this->Form->password('Contact.field');
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));

$this->Form->validationErrors['Model']['passwd'] = 1;
$this->Form->request->data['Model']['passwd'] = 'test';
$result = $this->Form->password('Model.passwd', array('id' => 'theID'));
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
$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' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
}

/**
Expand Down Expand Up @@ -3809,36 +3813,37 @@ public function testSelectMultipleCheckboxDiv() {
));
$this->assertTags($result, $expected);

$this->Form->validationErrors['Model']['tags'] = 'Select atleast one option';
$result = $this->Form->input('Model.tags', array(
$Contact = ClassRegistry::getObject('Contact');
$Contact->validationErrors['tags'] = 'Select atleast one option';
$result = $this->Form->input('Contact.tags', array(
'options' => array('one'),
'multiple' => 'checkbox',
'label' => false,
'div' => false
));
$expected = array(
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'),
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
array('div' => array('class' => 'checkbox form-error')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][tags][]', 'value' => '0', 'id' => 'ModelTags0')),
array('label' => array('for' => 'ModelTags0')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
array('label' => array('for' => 'ContactTags0')),
'one',
'/label',
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Model.tags', array(
$result = $this->Form->input('Contact.tags', array(
'options' => array('one'),
'multiple' => 'checkbox',
'class' => 'mycheckbox',
'label' => false,
'div' => false
));
$expected = array(
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'),
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
array('div' => array('class' => 'mycheckbox form-error')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][tags][]', 'value' => '0', 'id' => 'ModelTags0')),
array('label' => array('for' => 'ModelTags0')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
array('label' => array('for' => 'ContactTags0')),
'one',
'/label',
'/div'
Expand Down Expand Up @@ -4128,35 +4133,36 @@ public function testCheckbox() {
);
$this->assertTags($result, $expected);

$this->Form->validationErrors['Model']['field'] = 1;
$this->Form->request->data['Model']['field'] = 'myvalue';
$result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
$Contact = ClassRegistry::getObject('Contact');
$Contact->validationErrors['field'] = 1;
$this->Form->request->data['Contact']['field'] = 'myvalue';
$result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
$expected = array(
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
);
$this->assertTags($result, $expected);

$result = $this->Form->checkbox('Model.field', array('value' => 'myvalue'));
$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
$expected = array(
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ModelField', 'checked' => 'checked', 'class' => 'form-error'))
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
);
$this->assertTags($result, $expected);

$this->Form->request->data['Model']['field'] = '';
$result = $this->Form->checkbox('Model.field', array('id' => 'theID'));
$this->Form->request->data['Contact']['field'] = '';
$result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
$expected = array(
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
);
$this->assertTags($result, $expected);

unset($this->Form->validationErrors['Model']['field']);
$result = $this->Form->checkbox('Model.field', array('value' => 'myvalue'));
$Contact->validationErrors = array();
$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'ModelField'))
'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
);
$this->assertTags($result, $expected);

Expand Down Expand Up @@ -5532,11 +5538,12 @@ public function testTextAreaWithStupidCharacters() {
* @return void
*/
public function testHiddenField() {
$this->Form->validationErrors['Model']['field'] = 1;
$this->Form->request->data['Model']['field'] = 'test';
$result = $this->Form->hidden('Model.field', array('id' => 'theID'));
$Contact = ClassRegistry::getObject('Contact');
$Contact->validationErrors['field'] = 1;
$this->Form->request->data['Contact']['field'] = 'test';
$result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
$this->assertTags($result, array(
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][field]', 'id' => 'theID', 'value' => 'test'))
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
);
}

Expand Down

0 comments on commit 4ed46cf

Please sign in to comment.