Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

6738 lines (6226 sloc) 214.859 kb
<?php
/**
* FormHelperTest file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2006-2010, Cake Software Foundation, Inc.
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
* @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', array('ClassRegistry', 'Controller', 'View', 'Model', 'Security'));
App::import('Helper', 'Html');
App::import('Helper', 'Form');
/**
* ContactTestController class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class ContactTestController extends Controller {
/**
* name property
*
* @var string 'ContactTest'
* @access public
*/
var $name = 'ContactTest';
/**
* uses property
*
* @var mixed null
* @access public
*/
var $uses = null;
}
/**
* Contact class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class Contact extends CakeTestModel {
/**
* primaryKey property
*
* @var string 'id'
* @access public
*/
var $primaryKey = 'id';
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* name property
*
* @var string 'Contact'
* @access public
*/
var $name = 'Contact';
/**
* Default schema
*
* @var array
* @access public
*/
var $_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)
);
/**
* validate property
*
* @var array
* @access public
*/
var $validate = array(
'non_existing' => array(),
'idontexist' => array(),
'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
'imrequiredtoo' => array('rule' => 'notEmpty'),
'required_one' => array('required' => array('rule' => array('notEmpty'))),
'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
'imalsonotrequired' => array(
'alpha' => array('rule' => 'alphaNumeric','allowEmpty' => true),
'between' => array('rule' => array('between', 5, 30)),
),
'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
);
/**
* schema method
*
* @access public
* @return void
*/
function setSchema($schema) {
$this->_schema = $schema;
}
/**
* hasAndBelongsToMany property
*
* @var array
* @access public
*/
var $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
/**
* hasAndBelongsToMany property
*
* @var array
* @access public
*/
var $belongsTo = array('User' => array('className' => 'UserForm'));
}
/**
* ContactTagsContact class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class ContactTagsContact extends CakeTestModel {
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* name property
*
* @var string 'Contact'
* @access public
*/
var $name = 'ContactTagsContact';
/**
* Default schema
*
* @var array
* @access public
*/
var $_schema = array(
'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'contact_tag_id' => array(
'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
)
);
/**
* schema method
*
* @access public
* @return void
*/
function setSchema($schema) {
$this->_schema = $schema;
}
}
/**
* ContactNonStandardPk class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class ContactNonStandardPk extends Contact {
/**
* primaryKey property
*
* @var string 'pk'
* @access public
*/
var $primaryKey = 'pk';
/**
* name property
*
* @var string 'ContactNonStandardPk'
* @access public
*/
var $name = 'ContactNonStandardPk';
/**
* schema method
*
* @access public
* @return void
*/
function schema() {
$this->_schema = parent::schema();
$this->_schema['pk'] = $this->_schema['id'];
unset($this->_schema['id']);
return $this->_schema;
}
}
/**
* ContactTag class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class ContactTag extends Model {
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* schema definition
*
* @var array
* @access protected
*/
var $_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)
);
}
/**
* UserForm class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class UserForm extends CakeTestModel {
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* primaryKey property
*
* @var string 'id'
* @access public
*/
var $primaryKey = 'id';
/**
* name property
*
* @var string 'UserForm'
* @access public
*/
var $name = 'UserForm';
/**
* hasMany property
*
* @var array
* @access public
*/
var $hasMany = array(
'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
));
/**
* schema definition
*
* @var array
* @access protected
*/
var $_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' => 10),
'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
);
}
/**
* OpenidUrl class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class OpenidUrl extends CakeTestModel {
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* primaryKey property
*
* @var string 'id'
* @access public
*/
var $primaryKey = 'id';
/**
* name property
*
* @var string 'OpenidUrl'
* @access public
*/
var $name = 'OpenidUrl';
/**
* belongsTo property
*
* @var array
* @access public
*/
var $belongsTo = array('UserForm' => array(
'className' => 'UserForm', 'foreignKey' => 'user_form_id'
));
/**
* validate property
*
* @var array
* @access public
*/
var $validate = array('openid_not_registered' => array());
/**
* schema method
*
* @var array
* @access protected
*/
var $_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'),
);
/**
* beforeValidate method
*
* @access public
* @return void
*/
function beforeValidate() {
$this->invalidate('openid_not_registered');
return true;
}
}
/**
* ValidateUser class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class ValidateUser extends CakeTestModel {
/**
* primaryKey property
*
* @var string 'id'
* @access public
*/
var $primaryKey = 'id';
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* name property
*
* @var string 'ValidateUser'
* @access public
*/
var $name = 'ValidateUser';
/**
* hasOne property
*
* @var array
* @access public
*/
var $hasOne = array('ValidateProfile' => array(
'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
));
/**
* schema method
*
* @var array
* @access protected
*/
var $_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'),
'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
);
/**
* beforeValidate method
*
* @access public
* @return void
*/
function beforeValidate() {
$this->invalidate('email');
return false;
}
}
/**
* ValidateProfile class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class ValidateProfile extends CakeTestModel {
/**
* primaryKey property
*
* @var string 'id'
* @access public
*/
var $primaryKey = 'id';
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* schema property
*
* @var array
* @access protected
*/
var $_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)
);
/**
* name property
*
* @var string 'ValidateProfile'
* @access public
*/
var $name = 'ValidateProfile';
/**
* hasOne property
*
* @var array
* @access public
*/
var $hasOne = array('ValidateItem' => array(
'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
));
/**
* belongsTo property
*
* @var array
* @access public
*/
var $belongsTo = array('ValidateUser' => array(
'className' => 'ValidateUser', 'foreignKey' => 'user_id'
));
/**
* beforeValidate method
*
* @access public
* @return void
*/
function beforeValidate() {
$this->invalidate('full_name');
$this->invalidate('city');
return false;
}
}
/**
* ValidateItem class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class ValidateItem extends CakeTestModel {
/**
* primaryKey property
*
* @var string 'id'
* @access public
*/
var $primaryKey = 'id';
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* name property
*
* @var string 'ValidateItem'
* @access public
*/
var $name = 'ValidateItem';
/**
* schema property
*
* @var array
* @access protected
*/
var $_schema = array(
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'name' => array('type' => 'text', '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)
);
/**
* belongsTo property
*
* @var array
* @access public
*/
var $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
/**
* beforeValidate method
*
* @access public
* @return void
*/
function beforeValidate() {
$this->invalidate('description');
return false;
}
}
/**
* TestMail class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class TestMail extends CakeTestModel {
/**
* primaryKey property
*
* @var string 'id'
* @access public
*/
var $primaryKey = 'id';
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* name property
*
* @var string 'TestMail'
* @access public
*/
var $name = 'TestMail';
}
/**
* FormHelperTest class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class FormHelperTest extends CakeTestCase {
/**
* fixtures property
*
* @var array
* @access public
*/
var $fixtures = array(null);
/**
* setUp method
*
* @access public
* @return void
*/
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);
$this->Form->params['action'] = 'add';
ClassRegistry::addObject('view', $view);
ClassRegistry::addObject('Contact', new Contact());
ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
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());
$this->oldSalt = Configure::read('Security.salt');
$this->dateRegex = array(
'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
);
Configure::write('Security.salt', 'foo!');
}
/**
* tearDown method
*
* @access public
* @return void
*/
function tearDown() {
ClassRegistry::removeObject('view');
ClassRegistry::removeObject('Contact');
ClassRegistry::removeObject('ContactNonStandardPk');
ClassRegistry::removeObject('ContactTag');
ClassRegistry::removeObject('OpenidUrl');
ClassRegistry::removeObject('UserForm');
ClassRegistry::removeObject('ValidateItem');
ClassRegistry::removeObject('ValidateUser');
ClassRegistry::removeObject('ValidateProfile');
unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
Configure::write('Security.salt', $this->oldSalt);
}
/**
* testFormCreateWithSecurity method
*
* Test form->create() with security key.
*
* @access public
* @return void
*/
function testCreateWithSecurity() {
$this->Form->params['_Token'] = array('key' => 'testKey');
$encoding = strtolower(Configure::read('App.encoding'));
$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding),
'div' => array('style' => 'display:none;'),
array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
array('input' => array(
'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
)),
'/div'
);
$this->assertTags($result, $expected,true);
$result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
$expected['form']['id'] = 'MyForm';
$this->assertTags($result, $expected);
}
/**
* test that create() clears the fields property so it starts fresh
*
* @return void
*/
function testCreateClearingFields() {
$this->Form->fields = array('model_id');
$this->Form->create('Contact');
$this->assertEqual($this->Form->fields, array());
}
/**
* Tests form hash generation with model-less data
*
* @access public
* @return void
*/
function testValidateHashNoModel() {
$this->Form->params['_Token'] = array('key' => 'foo');
$result = $this->Form->secure(array('anything'));
$this->assertPattern('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
}
/**
* Tests that models with identical field names get resolved properly
*
* @access public
* @return void
*/
function testDuplicateFieldNameResolution() {
$result = $this->Form->create('ValidateUser');
$this->assertEqual($this->View->entity(), array('ValidateUser'));
$result = $this->Form->input('ValidateItem.name');
$this->assertEqual($this->View->entity(), array('ValidateItem', 'name'));
$result = $this->Form->input('ValidateUser.name');
$this->assertEqual($this->View->entity(), array('ValidateUser', 'name'));
$this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
$this->assertPattern('/type="text"/', $result);
$result = $this->Form->input('ValidateItem.name');
$this->assertEqual($this->View->entity(), array('ValidateItem', 'name'));
$this->assertPattern('/name="data\[ValidateItem\]\[name\]"/', $result);
$this->assertPattern('/<textarea/', $result);
$result = $this->Form->input('name');
$this->assertEqual($this->View->entity(), array('ValidateUser', 'name'));
$this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
$this->assertPattern('/type="text"/', $result);
}
/**
* Tests that hidden fields generated for checkboxes don't get locked
*
* @access public
* @return void
*/
function testNoCheckboxLocking() {
$this->Form->params['_Token'] = array('key' => 'foo');
$this->assertIdentical($this->Form->fields, array());
$this->Form->checkbox('check', array('value' => '1'));
$this->assertIdentical($this->Form->fields, array('check'));
}
/**
* testFormSecurityFields method
*
* Test generation of secure form hash generation.
*
* @access public
* @return void
*/
function testFormSecurityFields() {
$key = 'testKey';
$fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
$this->Form->params['_Token']['key'] = $key;
$result = $this->Form->secure($fields);
$expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
$expected .= ':' . 'Model.valid';
$expected = array(
'div' => array('style' => 'display:none;'),
'input' => array(
'type' => 'hidden', 'name' => 'data[_Token][fields]',
'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* Tests correct generation of text fields for double and float fields
*
* @access public
* @return void
*/
function testTextFieldGenerationForFloats() {
$model = ClassRegistry::getObject('Contact');
$model->setSchema(array('foo' => array(
'type' => 'float',
'null' => false,
'default' => null,
'length' => null
)));
$this->Form->create('Contact');
$result = $this->Form->input('foo');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactFoo'),
'Foo',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'data[Contact][foo]',
'id' => 'ContactFoo'
)),
'/div'
);
}
/**
* testFormSecurityMultipleFields method
*
* Test secure() with multiple row form. Ensure hash is correct.
*
* @access public
* @return void
*/
function testFormSecurityMultipleFields() {
$key = 'testKey';
$fields = array(
'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
);
$this->Form->params['_Token']['key'] = $key;
$result = $this->Form->secure($fields);
$hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
$hash .= '%7CModel.1.hidden%7CModel.1.valid';
$expected = array(
'div' => array('style' => 'display:none;'),
'input' => array(
'type' => 'hidden', 'name' => 'data[_Token][fields]',
'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormSecurityMultipleSubmitButtons
*
* test form submit generation and ensure that _Token is only created on end()
*
* @return void
*/
function testFormSecurityMultipleSubmitButtons() {
$key = 'testKey';
$this->Form->params['_Token']['key'] = $key;
$this->Form->create('Addresses');
$this->Form->input('Address.title');
$this->Form->input('Address.first_name');
$result = $this->Form->submit('Save', array('name' => 'save'));
$expected = array(
'div' => array('class' => 'submit'),
'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
'/div',
);
$this->assertTags($result, $expected);
$result = $this->Form->submit('Cancel', array('name' => 'cancel'));
$expected = array(
'div' => array('class' => 'submit'),
'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
'/div',
);
$this->assertTags($result, $expected);
$result = $this->Form->end(null);
$expected = array(
'div' => array('style' => 'display:none;'),
'input' => array(
'type' => 'hidden', 'name' => 'data[_Token][fields]',
'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormSecurityMultipleInputFields method
*
* Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
*
* @access public
* @return void
*/
function testFormSecurityMultipleInputFields() {
$key = 'testKey';
$this->Form->params['_Token']['key'] = $key;
$this->Form->create('Addresses');
$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
$this->Form->input('Addresses.0.title');
$this->Form->input('Addresses.0.first_name');
$this->Form->input('Addresses.0.last_name');
$this->Form->input('Addresses.0.address');
$this->Form->input('Addresses.0.city');
$this->Form->input('Addresses.0.phone');
$this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
$this->Form->input('Addresses.1.title');
$this->Form->input('Addresses.1.first_name');
$this->Form->input('Addresses.1.last_name');
$this->Form->input('Addresses.1.address');
$this->Form->input('Addresses.1.city');
$this->Form->input('Addresses.1.phone');
$this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
$result = $this->Form->secure($this->Form->fields);
$hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
$expected = array(
'div' => array('style' => 'display:none;'),
'input' => array(
'type' => 'hidden', 'name' => 'data[_Token][fields]',
'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormSecurityMultipleInputDisabledFields method
*
* test secure form generation with multiple records and disabled fields.
*
* @access public
* @return void
*/
function testFormSecurityMultipleInputDisabledFields() {
$key = 'testKey';
$this->Form->params['_Token']['key'] = $key;
$this->Form->params['_Token']['disabledFields'] = array('first_name', 'address');
$this->Form->create();
$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
$this->Form->input('Addresses.0.title');
$this->Form->input('Addresses.0.first_name');
$this->Form->input('Addresses.0.last_name');
$this->Form->input('Addresses.0.address');
$this->Form->input('Addresses.0.city');
$this->Form->input('Addresses.0.phone');
$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
$this->Form->input('Addresses.1.title');
$this->Form->input('Addresses.1.first_name');
$this->Form->input('Addresses.1.last_name');
$this->Form->input('Addresses.1.address');
$this->Form->input('Addresses.1.city');
$this->Form->input('Addresses.1.phone');
$result = $this->Form->secure($this->Form->fields);
$hash = '774df31936dc850b7d8a5277dc0b890123788b09%3AAddresses.0.id%7CAddresses.1.id';
$expected = array(
'div' => array('style' => 'display:none;'),
'input' => array(
'type' => 'hidden', 'name' => 'data[_Token][fields]',
'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormSecurityInputDisabledFields method
*
* Test single record form with disabled fields.
*
* @access public
* @return void
*/
function testFormSecurityInputDisabledFields() {
$key = 'testKey';
$this->Form->params['_Token']['key'] = $key;
$this->Form->params['_Token']['disabledFields'] = array('first_name', 'address');
$this->Form->create();
$this->Form->hidden('Addresses.id', array('value' => '123456'));
$this->Form->input('Addresses.title');
$this->Form->input('Addresses.first_name');
$this->Form->input('Addresses.last_name');
$this->Form->input('Addresses.address');
$this->Form->input('Addresses.city');
$this->Form->input('Addresses.phone');
$result = $this->Form->fields;
$expected = array(
'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
'Addresses.city', 'Addresses.phone'
);
$this->assertEqual($result, $expected);
$result = $this->Form->secure($expected);
$hash = '449b7e889128e8e52c5e81d19df68f5346571492%3AAddresses.id';
$expected = array(
'div' => array('style' => 'display:none;'),
'input' => array(
'type' => 'hidden', 'name' => 'data[_Token][fields]',
'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* test securing inputs with custom name attributes.
*
* @return void
*/
function testFormSecureWithCustomNameAttribute() {
$this->Form->params['_Token']['key'] = 'testKey';
$this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
$this->assertEqual('User.custom', $this->Form->fields[0]);
$this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
$this->assertEqual('User.custom.another.value', $this->Form->fields[1]);
}
/**
* testFormSecuredInput method
*
* Test generation of entire secure form, assertions made on input() output.
*
* @access public
* @return void
*/
function testFormSecuredInput() {
$this->Form->params['_Token']['key'] = 'testKey';
$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding),
'div' => array('style' => 'display:none;'),
array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
array('input' => array(
'type' => 'hidden', 'name' => 'data[_Token][key]',
'value' => 'testKey', 'id' => 'preg:/Token\d+/'
)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('UserForm.published', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'UserFormPublished'),
'Published',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'data[UserForm][published]',
'id' => 'UserFormPublished'
)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('UserForm.other', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'UserFormOther'),
'Other',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'data[UserForm][other]',
'id' => 'UserFormOther'
)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->hidden('UserForm.stuff');
$expected = array('input' => array(
'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
'id' => 'UserFormStuff'
));
$this->assertTags($result, $expected);
$result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
$expected = array('input' => array(
'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
'value' => '0', 'id' => 'UserFormHidden'
));
$this->assertTags($result, $expected);
$result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
$expected = array(
'div' => array('class' => 'input checkbox'),
array('input' => array(
'type' => 'hidden', 'name' => 'data[UserForm][something]',
'value' => '0', 'id' => 'UserFormSomething_'
)),
array('input' => array(
'type' => 'checkbox', 'name' => 'data[UserForm][something]',
'value' => '1', 'id' => 'UserFormSomething'
)),
'label' => array('for' => 'UserFormSomething'),
'Something',
'/label',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->fields;
$expected = array(
'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
'UserForm.hidden' => '0', 'UserForm.something'
);
$this->assertEqual($result, $expected);
$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' => 'data[_Token][fields]',
'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
)),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* Tests that the correct keys are added to the field hash index
*
* @access public
* @return void
*/
function testFormSecuredFileInput() {
$this->Form->params['_Token']['key'] = 'testKey';
$this->assertEqual($this->Form->fields, array());
$result = $this->Form->file('Attachment.file');
$expected = array (
'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
'Attachment.file.error', 'Attachment.file.size'
);
$this->assertEqual($this->Form->fields, $expected);
}
/**
* test that multiple selects keys are added to field hash
*
* @access public
* @return void
*/
function testFormSecuredMultipleSelect() {
$this->Form->params['_Token']['key'] = 'testKey';
$this->assertEqual($this->Form->fields, array());
$options = array('1' => 'one', '2' => 'two');
$this->Form->select('Model.select', $options);
$expected = array('Model.select');
$this->assertEqual($this->Form->fields, $expected);
$this->Form->fields = array();
$this->Form->select('Model.select', $options, null, array('multiple' => true));
$this->assertEqual($this->Form->fields, $expected);
}
/**
* testFormSecuredRadio method
*
* @access public
* @return void
*/
function testFormSecuredRadio() {
$this->Form->params['_Token']['key'] = 'testKey';
$this->assertEqual($this->Form->fields, array());
$options = array('1' => 'option1', '2' => 'option2');
$this->Form->radio('Test.test', $options);
$expected = array('Test.test');
$this->assertEqual($this->Form->fields, $expected);
}
/**
* testDisableSecurityUsingForm method
*
* @access public
* @return void
*/
function testDisableSecurityUsingForm() {
$this->Form->params['_Token']['key'] = 'testKey';
$this->Form->params['_Token']['disabledFields'] = array();
$this->Form->create();
$this->Form->hidden('Addresses.id', array('value' => '123456'));
$this->Form->input('Addresses.title');
$this->Form->input('Addresses.first_name', array('secure' => false));
$this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
$this->Form->input('Addresses.zip', array(
'type' => 'select', 'options' => array(1,2), 'secure' => false
));
$result = $this->Form->fields;
$expected = array(
'Addresses.id' => '123456', 'Addresses.title',
);
$this->assertEqual($result, $expected);
}
/**
* testPasswordValidation method
*
* test validation errors on password input.
*
* @access public
* @return void
*/
function testPasswordValidation() {
$this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
$result = $this->Form->input('Contact.password');
$expected = array(
'div' => array('class' => 'input password error'),
'label' => array('for' => 'ContactPassword'),
'Password',
'/label',
'input' => array(
'type' => 'password', 'name' => 'data[Contact][password]',
'id' => 'ContactPassword', 'class' => 'form-error'
),
array('div' => array('class' => 'error-message')),
'Please provide a password',
'/div',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormValidationAssociated method
*
* test display of form errors in conjunction with model::validates.
*
* @access public
* @return void
*/
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'));
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array(
'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
'accept-charset' => $encoding
),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/div'
);
$this->assertTags($result, $expected);
$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, $this->UserForm);
}
/**
* testFormValidationAssociatedFirstLevel method
*
* test form error display with associated model.
*
* @access public
* @return void
*/
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'));
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/div'
);
$this->assertTags($result, $expected);
$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);
}
/**
* testFormValidationAssociatedSecondLevel method
*
* test form error display with associated model.
*
* @access public
* @return void
*/
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'));
$encoding = strtolower(Configure::read('App.encoding'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
'div' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/div'
);
$this->assertTags($result, $expected);
$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);
}
/**
* testFormValidationMultiRecord method
*
* test form error display with multiple records.
*
* @access public
* @return void
*/
function testFormValidationMultiRecord() {
$this->Form->validationErrors['Contact'] = array(2 => array(
'name' => 'This field cannot be left blank'
));
$result = $this->Form->input('Contact.2.name');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'preg:/[^<]+/',
'/label',
'input' => array(
'type' => 'text', 'name', 'id',
'class' => 'form-error', 'maxlength' => 255
),
array('div' => array('class' => 'error-message')),
'This field cannot be left blank',
'/div',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->validationErrors['UserForm'] = array(
'OpenidUrl' => array('url' => 'You must provide a URL'
));
$this->Form->create('UserForm');
$result = $this->Form->input('OpenidUrl.url');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'preg:/[^<]+/',
'/label',
'input' => array(
'type' => 'text', 'name', 'id', 'class' => 'form-error'
),
array('div' => array('class' => 'error-message')),
'You must provide a URL',
'/div',
'/div'
);
}
/**
* testMultipleInputValidation method
*
* test multiple record form validation error display.
*
* @access public
* @return void
*/
function testMultipleInputValidation() {
$this->Form->create();
$this->Form->validationErrors['Address'][0]['title'] = 'This field cannot be empty';
$this->Form->validationErrors['Address'][0]['first_name'] = 'This field cannot be empty';
$this->Form->validationErrors['Address'][1]['last_name'] = 'You must have a last name';
$result = $this->Form->input('Address.0.title');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'preg:/[^<]+/',
'/label',
'input' => array(
'type' => 'text', 'name', 'id', 'class' => 'form-error'
),
array('div' => array('class' => 'error-message')),
'This field cannot be empty',
'/div',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Address.0.first_name');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'preg:/[^<]+/',
'/label',
'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
array('div' => array('class' => 'error-message')),
'This field cannot be empty',
'/div',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Address.0.last_name');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'preg:/[^<]+/',
'/label',
'input' => array('type' => 'text', 'name', 'id'),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Address.1.last_name');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'preg:/[^<]+/',
'/label',
'input' => array(
'type' => 'text', 'name' => 'preg:/[^<]+/',
'id' => 'preg:/[^<]+/', 'class' => 'form-error'
),
array('div' => array('class' => 'error-message')),
'You must have a last name',
'/div',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testInput method
*
* Test various incarnations of input().
*
* @access public
* @return void
*/
function testInput() {
$result = $this->Form->input('ValidateUser.balance');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'Balance',
'/label',
'input' => array('name', 'type' => 'text', 'maxlength' => 8, 'id'),
'/div',
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.email', array('id' => 'custom'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'custom'),
'Email',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'data[Contact][email]',
'id' => 'custom', 'maxlength' => 255
)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
$expected = array(
'<div',
'label' => array('for' => 'ContactEmail'),
'Email',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'data[Contact][email]',
'id' => 'ContactEmail', 'maxlength' => 255
)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->hidden('Contact.idontexist');
$expected = array('input' => array(
'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
'id' => 'ContactIdontexist'
));
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.email', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactEmail'),
'Email',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'data[Contact][email]',
'id' => 'ContactEmail'
)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.5.email', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'Contact5Email'),
'Email',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'data[Contact][5][email]',
'id' => 'Contact5Email'
)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.password');
$expected = array(
'div' => array('class' => 'input password'),
'label' => array('for' => 'ContactPassword'),
'Password',
'/label',
array('input' => array(
'type' => 'password', 'name' => 'data[Contact][password]',
'id' => 'ContactPassword'
)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.email', array(
'type' => 'file', 'class' => 'textbox'
));
$expected = array(
'div' => array('class' => 'input file'),
'label' => array('for' => 'ContactEmail'),
'Email',
'/label',
array('input' => array(
'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
'id' => 'ContactEmail'
)),
'/div'
);
$this->assertTags($result, $expected);
$this->Form->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
$result = $this->Form->input('Contact.phone');
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactPhone'),
'Phone',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'data[Contact][phone]',
'value' => 'Hello &amp; World &gt; weird chars',
'id' => 'ContactPhone', 'maxlength' => 255
)),
'/div'
);
$this->assertTags($result, $expected);
$this->Form->data['Model']['0']['OtherModel']['field'] = 'My value';
$result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'myId'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
'value' => 'My value', 'id' => 'myId'
),
'/div'
);
$this->assertTags($result, $expected);
unset($this->Form->data);
$this->Form->validationErrors['Model']['field'] = 'Badness!';
$result = $this->Form->input('Model.field');
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][field]',
'id' => 'ModelField', 'class' => 'form-error'
),
array('div' => array('class' => 'error-message')),
'Badness!',
'/div',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array(
'div' => false, 'error' => array('wrap' => 'span')
));
$expected = array(
'label' => array('for' => 'ModelField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][field]',
'id' => 'ModelField', 'class' => 'form-error'
),
array('span' => array('class' => 'error-message')),
'Badness!',
'/span'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array(
'div' => array('tag' => 'span'), 'error' => array('wrap' => false)
));
$expected = array(
'span' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][field]',
'id' => 'ModelField', 'class' => 'form-error'
),
'Badness!',
'/span'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy'));
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'Field',
'/label',
'input' => array(
'type' => 'text', 'name' => 'data[Model][field]',
'id' => 'ModelField', 'class' => 'form-error'
),
'A message to you, Rudy',
array('div' => array('class' => 'error-message')),
'Badness!',
'/div',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->setEntity(null);
$this->Form->setEntity('Model.field');
$result = $this->Form->input('Model.field', array(
'after' => 'A message to you, Rudy', 'error' => false
));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ModelField'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
'A message to you, Rudy',
'/div'
);
$this->assertTags($result, $expected);
unset($this->Form->validationErrors['Model']['field']);
$result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ModelField'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField'),
'A message to you, Rudy',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->validationErrors['Model']['field'] = 'minLength';
$result = $this->Form->input('Model.field', array(
'error' => array(
'minLength' => 'Le login doit contenir au moins 2 caractères',
'maxLength' => 'login too large'
)
));
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
array('div' => array('class' => 'error-message')),
'Le login doit contenir au moins 2 caractères',
'/div',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->validationErrors['Model']['field'] = 'maxLength';
$result = $this->Form->input('Model.field', array(
'error' => array(
'wrap' => 'span',
'attributes' => array('rel' => 'fake'),
'minLength' => 'Le login doit contenir au moins 2 caractères',
'maxLength' => 'login too large',
)
));
$expected = array(
'div' => array('class' => 'input text error'),
'label' => array('for' => 'ModelField'),
'Field',
'/label',
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
array('span' => array('class' => 'error-message', 'rel' => 'fake')),
'login too large',
'/span',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* test input() with checkbox creation
*
* @return void
*/
function testInputCheckbox() {
$result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
$expected = array(
'div' => array('class' => 'input checkbox'),
'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
$expected = array(
'div' => array('class' => 'input checkbox'),
'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
$expected = array(
'div' => array('class' => 'input checkbox'),
'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* test form->input() with datetime, date and time types
*
* @return void
*/
function testInputDatetime() {
extract($this->dateRegex);
$result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
$result = explode(':', $result);
$this->assertPattern('/option value="23"/', $result[0]);
$this->assertNoPattern('/option value="24"/', $result[0]);
$result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
$result = explode(':', $result);
$this->assertPattern('/option value="23"/', $result[0]);
$this->assertNoPattern('/option value="24"/', $result[0]);
$result = $this->Form->input('Model.field', array(
'type' => 'time', 'timeFormat' => 24, 'interval' => 15
));
$result = explode(':', $result);
$this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
$this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
$this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
$result = $this->Form->input('Model.field', array(
'type' => 'time', 'timeFormat' => 12, 'interval' => 15
));
$result = explode(':', $result);
$this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
$this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
$this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
$result = $this->Form->input('prueba', array(
'type' => 'time', 'timeFormat'=> 24 , 'dateFormat'=>'DMY' , 'minYear' => 2008,
'maxYear' => date('Y') + 1 ,'interval' => 15
));
$result = explode(':', $result);
$this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
$this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
$this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
$this->assertPattern('#<option value="30"[^>]*>30</option>#', $result[1]);
$result = $this->Form->input('prueba', array(
'type' => 'datetime', 'timeFormat'=> 24 , 'dateFormat'=>'DMY' , 'minYear' => 2008,
'maxYear' => date('Y') + 1 ,'interval' => 15
));
$result = explode(':', $result);
$this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
$this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
$this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
$this->assertPattern('#<option value="30"[^>]*>30</option>#', $result[1]);
//related to ticket #5013
$result = $this->Form->input('Contact.date', array(
'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
));
$this->assertPattern('/class="customClass"/', $result);
$this->assertPattern('/onChange="function\(\)\{\}"/', $result);
$result = $this->Form->input('Contact.date', array(
'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
));
$this->assertPattern('/id="customIdDay"/', $result);
$this->assertPattern('/id="customIdMonth"/', $result);
$this->assertPattern('/onChange="function\(\)\{\}"/', $result);
$result = $this->Form->input('Model.field', array(
'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
));
$this->assertPattern('/id="customIDDay"/', $result);
$this->assertPattern('/id="customIDHour"/', $result);
$result = explode('</select><select', $result);
$result = explode(':', $result[1]);
$this->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('</select><select', $result);
$result = explode(':', $result[1]);
$this->assertPattern('/option value="12"/', $result[0]);
$this->assertNoPattern('/option value="13"/', $result[0]);
$this->Form->data = array('Contact' => array('created' => null));
$result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
$expected = array(
'div' => array('class' => 'input date'),
'label' => array('for' => 'ContactCreatedMonth'),
'Created',
'/label',
array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
array('option' => array('value' => '')), 'Date Unknown', '/option',
$monthsRegex,
'/select', '-',
array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
array('option' => array('value' => '')), 'Date Unknown', '/option',
$daysRegex,
'/select', '-',
array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
array('option' => array('value' => '')), 'Date Unknown', '/option',
$yearsRegex,
'/select',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->data = array('Contact' => array('created' => null));
$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
$this->assertPattern('/for\="ContactCreatedHour"/', $result);
$this->Form->data = array('Contact' => array('created' => null));
$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
$this->assertPattern('/for\="ContactCreatedMonth"/', $result);
$result = $this->Form->input('Contact.created', array(
'type' => 'date',
'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
'timeFormat' => 'NONE'
));
$this->assertPattern('/for\="created-month"/', $result);
}
/**
* Test generating checkboxes in a loop.
*
* @return void
*/
function testInputCheckboxesInLoop() {
for ($i = 1; $i < 5; $i++) {
$result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
$expected = array(
'div' => array('class' => 'input checkbox'),
'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
'label' => array('for' => "Contact{$i}Email"),
'Email',
'/label',
'/div'
);
$this->assertTags($result, $expected);
}
}
/**
* test input name with leading integer, ensure attributes are generated correctly.
*
* @return void
*/
function testInputWithLeadingInteger() {
$result = $this->Form->text('0.Node.title');
$expected = array(
'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
);
$this->assertTags($result, $expected);
}
/**
* test form->input() with select type inputs.
*
* @return void
*/
function testInputSelectType() {
$result = $this->Form->input('email', array(
'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
);
$expected = array(
'div' => array('class' => 'input select'),
'label' => array('for' => 'email'),
'Email',
'/label',
array('select' => array('name' => 'data[email]', 'id' => 'email')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => 'è')),
'Firést',
'/option',
array('option' => array('value' => 'é')),
'Secoènd',
'/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('email', array(
'options' => array('First', 'Second'), 'empty' => true)
);
$expected = array(
'div' => array('class' => 'input select'),
'label' => array('for' => 'email'),
'Email',
'/label',
array('select' => array('name' => 'data[email]', 'id' => 'email')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '0')),
'First',
'/option',
array('option' => array('value' => '1')),
'Second',
'/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
$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));
$expected = array(
'div' => array('class' => 'input select'),
'label' => array('for' => 'ModelUserId'),
'User',
'/label',
'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => 'value', 'selected' => 'selected')),
'good',
'/option',
array('option' => array('value' => 'other')),
'bad',
'/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->data = array('Model' => array('user_id' => null));
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('empty' => 'Some Empty'));
$expected = array(
'div' => array('class' => 'input select'),
'label' => array('for' => 'ModelUserId'),
'User',
'/label',
'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
array('option' => array('value' => '')),
'Some Empty',
'/option',
array('option' => array('value' => 'value')),
'good',
'/option',
array('option' => array('value' => 'other')),
'bad',
'/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
$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' => 'Some Empty'));
$expected = array(
'div' => array('class' => 'input select'),
'label' => array('for' => 'ModelUserId'),
'User',
'/label',
'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
array('option' => array('value' => '')),
'Some Empty',
'/option',
array('option' => array('value' => 'value', 'selected' => 'selected')),
'good',
'/option',
array('option' => array('value' => 'other')),
'bad',
'/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->data = array('User' => array('User' => array('value')));
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('User.User', array('empty' => true));
$expected = array(
'div' => array('class' => 'input select'),
'label' => array('for' => 'UserUser'),
'User',
'/label',
'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => 'value', 'selected' => 'selected')),
'good',
'/option',
array('option' => array('value' => 'other')),
'bad',
'/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->data = array();
$result = $this->Form->input('Publisher.id', array(
'label' => 'Publisher',
'type' => 'select',
'multiple' => 'checkbox',
'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
));
$expected = array(
array('div' => array('class' => 'input select')),
array('label' => array('for' => 'PublisherId')),
'Publisher',
'/label',
'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
array('label' => array('for' => 'PublisherIdValue1')),
'Label 1',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
array('label' => array('for' => 'PublisherIdValue2')),
'Label 2',
'/label',
'/div',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* test that input() and a non standard primary key makes a hidden input by default.
*
* @return void
*/
function testInputWithNonStandardPrimaryKeyMakesHidden() {
$this->Form->create('User');
$this->Form->fieldset = array(
'User' => array(
'fields' => array(
'model_id' => array('type' => 'integer')
),
'validates' => array(),
'key' => 'model_id'
)
);
$result = $this->Form->input('model_id');
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
);
$this->assertTags($result, $expected);
}
/**
* test that overriding the magic select type widget is possible
*
* @return void
*/
function testInputOverridingMagicSelectType() {
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ModelUserId'), 'User', '/label',
'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
'/div'
);
$this->assertTags($result, $expected);
//Check that magic types still work for plural/singular vars
$view =& ClassRegistry::getObject('view');
$view->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.type');
$expected = array(
'div' => array('class' => 'input select'),
'label' => array('for' => 'ModelType'), 'Type', '/label',
'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
array('option' => array('value' => 'value')), 'good', '/option',
array('option' => array('value' => 'other')), 'bad', '/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* Test that magic input() selects can easily be converted into radio types without error.
*
* @return void
*/
function testInputMagicSelectChangeToRadio() {
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('type' => 'radio'));
$this->assertPattern('/input type="radio"/', $result);
}
/**
* fields with the same name as the model should work.
*
* @return void
*/
function testInputWithMatchingFieldAndModelName() {
$this->Form->create('User');
$this->Form->fieldset = array(
'User' => array(
'fields' => array(
'User' => array('type' => 'text')
),
'validates' => array(),
'key' => 'id'
)
);
$this->Form->data['User']['User'] = 'ABC, Inc.';
$result = $this->Form->input('User', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'UserUser'), 'User', '/label',
'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormInputs method
*
* test correct results from form::inputs().
*
* @access public
* @return void
*/
function testFormInputs() {
$this->Form->create('Contact');
$result = $this->Form->inputs('The Legend');
$expected = array(
'<fieldset',
'<legend',
'The Legend',
'/legend',
'*/fieldset',
);
$this->assertTags($result, $expected);
$result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
$expected = array(
'fieldset' => array('class' => 'classy-stuff'),
'<legend',
'Field of Dreams',
'/legend',
'*/fieldset'
);
$this->assertTags($result, $expected);
$View = ClassRegistry::getObject('view');
$this->Form->create('Contact');
$this->Form->params['prefix'] = 'admin';
$this->Form->action = 'admin_edit';
$result = $this->Form->inputs();
$expected = array(
'<fieldset',
'<legend',
'Edit Contact',
'/legend',
'*/fieldset',
);
$this->assertTags($result, $expected);
$this->Form->create('Contact');
$result = $this->Form->inputs(false);
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input password')),
'*/div',
array('div' => array('class' => 'input date')),
'*/div',
array('div' => array('class' => 'input date')),
'*/div',
array('div' => array('class' => 'input datetime')),
'*/div',
array('div' => array('class' => 'input select')),
'*/div',
);
$this->assertTags($result, $expected);
$this->Form->create('Contact');
$result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input password')),
'*/div',
array('div' => array('class' => 'input date')),
'*/div',
array('div' => array('class' => 'input date')),
'*/div',
array('div' => array('class' => 'input datetime')),
'*/div',
array('div' => array('class' => 'input select')),
'*/div',
);
$this->assertTags($result, $expected);
$this->Form->create('Contact');
$result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
$expected = array(
'fieldset' => array(),
'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input text')),
'*/div',
array('div' => array('class' => 'input password