PageRenderTime 517ms CodeModel.GetById 222ms app.highlight 187ms RepoModel.GetById 54ms app.codeStats 4ms

/lib/Cake/Test/Case/View/Helper/FormHelperTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 7623 lines | 5950 code | 591 blank | 1082 comment | 1 complexity | f3178e7bffb0c509e1d08f66860afefe MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/**
   3 * FormHelperTest file
   4 *
   5 * PHP 5
   6 *
   7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
   8 * Copyright 2005-2011, Cake Software Foundation, Inc.
   9 *
  10 * Licensed under The MIT License
  11 * Redistributions of files must retain the above copyright notice
  12 *
  13 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc.
  14 * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15 * @package       Cake.Test.Case.View.Helper
  16 * @since         CakePHP(tm) v 1.2.0.4206
  17 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  18 */
  19App::uses('ClassRegistry', 'Utility');
  20App::uses('Controller', 'Controller');
  21App::uses('View', 'View');
  22App::uses('Model', 'Model');
  23App::uses('Security', 'Utility');
  24App::uses('CakeRequest', 'Network');
  25App::uses('HtmlHelper', 'View/Helper');
  26App::uses('FormHelper', 'View/Helper');
  27App::uses('Router', 'Routing');
  28
  29/**
  30 * ContactTestController class
  31 *
  32 * @package	   cake
  33 * @package       Cake.Test.Case.View.Helper
  34 */
  35class ContactTestController extends Controller {
  36
  37/**
  38 * name property
  39 *
  40 * @var string 'ContactTest'
  41 */
  42	public $name = 'ContactTest';
  43
  44/**
  45 * uses property
  46 *
  47 * @var mixed null
  48 */
  49	public $uses = null;
  50}
  51
  52/**
  53 * Contact class
  54 *
  55 * @package	   cake
  56 * @package       Cake.Test.Case.View.Helper
  57 */
  58class Contact extends CakeTestModel {
  59
  60/**
  61 * primaryKey property
  62 *
  63 * @var string 'id'
  64 */
  65	public $primaryKey = 'id';
  66
  67/**
  68 * useTable property
  69 *
  70 * @var bool false
  71 */
  72	public $useTable = false;
  73
  74/**
  75 * name property
  76 *
  77 * @var string 'Contact'
  78 */
  79	public $name = 'Contact';
  80
  81/**
  82 * Default schema
  83 *
  84 * @var array
  85 */
  86	protected $_schema = array(
  87		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  88		'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  89		'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  90		'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  91		'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  92		'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  93		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  94		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  95		'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  96	);
  97
  98/**
  99 * validate property
 100 *
 101 * @var array
 102 */
 103	public $validate = array(
 104		'non_existing' => array(),
 105		'idontexist' => array(),
 106		'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
 107		'string_required' => 'notEmpty',
 108		'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
 109		'imrequiredtoo' => array('rule' => 'notEmpty'),
 110		'required_one' => array('required' => array('rule' => array('notEmpty'))),
 111		'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
 112		'imalsonotrequired' => array(
 113			'alpha' => array('rule' => 'alphaNumeric','allowEmpty' => true),
 114			'between' => array('rule' => array('between', 5, 30)),
 115		),
 116		'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
 117	);
 118
 119/**
 120 * schema method
 121 *
 122 * @return void
 123 */
 124	public function setSchema($schema) {
 125		$this->_schema = $schema;
 126	}
 127
 128/**
 129 * hasAndBelongsToMany property
 130 *
 131 * @var array
 132 */
 133	public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
 134
 135/**
 136 * hasAndBelongsToMany property
 137 *
 138 * @var array
 139 */
 140	public $belongsTo = array('User' => array('className' => 'UserForm'));
 141}
 142
 143/**
 144 * ContactTagsContact class
 145 *
 146 * @package	   cake
 147 * @package       Cake.Test.Case.View.Helper
 148 */
 149class ContactTagsContact extends CakeTestModel {
 150
 151/**
 152 * useTable property
 153 *
 154 * @var bool false
 155 */
 156	public $useTable = false;
 157
 158/**
 159 * name property
 160 *
 161 * @var string 'Contact'
 162 */
 163	public $name = 'ContactTagsContact';
 164
 165/**
 166 * Default schema
 167 *
 168 * @var array
 169 */
 170	protected $_schema = array(
 171		'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
 172		'contact_tag_id' => array(
 173			'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
 174		)
 175	);
 176
 177/**
 178 * schema method
 179 *
 180 * @return void
 181 */
 182	public function setSchema($schema) {
 183		$this->_schema = $schema;
 184	}
 185}
 186
 187/**
 188 * ContactNonStandardPk class
 189 *
 190 * @package	   cake
 191 * @package       Cake.Test.Case.View.Helper
 192 */
 193class ContactNonStandardPk extends Contact {
 194
 195/**
 196 * primaryKey property
 197 *
 198 * @var string 'pk'
 199 */
 200	public $primaryKey = 'pk';
 201
 202/**
 203 * name property
 204 *
 205 * @var string 'ContactNonStandardPk'
 206 */
 207	public $name = 'ContactNonStandardPk';
 208
 209/**
 210 * schema method
 211 *
 212 * @return void
 213 */
 214	public function schema($field = false) {
 215		$this->_schema = parent::schema();
 216		$this->_schema['pk'] = $this->_schema['id'];
 217		unset($this->_schema['id']);
 218		return $this->_schema;
 219	}
 220}
 221
 222/**
 223 * ContactTag class
 224 *
 225 * @package	   cake
 226 * @package       Cake.Test.Case.View.Helper
 227 */
 228class ContactTag extends Model {
 229
 230/**
 231 * useTable property
 232 *
 233 * @var bool false
 234 */
 235	public $useTable = false;
 236
 237/**
 238 * schema definition
 239 *
 240 * @var array
 241 */
 242	protected $_schema = array(
 243		'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
 244		'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
 245		'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
 246		'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
 247	);
 248}
 249
 250/**
 251 * UserForm class
 252 *
 253 * @package	   cake
 254 * @package       Cake.Test.Case.View.Helper
 255 */
 256class UserForm extends CakeTestModel {
 257
 258/**
 259 * useTable property
 260 *
 261 * @var bool false
 262 */
 263	public $useTable = false;
 264
 265/**
 266 * primaryKey property
 267 *
 268 * @var string 'id'
 269 */
 270	public $primaryKey = 'id';
 271
 272/**
 273 * name property
 274 *
 275 * @var string 'UserForm'
 276 */
 277	public $name = 'UserForm';
 278
 279/**
 280 * hasMany property
 281 *
 282 * @var array
 283 */
 284	public $hasMany = array(
 285		'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
 286	));
 287
 288/**
 289 * schema definition
 290 *
 291 * @var array
 292 */
 293	protected $_schema = array(
 294		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
 295		'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
 296		'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
 297		'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
 298		'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
 299		'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
 300		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
 301		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
 302	);
 303}
 304
 305/**
 306 * OpenidUrl class
 307 *
 308 * @package	   cake
 309 * @package       Cake.Test.Case.View.Helper
 310 */
 311class OpenidUrl extends CakeTestModel {
 312
 313/**
 314 * useTable property
 315 *
 316 * @var bool false
 317 */
 318	public $useTable = false;
 319
 320/**
 321 * primaryKey property
 322 *
 323 * @var string 'id'
 324 */
 325	public $primaryKey = 'id';
 326
 327/**
 328 * name property
 329 *
 330 * @var string 'OpenidUrl'
 331 */
 332	public $name = 'OpenidUrl';
 333
 334/**
 335 * belongsTo property
 336 *
 337 * @var array
 338 */
 339	public $belongsTo = array('UserForm' => array(
 340		'className' => 'UserForm', 'foreignKey' => 'user_form_id'
 341	));
 342
 343/**
 344 * validate property
 345 *
 346 * @var array
 347 */
 348	public $validate = array('openid_not_registered' => array());
 349
 350/**
 351 * schema method
 352 *
 353 * @var array
 354 */
 355	protected $_schema = array(
 356		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
 357		'user_form_id' => array(
 358			'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
 359		),
 360		'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
 361	);
 362
 363/**
 364 * beforeValidate method
 365 *
 366 * @return void
 367 */
 368	public function beforeValidate($options = array()) {
 369		$this->invalidate('openid_not_registered');
 370		return true;
 371	}
 372}
 373
 374/**
 375 * ValidateUser class
 376 *
 377 * @package	   cake
 378 * @package       Cake.Test.Case.View.Helper
 379 */
 380class ValidateUser extends CakeTestModel {
 381
 382/**
 383 * primaryKey property
 384 *
 385 * @var string 'id'
 386 */
 387	public $primaryKey = 'id';
 388
 389/**
 390 * useTable property
 391 *
 392 * @var bool false
 393 */
 394	public $useTable = false;
 395
 396/**
 397 * name property
 398 *
 399 * @var string 'ValidateUser'
 400 */
 401	public $name = 'ValidateUser';
 402
 403/**
 404 * hasOne property
 405 *
 406 * @var array
 407 */
 408	public $hasOne = array('ValidateProfile' => array(
 409		'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
 410	));
 411
 412/**
 413 * schema method
 414 *
 415 * @var array
 416 */
 417	protected $_schema = array(
 418		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
 419		'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
 420		'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
 421		'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
 422		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
 423		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
 424	);
 425
 426/**
 427 * beforeValidate method
 428 *
 429 * @return void
 430 */
 431	public function beforeValidate($options = array()) {
 432		$this->invalidate('email');
 433		return false;
 434	}
 435}
 436
 437/**
 438 * ValidateProfile class
 439 *
 440 * @package	   cake
 441 * @package       Cake.Test.Case.View.Helper
 442 */
 443class ValidateProfile extends CakeTestModel {
 444
 445/**
 446 * primaryKey property
 447 *
 448 * @var string 'id'
 449 */
 450	public $primaryKey = 'id';
 451
 452/**
 453 * useTable property
 454 *
 455 * @var bool false
 456 */
 457	public $useTable = false;
 458
 459/**
 460 * schema property
 461 *
 462 * @var array
 463 */
 464	protected $_schema = array(
 465		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
 466		'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
 467		'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
 468		'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
 469		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
 470		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
 471	);
 472
 473/**
 474 * name property
 475 *
 476 * @var string 'ValidateProfile'
 477 */
 478	public $name = 'ValidateProfile';
 479
 480/**
 481 * hasOne property
 482 *
 483 * @var array
 484 */
 485	public $hasOne = array('ValidateItem' => array(
 486		'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
 487	));
 488
 489/**
 490 * belongsTo property
 491 *
 492 * @var array
 493 */
 494	public $belongsTo = array('ValidateUser' => array(
 495		'className' => 'ValidateUser', 'foreignKey' => 'user_id'
 496	));
 497
 498/**
 499 * beforeValidate method
 500 *
 501 * @return void
 502 */
 503	public function beforeValidate($options = array()) {
 504		$this->invalidate('full_name');
 505		$this->invalidate('city');
 506		return false;
 507	}
 508}
 509
 510/**
 511 * ValidateItem class
 512 *
 513 * @package	   cake
 514 * @package       Cake.Test.Case.View.Helper
 515 */
 516class ValidateItem extends CakeTestModel {
 517
 518/**
 519 * primaryKey property
 520 *
 521 * @var string 'id'
 522 */
 523	public $primaryKey = 'id';
 524
 525/**
 526 * useTable property
 527 *
 528 * @var bool false
 529 */
 530	public $useTable = false;
 531
 532/**
 533 * name property
 534 *
 535 * @var string 'ValidateItem'
 536 */
 537	public $name = 'ValidateItem';
 538
 539/**
 540 * schema property
 541 *
 542 * @var array
 543 */
 544	protected $_schema = array(
 545		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
 546		'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
 547		'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
 548		'description' => array(
 549			'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
 550		),
 551		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
 552		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
 553	);
 554
 555/**
 556 * belongsTo property
 557 *
 558 * @var array
 559 */
 560	public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
 561
 562/**
 563 * beforeValidate method
 564 *
 565 * @return void
 566 */
 567	public function beforeValidate($options = array()) {
 568		$this->invalidate('description');
 569		return false;
 570	}
 571}
 572
 573/**
 574 * TestMail class
 575 *
 576 * @package	   cake
 577 * @package       Cake.Test.Case.View.Helper
 578 */
 579class TestMail extends CakeTestModel {
 580
 581/**
 582 * primaryKey property
 583 *
 584 * @var string 'id'
 585 */
 586	public $primaryKey = 'id';
 587
 588/**
 589 * useTable property
 590 *
 591 * @var bool false
 592 */
 593	public $useTable = false;
 594
 595/**
 596 * name property
 597 *
 598 * @var string 'TestMail'
 599 */
 600	public $name = 'TestMail';
 601}
 602
 603/**
 604 * FormHelperTest class
 605 *
 606 * @package	   cake
 607 * @package       Cake.Test.Case.View.Helper
 608 */
 609class FormHelperTest extends CakeTestCase {
 610
 611/**
 612 * Fixtures to be used
 613 *
 614 * @var array
 615 */
 616	public $fixtures = array('core.post');
 617
 618/**
 619 * Do not load the fixtures by default
 620 *
 621 * @var boolean
 622 */
 623	public $autoFixtures = false;
 624
 625/**
 626 * setUp method
 627 *
 628 * @return void
 629 */
 630	public function setUp() {
 631		parent::setUp();
 632
 633		Configure::write('App.base', '');
 634		$this->Controller = new ContactTestController();
 635		$this->View = new View($this->Controller);
 636
 637		$this->Form = new FormHelper($this->View);
 638		$this->Form->Html = new HtmlHelper($this->View);
 639		$this->Form->request = new CakeRequest('contacts/add', false);
 640		$this->Form->request->here = '/contacts/add';
 641		$this->Form->request['action'] = 'add';
 642		$this->Form->request->webroot = '';
 643		$this->Form->request->base = '';
 644
 645		ClassRegistry::addObject('Contact', new Contact());
 646		ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
 647		ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
 648		ClassRegistry::addObject('User', new UserForm());
 649		ClassRegistry::addObject('ValidateItem', new ValidateItem());
 650		ClassRegistry::addObject('ValidateUser', new ValidateUser());
 651		ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
 652
 653		$this->oldSalt = Configure::read('Security.salt');
 654
 655		$this->dateRegex = array(
 656			'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
 657			'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
 658			'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
 659			'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
 660			'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
 661			'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
 662		);
 663
 664		Configure::write('Security.salt', 'foo!');
 665	}
 666
 667/**
 668 * tearDown method
 669 *
 670 * @return void
 671 */
 672	public function tearDown() {
 673		parent::tearDown();
 674		unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
 675		Configure::write('Security.salt', $this->oldSalt);
 676	}
 677
 678
 679
 680/**
 681 * testFormCreateWithSecurity method
 682 *
 683 * Test form->create() with security key.
 684 *
 685 * @return void
 686 */
 687	public function testCreateWithSecurity() {
 688		$this->Form->request['_Token'] = array('key' => 'testKey');
 689		$encoding = strtolower(Configure::read('App.encoding'));
 690		$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
 691		$expected = array(
 692			'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
 693			'div' => array('style' => 'display:none;'),
 694			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
 695			array('input' => array(
 696				'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
 697			)),
 698			'/div'
 699		);
 700		$this->assertTags($result, $expected);
 701
 702		$result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
 703		$expected['form']['id'] = 'MyForm';
 704		$this->assertTags($result, $expected);
 705	}
 706
 707/**
 708 * test that create() clears the fields property so it starts fresh
 709 *
 710 * @return void
 711 */
 712	public function testCreateClearingFields() {
 713		$this->Form->fields = array('model_id');
 714		$this->Form->create('Contact');
 715		$this->assertEquals($this->Form->fields, array());
 716	}
 717
 718/**
 719 * Tests form hash generation with model-less data
 720 *
 721 * @return void
 722 */
 723	public function testValidateHashNoModel() {
 724		$this->Form->request['_Token'] = array('key' => 'foo');
 725		$result = $this->Form->secure(array('anything'));
 726		$this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
 727	}
 728
 729/**
 730 * Tests that models with identical field names get resolved properly
 731 *
 732 * @return void
 733 */
 734	public function testDuplicateFieldNameResolution() {
 735		$result = $this->Form->create('ValidateUser');
 736		$this->assertEquals($this->Form->entity(), array('ValidateUser'));
 737
 738		$result = $this->Form->input('ValidateItem.name');
 739		$this->assertEquals($this->Form->entity(), array('ValidateItem', 'name'));
 740
 741		$result = $this->Form->input('ValidateUser.name');
 742		$this->assertEquals($this->Form->entity(), array('ValidateUser', 'name'));
 743		$this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
 744		$this->assertRegExp('/type="text"/', $result);
 745
 746		$result = $this->Form->input('ValidateItem.name');
 747		$this->assertEquals($this->Form->entity(), array('ValidateItem', 'name'));
 748		$this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
 749		$this->assertRegExp('/<textarea/', $result);
 750
 751		$result = $this->Form->input('name');
 752		$this->assertEquals($this->Form->entity(), array('ValidateUser', 'name'));
 753		$this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
 754		$this->assertRegExp('/type="text"/', $result);
 755	}
 756
 757/**
 758 * Tests that hidden fields generated for checkboxes don't get locked
 759 *
 760 * @return void
 761 */
 762	public function testNoCheckboxLocking() {
 763		$this->Form->request['_Token'] = array('key' => 'foo');
 764		$this->assertSame($this->Form->fields, array());
 765
 766		$this->Form->checkbox('check', array('value' => '1'));
 767		$this->assertSame($this->Form->fields, array('check'));
 768	}
 769
 770/**
 771 * testFormSecurityFields method
 772 *
 773 * Test generation of secure form hash generation.
 774 *
 775 * @return void
 776 */
 777	public function testFormSecurityFields() {
 778		$key = 'testKey';
 779		$fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
 780
 781		$this->Form->request['_Token'] = array('key' => $key);
 782		$result = $this->Form->secure($fields);
 783
 784		$expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
 785		$expected .= ':' . 'Model.valid';
 786
 787		$expected = array(
 788			'div' => array('style' => 'display:none;'),
 789			array('input' => array(
 790				'type' => 'hidden', 'name' => 'data[_Token][fields]',
 791				'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
 792			)),
 793			array('input' => array(
 794				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
 795				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
 796			)),
 797			'/div'
 798		);
 799		$this->assertTags($result, $expected);
 800	}
 801
 802/**
 803 * Tests correct generation of number fields for double and float fields
 804 *
 805 * @return void
 806 */
 807	public function testTextFieldGenerationForFloats() {
 808		$model = ClassRegistry::getObject('Contact');
 809		$model->setSchema(array('foo' => array(
 810			'type' => 'float',
 811			'null' => false,
 812			'default' => null,
 813			'length' => null
 814		)));
 815
 816		$this->Form->create('Contact');
 817		$result = $this->Form->input('foo');
 818		$expected = array(
 819			'div' => array('class' => 'input number'),
 820			'label' => array('for' => 'ContactFoo'),
 821			'Foo',
 822			'/label',
 823			array('input' => array(
 824				'type' => 'number',
 825				'name' => 'data[Contact][foo]',
 826				'id' => 'ContactFoo',
 827				'step' => 'any'
 828			)),
 829			'/div'
 830		);
 831		$this->assertTags($result, $expected);
 832
 833		$result = $this->Form->input('foo', array('step' => 0.5));
 834		$expected = array(
 835			'div' => array('class' => 'input number'),
 836			'label' => array('for' => 'ContactFoo'),
 837			'Foo',
 838			'/label',
 839			array('input' => array(
 840				'type' => 'number',
 841				'name' => 'data[Contact][foo]',
 842				'id' => 'ContactFoo',
 843				'step' => '0.5'
 844			)),
 845			'/div'
 846		);
 847		$this->assertTags($result, $expected);
 848	}
 849
 850/**
 851 * Tests correct generation of number fields for integer fields
 852 *
 853 * @access public
 854 * @return void
 855 */
 856	public function testTextFieldTypeNumberGenerationForIntegers() {
 857		$model = ClassRegistry::getObject('Contact');
 858		$model->setSchema(array('foo' => array(
 859			'type' => 'integer',
 860			'null' => false,
 861			'default' => null,
 862			'length' => null
 863		)));
 864
 865		$this->Form->create('Contact');
 866		$result = $this->Form->input('foo');
 867		$expected = array(
 868			'div' => array('class' => 'input number'),
 869			'label' => array('for' => 'ContactFoo'),
 870			'Foo',
 871			'/label',
 872			array('input' => array(
 873				'type' => 'number', 'name' => 'data[Contact][foo]',
 874				'id' => 'ContactFoo'
 875			)),
 876			'/div'
 877		);
 878		$this->assertTags($result, $expected);
 879	}
 880
 881/**
 882 * testFormSecurityMultipleFields method
 883 *
 884 * Test secure() with multiple row form. Ensure hash is correct.
 885 *
 886 * @return void
 887 */
 888	public function testFormSecurityMultipleFields() {
 889		$key = 'testKey';
 890
 891		$fields = array(
 892			'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
 893			'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
 894			'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
 895		);
 896		$this->Form->request['_Token'] = array('key' => $key);
 897		$result = $this->Form->secure($fields);
 898
 899		$hash  = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
 900		$hash .= '%7CModel.1.hidden%7CModel.1.valid';
 901
 902		$expected = array(
 903			'div' => array('style' => 'display:none;'),
 904			array('input' => array(
 905				'type' => 'hidden', 'name' => 'data[_Token][fields]',
 906				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
 907			)),
 908			array('input' => array(
 909				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
 910				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
 911			)),
 912			'/div'
 913		);
 914		$this->assertTags($result, $expected);
 915	}
 916
 917/**
 918 * testFormSecurityMultipleSubmitButtons
 919 *
 920 * test form submit generation and ensure that _Token is only created on end()
 921 *
 922 * @return void
 923 */
 924	public function testFormSecurityMultipleSubmitButtons() {
 925		$key = 'testKey';
 926		$this->Form->request['_Token'] = array('key' => $key);
 927
 928		$this->Form->create('Addresses');
 929		$this->Form->input('Address.title');
 930		$this->Form->input('Address.first_name');
 931
 932		$result = $this->Form->submit('Save', array('name' => 'save'));
 933		$expected = array(
 934			'div' => array('class' => 'submit'),
 935			'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
 936			'/div',
 937		);
 938		$this->assertTags($result, $expected);
 939		$result = $this->Form->submit('Cancel', array('name' => 'cancel'));
 940		$expected = array(
 941			'div' => array('class' => 'submit'),
 942			'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
 943			'/div',
 944		);
 945		$this->assertTags($result, $expected);
 946		$result = $this->Form->end(null);
 947
 948		$expected = array(
 949			'div' => array('style' => 'display:none;'),
 950			array('input' => array(
 951				'type' => 'hidden', 'name' => 'data[_Token][fields]',
 952				'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
 953			)),
 954			array('input' => array(
 955				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
 956				'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
 957			)),
 958			'/div'
 959		);
 960		$this->assertTags($result, $expected);
 961	}
 962
 963/**
 964 * Test that buttons created with foo[bar] name attributes are unlocked correctly.
 965 *
 966 * @return void
 967 */
 968	public function testSecurityButtonNestedNamed() {
 969		$key = 'testKey';
 970		$this->Form->request['_Token'] = array('key' => $key);
 971
 972		$this->Form->create('Addresses');
 973		$this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
 974		$result = $this->Form->unlockField();
 975		$this->assertEquals(array('Address.button'), $result);
 976	}
 977
 978
 979/**
 980 * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
 981 *
 982 * @return void
 983 */
 984	public function testSecuritySubmitNestedNamed() {
 985		$key = 'testKey';
 986		$this->Form->request['_Token'] = array('key' => $key);
 987
 988		$this->Form->create('Addresses');
 989		$this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
 990		$result = $this->Form->unlockField();
 991		$this->assertEquals(array('Address.button'), $result);
 992	}
 993
 994/**
 995 * Test that the correct fields are unlocked for image submits with no names.
 996 *
 997 * @return void
 998 */
 999	public function testSecuritySubmitImageNoName() {
1000		$key = 'testKey';
1001		$this->Form->request['_Token'] = array('key' => $key);
1002
1003		$this->Form->create('User');
1004		$result = $this->Form->submit('save.png');
1005		$expected = array(
1006			'div' => array('class' => 'submit'),
1007			'input' => array('type' => 'image', 'src' => 'img/save.png'),
1008			'/div'
1009		);
1010		$this->assertTags($result, $expected);
1011		$this->assertEquals(array('x', 'y'), $this->Form->unlockField());
1012	}
1013
1014/**
1015 * Test that the correct fields are unlocked for image submits with names.
1016 *
1017 * @return void
1018 */
1019	public function testSecuritySubmitImageName() {
1020		$key = 'testKey';
1021		$this->Form->request['_Token'] = array('key' => $key);
1022
1023		$this->Form->create('User');
1024		$result = $this->Form->submit('save.png', array('name' => 'test'));
1025		$expected = array(
1026			'div' => array('class' => 'submit'),
1027			'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
1028			'/div'
1029		);
1030		$this->assertTags($result, $expected);
1031		$this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
1032	}
1033
1034/**
1035 * testFormSecurityMultipleInputFields method
1036 *
1037 * Test secure form creation with multiple row creation.  Checks hidden, text, checkbox field types
1038 *
1039 * @return void
1040 */
1041	public function testFormSecurityMultipleInputFields() {
1042		$key = 'testKey';
1043
1044		$this->Form->request['_Token'] = array('key' => $key);
1045		$this->Form->create('Addresses');
1046
1047		$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
1048		$this->Form->input('Addresses.0.title');
1049		$this->Form->input('Addresses.0.first_name');
1050		$this->Form->input('Addresses.0.last_name');
1051		$this->Form->input('Addresses.0.address');
1052		$this->Form->input('Addresses.0.city');
1053		$this->Form->input('Addresses.0.phone');
1054		$this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
1055
1056		$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1057		$this->Form->input('Addresses.1.title');
1058		$this->Form->input('Addresses.1.first_name');
1059		$this->Form->input('Addresses.1.last_name');
1060		$this->Form->input('Addresses.1.address');
1061		$this->Form->input('Addresses.1.city');
1062		$this->Form->input('Addresses.1.phone');
1063		$this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
1064
1065		$result = $this->Form->secure($this->Form->fields);
1066
1067		$hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
1068
1069		$expected = array(
1070			'div' => array('style' => 'display:none;'),
1071			array('input' => array(
1072				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1073				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1074			)),
1075			array('input' => array(
1076				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1077				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
1078			)),
1079			'/div'
1080		);
1081		$this->assertTags($result, $expected);
1082	}
1083
1084/**
1085 * Test form security with Model.field.0 style inputs
1086 *
1087 * @return void
1088 */
1089	function testFormSecurityArrayFields() {
1090		$key = 'testKey';
1091
1092		$this->Form->request->params['_Token']['key'] = $key;
1093		$this->Form->create('Address');
1094		$this->Form->input('Address.primary.1');
1095		$this->assertEquals('Address.primary', $this->Form->fields[0]);
1096	}
1097
1098/**
1099 * testFormSecurityMultipleInputDisabledFields method
1100 *
1101 * test secure form generation with multiple records and disabled fields.
1102 *
1103 * @return void
1104 */
1105	public function testFormSecurityMultipleInputDisabledFields() {
1106		$key = 'testKey';
1107		$this->Form->request->params['_Token'] = array(
1108			'key' => $key,
1109			'unlockedFields' => array('first_name', 'address')
1110		);
1111		$this->Form->create();
1112
1113		$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
1114		$this->Form->input('Addresses.0.title');
1115		$this->Form->input('Addresses.0.first_name');
1116		$this->Form->input('Addresses.0.last_name');
1117		$this->Form->input('Addresses.0.address');
1118		$this->Form->input('Addresses.0.city');
1119		$this->Form->input('Addresses.0.phone');
1120		$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1121		$this->Form->input('Addresses.1.title');
1122		$this->Form->input('Addresses.1.first_name');
1123		$this->Form->input('Addresses.1.last_name');
1124		$this->Form->input('Addresses.1.address');
1125		$this->Form->input('Addresses.1.city');
1126		$this->Form->input('Addresses.1.phone');
1127
1128		$result = $this->Form->secure($this->Form->fields);
1129		$hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
1130
1131		$expected = array(
1132			'div' => array('style' => 'display:none;'),
1133			array('input' => array(
1134				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1135				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1136			)),
1137			array('input' => array(
1138				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1139				'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
1140			)),
1141			'/div'
1142		);
1143		$this->assertTags($result, $expected);
1144	}
1145
1146/**
1147 * testFormSecurityInputDisabledFields method
1148 *
1149 * Test single record form with disabled fields.
1150 *
1151 * @return void
1152 */
1153	public function testFormSecurityInputUnlockedFields() {
1154		$key = 'testKey';
1155		$this->Form->request['_Token'] = array(
1156			'key' => $key,
1157			'unlockedFields' => array('first_name', 'address')
1158		);
1159		$this->Form->create();
1160		$this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
1161
1162		$this->Form->hidden('Addresses.id', array('value' => '123456'));
1163		$this->Form->input('Addresses.title');
1164		$this->Form->input('Addresses.first_name');
1165		$this->Form->input('Addresses.last_name');
1166		$this->Form->input('Addresses.address');
1167		$this->Form->input('Addresses.city');
1168		$this->Form->input('Addresses.phone');
1169
1170		$result = $this->Form->fields;
1171		$expected = array(
1172			'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
1173			'Addresses.city', 'Addresses.phone'
1174		);
1175		$this->assertEquals($expected, $result);
1176
1177		$result = $this->Form->secure($expected);
1178
1179		$hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
1180		$expected = array(
1181			'div' => array('style' => 'display:none;'),
1182			array('input' => array(
1183				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1184				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1185			)),
1186			array('input' => array(
1187				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1188				'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
1189			)),
1190			'/div'
1191		);
1192		$this->assertTags($result, $expected);
1193	}
1194
1195/**
1196 * test securing inputs with custom name attributes.
1197 *
1198 * @return void
1199 */
1200	public function testFormSecureWithCustomNameAttribute() {
1201		$this->Form->request->params['_Token']['key'] = 'testKey';
1202
1203		$this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
1204		$this->assertEquals('User.custom', $this->Form->fields[0]);
1205
1206		$this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
1207		$this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
1208	}
1209
1210/**
1211 * testFormSecuredInput method
1212 *
1213 * Test generation of entire secure form, assertions made on input() output.
1214 *
1215 * @return void
1216 */
1217	public function testFormSecuredInput() {
1218		$this->Form->request['_Token'] = array('key' => 'testKey');
1219
1220		$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
1221		$encoding = strtolower(Configure::read('App.encoding'));
1222		$expected = array(
1223			'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
1224			'div' => array('style' => 'display:none;'),
1225			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
1226			array('input' => array(
1227				'type' => 'hidden', 'name' => 'data[_Token][key]',
1228				'value' => 'testKey', 'id' => 'preg:/Token\d+/'
1229			)),
1230			'/div'
1231		);
1232		$this->assertTags($result, $expected);
1233
1234		$result = $this->Form->input('UserForm.published', array('type' => 'text'));
1235		$expected = array(
1236			'div' => array('class' => 'input text'),
1237			'label' => array('for' => 'UserFormPublished'),
1238			'Published',
1239			'/label',
1240			array('input' => array(
1241				'type' => 'text', 'name' => 'data[UserForm][published]',
1242				'id' => 'UserFormPublished'
1243			)),
1244			'/div'
1245		);
1246		$this->assertTags($result, $expected);
1247
1248		$result = $this->Form->input('UserForm.other', array('type' => 'text'));
1249		$expected = array(
1250			'div' => array('class' => 'input text'),
1251			'label' => array('for' => 'UserFormOther'),
1252			'Other',
1253			'/label',
1254			array('input' => array(
1255				'type' => 'text', 'name' => 'data[UserForm][other]',
1256				'id' => 'UserFormOther'
1257			)),
1258			'/div'
1259		);
1260		$this->assertTags($result, $expected);
1261
1262		$result = $this->Form->hidden('UserForm.stuff');
1263		$expected = array('input' => array(
1264				'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
1265				'id' => 'UserFormStuff'
1266		));
1267		$this->assertTags($result, $expected);
1268
1269		$result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
1270		$expected = array('input' => array(
1271			'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
1272			'value' => '0', 'id' => 'UserFormHidden'
1273		));
1274		$this->assertTags($result, $expected);
1275
1276		$result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
1277		$expected = array(
1278			'div' => array('class' => 'input checkbox'),
1279			array('input' => array(
1280				'type' => 'hidden', 'name' => 'data[UserForm][something]',
1281				'value' => '0', 'id' => 'UserFormSomething_'
1282			)),
1283			array('input' => array(
1284				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
1285				'value' => '1', 'id' => 'UserFormSomething'
1286			)),
1287			'label' => array('for' => 'UserFormSomething'),
1288			'Something',
1289			'/label',
1290			'/div'
1291		);
1292		$this->assertTags($result, $expected);
1293
1294		$result = $this->Form->fields;
1295		$expected = array(
1296			'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
1297			'UserForm.hidden' => '0', 'UserForm.something'
1298		);
1299		$this->assertEquals($expected, $result);
1300
1301		$hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
1302
1303		$result = $this->Form->secure($this->Form->fields);
1304		$expected = array(
1305			'div' => array('style' => 'display:none;'),
1306			array('input' => array(
1307				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1308				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1309			)),
1310			array('input' => array(
1311				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1312				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
1313			)),
1314			'/div'
1315		);
1316		$this->assertTags($result, $expected);
1317	}
1318
1319/**
1320 * Tests that the correct keys are added to the field hash index
1321 *
1322 * @return void
1323 */
1324	public function testFormSecuredFileInput() {
1325		$this->Form->request['_Token'] = array('key' => 'testKey');
1326		$this->assertEquals($this->Form->fields, array());
1327
1328		$result = $this->Form->file('Attachment.file');
1329		$expected = array(
1330			'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
1331			'Attachment.file.error', 'Attachment.file.size'
1332		);
1333		$this->assertEquals($this->Form->fields, $expected);
1334	}
1335
1336/**
1337 * test that multiple selects keys are added to field hash
1338 *
1339 * @return void
1340 */
1341	public function testFormSecuredMultipleSelect() {
1342		$this->Form->request['_Token'] = array('key' => 'testKey');
1343		$this->assertEquals($this->Form->fields, array());
1344		$options = array('1' => 'one', '2' => 'two');
1345
1346		$this->Form->select('Model.select', $options);
1347		$expected = array('Model.select');
1348		$this->assertEquals($this->Form->fields, $expected);
1349
1350		$this->Form->fields = array();
1351		$this->Form->select('Model.select', $options, array('multiple' => true));
1352		$this->assertEquals($this->Form->fields, $expected);
1353	}
1354
1355/**
1356 * testFormSecuredRadio method
1357 *
1358 * @return void
1359 */
1360	public function testFormSecuredRadio() {
1361		$this->Form->request['_Token'] = array('key' => 'testKey');
1362		$this->assertEquals($this->Form->fields, array());
1363		$options = array('1' => 'option1', '2' => 'option2');
1364
1365		$this->Form->radio('Test.test', $options);
1366		$expected = array('Test.test');
1367		$this->assertEquals($this->Form->fields, $expected);
1368	}
1369
1370/**
1371 * testDisableSecurityUsingForm method
1372 *
1373 * @return void
1374 */
1375	public function testDisableSecurityUsingForm() {
1376		$this->Form->request['_Token'] = array(
1377			'key' => 'testKey',
1378			'disabledFields' => array()
1379		);
1380		$this->Form->create();
1381
1382		$this->Form->hidden('Addresses.id', array('value' => '123456'));
1383		$this->Form->input('Addresses.title');
1384		$this->Form->input('Addresses.first_name', array('secure' => false));
1385		$this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
1386		$this->Form->input('Addresses.zip', array(
1387			'type' => 'select', 'options' => array(1,2), 'secure' => false
1388		));
1389
1390		$result = $this->Form->fields;
1391		$expected = array(
1392			'Addresses.id' => '123456', 'Addresses.title',
1393		);
1394		$this->assertEquals($expected, $result);
1395	}
1396
1397/**
1398 * test disableField
1399 *
1400 * @return void
1401 */
1402	public function testUnlockFieldAddsToList() {
1403		$this->Form->request['_Token'] = array(
1404			'key' => 'testKey',
1405			'unlockedFields' => array()
1406		);
1407		$this->Form->create('Contact');
1408		$this->Form->unlockField('Contact.name');
1409		$this->Form->text('Contact.name');
1410
1411		$this->assertEquals(array('Contact.name'), $this->Form->unlockField());
1412		$this->assertEquals(array(), $this->Form->fields);
1413	}
1414
1415/**
1416 * test unlockField removing from fields array.
1417 *
1418 * @return void
1419 */
1420	public function testUnlockFieldRemovingFromFields() {
1421		$this->Form->request['_Token'] = array(
1422			'key' => 'testKey',
1423			'unlockedFields' => array()
1424		);
1425		$this->Form->create('Contact');
1426		$this->Form->hidden('Contact.id', array('value' => 1));
1427		$this->Form->text('Contact.name');
1428
1429		$this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
1430		$this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
1431
1432		$this->Form->unlockField('Contact.name');
1433		$this->Form->unlockField('Contact.id');
1434		$this->assertEquals(array(), $this->Form->fields);
1435	}
1436
1437/**
1438 * testPasswordValidation method
1439 *
1440 * test validation errors on password input.
1441 *
1442 * @return void
1443 */
1444	public function testPasswordValidation() {
1445		$Contact = ClassRegistry::getObject('Contact');
1446		$Contact->validationErrors['password'] =  array('Please provide a password');
1447
1448		$result = $this->Form->input('Contact.password');
1449		$expected = array(
1450			'div' => array('class' => 'input password error'),
1451			'label' => array('for' => 'ContactPassword'),
1452			'Password',
1453			'/label',
1454			'input' => array(
1455				'type' => 'password', 'name' => 'data[Contact][password]',
1456				'id' => 'ContactPassword', 'class' => 'form-error'
1457			),
1458			array('div' => array('class' => 'error-message')),
1459			'Please provide a password',
1460			'/div',
1461			'/div'
1462		);
1463		$this->assertTags($result, $expected);
1464	}
1465
1466/**
1467 * testEmptyErrorValidation method
1468 *
1469 * test validation error div when validation message is an empty string
1470 *
1471 * @access public
1472 * @return void
1473 */
1474	function testEmptyErrorValidation() {
1475		$this->Form->validationErrors['Contact']['password'] = '';
1476		$result = $this->Form->input('Contact.password');
1477		$expected = array(
1478			'div' => array('class' => 'input password error'),
1479			'label' => array('for' => 'ContactPassword'),
1480			'Password',
1481			'/label',
1482			'input' => array(
1483				'type' => 'password', 'name' => 'data[Contact][password]',
1484				'id' => 'ContactPassword', 'class' => 'form-error'
1485			),
1486			array('div' => array('class' => 'error-message')),
1487			array(),
1488			'/div',
1489			'/div'
1490		);
1491		$this->assertTags($result, $expected);
1492	}
1493
1494/**
1495 * testEmptyInputErrorValidation method
1496 *
1497 * test validation error div when validation message is overriden by an empty string when calling input()
1498 *
1499 * @access public
1500 * @return void
1501 */
1502	function testEmptyInputErrorValidation() {
1503		$this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
1504		$result = $this->Form->input('Contact.password', array('error' => ''));
1505		$expected = array(
1506			'div' => array('class' => 'input password error'),
1507			'label' => array('for' => 'ContactPassword'),
1508			'Password',
1509			'/label',
1510			'input' => array(
1511				'type' => 'password', 'name' => 'data[Contact][password]',
1512				'id' => 'ContactPassword', 'class' => 'form-error'
1513			),
1514			array('div' => array('class' => 'error-message')),
1515			array(),
1516			'/div',
1517			'/div'
1518		);
1519		$this->assertTags($result, $expected);
1520	}
1521
1522/**
1523 * testFormValidationAssociated method
1524 *
1525 * test display of form errors in conjunction with model::validates.
1526 *
1527 * @return void
1528 */
1529	public function testFormValidationAssociated() {
1530		$this->UserForm = ClassRegistry::getObject('UserForm');
1531		$this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
1532
1533		$data = array(
1534			'UserForm' => array('name' => 'user'),
1535			'OpenidUrl' => array('url' => 'http://www.cakephp.org')
1536		);
1537
1538		$result = $this->UserForm->OpenidUrl->create($data);
1539		$this->assertFalse(empty($result));
1540		$this->assertFalse($this->UserForm->OpenidUrl->validates());
1541
1542		$result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
1543		$encoding = strtolower(Configure::read('App.encoding'));
1544		$expected = array(
1545			'form' => array(
1546				'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
1547				'accept-charset' => $encoding
1548			),
1549			'div' => array('style' => 'display:none;'),
1550			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1551			'/div'
1552		);
1553		$this->assertTags($result, $expected);
1554
1555		$result = $this->Form->error(
1556			'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
1557		);
1558		$this->assertEquals($result, 'Error, not registered');
1559
1560		unset($this->UserForm->OpenidUrl, $this->UserForm);
1561	}
1562
1563/**
1564 * testFormValidationAssociatedFirstLevel method
1565 *
1566 * test form error display with associated model.
1567 *
1568 * @return void
1569 */
1570	public function testFormValidationAssociatedFirstLevel() {
1571		$this->ValidateUser = ClassRegistry::getObject('ValidateUser');
1572		$this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
1573
1574		$data = array(
1575			'ValidateUser' => array('name' => 'mariano'),
1576			'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
1577		);
1578
1579		$result = $this->ValidateUser->create($data);
1580		$this->assertFalse(empty($result));
1581		$this->assertFalse($this->ValidateUser->validates());
1582		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1583
1584		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1585		$encoding = strtolower(Configure::read('App.encoding'));
1586		$expected = array(
1587			'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
1588			'div' => array('style' => 'display:none;'),
1589			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1590			'/div'
1591		);
1592		$this->assertTags($result, $expected);
1593
1594		$result = $this->Form->error(
1595			'ValidateUser.email', 'Invalid email', array('wrap' => false)
1596		);
1597		$this->assertEquals($result, 'Invalid email');
1598
1599		$result = $this->Form->error(
1600			'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
1601		);
1602		$this->assertEquals($result, 'Invalid name');
1603
1604		$result = $this->Form->error(
1605			'ValidateProfile.city', 'Invalid city', array('wrap' => false)
1606		);
1607		$this->assertEquals($result, 'Invalid city');
1608
1609		unset($this->ValidateUser->ValidateProfile);
1610		unset($this->ValidateUser);
1611	}
1612
1613/**
1614 * testFormValidationAssociatedSecondLevel method
1615 *
1616 * test form error display with associated model.
1617 *
1618 * @return void
1619 */
1620	public function testFormValidationAssociatedSecondLevel() {
1621		$this->ValidateUser = ClassRegistry::getObject('ValidateUser');
1622		$this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
1623		$this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
1624
1625		$data = array(
1626			'ValidateUser' => array('name' => 'mariano'),
1627			'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
1628			'ValidateItem' => array('name' => 'Item')
1629		);
1630
1631		$result = $this->ValidateUser->create($data);
1632		$this->assertFalse(empty($result));
1633		$this->assertFalse($this->ValidateUser->validates());
1634		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1635		$this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
1636
1637		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1638		$encoding = strtolower(Configure::read('App.encoding'));
1639		$expected = array(
1640			'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
1641			'div' => array('style' => 'display:none;'),
1642			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1643			'/div'
1644		);
1645		$this->assertTags($result, $expected);
1646
1647		$result = $this->Form->error(
1648			'ValidateUser.email', 'Invalid email', array('wrap' => false)
1649		);
1650		$this->assertEquals($result, 'Invalid email');
1651
1652		$result = $this->Form->error(
1653			'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
1654		);
1655		$this->assertEquals($result, 'Invalid name');
1656
1657		$result = $this->Form->error(
1658			'ValidateProfile.city', 'Invalid city', array('wrap' => false)
1659		);
1660
1661		$result = $this->Form->error(
1662			'ValidateItem.description', 'Invalid description', array('wrap' => false)
1663		);
1664		$this->assertEquals($result, 'Invalid description');
1665
1666		unset($this->ValidateUser->ValidateProfile->ValidateItem);
1667		unset($this->ValidateUser->ValidateProfile);
1668		unset($this->ValidateUser);
1669	}
1670
1671/**
1672 * testFormValidationMultiRecord method
1673 *
1674 * test form error display with multiple records.
1675 *
1676 * @return void
1677 */
1678	public function testFormValidationMultiRecord() {
1679		$Contact = ClassRegistry::getObject('Contact');
1680		$Contact->validationErrors[2] = array(
1681			'name' => array('This field cannot be left blank')
1682		);
1683		$result = $this->Form->input('Contact.2.name');
1684		$expected = array(
1685			'div' => array('class' => 'input text error'),
1686			'label' => array('for' => 'Contact2Name'),
1687			'Name',
1688			'/label',
1689			'input' => array(
1690				'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
1691				'class' => 'form-error', 'maxlength' => 255
1692			),
1693			array('div' => array('class' => 'error-message')),
1694			'This field cannot be left blank',
1695			'/div',
1696			'/div'
1697		);
1698		$this->assertTags($result, $expected);
1699	}
1700
1701/**
1702 * testMultipleInputValidation method
1703 *
1704 * test multiple record form validation error display.
1705 *
1706 * @return void
1707 */
1708	public function testMultipleInputValidation() {
1709		$Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
1710		$Address->validationErrors[0] = array(
1711			'title' => array('This field cannot be empty'),
1712			'first_name' => array('This field cannot be empty')
1713		);
1714		$Address->validationErrors[1] = array(
1715			'last_name' => array('You must have a last name')
1716		);
1717		$this->Form->create();
1718
1719		$result = $this->Form->input('Address.0.title');
1720		$expected = array(
1721			'div' => array('class'),
1722			'label' => array('for'),
1723			'preg:/[^<]+/',
1724			'/label',
1725			'input' => array(
1726				'type' => 'text', 'name', 'id', 'class' => 'form-error'
1727			),
1728			array('div' => array('class' => 'error-message')),
1729			'This field cannot be empty',
1730			'/div',
1731			'/div'
1732		);
1733		$this->assertTags($result, $expected);
1734
1735		$result = $this->Form->input('Address.0.first_name');
1736		$expected = array(
1737			'div' => array('class'),
1738			'label' => array('for'),
1739			'preg:/[^<]+/',
1740			'/label',
1741			'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
1742			array('div' => array('class' => 'error-message')),
1743			'This field cannot be empty',
1744			'/div',
1745			'/div'
1746		);
1747		$this->assertTags($result, $expected);
1748
1749		$result = $this->Form->input('Address.0.last_name');
1750		$expected = array(
1751			'div' => array('class'),
1752			'label' => array('for'),
1753			'preg:/[^<]+/',
1754			'/label',
1755			'input' => array('type' => 'text', 'name', 'id'),
1756			'/div'
1757		);
1758		$this->assertTags($result, $expected);
1759
1760		$result = $this->Form->input('Address.1.last_name');
1761		$expected = array(
1762			'div' => array('class'),
1763			'label' => array('for'),
1764			'preg:/[^<]+/',
1765			'/label',
1766			'input' => array(
1767				'type' => 'text', 'name' => 'preg:/[^<]+/',
1768				'id' => 'preg:/[^<]+/', 'class' => 'form-error'
1769			),
1770			array('div' => array('class' => 'error-message')),
1771			'You must have a last name',
1772			'/div',
1773			'/div'
1774		);
1775		$this->assertTags($result, $expected);
1776	}
1777
1778/**
1779 * testInput method
1780 *
1781 * Test various incarnations of input().
1782 *
1783 * @return void
1784 */
1785	public function testInput() {
1786		$result = $this->Form->input('ValidateUser.balance');
1787		$expected = array(
1788			'div' => array('class'),
1789			'label' => array('for'),
1790			'Balance',
1791			'/label',
1792			'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
1793			'/div',
1794		);
1795		$this->assertTags($result, $expected);
1796
1797		$result = $this->Form->input('Contact.email', array('id' => 'custom'));
1798		$expected = array(
1799			'div' => array('class' => 'input text'),
1800			'label' => array('for' => 'custom'),
1801			'Email',
1802			'/label',
1803			array('input' => array(
1804				'type' => 'text', 'name' => 'data[Contact][email]',
1805				'id' => 'custom', 'maxlength' => 255
1806			)),
1807			'/div'
1808		);
1809		$this->assertTags($result, $expected);
1810
1811		$result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
1812		$expected = array(
1813			'<div',
1814			'label' => array('for' => 'ContactEmail'),
1815			'Email',
1816			'/label',
1817			array('input' => array(
1818				'type' => 'text', 'name' => 'data[Contact][email]',
1819				'id' => 'ContactEmail', 'maxlength' => 255
1820			)),
1821			'/div'
1822		);
1823		$this->assertTags($result, $expected);
1824
1825		$result = $this->Form->hidden('Contact.idontexist');
1826		$expected = array('input' => array(
1827				'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
1828				'id' => 'Con…

Large files files are truncated, but you can click here to view the full file