* 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);
}
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), 'required' => true),
'imalsorequired' => array('rule' => 'alphaNumeric', 'required' => true),
'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric'),
'imalsonotrequired' => array('alpha' => array('rule' => 'alphaNumeric','required' => false),
'between' => array('rule' => array('between', 5, 30))));
/**
* schema method
*
* @access public
* @return void
*/
function setSchema($schema) {
$this->_schema = $schema;
}
/**
* hasAndBelongsToMany property
*
* @var array
* @access public
*/
var $hasAndBelongsToMany = array('ContactTag' => array());
}
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 method
*
* @access public
* @return void
*/
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;
}
}
/**
* 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 method
*
* @access public
* @return void
*/
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;
}
}
/**
* 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
*
* @access public
* @return void
*/
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;
}
/**
* 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
*
* @access public
* @return void
*/
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'),
'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)
);
return $this->_schema;
}
/**
* 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;
/**
* 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'));
/**
* schema method
*
* @access public
* @return void
*/
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;
}
/**
* 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';
/**
* belongsTo property
*
* @var array
* @access public
*/
var $belongsTo = array('ValidateProfile' => array('className' => 'ValidateProfile', 'foreignKey' => 'profile_id'));
/**
* schema method
*
* @access public
* @return void
*/
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;
}
/**
* 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';
}
/**
* Short description for class.
*
* @package cake.tests
* @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);
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());
}
/**
* testFormCreateWithSecurity method
*
* @access public
* @return void
*/
function testFormCreateWithSecurity() {
$this->Form->params['_Token'] = array('key' => 'testKey');
$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/contacts/add'),
'fieldset' => 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')),
'/fieldset'
);
$this->assertTags($result, $expected);
$result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
$expected['form']['id'] = 'MyForm';
$this->assertTags($result, $expected);
}
/**
* testFormSecurityFields method
*
* @access public
* @return void
*/
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);
$fields = $this->__sortFields($fields);
$expected = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
$expected = array(
'fieldset' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => 'data[__Token][fields]', 'value' => $expected, 'id' => 'preg:/TokenFields\d+/'),
'/fieldset'
);
$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]', 'value' => '', 'id' => 'ContactFoo')),
'/div'
);
}
/**
* testFormSecurityMultipleFields method
*
* @access public
* @return void
*/
function testFormSecurityMultipleFields() {
$key = 'testKey';
$fields = array(
'Model' => array(
0 => array('username', 'password', 'valid'),
1 => array('username', 'password', 'valid')),
'_Model' => array(
0 => array('hidden' => 'value', 'valid' => '0'),
1 => array('hidden' => 'value', 'valid' => '0')),
'__Token' => array('key' => $key));
$this->Form->params['_Token']['key'] = $key;
$result = $this->Form->secure($fields);
$fields = $this->__sortFields($fields);
$expected = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
$expected = array(
'fieldset' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => 'data[__Token][fields]', 'value' => $expected, 'id' => 'preg:/TokenFields\d+/'),
'/fieldset'
);
$this->assertTags($result, $expected);
}
/**
* testFormSecurityMultipleInputFields method
*
* @access public
* @return void
*/
function testFormSecurityMultipleInputFields() {
$key = 'testKey';
$this->Form->params['_Token']['key'] = $key;
$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');
$fields = array(
'Addresses' => array(
0 => array('title', 'first_name', 'last_name', 'address', 'city', 'phone'),
1 => array('title', 'first_name', 'last_name', 'address', 'city', 'phone')),
'_Addresses' => array(
0 => array('id' => '123456'),
1 => array('id' => '654321')),
'__Token' => array('key' => $key));
$fields = $this->__sortFields($fields);
$result = $this->Form->secure($this->Form->fields);
$expected = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
$expected = array(
'fieldset' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => 'data[__Token][fields]', 'value' => $expected, 'id' => 'preg:/TokenFields\d+/'),
'/fieldset'
);
$this->assertTags($result, $expected);
}
/**
* testFormSecurityMultipleInputDisabledFields method
*
* @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');
$fields = array(
'Addresses' => array(
0 => array('title', 'last_name', 'city', 'phone'),
1 => array('title', 'last_name', 'city', 'phone')),
'_Addresses' => array(
0 => array('id' => '123456'),
1 => array('id' => '654321')),
'__Token' => array('key' => $key));
$fields = $this->__sortFields($fields);
$result = $this->Form->secure($this->Form->fields);
$expected = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
$expected = array(
'fieldset' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => 'data[__Token][fields]', 'value' => $expected, 'id' => 'preg:/TokenFields\d+/'),
'/fieldset'
);
$this->assertTags($result, $expected);
}
/**
* testFormSecurityInputDisabledFields method
*
* @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');
$fields = array(
'Addresses' => array('title', 'last_name', 'city', 'phone'),
'_Addresses' => array('id' => '123456'),
'__Token' => array('key' => $key));
$fields = $this->__sortFields($fields);
$result = $this->Form->secure($this->Form->fields);
$expected = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
$expected = array(
'fieldset' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => 'data[__Token][fields]', 'value' => $expected, 'id' => 'preg:/TokenFields\d+/'),
'/fieldset'
);
$this->assertTags($result, $expected);
}
/**
* testFormSecuredInput method
*
* @access public
* @return void
*/
function testFormSecuredInput() {
$fields = array(
'UserForm' => array('0' => 'published', '1' => 'other', '2' => 'hidden', '3' => 'something'),
'_UserForm' => array('stuff' => '', 'hidden' => '0', '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 = array(
'form' => array('method' => 'post', 'action' => '/contacts/add'),
'fieldset' => 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+/')),
'/fieldset'
);
$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]', 'value' => '', '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]', 'value' => '', 'id' => 'UserFormOther')),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->hidden('UserForm.stuff');
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[_UserForm][stuff]', 'value' => '', '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->secure($this->Form->fields);
$expected = array(
'fieldset' => array('style' => 'display:none;'),
array('input' => array('type' => 'hidden', 'name' => 'data[__Token][fields]', 'value' => $fieldsKey, 'id' => 'preg:/TokenFields\d+/')),
'/fieldset'
);
$this->assertTags($result, $expected);
$result = $this->Form->fields;
$result = $this->__sortFields($result);
$this->assertEqual($result, $fields);
}
/**
* testPasswordValidation method
*
* @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]', 'value' => '', 'id' => 'ContactPassword', 'class' => 'form-error'),
array('div' => array('class' => 'error-message')),
'Please provide a password',
'/div',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormValidationAssociated method
*
* @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'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/user_forms/login/', 'id' => 'UserFormLoginForm'),
'fieldset' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/fieldset'
);
$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);
unset($this->UserForm);
}
/**
* testFormValidationAssociatedFirstLevel method
*
* @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'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/validate_users/add/', 'id'),
'fieldset' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/fieldset'
);
$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
*
* @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'));
$expected = array(
'form' => array('method' => 'post', 'action' => '/validate_users/add/', 'id'),
'fieldset' => array('style' => 'display:none;'),
'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
'/fieldset'
);
$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
*
* @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', 'value' => '', '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', 'value' => '', 'id', 'class' => 'form-error'),
array('div' => array('class' => 'error-message')),
'You must provide a URL',
'/div',
'/div'
);
}
/**
* testMultipleInputValidation method
*
* @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', 'value' => '', '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', 'value' => '', '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', 'value' => '', 'id', 'class' => 'form-error'),
'/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:/[^<]+/', 'value' => '', 'id' => 'preg:/[^<]+/', 'class' => 'form-error'),
array('div' => array('class' => 'error-message')),
'You must have a last name',
'/div',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* testFormInput method
*
* @access public
* @return void
*/
function testFormInput() {
$result = $this->Form->input('ValidateUser.balance');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'Balance',
'/label',
'input' => array('name', 'type' => 'text', 'maxlength' => 8, 'value' => '', '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]', 'value' => '', 'id' => 'custom', 'maxlength' => 255)),
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->hidden('Contact/idontexist');
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Contact][idontexist]', 'value' => '', '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]', 'value' => '', '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]', 'value' => '', '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]', 'value' => '', 'id' => 'ContactPassword')),
'/div'
);
$this->assertTags($result, $expected);
$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);
$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', 'value' => '', 'id' => 'ContactEmail')),
'/div'
);
$this->assertTags($result, $expected);
$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' => 12));
$result = explode(':', $result);
$this->assertPattern('/option value="12"/', $result[0]);
$this->assertNoPattern('/option value="13"/', $result[0]);
//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('