* Copyright 2006-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The Open Group Test Suite License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2006-2008, Cake Software Foundation, Inc. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers * @since CakePHP(tm) v 1.2.0.4206 * @version $Revision$ * @modifiedby $LastChangedBy$ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { define('CAKEPHP_UNIT_TEST_EXECUTION', 1); } uses('view'.DS.'helpers'.DS.'app_helper', 'class_registry', 'controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'html', 'view'.DS.'view', 'view'.DS.'helpers'.DS.'form'); class ContactTestController extends Controller { var $name = 'ContactTest'; var $uses = null; } class Contact extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'Contact'; var $validate = array('non_existing' => array(), 'idontexist' => array(), 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } var $hasAndBelongsToMany = array('ContactTag' => array()); } class ContactTag extends Model { var $useTable = false; function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''), 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null) ); return $this->_schema; } } class UserForm extends CakeTestModel { var $useTable = false; var $primaryKey = 'id'; var $name = 'UserForm'; var $hasMany = array('OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null), 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null), 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255), 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } } class OpenidUrl extends CakeTestModel { var $useTable = false; var $primaryKey = 'id'; var $name = 'OpenidUrl'; var $belongsTo = array('UserForm' => array('className' => 'UserForm', 'foreignKey' => 'user_form_id')); var $validate = array('openid_not_registered' => array()); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'user_form_id' => array('type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'), 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), ); return $this->_schema; } function beforeValidate() { $this->invalidate('openid_not_registered'); return true; } } class ValidateUser extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateUser'; var $hasOne = array('ValidateProfile' => array('className' => 'ValidateProfile', 'foreignKey' => 'user_id')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } function beforeValidate() { $this->invalidate('email'); return false; } } class ValidateProfile extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateProfile'; var $hasOne = array('ValidateItem' => array('className' => 'ValidateItem', 'foreignKey' => 'profile_id')); var $belongsTo = array('ValidateUser' => array('className' => 'ValidateUser', 'foreignKey' => 'user_id')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } function beforeValidate() { $this->invalidate('full_name'); $this->invalidate('city'); return false; } } class ValidateItem extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateItem'; var $belongsTo = array('ValidateProfile' => array('className' => 'ValidateProfile', 'foreignKey' => 'profile_id')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'description' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } function beforeValidate() { $this->invalidate('description'); return false; } } class TestMail extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'TestMail'; } /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class FormHelperTest extends CakeTestCase { var $fixtures = array(null); function setUp() { parent::setUp(); Router::reload(); $this->Form =& new FormHelper(); $this->Form->Html =& new HtmlHelper(); $this->Controller =& new ContactTestController(); $this->View =& new View($this->Controller); ClassRegistry::addObject('view', $view); ClassRegistry::addObject('Contact', new Contact()); ClassRegistry::addObject('OpenidUrl', new OpenidUrl()); ClassRegistry::addObject('UserForm', new UserForm()); ClassRegistry::addObject('ValidateItem', new ValidateItem()); ClassRegistry::addObject('ValidateUser', new ValidateUser()); ClassRegistry::addObject('ValidateProfile', new ValidateProfile()); } function testFormCreateWithSecurity() { $this->Form->params['_Token'] = array('key' => 'testKey'); $result = $this->Form->create('Contact', array('url' => '/contacts/add')); $this->assertPattern('/^]*>.+$/', $result); $this->assertPattern('/^]+method="post"[^<>]*>.+$/', $result); $this->assertPattern('/^]+action="[^"]+"[^<>]*>.+$/', $result); $this->assertNoPattern('/^]+[^id|method|action]=[^<>]*>/', $result); $this->assertPattern('/]+type="hidden"[^<>]*\/>/', $result); $this->assertPattern('/]+name="data\[__Token\]\[key\]"[^<>]*\/>/', $result); $this->assertPattern('/]+value="testKey"[^<>]*\/>/', $result); $this->assertPattern('/]+id="\w+"[^<>]*\/>/', $result); $this->assertNoPattern('/]+[^type|name|value|id]=[^<>]*>/', $result); $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm')); $this->assertPattern('/^]+id="MyForm"[^<>]*>.+$/', $result); $this->assertPattern('/^]+method="post"[^<>]*>.+$/', $result); $this->assertPattern('/^]+action="[^"]+"[^<>]*>.+$/', $result); $this->assertNoPattern('/^]+[^id|method|action]=[^<>]*>/', $result); $this->assertPattern('/]+type="hidden"[^<>]*\/>/', $result); $this->assertPattern('/]+name="data\[__Token\]\[key\]"[^<>]*\/>/', $result); $this->assertPattern('/]+value="testKey"[^<>]*\/>/', $result); $this->assertPattern('/]+id="\w+"[^<>]*\/>/', $result); $this->assertNoPattern('/]+[^type|name|value|id]=[^<>]*>/', $result); } function testFormSecurityFields() { $key = 'testKey'; $fields = array( 'Model' => array('password', 'username', 'valid'), '_Model' => array('valid' => '0'), '__Token' => array('key' => $key) ); $this->Form->params['_Token']['key'] = $key; $result = $this->Form->secure($fields); $expected = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $this->assertPattern('/'.$expected.'/', $result); $this->assertPattern('/input type="hidden" name="data\[__Token\]\[fields\]" value="'.$expected.'"/', $result); } function testFormSecuredInput() { $fields = array( 'UserForm' => array('0' => 'published', '1' => 'other', '2' => 'something'), '_UserForm' => array('stuff' => '', 'something' => '0'), '__Token' => array('key' => 'testKey' )); $fields = $this->__sortFields($fields); $fieldsKey = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $fields['__Token']['fields'] = $fieldsKey; $this->Form->params['_Token']['key'] = 'testKey'; $result = $this->Form->create('Contact', array('url' => '/contacts/add')); $expected = '/^
$/'; $this->assertPattern($expected, $result); $result = $this->Form->input('UserForm.published', array('type' => 'text')); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->input('UserForm.other', array('type' => 'text')); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->hidden('UserForm.stuff', array('type' => 'text')); $expected = ''; $this->assertEqual($result, $expected); $result = $this->Form->input('UserForm.something', array('type' => 'checkbox')); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->secure($this->Form->fields); $expected = '/
<\/fieldset>$/'; $this->assertPattern($expected, $result); $result = $this->Form->fields; $result = $this->__sortFields($result); $this->assertEqual($result, $fields); } function testFormValidationAssociated() { $this->UserForm =& ClassRegistry::getObject('UserForm'); $this->UserForm->OpenidUrl =& ClassRegistry::getObject('OpenidUrl'); $data = array('UserForm' => array('name' => 'user'), 'OpenidUrl' => array('url' => 'http://www.cakephp.org')); $this->assertTrue($this->UserForm->OpenidUrl->create($data)); $this->assertFalse($this->UserForm->OpenidUrl->validates()); $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login')); $this->assertPattern('/^]+>]+>]+\/><\/fieldset>$/', $result); $this->assertPattern('/^]+id="UserFormLoginForm"[^<>]*>/', $result); $this->assertPattern('/^]+method="post"[^<>]*>/', $result); $this->assertPattern('/^]+action="\/user_forms\/login\/"[^<>]*>/', $result); $this->assertNoPattern('/]+[^id|method|action]=[^<>\/]*>/', $result); $this->assertPattern('/]+type="hidden"[^<>]*\/>/', $result); $this->assertPattern('/]+name="_method"[^<>]*\/>/', $result); $this->assertPattern('/]+value="POST"[^<>]*\/>/', $result); $this->assertNoPattern('/]+[^type|name|value]=[^<>\/]*\/>/', $result); $expected = array('OpenidUrl' => array('openid_not_registered' => 1)); $this->assertEqual($this->Form->validationErrors, $expected); $result = $this->Form->error('OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)); $this->assertEqual($result, 'Error, not registered'); unset($this->UserForm->OpenidUrl); unset($this->UserForm); } function testFormValidationAssociatedFirstLevel() { $this->ValidateUser =& ClassRegistry::getObject('ValidateUser'); $this->ValidateUser->ValidateProfile =& ClassRegistry::getObject('ValidateProfile'); $data = array('ValidateUser' => array('name' => 'mariano'), 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')); $this->assertTrue($this->ValidateUser->create($data)); $this->assertFalse($this->ValidateUser->validates()); $this->assertFalse($this->ValidateUser->ValidateProfile->validates()); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); $this->assertPattern('/^]*>]+>]+\/><\/fieldset>$/', $result); $expected = array( 'ValidateUser' => array('email' => 1), 'ValidateProfile' => array('full_name' => 1, 'city' => 1) ); $this->assertEqual($this->Form->validationErrors, $expected); unset($this->ValidateUser->ValidateProfile); unset($this->ValidateUser); } function testFormValidationAssociatedSecondLevel() { $this->ValidateUser =& ClassRegistry::getObject('ValidateUser'); $this->ValidateUser->ValidateProfile =& ClassRegistry::getObject('ValidateProfile'); $this->ValidateUser->ValidateProfile->ValidateItem =& ClassRegistry::getObject('ValidateItem'); $data = array( 'ValidateUser' => array('name' => 'mariano'), 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'), 'ValidateItem' => array('name' => 'Item') ); $this->assertTrue($this->ValidateUser->create($data)); $this->assertFalse($this->ValidateUser->validates()); $this->assertFalse($this->ValidateUser->ValidateProfile->validates()); $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates()); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); $this->assertPattern('/^]*>]+>]+\/><\/fieldset>$/', $result); $expected = array( 'ValidateUser' => array('email' => 1), 'ValidateProfile' => array('full_name' => 1, 'city' => 1), 'ValidateItem' => array('description' => 1) ); $this->assertEqual($this->Form->validationErrors, $expected); unset($this->ValidateUser->ValidateProfile->ValidateItem); unset($this->ValidateUser->ValidateProfile); unset($this->ValidateUser); } function testFormValidationMultiRecord() { $this->Form->validationErrors['Contact'] = array(2 => array('name' => 'This field cannot be left blank')); $result = $this->Form->input('Contact.2.name'); $this->assertPattern('/]*class="error-message"[^<>]*>This field cannot be left blank<\/div>/', $result); $this->Form->validationErrors['UserForm'] = array('OpenidUrl' => array('url' => 'You must provide a URL')); $this->Form->create('UserForm'); $result = $this->Form->input('OpenidUrl.url'); $this->assertPattern('/]*class="error-message"[^<>]*>You must provide a URL<\/div>/', $result); } function testFormInput() { $result = $this->Form->input('Contact.email', array('id' => 'custom')); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->hidden('Contact/idontexist'); $expected = ''; $this->assertEqual($result, $expected); $result = $this->Form->input('Contact.email', array('type' => 'text')); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->input('Contact.5.email', array('type' => 'text')); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->input('Contact/password'); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->input('email', array('options' => array('First', 'Second'), 'empty' => true)); $this->assertPattern('/assertPattern('/option value="23"/', $result[0]); $this->assertNoPattern('/option value="24"/', $result[0]); $result = $this->Form->input('Model.field', array('type' => 'datetime', 'timeFormat' => 12)); $result = explode('assertPattern('/option value="12"/', $result[0]); $this->assertNoPattern('/option value="13"/', $result[0]); $this->Form->data = array('Contact' => array('phone' => 'Hello & World > weird chars' )); $result = $this->Form->input('Contact.phone'); $expected = '
'; $this->assertEqual($result, $expected); unset($this->Form->data); $this->Form->validationErrors['Model']['field'] = 'Badness!'; $result = $this->Form->input('Model.field'); $expected = '
'; $this->assertPattern('/^]+class="input"[^<>]*>]+for="ModelField"[^<>]*>Field<\/label>]+class="[^"]*form-error"[^<>]+\/>]+class="error-message">Badness!<\/div><\/div>$/', $result); $result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy')); $this->assertPattern('/^]+class="input"[^<>]*>]+for="ModelField"[^<>]*>Field<\/label>]+class="[^"]*form-error"[^<>]+\/>A message to you, Rudy]+class="error-message">Badness!<\/div><\/div>$/', $result); $this->Form->setEntity(null); $this->Form->setEntity('Model.field'); $result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy', 'error' => false)); $this->assertPattern('/^]+class="input"[^<>]*>]+for="ModelField"[^<>]*>Field<\/label>]+class="[^"]*form-error"[^<>]+\/>A message to you, Rudy<\/div>$/', $result); unset($this->Form->validationErrors['Model']['field']); $result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy')); $this->assertPattern('/^]+class="input"[^<>]*>]+for="ModelField"[^<>]*>Field<\/label>]+\/>A message to you, Rudy<\/div>$/', $result); $this->assertNoPattern('/form-error/', $result); $this->assertNoPattern('/error-message/', $result); $this->Form->data = array('Model' => array('user_id' => 'value')); $view =& ClassRegistry::getObject('view'); $view->viewVars['users'] = array('value' => 'good', 'other' => 'bad'); $result = $this->Form->input('Model.user_id', array('empty' => true)); $this->assertPattern('/^]+class="input"[^<>]*>]+for="ModelUserId"[^<>]*>User<\/label>/', $result); $this->assertPattern('/]+ \/>\s*(]+>\]+>\s*]*>\w+<\/label><\/div>\s*){3}$/', $result); $this->assertNoPattern('/]*>[^(first|second)]<\/label>/', $result); /*$this->assertPattern('/^]+name="data\[Model\]\[multi_field\]\[\]+value="0"[^<>]+checked="checked">/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]+value="1"[^<>]+checked="checked">/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]+value="2">third/', $result); $this->assertNoPattern('/]+name="data\[Model\]\[multi_field\]\[\]+value="[^012]"[^<>\/]*>$/', $result);*/ $this->assertNoPattern('/]*>/', $result); $this->assertNoPattern('/<\/select>/', $result); } function testLabel() { $this->Form->text('Person/name'); $result = $this->Form->label(); $this->assertEqual($result, ''); $this->Form->text('Person.name'); $result = $this->Form->label(); $this->assertEqual($result, ''); $this->Form->text('Person.Name'); $result = $this->Form->label(); $this->assertEqual($result, ''); $result = $this->Form->label('Person.first_name'); $this->assertEqual($result, ''); $result = $this->Form->label('Person.first_name', 'Your first name'); $this->assertEqual($result, ''); $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class')); $this->assertPattern('/^]+>Your first name<\/label>$/', $result); $this->assertPattern('/^]+for="PersonFirstName"[^<>]*>/', $result); $this->assertPattern('/^]+class="my-class"[^<>]*>/', $result); $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID')); $this->assertEqual($result, ''); $result = $this->Form->label('Person.first_name', ''); $this->assertEqual($result, ''); $result = $this->Form->label('Person.2.name', ''); $this->assertEqual($result, ''); } function testTextbox() { $result = $this->Form->text('Model.field'); $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="text"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ModelField"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value]=[^<>]*>/', $result); $result = $this->Form->text('Model.field', array('type' => 'password')); $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="password"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ModelField"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value]=[^<>]*>/', $result); $result = $this->Form->text('Model.field', array('id' => 'theID')); $expected = ''; $this->assertEqual($result, $expected); $this->Form->data['Model']['text'] = 'test HTML values'; $result = $this->Form->text('Model/text'); $this->assertPattern('/^]+type="text"[^<>]+\/>$/', $result); $this->assertPattern('/^]+name="data\[Model\]\[text\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value="test <strong>HTML<\/strong> values"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value|class]=[^<>]*>/', $result); $this->Form->validationErrors['Model']['text'] = 1; $this->Form->data['Model']['text'] = 'test'; $result = $this->Form->text('Model/text', array('id' => 'theID')); $this->assertPattern('/^]+name="data\[Model\]\[text\]"[^<>]+id="theID"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value="test"[^<>]+class="form-error"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value|class]=[^<>]*>/', $result); } function testDefaultValue() { $this->Form->data['Model']['field'] = 'test'; $result = $this->Form->text('Model.field', array('default' => 'default value')); $this->assertPattern('/^]+value="test"[^<>]+\/>$/', $result); unset($this->Form->data['Model']['field']); $result = $this->Form->text('Model.field', array('default' => 'default value')); $this->assertPattern('/^]+value="default value"[^<>]+\/>$/', $result); } function testFieldError() { $this->Form->validationErrors['Model']['field'] = 1; $result = $this->Form->error('Model.field'); $this->assertEqual($result, '
Error in field Field
'); $result = $this->Form->error('Model.field', null, array('wrap' => false)); $this->assertEqual($result, 'Error in field Field'); $this->Form->validationErrors['Model']['field'] = "This field contains invalid input"; $result = $this->Form->error('Model.field', null, array('wrap' => false)); $this->assertEqual($result, 'This field contains invalid input'); $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false)); $this->assertEqual($result, '<strong>Badness!</strong>'); $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false, 'escape' => true)); $this->assertEqual($result, '<strong>Badness!</strong>'); $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false, 'escape' => false)); $this->assertEqual($result, 'Badness!'); } function testPassword() { $result = $this->Form->password('Model.field'); $expected = ''; $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="password"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ModelField"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value]=[^<>]*>/', $result); $this->Form->validationErrors['Model']['passwd'] = 1; $this->Form->data['Model']['passwd'] = 'test'; $result = $this->Form->password('Model/passwd', array('id' => 'theID')); $this->assertPattern('/^]+name="data\[Model\]\[passwd\]"[^<>]+id="theID"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value="test"[^<>]+class="form-error"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value|class]=[^<>]*>/', $result); } function testRadio() { $result = $this->Form->radio('Model.field', array('option A')); $this->assertPattern('/id="ModelField0"/', $result); $this->assertNoPattern('/^
Field<\/legend>$/', $result); $this->assertPattern('/(]+name="data\[Model\]\[field\]"[^<>]+>.+){1}/', $result); $result = $this->Form->radio('Model.field', array('option A', 'option B')); $this->assertPattern('/id="ModelField0"/', $result); $this->assertPattern('/id="ModelField1"/', $result); $this->assertNoPattern('/checked="checked"/', $result); $this->assertPattern('/^
Field<\/legend>]+>(]+>]+>option [AB]<\/label>)+<\/fieldset>$/', $result); $this->assertPattern('/(]+name="data\[Model\]\[field\]"[^<>]+>.+){2}/', $result); $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '
')); $this->assertPattern('/id="ModelField0"/', $result); $this->assertPattern('/id="ModelField1"/', $result); $this->assertNoPattern('/checked="checked"/', $result); $this->assertPattern('/^
Field<\/legend>]+>]+>]+>option A<\/label>+]>]+>]+>option B<\/label><\/fieldset>$/', $result); $this->assertPattern('/(]+name="data\[Model\]\[field\]"[^<>]+>.+){2}/', $result); $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1')); $this->assertPattern('/id="ModelField1".*checked="checked"/', $result); $this->assertPattern('/id="ModelField0"/', $result); $this->assertPattern('/(]+name="data\[Model\]\[field\]"[^<>]+>.+){2}/', $result); $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0')); $this->assertPattern('/id="ModelField1"/', $result); $this->assertPattern('/id="ModelField0".*checked="checked"/', $result); $this->assertPattern('/(]+name="data\[Model\]\[field\]"[^<>]+>.+){2}/', $result); $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe'))); $expected = '
Legend title
'; $this->assertEqual($result, $expected); $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe'))); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe'))); $expected = '
Legend titleUnsubscribeSubscribe
'; $this->assertEqual($result, $expected); $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe'))); $expected = '
UnsubscribeSubscribe
'; $this->assertEqual($result, $expected); $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female')); $expected = '
Gender
'; $this->assertEqual($result, $expected); $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female')); $expected = '
Gender
'; $this->assertEqual($result, $expected); } function testSelect() { $result = $this->Form->select('Model.field', array()); $this->assertPattern('/^]+ \/>\s*]+name="data\[Model\]\[multi_field\]\[\]"[^<>\/]*>/', $result); $this->assertPattern('/^]+ \/>\s*]+id="ModelMultiField"[^<>\/]*>/', $result); $this->assertPattern('/^]+ \/>\s*]+multiple="multiple"[^<>\/]*>/', $result); $this->assertNoPattern('/^]+[^name|id|multiple]=[^<>\/]*>/', $result); $this->assertNoPattern('/option value=""/', $result); $this->assertNoPattern('/selected/', $result); $this->assertPattern('/]+value="0">first/', $result); $this->assertPattern('/]+value="1">second/', $result); $this->assertPattern('/]+value="2">third/', $result); $this->assertNoPattern('/]+value="[^012]"[^<>\/]*>/', $result); $this->assertPattern('/<\/select>$/', $result); $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), null, array('multiple' => 'multiple')); $this->assertPattern('/^]+ \/>\s*]+multiple="multiple"[^<>\/]*>/', $result); $this->assertNoPattern('/^]+[^name|id|multiple]=[^<>\/]*>/', $result); $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array(0, 1), array('multiple' => true)); $this->assertPattern('/]+value="0"[^<>]+selected="selected">first/', $result); $this->assertPattern('/]+value="1"[^<>]+selected="selected">second/', $result); $this->assertPattern('/]+value="2">third/', $result); $this->assertNoPattern('/]+value="[^012]"[^<>\/]*>/', $result); } function testSelectMultipleCheckboxes() { $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), null, array('multiple' => 'checkbox')); $this->assertPattern('/]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+value="0"[^<>]+\/>]+>first<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>first<\/label>/', $result); $this->assertPattern('/]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+value="1"[^<>]+\/>]+>second<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>second<\/label>/', $result); $this->assertPattern('/]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+value="2"[^<>]+\/>]+>third<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>third<\/label>/', $result); $this->assertNoPattern('/]+value="[^012]"[^<>\/]*>/', $result); $result = $this->Form->select('Model.multi_field', array('a' => 'first', 'b' => 'second', 'c' => 'third'), null, array('multiple' => 'checkbox')); $this->assertPattern('/]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+value="a"[^<>]+\/>]+>first<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>first<\/label>/', $result); $this->assertPattern('/]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+value="b"[^<>]+\/>]+>second<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>second<\/label>/', $result); $this->assertPattern('/]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+value="c"[^<>]+\/>]+>third<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>third<\/label>/', $result); $this->assertNoPattern('/]+value="[^abc]"[^<>\/]*>/', $result); $result = $this->Form->select('Model.multi_field', array('1' => 'first'), null, array('multiple' => 'checkbox')); $this->assertPattern('/^]+ \/>\s*]+>]+\/>]+>first<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>first<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+type="checkbox"[^<>]+\/>]+>first<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+value="1"[^<>]+\/>]+>first<\/label><\/div>$/', $result); $this->assertNoPattern('/^]+ \/>\s*]+>]+[^name|type|value]=[^<>\/]*>]+>first<\/label><\/div>$/', $result); $result = $this->Form->select('Model.multi_field', array('2' => 'second'), null, array('multiple' => 'checkbox')); $this->assertPattern('/^]+ \/>\s*]+>]+\/>]+>second<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>second<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+type="checkbox"[^<>]+\/>]+>second<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+value="2"[^<>]+\/>]+>second<\/label><\/div>$/', $result); $this->assertNoPattern('/^]+ \/>\s*]+>]+[^name|type|value]=[^<>\/]*>]+>second<\/label><\/div>$/', $result); } function testInputMultipleCheckboxes() { $result = $this->Form->input('Model.multi_field', array('options' => array('first', 'second', 'third'), 'multiple' => 'checkbox')); $this->assertPattern('/]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+value="0"[^<>]+\/>]+>first<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>first<\/label>/', $result); $this->assertPattern('/]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+value="1"[^<>]+\/>]+>second<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>second<\/label>/', $result); $this->assertPattern('/]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+value="2"[^<>]+\/>]+>third<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>third<\/label>/', $result); $this->assertNoPattern('/]+value="[^012]"[^<>\/]*>/', $result); $result = $this->Form->input('Model.multi_field', array('options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'), 'multiple' => 'checkbox')); $this->assertPattern('/]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>first<\/label>/', $result); $this->assertPattern('/]+value="a"[^<>]+\/>]+>first<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>first<\/label>/', $result); $this->assertPattern('/]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>second<\/label>/', $result); $this->assertPattern('/]+value="b"[^<>]+\/>]+>second<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>second<\/label>/', $result); $this->assertPattern('/]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+type="checkbox"[^<>]+\/>]+>third<\/label>/', $result); $this->assertPattern('/]+value="c"[^<>]+\/>]+>third<\/label>/', $result); $this->assertNoPattern('/]+[^name|type|value]=[^<>\/]*>]+>third<\/label>/', $result); $this->assertNoPattern('/]+value="[^abc]"[^<>\/]*>/', $result); $result = $this->Form->input('Model.multi_field', array('options' => array('1' => 'first'), 'multiple' => 'checkbox', 'label' => false, 'div' => false)); $this->assertPattern('/^]+ \/>\s*]+>]+\/>]+>first<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>first<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+type="checkbox"[^<>]+\/>]+>first<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+value="1"[^<>]+\/>]+>first<\/label><\/div>$/', $result); $this->assertNoPattern('/^]+ \/>\s*]+>]+[^name|type|value]=[^<>\/]*>]+>first<\/label><\/div>$/', $result); $result = $this->Form->input('Model.multi_field', array('options' => array('2' => 'second'), 'multiple' => 'checkbox', 'label' => false, 'div' => false)); $this->assertPattern('/^]+ \/>\s*]+>]+\/>]+>second<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/>]+>second<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+type="checkbox"[^<>]+\/>]+>second<\/label><\/div>$/', $result); $this->assertPattern('/^]+ \/>\s*]+>]+value="2"[^<>]+\/>]+>second<\/label><\/div>$/', $result); $this->assertNoPattern('/^]+ \/>\s*]+>]+[^name|type|value]=[^<>\/]*>]+>second<\/label><\/div>$/', $result); } function testCheckbox() { $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue')); $this->assertPattern('/^]+type="hidden"[^<>]+\/>]+type="checkbox"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value]=[^<>]*>$/', $result); $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+type="hidden"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+value="0"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+id="theID_"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+type="checkbox"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+value="myvalue"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+id="theID"[^<>]+\/>$/', $result); $this->Form->validationErrors['Model']['field'] = 1; $this->Form->data['Model']['field'] = 'myvalue'; $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue')); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value|class|checked]=[^<>]*>$/', $result); $this->assertPattern('/^]+\/>]+class="form-error"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+checked="checked"[^<>]+\/>$/', $result); $result = $this->Form->checkbox('Model.field', array('value' => 'myvalue')); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value|class|checked]=[^<>]*>$/', $result); $this->assertPattern('/^]+id="ModelField_"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+value="0"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+id="ModelField"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+value="myvalue"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+checked="checked"[^<>]+\/>$/', $result); $this->Form->data['Model']['field'] = ''; $result = $this->Form->checkbox('Model.field', array('id' => 'theID')); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value|class|checked]=[^<>]*>$/', $result); $this->assertPattern('/^]+id="theID_"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+value="0"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+id="theID"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+value="1"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+checked="checked"[^<>]+\/>$/', $result); unset($this->Form->validationErrors['Model']['field']); $result = $this->Form->checkbox('Model.field', array('value' => 'myvalue')); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value|class|checked]=[^<>]*>$/', $result); $this->assertPattern('/^]+id="ModelField_"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+value="0"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+id="ModelField"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+value="myvalue"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+checked="checked"[^<>]+\/>$/', $result); $result = $this->Form->checkbox('Contact.name', array('value' => 'myvalue')); $this->assertEqual($result, ''); $result = $this->Form->checkbox('Model.field'); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value|class|checked]=[^<>]*>$/', $result); $this->assertPattern('/^]+id="ModelField_"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+value="0"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+id="ModelField"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+value="1"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+checked="checked"[^<>]+\/>$/', $result); $result = $this->Form->checkbox('Model.field', array('checked' => false)); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value|class|checked]=[^<>]*>$/', $result); $this->assertPattern('/^]+id="ModelField_"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+value="0"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+id="ModelField"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+value="1"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+checked="checked"[^<>]+\/>$/', $result); $this->Form->validationErrors['Model']['field'] = 1; $this->Form->data['Contact']['published'] = 1; $result = $this->Form->checkbox('Contact.published', array('id'=>'theID')); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value|class|checked]=[^<>]*>$/', $result); $this->assertPattern('/^]+id="theID_"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+value="0"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+id="theID"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+value="1"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+checked="checked"[^<>]+\/>$/', $result); $this->Form->validationErrors['Model']['field'] = 1; $this->Form->data['Contact']['published'] = 0; $result = $this->Form->checkbox('Contact.published', array('id'=>'theID')); $this->assertNoPattern('/^]+[^type|name|id|value]=[^<>]*\/>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+[^type|name|id|value|class|checked]=[^<>]*>$/', $result); $this->assertPattern('/^]+id="theID_"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+value="0"[^<>]+\/>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+id="theID"[^<>]+\/>$/', $result); $this->assertPattern('/^]+\/>]+value="1"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+\/>]+checked="checked"[^<>]+\/>$/', $result); } function testDateTime() { $result = $this->Form->dateTime('Contact.date', 'DMY', '12', null, array(), false); $this->assertPattern('/]+value="'.date('m').'"[^<>]+selected="selected"[^>]*>/', $result); $result = $this->Form->dateTime('Contact.date', 'DMY', '12'); $this->assertPattern('/]*>/', $result); $this->assertNoPattern('/]+value=""[^<>]+selected="selected"[^>]*>/', $result); $result = $this->Form->dateTime('Contact.date', 'DMY', '12', false); $this->assertPattern('/]*>/', $result); $this->assertNoPattern('/]+selected="selected"[^>]*>/', $result); $result = $this->Form->dateTime('Contact.date', 'DMY', '12', ''); $this->assertPattern('/]*>/', $result); $this->assertNoPattern('/]+selected="selected"[^>]*>/', $result); $result = $this->Form->dateTime('Contact.date', 'DMY', '12', '', array('interval' => 5)); $this->assertPattern('/]*>/', $result); $this->assertPattern('/option value="55"/', $result); $this->assertNoPattern('/option value="59"/', $result); $this->assertNoPattern('/]+selected="selected"[^>]*>/', $result); $this->Form->data['Contact']['data'] = null; $result = $this->Form->dateTime('Contact.date', 'DMY', '12'); $this->assertPattern('/]*>/', $result); $this->assertNoPattern('/]+selected="selected"[^>]*>/', $result); $this->Form->data['Model']['field'] = '2008-01-01 00:00:00'; $result = $this->Form->dateTime('Model.field', 'DMY', '12', null, array(), false); $this->assertPattern('/option value="12" selected="selected"/', $result); $this->Form->create('Contact'); $result = $this->Form->input('published'); $this->assertPattern('/name="data\[Contact\]\[published\]\[month\]"/', $result); $this->assertPattern('/name="data\[Contact\]\[published\]\[day\]"/', $result); $this->assertPattern('/name="data\[Contact\]\[published\]\[year\]"/', $result); $result = $this->Form->input('published2', array('type' => 'date')); $this->assertPattern('/name="data\[Contact\]\[published2\]\[month\]"/', $result); $this->assertPattern('/name="data\[Contact\]\[published2\]\[day\]"/', $result); $this->assertPattern('/name="data\[Contact\]\[published2\]\[year\]"/', $result); $result = $this->Form->input('ContactTag'); $this->assertPattern('/name="data\[ContactTag\]\[ContactTag\]\[\]"/', $result); } function testFormDateTimeMulti() { $result = $this->Form->dateTime('Contact.1.updated'); $this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[day\]"/', $result); $this->assertPattern('/id="Contact1UpdatedDay"/', $result); $this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[month\]"/', $result); $this->assertPattern('/id="Contact1UpdatedMonth"/', $result); $this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[year\]"/', $result); $this->assertPattern('/id="Contact1UpdatedYear"/', $result); $this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[hour\]"/', $result); $this->assertPattern('/id="Contact1UpdatedHour"/', $result); $this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[min\]"/', $result); $this->assertPattern('/id="Contact1UpdatedMin"/', $result); $this->assertPattern('/name="data\[Contact\]\[1\]\[updated\]\[meridian\]"/', $result); $this->assertPattern('/id="Contact1UpdatedMeridian"/', $result); $result = $this->Form->dateTime('Contact.2.updated'); $this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[day\]"/', $result); $this->assertPattern('/id="Contact2UpdatedDay"/', $result); $this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[month\]"/', $result); $this->assertPattern('/id="Contact2UpdatedMonth"/', $result); $this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[year\]"/', $result); $this->assertPattern('/id="Contact2UpdatedYear"/', $result); $this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[hour\]"/', $result); $this->assertPattern('/id="Contact2UpdatedHour"/', $result); $this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[min\]"/', $result); $this->assertPattern('/id="Contact2UpdatedMin"/', $result); $this->assertPattern('/name="data\[Contact\]\[2\]\[updated\]\[meridian\]"/', $result); $this->assertPattern('/id="Contact2UpdatedMeridian"/', $result); } function testMonth() { $result = $this->Form->month('Model.field'); $this->assertPattern('/^]+name="data\[Model\]\[field\]\[month\]"[^<>]*>/', $result); $this->assertPattern('/]*>January<\/option>\s+/i', $result); $this->assertPattern('/]*>February<\/option>\s+/i', $result); } function testDay() { $result = $this->Form->day('Model.field', false); $this->assertPattern('/option value="12"/', $result); $this->assertPattern('/option value="13"/', $result); $this->Form->data['Model']['field'] = '2006-10-10 23:12:32'; $result = $this->Form->day('Model.field'); $this->assertPattern('/option value="10" selected="selected"/', $result); $this->assertNoPattern('/option value="32"/', $result); $this->Form->data['Model']['field'] = ''; $result = $this->Form->day('Model.field', '10'); $this->assertPattern('/option value="10" selected="selected"/', $result); $this->assertPattern('/option value="23"/', $result); $this->assertPattern('/option value="24"/', $result); $this->Form->data['Model']['field'] = '2006-10-10 23:12:32'; $result = $this->Form->day('Model.field', true); $this->assertPattern('/option value="10" selected="selected"/', $result); $this->assertPattern('/option value="23"/', $result); } function testMinute() { $result = $this->Form->minute('Model.field'); $this->assertPattern('/option value="59"/', $result); $this->assertNoPattern('/option value="60"/', $result); $this->Form->data['Model']['field'] = '2006-10-10 00:12:32'; $result = $this->Form->minute('Model.field'); $this->assertPattern('/option value="12" selected="selected"/', $result); $this->Form->data['Model']['field'] = ''; $result = $this->Form->minute('Model.field', null, array('interval' => 5)); $this->assertPattern('/option value="55"/', $result); $this->assertNoPattern('/option value="59"/', $result); $this->Form->data['Model']['field'] = '2006-10-10 00:10:32'; $result = $this->Form->minute('Model.field', null, array('interval' => 5)); $this->assertPattern('/option value="10" selected="selected"/', $result); } function testHour() { $result = $this->Form->hour('Model.field', false); $this->assertPattern('/option value="12"/', $result); $this->assertNoPattern('/option value="13"/', $result); $this->Form->data['Model']['field'] = '2006-10-10 00:12:32'; $result = $this->Form->hour('Model.field', false); $this->assertPattern('/option value="12" selected="selected"/', $result); $this->assertNoPattern('/option value="13"/', $result); $this->Form->data['Model']['field'] = ''; $result = $this->Form->hour('Model.field', true); $this->assertPattern('/option value="23"/', $result); $this->assertNoPattern('/option value="24"/', $result); $this->Form->data['Model']['field'] = '2006-10-10 00:12:32'; $result = $this->Form->hour('Model.field', true); $this->assertPattern('/option value="23"/', $result); $this->assertPattern('/option value="00" selected="selected"/', $result); $this->assertNoPattern('/option value="24"/', $result); } function testYear() { $result = $this->Form->year('Model.field', 2006, 2007); $this->assertPattern('/option value="2006"/', $result); $this->assertPattern('/option value="2007"/', $result); $this->assertNoPattern('/option value="2005"/', $result); $this->assertNoPattern('/option value="2008"/', $result); $this->data['Contact']['published'] = ''; $result = $this->Form->year('Contact.published', 2006, 2007, null, array('class' => 'year')); $expecting = ""; $this->assertEqual($result, $expecting); $this->Form->data['Contact']['published'] = '2006-10-10'; $result = $this->Form->year('Contact.published', 2006, 2007, null, array(), false); $expecting = ""; $this->assertEqual($result, $expecting); $this->Form->data['Contact']['published'] = ''; $result = $this->Form->year('Contact.published', 2006, 2007, false); $expecting = ""; $this->assertEqual($result, $expecting); $this->Form->data['Contact']['published'] = '2006-10-10'; $result = $this->Form->year('Contact.published', 2006, 2007, false, array(), false); $expecting = ""; $this->assertEqual($result, $expecting); $this->Form->data['Contact']['published'] = ''; $result = $this->Form->year('Contact.published', 2006, 2007, 2007); $expecting = ""; $this->assertEqual($result, $expecting); $this->Form->data['Contact']['published'] = '2006-10-10'; $result = $this->Form->year('Contact.published', 2006, 2007, 2007, array(), false); $expecting = ""; $this->assertEqual($result, $expecting); $this->Form->data['Contact']['published'] = ''; $result = $this->Form->year('Contact.published', 2006, 2008, 2007, array(), false); $expecting = ""; $this->assertEqual($result, $expecting); $this->Form->data['Contact']['published'] = '2006-10-10'; $result = $this->Form->year('Contact.published', 2006, 2008, null, array(), false); $expecting = ""; $this->assertEqual($result, $expecting); } function testTextArea() { $this->Form->data = array('Model' => array('field' => 'some test data')); $result = $this->Form->textarea('Model.field'); $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]*>/', $result); $this->assertPattern('/^]+id="ModelField"[^<>]*>/', $result); $this->assertPattern('/^]+>some test data<\/textarea>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+>$/', $result); $this->assertNoPattern('/]+[^name|id]=[^<>]*>/', $result); $result = $this->Form->textarea('Model/tmp'); $this->assertPattern('/^]+name="data\[Model\]\[tmp\]"[^<>]+><\/textarea>/', $result); $this->Form->data = array('Model' => array('field' => 'some test data with HTML chars')); $result = $this->Form->textarea('Model.field'); $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]*>/', $result); $this->assertPattern('/^]+id="ModelField"[^<>]*>/', $result); $this->assertPattern('/^]+>some <strong>test<\/strong> data with <a href="#">HTML<\/a> chars<\/textarea>$/', $result); $this->assertNoPattern('/^]+value="[^<>]+>/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+>$/', $result); $this->assertNoPattern('/]+[^name|id]=[^<>]*>/', $result); } function testHiddenField() { $this->Form->validationErrors['Model']['field'] = 1; $this->Form->data['Model']['field'] = 'test'; $result = $this->Form->hidden('Model.field', array('id' => 'theID')); $this->assertPattern('/^]+type="hidden"[^<>]+\/>$/', $result); $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]+id="theID"[^<>]+value="test"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value|class]=[^<>]*>/', $result); } function testFileUploadField() { $result = $this->Form->file('Model.upload'); $this->assertPattern('/^]+name="data\[Model\]\[upload\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ModelUpload"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|value|id]=[^<>]*>$/', $result); $this->Form->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0); $result = $this->Form->file('Model.upload'); $result = $this->Form->input('Model.upload', array('type' => 'file')); $this->assertPattern('/]+type="file"[^<>]+\/>/', $result); $this->assertPattern('/]+name="data\[Model\]\[upload\]"[^<>]+\/>/', $result); $this->assertPattern('/]+value=""[^<>]+\/>/', $result); $this->assertPattern('/]+id="ModelUpload"[^<>]+\/>/', $result); $this->assertNoPattern('/]+[^(type|name|value|id)]=[^<>]*>$/', $result); } function testButton() { $result = $this->Form->button('Hi'); $expected = ''; $this->assertEqual($result, $expected); $result = $this->Form->button('Clear Form', array('type' => 'clear')); $expected = ''; $this->assertEqual($result, $expected); $result = $this->Form->button('Reset Form', array('type' => 'reset')); $expected = ''; $this->assertEqual($result, $expected); $result = $this->Form->button('Options', array('type' => 'reset', 'name' => 'Post.options')); $this->assertPattern('/^]+ \/>$/', $result); $this->assertPattern('/^]+value="Options"[^<>]+\/>$/', $result); $this->assertPattern('/^]+name="data\[Post\]\[options\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="PostOptions"[^<>]+\/>$/', $result); $result = $this->Form->button('Options', array('type' => 'reset', 'name' => 'Post.options', 'id' => 'Opt')); $this->assertPattern('/^]+id="Opt"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+id=[^<>]+id=/', $result); } function testSubmitButton() { $result = $this->Form->submit('Test Submit'); $this->assertPattern('/^]+value="Test Submit"[^<>]+\/><\/div>$/', $result); $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false)); $this->assertPattern('/^]+value="Test Submit"[^<>]+\/>$/', $result); $this->assertPattern('/^<[^<>]+class="save"[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|class|value]=[^<>]*>/', $result); $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton'))); $this->assertPattern('/^]+id="SaveButton"[^<>]*>]+value="Test Submit"[^<>]+\/><\/div>$/', $result); $this->assertNoPattern('/]+[^type|value]=[^<>]*>/', $result); $result = $this->Form->submit('Next >'); $this->assertPattern('/^]+value="Next >"[^<>]+\/><\/div>$/', $result); $result = $this->Form->submit('Next >', array('escape' => false)); $this->assertPattern('/^]+value="Next >"[^<>]+\/><\/div>$/', $result); $result = $this->Form->submit('http://example.com/cake.power.gif'); $this->assertEqual('
', $result); $result = $this->Form->submit('/relative/cake.power.gif'); $this->assertEqual('
', $result); $result = $this->Form->submit('cake.power.gif'); $this->assertEqual('
', $result); $result = $this->Form->submit('Not.an.image'); $this->assertEqual('
', $result); } function testFormCreate() { $result = $this->Form->create('Contact'); $this->assertPattern('/^]+>/', $result); $this->assertPattern('/\s+id="ContactAddForm"/', $result); $this->assertPattern('/\s+method="post"/', $result); $this->assertPattern('/\s+action="\/contacts\/add\/"/', $result); $result = $this->Form->create('Contact', array('type' => 'GET')); $this->assertPattern('/^]+method="get"[^<>]+>$/', $result); $result = $this->Form->create('Contact', array('type' => 'get')); $this->assertPattern('/^]+method="get"[^<>]+>$/', $result); $result = $this->Form->create('Contact', array('type' => 'put')); $this->assertPattern('/^]+method="post"[^<>]+>/', $result); $this->Form->data['Contact']['id'] = 1; $result = $this->Form->create('Contact'); $this->assertPattern('/^]+method="post"[^<>]+>/', $result); $this->assertPattern('/^]+id="ContactEditForm"[^<>]+>/', $result); $this->assertPattern('/^]+action="\/contacts\/edit\/1"[^<>]*>/', $result); $this->assertNoPattern('/^]+[^id|method|action]=[^<>]*>/', $result); $result = $this->Form->create('Contact', array('id' => 'TestId')); $this->assertPattern('/id="TestId"/', $result); $result = $this->Form->create('User', array('url' => array('action' => 'login'))); $this->assertPattern('/id="UserAddForm"/', $result); $this->assertPattern('/action="\/users\/login(\/)?"/', $result); $result = $this->Form->create('User', array('action' => 'login')); $this->assertPattern('/id="UserLoginForm"/', $result); $this->assertPattern('/action="\/users\/login(\/)?"/', $result); $result = $this->Form->create('User', array('url' => '/users/login')); $this->assertPattern('/method="post"/', $result); $this->assertPattern('/action="\/users\/login(\/)?"/', $result); $this->assertNoPattern('/^]+[^method|action]=[^<>]*>/', $result); $this->Form->params['controller'] = 'pages'; $this->Form->params['models'] = array('User', 'Post'); $result = $this->Form->create('User', array('action' => 'signup')); $this->assertPattern('/id="UserSignupForm"/', $result); $this->assertPattern('/action="\/users\/signup[\/]"/', $result); } function testGetFormCreate() { $result = $this->Form->create('Contact', array('type' => 'get')); $this->assertPattern('/^]+>/', $result); $this->assertPattern('/\s+id="ContactAddForm"/', $result); $this->assertPattern('/\s+method="get"/', $result); $this->assertPattern('/\s+action="\/contacts\/add\/"/', $result); $this->assertNoPattern('/^]+[^method|action|id]=[^<>]*>/', $result); $result = $this->Form->text('Contact.name'); $this->assertPattern('/^]+name="name"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="text"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ContactName"[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^id|name|type|value]=[^<>]*>$/', $result); $result = $this->Form->password('password'); $this->assertPattern('/^]+name="password"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="password"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ContactPassword"[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^id|name|type|value]=[^<>]*>$/', $result); $result = $this->Form->text('user_form'); $this->assertPattern('/^]+name="user_form"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="text"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ContactUserForm"[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^id|name|type|value]=[^<>]*>$/', $result); } function testEditFormWithData() { $this->Form->data = array('Person' => array( 'id' => 1, 'first_name' => 'Nate', 'last_name' => 'Abele', 'email' => 'nate@cakephp.org' )); $this->Form->params = array('models' => array('Person'), 'controller' => 'people'); $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry'); $this->Form->create(); $result = $this->Form->select('People.People', $options, null, array('multiple' => true)); $this->assertPattern('/^]+ \/>\s*]+>\s*(]+>.+<\/option>\s*){3}<\/select>$/', $result); $this->assertPattern('/^]+ \/>\s*]+name="data\[People\]\[People\]\[\]"[^<>]*>/', $result); $this->assertPattern('/^]+ \/>\s*]+multiple="multiple"[^<>]*>/', $result); $this->assertPattern('/^]+ \/>\s*]+id="PeoplePeople"[^<>]*>/', $result); $this->assertNoPattern('/]+[^id|name|multiple]=[^<>]*>$/', $result); } function testFormMagicInput() { $result = $this->Form->create('Contact'); $this->assertPattern('/^]+>]+\/><\/fieldset>$/', $result); $this->assertNoPattern('/^]+[^id|method|action]=[^<>]*>/', $result); $result = $this->Form->input('name'); $this->assertPattern('/^]+>]+>Name<\/label>]+ \/><\/div>$/', $result); $this->assertPattern('/^]+class="input">/', $result); $this->assertPattern('/]+for="ContactName">/', $result); $this->assertPattern('/]+name="data\[Contact\]\[name\]"[^<>]+\/>/', $result); $this->assertPattern('/]+type="text"[^<>]+\/>/', $result); $this->assertPattern('/]+maxlength="255"[^<>]+\/>/', $result); $this->assertPattern('/]+value=""[^<>]+\/>/', $result); $this->assertPattern('/]+id="ContactName"[^<>]+\/>/', $result); $this->assertNoPattern('/]+[^id|maxlength|name|type|value]=[^<>]*>/', $result); $result = $this->Form->input('Address.street'); $this->assertPattern('/^]+>]+>[^<>]+<\/label>]+\/><\/div>$/', $result); $this->assertPattern('//', $result); $this->assertPattern('/Street<\/label>/', $result); $this->assertPattern('/]+name="data\[Address\]\[street\]"[^<>]+\/>/', $result); $this->assertPattern('/]+type="text"[^<>]+\/>/', $result); $this->assertPattern('/]+value=""[^<>]+\/>/', $result); $this->assertPattern('/]+id="AddressStreet"[^<>]+\/>/', $result); $this->assertNoPattern('/]+[^id|name|type|value]=[^<>]*>/', $result); $result = $this->Form->input('name', array('div' => false)); $this->assertPattern('/^]+>Name<\/label>]+\/>$/', $result); $this->assertPattern('/]+for="ContactName"[^<>]*>Name<\/label>/', $result); $this->assertNoPattern('/]+[^for]=[^<>]*>/', $result); $this->assertPattern('/]+name="data\[Contact\]\[name\]"[^<>]*\/>$/', $result); $this->assertPattern('/]+type="text"[^<>]*\/>$/', $result); $this->assertPattern('/]+maxlength="255"[^<>]*\/>$/', $result); $this->assertPattern('/]+value=""[^<>]*\/>$/', $result); $this->assertPattern('/]+id="ContactName"[^<>]*\/>$/', $result); $this->assertNoPattern('/]+[^id|maxlength|name|type|value]=[^<>]*>/', $result); $result = $this->Form->input('Contact.non_existing'); $this->assertPattern('/^
' . '