* Copyright 2006-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The Open Group Test Suite License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2006-2008, Cake Software Foundation, Inc. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers * @since CakePHP(tm) v 1.2.0.4206 * @version $Revision$ * @modifiedby $LastChangedBy$ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { define('CAKEPHP_UNIT_TEST_EXECUTION', 1); } uses('view'.DS.'helpers'.DS.'app_helper', 'class_registry', 'controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'html', 'view'.DS.'view', 'view'.DS.'helpers'.DS.'form'); class ContactTestController extends Controller { var $name = 'ContactTest'; var $uses = null; } class Contact extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'Contact'; var $validate = array('non_existing' => array(), 'idontexist' => array(), 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } var $hasAndBelongsToMany = array('ContactTag' => array()); } Class ContactNonStandardPk extends Contact { var $primaryKey = 'pk'; var $name = 'ContactNonStandardPk'; function schema() { $this->_schema = parent::schema(); $this->_schema['pk'] = $this->_schema['id']; unset($this->_schema['id']); return $this->_schema; } } class ContactTag extends Model { var $useTable = false; function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'), 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''), 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null) ); return $this->_schema; } } class UserForm extends CakeTestModel { var $useTable = false; var $primaryKey = 'id'; var $name = 'UserForm'; var $hasMany = array('OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null), 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null), 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255), 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } } class OpenidUrl extends CakeTestModel { var $useTable = false; var $primaryKey = 'id'; var $name = 'OpenidUrl'; var $belongsTo = array('UserForm' => array('className' => 'UserForm', 'foreignKey' => 'user_form_id')); var $validate = array('openid_not_registered' => array()); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'user_form_id' => array('type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'), 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), ); return $this->_schema; } function beforeValidate() { $this->invalidate('openid_not_registered'); return true; } } class ValidateUser extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateUser'; var $hasOne = array('ValidateProfile' => array('className' => 'ValidateProfile', 'foreignKey' => 'user_id')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } function beforeValidate() { $this->invalidate('email'); return false; } } class ValidateProfile extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateProfile'; var $hasOne = array('ValidateItem' => array('className' => 'ValidateItem', 'foreignKey' => 'profile_id')); var $belongsTo = array('ValidateUser' => array('className' => 'ValidateUser', 'foreignKey' => 'user_id')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } function beforeValidate() { $this->invalidate('full_name'); $this->invalidate('city'); return false; } } class ValidateItem extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateItem'; var $belongsTo = array('ValidateProfile' => array('className' => 'ValidateProfile', 'foreignKey' => 'profile_id')); function schema() { $this->_schema = array( 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'description' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); return $this->_schema; } function beforeValidate() { $this->invalidate('description'); return false; } } class TestMail extends CakeTestModel { var $primaryKey = 'id'; var $useTable = false; var $name = 'TestMail'; } /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class FormHelperTest extends CakeTestCase { var $fixtures = array(null); function setUp() { parent::setUp(); Router::reload(); $this->Form =& new FormHelper(); $this->Form->Html =& new HtmlHelper(); $this->Controller =& new ContactTestController(); $this->View =& new View($this->Controller); ClassRegistry::addObject('view', $view); ClassRegistry::addObject('Contact', new Contact()); ClassRegistry::addObject('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()); } 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); } 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'))); $this->assertPattern('/'.$expected.'/', $result); $this->assertPattern('/input type="hidden" name="data\[__Token\]\[fields\]" value="'.$expected.'"/', $result); } 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'))); $this->assertPattern('/'.$expected.'/', $result); $this->assertPattern('/input type="hidden" name="data\[__Token\]\[fields\]" value="'.$expected.'"/', $result); } 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'))); $this->assertPattern('/'.$expected.'/', $result); $this->assertPattern('/input type="hidden" name="data\[__Token\]\[fields\]" value="'.$expected.'"/', $result); } 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'))); $this->assertPattern('/'.$expected.'/', $result); $this->assertPattern('/input type="hidden" name="data\[__Token\]\[fields\]" value="'.$expected.'"/', $result); } 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'))); $this->assertPattern('/'.$expected.'/', $result); $this->assertPattern('/input type="hidden" name="data\[__Token\]\[fields\]" value="'.$expected.'"/', $result); } function testFormSecuredInput() { $fields = array( 'UserForm' => array('0' => 'published', '1' => 'other', '2' => 'something'), '_UserForm' => array('stuff' => '', 'something' => '0'), '__Token' => array('key' => 'testKey' )); $fields = $this->__sortFields($fields); $fieldsKey = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $fields['__Token']['fields'] = $fieldsKey; $this->Form->params['_Token']['key'] = 'testKey'; $result = $this->Form->create('Contact', array('url' => '/contacts/add')); $expected = '/^