Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
/**
3
 * FormHelperTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.View.Helper
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ClassRegistry', 'Utility');
20
App::uses('Controller', 'Controller');
21
App::uses('View', 'View');
22
App::uses('Model', 'Model');
23
App::uses('Security', 'Utility');
24
App::uses('CakeRequest', 'Network');
25
App::uses('HtmlHelper', 'View/Helper');
26
App::uses('FormHelper', 'View/Helper');
27
App::uses('Router', 'Routing');
28
 
29
/**
30
 * ContactTestController class
31
 *
32
 * @package       Cake.Test.Case.View.Helper
33
 */
34
class ContactTestController extends Controller {
35
 
36
/**
37
 * uses property
38
 *
39
 * @var mixed
40
 */
41
	public $uses = null;
42
}
43
 
44
/**
45
 * Contact class
46
 *
47
 * @package       Cake.Test.Case.View.Helper
48
 */
49
class Contact extends CakeTestModel {
50
 
51
/**
52
 * useTable property
53
 *
54
 * @var bool
55
 */
56
	public $useTable = false;
57
 
58
/**
59
 * Default schema
60
 *
61
 * @var array
62
 */
63
	protected $_schema = array(
64
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
65
		'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
66
		'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
67
		'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
68
		'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
69
		'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
70
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
71
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
72
		'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
73
	);
74
 
75
/**
76
 * validate property
77
 *
78
 * @var array
79
 */
80
	public $validate = array(
81
		'non_existing' => array(),
82
		'idontexist' => array(),
83
		'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
84
		'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
85
		'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
86
		'imrequiredonboth' => array(
87
			'required' => array('rule' => 'alphaNumeric'),
88
		),
89
		'string_required' => 'notEmpty',
90
		'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
91
		'imrequiredtoo' => array('rule' => 'notEmpty'),
92
		'required_one' => array('required' => array('rule' => array('notEmpty'))),
93
		'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
94
		'imalsonotrequired' => array(
95
			'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
96
			'between' => array('rule' => array('between', 5, 30)),
97
		),
98
		'imalsonotrequired2' => array(
99
			'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
100
			'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
101
		),
102
		'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
103
		'iamrequiredalways' => array(
104
			'email' => array('rule' => 'email'),
105
			'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
106
			'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
107
		),
108
		'boolean_field' => array('rule' => 'boolean')
109
	);
110
 
111
/**
112
 * schema method
113
 *
114
 * @return void
115
 */
116
	public function setSchema($schema) {
117
		$this->_schema = $schema;
118
	}
119
 
120
/**
121
 * hasAndBelongsToMany property
122
 *
123
 * @var array
124
 */
125
	public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
126
 
127
/**
128
 * hasAndBelongsToMany property
129
 *
130
 * @var array
131
 */
132
	public $belongsTo = array('User' => array('className' => 'UserForm'));
133
}
134
 
135
/**
136
 * ContactTagsContact class
137
 *
138
 * @package       Cake.Test.Case.View.Helper
139
 */
140
class ContactTagsContact extends CakeTestModel {
141
 
142
/**
143
 * useTable property
144
 *
145
 * @var bool
146
 */
147
	public $useTable = false;
148
 
149
/**
150
 * Default schema
151
 *
152
 * @var array
153
 */
154
	protected $_schema = array(
155
		'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
156
		'contact_tag_id' => array(
157
			'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
158
		)
159
	);
160
 
161
/**
162
 * schema method
163
 *
164
 * @return void
165
 */
166
	public function setSchema($schema) {
167
		$this->_schema = $schema;
168
	}
169
 
170
}
171
 
172
/**
173
 * ContactNonStandardPk class
174
 *
175
 * @package       Cake.Test.Case.View.Helper
176
 */
177
class ContactNonStandardPk extends Contact {
178
 
179
/**
180
 * primaryKey property
181
 *
182
 * @var string
183
 */
184
	public $primaryKey = 'pk';
185
 
186
/**
187
 * schema method
188
 *
189
 * @return void
190
 */
191
	public function schema($field = false) {
192
		$this->_schema = parent::schema();
193
		$this->_schema['pk'] = $this->_schema['id'];
194
		unset($this->_schema['id']);
195
		return $this->_schema;
196
	}
197
 
198
}
199
 
200
/**
201
 * ContactTag class
202
 *
203
 * @package       Cake.Test.Case.View.Helper
204
 */
205
class ContactTag extends Model {
206
 
207
/**
208
 * useTable property
209
 *
210
 * @var bool
211
 */
212
	public $useTable = false;
213
 
214
/**
215
 * schema definition
216
 *
217
 * @var array
218
 */
219
	protected $_schema = array(
220
		'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
221
		'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
222
		'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
223
		'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
224
	);
225
}
226
 
227
/**
228
 * UserForm class
229
 *
230
 * @package       Cake.Test.Case.View.Helper
231
 */
232
class UserForm extends CakeTestModel {
233
 
234
/**
235
 * useTable property
236
 *
237
 * @var bool
238
 */
239
	public $useTable = false;
240
 
241
/**
242
 * hasMany property
243
 *
244
 * @var array
245
 */
246
	public $hasMany = array(
247
		'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
248
	));
249
 
250
/**
251
 * schema definition
252
 *
253
 * @var array
254
 */
255
	protected $_schema = array(
256
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
257
		'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
258
		'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
259
		'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
260
		'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
261
		'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
262
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
263
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
264
	);
265
}
266
 
267
/**
268
 * OpenidUrl class
269
 *
270
 * @package       Cake.Test.Case.View.Helper
271
 */
272
class OpenidUrl extends CakeTestModel {
273
 
274
/**
275
 * useTable property
276
 *
277
 * @var bool
278
 */
279
	public $useTable = false;
280
 
281
/**
282
 * belongsTo property
283
 *
284
 * @var array
285
 */
286
	public $belongsTo = array('UserForm' => array(
287
		'className' => 'UserForm', 'foreignKey' => 'user_form_id'
288
	));
289
 
290
/**
291
 * validate property
292
 *
293
 * @var array
294
 */
295
	public $validate = array('openid_not_registered' => array());
296
 
297
/**
298
 * schema method
299
 *
300
 * @var array
301
 */
302
	protected $_schema = array(
303
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
304
		'user_form_id' => array(
305
			'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
306
		),
307
		'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
308
	);
309
 
310
/**
311
 * beforeValidate method
312
 *
313
 * @return void
314
 */
315
	public function beforeValidate($options = array()) {
316
		$this->invalidate('openid_not_registered');
317
		return true;
318
	}
319
 
320
}
321
 
322
/**
323
 * ValidateUser class
324
 *
325
 * @package       Cake.Test.Case.View.Helper
326
 */
327
class ValidateUser extends CakeTestModel {
328
 
329
/**
330
 * useTable property
331
 *
332
 * @var bool
333
 */
334
	public $useTable = false;
335
 
336
/**
337
 * hasOne property
338
 *
339
 * @var array
340
 */
341
	public $hasOne = array('ValidateProfile' => array(
342
		'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
343
	));
344
 
345
/**
346
 * schema method
347
 *
348
 * @var array
349
 */
350
	protected $_schema = array(
351
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
352
		'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
353
		'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
354
		'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
355
		'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
356
		'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
357
		'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
358
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
359
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
360
	);
361
 
362
/**
363
 * beforeValidate method
364
 *
365
 * @return void
366
 */
367
	public function beforeValidate($options = array()) {
368
		$this->invalidate('email');
369
		return false;
370
	}
371
 
372
}
373
 
374
/**
375
 * ValidateProfile class
376
 *
377
 * @package       Cake.Test.Case.View.Helper
378
 */
379
class ValidateProfile extends CakeTestModel {
380
 
381
/**
382
 * useTable property
383
 *
384
 * @var bool
385
 */
386
	public $useTable = false;
387
 
388
/**
389
 * schema property
390
 *
391
 * @var array
392
 */
393
	protected $_schema = array(
394
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
395
		'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
396
		'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
397
		'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
398
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
399
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
400
	);
401
 
402
/**
403
 * hasOne property
404
 *
405
 * @var array
406
 */
407
	public $hasOne = array('ValidateItem' => array(
408
		'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
409
	));
410
 
411
/**
412
 * belongsTo property
413
 *
414
 * @var array
415
 */
416
	public $belongsTo = array('ValidateUser' => array(
417
		'className' => 'ValidateUser', 'foreignKey' => 'user_id'
418
	));
419
 
420
/**
421
 * beforeValidate method
422
 *
423
 * @return void
424
 */
425
	public function beforeValidate($options = array()) {
426
		$this->invalidate('full_name');
427
		$this->invalidate('city');
428
		return false;
429
	}
430
 
431
}
432
 
433
/**
434
 * ValidateItem class
435
 *
436
 * @package       Cake.Test.Case.View.Helper
437
 */
438
class ValidateItem extends CakeTestModel {
439
 
440
/**
441
 * useTable property
442
 *
443
 * @var bool
444
 */
445
	public $useTable = false;
446
 
447
/**
448
 * schema property
449
 *
450
 * @var array
451
 */
452
	protected $_schema = array(
453
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
454
		'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
455
		'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
456
		'description' => array(
457
			'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
458
		),
459
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
460
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
461
	);
462
 
463
/**
464
 * belongsTo property
465
 *
466
 * @var array
467
 */
468
	public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
469
 
470
/**
471
 * beforeValidate method
472
 *
473
 * @return void
474
 */
475
	public function beforeValidate($options = array()) {
476
		$this->invalidate('description');
477
		return false;
478
	}
479
 
480
}
481
 
482
/**
483
 * TestMail class
484
 *
485
 * @package       Cake.Test.Case.View.Helper
486
 */
487
class TestMail extends CakeTestModel {
488
 
489
/**
490
 * useTable property
491
 *
492
 * @var bool
493
 */
494
	public $useTable = false;
495
 
496
}
497
 
498
/**
499
 * FormHelperTest class
500
 *
501
 * @package       Cake.Test.Case.View.Helper
502
 * @property FormHelper $Form
503
 */
504
class FormHelperTest extends CakeTestCase {
505
 
506
/**
507
 * Fixtures to be used
508
 *
509
 * @var array
510
 */
511
	public $fixtures = array('core.post');
512
 
513
/**
514
 * Do not load the fixtures by default
515
 *
516
 * @var bool
517
 */
518
	public $autoFixtures = false;
519
 
520
/**
521
 * setUp method
522
 *
523
 * @return void
524
 */
525
	public function setUp() {
526
		parent::setUp();
527
 
528
		Configure::write('Config.language', 'eng');
529
		Configure::write('App.base', '');
530
		Configure::delete('Asset');
531
		$this->Controller = new ContactTestController();
532
		$this->View = new View($this->Controller);
533
 
534
		$this->Form = new FormHelper($this->View);
535
		$this->Form->Html = new HtmlHelper($this->View);
536
		$this->Form->request = new CakeRequest('contacts/add', false);
537
		$this->Form->request->here = '/contacts/add';
538
		$this->Form->request['action'] = 'add';
539
		$this->Form->request->webroot = '';
540
		$this->Form->request->base = '';
541
 
542
		ClassRegistry::addObject('Contact', new Contact());
543
		ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
544
		ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
545
		ClassRegistry::addObject('User', new UserForm());
546
		ClassRegistry::addObject('ValidateItem', new ValidateItem());
547
		ClassRegistry::addObject('ValidateUser', new ValidateUser());
548
		ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
549
 
550
		$this->oldSalt = Configure::read('Security.salt');
551
 
552
		$this->dateRegex = array(
553
			'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
554
			'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
555
			'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
556
			'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
557
			'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
558
			'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
559
		);
560
 
561
		Configure::write('Security.salt', 'foo!');
562
	}
563
 
564
/**
565
 * tearDown method
566
 *
567
 * @return void
568
 */
569
	public function tearDown() {
570
		parent::tearDown();
571
		unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
572
		Configure::write('Security.salt', $this->oldSalt);
573
	}
574
 
575
/**
576
 * testFormCreateWithSecurity method
577
 *
578
 * Test form->create() with security key.
579
 *
580
 * @return void
581
 */
582
	public function testCreateWithSecurity() {
583
		$this->Form->request['_Token'] = array('key' => 'testKey');
584
		$encoding = strtolower(Configure::read('App.encoding'));
585
		$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
586
		$expected = array(
587
			'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
588
			'div' => array('style' => 'display:none;'),
589
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
590
			array('input' => array(
591
				'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
592
			)),
593
			'/div'
594
		);
595
		$this->assertTags($result, $expected);
596
 
597
		$result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
598
		$expected['form']['id'] = 'MyForm';
599
		$this->assertTags($result, $expected);
600
	}
601
 
602
/**
603
 * testFormCreateGetNoSecurity method
604
 *
605
 * Test form->create() with no security key as its a get form
606
 *
607
 * @return void
608
 */
609
	public function testCreateEndGetNoSecurity() {
610
		$this->Form->request['_Token'] = array('key' => 'testKey');
611
		$encoding = strtolower(Configure::read('App.encoding'));
612
		$result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
613
		$this->assertNotContains('Token', $result);
614
 
615
		$result = $this->Form->end('Save');
616
		$this->assertNotContains('Token', $result);
617
	}
618
 
619
/**
620
 * test that create() clears the fields property so it starts fresh
621
 *
622
 * @return void
623
 */
624
	public function testCreateClearingFields() {
625
		$this->Form->fields = array('model_id');
626
		$this->Form->create('Contact');
627
		$this->assertEquals(array(), $this->Form->fields);
628
	}
629
 
630
/**
631
 * Tests form hash generation with model-less data
632
 *
633
 * @return void
634
 */
635
	public function testValidateHashNoModel() {
636
		$this->Form->request['_Token'] = array('key' => 'foo');
637
		$result = $this->Form->secure(array('anything'));
638
		$this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
639
	}
640
 
641
/**
642
 * Tests that models with identical field names get resolved properly
643
 *
644
 * @return void
645
 */
646
	public function testDuplicateFieldNameResolution() {
647
		$result = $this->Form->create('ValidateUser');
648
		$this->assertEquals(array('ValidateUser'), $this->Form->entity());
649
 
650
		$result = $this->Form->input('ValidateItem.name');
651
		$this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
652
 
653
		$result = $this->Form->input('ValidateUser.name');
654
		$this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
655
		$this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
656
		$this->assertRegExp('/type="text"/', $result);
657
 
658
		$result = $this->Form->input('ValidateItem.name');
659
		$this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
660
		$this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
661
		$this->assertRegExp('/<textarea/', $result);
662
 
663
		$result = $this->Form->input('name');
664
		$this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
665
		$this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
666
		$this->assertRegExp('/type="text"/', $result);
667
	}
668
 
669
/**
670
 * Tests that hidden fields generated for checkboxes don't get locked
671
 *
672
 * @return void
673
 */
674
	public function testNoCheckboxLocking() {
675
		$this->Form->request['_Token'] = array('key' => 'foo');
676
		$this->assertSame(array(), $this->Form->fields);
677
 
678
		$this->Form->checkbox('check', array('value' => '1'));
679
		$this->assertSame($this->Form->fields, array('check'));
680
	}
681
 
682
/**
683
 * testFormSecurityFields method
684
 *
685
 * Test generation of secure form hash generation.
686
 *
687
 * @return void
688
 */
689
	public function testFormSecurityFields() {
690
		$key = 'testKey';
691
		$fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
692
		$secureAttributes = array('form' => 'MyTestForm');
693
 
694
		$this->Form->request['_Token'] = array('key' => $key);
695
		$result = $this->Form->secure($fields, $secureAttributes);
696
 
697
		$hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
698
		$hash .= ':' . 'Model.valid';
699
		$hash = urlencode($hash);
700
 
701
		$expected = array(
702
			'div' => array('style' => 'display:none;'),
703
			array('input' => array(
704
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
705
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/',
706
				'form' => 'MyTestForm',
707
			)),
708
			array('input' => array(
709
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
710
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/',
711
				'form' => 'MyTestForm',
712
			)),
713
			'/div'
714
		);
715
		$this->assertTags($result, $expected);
716
	}
717
 
718
/**
719
 * Tests correct generation of number fields for double and float fields
720
 *
721
 * @return void
722
 */
723
	public function testTextFieldGenerationForFloats() {
724
		$model = ClassRegistry::getObject('Contact');
725
		$model->setSchema(array('foo' => array(
726
			'type' => 'float',
727
			'null' => false,
728
			'default' => null,
729
			'length' => 10
730
		)));
731
 
732
		$this->Form->create('Contact');
733
		$result = $this->Form->input('foo');
734
		$expected = array(
735
			'div' => array('class' => 'input number'),
736
			'label' => array('for' => 'ContactFoo'),
737
			'Foo',
738
			'/label',
739
			array('input' => array(
740
				'type' => 'number',
741
				'name' => 'data[Contact][foo]',
742
				'id' => 'ContactFoo',
743
				'step' => 'any'
744
			)),
745
			'/div'
746
		);
747
		$this->assertTags($result, $expected);
748
 
749
		$result = $this->Form->input('foo', array('step' => 0.5));
750
		$expected = array(
751
			'div' => array('class' => 'input number'),
752
			'label' => array('for' => 'ContactFoo'),
753
			'Foo',
754
			'/label',
755
			array('input' => array(
756
				'type' => 'number',
757
				'name' => 'data[Contact][foo]',
758
				'id' => 'ContactFoo',
759
				'step' => '0.5'
760
			)),
761
			'/div'
762
		);
763
		$this->assertTags($result, $expected);
764
	}
765
 
766
/**
767
 * Tests correct generation of decimal fields as text inputs
768
 *
769
 * @return void
770
 */
771
	public function testTextFieldGenerationForDecimalAsText() {
772
		$this->Form->create('ValidateUser');
773
		$result = $this->Form->input('cost_decimal', array(
774
			'type' => 'text'
775
		));
776
		$expected = array(
777
			'div' => array('class' => 'input text'),
778
			'label' => array('for' => 'ValidateUserCostDecimal'),
779
			'Cost Decimal',
780
			'/label',
781
			array('input' => array(
782
				'type' => 'text',
783
				'name' => 'data[ValidateUser][cost_decimal]',
784
				'id' => 'ValidateUserCostDecimal',
785
			)),
786
			'/div'
787
		);
788
		$this->assertTags($result, $expected);
789
	}
790
 
791
/**
792
 * Tests correct generation of number fields for integer fields
793
 *
794
 * @return void
795
 */
796
	public function testTextFieldTypeNumberGenerationForIntegers() {
797
		$model = ClassRegistry::getObject('Contact');
798
		$model->setSchema(array('foo' => array(
799
			'type' => 'integer',
800
			'null' => false,
801
			'default' => null,
802
			'length' => null
803
		)));
804
 
805
		$this->Form->create('Contact');
806
		$result = $this->Form->input('foo');
807
		$expected = array(
808
			'div' => array('class' => 'input number'),
809
			'label' => array('for' => 'ContactFoo'),
810
			'Foo',
811
			'/label',
812
			array('input' => array(
813
				'type' => 'number', 'name' => 'data[Contact][foo]',
814
				'id' => 'ContactFoo'
815
			)),
816
			'/div'
817
		);
818
		$this->assertTags($result, $expected);
819
	}
820
 
821
/**
822
 * Tests correct generation of file upload fields for binary fields
823
 *
824
 * @return void
825
 */
826
	public function testFileUploadFieldTypeGenerationForBinaries() {
827
		$model = ClassRegistry::getObject('Contact');
828
		$model->setSchema(array('foo' => array(
829
			'type' => 'binary',
830
			'null' => false,
831
			'default' => null,
832
			'length' => 1024
833
		)));
834
 
835
		$this->Form->create('Contact');
836
		$result = $this->Form->input('foo');
837
		$expected = array(
838
			'div' => array('class' => 'input file'),
839
			'label' => array('for' => 'ContactFoo'),
840
			'Foo',
841
			'/label',
842
			array('input' => array(
843
				'type' => 'file', 'name' => 'data[Contact][foo]',
844
				'id' => 'ContactFoo'
845
			)),
846
			'/div'
847
		);
848
		$this->assertTags($result, $expected);
849
	}
850
 
851
/**
852
 * testFormSecurityMultipleFields method
853
 *
854
 * Test secure() with multiple row form. Ensure hash is correct.
855
 *
856
 * @return void
857
 */
858
	public function testFormSecurityMultipleFields() {
859
		$key = 'testKey';
860
 
861
		$fields = array(
862
			'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
863
			'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
864
			'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
865
		);
866
		$this->Form->request['_Token'] = array('key' => $key);
867
		$result = $this->Form->secure($fields);
868
 
869
		$hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
870
		$hash .= '%7CModel.1.hidden%7CModel.1.valid';
871
 
872
		$expected = array(
873
			'div' => array('style' => 'display:none;'),
874
			array('input' => array(
875
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
876
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
877
			)),
878
			array('input' => array(
879
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
880
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
881
			)),
882
			'/div'
883
		);
884
		$this->assertTags($result, $expected);
885
	}
886
 
887
/**
888
 * testFormSecurityMultipleSubmitButtons
889
 *
890
 * test form submit generation and ensure that _Token is only created on end()
891
 *
892
 * @return void
893
 */
894
	public function testFormSecurityMultipleSubmitButtons() {
895
		$key = 'testKey';
896
		$this->Form->request['_Token'] = array('key' => $key);
897
 
898
		$this->Form->create('Addresses');
899
		$this->Form->input('Address.title');
900
		$this->Form->input('Address.first_name');
901
 
902
		$result = $this->Form->submit('Save', array('name' => 'save'));
903
		$expected = array(
904
			'div' => array('class' => 'submit'),
905
			'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
906
			'/div',
907
		);
908
		$this->assertTags($result, $expected);
909
 
910
		$result = $this->Form->submit('Cancel', array('name' => 'cancel'));
911
		$expected = array(
912
			'div' => array('class' => 'submit'),
913
			'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
914
			'/div',
915
		);
916
		$this->assertTags($result, $expected);
917
		$result = $this->Form->end(null);
918
 
919
		$expected = array(
920
			'div' => array('style' => 'display:none;'),
921
			array('input' => array(
922
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
923
				'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
924
			)),
925
			array('input' => array(
926
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
927
				'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
928
			)),
929
			'/div'
930
		);
931
		$this->assertTags($result, $expected);
932
	}
933
 
934
/**
935
 * Test that buttons created with foo[bar] name attributes are unlocked correctly.
936
 *
937
 * @return void
938
 */
939
	public function testSecurityButtonNestedNamed() {
940
		$key = 'testKey';
941
		$this->Form->request['_Token'] = array('key' => $key);
942
 
943
		$this->Form->create('Addresses');
944
		$this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
945
		$result = $this->Form->unlockField();
946
		$this->assertEquals(array('Address.button'), $result);
947
	}
948
 
949
/**
950
 * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
951
 *
952
 * @return void
953
 */
954
	public function testSecuritySubmitNestedNamed() {
955
		$key = 'testKey';
956
		$this->Form->request['_Token'] = array('key' => $key);
957
 
958
		$this->Form->create('Addresses');
959
		$this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
960
		$result = $this->Form->unlockField();
961
		$this->assertEquals(array('Address.button'), $result);
962
	}
963
 
964
/**
965
 * Test that the correct fields are unlocked for image submits with no names.
966
 *
967
 * @return void
968
 */
969
	public function testSecuritySubmitImageNoName() {
970
		$key = 'testKey';
971
		$this->Form->request['_Token'] = array('key' => $key);
972
 
973
		$this->Form->create('User');
974
		$result = $this->Form->submit('save.png');
975
		$expected = array(
976
			'div' => array('class' => 'submit'),
977
			'input' => array('type' => 'image', 'src' => 'img/save.png'),
978
			'/div'
979
		);
980
		$this->assertTags($result, $expected);
981
		$this->assertEquals(array('x', 'y'), $this->Form->unlockField());
982
	}
983
 
984
/**
985
 * Test that the correct fields are unlocked for image submits with names.
986
 *
987
 * @return void
988
 */
989
	public function testSecuritySubmitImageName() {
990
		$key = 'testKey';
991
		$this->Form->request['_Token'] = array('key' => $key);
992
 
993
		$this->Form->create('User');
994
		$result = $this->Form->submit('save.png', array('name' => 'test'));
995
		$expected = array(
996
			'div' => array('class' => 'submit'),
997
			'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
998
			'/div'
999
		);
1000
		$this->assertTags($result, $expected);
1001
		$this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
1002
	}
1003
 
1004
/**
1005
 * testFormSecurityMultipleInputFields method
1006
 *
1007
 * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
1008
 *
1009
 * @return void
1010
 */
1011
	public function testFormSecurityMultipleInputFields() {
1012
		$key = 'testKey';
1013
 
1014
		$this->Form->request['_Token'] = array('key' => $key);
1015
		$this->Form->create('Addresses');
1016
 
1017
		$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
1018
		$this->Form->input('Addresses.0.title');
1019
		$this->Form->input('Addresses.0.first_name');
1020
		$this->Form->input('Addresses.0.last_name');
1021
		$this->Form->input('Addresses.0.address');
1022
		$this->Form->input('Addresses.0.city');
1023
		$this->Form->input('Addresses.0.phone');
1024
		$this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
1025
 
1026
		$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1027
		$this->Form->input('Addresses.1.title');
1028
		$this->Form->input('Addresses.1.first_name');
1029
		$this->Form->input('Addresses.1.last_name');
1030
		$this->Form->input('Addresses.1.address');
1031
		$this->Form->input('Addresses.1.city');
1032
		$this->Form->input('Addresses.1.phone');
1033
		$this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
1034
 
1035
		$result = $this->Form->secure($this->Form->fields);
1036
 
1037
		$hash = 'a3b9b2ba1cb688838f92818a5970e17dd7943a78%3AAddresses.0.id%7CAddresses.1.id';
1038
 
1039
		$expected = array(
1040
			'div' => array('style' => 'display:none;'),
1041
			array('input' => array(
1042
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1043
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1044
			)),
1045
			array('input' => array(
1046
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1047
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
1048
			)),
1049
			'/div'
1050
		);
1051
		$this->assertTags($result, $expected);
1052
	}
1053
 
1054
/**
1055
 * Test form security with Model.field.0 style inputs
1056
 *
1057
 * @return void
1058
 */
1059
	public function testFormSecurityArrayFields() {
1060
		$key = 'testKey';
1061
 
1062
		$this->Form->request->params['_Token']['key'] = $key;
1063
		$this->Form->create('Address');
1064
		$this->Form->input('Address.primary.1');
1065
		$this->assertEquals('Address.primary', $this->Form->fields[0]);
1066
 
1067
		$this->Form->input('Address.secondary.1.0');
1068
		$this->assertEquals('Address.secondary', $this->Form->fields[1]);
1069
	}
1070
 
1071
/**
1072
 * testFormSecurityMultipleInputDisabledFields method
1073
 *
1074
 * test secure form generation with multiple records and disabled fields.
1075
 *
1076
 * @return void
1077
 */
1078
	public function testFormSecurityMultipleInputDisabledFields() {
1079
		$key = 'testKey';
1080
		$this->Form->request->params['_Token'] = array(
1081
			'key' => $key,
1082
			'unlockedFields' => array('first_name', 'address')
1083
		);
1084
		$this->Form->create();
1085
 
1086
		$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
1087
		$this->Form->input('Addresses.0.title');
1088
		$this->Form->input('Addresses.0.first_name');
1089
		$this->Form->input('Addresses.0.last_name');
1090
		$this->Form->input('Addresses.0.address');
1091
		$this->Form->input('Addresses.0.city');
1092
		$this->Form->input('Addresses.0.phone');
1093
		$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1094
		$this->Form->input('Addresses.1.title');
1095
		$this->Form->input('Addresses.1.first_name');
1096
		$this->Form->input('Addresses.1.last_name');
1097
		$this->Form->input('Addresses.1.address');
1098
		$this->Form->input('Addresses.1.city');
1099
		$this->Form->input('Addresses.1.phone');
1100
 
1101
		$result = $this->Form->secure($this->Form->fields);
1102
		$hash = '5c9cadf9da008cc444d3960b481391a425a5d979%3AAddresses.0.id%7CAddresses.1.id';
1103
 
1104
		$expected = array(
1105
			'div' => array('style' => 'display:none;'),
1106
			array('input' => array(
1107
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1108
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1109
			)),
1110
			array('input' => array(
1111
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1112
				'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
1113
			)),
1114
			'/div'
1115
		);
1116
		$this->assertTags($result, $expected);
1117
	}
1118
 
1119
/**
1120
 * testFormSecurityInputDisabledFields method
1121
 *
1122
 * Test single record form with disabled fields.
1123
 *
1124
 * @return void
1125
 */
1126
	public function testFormSecurityInputUnlockedFields() {
1127
		$key = 'testKey';
1128
		$this->Form->request['_Token'] = array(
1129
			'key' => $key,
1130
			'unlockedFields' => array('first_name', 'address')
1131
		);
1132
		$this->Form->create();
1133
		$this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
1134
 
1135
		$this->Form->hidden('Addresses.id', array('value' => '123456'));
1136
		$this->Form->input('Addresses.title');
1137
		$this->Form->input('Addresses.first_name');
1138
		$this->Form->input('Addresses.last_name');
1139
		$this->Form->input('Addresses.address');
1140
		$this->Form->input('Addresses.city');
1141
		$this->Form->input('Addresses.phone');
1142
 
1143
		$result = $this->Form->fields;
1144
		$expected = array(
1145
			'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
1146
			'Addresses.city', 'Addresses.phone'
1147
		);
1148
		$this->assertEquals($expected, $result);
1149
 
1150
		$result = $this->Form->secure($expected);
1151
 
1152
		$hash = '40289bd07811587887ff56585a8526ff9da59d7a%3AAddresses.id';
1153
		$expected = array(
1154
			'div' => array('style' => 'display:none;'),
1155
			array('input' => array(
1156
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1157
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1158
			)),
1159
			array('input' => array(
1160
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1161
				'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
1162
			)),
1163
			'/div'
1164
		);
1165
		$this->assertTags($result, $expected);
1166
	}
1167
 
1168
/**
1169
 * test securing inputs with custom name attributes.
1170
 *
1171
 * @return void
1172
 */
1173
	public function testFormSecureWithCustomNameAttribute() {
1174
		$this->Form->request->params['_Token']['key'] = 'testKey';
1175
 
1176
		$this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
1177
		$this->assertEquals('User.custom', $this->Form->fields[0]);
1178
 
1179
		$this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
1180
		$this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
1181
	}
1182
 
1183
/**
1184
 * testFormSecuredInput method
1185
 *
1186
 * Test generation of entire secure form, assertions made on input() output.
1187
 *
1188
 * @return void
1189
 */
1190
	public function testFormSecuredInput() {
1191
		$this->Form->request['_Token'] = array('key' => 'testKey');
1192
 
1193
		$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
1194
		$encoding = strtolower(Configure::read('App.encoding'));
1195
		$expected = array(
1196
			'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
1197
			'div' => array('style' => 'display:none;'),
1198
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
1199
			array('input' => array(
1200
				'type' => 'hidden', 'name' => 'data[_Token][key]',
1201
				'value' => 'testKey', 'id' => 'preg:/Token\d+/'
1202
			)),
1203
			'/div'
1204
		);
1205
		$this->assertTags($result, $expected);
1206
 
1207
		$result = $this->Form->input('UserForm.published', array('type' => 'text'));
1208
		$expected = array(
1209
			'div' => array('class' => 'input text'),
1210
			'label' => array('for' => 'UserFormPublished'),
1211
			'Published',
1212
			'/label',
1213
			array('input' => array(
1214
				'type' => 'text', 'name' => 'data[UserForm][published]',
1215
				'id' => 'UserFormPublished'
1216
			)),
1217
			'/div'
1218
		);
1219
		$this->assertTags($result, $expected);
1220
 
1221
		$result = $this->Form->input('UserForm.other', array('type' => 'text'));
1222
		$expected = array(
1223
			'div' => array('class' => 'input text'),
1224
			'label' => array('for' => 'UserFormOther'),
1225
			'Other',
1226
			'/label',
1227
			array('input' => array(
1228
				'type' => 'text', 'name' => 'data[UserForm][other]',
1229
				'id' => 'UserFormOther'
1230
			)),
1231
			'/div'
1232
		);
1233
		$this->assertTags($result, $expected);
1234
 
1235
		$result = $this->Form->hidden('UserForm.stuff');
1236
		$expected = array(
1237
			'input' => array(
1238
				'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
1239
				'id' => 'UserFormStuff'
1240
		));
1241
		$this->assertTags($result, $expected);
1242
 
1243
		$result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
1244
		$expected = array('input' => array(
1245
			'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
1246
			'value' => '0', 'id' => 'UserFormHidden'
1247
		));
1248
		$this->assertTags($result, $expected);
1249
 
1250
		$result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
1251
		$expected = array(
1252
			'div' => array('class' => 'input checkbox'),
1253
			array('input' => array(
1254
				'type' => 'hidden', 'name' => 'data[UserForm][something]',
1255
				'value' => '0', 'id' => 'UserFormSomething_'
1256
			)),
1257
			array('input' => array(
1258
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
1259
				'value' => '1', 'id' => 'UserFormSomething'
1260
			)),
1261
			'label' => array('for' => 'UserFormSomething'),
1262
			'Something',
1263
			'/label',
1264
			'/div'
1265
		);
1266
		$this->assertTags($result, $expected);
1267
 
1268
		$result = $this->Form->fields;
1269
		$expected = array(
1270
			'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
1271
			'UserForm.hidden' => '0', 'UserForm.something'
1272
		);
1273
		$this->assertEquals($expected, $result);
1274
 
1275
		$hash = '6014b4e1c4f39eb62389712111dbe6435bec66cb%3AUserForm.hidden%7CUserForm.stuff';
1276
 
1277
		$result = $this->Form->secure($this->Form->fields);
1278
		$expected = array(
1279
			'div' => array('style' => 'display:none;'),
1280
			array('input' => array(
1281
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1282
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1283
			)),
1284
			array('input' => array(
1285
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1286
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
1287
			)),
1288
			'/div'
1289
		);
1290
		$this->assertTags($result, $expected);
1291
	}
1292
 
1293
/**
1294
 * Test secured inputs with custom names.
1295
 *
1296
 * @return void
1297
 */
1298
	public function testSecuredInputCustomName() {
1299
		$this->Form->request['_Token'] = array('key' => 'testKey');
1300
		$this->assertEquals(array(), $this->Form->fields);
1301
 
1302
		$this->Form->input('text_input', array(
1303
			'name' => 'data[Option][General.default_role]',
1304
		));
1305
		$expected = array('Option.General.default_role');
1306
		$this->assertEquals($expected, $this->Form->fields);
1307
 
1308
		$this->Form->input('select_box', array(
1309
			'name' => 'data[Option][General.select_role]',
1310
			'type' => 'select',
1311
			'options' => array(1, 2),
1312
		));
1313
		$expected = array('Option.General.default_role', 'Option.General.select_role');
1314
		$this->assertEquals($expected, $this->Form->fields);
1315
	}
1316
 
1317
/**
1318
 * Tests that the correct keys are added to the field hash index
1319
 *
1320
 * @return void
1321
 */
1322
	public function testSecuredFileInput() {
1323
		$this->Form->request['_Token'] = array('key' => 'testKey');
1324
		$this->assertEquals(array(), $this->Form->fields);
1325
 
1326
		$this->Form->file('Attachment.file');
1327
		$expected = array(
1328
			'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
1329
			'Attachment.file.error', 'Attachment.file.size'
1330
		);
1331
		$this->assertEquals($expected, $this->Form->fields);
1332
	}
1333
 
1334
/**
1335
 * test that multiple selects keys are added to field hash
1336
 *
1337
 * @return void
1338
 */
1339
	public function testSecuredMultipleSelect() {
1340
		$this->Form->request['_Token'] = array('key' => 'testKey');
1341
		$this->assertEquals(array(), $this->Form->fields);
1342
		$options = array('1' => 'one', '2' => 'two');
1343
 
1344
		$this->Form->select('Model.select', $options);
1345
		$expected = array('Model.select');
1346
		$this->assertEquals($expected, $this->Form->fields);
1347
 
1348
		$this->Form->fields = array();
1349
		$this->Form->select('Model.select', $options, array('multiple' => true));
1350
		$this->assertEquals($expected, $this->Form->fields);
1351
	}
1352
 
1353
/**
1354
 * testFormSecuredRadio method
1355
 *
1356
 * @return void
1357
 */
1358
	public function testSecuredRadio() {
1359
		$this->Form->request['_Token'] = array('key' => 'testKey');
1360
		$this->assertEquals(array(), $this->Form->fields);
1361
		$options = array('1' => 'option1', '2' => 'option2');
1362
 
1363
		$this->Form->radio('Test.test', $options);
1364
		$expected = array('Test.test');
1365
		$this->assertEquals($expected, $this->Form->fields);
1366
 
1367
		$this->Form->radio('Test.all', $options, array(
1368
			'disabled' => array('option1', 'option2')
1369
		));
1370
		$expected = array('Test.test', 'Test.all' => '');
1371
		$this->assertEquals($expected, $this->Form->fields);
1372
 
1373
		$this->Form->radio('Test.some', $options, array(
1374
			'disabled' => array('option1')
1375
		));
1376
		$expected = array('Test.test', 'Test.all' => '', 'Test.some');
1377
		$this->assertEquals($expected, $this->Form->fields);
1378
	}
1379
 
1380
/**
1381
 * Test that when disabled is in a list based attribute array it works.
1382
 *
1383
 * @return void
1384
 */
1385
	public function testSecuredAndDisabledNotAssoc() {
1386
		$this->Form->request['_Token'] = array('key' => 'testKey');
1387
 
1388
		$this->Form->select('Model.select', array(1, 2), array('disabled'));
1389
		$this->Form->checkbox('Model.checkbox', array('disabled'));
1390
		$this->Form->text('Model.text', array('disabled'));
1391
		$this->Form->textarea('Model.textarea', array('disabled'));
1392
		$this->Form->password('Model.password', array('disabled'));
1393
		$this->Form->radio('Model.radio', array(1, 2), array('disabled'));
1394
 
1395
		$expected = array(
1396
			'Model.radio' => ''
1397
		);
1398
		$this->assertEquals($expected, $this->Form->fields);
1399
	}
1400
 
1401
/**
1402
 * test that forms with disabled inputs + secured forms leave off the inputs from the form
1403
 * hashing.
1404
 *
1405
 * @return void
1406
 */
1407
	public function testSecuredAndDisabled() {
1408
		$this->Form->request['_Token'] = array('key' => 'testKey');
1409
 
1410
		$this->Form->checkbox('Model.checkbox', array('disabled' => true));
1411
		$this->Form->text('Model.text', array('disabled' => true));
1412
		$this->Form->text('Model.text2', array('disabled' => 'disabled'));
1413
		$this->Form->password('Model.password', array('disabled' => true));
1414
		$this->Form->textarea('Model.textarea', array('disabled' => true));
1415
		$this->Form->select('Model.select', array(1, 2), array('disabled' => true));
1416
		$this->Form->select('Model.select', array(1, 2), array('disabled' => array(1, 2)));
1417
		$this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
1418
		$this->Form->year('Model.year', null, null, array('disabled' => true));
1419
		$this->Form->month('Model.month', array('disabled' => true));
1420
		$this->Form->day('Model.day', array('disabled' => true));
1421
		$this->Form->hour('Model.hour', false, array('disabled' => true));
1422
		$this->Form->minute('Model.minute', array('disabled' => true));
1423
		$this->Form->meridian('Model.meridian', array('disabled' => true));
1424
 
1425
		$expected = array(
1426
			'Model.radio' => ''
1427
		);
1428
		$this->assertEquals($expected, $this->Form->fields);
1429
	}
1430
 
1431
/**
1432
 * Test that only the path + query elements of a form's URL show up in their hash.
1433
 *
1434
 * @return void
1435
 */
1436
	public function testSecuredFormUrlIgnoresHost() {
1437
		$this->Form->request['_Token'] = array('key' => 'testKey');
1438
 
1439
		$expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
1440
		$this->Form->create('Address', array(
1441
			'url' => array('controller' => 'articles', 'action' => 'view', 1, '?' => array('page' => 1))
1442
		));
1443
		$result = $this->Form->secure();
1444
		$this->assertContains($expected, $result);
1445
 
1446
		$this->Form->create('Address', array('url' => 'http://localhost/articles/view/1?page=1'));
1447
		$result = $this->Form->secure();
1448
		$this->assertContains($expected, $result, 'Full URL should only use path and query.');
1449
 
1450
		$this->Form->create('Address', array('url' => '/articles/view/1?page=1'));
1451
		$result = $this->Form->secure();
1452
		$this->assertContains($expected, $result, 'URL path + query should work.');
1453
 
1454
		$this->Form->create('Address', array('url' => '/articles/view/1'));
1455
		$result = $this->Form->secure();
1456
		$this->assertNotContains($expected, $result, 'URL is different');
1457
	}
1458
 
1459
/**
1460
 * Ensure named parameters work correctly with hash generation.
1461
 *
1462
 * @return void
1463
 */
1464
	public function testSecuredFormUrlWorksWithNamedParameter() {
1465
		$this->Form->request['_Token'] = array('key' => 'testKey');
1466
 
1467
		$expected = 'c890c5f041b1d83d1610dee8f52cd257df7ce618%3A';
1468
		$this->Form->create('Address', array(
1469
			'url' => array('controller' => 'articles', 'action' => 'view', 1, 'type' => 'red')
1470
		));
1471
		$result = $this->Form->secure();
1472
		$this->assertContains($expected, $result);
1473
	}
1474
 
1475
/**
1476
 * Test that URL, HTML and identifier show up in their hashs.
1477
 *
1478
 * @return void
1479
 */
1480
	public function testSecuredFormUrlHasHtmlAndIdentifier() {
1481
		$this->Form->request['_Token'] = array('key' => 'testKey');
1482
 
1483
		$expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
1484
		$this->Form->create('Address', array(
1485
			'url' => array(
1486
				'controller' => 'articles',
1487
				'action' => 'view',
1488
				'?' => array(
1489
					'page' => 1,
1490
					'limit' => 10,
1491
					'html' => '<>"',
1492
				),
1493
				'#' => 'result',
1494
			),
1495
		));
1496
		$result = $this->Form->secure();
1497
		$this->assertContains($expected, $result);
1498
 
1499
		$this->Form->create('Address', array('url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'));
1500
		$result = $this->Form->secure();
1501
		$this->assertContains($expected, $result, 'Full URL should only use path and query.');
1502
 
1503
		$this->Form->create('Address', array('url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'));
1504
		$result = $this->Form->secure();
1505
		$this->assertContains($expected, $result, 'URL path + query should work.');
1506
	}
1507
 
1508
/**
1509
 * testDisableSecurityUsingForm method
1510
 *
1511
 * @return void
1512
 */
1513
	public function testDisableSecurityUsingForm() {
1514
		$this->Form->request['_Token'] = array(
1515
			'key' => 'testKey',
1516
			'disabledFields' => array()
1517
		);
1518
		$this->Form->create();
1519
 
1520
		$this->Form->hidden('Addresses.id', array('value' => '123456'));
1521
		$this->Form->input('Addresses.title');
1522
		$this->Form->input('Addresses.first_name', array('secure' => false));
1523
		$this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
1524
		$this->Form->input('Addresses.zip', array(
1525
			'type' => 'select', 'options' => array(1, 2), 'secure' => false
1526
		));
1527
 
1528
		$result = $this->Form->fields;
1529
		$expected = array(
1530
			'Addresses.id' => '123456', 'Addresses.title',
1531
		);
1532
		$this->assertEquals($expected, $result);
1533
	}
1534
 
1535
/**
1536
 * test disableField
1537
 *
1538
 * @return void
1539
 */
1540
	public function testUnlockFieldAddsToList() {
1541
		$this->Form->request['_Token'] = array(
1542
			'key' => 'testKey',
1543
			'unlockedFields' => array()
1544
		);
1545
		$this->Form->create('Contact');
1546
		$this->Form->unlockField('Contact.name');
1547
		$this->Form->text('Contact.name');
1548
 
1549
		$this->assertEquals(array('Contact.name'), $this->Form->unlockField());
1550
		$this->assertEquals(array(), $this->Form->fields);
1551
	}
1552
 
1553
/**
1554
 * test unlockField removing from fields array.
1555
 *
1556
 * @return void
1557
 */
1558
	public function testUnlockFieldRemovingFromFields() {
1559
		$this->Form->request['_Token'] = array(
1560
			'key' => 'testKey',
1561
			'unlockedFields' => array()
1562
		);
1563
		$this->Form->create('Contact');
1564
		$this->Form->hidden('Contact.id', array('value' => 1));
1565
		$this->Form->text('Contact.name');
1566
 
1567
		$this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
1568
		$this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
1569
 
1570
		$this->Form->unlockField('Contact.name');
1571
		$this->Form->unlockField('Contact.id');
1572
		$this->assertEquals(array(), $this->Form->fields);
1573
	}
1574
 
1575
/**
1576
 * testTagIsInvalid method
1577
 *
1578
 * @return void
1579
 */
1580
	public function testTagIsInvalid() {
1581
		$Contact = ClassRegistry::getObject('Contact');
1582
		$Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
1583
 
1584
		$this->Form->setEntity('Contact.0.email');
1585
		$result = $this->Form->tagIsInvalid();
1586
		$this->assertEquals($expected, $result);
1587
 
1588
		$this->Form->setEntity('Contact.1.email');
1589
		$result = $this->Form->tagIsInvalid();
1590
		$this->assertFalse($result);
1591
 
1592
		$this->Form->setEntity('Contact.0.name');
1593
		$result = $this->Form->tagIsInvalid();
1594
		$this->assertFalse($result);
1595
	}
1596
 
1597
/**
1598
 * Test tagIsInvalid with validation errors from a saveMany
1599
 *
1600
 * @return void
1601
 */
1602
	public function testTagIsInvalidSaveMany() {
1603
		$Contact = ClassRegistry::getObject('Contact');
1604
		$Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
1605
 
1606
		$this->Form->create('Contact');
1607
 
1608
		$this->Form->setEntity('0.email');
1609
		$result = $this->Form->tagIsInvalid();
1610
		$this->assertEquals($expected, $result);
1611
 
1612
		$this->Form->setEntity('0.Contact.email');
1613
		$result = $this->Form->tagIsInvalid();
1614
		$this->assertEquals($expected, $result);
1615
	}
1616
 
1617
/**
1618
 * Test validation errors.
1619
 *
1620
 * @return void
1621
 */
1622
	public function testPasswordValidation() {
1623
		$Contact = ClassRegistry::getObject('Contact');
1624
		$Contact->validationErrors['password'] = array('Please provide a password');
1625
 
1626
		$result = $this->Form->input('Contact.password');
1627
		$expected = array(
1628
			'div' => array('class' => 'input password error'),
1629
			'label' => array('for' => 'ContactPassword'),
1630
			'Password',
1631
			'/label',
1632
			'input' => array(
1633
				'type' => 'password', 'name' => 'data[Contact][password]',
1634
				'id' => 'ContactPassword', 'class' => 'form-error'
1635
			),
1636
			array('div' => array('class' => 'error-message')),
1637
			'Please provide a password',
1638
			'/div',
1639
			'/div'
1640
		);
1641
		$this->assertTags($result, $expected);
1642
 
1643
		$result = $this->Form->input('Contact.password', array('errorMessage' => false));
1644
		$expected = array(
1645
			'div' => array('class' => 'input password error'),
1646
			'label' => array('for' => 'ContactPassword'),
1647
			'Password',
1648
			'/label',
1649
			'input' => array(
1650
				'type' => 'password', 'name' => 'data[Contact][password]',
1651
				'id' => 'ContactPassword', 'class' => 'form-error'
1652
			),
1653
			'/div'
1654
		);
1655
		$this->assertTags($result, $expected);
1656
	}
1657
 
1658
/**
1659
 * Test validation errors, when validation message is an empty string.
1660
 *
1661
 * @return void
1662
 */
1663
	public function testEmptyErrorValidation() {
1664
		$this->Form->validationErrors['Contact']['password'] = '';
1665
 
1666
		$result = $this->Form->input('Contact.password');
1667
		$expected = array(
1668
			'div' => array('class' => 'input password error'),
1669
			'label' => array('for' => 'ContactPassword'),
1670
			'Password',
1671
			'/label',
1672
			'input' => array(
1673
				'type' => 'password', 'name' => 'data[Contact][password]',
1674
				'id' => 'ContactPassword', 'class' => 'form-error'
1675
			),
1676
			array('div' => array('class' => 'error-message')),
1677
			array(),
1678
			'/div',
1679
			'/div'
1680
		);
1681
		$this->assertTags($result, $expected);
1682
 
1683
		$result = $this->Form->input('Contact.password', array('errorMessage' => false));
1684
		$expected = array(
1685
			'div' => array('class' => 'input password error'),
1686
			'label' => array('for' => 'ContactPassword'),
1687
			'Password',
1688
			'/label',
1689
			'input' => array(
1690
				'type' => 'password', 'name' => 'data[Contact][password]',
1691
				'id' => 'ContactPassword', 'class' => 'form-error'
1692
			),
1693
			'/div'
1694
		);
1695
		$this->assertTags($result, $expected);
1696
	}
1697
 
1698
/**
1699
 * Test validation errors, when calling input() overriding validation message by an empty string.
1700
 *
1701
 * @return void
1702
 */
1703
	public function testEmptyInputErrorValidation() {
1704
		$this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
1705
 
1706
		$result = $this->Form->input('Contact.password', array('error' => ''));
1707
		$expected = array(
1708
			'div' => array('class' => 'input password error'),
1709
			'label' => array('for' => 'ContactPassword'),
1710
			'Password',
1711
			'/label',
1712
			'input' => array(
1713
				'type' => 'password', 'name' => 'data[Contact][password]',
1714
				'id' => 'ContactPassword', 'class' => 'form-error'
1715
			),
1716
			array('div' => array('class' => 'error-message')),
1717
			array(),
1718
			'/div',
1719
			'/div'
1720
		);
1721
		$this->assertTags($result, $expected);
1722
 
1723
		$result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
1724
		$expected = array(
1725
			'div' => array('class' => 'input password error'),
1726
			'label' => array('for' => 'ContactPassword'),
1727
			'Password',
1728
			'/label',
1729
			'input' => array(
1730
				'type' => 'password', 'name' => 'data[Contact][password]',
1731
				'id' => 'ContactPassword', 'class' => 'form-error'
1732
			),
1733
			'/div'
1734
		);
1735
		$this->assertTags($result, $expected);
1736
	}
1737
 
1738
/**
1739
 * testFormValidationAssociated method
1740
 *
1741
 * test display of form errors in conjunction with model::validates.
1742
 *
1743
 * @return void
1744
 */
1745
	public function testFormValidationAssociated() {
1746
		$this->UserForm = ClassRegistry::getObject('UserForm');
1747
		$this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
1748
 
1749
		$data = array(
1750
			'UserForm' => array('name' => 'user'),
1751
			'OpenidUrl' => array('url' => 'http://www.cakephp.org')
1752
		);
1753
 
1754
		$result = $this->UserForm->OpenidUrl->create($data);
1755
		$this->assertFalse(empty($result));
1756
		$this->assertFalse($this->UserForm->OpenidUrl->validates());
1757
 
1758
		$result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
1759
		$encoding = strtolower(Configure::read('App.encoding'));
1760
		$expected = array(
1761
			'form' => array(
1762
				'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
1763
				'accept-charset' => $encoding
1764
			),
1765
			'div' => array('style' => 'display:none;'),
1766
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1767
			'/div'
1768
		);
1769
		$this->assertTags($result, $expected);
1770
 
1771
		$result = $this->Form->error(
1772
			'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
1773
		);
1774
		$this->assertEquals('Error, not registered', $result);
1775
 
1776
		unset($this->UserForm->OpenidUrl, $this->UserForm);
1777
	}
1778
 
1779
/**
1780
 * testFormValidationAssociatedFirstLevel method
1781
 *
1782
 * test form error display with associated model.
1783
 *
1784
 * @return void
1785
 */
1786
	public function testFormValidationAssociatedFirstLevel() {
1787
		$this->ValidateUser = ClassRegistry::getObject('ValidateUser');
1788
		$this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
1789
 
1790
		$data = array(
1791
			'ValidateUser' => array('name' => 'mariano'),
1792
			'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
1793
		);
1794
 
1795
		$result = $this->ValidateUser->create($data);
1796
		$this->assertFalse(empty($result));
1797
		$this->assertFalse($this->ValidateUser->validates());
1798
		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1799
 
1800
		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1801
		$encoding = strtolower(Configure::read('App.encoding'));
1802
		$expected = array(
1803
			'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
1804
			'div' => array('style' => 'display:none;'),
1805
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1806
			'/div'
1807
		);
1808
		$this->assertTags($result, $expected);
1809
 
1810
		$result = $this->Form->error(
1811
			'ValidateUser.email', 'Invalid email', array('wrap' => false)
1812
		);
1813
		$this->assertEquals('Invalid email', $result);
1814
 
1815
		$result = $this->Form->error(
1816
			'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
1817
		);
1818
		$this->assertEquals('Invalid name', $result);
1819
 
1820
		$result = $this->Form->error(
1821
			'ValidateProfile.city', 'Invalid city', array('wrap' => false)
1822
		);
1823
		$this->assertEquals('Invalid city', $result);
1824
 
1825
		unset($this->ValidateUser->ValidateProfile);
1826
		unset($this->ValidateUser);
1827
	}
1828
 
1829
/**
1830
 * testFormValidationAssociatedSecondLevel method
1831
 *
1832
 * test form error display with associated model.
1833
 *
1834
 * @return void
1835
 */
1836
	public function testFormValidationAssociatedSecondLevel() {
1837
		$this->ValidateUser = ClassRegistry::getObject('ValidateUser');
1838
		$this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
1839
		$this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
1840
 
1841
		$data = array(
1842
			'ValidateUser' => array('name' => 'mariano'),
1843
			'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
1844
			'ValidateItem' => array('name' => 'Item')
1845
		);
1846
 
1847
		$result = $this->ValidateUser->create($data);
1848
		$this->assertFalse(empty($result));
1849
		$this->assertFalse($this->ValidateUser->validates());
1850
		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1851
		$this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
1852
 
1853
		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1854
		$encoding = strtolower(Configure::read('App.encoding'));
1855
		$expected = array(
1856
			'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
1857
			'div' => array('style' => 'display:none;'),
1858
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1859
			'/div'
1860
		);
1861
		$this->assertTags($result, $expected);
1862
 
1863
		$result = $this->Form->error(
1864
			'ValidateUser.email', 'Invalid email', array('wrap' => false)
1865
		);
1866
		$this->assertEquals('Invalid email', $result);
1867
 
1868
		$result = $this->Form->error(
1869
			'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
1870
		);
1871
		$this->assertEquals('Invalid name', $result);
1872
 
1873
		$result = $this->Form->error(
1874
			'ValidateProfile.city', 'Invalid city', array('wrap' => false)
1875
		);
1876
 
1877
		$result = $this->Form->error(
1878
			'ValidateItem.description', 'Invalid description', array('wrap' => false)
1879
		);
1880
		$this->assertEquals('Invalid description', $result);
1881
 
1882
		unset($this->ValidateUser->ValidateProfile->ValidateItem);
1883
		unset($this->ValidateUser->ValidateProfile);
1884
		unset($this->ValidateUser);
1885
	}
1886
 
1887
/**
1888
 * testFormValidationMultiRecord method
1889
 *
1890
 * test form error display with multiple records.
1891
 *
1892
 * @return void
1893
 */
1894
	public function testFormValidationMultiRecord() {
1895
		$Contact = ClassRegistry::getObject('Contact');
1896
		$Contact->validationErrors[2] = array(
1897
			'name' => array('This field cannot be left blank')
1898
		);
1899
		$result = $this->Form->input('Contact.2.name');
1900
		$expected = array(
1901
			'div' => array('class' => 'input text error'),
1902
			'label' => array('for' => 'Contact2Name'),
1903
			'Name',
1904
			'/label',
1905
			'input' => array(
1906
				'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
1907
				'class' => 'form-error', 'maxlength' => 255
1908
			),
1909
			array('div' => array('class' => 'error-message')),
1910
			'This field cannot be left blank',
1911
			'/div',
1912
			'/div'
1913
		);
1914
		$this->assertTags($result, $expected);
1915
	}
1916
 
1917
/**
1918
 * testMultipleInputValidation method
1919
 *
1920
 * test multiple record form validation error display.
1921
 *
1922
 * @return void
1923
 */
1924
	public function testMultipleInputValidation() {
1925
		$Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
1926
		$Address->validationErrors[0] = array(
1927
			'title' => array('This field cannot be empty'),
1928
			'first_name' => array('This field cannot be empty')
1929
		);
1930
		$Address->validationErrors[1] = array(
1931
			'last_name' => array('You must have a last name')
1932
		);
1933
		$this->Form->create();
1934
 
1935
		$result = $this->Form->input('Address.0.title');
1936
		$expected = array(
1937
			'div' => array('class'),
1938
			'label' => array('for'),
1939
			'preg:/[^<]+/',
1940
			'/label',
1941
			'input' => array(
1942
				'type' => 'text', 'name', 'id', 'class' => 'form-error'
1943
			),
1944
			array('div' => array('class' => 'error-message')),
1945
			'This field cannot be empty',
1946
			'/div',
1947
			'/div'
1948
		);
1949
		$this->assertTags($result, $expected);
1950
 
1951
		$result = $this->Form->input('Address.0.first_name');
1952
		$expected = array(
1953
			'div' => array('class'),
1954
			'label' => array('for'),
1955
			'preg:/[^<]+/',
1956
			'/label',
1957
			'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
1958
			array('div' => array('class' => 'error-message')),
1959
			'This field cannot be empty',
1960
			'/div',
1961
			'/div'
1962
		);
1963
		$this->assertTags($result, $expected);
1964
 
1965
		$result = $this->Form->input('Address.0.last_name');
1966
		$expected = array(
1967
			'div' => array('class'),
1968
			'label' => array('for'),
1969
			'preg:/[^<]+/',
1970
			'/label',
1971
			'input' => array('type' => 'text', 'name', 'id'),
1972
			'/div'
1973
		);
1974
		$this->assertTags($result, $expected);
1975
 
1976
		$result = $this->Form->input('Address.1.last_name');
1977
		$expected = array(
1978
			'div' => array('class'),
1979
			'label' => array('for'),
1980
			'preg:/[^<]+/',
1981
			'/label',
1982
			'input' => array(
1983
				'type' => 'text', 'name', 'id',
1984
				'class' => 'form-error'
1985
			),
1986
			array('div' => array('class' => 'error-message')),
1987
			'You must have a last name',
1988
			'/div',
1989
			'/div'
1990
		);
1991
		$this->assertTags($result, $expected);
1992
	}
1993
 
1994
/**
1995
 * testInput method
1996
 *
1997
 * Test various incarnations of input().
1998
 *
1999
 * @return void
2000
 */
2001
	public function testInput() {
2002
		$result = $this->Form->input('ValidateUser.balance');
2003
		$expected = array(
2004
			'div' => array('class'),
2005
			'label' => array('for'),
2006
			'Balance',
2007
			'/label',
2008
			'input' => array('name', 'type' => 'number', 'id', 'step'),
2009
			'/div',
2010
		);
2011
		$this->assertTags($result, $expected);
2012
 
2013
		$result = $this->Form->input('ValidateUser.cost_decimal');
2014
		$expected = array(
2015
			'div' => array('class'),
2016
			'label' => array('for'),
2017
			'Cost Decimal',
2018
			'/label',
2019
			'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
2020
			'/div',
2021
		);
2022
		$this->assertTags($result, $expected);
2023
 
2024
		$result = $this->Form->input('ValidateUser.ratio');
2025
		$expected = array(
2026
			'div' => array('class'),
2027
			'label' => array('for'),
2028
			'Ratio',
2029
			'/label',
2030
			'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
2031
			'/div',
2032
		);
2033
		$this->assertTags($result, $expected);
2034
 
2035
		$result = $this->Form->input('ValidateUser.population');
2036
		$expected = array(
2037
			'div' => array('class'),
2038
			'label' => array('for'),
2039
			'Population',
2040
			'/label',
2041
			'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
2042
			'/div',
2043
		);
2044
		$this->assertTags($result, $expected);
2045
 
2046
		$result = $this->Form->input('Contact.email', array('id' => 'custom'));
2047
		$expected = array(
2048
			'div' => array('class' => 'input email'),
2049
			'label' => array('for' => 'custom'),
2050
			'Email',
2051
			'/label',
2052
			array('input' => array(
2053
				'type' => 'email', 'name' => 'data[Contact][email]',
2054
				'id' => 'custom', 'maxlength' => 255
2055
			)),
2056
			'/div'
2057
		);
2058
		$this->assertTags($result, $expected);
2059
 
2060
		$result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
2061
		$expected = array(
2062
			'<div',
2063
			'label' => array('for' => 'ContactEmail'),
2064
			'Email',
2065
			'/label',
2066
			array('input' => array(
2067
				'type' => 'email', 'name' => 'data[Contact][email]',
2068
				'id' => 'ContactEmail', 'maxlength' => 255
2069
			)),
2070
			'/div'
2071
		);
2072
		$this->assertTags($result, $expected);
2073
 
2074
		$result = $this->Form->hidden('Contact.idontexist');
2075
		$expected = array('input' => array(
2076
				'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
2077
				'id' => 'ContactIdontexist'
2078
		));
2079
		$this->assertTags($result, $expected);
2080
 
2081
		$result = $this->Form->input('Contact.email', array('type' => 'text'));
2082
		$expected = array(
2083
			'div' => array('class' => 'input text'),
2084
			'label' => array('for' => 'ContactEmail'),
2085
			'Email',
2086
			'/label',
2087
			array('input' => array(
2088
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]',
2089
				'id' => 'ContactEmail'
2090
			)),
2091
			'/div'
2092
		);
2093
		$this->assertTags($result, $expected);
2094
 
2095
		$result = $this->Form->input('Contact.5.email', array('type' => 'text'));
2096
		$expected = array(
2097
			'div' => array('class' => 'input text'),
2098
			'label' => array('for' => 'Contact5Email'),
2099
			'Email',
2100
			'/label',
2101
			array('input' => array(
2102
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][5][email]',
2103
				'id' => 'Contact5Email'
2104
			)),
2105
			'/div'
2106
		);
2107
		$this->assertTags($result, $expected);
2108
 
2109
		$result = $this->Form->input('Contact.password');
2110
		$expected = array(
2111
			'div' => array('class' => 'input password'),
2112
			'label' => array('for' => 'ContactPassword'),
2113
			'Password',
2114
			'/label',
2115
			array('input' => array(
2116
				'type' => 'password', 'name' => 'data[Contact][password]',
2117
				'id' => 'ContactPassword'
2118
			)),
2119
			'/div'
2120
		);
2121
		$this->assertTags($result, $expected);
2122
 
2123
		$result = $this->Form->input('Contact.email', array(
2124
			'type' => 'file', 'class' => 'textbox'
2125
		));
2126
		$expected = array(
2127
			'div' => array('class' => 'input file'),
2128
			'label' => array('for' => 'ContactEmail'),
2129
			'Email',
2130
			'/label',
2131
			array('input' => array(
2132
				'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
2133
				'id' => 'ContactEmail'
2134
			)),
2135
			'/div'
2136
		);
2137
		$this->assertTags($result, $expected);
2138
 
2139
		$this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
2140
		$result = $this->Form->input('Contact.phone');
2141
		$expected = array(
2142
			'div' => array('class' => 'input tel'),
2143
			'label' => array('for' => 'ContactPhone'),
2144
			'Phone',
2145
			'/label',
2146
			array('input' => array(
2147
				'type' => 'tel', 'name' => 'data[Contact][phone]',
2148
				'value' => 'Hello &amp; World &gt; weird chars',
2149
				'id' => 'ContactPhone', 'maxlength' => 255
2150
			)),
2151
			'/div'
2152
		);
2153
		$this->assertTags($result, $expected);
2154
 
2155
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
2156
		$result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
2157
		$expected = array(
2158
			'div' => array('class' => 'input text'),
2159
			'label' => array('for' => 'myId'),
2160
			'Field',
2161
			'/label',
2162
			'input' => array(
2163
				'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
2164
				'value' => 'My value', 'id' => 'myId'
2165
			),
2166
			'/div'
2167
		);
2168
		$this->assertTags($result, $expected);
2169
 
2170
		unset($this->Form->request->data);
2171
 
2172
		$Contact = ClassRegistry::getObject('Contact');
2173
		$Contact->validationErrors['field'] = array('Badness!');
2174
		$result = $this->Form->input('Contact.field');
2175
		$expected = array(
2176
			'div' => array('class' => 'input text error'),
2177
			'label' => array('for' => 'ContactField'),
2178
			'Field',
2179
			'/label',
2180
			'input' => array(
2181
				'type' => 'text', 'name' => 'data[Contact][field]',
2182
				'id' => 'ContactField', 'class' => 'form-error'
2183
			),
2184
			array('div' => array('class' => 'error-message')),
2185
			'Badness!',
2186
			'/div',
2187
			'/div'
2188
		);
2189
		$this->assertTags($result, $expected);
2190
 
2191
		$result = $this->Form->input('Contact.field', array(
2192
			'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
2193
		));
2194
		$expected = array(
2195
			'label' => array('for' => 'ContactField'),
2196
			'Field',
2197
			'/label',
2198
			'input' => array(
2199
				'type' => 'text', 'name' => 'data[Contact][field]',
2200
				'id' => 'ContactField', 'class' => 'form-error'
2201
			),
2202
			array('span' => array('class' => 'error-message')),
2203
			'Badness!',
2204
			'/span'
2205
		);
2206
		$this->assertTags($result, $expected);
2207
 
2208
		$result = $this->Form->input('Contact.field', array(
2209
			'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
2210
		));
2211
		$expected = array(
2212
			'div' => array('class' => 'input text error'),
2213
			'label' => array('for' => 'ContactField'),
2214
			'Field',
2215
			'/label',
2216
			'input' => array(
2217
				'type' => 'text', 'name' => 'data[Contact][field]',
2218
				'id' => 'ContactField', 'class' => 'form-error'
2219
			),
2220
			array('div' => array('class' => 'error')),
2221
			'Badness!',
2222
			'/div'
2223
		);
2224
		$this->assertTags($result, $expected);
2225
 
2226
		$result = $this->Form->input('Contact.field', array(
2227
			'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
2228
		));
2229
		$expected = array(
2230
			'span' => array('class' => 'input text error'),
2231
			'label' => array('for' => 'ContactField'),
2232
			'Field',
2233
			'/label',
2234
			'input' => array(
2235
				'type' => 'text', 'name' => 'data[Contact][field]',
2236
				'id' => 'ContactField', 'class' => 'form-error'
2237
			),
2238
			'Badness!',
2239
			'/span'
2240
		);
2241
		$this->assertTags($result, $expected);
2242
 
2243
		$result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
2244
		$expected = array(
2245
			'div' => array('class' => 'input text error'),
2246
			'label' => array('for' => 'ContactField'),
2247
			'Field',
2248
			'/label',
2249
			'input' => array(
2250
				'type' => 'text', 'name' => 'data[Contact][field]',
2251
				'id' => 'ContactField', 'class' => 'form-error'
2252
			),
2253
			'A message to you, Rudy',
2254
			array('div' => array('class' => 'error-message')),
2255
			'Badness!',
2256
			'/div',
2257
			'/div'
2258
		);
2259
		$this->assertTags($result, $expected);
2260
 
2261
		$this->Form->setEntity(null);
2262
		$this->Form->setEntity('Contact.field');
2263
		$result = $this->Form->input('Contact.field', array(
2264
			'after' => 'A message to you, Rudy', 'error' => false
2265
		));
2266
		$expected = array(
2267
			'div' => array('class' => 'input text'),
2268
			'label' => array('for' => 'ContactField'),
2269
			'Field',
2270
			'/label',
2271
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2272
			'A message to you, Rudy',
2273
			'/div'
2274
		);
2275
		$this->assertTags($result, $expected);
2276
 
2277
		$result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
2278
		$expected = array(
2279
			'div' => array('class' => 'input text'),
2280
			'label' => array('for' => 'ObjectField'),
2281
			'Field',
2282
			'/label',
2283
			'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
2284
			'A message to you, Rudy',
2285
			'/div'
2286
		);
2287
		$this->assertTags($result, $expected);
2288
 
2289
		$Contact->validationErrors['field'] = array('minLength');
2290
		$result = $this->Form->input('Contact.field', array(
2291
			'error' => array(
2292
				'minLength' => 'Le login doit contenir au moins 2 caractères',
2293
				'maxLength' => 'login too large'
2294
			)
2295
		));
2296
		$expected = array(
2297
			'div' => array('class' => 'input text error'),
2298
			'label' => array('for' => 'ContactField'),
2299
			'Field',
2300
			'/label',
2301
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2302
			array('div' => array('class' => 'error-message')),
2303
			'Le login doit contenir au moins 2 caractères',
2304
			'/div',
2305
			'/div'
2306
		);
2307
		$this->assertTags($result, $expected);
2308
 
2309
		$Contact->validationErrors['field'] = array('maxLength');
2310
		$result = $this->Form->input('Contact.field', array(
2311
			'error' => array(
2312
				'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
2313
				'minLength' => 'Le login doit contenir au moins 2 caractères',
2314
				'maxLength' => 'login too large',
2315
			)
2316
		));
2317
		$expected = array(
2318
			'div' => array('class' => 'input text error'),
2319
			'label' => array('for' => 'ContactField'),
2320
			'Field',
2321
			'/label',
2322
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2323
			array('span' => array('class' => 'error-message', 'rel' => 'fake')),
2324
			'login too large',
2325
			'/span',
2326
			'/div'
2327
		);
2328
		$this->assertTags($result, $expected);
2329
	}
2330
 
2331
/**
2332
 * Test that inputs with 0 can be created.
2333
 *
2334
 * @return void
2335
 */
2336
	public function testInputZero() {
2337
		$this->Form->create('User');
2338
		$result = $this->Form->input('0');
2339
		$expected = array(
2340
			'div' => array('class' => 'input text'),
2341
			'label' => array('for' => 'User0'), '/label',
2342
			'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
2343
			'/div'
2344
		);
2345
		$this->assertTags($result, $expected);
2346
	}
2347
 
2348
/**
2349
 * test input() with checkbox creation
2350
 *
2351
 * @return void
2352
 */
2353
	public function testInputCheckbox() {
2354
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
2355
		$expected = array(
2356
			'div' => array('class' => 'input checkbox'),
2357
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2358
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2359
			'/div'
2360
		);
2361
		$this->assertTags($result, $expected);
2362
 
2363
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
2364
		$expected = array(
2365
			'div' => array('class' => 'input checkbox'),
2366
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2367
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2368
			'/div'
2369
		);
2370
		$this->assertTags($result, $expected);
2371
 
2372
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
2373
		$expected = array(
2374
			'div' => array('class' => 'input checkbox'),
2375
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2376
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2377
			'/div'
2378
		);
2379
		$this->assertTags($result, $expected);
2380
 
2381
		$result = $this->Form->input('User.disabled', array(
2382
			'label' => 'Disabled',
2383
			'type' => 'checkbox',
2384
			'data-foo' => 'disabled'
2385
		));
2386
		$expected = array(
2387
			'div' => array('class' => 'input checkbox'),
2388
			'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
2389
			array('input' => array(
2390
				'type' => 'checkbox',
2391
				'name' => 'data[User][disabled]',
2392
				'value' => '1',
2393
				'id' => 'UserDisabled',
2394
				'data-foo' => 'disabled'
2395
			)),
2396
			'label' => array('for' => 'UserDisabled'),
2397
			'Disabled',
2398
			'/label',
2399
			'/div'
2400
		);
2401
		$this->assertTags($result, $expected);
2402
	}
2403
 
2404
/**
2405
 * test form->input() with time types.
2406
 *
2407
 * @return void
2408
 */
2409
	public function testInputTime() {
2410
		extract($this->dateRegex);
2411
		$result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
2412
		$result = explode(':', $result);
2413
		$this->assertRegExp('/option value="23"/', $result[0]);
2414
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2415
 
2416
		$result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
2417
		$result = explode(':', $result);
2418
		$this->assertRegExp('/option value="23"/', $result[0]);
2419
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2420
 
2421
		$result = $this->Form->input('Model.field', array(
2422
			'type' => 'time', 'timeFormat' => 24, 'interval' => 15
2423
		));
2424
		$result = explode(':', $result);
2425
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2426
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2427
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2428
 
2429
		$result = $this->Form->input('Model.field', array(
2430
			'type' => 'time', 'timeFormat' => 12, 'interval' => 15
2431
		));
2432
		$result = explode(':', $result);
2433
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2434
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2435
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2436
 
2437
		$result = $this->Form->input('prueba', array(
2438
			'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
2439
			'maxYear' => date('Y') + 1, 'interval' => 15
2440
		));
2441
		$result = explode(':', $result);
2442
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2443
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2444
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2445
		$this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
2446
 
2447
		$result = $this->Form->input('Random.start_time', array(
2448
			'type' => 'time',
2449
			'selected' => '18:15'
2450
		));
2451
		$this->assertContains('<option value="06" selected="selected">6</option>', $result);
2452
		$this->assertContains('<option value="15" selected="selected">15</option>', $result);
2453
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2454
 
2455
		$result = $this->Form->input('published', array('type' => 'time'));
2456
		$now = strtotime('now');
2457
		$this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
2458
 
2459
		$now = strtotime('2013-03-09 00:42:21');
2460
		$result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
2461
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2462
		$this->assertContains('<option value="42" selected="selected">42</option>', $result);
2463
	}
2464
 
2465
/**
2466
 * Test interval + selected near the hour roll over.
2467
 *
2468
 * @return void
2469
 */
2470
	public function testTimeSelectedWithInterval() {
2471
		$result = $this->Form->input('Model.start_time', array(
2472
			'type' => 'time',
2473
			'interval' => 15,
2474
			'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
2475
		));
2476
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2477
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2478
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2479
 
2480
		$result = $this->Form->input('Model.start_time', array(
2481
			'type' => 'time',
2482
			'interval' => 15,
2483
			'selected' => '2012-10-23 15:57:00'
2484
		));
2485
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2486
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2487
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2488
 
2489
		$result = $this->Form->input('Model.start_time', array(
2490
			'timeFormat' => 24,
2491
			'type' => 'time',
2492
			'interval' => 15,
2493
			'selected' => '15:57'
2494
		));
2495
		$this->assertContains('<option value="16" selected="selected">16</option>', $result);
2496
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2497
 
2498
		$result = $this->Form->input('Model.start_time', array(
2499
			'timeFormat' => 24,
2500
			'type' => 'time',
2501
			'interval' => 15,
2502
			'selected' => '23:57'
2503
		));
2504
		$this->assertContains('<option value="00" selected="selected">0</option>', $result);
2505
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2506
 
2507
		$result = $this->Form->input('Model.created', array(
2508
			'timeFormat' => 24,
2509
			'type' => 'datetime',
2510
			'interval' => 15,
2511
			'selected' => '2012-09-30 23:56'
2512
		));
2513
		$this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
2514
		$this->assertContains('<option value="10" selected="selected">October</option>', $result);
2515
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2516
		$this->assertContains('<option value="00" selected="selected">0</option>', $result);
2517
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2518
	}
2519
 
2520
/**
2521
 * Test time with selected values around 12:xx:xx
2522
 *
2523
 * @return void
2524
 */
2525
	public function testTimeSelectedWithIntervalTwelve() {
2526
		$result = $this->Form->input('Model.start_time', array(
2527
			'type' => 'time',
2528
			'timeFormat' => 12,
2529
			'interval' => 15,
2530
			'selected' => '00:00:00'
2531
		));
2532
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2533
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2534
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2535
 
2536
		$result = $this->Form->input('Model.start_time', array(
2537
			'type' => 'time',
2538
			'timeFormat' => 12,
2539
			'interval' => 15,
2540
			'selected' => '12:00:00'
2541
		));
2542
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2543
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2544
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2545
 
2546
		$result = $this->Form->input('Model.start_time', array(
2547
			'type' => 'time',
2548
			'timeFormat' => 12,
2549
			'interval' => 15,
2550
			'selected' => '12:15:00'
2551
		));
2552
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2553
		$this->assertContains('<option value="15" selected="selected">15</option>', $result);
2554
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2555
	}
2556
 
2557
/**
2558
 * Test interval & timeFormat = 12
2559
 *
2560
 * @return void
2561
 */
2562
	public function testInputTimeWithIntervalAnd12HourFormat() {
2563
		$result = $this->Form->input('Model.start_time', array(
2564
			'type' => 'time',
2565
			'timeFormat' => 12,
2566
			'interval' => 5,
2567
			'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
2568
		));
2569
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2570
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2571
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2572
 
2573
		$result = $this->Form->input('Model.start_time', array(
2574
			'type' => 'time',
2575
			'timeFormat' => '12',
2576
			'interval' => 5,
2577
			'selected' => '2013-04-19 16:30:00'
2578
		));
2579
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2580
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2581
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2582
 
2583
		$result = $this->Form->input('Model.start_time', array(
2584
			'type' => 'time',
2585
			'timeFormat' => '12',
2586
			'interval' => 10,
2587
			'selected' => '2013-05-19 00:33:00'
2588
		));
2589
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2590
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2591
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2592
 
2593
		$result = $this->Form->input('Model.start_time', array(
2594
			'type' => 'time',
2595
			'timeFormat' => '12',
2596
			'interval' => 10,
2597
			'selected' => '2013-05-19 13:33:00'
2598
		));
2599
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2600
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2601
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2602
 
2603
		$result = $this->Form->input('Model.start_time', array(
2604
			'type' => 'time',
2605
			'timeFormat' => '12',
2606
			'interval' => 10,
2607
			'selected' => '2013-05-19 01:33:00'
2608
		));
2609
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2610
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2611
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2612
	}
2613
 
2614
/**
2615
 * test form->input() with datetime, date and time types
2616
 *
2617
 * @return void
2618
 */
2619
	public function testInputDatetime() {
2620
		extract($this->dateRegex);
2621
		$result = $this->Form->input('prueba', array(
2622
			'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
2623
			'maxYear' => date('Y') + 1, 'interval' => 15
2624
		));
2625
		$result = explode(':', $result);
2626
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2627
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2628
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2629
		$this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
2630
 
2631
		//related to ticket #5013
2632
		$result = $this->Form->input('Contact.date', array(
2633
			'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
2634
		));
2635
		$this->assertRegExp('/class="customClass"/', $result);
2636
		$this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
2637
 
2638
		$result = $this->Form->input('Contact.date', array(
2639
			'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
2640
		));
2641
		$this->assertRegExp('/id="customIdDay"/', $result);
2642
		$this->assertRegExp('/id="customIdMonth"/', $result);
2643
		$this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
2644
 
2645
		$result = $this->Form->input('Model.field', array(
2646
			'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
2647
		));
2648
		$this->assertRegExp('/id="customIDDay"/', $result);
2649
		$this->assertRegExp('/id="customIDHour"/', $result);
2650
		$result = explode('</select><select', $result);
2651
		$result = explode(':', $result[1]);
2652
		$this->assertRegExp('/option value="23"/', $result[0]);
2653
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2654
 
2655
		$result = $this->Form->input('Model.field', array(
2656
			'type' => 'datetime', 'timeFormat' => 12
2657
		));
2658
		$result = explode('</select><select', $result);
2659
		$result = explode(':', $result[1]);
2660
		$this->assertRegExp('/option value="12"/', $result[0]);
2661
		$this->assertNotRegExp('/option value="13"/', $result[0]);
2662
 
2663
		$this->Form->request->data = array('Contact' => array('created' => null));
2664
		$result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
2665
		$expected = array(
2666
			'div' => array('class' => 'input date'),
2667
			'label' => array('for' => 'ContactCreatedMonth'),
2668
			'Created',
2669
			'/label',
2670
			array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
2671
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2672
			$monthsRegex,
2673
			'/select', '-',
2674
			array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
2675
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2676
			$daysRegex,
2677
			'/select', '-',
2678
			array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
2679
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2680
			$yearsRegex,
2681
			'/select',
2682
			'/div'
2683
		);
2684
		$this->assertTags($result, $expected);
2685
 
2686
		$this->Form->request->data = array('Contact' => array('created' => null));
2687
		$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
2688
		$this->assertRegExp('/for\="ContactCreatedHour"/', $result);
2689
 
2690
		$this->Form->request->data = array('Contact' => array('created' => null));
2691
		$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
2692
		$this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
2693
 
2694
		$result = $this->Form->input('Contact.created', array(
2695
			'type' => 'date',
2696
			'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
2697
			'timeFormat' => 'NONE'
2698
		));
2699
		$this->assertRegExp('/for\="created-month"/', $result);
2700
	}
2701
 
2702
/**
2703
 * Test generating checkboxes in a loop.
2704
 *
2705
 * @return void
2706
 */
2707
	public function testInputCheckboxesInLoop() {
2708
		for ($i = 1; $i < 5; $i++) {
2709
			$result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
2710
			$expected = array(
2711
				'div' => array('class' => 'input checkbox'),
2712
				'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
2713
				array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
2714
				'label' => array('for' => "Contact{$i}Email"),
2715
				'Email',
2716
				'/label',
2717
				'/div'
2718
			);
2719
			$this->assertTags($result, $expected);
2720
		}
2721
	}
2722
 
2723
/**
2724
 * Test generating checkboxes with disabled elements.
2725
 *
2726
 * @return void
2727
 */
2728
	public function testInputCheckboxWithDisabledElements() {
2729
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
2730
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
2731
 
2732
		$expected = array(
2733
			array('div' => array('class' => 'input select')),
2734
			array('label' => array('for' => "ContactMultiple")),
2735
			'Multiple',
2736
			'/label',
2737
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple", 'disabled' => 'disabled')),
2738
			array('div' => array('class' => 'checkbox')),
2739
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
2740
			array('label' => array('for' => "ContactMultiple1")),
2741
			'One',
2742
			'/label',
2743
			'/div',
2744
			array('div' => array('class' => 'checkbox')),
2745
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
2746
			array('label' => array('for' => "ContactMultiple2")),
2747
			'Two',
2748
			'/label',
2749
			'/div',
2750
			array('div' => array('class' => 'checkbox')),
2751
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
2752
			array('label' => array('for' => "ContactMultiple3")),
2753
			'Three',
2754
			'/label',
2755
			'/div',
2756
			'/div'
2757
		);
2758
		$this->assertTags($result, $expected);
2759
 
2760
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
2761
		$this->assertTags($result, $expected);
2762
 
2763
		$disabled = array('2', 3);
2764
 
2765
		$expected = array(
2766
			array('div' => array('class' => 'input select')),
2767
			array('label' => array('for' => "ContactMultiple")),
2768
			'Multiple',
2769
			'/label',
2770
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
2771
			array('div' => array('class' => 'checkbox')),
2772
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
2773
			array('label' => array('for' => "ContactMultiple1")),
2774
			'One',
2775
			'/label',
2776
			'/div',
2777
			array('div' => array('class' => 'checkbox')),
2778
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
2779
			array('label' => array('for' => "ContactMultiple2")),
2780
			'Two',
2781
			'/label',
2782
			'/div',
2783
			array('div' => array('class' => 'checkbox')),
2784
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
2785
			array('label' => array('for' => "ContactMultiple3")),
2786
			'Three',
2787
			'/label',
2788
			'/div',
2789
			'/div'
2790
		);
2791
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
2792
		$this->assertTags($result, $expected);
2793
 
2794
		// make sure 50 does only disable 50, and not 50f5c0cf
2795
		$options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
2796
		$disabled = array(50);
2797
 
2798
		$expected = array(
2799
			array('div' => array('class' => 'input select')),
2800
			array('label' => array('for' => "ContactMultiple")),
2801
			'Multiple',
2802
			'/label',
2803
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
2804
			array('div' => array('class' => 'checkbox')),
2805
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
2806
			array('label' => array('for' => "ContactMultiple50")),
2807
			'Fifty',
2808
			'/label',
2809
			'/div',
2810
			array('div' => array('class' => 'checkbox')),
2811
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
2812
			array('label' => array('for' => "ContactMultiple50f5c0cf")),
2813
			'Stringy',
2814
			'/label',
2815
			'/div',
2816
			'/div'
2817
		);
2818
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
2819
		$this->assertTags($result, $expected);
2820
	}
2821
 
2822
/**
2823
 * test input name with leading integer, ensure attributes are generated correctly.
2824
 *
2825
 * @return void
2826
 */
2827
	public function testInputWithLeadingInteger() {
2828
		$result = $this->Form->text('0.Node.title');
2829
		$expected = array(
2830
			'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
2831
		);
2832
		$this->assertTags($result, $expected);
2833
	}
2834
 
2835
/**
2836
 * test form->input() with select type inputs.
2837
 *
2838
 * @return void
2839
 */
2840
	public function testInputSelectType() {
2841
		$result = $this->Form->input('email', array(
2842
			'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
2843
		);
2844
		$expected = array(
2845
			'div' => array('class' => 'input select'),
2846
			'label' => array('for' => 'email'),
2847
			'Email',
2848
			'/label',
2849
			array('select' => array('name' => 'data[email]', 'id' => 'email')),
2850
			array('option' => array('value' => '')),
2851
			'/option',
2852
			array('option' => array('value' => 'è')),
2853
			'Firést',
2854
			'/option',
2855
			array('option' => array('value' => 'é')),
2856
			'Secoènd',
2857
			'/option',
2858
			'/select',
2859
			'/div'
2860
		);
2861
		$this->assertTags($result, $expected);
2862
 
2863
		$result = $this->Form->input('email', array(
2864
			'options' => array('First', 'Second'), 'empty' => true)
2865
		);
2866
		$expected = array(
2867
			'div' => array('class' => 'input select'),
2868
			'label' => array('for' => 'email'),
2869
			'Email',
2870
			'/label',
2871
			array('select' => array('name' => 'data[email]', 'id' => 'email')),
2872
			array('option' => array('value' => '')),
2873
			'/option',
2874
			array('option' => array('value' => '0')),
2875
			'First',
2876
			'/option',
2877
			array('option' => array('value' => '1')),
2878
			'Second',
2879
			'/option',
2880
			'/select',
2881
			'/div'
2882
		);
2883
		$this->assertTags($result, $expected);
2884
 
2885
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2886
		$this->Form->request->data = array('Model' => array('user_id' => 'value'));
2887
 
2888
		$result = $this->Form->input('Model.user_id', array('empty' => true));
2889
		$expected = array(
2890
			'div' => array('class' => 'input select'),
2891
			'label' => array('for' => 'ModelUserId'),
2892
			'User',
2893
			'/label',
2894
			'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
2895
			array('option' => array('value' => '')),
2896
			'/option',
2897
			array('option' => array('value' => 'value', 'selected' => 'selected')),
2898
			'good',
2899
			'/option',
2900
			array('option' => array('value' => 'other')),
2901
			'bad',
2902
			'/option',
2903
			'/select',
2904
			'/div'
2905
		);
2906
		$this->assertTags($result, $expected);
2907
 
2908
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2909
		$this->Form->request->data = array('Thing' => array('user_id' => null));
2910
		$result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
2911
		$expected = array(
2912
			'div' => array('class' => 'input select'),
2913
			'label' => array('for' => 'ThingUserId'),
2914
			'User',
2915
			'/label',
2916
			'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
2917
			array('option' => array('value' => '')),
2918
			'Some Empty',
2919
			'/option',
2920
			array('option' => array('value' => 'value')),
2921
			'good',
2922
			'/option',
2923
			array('option' => array('value' => 'other')),
2924
			'bad',
2925
			'/option',
2926
			'/select',
2927
			'/div'
2928
		);
2929
		$this->assertTags($result, $expected);
2930
 
2931
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2932
		$this->Form->request->data = array('Thing' => array('user_id' => 'value'));
2933
		$result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
2934
		$expected = array(
2935
			'div' => array('class' => 'input select'),
2936
			'label' => array('for' => 'ThingUserId'),
2937
			'User',
2938
			'/label',
2939
			'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
2940
			array('option' => array('value' => '')),
2941
			'Some Empty',
2942
			'/option',
2943
			array('option' => array('value' => 'value', 'selected' => 'selected')),
2944
			'good',
2945
			'/option',
2946
			array('option' => array('value' => 'other')),
2947
			'bad',
2948
			'/option',
2949
			'/select',
2950
			'/div'
2951
		);
2952
		$this->assertTags($result, $expected);
2953
 
2954
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2955
		$this->Form->request->data = array('User' => array('User' => array('value')));
2956
		$result = $this->Form->input('User.User', array('empty' => true));
2957
		$expected = array(
2958
			'div' => array('class' => 'input select'),
2959
			'label' => array('for' => 'UserUser'),
2960
			'User',
2961
			'/label',
2962
			'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
2963
			'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
2964
			array('option' => array('value' => '')),
2965
			'/option',
2966
			array('option' => array('value' => 'value', 'selected' => 'selected')),
2967
			'good',
2968
			'/option',
2969
			array('option' => array('value' => 'other')),
2970
			'bad',
2971
			'/option',
2972
			'/select',
2973
			'/div'
2974
		);
2975
		$this->assertTags($result, $expected);
2976
 
2977
		$this->Form->data = array();
2978
		$result = $this->Form->input('Publisher.id', array(
2979
				'label'		=> 'Publisher',
2980
				'type'		=> 'select',
2981
				'multiple'	=> 'checkbox',
2982
				'options'	=> array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
2983
		));
2984
		$expected = array(
2985
			array('div' => array('class' => 'input select')),
2986
				array('label' => array('for' => 'PublisherId')),
2987
				'Publisher',
2988
				'/label',
2989
				'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
2990
				array('div' => array('class' => 'checkbox')),
2991
				array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
2992
				array('label' => array('for' => 'PublisherIdValue1')),
2993
				'Label 1',
2994
				'/label',
2995
				'/div',
2996
				array('div' => array('class' => 'checkbox')),
2997
				array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
2998
				array('label' => array('for' => 'PublisherIdValue2')),
2999
				'Label 2',
3000
				'/label',
3001
				'/div',
3002
			'/div'
3003
		);
3004
		$this->assertTags($result, $expected);
3005
	}
3006
 
3007
/**
3008
 * test that input() and a non standard primary key makes a hidden input by default.
3009
 *
3010
 * @return void
3011
 */
3012
	public function testInputWithNonStandardPrimaryKeyMakesHidden() {
3013
		$this->Form->create('User');
3014
		$this->Form->fieldset = array(
3015
			'User' => array(
3016
				'fields' => array(
3017
					'model_id' => array('type' => 'integer')
3018
				),
3019
				'validates' => array(),
3020
				'key' => 'model_id'
3021
			)
3022
		);
3023
		$result = $this->Form->input('model_id');
3024
		$expected = array(
3025
			'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
3026
		);
3027
		$this->assertTags($result, $expected);
3028
	}
3029
 
3030
/**
3031
 * test that overriding the magic select type widget is possible
3032
 *
3033
 * @return void
3034
 */
3035
	public function testInputOverridingMagicSelectType() {
3036
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3037
		$result = $this->Form->input('Model.user_id', array('type' => 'text'));
3038
		$expected = array(
3039
			'div' => array('class' => 'input text'),
3040
			'label' => array('for' => 'ModelUserId'), 'User', '/label',
3041
			'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
3042
			'/div'
3043
		);
3044
		$this->assertTags($result, $expected);
3045
 
3046
		//Check that magic types still work for plural/singular vars
3047
		$this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
3048
		$result = $this->Form->input('Model.type');
3049
		$expected = array(
3050
			'div' => array('class' => 'input select'),
3051
			'label' => array('for' => 'ModelType'), 'Type', '/label',
3052
			'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
3053
			array('option' => array('value' => 'value')), 'good', '/option',
3054
			array('option' => array('value' => 'other')), 'bad', '/option',
3055
			'/select',
3056
			'/div'
3057
		);
3058
		$this->assertTags($result, $expected);
3059
	}
3060
 
3061
/**
3062
 * Test that inferred types do not override developer input
3063
 *
3064
 * @return void
3065
 */
3066
	public function testInputMagicTypeDoesNotOverride() {
3067
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3068
		$result = $this->Form->input('Model.user', array('type' => 'checkbox'));
3069
		$expected = array(
3070
			'div' => array('class' => 'input checkbox'),
3071
			array('input' => array(
3072
				'type' => 'hidden',
3073
				'name' => 'data[Model][user]',
3074
				'id' => 'ModelUser_',
3075
				'value' => 0,
3076
			)),
3077
			array('input' => array(
3078
				'name' => 'data[Model][user]',
3079
				'type' => 'checkbox',
3080
				'id' => 'ModelUser',
3081
				'value' => 1
3082
			)),
3083
			'label' => array('for' => 'ModelUser'), 'User', '/label',
3084
			'/div'
3085
		);
3086
		$this->assertTags($result, $expected);
3087
	}
3088
 
3089
/**
3090
 * Test that magic input() selects are created for type=number
3091
 *
3092
 * @return void
3093
 */
3094
	public function testInputMagicSelectForTypeNumber() {
3095
		$this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
3096
		$this->Form->request->data = array('ValidateUser' => array('balance' => 1));
3097
		$result = $this->Form->input('ValidateUser.balance');
3098
		$expected = array(
3099
			'div' => array('class' => 'input select'),
3100
			'label' => array('for' => 'ValidateUserBalance'),
3101
			'Balance',
3102
			'/label',
3103
			'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
3104
			array('option' => array('value' => '0')),
3105
			'nothing',
3106
			'/option',
3107
			array('option' => array('value' => '1', 'selected' => 'selected')),
3108
			'some',
3109
			'/option',
3110
			array('option' => array('value' => '100')),
3111
			'a lot',
3112
			'/option',
3113
			'/select',
3114
			'/div'
3115
		);
3116
		$this->assertTags($result, $expected);
3117
	}
3118
 
3119
/**
3120
 * Test that magic input() selects can easily be converted into radio types without error.
3121
 *
3122
 * @return void
3123
 */
3124
	public function testInputMagicSelectChangeToRadio() {
3125
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3126
		$result = $this->Form->input('Model.user_id', array('type' => 'radio'));
3127
		$this->assertRegExp('/input type="radio"/', $result);
3128
	}
3129
 
3130
/**
3131
 * fields with the same name as the model should work.
3132
 *
3133
 * @return void
3134
 */
3135
	public function testInputWithMatchingFieldAndModelName() {
3136
		$this->Form->create('User');
3137
		$this->Form->fieldset = array(
3138
			'User' => array(
3139
				'fields' => array(
3140
					'User' => array('type' => 'text')
3141
				),
3142
				'validates' => array(),
3143
				'key' => 'id'
3144
			)
3145
		);
3146
		$this->Form->request->data['User']['User'] = 'ABC, Inc.';
3147
		$result = $this->Form->input('User', array('type' => 'text'));
3148
		$expected = array(
3149
			'div' => array('class' => 'input text'),
3150
			'label' => array('for' => 'UserUser'), 'User', '/label',
3151
			'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
3152
			'/div'
3153
		);
3154
		$this->assertTags($result, $expected);
3155
	}
3156
 
3157
/**
3158
 * testFormInputs method
3159
 *
3160
 * test correct results from form::inputs().
3161
 *
3162
 * @return void
3163
 */
3164
	public function testFormInputs() {
3165
		$this->Form->create('Contact');
3166
		$result = $this->Form->inputs('The Legend');
3167
		$expected = array(
3168
			'<fieldset',
3169
			'<legend',
3170
			'The Legend',
3171
			'/legend',
3172
			'*/fieldset',
3173
		);
3174
		$this->assertTags($result, $expected);
3175
 
3176
		$result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
3177
		$expected = array(
3178
			'fieldset' => array('class' => 'classy-stuff'),
3179
			'<legend',
3180
			'Field of Dreams',
3181
			'/legend',
3182
			'*/fieldset'
3183
		);
3184
		$this->assertTags($result, $expected);
3185
 
3186
		$result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
3187
		$this->assertTags($result, $expected);
3188
 
3189
		$result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
3190
		$this->assertTags($result, $expected);
3191
 
3192
		$this->Form->create('Contact');
3193
		$this->Form->request['prefix'] = 'admin';
3194
		$this->Form->request['action'] = 'admin_edit';
3195
		$result = $this->Form->inputs();
3196
		$expected = array(
3197
			'<fieldset',
3198
			'<legend',
3199
			'Edit Contact',
3200
			'/legend',
3201
			'*/fieldset',
3202
		);
3203
		$this->assertTags($result, $expected);
3204
 
3205
		$this->Form->create('Contact');
3206
		$result = $this->Form->inputs(false);
3207
		$expected = array(
3208
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3209
			array('div' => array('class' => 'input text')),
3210
			'*/div',
3211
			array('div' => array('class' => 'input email')),
3212
			'*/div',
3213
			array('div' => array('class' => 'input tel')),
3214
			'*/div',
3215
			array('div' => array('class' => 'input password')),
3216
			'*/div',
3217
			array('div' => array('class' => 'input date')),
3218
			'*/div',
3219
			array('div' => array('class' => 'input date')),
3220
			'*/div',
3221
			array('div' => array('class' => 'input datetime')),
3222
			'*/div',
3223
			array('div' => array('class' => 'input number')),
3224
			'*/div',
3225
			array('div' => array('class' => 'input select')),
3226
			'*/div',
3227
		);
3228
		$this->assertTags($result, $expected);
3229
 
3230
		$this->Form->create('Contact');
3231
		$result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
3232
		$expected = array(
3233
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3234
			array('div' => array('class' => 'input text')),
3235
			'*/div',
3236
			array('div' => array('class' => 'input email')),
3237
			'*/div',
3238
			array('div' => array('class' => 'input tel')),
3239
			'*/div',
3240
			array('div' => array('class' => 'input password')),
3241
			'*/div',
3242
			array('div' => array('class' => 'input date')),
3243
			'*/div',
3244
			array('div' => array('class' => 'input date')),
3245
			'*/div',
3246
			array('div' => array('class' => 'input datetime')),
3247
			'*/div',
3248
			array('div' => array('class' => 'input number')),
3249
			'*/div',
3250
			array('div' => array('class' => 'input select')),
3251
			'*/div',
3252
		);
3253
		$this->assertTags($result, $expected);
3254
 
3255
		$this->Form->create('Contact');
3256
		$result = $this->Form->inputs(null, null, array('fieldset' => false));
3257
		$this->assertTags($result, $expected);
3258
 
3259
		$this->Form->create('Contact');
3260
		$result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
3261
		$expected = array(
3262
			'fieldset' => array(),
3263
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3264
			array('div' => array('class' => 'input text')),
3265
			'*/div',
3266
			array('div' => array('class' => 'input email')),
3267
			'*/div',
3268
			array('div' => array('class' => 'input tel')),
3269
			'*/div',
3270
			array('div' => array('class' => 'input password')),
3271
			'*/div',
3272
			array('div' => array('class' => 'input date')),
3273
			'*/div',
3274
			array('div' => array('class' => 'input date')),
3275
			'*/div',
3276
			array('div' => array('class' => 'input datetime')),
3277
			'*/div',
3278
			array('div' => array('class' => 'input number')),
3279
			'*/div',
3280
			array('div' => array('class' => 'input select')),
3281
			'*/div',
3282
			'/fieldset'
3283
		);
3284
		$this->assertTags($result, $expected);
3285
 
3286
		$this->Form->create('Contact');
3287
		$result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
3288
		$expected = array(
3289
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3290
			array('div' => array('class' => 'input text')),
3291
			'*/div',
3292
			array('div' => array('class' => 'input email')),
3293
			'*/div',
3294
			array('div' => array('class' => 'input tel')),
3295
			'*/div',
3296
			array('div' => array('class' => 'input password')),
3297
			'*/div',
3298
			array('div' => array('class' => 'input date')),
3299
			'*/div',
3300
			array('div' => array('class' => 'input date')),
3301
			'*/div',
3302
			array('div' => array('class' => 'input datetime')),
3303
			'*/div',
3304
			array('div' => array('class' => 'input number')),
3305
			'*/div',
3306
			array('div' => array('class' => 'input select')),
3307
			'*/div',
3308
		);
3309
		$this->assertTags($result, $expected);
3310
 
3311
		$this->Form->create('Contact');
3312
		$result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
3313
		$this->assertTags($result, $expected);
3314
 
3315
		$this->Form->create('Contact');
3316
		$result = $this->Form->inputs('Hello');
3317
		$expected = array(
3318
			'fieldset' => array(),
3319
			'legend' => array(),
3320
			'Hello',
3321
			'/legend',
3322
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3323
			array('div' => array('class' => 'input text')),
3324
			'*/div',
3325
			array('div' => array('class' => 'input email')),
3326
			'*/div',
3327
			array('div' => array('class' => 'input tel')),
3328
			'*/div',
3329
			array('div' => array('class' => 'input password')),
3330
			'*/div',
3331
			array('div' => array('class' => 'input date')),
3332
			'*/div',
3333
			array('div' => array('class' => 'input date')),
3334
			'*/div',
3335
			array('div' => array('class' => 'input datetime')),
3336
			'*/div',
3337
			array('div' => array('class' => 'input number')),
3338
			'*/div',
3339
			array('div' => array('class' => 'input select')),
3340
			'*/div',
3341
			'/fieldset'
3342
		);
3343
		$this->assertTags($result, $expected);
3344
 
3345
		$this->Form->create('Contact');
3346
		$result = $this->Form->inputs(array('legend' => 'Hello'));
3347
		$expected = array(
3348
			'fieldset' => array(),
3349
			'legend' => array(),
3350
			'Hello',
3351
			'/legend',
3352
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3353
			array('div' => array('class' => 'input text')),
3354
			'*/div',
3355
			array('div' => array('class' => 'input email')),
3356
			'*/div',
3357
			array('div' => array('class' => 'input tel')),
3358
			'*/div',
3359
			array('div' => array('class' => 'input password')),
3360
			'*/div',
3361
			array('div' => array('class' => 'input date')),
3362
			'*/div',
3363
			array('div' => array('class' => 'input date')),
3364
			'*/div',
3365
			array('div' => array('class' => 'input datetime')),
3366
			'*/div',
3367
			array('div' => array('class' => 'input number')),
3368
			'*/div',
3369
			array('div' => array('class' => 'input select')),
3370
			'*/div',
3371
			'/fieldset'
3372
		);
3373
		$this->assertTags($result, $expected);
3374
 
3375
		$this->Form->create('Contact');
3376
		$result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
3377
		$this->assertTags($result, $expected);
3378
		$this->Form->end();
3379
 
3380
		$this->Form->create(false);
3381
		$expected = array(
3382
			'fieldset' => array(),
3383
			array('div' => array('class' => 'input text')),
3384
			'label' => array('for' => 'foo'),
3385
			'Foo',
3386
			'/label',
3387
			'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
3388
			'*/div',
3389
			'/fieldset'
3390
		);
3391
		$result = $this->Form->inputs(
3392
			array('foo' => array('type' => 'text')),
3393
			array(),
3394
			array('legend' => false)
3395
		);
3396
		$this->assertTags($result, $expected);
3397
	}
3398
 
3399
/**
3400
 * Tests inputs() works with plugin models
3401
 *
3402
 * @return void
3403
 */
3404
	public function testInputsPluginModel() {
3405
		$this->loadFixtures('Post');
3406
		App::build(array(
3407
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
3408
		));
3409
		CakePlugin::load('TestPlugin');
3410
		$this->Form->request['models'] = array(
3411
			'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
3412
		);
3413
		$this->Form->create('TestPlugin.TestPluginPost');
3414
		$result = $this->Form->inputs();
3415
 
3416
		$this->assertContains('data[TestPluginPost][id]', $result);
3417
		$this->assertContains('data[TestPluginPost][author_id]', $result);
3418
		$this->assertContains('data[TestPluginPost][title]', $result);
3419
		$this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
3420
		$this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
3421
		$this->assertEquals('TestPluginPost', $this->Form->model());
3422
	}
3423
 
3424
/**
3425
 * testSelectAsCheckbox method
3426
 *
3427
 * test multi-select widget with checkbox formatting.
3428
 *
3429
 * @return void
3430
 */
3431
	public function testSelectAsCheckbox() {
3432
		$result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
3433
		$expected = array(
3434
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3435
			array('div' => array('class' => 'checkbox')),
3436
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
3437
			array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
3438
			'first',
3439
			'/label',
3440
			'/div',
3441
			array('div' => array('class' => 'checkbox')),
3442
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
3443
			array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
3444
			'second',
3445
			'/label',
3446
			'/div',
3447
			array('div' => array('class' => 'checkbox')),
3448
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
3449
			array('label' => array('for' => 'ModelMultiField2')),
3450
			'third',
3451
			'/label',
3452
			'/div',
3453
		);
3454
		$this->assertTags($result, $expected);
3455
 
3456
		$result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
3457
		$expected = array(
3458
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3459
			array('div' => array('class' => 'checkbox')),
3460
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
3461
			array('label' => array('for' => 'ModelMultiField12')),
3462
			'half',
3463
			'/label',
3464
			'/div',
3465
		);
3466
		$this->assertTags($result, $expected);
3467
	}
3468
 
3469
/**
3470
 * testLabel method
3471
 *
3472
 * test label generation.
3473
 *
3474
 * @return void
3475
 */
3476
	public function testLabel() {
3477
		$this->Form->text('Person.name');
3478
		$result = $this->Form->label();
3479
		$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
3480
 
3481
		$this->Form->text('Person.name');
3482
		$result = $this->Form->label();
3483
		$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
3484
 
3485
		$result = $this->Form->label('Person.first_name');
3486
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
3487
 
3488
		$result = $this->Form->label('Person.first_name', 'Your first name');
3489
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
3490
 
3491
		$result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
3492
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
3493
 
3494
		$result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
3495
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
3496
 
3497
		$result = $this->Form->label('Person.first_name', '');
3498
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
3499
 
3500
		$result = $this->Form->label('Person.2.name', '');
3501
		$this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
3502
	}
3503
 
3504
/**
3505
 * testTextbox method
3506
 *
3507
 * test textbox element generation
3508
 *
3509
 * @return void
3510
 */
3511
	public function testTextbox() {
3512
		$result = $this->Form->text('Model.field');
3513
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
3514
 
3515
		$result = $this->Form->text('Model.field', array('type' => 'password'));
3516
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
3517
 
3518
		$result = $this->Form->text('Model.field', array('id' => 'theID'));
3519
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
3520
 
3521
		$this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
3522
		$result = $this->Form->text('Model.text');
3523
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
3524
 
3525
		$Contact = ClassRegistry::getObject('Contact');
3526
		$Contact->validationErrors['text'] = array(true);
3527
		$this->Form->request->data['Contact']['text'] = 'test';
3528
		$result = $this->Form->text('Contact.text', array('id' => 'theID'));
3529
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
3530
 
3531
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
3532
		$result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
3533
		$expected = array(
3534
			'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
3535
		);
3536
		$this->assertTags($result, $expected);
3537
	}
3538
 
3539
/**
3540
 * testDefaultValue method
3541
 *
3542
 * Test default value setting
3543
 *
3544
 * @return void
3545
 */
3546
	public function testDefaultValue() {
3547
		$this->Form->request->data['Model']['field'] = 'test';
3548
		$result = $this->Form->text('Model.field', array('default' => 'default value'));
3549
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
3550
 
3551
		unset($this->Form->request->data['Model']['field']);
3552
		$result = $this->Form->text('Model.field', array('default' => 'default value'));
3553
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
3554
	}
3555
 
3556
/**
3557
 * testCheckboxDefaultValue method
3558
 *
3559
 * Test default value setting on checkbox() method
3560
 *
3561
 * @return void
3562
 */
3563
	public function testCheckboxDefaultValue() {
3564
		$this->Form->request->data['Model']['field'] = false;
3565
		$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
3566
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
3567
 
3568
		unset($this->Form->request->data['Model']['field']);
3569
		$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
3570
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
3571
 
3572
		$this->Form->request->data['Model']['field'] = true;
3573
		$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
3574
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
3575
 
3576
		unset($this->Form->request->data['Model']['field']);
3577
		$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
3578
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
3579
	}
3580
 
3581
/**
3582
 * testError method
3583
 *
3584
 * Test field error generation
3585
 *
3586
 * @return void
3587
 */
3588
	public function testError() {
3589
		$Contact = ClassRegistry::getObject('Contact');
3590
		$Contact->validationErrors['field'] = array(1);
3591
		$result = $this->Form->error('Contact.field');
3592
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
3593
 
3594
		$result = $this->Form->error('Contact.field', null, array('wrap' => false));
3595
		$this->assertEquals('Error in field Field', $result);
3596
 
3597
		$Contact->validationErrors['field'] = array("This field contains invalid input");
3598
		$result = $this->Form->error('Contact.field', null, array('wrap' => false));
3599
		$this->assertEquals('This field contains invalid input', $result);
3600
 
3601
		$Contact->validationErrors['field'] = array("This field contains invalid input");
3602
		$result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
3603
		$this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
3604
 
3605
		$result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
3606
		$this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
3607
 
3608
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
3609
		$this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
3610
 
3611
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
3612
		$this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
3613
 
3614
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
3615
		$this->assertEquals('<strong>Badness!</strong>', $result);
3616
 
3617
		$Contact->validationErrors['field'] = array("email");
3618
		$result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
3619
		$expected = array(
3620
			'div' => array('class' => 'field-error'),
3621
			'No good!',
3622
			'/div'
3623
		);
3624
		$this->assertTags($result, $expected);
3625
 
3626
		$Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
3627
		$result = $this->Form->error('Contact.field', array(
3628
			'notEmpty' => 'Cannot be empty',
3629
			'email' => 'No good!'
3630
		));
3631
		$expected = array(
3632
			'div' => array('class' => 'error-message'),
3633
				'ul' => array(),
3634
					'<li', 'Cannot be empty', '/li',
3635
					'<li', 'No good!', '/li',
3636
					'<li', 'Something else', '/li',
3637
				'/ul',
3638
			'/div'
3639
		);
3640
		$this->assertTags($result, $expected);
3641
 
3642
		// Testing error messages list options
3643
		$Contact->validationErrors['field'] = array('notEmpty', 'email');
3644
 
3645
		$result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
3646
		$expected = array(
3647
			'div' => array('class' => 'error-message'),
3648
				'ol' => array(),
3649
					'<li', 'notEmpty', '/li',
3650
					'<li', 'email', '/li',
3651
				'/ol',
3652
			'/div'
3653
		);
3654
		$this->assertTags($result, $expected);
3655
 
3656
		$result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
3657
		$expected = array(
3658
			'div' => array('class' => 'error-message'),
3659
				'ol' => array(),
3660
					'<li', 'notEmpty', '/li',
3661
					'<li', 'email', '/li',
3662
				'/ol',
3663
			'/div'
3664
		);
3665
		$this->assertTags($result, $expected);
3666
 
3667
		$result = $this->Form->error('Contact.field', null, array(
3668
			'listOptions' => array(
3669
				'class' => 'ul-class',
3670
				'itemOptions' => array(
3671
					'class' => 'li-class'
3672
				)
3673
			)
3674
		));
3675
		$expected = array(
3676
			'div' => array('class' => 'error-message'),
3677
				'ul' => array('class' => 'ul-class'),
3678
					array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
3679
					array('li' => array('class' => 'li-class')), 'email', '/li',
3680
				'/ul',
3681
			'/div'
3682
		);
3683
		$this->assertTags($result, $expected);
3684
	}
3685
 
3686
/**
3687
 * test error options when using form->input();
3688
 *
3689
 * @return void
3690
 */
3691
	public function testInputErrorEscape() {
3692
		$this->Form->create('ValidateProfile');
3693
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
3694
		$ValidateProfile->validationErrors['city'] = array('required<br>');
3695
		$result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
3696
		$this->assertRegExp('/required&lt;br&gt;/', $result);
3697
 
3698
		$result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
3699
		$this->assertRegExp('/required<br>/', $result);
3700
	}
3701
 
3702
/**
3703
 * testPassword method
3704
 *
3705
 * Test password element generation
3706
 *
3707
 * @return void
3708
 */
3709
	public function testPassword() {
3710
		$Contact = ClassRegistry::getObject('Contact');
3711
		$result = $this->Form->password('Contact.field');
3712
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
3713
 
3714
		$Contact->validationErrors['passwd'] = 1;
3715
		$this->Form->request->data['Contact']['passwd'] = 'test';
3716
		$result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
3717
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
3718
	}
3719
 
3720
/**
3721
 * testRadio method
3722
 *
3723
 * Test radio element set generation
3724
 *
3725
 * @return void
3726
 */
3727
	public function testRadio() {
3728
		$result = $this->Form->radio('Model.field', array('option A'));
3729
		$expected = array(
3730
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3731
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3732
			'label' => array('for' => 'ModelField0'),
3733
			'option A',
3734
			'/label'
3735
		);
3736
		$this->assertTags($result, $expected);
3737
 
3738
		$result = $this->Form->radio('Model.field', array('1/2' => 'half'));
3739
		$expected = array(
3740
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3741
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
3742
			'label' => array('for' => 'ModelField12'),
3743
			'half',
3744
			'/label'
3745
		);
3746
		$this->assertTags($result, $expected);
3747
 
3748
		$result = $this->Form->radio('Model.field', array('option A', 'option B'));
3749
		$expected = array(
3750
			'fieldset' => array(),
3751
			'legend' => array(),
3752
			'Field',
3753
			'/legend',
3754
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3755
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3756
			array('label' => array('for' => 'ModelField0')),
3757
			'option A',
3758
			'/label',
3759
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
3760
			array('label' => array('for' => 'ModelField1')),
3761
			'option B',
3762
			'/label',
3763
			'/fieldset'
3764
		);
3765
		$this->assertTags($result, $expected);
3766
 
3767
		$result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
3768
		$expected = array(
3769
			'fieldset' => array(),
3770
			'legend' => array(),
3771
			'Field',
3772
			'/legend',
3773
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3774
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3775
			array('label' => array('for' => 'ModelField0')),
3776
			'option A',
3777
			'/label',
3778
			'br' => array(),
3779
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
3780
			array('label' => array('for' => 'ModelField1')),
3781
			'option B',
3782
			'/label',
3783
			'/fieldset'
3784
		);
3785
		$this->assertTags($result, $expected);
3786
 
3787
		$result = $this->Form->radio(
3788
			'Employee.gender',
3789
			array('male' => 'Male', 'female' => 'Female'),
3790
			array('form' => 'my-form')
3791
		);
3792
		$expected = array(
3793
			'fieldset' => array(),
3794
			'legend' => array(),
3795
			'Gender',
3796
			'/legend',
3797
			'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_', 'form' => 'my-form'),
3798
			array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')),
3799
			array('label' => array('for' => 'EmployeeGenderMale')),
3800
			'Male',
3801
			'/label',
3802
			array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale', 'form' => 'my-form')),
3803
			array('label' => array('for' => 'EmployeeGenderFemale')),
3804
			'Female',
3805
			'/label',
3806
			'/fieldset',
3807
		);
3808
		$this->assertTags($result, $expected);
3809
 
3810
		$result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
3811
		$expected = array(
3812
			'fieldset' => array(),
3813
			'legend' => array(),
3814
			'Gender',
3815
			'/legend',
3816
			'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
3817
			array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
3818
			array('label' => array('for' => 'OfficerGenderMale')),
3819
			'Male',
3820
			'/label',
3821
			array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
3822
			array('label' => array('for' => 'OfficerGenderFemale')),
3823
			'Female',
3824
			'/label',
3825
			'/fieldset',
3826
		);
3827
		$this->assertTags($result, $expected);
3828
 
3829
		$result = $this->Form->radio('Contact.1.imrequired', array('option A'));
3830
		$expected = array(
3831
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
3832
			array('input' => array(
3833
				'type' => 'radio',
3834
				'name' => 'data[Contact][1][imrequired]',
3835
				'value' => '0',
3836
				'id' => 'Contact1Imrequired0',
3837
				'required' => 'required'
3838
			)),
3839
			'label' => array('for' => 'Contact1Imrequired0'),
3840
			'option A',
3841
			'/label'
3842
		);
3843
		$this->assertTags($result, $expected);
3844
 
3845
		$result = $this->Form->radio('Model.1.field', array('option A'));
3846
		$expected = array(
3847
			'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
3848
			array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
3849
			'label' => array('for' => 'Model1Field0'),
3850
			'option A',
3851
			'/label'
3852
		);
3853
		$this->assertTags($result, $expected);
3854
 
3855
		$result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
3856
		$expected = array(
3857
			'fieldset' => array(),
3858
			'legend' => array(),
3859
			'Field',
3860
			'/legend',
3861
			'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
3862
			array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
3863
			array('label' => array('for' => 'ModelField0')),
3864
			'option A',
3865
			'/label',
3866
			array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
3867
			array('label' => array('for' => 'ModelField1')),
3868
			'option B',
3869
			'/label',
3870
			'/fieldset'
3871
		);
3872
		$this->assertTags($result, $expected);
3873
 
3874
		$result = $this->Form->radio(
3875
			'Model.field',
3876
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
3877
		);
3878
		$expected = array(
3879
			'fieldset' => array(),
3880
			'legend' => array(),
3881
			'Field',
3882
			'/legend',
3883
			'input' => array(
3884
				'type' => 'hidden', 'name' => 'data[Model][field]',
3885
				'id' => 'ModelField_', 'value' => '',
3886
			),
3887
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
3888
				'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
3889
			array('label' => array('for' => 'ModelFieldAB')),
3890
			'first',
3891
			'/label',
3892
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
3893
				'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
3894
			array('label' => array('for' => 'ModelFieldAB1')),
3895
			'second',
3896
			'/label',
3897
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
3898
				'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
3899
			array('label' => array('for' => 'ModelFieldAB2')),
3900
			'third',
3901
			'/label',
3902
			'/fieldset'
3903
		);
3904
		$this->assertTags($result, $expected);
3905
	}
3906
 
3907
/**
3908
 * testRadioDifferentModel
3909
 * Refs #2911
3910
 *
3911
 * @return void
3912
 */
3913
	public function testRadioDifferentModel() {
3914
		$this->Form->create('User');
3915
 
3916
		$result = $this->Form->radio(
3917
			'Model.field',
3918
			array('v1' => 'option A', 'v2' => 'option B'),
3919
			array('label' => true, 'legend' => false, 'value' => false)
3920
		);
3921
		$expected = array(
3922
			array('input' => array(
3923
				'type' => 'radio', 'name' => 'data[Model][field]',
3924
				'value' => 'v1', 'id' => 'ModelFieldV1'
3925
			)),
3926
			array('label' => array('for' => 'ModelFieldV1')),
3927
			'option A',
3928
			'/label',
3929
			array('input' => array(
3930
				'type' => 'radio', 'name' => 'data[Model][field]',
3931
				'value' => 'v2', 'id' => 'ModelFieldV2'
3932
			)),
3933
			array('label' => array('for' => 'ModelFieldV2')),
3934
			'option B',
3935
			'/label'
3936
		);
3937
		$this->assertTags($result, $expected);
3938
	}
3939
 
3940
/**
3941
 * Test radio inputs with between as string or array. Also ensure
3942
 * that an array with less between elements works.
3943
 *
3944
 * @return void
3945
 */
3946
	public function testRadioBetween() {
3947
		$result = $this->Form->radio(
3948
			'Model.field',
3949
			array('option A', 'option B'),
3950
			array('between' => 'I am between')
3951
		);
3952
		$expected = array(
3953
			'fieldset' => array(),
3954
			'legend' => array(),
3955
			'Field',
3956
			'/legend',
3957
			'I am between',
3958
			'input' => array(
3959
				'type' => 'hidden', 'name' => 'data[Model][field]',
3960
				'value' => '', 'id' => 'ModelField_'
3961
			),
3962
			array('input' => array(
3963
				'type' => 'radio', 'name' => 'data[Model][field]',
3964
				'value' => '0', 'id' => 'ModelField0'
3965
			)),
3966
			array('label' => array('for' => 'ModelField0')),
3967
			'option A',
3968
			'/label',
3969
			array('input' => array(
3970
				'type' => 'radio', 'name' => 'data[Model][field]',
3971
				'value' => '1', 'id' => 'ModelField1'
3972
			)),
3973
			array('label' => array('for' => 'ModelField1')),
3974
			'option B',
3975
			'/label',
3976
			'/fieldset'
3977
		);
3978
		$this->assertTags($result, $expected);
3979
 
3980
		$result = $this->Form->radio(
3981
			'Model.field',
3982
			array('option A', 'option B', 'option C'),
3983
			array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
3984
		);
3985
 
3986
		$expected = array(
3987
			'fieldset' => array(),
3988
			'legend' => array(),
3989
			'Field',
3990
			'/legend',
3991
			'input' => array(
3992
				'type' => 'hidden', 'name' => 'data[Model][field]',
3993
				'value' => '', 'id' => 'ModelField_'
3994
			),
3995
			array('input' => array(
3996
				'type' => 'radio', 'name' => 'data[Model][field]',
3997
				'value' => '0', 'id' => 'ModelField0'
3998
			)),
3999
			array('label' => array('for' => 'ModelField0')),
4000
			'option A',
4001
			'/label',
4002
			'between A',
4003
			'--separator--',
4004
			array('input' => array(
4005
				'type' => 'radio', 'name' => 'data[Model][field]',
4006
				'value' => '1', 'id' => 'ModelField1'
4007
			)),
4008
			array('label' => array('for' => 'ModelField1')),
4009
			'option B',
4010
			'/label',
4011
			'between B',
4012
			'--separator--',
4013
			array('input' => array(
4014
				'type' => 'radio', 'name' => 'data[Model][field]',
4015
				'value' => '2', 'id' => 'ModelField2'
4016
			)),
4017
			array('label' => array('for' => 'ModelField2')),
4018
			'option C',
4019
			'/label',
4020
			'between C',
4021
			'/fieldset'
4022
		);
4023
		$this->assertTags($result, $expected);
4024
 
4025
		$result = $this->Form->input('Model.field', array(
4026
			'options' => array('1' => 'first', '2' => 'second'),
4027
			'type' => 'radio',
4028
			'before' => '--before--',
4029
			'after' => '--after--',
4030
			'separator' => '--separator--',
4031
			'between' => array('--between first--', '--between second--')
4032
		));
4033
 
4034
		$expected = array(
4035
			'div' => array('class' => 'input radio'),
4036
			'--before--',
4037
			'fieldset' => array(),
4038
			'legend' => array(),
4039
			'Field',
4040
			'/legend',
4041
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4042
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4043
			array('label' => array('for' => 'ModelField1')),
4044
			'first',
4045
			'/label',
4046
			'--between first--',
4047
			'--separator--',
4048
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
4049
			array('label' => array('for' => 'ModelField2')),
4050
			'second',
4051
			'/label',
4052
			'--between second--',
4053
			'/fieldset',
4054
			'--after--',
4055
			'/div'
4056
		);
4057
		$this->assertTags($result, $expected);
4058
 
4059
		$result = $this->Form->input('Model.field', array(
4060
			'options' => array('1' => 'first', '2' => 'second'),
4061
			'type' => 'radio',
4062
			'before' => '--before--',
4063
			'after' => '--after--',
4064
			'separator' => '--separator--',
4065
			'between' => array('--between first--')
4066
		));
4067
 
4068
		$expected = array(
4069
			'div' => array('class' => 'input radio'),
4070
			'--before--',
4071
			'fieldset' => array(),
4072
			'legend' => array(),
4073
			'Field',
4074
			'/legend',
4075
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4076
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4077
			array('label' => array('for' => 'ModelField1')),
4078
			'first',
4079
			'/label',
4080
			'--between first--',
4081
			'--separator--',
4082
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
4083
			array('label' => array('for' => 'ModelField2')),
4084
			'second',
4085
			'/label',
4086
			'/fieldset',
4087
			'--after--',
4088
			'/div'
4089
		);
4090
		$this->assertTags($result, $expected);
4091
	}
4092
 
4093
/**
4094
 * Test that radios with a 0 value are selected under the correct conditions.
4095
 * Also ensure that values that are booleanish are handled correctly.
4096
 *
4097
 * @return void
4098
 */
4099
	public function testRadioOptionWithBooleanishValues() {
4100
		$expected = array(
4101
			'fieldset' => array(),
4102
			'legend' => array(),
4103
			'Field',
4104
			'/legend',
4105
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4106
			array('label' => array('for' => 'ModelField1')),
4107
			'Yes',
4108
			'/label',
4109
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
4110
			array('label' => array('for' => 'ModelField0')),
4111
			'No',
4112
			'/label',
4113
			'/fieldset'
4114
		);
4115
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
4116
		$this->assertTags($result, $expected);
4117
 
4118
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
4119
		$this->assertTags($result, $expected);
4120
 
4121
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
4122
		$this->assertTags($result, $expected);
4123
 
4124
		$expected = array(
4125
			'fieldset' => array(),
4126
			'legend' => array(),
4127
			'Field',
4128
			'/legend',
4129
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
4130
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4131
			array('label' => array('for' => 'ModelField1')),
4132
			'Yes',
4133
			'/label',
4134
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
4135
			array('label' => array('for' => 'ModelField0')),
4136
			'No',
4137
			'/label',
4138
			'/fieldset'
4139
		);
4140
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
4141
		$this->assertTags($result, $expected);
4142
 
4143
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
4144
		$this->assertTags($result, $expected);
4145
 
4146
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
4147
		$this->assertTags($result, $expected);
4148
 
4149
		$expected = array(
4150
			'fieldset' => array(),
4151
			'legend' => array(),
4152
			'Field',
4153
			'/legend',
4154
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
4155
			array('label' => array('for' => 'ModelField1')),
4156
			'Yes',
4157
			'/label',
4158
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
4159
			array('label' => array('for' => 'ModelField0')),
4160
			'No',
4161
			'/label',
4162
			'/fieldset'
4163
		);
4164
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
4165
		$this->assertTags($result, $expected);
4166
 
4167
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
4168
		$this->assertTags($result, $expected);
4169
 
4170
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
4171
		$this->assertTags($result, $expected);
4172
	}
4173
 
4174
/**
4175
 * test disabled radio options
4176
 *
4177
 * @return void
4178
 */
4179
	public function testRadioDisabled() {
4180
		$result = $this->Form->radio(
4181
			'Model.field',
4182
			array('option A', 'option B'),
4183
			array('disabled' => array(0), 'value' => '0')
4184
		);
4185
		$expected = array(
4186
			'fieldset' => array(),
4187
			'legend' => array(),
4188
			'Field',
4189
			'/legend',
4190
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4191
			array('label' => array('for' => 'ModelField0')),
4192
			'option A',
4193
			'/label',
4194
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4195
			array('label' => array('for' => 'ModelField1')),
4196
			'option B',
4197
			'/label',
4198
			'/fieldset'
4199
		);
4200
		$this->assertTags($result, $expected);
4201
 
4202
		$result = $this->Form->radio(
4203
			'Model.field',
4204
			array('option A', 'option B'),
4205
			array('disabled' => true, 'value' => '0')
4206
		);
4207
		$expected = array(
4208
			'fieldset' => array(),
4209
			'legend' => array(),
4210
			'Field',
4211
			'/legend',
4212
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4213
			array('label' => array('for' => 'ModelField0')),
4214
			'option A',
4215
			'/label',
4216
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
4217
			array('label' => array('for' => 'ModelField1')),
4218
			'option B',
4219
			'/label',
4220
			'/fieldset'
4221
		);
4222
		$this->assertTags($result, $expected);
4223
 
4224
		$result = $this->Form->radio(
4225
			'Model.field',
4226
			array('option A', 'option B'),
4227
			array('disabled' => 'disabled', 'value' => '0')
4228
		);
4229
		$expected = array(
4230
			'fieldset' => array(),
4231
			'legend' => array(),
4232
			'Field',
4233
			'/legend',
4234
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4235
			array('label' => array('for' => 'ModelField0')),
4236
			'option A',
4237
			'/label',
4238
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
4239
			array('label' => array('for' => 'ModelField1')),
4240
			'option B',
4241
			'/label',
4242
			'/fieldset'
4243
		);
4244
		$this->assertTags($result, $expected);
4245
 
4246
		$result = $this->Form->input('Model.field', array(
4247
			'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
4248
			'type' => 'radio',
4249
			'disabled' => array(2, '3x'),
4250
		));
4251
 
4252
		$expected = array(
4253
			'div' => array('class' => 'input radio'),
4254
			'fieldset' => array(),
4255
			'legend' => array(),
4256
			'Field',
4257
			'/legend',
4258
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4259
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4260
			array('label' => array('for' => 'ModelField1')),
4261
			'first',
4262
			'/label',
4263
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
4264
			array('label' => array('for' => 'ModelField2')),
4265
			'second',
4266
			'/label',
4267
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
4268
			array('label' => array('for' => 'ModelField2x')),
4269
			'2x',
4270
			'/label',
4271
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
4272
			array('label' => array('for' => 'ModelField3')),
4273
			'third',
4274
			'/label',
4275
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
4276
			array('label' => array('for' => 'ModelField3x')),
4277
			'3x',
4278
			'/label',
4279
			'/fieldset',
4280
			'/div'
4281
		);
4282
		$this->assertTags($result, $expected);
4283
 
4284
		$result = $this->Form->input('Model.field', array(
4285
			'type' => 'radio',
4286
			'options' => array(
4287
				1 => 'A',
4288
				2 => 'B',
4289
				3 => 'C'
4290
			),
4291
			'disabled' => array(1)
4292
		));
4293
 
4294
		$expected = array(
4295
			'div' => array('class' => 'input radio'),
4296
			'fieldset' => array(),
4297
			'legend' => array(),
4298
			'Field',
4299
			'/legend',
4300
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4301
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
4302
			array('label' => array('for' => 'ModelField1')),
4303
			'A',
4304
			'/label',
4305
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
4306
			array('label' => array('for' => 'ModelField2')),
4307
			'B',
4308
			'/label',
4309
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
4310
			array('label' => array('for' => 'ModelField3')),
4311
			'C',
4312
			'/label',
4313
			'/fieldset',
4314
			'/div'
4315
		);
4316
		$this->assertTags($result, $expected);
4317
	}
4318
 
4319
/**
4320
 * test disabling the hidden input for radio buttons
4321
 *
4322
 * @return void
4323
 */
4324
	public function testRadioHiddenInputDisabling() {
4325
		$result = $this->Form->input('Model.1.field', array(
4326
				'type' => 'radio',
4327
				'options' => array('option A'),
4328
				'hiddenField' => false
4329
			)
4330
		);
4331
		$expected = array(
4332
			'div' => array('class' => 'input radio'),
4333
			'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
4334
			'label' => array('for' => 'Model1Field0'),
4335
			'option A',
4336
			'/label',
4337
			'/div'
4338
		);
4339
		$this->assertTags($result, $expected);
4340
 
4341
		$result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
4342
		$expected = array(
4343
			'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
4344
			'label' => array('for' => 'Model1Field0'),
4345
			'option A',
4346
			'/label'
4347
		);
4348
		$this->assertTags($result, $expected);
4349
	}
4350
 
4351
/**
4352
 * test adding an empty option for radio buttons
4353
 *
4354
 * @return void
4355
 */
4356
	public function testRadioAddEmptyOption() {
4357
		$result = $this->Form->input('Model.1.field', array(
4358
			'type' => 'radio',
4359
			'options' => array('option A'),
4360
			'empty' => true,
4361
			'hiddenField' => false
4362
		));
4363
		$expected = array(
4364
			'div' => array('class' => 'input radio'),
4365
				'fieldset' => array(),
4366
					'legend' => array(),
4367
						'Field',
4368
					'/legend',
4369
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
4370
					array('label' => array('for' => 'Model1Field')),
4371
						__('empty'),
4372
					'/label',
4373
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
4374
					array('label' => array('for' => 'Model1Field0')),
4375
						'option A',
4376
					'/label',
4377
				'/fieldset',
4378
			'/div'
4379
		);
4380
		$this->assertTags($result, $expected);
4381
 
4382
		$result = $this->Form->input('Model.1.field', array(
4383
			'type' => 'radio',
4384
			'options' => array('option A'),
4385
			'empty' => 'CustomEmptyLabel',
4386
			'hiddenField' => false
4387
		));
4388
		$expected = array(
4389
			'div' => array('class' => 'input radio'),
4390
				'fieldset' => array(),
4391
					'legend' => array(),
4392
						'Field',
4393
					'/legend',
4394
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
4395
					array('label' => array('for' => 'Model1Field')),
4396
						'CustomEmptyLabel',
4397
					'/label',
4398
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
4399
					array('label' => array('for' => 'Model1Field0')),
4400
						'option A',
4401
					'/label',
4402
				'/fieldset',
4403
			'/div'
4404
		);
4405
		$this->assertTags($result, $expected);
4406
 
4407
		$result = $this->Form->input('Model.1.field', array(
4408
			'type' => 'radio',
4409
			'options' => array('option A'),
4410
			'empty' => false,
4411
			'hiddenField' => false
4412
		));
4413
		$this->assertTextNotContains('"Model1Field"', $result);
4414
	}
4415
 
4416
/**
4417
 * Test that radio() accepts an array for label
4418
 *
4419
 * @return void
4420
 */
4421
	public function testRadioLabelArray() {
4422
		$result = $this->Form->input('Model.field', array(
4423
			'type' => 'radio',
4424
			'legend' => false,
4425
			'label' => array(
4426
				'class' => 'checkbox float-left',
4427
			),
4428
			'options' => array('1' => 'Option A', '2' => 'Option B.')
4429
		));
4430
		$this->assertTextContains(
4431
			'<label for="ModelField1" class="checkbox float-left">Option A</label>',
4432
			$result
4433
		);
4434
	}
4435
 
4436
/**
4437
 * Test that label id's match the input element id's when radio is called after create().
4438
 *
4439
 * @return void
4440
 */
4441
	public function testRadioWithCreate() {
4442
		$this->Form->create('Model');
4443
		$result = $this->Form->radio('recipient',
4444
			array('1' => '1', '2' => '2', '3' => '3'),
4445
			array('legend' => false, 'value' => '1')
4446
		);
4447
		$this->assertTextNotContains(
4448
			'<label for="ModelModelRecipient1">1</label>',
4449
			$result
4450
		);
4451
		$this->assertTextContains(
4452
			'<label for="ModelRecipient1">1</label>',
4453
			$result
4454
		);
4455
	}
4456
 
4457
/**
4458
 * testDomIdSuffix method
4459
 *
4460
 * @return void
4461
 */
4462
	public function testDomIdSuffix() {
4463
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
4464
		$this->assertEquals('1StringWith1DollarSigns', $result);
4465
 
4466
		$result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
4467
		$this->assertEquals('AbcXFooYBar', $result);
4468
 
4469
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
4470
		$this->assertEquals('1StringWith1$-dollarSigns', $result);
4471
 
4472
		$result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
4473
		$this->assertEquals('AbcX=FooY=Bar', $result);
4474
	}
4475
 
4476
/**
4477
 * testDomIdSuffixCollisionResolvement()
4478
 *
4479
 * @return void
4480
 */
4481
	public function testDomIdSuffixCollisionResolvement() {
4482
		$result = $this->Form->domIdSuffix('a>b');
4483
		$this->assertEquals('AB', $result);
4484
 
4485
		$result = $this->Form->domIdSuffix('a<b');
4486
		$this->assertEquals('AB1', $result);
4487
 
4488
		$result = $this->Form->domIdSuffix('a\'b');
4489
		$this->assertEquals('AB2', $result);
4490
 
4491
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4492
		$this->assertEquals('1StringWith1Dollar', $result);
4493
 
4494
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4495
		$this->assertEquals('1StringWith1Dollar1', $result);
4496
 
4497
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4498
		$this->assertEquals('1StringWith1Dollar2', $result);
4499
	}
4500
 
4501
/**
4502
 * testSelect method
4503
 *
4504
 * Test select element generation.
4505
 *
4506
 * @return void
4507
 */
4508
	public function testSelect() {
4509
		$result = $this->Form->select('Model.field', array());
4510
		$expected = array(
4511
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4512
			array('option' => array('value' => '')),
4513
			'/option',
4514
			'/select'
4515
		);
4516
		$this->assertTags($result, $expected);
4517
 
4518
		$this->Form->request->data = array('Model' => array('field' => 'value'));
4519
		$result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
4520
		$expected = array(
4521
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4522
			array('option' => array('value' => '')),
4523
			'/option',
4524
			array('option' => array('value' => 'value', 'selected' => 'selected')),
4525
			'good',
4526
			'/option',
4527
			array('option' => array('value' => 'other')),
4528
			'bad',
4529
			'/option',
4530
			'/select'
4531
		);
4532
		$this->assertTags($result, $expected);
4533
 
4534
		$this->Form->request->data = array();
4535
		$result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
4536
		$expected = array(
4537
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4538
			array('option' => array('value' => '')),
4539
			'/option',
4540
			array('option' => array('value' => 'value')),
4541
			'good',
4542
			'/option',
4543
			array('option' => array('value' => 'other')),
4544
			'bad',
4545
			'/option',
4546
			'/select'
4547
		);
4548
		$this->assertTags($result, $expected);
4549
 
4550
		$result = $this->Form->select(
4551
			'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
4552
			array('empty' => false)
4553
		);
4554
		$expected = array(
4555
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4556
			array('option' => array('value' => 'first')),
4557
			'first &quot;html&quot; &lt;chars&gt;',
4558
			'/option',
4559
			array('option' => array('value' => 'second')),
4560
			'value',
4561
			'/option',
4562
			'/select'
4563
		);
4564
		$this->assertTags($result, $expected);
4565
 
4566
		$result = $this->Form->select(
4567
			'Model.field',
4568
			array('first' => 'first "html" <chars>', 'second' => 'value'),
4569
			array('escape' => false, 'empty' => false)
4570
		);
4571
		$expected = array(
4572
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4573
			array('option' => array('value' => 'first')),
4574
			'first "html" <chars>',
4575
			'/option',
4576
			array('option' => array('value' => 'second')),
4577
			'value',
4578
			'/option',
4579
			'/select'
4580
		);
4581
		$this->assertTags($result, $expected);
4582
 
4583
		$options = array(
4584
			array('value' => 'first', 'name' => 'First'),
4585
			array('value' => 'first', 'name' => 'Another First'),
4586
		);
4587
		$result = $this->Form->select(
4588
			'Model.field',
4589
			$options,
4590
			array('escape' => false, 'empty' => false)
4591
		);
4592
		$expected = array(
4593
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4594
			array('option' => array('value' => 'first')),
4595
			'First',
4596
			'/option',
4597
			array('option' => array('value' => 'first')),
4598
			'Another First',
4599
			'/option',
4600
			'/select'
4601
		);
4602
		$this->assertTags($result, $expected);
4603
 
4604
		$this->Form->request->data = array('Model' => array('contact_id' => 228));
4605
		$result = $this->Form->select(
4606
			'Model.contact_id',
4607
			array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
4608
			array('escape' => false, 'empty' => 'pick something')
4609
		);
4610
 
4611
		$expected = array(
4612
			'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
4613
			array('option' => array('value' => '')), 'pick something', '/option',
4614
			array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
4615
			array('option' => array('value' => '228-1')), '228-1 value', '/option',
4616
			array('option' => array('value' => '228-2')), '228-2 value', '/option',
4617
			'/select'
4618
		);
4619
		$this->assertTags($result, $expected);
4620
 
4621
		$this->Form->request->data['Model']['field'] = 0;
4622
		$result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
4623
		$expected = array(
4624
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4625
			array('option' => array('value' => '')), '/option',
4626
			array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
4627
			array('option' => array('value' => '1')), 'Yes', '/option',
4628
			'/select'
4629
		);
4630
		$this->assertTags($result, $expected);
4631
 
4632
		$this->Form->request->data['Model']['field'] = 50;
4633
		$result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
4634
		$expected = array(
4635
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4636
			array('option' => array('value' => '')), '/option',
4637
			array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
4638
			array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
4639
			'/select'
4640
		);
4641
		$this->assertTags($result, $expected);
4642
 
4643
		$result = $this->Form->select('Contact.required_one', array('option A'));
4644
		$expected = array(
4645
			'select' => array(
4646
				'name' => 'data[Contact][required_one]',
4647
				'id' => 'ContactRequiredOne',
4648
				'required' => 'required'
4649
			),
4650
			array('option' => array('value' => '')), '/option',
4651
			array('option' => array('value' => '0')), 'option A', '/option',
4652
			'/select'
4653
		);
4654
		$this->assertTags($result, $expected);
4655
 
4656
		$result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
4657
		$expected = array(
4658
			'select' => array(
4659
				'name' => 'data[Contact][required_one]',
4660
				'id' => 'ContactRequiredOne',
4661
				'disabled' => 'disabled'
4662
			),
4663
			array('option' => array('value' => '')), '/option',
4664
			array('option' => array('value' => '0')), 'option A', '/option',
4665
			'/select'
4666
		);
4667
		$this->assertTags($result, $expected);
4668
	}
4669
 
4670
/**
4671
 * test that select() with optiongroups listens to the escape param.
4672
 *
4673
 * @return void
4674
 */
4675
	public function testSelectOptionGroupEscaping() {
4676
		$options = array(
4677
			'>< Key' => array(
4678
				1 => 'One',
4679
				2 => 'Two'
4680
			)
4681
		);
4682
		$result = $this->Form->select('Model.field', $options, array('empty' => false));
4683
		$expected = array(
4684
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4685
			'optgroup' => array('label' => '&gt;&lt; Key'),
4686
			array('option' => array('value' => '1')), 'One', '/option',
4687
			array('option' => array('value' => '2')), 'Two', '/option',
4688
			'/optgroup',
4689
			'/select'
4690
		);
4691
		$this->assertTags($result, $expected);
4692
 
4693
		$options = array(
4694
			'>< Key' => array(
4695
				1 => 'One',
4696
				2 => 'Two'
4697
			)
4698
		);
4699
		$result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
4700
		$expected = array(
4701
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4702
			'optgroup' => array('label' => '>< Key'),
4703
			array('option' => array('value' => '1')), 'One', '/option',
4704
			array('option' => array('value' => '2')), 'Two', '/option',
4705
			'/optgroup',
4706
			'/select'
4707
		);
4708
		$this->assertTags($result, $expected);
4709
	}
4710
 
4711
/**
4712
 * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
4713
 *
4714
 * @return void
4715
 */
4716
	public function testSelectWithNullAttributes() {
4717
		$result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
4718
		$expected = array(
4719
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4720
			array('option' => array('value' => '0')),
4721
			'first',
4722
			'/option',
4723
			array('option' => array('value' => '1')),
4724
			'second',
4725
			'/option',
4726
			'/select'
4727
		);
4728
		$this->assertTags($result, $expected);
4729
	}
4730
 
4731
/**
4732
 * testNestedSelect method
4733
 *
4734
 * test select element generation with optgroups
4735
 *
4736
 * @return void
4737
 */
4738
	public function testNestedSelect() {
4739
		$result = $this->Form->select(
4740
			'Model.field',
4741
			array(1 => 'One', 2 => 'Two', 'Three' => array(
4742
				3 => 'Three', 4 => 'Four', 5 => 'Five'
4743
			)), array('empty' => false)
4744
		);
4745
		$expected = array(
4746
			'select' => array('name' => 'data[Model][field]',
4747
					'id' => 'ModelField'),
4748
					array('option' => array('value' => 1)),
4749
					'One',
4750
					'/option',
4751
					array('option' => array('value' => 2)),
4752
					'Two',
4753
					'/option',
4754
					array('optgroup' => array('label' => 'Three')),
4755
						array('option' => array('value' => 4)),
4756
						'Four',
4757
						'/option',
4758
						array('option' => array('value' => 5)),
4759
						'Five',
4760
						'/option',
4761
					'/optgroup',
4762
					'/select'
4763
					);
4764
		$this->assertTags($result, $expected);
4765
 
4766
		$result = $this->Form->select(
4767
			'Model.field',
4768
			array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
4769
			array('showParents' => true, 'empty' => false)
4770
		);
4771
 
4772
		$expected = array(
4773
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4774
				array('option' => array('value' => 1)),
4775
				'One',
4776
				'/option',
4777
				array('option' => array('value' => 2)),
4778
				'Two',
4779
				'/option',
4780
				array('optgroup' => array('label' => 'Three')),
4781
					array('option' => array('value' => 3)),
4782
					'Three',
4783
					'/option',
4784
					array('option' => array('value' => 4)),
4785
					'Four',
4786
					'/option',
4787
				'/optgroup',
4788
			'/select'
4789
		);
4790
		$this->assertTags($result, $expected);
4791
	}
4792
 
4793
/**
4794
 * testSelectMultiple method
4795
 *
4796
 * test generation of multiple select elements
4797
 *
4798
 * @return void
4799
 */
4800
	public function testSelectMultiple() {
4801
		$options = array('first', 'second', 'third');
4802
		$result = $this->Form->select(
4803
			'Model.multi_field', $options, array('multiple' => true)
4804
		);
4805
		$expected = array(
4806
			'input' => array(
4807
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
4808
			),
4809
			'select' => array(
4810
				'name' => 'data[Model][multi_field][]',
4811
				'id' => 'ModelMultiField', 'multiple' => 'multiple'
4812
			),
4813
			array('option' => array('value' => '0')),
4814
			'first',
4815
			'/option',
4816
			array('option' => array('value' => '1')),
4817
			'second',
4818
			'/option',
4819
			array('option' => array('value' => '2')),
4820
			'third',
4821
			'/option',
4822
			'/select'
4823
		);
4824
		$this->assertTags($result, $expected);
4825
 
4826
		$result = $this->Form->select(
4827
			'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple')
4828
		);
4829
		$expected = array(
4830
			'input' => array(
4831
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_',
4832
				'form' => 'my-form',
4833
			),
4834
			'select' => array(
4835
				'name' => 'data[Model][multi_field][]',
4836
				'id' => 'ModelMultiField',
4837
				'multiple' => 'multiple',
4838
				'form' => 'my-form',
4839
			),
4840
			array('option' => array('value' => '0')),
4841
			'first',
4842
			'/option',
4843
			array('option' => array('value' => '1')),
4844
			'second',
4845
			'/option',
4846
			array('option' => array('value' => '2')),
4847
			'third',
4848
			'/option',
4849
			'/select'
4850
		);
4851
		$this->assertTags($result, $expected);
4852
 
4853
		$result = $this->Form->select(
4854
			'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
4855
		);
4856
		$expected = array(
4857
			'input' => array(
4858
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
4859
			),
4860
			'select' => array(
4861
				'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
4862
				'multiple' => 'multiple'
4863
			),
4864
			array('option' => array('value' => '0', 'selected' => 'selected')),
4865
			'first',
4866
			'/option',
4867
			array('option' => array('value' => '1', 'selected' => 'selected')),
4868
			'second',
4869
			'/option',
4870
			array('option' => array('value' => '2')),
4871
			'third',
4872
			'/option',
4873
			'/select'
4874
		);
4875
		$this->assertTags($result, $expected);
4876
 
4877
		$result = $this->Form->select(
4878
			'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
4879
		);
4880
		$expected = array(
4881
			'select' => array(
4882
				'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
4883
			),
4884
			array('option' => array('value' => '0', 'selected' => 'selected')),
4885
			'first',
4886
			'/option',
4887
			array('option' => array('value' => '1', 'selected' => 'selected')),
4888
			'second',
4889
			'/option',
4890
			array('option' => array('value' => '2')),
4891
			'third',
4892
			'/option',
4893
			'/select'
4894
		);
4895
		$this->assertTags($result, $expected);
4896
 
4897
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
4898
		$selected = array('2', '3x');
4899
		$result = $this->Form->select(
4900
			'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
4901
		);
4902
		$expected = array(
4903
			'input' => array(
4904
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
4905
			),
4906
			'select' => array(
4907
				'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
4908
			),
4909
			array('option' => array('value' => '1')),
4910
			'One',
4911
			'/option',
4912
			array('option' => array('value' => '2', 'selected' => 'selected')),
4913
			'Two',
4914
			'/option',
4915
			array('option' => array('value' => '3')),
4916
			'Three',
4917
			'/option',
4918
			array('option' => array('value' => '3x', 'selected' => 'selected')),
4919
			'Stringy',
4920
			'/option',
4921
			'/select'
4922
		);
4923
		$this->assertTags($result, $expected);
4924
 
4925
		$result = $this->Form->select('Contact.required_one', array(
4926
			'1' => 'option A',
4927
			'2' => 'option B'
4928
		), array('multiple' => true));
4929
		$expected = array(
4930
			'input' => array(
4931
				'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
4932
			),
4933
			'select' => array(
4934
				'name' => 'data[Contact][required_one][]',
4935
				'id' => 'ContactRequiredOne',
4936
				'required' => 'required',
4937
				'multiple' => 'multiple'
4938
			),
4939
			array('option' => array('value' => '1')), 'option A', '/option',
4940
			array('option' => array('value' => '2')), 'option B', '/option',
4941
			'/select'
4942
		);
4943
		$this->assertTags($result, $expected);
4944
 
4945
		$result = $this->Form->select(
4946
			'Model.multi_field',
4947
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
4948
			array('multiple' => true)
4949
		);
4950
		$expected = array(
4951
			'input' => array(
4952
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
4953
				'id' => 'ModelMultiField_'
4954
			),
4955
			array('select' => array('name' => 'data[Model][multi_field][]',
4956
				'multiple' => 'multiple', 'id' => 'ModelMultiField'
4957
			)),
4958
			array('option' => array('value' => 'a&gt;b')),
4959
			'first',
4960
			'/option',
4961
			array('option' => array('value' => 'a&lt;b')),
4962
			'second',
4963
			'/option',
4964
			array('option' => array(
4965
				'value' => 'a&quot;b'
4966
			)),
4967
			'third',
4968
			'/option',
4969
			'/select'
4970
		);
4971
		$this->assertTags($result, $expected);
4972
	}
4973
 
4974
/**
4975
 * Test generating multiple select with disabled elements.
4976
 *
4977
 * @return void
4978
 */
4979
	public function testSelectMultipleWithDisabledElements() {
4980
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
4981
		$disabled = array(1);
4982
		$result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
4983
		$expected = array(
4984
			'input' => array(
4985
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
4986
			),
4987
			'select' => array(
4988
				'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
4989
			),
4990
			array('option' => array('value' => '1', 'disabled' => 'disabled')),
4991
			'One',
4992
			'/option',
4993
			array('option' => array('value' => '2')),
4994
			'Two',
4995
			'/option',
4996
			array('option' => array('value' => '3')),
4997
			'Three',
4998
			'/option',
4999
			array('option' => array('value' => '3x')),
5000
			'Stringy',
5001
			'/option',
5002
			'/select'
5003
		);
5004
		$this->assertTags($result, $expected);
5005
 
5006
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5007
		$disabled = array('2', '3x');
5008
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
5009
		$expected = array(
5010
			array('div' => array('class' => 'input select')),
5011
			array('label' => array('for' => 'ContactMultiple')),
5012
			'Multiple',
5013
			'/label',
5014
			'input' => array(
5015
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
5016
			),
5017
			'select' => array(
5018
				'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
5019
			),
5020
			array('option' => array('value' => '1')),
5021
			'One',
5022
			'/option',
5023
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
5024
			'Two',
5025
			'/option',
5026
			array('option' => array('value' => '3')),
5027
			'Three',
5028
			'/option',
5029
			array('option' => array('value' => '3x', 'disabled' => 'disabled')),
5030
			'Stringy',
5031
			'/option',
5032
			'/select',
5033
			'/div'
5034
		);
5035
		$this->assertTags($result, $expected);
5036
 
5037
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5038
		$disabled = true;
5039
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
5040
		$expected = array(
5041
			array('div' => array('class' => 'input select')),
5042
			array('label' => array('for' => 'ContactMultiple')),
5043
			'Multiple',
5044
			'/label',
5045
			'input' => array(
5046
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_', 'disabled' => 'disabled'
5047
			),
5048
			'select' => array(
5049
				'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
5050
			),
5051
			array('option' => array('value' => '1')),
5052
			'One',
5053
			'/option',
5054
			array('option' => array('value' => '2')),
5055
			'Two',
5056
			'/option',
5057
			array('option' => array('value' => '3')),
5058
			'Three',
5059
			'/option',
5060
			array('option' => array('value' => '3x')),
5061
			'Stringy',
5062
			'/option',
5063
			'/select',
5064
			'/div'
5065
		);
5066
		$this->assertTags($result, $expected);
5067
	}
5068
 
5069
/**
5070
 * Test generating select with disabled elements.
5071
 *
5072
 * @return void
5073
 */
5074
	public function testSelectWithDisabledElements() {
5075
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5076
		$disabled = array(2, 3);
5077
		$result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
5078
		$expected = array(
5079
			'select' => array(
5080
				'name' => 'data[Model][field]', 'id' => 'ModelField'
5081
			),
5082
			array('option' => array('value' => '')),
5083
			'/option',
5084
			array('option' => array('value' => '1')),
5085
			'One',
5086
			'/option',
5087
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
5088
			'Two',
5089
			'/option',
5090
			array('option' => array('value' => '3', 'disabled' => 'disabled')),
5091
			'Three',
5092
			'/option',
5093
			array('option' => array('value' => '3x')),
5094
			'Stringy',
5095
			'/option',
5096
			'/select'
5097
		);
5098
		$this->assertTags($result, $expected);
5099
 
5100
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5101
		$disabled = array('2', '3x');
5102
		$result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
5103
		$expected = array(
5104
			array('div' => array('class' => 'input select')),
5105
			array('label' => array('for' => 'ModelField')),
5106
			'Field',
5107
			'/label',
5108
			'select' => array(
5109
				'name' => 'data[Model][field]', 'id' => 'ModelField'
5110
			),
5111
			array('option' => array('value' => '1')),
5112
			'One',
5113
			'/option',
5114
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
5115
			'Two',
5116
			'/option',
5117
			array('option' => array('value' => '3')),
5118
			'Three',
5119
			'/option',
5120
			array('option' => array('value' => '3x', 'disabled' => 'disabled')),
5121
			'Stringy',
5122
			'/option',
5123
			'/select',
5124
			'/div'
5125
		);
5126
		$this->assertTags($result, $expected);
5127
 
5128
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5129
		$disabled = true;
5130
		$result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
5131
		$expected = array(
5132
			array('div' => array('class' => 'input select')),
5133
			array('label' => array('for' => 'ModelField')),
5134
			'Field',
5135
			'/label',
5136
			'select' => array(
5137
				'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
5138
			),
5139
			array('option' => array('value' => '1')),
5140
			'One',
5141
			'/option',
5142
			array('option' => array('value' => '2')),
5143
			'Two',
5144
			'/option',
5145
			array('option' => array('value' => '3')),
5146
			'Three',
5147
			'/option',
5148
			array('option' => array('value' => '3x')),
5149
			'Stringy',
5150
			'/option',
5151
			'/select',
5152
			'/div'
5153
		);
5154
		$this->assertTags($result, $expected);
5155
	}
5156
 
5157
/**
5158
 * test generation of habtm select boxes.
5159
 *
5160
 * @return void
5161
 */
5162
	public function testHabtmSelectBox() {
5163
		$this->View->viewVars['contactTags'] = array(
5164
			1 => 'blue',
5165
			2 => 'red',
5166
			3 => 'green'
5167
		);
5168
		$this->Form->request->data = array(
5169
			'Contact' => array(),
5170
			'ContactTag' => array(
5171
				array(
5172
					'id' => '1',
5173
					'name' => 'blue'
5174
				),
5175
				array(
5176
					'id' => 3,
5177
					'name' => 'green'
5178
				)
5179
			)
5180
		);
5181
		$this->Form->create('Contact');
5182
		$result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
5183
		$expected = array(
5184
			'input' => array(
5185
				'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
5186
			),
5187
			'select' => array(
5188
				'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
5189
				'multiple' => 'multiple'
5190
			),
5191
			array('option' => array('value' => '1', 'selected' => 'selected')),
5192
			'blue',
5193
			'/option',
5194
			array('option' => array('value' => '2')),
5195
			'red',
5196
			'/option',
5197
			array('option' => array('value' => '3', 'selected' => 'selected')),
5198
			'green',
5199
			'/option',
5200
			'/select'
5201
		);
5202
		$this->assertTags($result, $expected);
5203
 
5204
		// make sure only 50 is selected, and not 50f5c0cf
5205
		$this->View->viewVars['contactTags'] = array(
5206
			'1' => 'blue',
5207
			'50f5c0cf' => 'red',
5208
			'50' => 'green'
5209
		);
5210
		$this->Form->request->data = array(
5211
			'Contact' => array(),
5212
			'ContactTag' => array(
5213
				array(
5214
					'id' => 1,
5215
					'name' => 'blue'
5216
				),
5217
				array(
5218
					'id' => 50,
5219
					'name' => 'green'
5220
				)
5221
			)
5222
		);
5223
		$this->Form->create('Contact');
5224
		$result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
5225
		$expected = array(
5226
			'input' => array(
5227
				'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
5228
			),
5229
			'select' => array(
5230
				'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
5231
				'multiple' => 'multiple'
5232
			),
5233
			array('option' => array('value' => '1', 'selected' => 'selected')),
5234
			'blue',
5235
			'/option',
5236
			array('option' => array('value' => '50f5c0cf')),
5237
			'red',
5238
			'/option',
5239
			array('option' => array('value' => '50', 'selected' => 'selected')),
5240
			'green',
5241
			'/option',
5242
			'/select'
5243
		);
5244
		$this->assertTags($result, $expected);
5245
	}
5246
 
5247
/**
5248
 * test generation of multi select elements in checkbox format
5249
 *
5250
 * @return void
5251
 */
5252
	public function testSelectMultipleCheckboxes() {
5253
		$result = $this->Form->select(
5254
			'Model.multi_field',
5255
			array('first', 'second', 'third'),
5256
			array('multiple' => 'checkbox')
5257
		);
5258
 
5259
		$expected = array(
5260
			'input' => array(
5261
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5262
			),
5263
			array('div' => array('class' => 'checkbox')),
5264
			array('input' => array(
5265
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5266
				'value' => '0', 'id' => 'ModelMultiField0'
5267
			)),
5268
			array('label' => array('for' => 'ModelMultiField0')),
5269
			'first',
5270
			'/label',
5271
			'/div',
5272
			array('div' => array('class' => 'checkbox')),
5273
			array('input' => array(
5274
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5275
				'value' => '1', 'id' => 'ModelMultiField1'
5276
			)),
5277
			array('label' => array('for' => 'ModelMultiField1')),
5278
			'second',
5279
			'/label',
5280
			'/div',
5281
			array('div' => array('class' => 'checkbox')),
5282
			array('input' => array(
5283
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5284
				'value' => '2', 'id' => 'ModelMultiField2'
5285
			)),
5286
			array('label' => array('for' => 'ModelMultiField2')),
5287
			'third',
5288
			'/label',
5289
			'/div'
5290
		);
5291
		$this->assertTags($result, $expected);
5292
 
5293
		$result = $this->Form->select(
5294
			'Model.multi_field',
5295
			array('a' => 'first', 'b' => 'second', 'c' => 'third'),
5296
			array('multiple' => 'checkbox')
5297
		);
5298
		$expected = array(
5299
			'input' => array(
5300
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5301
			),
5302
			array('div' => array('class' => 'checkbox')),
5303
			array('input' => array(
5304
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5305
				'value' => 'a', 'id' => 'ModelMultiFieldA'
5306
			)),
5307
			array('label' => array('for' => 'ModelMultiFieldA')),
5308
			'first',
5309
			'/label',
5310
			'/div',
5311
			array('div' => array('class' => 'checkbox')),
5312
			array('input' => array(
5313
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5314
				'value' => 'b', 'id' => 'ModelMultiFieldB'
5315
			)),
5316
			array('label' => array('for' => 'ModelMultiFieldB')),
5317
			'second',
5318
			'/label',
5319
			'/div',
5320
			array('div' => array('class' => 'checkbox')),
5321
			array('input' => array(
5322
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5323
				'value' => 'c', 'id' => 'ModelMultiFieldC'
5324
			)),
5325
			array('label' => array('for' => 'ModelMultiFieldC')),
5326
			'third',
5327
			'/label',
5328
			'/div'
5329
		);
5330
		$this->assertTags($result, $expected);
5331
 
5332
		$result = $this->Form->select(
5333
			'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
5334
		);
5335
		$expected = array(
5336
			'input' => array(
5337
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5338
			),
5339
			array('div' => array('class' => 'checkbox')),
5340
			array('input' => array(
5341
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5342
				'value' => '1', 'id' => 'ModelMultiField1'
5343
			)),
5344
			array('label' => array('for' => 'ModelMultiField1')),
5345
			'first',
5346
			'/label',
5347
			'/div'
5348
		);
5349
		$this->assertTags($result, $expected);
5350
 
5351
		$this->Form->request->data = array('Model' => array('tags' => array(1)));
5352
		$result = $this->Form->select(
5353
			'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
5354
		);
5355
		$expected = array(
5356
			'input' => array(
5357
				'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
5358
			),
5359
			array('div' => array('class' => 'checkbox')),
5360
			array('input' => array(
5361
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5362
				'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
5363
			)),
5364
			array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
5365
			'first',
5366
			'/label',
5367
			'/div',
5368
 
5369
			array('div' => array('class' => 'checkbox')),
5370
			array('input' => array(
5371
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5372
				'value' => 'Array', 'id' => 'ModelTagsArray'
5373
			)),
5374
			array('label' => array('for' => 'ModelTagsArray')),
5375
			'Array',
5376
			'/label',
5377
			'/div'
5378
		);
5379
		$this->assertTags($result, $expected);
5380
 
5381
		$result = $this->Form->select(
5382
			'Model.multi_field',
5383
			array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
5384
			array('multiple' => 'checkbox')
5385
		);
5386
		$expected = array(
5387
			'input' => array(
5388
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5389
			),
5390
			array('div' => array('class' => 'checkbox')),
5391
			array('input' => array(
5392
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5393
				'value' => 'a+', 'id' => 'ModelMultiFieldA2'
5394
			)),
5395
			array('label' => array('for' => 'ModelMultiFieldA2')),
5396
			'first',
5397
			'/label',
5398
			'/div',
5399
			array('div' => array('class' => 'checkbox')),
5400
			array('input' => array(
5401
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5402
				'value' => 'a++', 'id' => 'ModelMultiFieldA1'
5403
			)),
5404
			array('label' => array('for' => 'ModelMultiFieldA1')),
5405
			'second',
5406
			'/label',
5407
			'/div',
5408
			array('div' => array('class' => 'checkbox')),
5409
			array('input' => array(
5410
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5411
				'value' => 'a+++', 'id' => 'ModelMultiFieldA'
5412
			)),
5413
			array('label' => array('for' => 'ModelMultiFieldA')),
5414
			'third',
5415
			'/label',
5416
			'/div'
5417
		);
5418
 
5419
		$this->assertTags($result, $expected);
5420
 
5421
		$result = $this->Form->select(
5422
			'Model.multi_field',
5423
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
5424
			array('multiple' => 'checkbox')
5425
		);
5426
		$expected = array(
5427
			'input' => array(
5428
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5429
			),
5430
			array('div' => array('class' => 'checkbox')),
5431
			array('input' => array(
5432
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5433
				'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
5434
			)),
5435
			array('label' => array('for' => 'ModelMultiFieldAB2')),
5436
			'first',
5437
			'/label',
5438
			'/div',
5439
			array('div' => array('class' => 'checkbox')),
5440
			array('input' => array(
5441
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5442
				'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
5443
			)),
5444
			array('label' => array('for' => 'ModelMultiFieldAB1')),
5445
			'second',
5446
			'/label',
5447
			'/div',
5448
			array('div' => array('class' => 'checkbox')),
5449
			array('input' => array(
5450
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5451
				'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
5452
			)),
5453
			array('label' => array('for' => 'ModelMultiFieldAB')),
5454
			'third',
5455
			'/label',
5456
			'/div'
5457
		);
5458
		$this->assertTags($result, $expected);
5459
	}
5460
 
5461
/**
5462
 * test multiple checkboxes with div styles.
5463
 *
5464
 * @return void
5465
 */
5466
	public function testSelectMultipleCheckboxDiv() {
5467
		$result = $this->Form->select(
5468
			'Model.tags',
5469
			array('first', 'second'),
5470
			array('multiple' => 'checkbox', 'class' => 'my-class')
5471
		);
5472
		$expected = array(
5473
			'input' => array(
5474
				'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
5475
			),
5476
			array('div' => array('class' => 'my-class')),
5477
			array('input' => array(
5478
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5479
				'value' => '0', 'id' => 'ModelTags0'
5480
			)),
5481
			array('label' => array('for' => 'ModelTags0')), 'first', '/label',
5482
			'/div',
5483
 
5484
			array('div' => array('class' => 'my-class')),
5485
			array('input' => array(
5486
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5487
				'value' => '1', 'id' => 'ModelTags1'
5488
			)),
5489
			array('label' => array('for' => 'ModelTags1')), 'second', '/label',
5490
			'/div'
5491
		);
5492
		$this->assertTags($result, $expected);
5493
 
5494
		$result = $this->Form->input('Model.tags', array(
5495
			'options' => array('first', 'second'),
5496
			'multiple' => 'checkbox',
5497
			'class' => 'my-class',
5498
			'div' => false,
5499
			'label' => false
5500
		));
5501
		$this->assertTags($result, $expected);
5502
 
5503
		$Contact = ClassRegistry::getObject('Contact');
5504
		$Contact->validationErrors['tags'] = 'Select atleast one option';
5505
		$result = $this->Form->input('Contact.tags', array(
5506
			'options' => array('one'),
5507
			'multiple' => 'checkbox',
5508
			'label' => false,
5509
			'div' => false
5510
		));
5511
		$expected = array(
5512
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
5513
			array('div' => array('class' => 'checkbox form-error')),
5514
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
5515
			array('label' => array('for' => 'ContactTags0')),
5516
			'one',
5517
			'/label',
5518
			'/div'
5519
		);
5520
		$this->assertTags($result, $expected);
5521
 
5522
		$result = $this->Form->input('Contact.tags', array(
5523
			'options' => array('one'),
5524
			'multiple' => 'checkbox',
5525
			'class' => 'mycheckbox',
5526
			'label' => false,
5527
			'div' => false
5528
		));
5529
		$expected = array(
5530
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
5531
			array('div' => array('class' => 'mycheckbox form-error')),
5532
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
5533
			array('label' => array('for' => 'ContactTags0')),
5534
			'one',
5535
			'/label',
5536
			'/div'
5537
		);
5538
		$this->assertTags($result, $expected);
5539
	}
5540
 
5541
/**
5542
 * Checks the security hash array generated for multiple-input checkbox elements
5543
 *
5544
 * @return void
5545
 */
5546
	public function testSelectMultipleCheckboxSecurity() {
5547
		$this->Form->request['_Token'] = array('key' => 'testKey');
5548
		$this->assertEquals(array(), $this->Form->fields);
5549
 
5550
		$result = $this->Form->select(
5551
			'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
5552
			array('multiple' => 'checkbox')
5553
		);
5554
		$this->assertEquals(array('Model.multi_field'), $this->Form->fields);
5555
 
5556
		$result = $this->Form->secure($this->Form->fields);
5557
		$key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
5558
		$this->assertRegExp('/"' . $key . '"/', $result);
5559
	}
5560
 
5561
/**
5562
 * Multiple select elements should always be secured as they always participate
5563
 * in the POST data.
5564
 *
5565
 * @return void
5566
 */
5567
	public function testSelectMultipleSecureWithNoOptions() {
5568
		$this->Form->request['_Token'] = array('key' => 'testkey');
5569
		$this->assertEquals(array(), $this->Form->fields);
5570
 
5571
		$this->Form->select(
5572
			'Model.select',
5573
			array(),
5574
			array('multiple' => true)
5575
		);
5576
		$this->assertEquals(array('Model.select'), $this->Form->fields);
5577
	}
5578
/**
5579
 * When a select box has no options it should not be added to the fields list
5580
 * as it always fail post validation.
5581
 *
5582
 * @return void
5583
 */
5584
	public function testSelectNoSecureWithNoOptions() {
5585
		$this->Form->request['_Token'] = array('key' => 'testkey');
5586
		$this->assertEquals(array(), $this->Form->fields);
5587
 
5588
		$this->Form->select(
5589
			'Model.select',
5590
			array()
5591
		);
5592
		$this->assertEquals(array(), $this->Form->fields);
5593
 
5594
		$this->Form->select(
5595
			'Model.select',
5596
			array(),
5597
			array('empty' => true)
5598
		);
5599
		$this->assertEquals(array('Model.select'), $this->Form->fields);
5600
	}
5601
 
5602
/**
5603
 * testInputMultipleCheckboxes method
5604
 *
5605
 * test input() resulting in multi select elements being generated.
5606
 *
5607
 * @return void
5608
 */
5609
	public function testInputMultipleCheckboxes() {
5610
		$result = $this->Form->input('Model.multi_field', array(
5611
			'options' => array('first', 'second', 'third'),
5612
			'multiple' => 'checkbox'
5613
		));
5614
		$expected = array(
5615
			array('div' => array('class' => 'input select')),
5616
			array('label' => array('for' => 'ModelMultiField')),
5617
			'Multi Field',
5618
			'/label',
5619
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5620
			array('div' => array('class' => 'checkbox')),
5621
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5622
			array('label' => array('for' => 'ModelMultiField0')),
5623
			'first',
5624
			'/label',
5625
			'/div',
5626
			array('div' => array('class' => 'checkbox')),
5627
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5628
			array('label' => array('for' => 'ModelMultiField1')),
5629
			'second',
5630
			'/label',
5631
			'/div',
5632
			array('div' => array('class' => 'checkbox')),
5633
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
5634
			array('label' => array('for' => 'ModelMultiField2')),
5635
			'third',
5636
			'/label',
5637
			'/div',
5638
			'/div'
5639
		);
5640
		$this->assertTags($result, $expected);
5641
 
5642
		$result = $this->Form->input('Model.multi_field', array(
5643
			'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
5644
			'multiple' => 'checkbox'
5645
		));
5646
		$expected = array(
5647
			array('div' => array('class' => 'input select')),
5648
			array('label' => array('for' => 'ModelMultiField')),
5649
			'Multi Field',
5650
			'/label',
5651
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5652
			array('div' => array('class' => 'checkbox')),
5653
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
5654
			array('label' => array('for' => 'ModelMultiFieldA')),
5655
			'first',
5656
			'/label',
5657
			'/div',
5658
			array('div' => array('class' => 'checkbox')),
5659
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
5660
			array('label' => array('for' => 'ModelMultiFieldB')),
5661
			'second',
5662
			'/label',
5663
			'/div',
5664
			array('div' => array('class' => 'checkbox')),
5665
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
5666
			array('label' => array('for' => 'ModelMultiFieldC')),
5667
			'third',
5668
			'/label',
5669
			'/div',
5670
			'/div'
5671
		);
5672
		$this->assertTags($result, $expected);
5673
 
5674
		$result = $this->Form->input('Model.multi_field', array(
5675
			'options' => array('1' => 'first'),
5676
			'multiple' => 'checkbox',
5677
			'label' => false,
5678
			'div' => false
5679
		));
5680
		$expected = array(
5681
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5682
			array('div' => array('class' => 'checkbox')),
5683
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5684
			array('label' => array('for' => 'ModelMultiField1')),
5685
			'first',
5686
			'/label',
5687
			'/div'
5688
		);
5689
		$this->assertTags($result, $expected);
5690
 
5691
		$result = $this->Form->input('Model.multi_field', array(
5692
			'options' => array('2' => 'second'),
5693
			'multiple' => 'checkbox',
5694
			'label' => false,
5695
			'div' => false
5696
		));
5697
		$expected = array(
5698
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5699
			array('div' => array('class' => 'checkbox')),
5700
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
5701
			array('label' => array('for' => 'ModelMultiField2')),
5702
			'second',
5703
			'/label',
5704
			'/div'
5705
		);
5706
		$this->assertTags($result, $expected);
5707
	}
5708
 
5709
/**
5710
 * testSelectHiddenFieldOmission method
5711
 *
5712
 * test that select() with 'hiddenField' => false omits the hidden field
5713
 *
5714
 * @return void
5715
 */
5716
	public function testSelectHiddenFieldOmission() {
5717
		$result = $this->Form->select('Model.multi_field',
5718
			array('first', 'second'),
5719
			array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
5720
		);
5721
		$expected = array(
5722
			array('div' => array('class' => 'checkbox')),
5723
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5724
			array('label' => array('for' => 'ModelMultiField0')),
5725
			'first',
5726
			'/label',
5727
			'/div',
5728
			array('div' => array('class' => 'checkbox')),
5729
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5730
			array('label' => array('for' => 'ModelMultiField1')),
5731
			'second',
5732
			'/label',
5733
			'/div'
5734
		);
5735
		$this->assertTags($result, $expected);
5736
 
5737
		$result = $this->Form->input('Model.multi_field', array(
5738
			'options' => array('first', 'second'),
5739
			'multiple' => 'checkbox',
5740
			'hiddenField' => false
5741
		));
5742
		$expected = array(
5743
			array('div' => array('class' => 'input select')),
5744
			array('label' => array('for' => 'ModelMultiField')),
5745
			'Multi Field',
5746
			'/label',
5747
			array('div' => array('class' => 'checkbox')),
5748
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5749
			array('label' => array('for' => 'ModelMultiField0')),
5750
			'first',
5751
			'/label',
5752
			'/div',
5753
			array('div' => array('class' => 'checkbox')),
5754
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5755
			array('label' => array('for' => 'ModelMultiField1')),
5756
			'second',
5757
			'/label',
5758
			'/div',
5759
			'/div'
5760
		);
5761
		$this->assertTags($result, $expected);
5762
	}
5763
 
5764
/**
5765
 * test that select() with multiple = checkbox works with overriding name attribute.
5766
 *
5767
 * @return void
5768
 */
5769
	public function testSelectCheckboxMultipleOverrideName() {
5770
		$result = $this->Form->input('category', array(
5771
			'type' => 'select',
5772
			'multiple' => 'checkbox',
5773
			'name' => 'data[fish]',
5774
			'options' => array('1', '2'),
5775
			'div' => false,
5776
			'label' => false,
5777
		));
5778
		$expected = array(
5779
			'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
5780
			array('div' => array('class' => 'checkbox')),
5781
				array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
5782
				array('label' => array('for' => 'Category0')), '1', '/label',
5783
			'/div',
5784
			array('div' => array('class' => 'checkbox')),
5785
				array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
5786
				array('label' => array('for' => 'Category1')), '2', '/label',
5787
			'/div'
5788
		);
5789
		$this->assertTags($result, $expected);
5790
	}
5791
 
5792
/**
5793
 * Test that 'id' overrides all the checkbox id's as well.
5794
 *
5795
 * @return void
5796
 */
5797
	public function testSelectCheckboxMultipleId() {
5798
		$result = $this->Form->select(
5799
			'Model.multi_field',
5800
			array('first', 'second', 'third'),
5801
			array('multiple' => 'checkbox', 'id' => 'CustomId')
5802
		);
5803
 
5804
		$expected = array(
5805
			'input' => array(
5806
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
5807
			),
5808
			array('div' => array('class' => 'checkbox')),
5809
			array('input' => array(
5810
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5811
				'value' => '0', 'id' => 'CustomId0'
5812
			)),
5813
			array('label' => array('for' => 'CustomId0')),
5814
			'first',
5815
			'/label',
5816
			'/div',
5817
			array('div' => array('class' => 'checkbox')),
5818
			array('input' => array(
5819
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5820
				'value' => '1', 'id' => 'CustomId1'
5821
			)),
5822
			array('label' => array('for' => 'CustomId1')),
5823
			'second',
5824
			'/label',
5825
			'/div',
5826
			array('div' => array('class' => 'checkbox')),
5827
			array('input' => array(
5828
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5829
				'value' => '2', 'id' => 'CustomId2'
5830
			)),
5831
			array('label' => array('for' => 'CustomId2')),
5832
			'third',
5833
			'/label',
5834
			'/div'
5835
		);
5836
		$this->assertTags($result, $expected);
5837
	}
5838
 
5839
/**
5840
 * testCheckbox method
5841
 *
5842
 * Test generation of checkboxes
5843
 *
5844
 * @return void
5845
 */
5846
	public function testCheckbox() {
5847
		$result = $this->Form->checkbox('Model.field');
5848
		$expected = array(
5849
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5850
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
5851
		);
5852
		$this->assertTags($result, $expected);
5853
 
5854
		$result = $this->Form->checkbox('Model.field', array(
5855
			'id' => 'theID',
5856
			'value' => 'myvalue',
5857
			'form' => 'my-form',
5858
		));
5859
		$expected = array(
5860
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_', 'form' => 'my-form'),
5861
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form'))
5862
		);
5863
		$this->assertTags($result, $expected);
5864
 
5865
		$Contact = ClassRegistry::getObject('Contact');
5866
		$Contact->validationErrors['field'] = 1;
5867
		$this->Form->request->data['Contact']['field'] = 'myvalue';
5868
		$result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
5869
		$expected = array(
5870
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
5871
			array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
5872
		);
5873
		$this->assertTags($result, $expected);
5874
 
5875
		$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
5876
		$expected = array(
5877
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
5878
			array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
5879
		);
5880
		$this->assertTags($result, $expected);
5881
 
5882
		$this->Form->request->data['Contact']['field'] = '';
5883
		$result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
5884
		$expected = array(
5885
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
5886
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
5887
		);
5888
		$this->assertTags($result, $expected);
5889
 
5890
		$Contact->validationErrors = array();
5891
		$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
5892
		$expected = array(
5893
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
5894
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
5895
		);
5896
		$this->assertTags($result, $expected);
5897
 
5898
		$this->Form->request->data['Contact']['published'] = 1;
5899
		$result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
5900
		$expected = array(
5901
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
5902
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
5903
		);
5904
		$this->assertTags($result, $expected);
5905
 
5906
		$this->Form->request->data['Contact']['published'] = 0;
5907
		$result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
5908
		$expected = array(
5909
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
5910
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
5911
		);
5912
		$this->assertTags($result, $expected);
5913
 
5914
		$result = $this->Form->checkbox('Model.CustomField.1.value');
5915
		$expected = array(
5916
			'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
5917
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
5918
		);
5919
		$this->assertTags($result, $expected);
5920
 
5921
		$result = $this->Form->checkbox('CustomField.1.value');
5922
		$expected = array(
5923
			'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
5924
			array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
5925
		);
5926
		$this->assertTags($result, $expected);
5927
	}
5928
 
5929
/**
5930
 * test checkbox() with a custom name attribute
5931
 *
5932
 * @return void
5933
 */
5934
	public function testCheckboxCustomNameAttribute() {
5935
		$result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
5936
		$expected = array(
5937
				'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
5938
				array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
5939
			);
5940
		$this->assertTags($result, $expected);
5941
	}
5942
 
5943
/**
5944
 * test the checked option for checkboxes.
5945
 *
5946
 * @return void
5947
 */
5948
	public function testCheckboxCheckedOption() {
5949
		$result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
5950
		$expected = array(
5951
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5952
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
5953
		);
5954
		$this->assertTags($result, $expected);
5955
 
5956
		$result = $this->Form->checkbox('Model.field', array('checked' => 1));
5957
		$expected = array(
5958
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5959
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
5960
		);
5961
		$this->assertTags($result, $expected);
5962
 
5963
		$result = $this->Form->checkbox('Model.field', array('checked' => true));
5964
		$expected = array(
5965
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5966
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
5967
		);
5968
		$this->assertTags($result, $expected);
5969
 
5970
		$result = $this->Form->checkbox('Model.field', array('checked' => false));
5971
		$expected = array(
5972
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5973
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
5974
		);
5975
		$this->assertTags($result, $expected);
5976
 
5977
		$this->Form->request->data['Model']['field'] = 1;
5978
		$result = $this->Form->checkbox('Model.field', array('checked' => false));
5979
		$expected = array(
5980
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5981
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
5982
		);
5983
		$this->assertTags($result, $expected);
5984
	}
5985
 
5986
/**
5987
 * Test that disabled attribute works on both the checkbox and hidden input.
5988
 *
5989
 * @return void
5990
 */
5991
	public function testCheckboxDisabling() {
5992
		$result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
5993
		$expected = array(
5994
			array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
5995
			array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
5996
		);
5997
		$this->assertTags($result, $expected);
5998
 
5999
		$result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
6000
		$expected = array(
6001
			array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
6002
			array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
6003
		);
6004
		$this->assertTags($result, $expected);
6005
	}
6006
 
6007
/**
6008
 * Test that the hidden input for checkboxes can be omitted or set to a
6009
 * specific value.
6010
 *
6011
 * @return void
6012
 */
6013
	public function testCheckboxHiddenField() {
6014
		$result = $this->Form->input('UserForm.something', array(
6015
			'type' => 'checkbox',
6016
			'hiddenField' => false
6017
		));
6018
		$expected = array(
6019
			'div' => array('class' => 'input checkbox'),
6020
			array('input' => array(
6021
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
6022
				'value' => '1', 'id' => 'UserFormSomething'
6023
			)),
6024
			'label' => array('for' => 'UserFormSomething'),
6025
			'Something',
6026
			'/label',
6027
			'/div'
6028
		);
6029
		$this->assertTags($result, $expected);
6030
 
6031
		$result = $this->Form->input('UserForm.something', array(
6032
			'type' => 'checkbox',
6033
			'value' => 'Y',
6034
			'hiddenField' => 'N',
6035
		));
6036
		$expected = array(
6037
			'div' => array('class' => 'input checkbox'),
6038
			array('input' => array(
6039
				'type' => 'hidden', 'name' => 'data[UserForm][something]',
6040
				'value' => 'N', 'id' => 'UserFormSomething_'
6041
			)),
6042
			array('input' => array(
6043
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
6044
				'value' => 'Y', 'id' => 'UserFormSomething'
6045
			)),
6046
			'label' => array('for' => 'UserFormSomething'),
6047
			'Something',
6048
			'/label',
6049
			'/div'
6050
		);
6051
		$this->assertTags($result, $expected);
6052
	}
6053
 
6054
/**
6055
 * Test that a checkbox can have 0 for the value and 1 for the hidden input.
6056
 *
6057
 * @return void
6058
 */
6059
	public function testCheckboxZeroValue() {
6060
		$result = $this->Form->input('User.get_spam', array(
6061
			'type' => 'checkbox',
6062
			'value' => '0',
6063
			'hiddenField' => '1',
6064
		));
6065
		$expected = array(
6066
			'div' => array('class' => 'input checkbox'),
6067
			array('input' => array(
6068
				'type' => 'hidden', 'name' => 'data[User][get_spam]',
6069
				'value' => '1', 'id' => 'UserGetSpam_'
6070
			)),
6071
			array('input' => array(
6072
				'type' => 'checkbox', 'name' => 'data[User][get_spam]',
6073
				'value' => '0', 'id' => 'UserGetSpam'
6074
			)),
6075
			'label' => array('for' => 'UserGetSpam'),
6076
			'Get Spam',
6077
			'/label',
6078
			'/div'
6079
		);
6080
		$this->assertTags($result, $expected);
6081
	}
6082
 
6083
/**
6084
 * testDateTime method
6085
 *
6086
 * Test generation of date/time select elements
6087
 *
6088
 * @return void
6089
 */
6090
	public function testDateTime() {
6091
		extract($this->dateRegex);
6092
 
6093
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
6094
		$now = strtotime('now');
6095
		$expected = array(
6096
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6097
			$daysRegex,
6098
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6099
			date('j', $now),
6100
			'/option',
6101
			'*/select',
6102
			'-',
6103
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6104
			$monthsRegex,
6105
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6106
			date('F', $now),
6107
			'/option',
6108
			'*/select',
6109
			'-',
6110
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6111
			$yearsRegex,
6112
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6113
			date('Y', $now),
6114
			'/option',
6115
			'*/select',
6116
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6117
			$hoursRegex,
6118
			array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
6119
			date('g', $now),
6120
			'/option',
6121
			'*/select',
6122
			':',
6123
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6124
			$minutesRegex,
6125
			array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
6126
			date('i', $now),
6127
			'/option',
6128
			'*/select',
6129
			' ',
6130
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6131
			$meridianRegex,
6132
			array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
6133
			date('a', $now),
6134
			'/option',
6135
			'*/select'
6136
		);
6137
		$this->assertTags($result, $expected);
6138
 
6139
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12');
6140
		$expected = array(
6141
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6142
			$daysRegex,
6143
			array('option' => array('value' => '')),
6144
			'/option',
6145
			'*/select',
6146
			'-',
6147
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6148
			$monthsRegex,
6149
			array('option' => array('value' => '')),
6150
			'/option',
6151
			'*/select',
6152
			'-',
6153
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6154
			$yearsRegex,
6155
			array('option' => array('value' => '')),
6156
			'/option',
6157
			'*/select',
6158
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6159
			$hoursRegex,
6160
			array('option' => array('value' => '')),
6161
			'/option',
6162
			'*/select',
6163
			':',
6164
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6165
			$minutesRegex,
6166
			array('option' => array('value' => '')),
6167
			'/option',
6168
			'*/select',
6169
			' ',
6170
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6171
			$meridianRegex,
6172
			array('option' => array('value' => '')),
6173
			'/option',
6174
			'*/select'
6175
		);
6176
		$this->assertTags($result, $expected);
6177
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6178
 
6179
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
6180
		$this->assertTags($result, $expected);
6181
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6182
 
6183
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
6184
		$this->assertTags($result, $expected);
6185
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6186
 
6187
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
6188
		$expected = array(
6189
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6190
			$daysRegex,
6191
			array('option' => array('value' => '')),
6192
			'/option',
6193
			'*/select',
6194
			'-',
6195
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6196
			$monthsRegex,
6197
			array('option' => array('value' => '')),
6198
			'/option',
6199
			'*/select',
6200
			'-',
6201
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6202
			$yearsRegex,
6203
			array('option' => array('value' => '')),
6204
			'/option',
6205
			'*/select',
6206
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6207
			$hoursRegex,
6208
			array('option' => array('value' => '')),
6209
			'/option',
6210
			'*/select',
6211
			':',
6212
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6213
			$minutesRegex,
6214
			array('option' => array('value' => '')),
6215
			'/option',
6216
			array('option' => array('value' => '00')),
6217
			'00',
6218
			'/option',
6219
			array('option' => array('value' => '05')),
6220
			'05',
6221
			'/option',
6222
			array('option' => array('value' => '10')),
6223
			'10',
6224
			'/option',
6225
			'*/select',
6226
			' ',
6227
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6228
			$meridianRegex,
6229
			array('option' => array('value' => '')),
6230
			'/option',
6231
			'*/select'
6232
		);
6233
		$this->assertTags($result, $expected);
6234
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6235
 
6236
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
6237
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
6238
 
6239
		$this->Form->request->data['Contact']['data'] = null;
6240
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12');
6241
		$expected = array(
6242
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6243
			$daysRegex,
6244
			array('option' => array('value' => '')),
6245
			'/option',
6246
			'*/select',
6247
			'-',
6248
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6249
			$monthsRegex,
6250
			array('option' => array('value' => '')),
6251
			'/option',
6252
			'*/select',
6253
			'-',
6254
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6255
			$yearsRegex,
6256
			array('option' => array('value' => '')),
6257
			'/option',
6258
			'*/select',
6259
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6260
			$hoursRegex,
6261
			array('option' => array('value' => '')),
6262
			'/option',
6263
			'*/select',
6264
			':',
6265
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6266
			$minutesRegex,
6267
			array('option' => array('value' => '')),
6268
			'/option',
6269
			'*/select',
6270
			' ',
6271
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6272
			$meridianRegex,
6273
			array('option' => array('value' => '')),
6274
			'/option',
6275
			'*/select'
6276
		);
6277
		$this->assertTags($result, $expected);
6278
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6279
 
6280
		$this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
6281
		$now = strtotime($this->Form->data['Model']['field']);
6282
		$result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
6283
		$expected = array(
6284
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6285
			$daysRegex,
6286
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6287
			date('j', $now),
6288
			'/option',
6289
			'*/select',
6290
			'-',
6291
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6292
			$monthsRegex,
6293
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6294
			date('F', $now),
6295
			'/option',
6296
			'*/select',
6297
			'-',
6298
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
6299
			$yearsRegex,
6300
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6301
			date('Y', $now),
6302
			'/option',
6303
			'*/select',
6304
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
6305
			$hoursRegex,
6306
			array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
6307
			date('g', $now),
6308
			'/option',
6309
			'*/select',
6310
			':',
6311
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6312
			$minutesRegex,
6313
			array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
6314
			date('i', $now),
6315
			'/option',
6316
			'*/select',
6317
			' ',
6318
			array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
6319
			$meridianRegex,
6320
			array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
6321
			date('a', $now),
6322
			'/option',
6323
			'*/select'
6324
		);
6325
		$this->assertTags($result, $expected);
6326
 
6327
		$selected = strtotime('2008-10-26 12:33:00');
6328
		$result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
6329
		$this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
6330
		$this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
6331
		$this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
6332
		$this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
6333
		$this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
6334
		$this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
6335
 
6336
		$this->Form->create('Contact');
6337
		$result = $this->Form->input('published');
6338
		$now = strtotime('now');
6339
		$expected = array(
6340
			'div' => array('class' => 'input date'),
6341
			'label' => array('for' => 'ContactPublishedMonth'),
6342
			'Published',
6343
			'/label',
6344
			array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
6345
			$monthsRegex,
6346
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6347
			date('F', $now),
6348
			'/option',
6349
			'*/select',
6350
			'-',
6351
			array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
6352
			$daysRegex,
6353
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6354
			date('j', $now),
6355
			'/option',
6356
			'*/select',
6357
			'-',
6358
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
6359
			$yearsRegex,
6360
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6361
			date('Y', $now),
6362
			'/option',
6363
			'*/select',
6364
			'/div'
6365
		);
6366
		$this->assertTags($result, $expected);
6367
 
6368
		$result = $this->Form->input('published2', array('type' => 'date'));
6369
		$now = strtotime('now');
6370
		$expected = array(
6371
			'div' => array('class' => 'input date'),
6372
			'label' => array('for' => 'ContactPublished2Month'),
6373
			'Published2',
6374
			'/label',
6375
			array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
6376
			$monthsRegex,
6377
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6378
			date('F', $now),
6379
			'/option',
6380
			'*/select',
6381
			'-',
6382
			array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
6383
			$daysRegex,
6384
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6385
			date('j', $now),
6386
			'/option',
6387
			'*/select',
6388
			'-',
6389
			array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
6390
			$yearsRegex,
6391
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6392
			date('Y', $now),
6393
			'/option',
6394
			'*/select',
6395
			'/div'
6396
		);
6397
		$this->assertTags($result, $expected);
6398
 
6399
		$this->Form->create('Contact');
6400
		$result = $this->Form->input('published', array('monthNames' => false));
6401
		$now = strtotime('now');
6402
		$expected = array(
6403
			'div' => array('class' => 'input date'),
6404
			'label' => array('for' => 'ContactPublishedMonth'),
6405
			'Published',
6406
			'/label',
6407
			array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
6408
			'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
6409
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6410
			date('m', $now),
6411
			'/option',
6412
			'*/select',
6413
			'-',
6414
			array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
6415
			$daysRegex,
6416
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6417
			date('j', $now),
6418
			'/option',
6419
			'*/select',
6420
			'-',
6421
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
6422
			$yearsRegex,
6423
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6424
			date('Y', $now),
6425
			'/option',
6426
			'*/select',
6427
			'/div'
6428
		);
6429
		$this->assertTags($result, $expected);
6430
 
6431
		$result = $this->Form->input('published', array(
6432
			'timeFormat' => 24,
6433
			'interval' => 5,
6434
			'selected' => strtotime('2009-09-03 13:37:00'),
6435
			'type' => 'datetime'
6436
		));
6437
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6438
		$this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
6439
		$this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
6440
		$this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
6441
		$this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
6442
	}
6443
 
6444
/**
6445
 * Test dateTime with rounding
6446
 *
6447
 * @return void
6448
 */
6449
	public function testDateTimeRounding() {
6450
		$this->Form->request->data['Contact'] = array(
6451
			'date' => array(
6452
				'day' => '13',
6453
				'month' => '12',
6454
				'year' => '2010',
6455
				'hour' => '04',
6456
				'min' => '19',
6457
				'meridian' => 'AM'
6458
			)
6459
		);
6460
 
6461
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
6462
		$this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
6463
 
6464
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
6465
		$this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
6466
 
6467
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
6468
		$this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
6469
	}
6470
 
6471
/**
6472
 * Test that empty values don't trigger errors.
6473
 *
6474
 * @return void
6475
 */
6476
	public function testDateTimeNoErrorsOnEmptyData() {
6477
		$this->Form->request->data['Contact'] = array(
6478
			'date' => array(
6479
				'day' => '',
6480
				'month' => '',
6481
				'year' => '',
6482
				'hour' => '',
6483
				'min' => '',
6484
				'meridian' => ''
6485
			)
6486
		);
6487
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
6488
		$this->assertNotEmpty($result);
6489
	}
6490
 
6491
/**
6492
 * test that datetime() and default values work.
6493
 *
6494
 * @return void
6495
 */
6496
	public function testDatetimeWithDefault() {
6497
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
6498
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6499
		$this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
6500
		$this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
6501
 
6502
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
6503
			'default' => '2009-06-01 11:15:30'
6504
		));
6505
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6506
		$this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
6507
		$this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
6508
	}
6509
 
6510
/**
6511
 * test that bogus non-date time data doesn't cause errors.
6512
 *
6513
 * @return void
6514
 */
6515
	public function testDateTimeWithBogusData() {
6516
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
6517
		$this->assertNotRegExp('/selected="selected">\d/', $result);
6518
	}
6519
 
6520
/**
6521
 * testDateTime all zeros
6522
 *
6523
 * @return void
6524
 */
6525
	public function testDateTimeAllZeros() {
6526
		$result = $this->Form->dateTime('Contact.date',
6527
			'DMY',
6528
			false,
6529
			array(
6530
				'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
6531
				'value' => '0000-00-00'
6532
			)
6533
		);
6534
 
6535
		$this->assertRegExp('/<option value="">-<\/option>/', $result);
6536
		$this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
6537
	}
6538
 
6539
/**
6540
 * testDateTimeEmptyAsArray
6541
 *
6542
 * @return void
6543
 */
6544
	public function testDateTimeEmptyAsArray() {
6545
		$result = $this->Form->dateTime('Contact.date',
6546
			'DMY',
6547
			'12',
6548
			array(
6549
				'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
6550
					'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
6551
				)
6552
			)
6553
		);
6554
 
6555
		$this->assertRegExp('/<option value="">DAY<\/option>/', $result);
6556
		$this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
6557
		$this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
6558
		$this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
6559
		$this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
6560
		$this->assertNotRegExp('/<option value=""><\/option>/', $result);
6561
 
6562
		$result = $this->Form->dateTime('Contact.date',
6563
			'DMY',
6564
			'12',
6565
			array(
6566
				'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
6567
			)
6568
		);
6569
 
6570
		$this->assertRegExp('/<option value="">DAY<\/option>/', $result);
6571
		$this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
6572
		$this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
6573
		$this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
6574
		$this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
6575
		$this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
6576
	}
6577
 
6578
/**
6579
 * testFormDateTimeMulti method
6580
 *
6581
 * test multiple datetime element generation
6582
 *
6583
 * @return void
6584
 */
6585
	public function testFormDateTimeMulti() {
6586
		extract($this->dateRegex);
6587
 
6588
		$result = $this->Form->dateTime('Contact.1.updated');
6589
		$expected = array(
6590
			array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
6591
			$daysRegex,
6592
			array('option' => array('value' => '')),
6593
			'/option',
6594
			'*/select',
6595
			'-',
6596
			array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
6597
			$monthsRegex,
6598
			array('option' => array('value' => '')),
6599
			'/option',
6600
			'*/select',
6601
			'-',
6602
			array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
6603
			$yearsRegex,
6604
			array('option' => array('value' => '')),
6605
			'/option',
6606
			'*/select',
6607
			array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
6608
			$hoursRegex,
6609
			array('option' => array('value' => '')),
6610
			'/option',
6611
			'*/select',
6612
			':',
6613
			array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
6614
			$minutesRegex,
6615
			array('option' => array('value' => '')),
6616
			'/option',
6617
			'*/select',
6618
			' ',
6619
			array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
6620
			$meridianRegex,
6621
			array('option' => array('value' => '')),
6622
			'/option',
6623
			'*/select'
6624
		);
6625
		$this->assertTags($result, $expected);
6626
 
6627
		$result = $this->Form->dateTime('Contact.2.updated');
6628
		$expected = array(
6629
			array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
6630
			$daysRegex,
6631
			array('option' => array('value' => '')),
6632
			'/option',
6633
			'*/select',
6634
			'-',
6635
			array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
6636
			$monthsRegex,
6637
			array('option' => array('value' => '')),
6638
			'/option',
6639
			'*/select',
6640
			'-',
6641
			array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
6642
			$yearsRegex,
6643
			array('option' => array('value' => '')),
6644
			'/option',
6645
			'*/select',
6646
			array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
6647
			$hoursRegex,
6648
			array('option' => array('value' => '')),
6649
			'/option',
6650
			'*/select',
6651
			':',
6652
			array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
6653
			$minutesRegex,
6654
			array('option' => array('value' => '')),
6655
			'/option',
6656
			'*/select',
6657
			' ',
6658
			array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
6659
			$meridianRegex,
6660
			array('option' => array('value' => '')),
6661
			'/option',
6662
			'*/select'
6663
		);
6664
		$this->assertTags($result, $expected);
6665
	}
6666
 
6667
/**
6668
 * When changing the date format, the label should always focus the first select box when
6669
 * clicked.
6670
 *
6671
 * @return void
6672
 */
6673
	public function testDateTimeLabelIdMatchesFirstInput() {
6674
		$result = $this->Form->input('Model.date', array('type' => 'date'));
6675
		$this->assertContains('label for="ModelDateMonth"', $result);
6676
 
6677
		$result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
6678
		$this->assertContains('label for="ModelDateDay"', $result);
6679
 
6680
		$result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
6681
		$this->assertContains('label for="ModelDateYear"', $result);
6682
	}
6683
 
6684
/**
6685
 * testMonth method
6686
 *
6687
 * @return void
6688
 */
6689
	public function testMonth() {
6690
		$result = $this->Form->month('Model.field');
6691
		$expected = array(
6692
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6693
			array('option' => array('value' => '')),
6694
			'/option',
6695
			array('option' => array('value' => '01')),
6696
			date('F', strtotime('2008-01-01 00:00:00')),
6697
			'/option',
6698
			array('option' => array('value' => '02')),
6699
			date('F', strtotime('2008-02-01 00:00:00')),
6700
			'/option',
6701
			'*/select',
6702
		);
6703
		$this->assertTags($result, $expected);
6704
 
6705
		$result = $this->Form->month('Model.field', array('empty' => true));
6706
		$expected = array(
6707
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6708
			array('option' => array('value' => '')),
6709
			'/option',
6710
			array('option' => array('value' => '01')),
6711
			date('F', strtotime('2008-01-01 00:00:00')),
6712
			'/option',
6713
			array('option' => array('value' => '02')),
6714
			date('F', strtotime('2008-02-01 00:00:00')),
6715
			'/option',
6716
			'*/select',
6717
		);
6718
		$this->assertTags($result, $expected);
6719
 
6720
		$result = $this->Form->month('Model.field', array('monthNames' => false));
6721
		$expected = array(
6722
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6723
			array('option' => array('value' => '')),
6724
			'/option',
6725
			array('option' => array('value' => '01')),
6726
			'01',
6727
			'/option',
6728
			array('option' => array('value' => '02')),
6729
			'02',
6730
			'/option',
6731
			'*/select',
6732
		);
6733
		$this->assertTags($result, $expected);
6734
 
6735
		$monthNames = array(
6736
			'01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
6737
			'07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
6738
		$result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
6739
		$expected = array(
6740
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6741
			array('option' => array('value' => '')),
6742
			'/option',
6743
			array('option' => array('value' => '01')),
6744
			'Jan',
6745
			'/option',
6746
			array('option' => array('value' => '02')),
6747
			'Feb',
6748
			'/option',
6749
			'*/select',
6750
		);
6751
		$this->assertTags($result, $expected);
6752
 
6753
		$this->Form->request->data['Project']['release'] = '2050-02-10';
6754
		$result = $this->Form->month('Project.release');
6755
 
6756
		$expected = array(
6757
			array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
6758
			array('option' => array('value' => '')),
6759
			'/option',
6760
			array('option' => array('value' => '01')),
6761
			'January',
6762
			'/option',
6763
			array('option' => array('value' => '02', 'selected' => 'selected')),
6764
			'February',
6765
			'/option',
6766
			'*/select',
6767
		);
6768
		$this->assertTags($result, $expected);
6769
 
6770
		$this->Form->request->data['Model']['field'] = '12a';
6771
		$result = $this->Form->month('Model.field');
6772
		$expected = array(
6773
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6774
			array('option' => array('value' => '')),
6775
			'/option',
6776
			array('option' => array('value' => '01')),
6777
			date('F', strtotime('2008-01-01 00:00:00')),
6778
			'/option',
6779
			array('option' => array('value' => '02')),
6780
			date('F', strtotime('2008-02-01 00:00:00')),
6781
			'/option',
6782
			'*/select',
6783
		);
6784
		$this->assertTags($result, $expected);
6785
	}
6786
 
6787
/**
6788
 * testDay method
6789
 *
6790
 * @return void
6791
 */
6792
	public function testDay() {
6793
		extract($this->dateRegex);
6794
 
6795
		$result = $this->Form->day('Model.field', array('value' => false));
6796
		$expected = array(
6797
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6798
			array('option' => array('value' => '')),
6799
			'/option',
6800
			array('option' => array('value' => '01')),
6801
			'1',
6802
			'/option',
6803
			array('option' => array('value' => '02')),
6804
			'2',
6805
			'/option',
6806
			$daysRegex,
6807
			'/select',
6808
		);
6809
		$this->assertTags($result, $expected);
6810
 
6811
		$this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
6812
		$result = $this->Form->day('Model.field');
6813
		$expected = array(
6814
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6815
			array('option' => array('value' => '')),
6816
			'/option',
6817
			array('option' => array('value' => '01')),
6818
			'1',
6819
			'/option',
6820
			array('option' => array('value' => '02')),
6821
			'2',
6822
			'/option',
6823
			$daysRegex,
6824
			array('option' => array('value' => '10', 'selected' => 'selected')),
6825
			'10',
6826
			'/option',
6827
			$daysRegex,
6828
			'/select',
6829
		);
6830
		$this->assertTags($result, $expected);
6831
 
6832
		$this->Form->request->data['Model']['field'] = '';
6833
		$result = $this->Form->day('Model.field', array('value' => '10'));
6834
		$expected = array(
6835
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6836
			array('option' => array('value' => '')),
6837
			'/option',
6838
			array('option' => array('value' => '01')),
6839
			'1',
6840
			'/option',
6841
			array('option' => array('value' => '02')),
6842
			'2',
6843
			'/option',
6844
			$daysRegex,
6845
			array('option' => array('value' => '10', 'selected' => 'selected')),
6846
			'10',
6847
			'/option',
6848
			$daysRegex,
6849
			'/select',
6850
		);
6851
		$this->assertTags($result, $expected);
6852
 
6853
		$this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
6854
		$result = $this->Form->day('Model.field', array('value' => true));
6855
		$expected = array(
6856
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6857
			array('option' => array('value' => '')),
6858
			'/option',
6859
			array('option' => array('value' => '01')),
6860
			'1',
6861
			'/option',
6862
			array('option' => array('value' => '02')),
6863
			'2',
6864
			'/option',
6865
			$daysRegex,
6866
			array('option' => array('value' => '10', 'selected' => 'selected')),
6867
			'10',
6868
			'/option',
6869
			$daysRegex,
6870
			'/select',
6871
		);
6872
		$this->assertTags($result, $expected);
6873
 
6874
		$this->Form->request->data['Project']['release'] = '2050-10-10';
6875
		$result = $this->Form->day('Project.release');
6876
 
6877
		$expected = array(
6878
			array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
6879
			array('option' => array('value' => '')),
6880
			'/option',
6881
			array('option' => array('value' => '01')),
6882
			'1',
6883
			'/option',
6884
			array('option' => array('value' => '02')),
6885
			'2',
6886
			'/option',
6887
			$daysRegex,
6888
			array('option' => array('value' => '10', 'selected' => 'selected')),
6889
			'10',
6890
			'/option',
6891
			$daysRegex,
6892
			'/select',
6893
		);
6894
		$this->assertTags($result, $expected);
6895
 
6896
		$this->Form->request->data['Model']['field'] = '12e';
6897
		$result = $this->Form->day('Model.field');
6898
		$expected = array(
6899
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6900
			array('option' => array('value' => '')),
6901
			'/option',
6902
			array('option' => array('value' => '01')),
6903
			'1',
6904
			'/option',
6905
			array('option' => array('value' => '02')),
6906
			'2',
6907
			'/option',
6908
			$daysRegex,
6909
			'/select',
6910
		);
6911
		$this->assertTags($result, $expected);
6912
	}
6913
 
6914
/**
6915
 * testMinute method
6916
 *
6917
 * @return void
6918
 */
6919
	public function testMinute() {
6920
		extract($this->dateRegex);
6921
 
6922
		$result = $this->Form->minute('Model.field');
6923
		$expected = array(
6924
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6925
			array('option' => array('value' => '')),
6926
			'/option',
6927
			array('option' => array('value' => '00')),
6928
			'00',
6929
			'/option',
6930
			array('option' => array('value' => '01')),
6931
			'01',
6932
			'/option',
6933
			array('option' => array('value' => '02')),
6934
			'02',
6935
			'/option',
6936
			$minutesRegex,
6937
			'/select',
6938
		);
6939
		$this->assertTags($result, $expected);
6940
 
6941
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
6942
		$result = $this->Form->minute('Model.field');
6943
		$expected = array(
6944
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6945
			array('option' => array('value' => '')),
6946
			'/option',
6947
			array('option' => array('value' => '00')),
6948
			'00',
6949
			'/option',
6950
			array('option' => array('value' => '01')),
6951
			'01',
6952
			'/option',
6953
			array('option' => array('value' => '02')),
6954
			'02',
6955
			'/option',
6956
			$minutesRegex,
6957
			array('option' => array('value' => '12', 'selected' => 'selected')),
6958
			'12',
6959
			'/option',
6960
			$minutesRegex,
6961
			'/select',
6962
		);
6963
		$this->assertTags($result, $expected);
6964
 
6965
		$this->Form->request->data['Model']['field'] = '';
6966
		$result = $this->Form->minute('Model.field', array('interval' => 5));
6967
		$expected = array(
6968
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6969
			array('option' => array('value' => '')),
6970
			'/option',
6971
			array('option' => array('value' => '00')),
6972
			'00',
6973
			'/option',
6974
			array('option' => array('value' => '05')),
6975
			'05',
6976
			'/option',
6977
			array('option' => array('value' => '10')),
6978
			'10',
6979
			'/option',
6980
			$minutesRegex,
6981
			'/select',
6982
		);
6983
		$this->assertTags($result, $expected);
6984
 
6985
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
6986
		$result = $this->Form->minute('Model.field', array('interval' => 5));
6987
		$expected = array(
6988
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6989
			array('option' => array('value' => '')),
6990
			'/option',
6991
			array('option' => array('value' => '00')),
6992
			'00',
6993
			'/option',
6994
			array('option' => array('value' => '05')),
6995
			'05',
6996
			'/option',
6997
			array('option' => array('value' => '10', 'selected' => 'selected')),
6998
			'10',
6999
			'/option',
7000
			$minutesRegex,
7001
			'/select',
7002
		);
7003
		$this->assertTags($result, $expected);
7004
 
7005
		$result = $this->Form->minute('Model.field', array('value' => '#invalid#'));
7006
		$expected = array(
7007
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
7008
			array('option' => array('value' => '')),
7009
			'/option',
7010
			array('option' => array('value' => '00')),
7011
			'00',
7012
			'/option',
7013
			array('option' => array('value' => '01')),
7014
			'01',
7015
			'/option',
7016
			array('option' => array('value' => '02')),
7017
			'02',
7018
			'/option',
7019
			$minutesRegex,
7020
			'/select',
7021
		);
7022
		$this->assertTags($result, $expected);
7023
	}
7024
 
7025
/**
7026
 * testHour method
7027
 *
7028
 * @return void
7029
 */
7030
	public function testHour() {
7031
		extract($this->dateRegex);
7032
 
7033
		$result = $this->Form->hour('Model.field', false);
7034
		$expected = array(
7035
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7036
			array('option' => array('value' => '')),
7037
			'/option',
7038
			array('option' => array('value' => '01')),
7039
			'1',
7040
			'/option',
7041
			array('option' => array('value' => '02')),
7042
			'2',
7043
			'/option',
7044
			$hoursRegex,
7045
			'/select',
7046
		);
7047
		$this->assertTags($result, $expected);
7048
 
7049
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
7050
		$result = $this->Form->hour('Model.field', false);
7051
		$expected = array(
7052
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7053
			array('option' => array('value' => '')),
7054
			'/option',
7055
			array('option' => array('value' => '01')),
7056
			'1',
7057
			'/option',
7058
			array('option' => array('value' => '02')),
7059
			'2',
7060
			'/option',
7061
			$hoursRegex,
7062
			array('option' => array('value' => '12', 'selected' => 'selected')),
7063
			'12',
7064
			'/option',
7065
			'/select',
7066
		);
7067
		$this->assertTags($result, $expected);
7068
 
7069
		$this->Form->request->data['Model']['field'] = '';
7070
		$result = $this->Form->hour('Model.field', true, array('value' => '23'));
7071
		$this->assertContains('<option value="23" selected="selected">23</option>', $result);
7072
 
7073
		$result = $this->Form->hour('Model.field', false, array('value' => '23'));
7074
		$this->assertContains('<option value="11" selected="selected">11</option>', $result);
7075
 
7076
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
7077
		$result = $this->Form->hour('Model.field', true);
7078
		$expected = array(
7079
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7080
			array('option' => array('value' => '')),
7081
			'/option',
7082
			array('option' => array('value' => '00', 'selected' => 'selected')),
7083
			'0',
7084
			'/option',
7085
			array('option' => array('value' => '01')),
7086
			'1',
7087
			'/option',
7088
			array('option' => array('value' => '02')),
7089
			'2',
7090
			'/option',
7091
			$hoursRegex,
7092
			'/select',
7093
		);
7094
		$this->assertTags($result, $expected);
7095
 
7096
		unset($this->Form->request->data['Model']['field']);
7097
		$result = $this->Form->hour('Model.field', true, array('value' => 'now'));
7098
		$thisHour = date('H');
7099
		$optValue = date('G');
7100
		$this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
7101
 
7102
		$this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
7103
		$result = $this->Form->hour('Model.field', true);
7104
		$expected = array(
7105
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7106
			array('option' => array('value' => '')),
7107
			'/option',
7108
			array('option' => array('value' => '00')),
7109
			'0',
7110
			'/option',
7111
			array('option' => array('value' => '01', 'selected' => 'selected')),
7112
			'1',
7113
			'/option',
7114
			array('option' => array('value' => '02')),
7115
			'2',
7116
			'/option',
7117
			$hoursRegex,
7118
			'/select',
7119
		);
7120
		$this->assertTags($result, $expected);
7121
 
7122
		$this->Form->request->data['Model']['field'] = '18a';
7123
		$result = $this->Form->hour('Model.field', false);
7124
		$expected = array(
7125
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7126
			array('option' => array('value' => '')),
7127
			'/option',
7128
			array('option' => array('value' => '01')),
7129
			'1',
7130
			'/option',
7131
			array('option' => array('value' => '02')),
7132
			'2',
7133
			'/option',
7134
			$hoursRegex,
7135
			'/select',
7136
		);
7137
		$this->assertTags($result, $expected);
7138
	}
7139
 
7140
/**
7141
 * testYear method
7142
 *
7143
 * @return void
7144
 */
7145
	public function testYear() {
7146
		$result = $this->Form->year('Model.field', 2006, 2007);
7147
		$expected = array(
7148
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
7149
			array('option' => array('value' => '')),
7150
			'/option',
7151
			array('option' => array('value' => '2007')),
7152
			'2007',
7153
			'/option',
7154
			array('option' => array('value' => '2006')),
7155
			'2006',
7156
			'/option',
7157
			'/select',
7158
		);
7159
		$this->assertTags($result, $expected);
7160
 
7161
		$result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
7162
		$expected = array(
7163
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
7164
			array('option' => array('value' => '')),
7165
			'/option',
7166
			array('option' => array('value' => '2006')),
7167
			'2006',
7168
			'/option',
7169
			array('option' => array('value' => '2007')),
7170
			'2007',
7171
			'/option',
7172
			'/select',
7173
		);
7174
		$this->assertTags($result, $expected);
7175
 
7176
		$this->request->data['Contact']['published'] = '';
7177
		$result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
7178
		$expected = array(
7179
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
7180
			array('option' => array('value' => '')),
7181
			'/option',
7182
			array('option' => array('value' => '2007')),
7183
			'2007',
7184
			'/option',
7185
			array('option' => array('value' => '2006')),
7186
			'2006',
7187
			'/option',
7188
			'/select',
7189
		);
7190
		$this->assertTags($result, $expected);
7191
 
7192
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7193
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
7194
		$expected = array(
7195
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7196
			array('option' => array('value' => '2007')),
7197
			'2007',
7198
			'/option',
7199
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7200
			'2006',
7201
			'/option',
7202
			'/select',
7203
		);
7204
		$this->assertTags($result, $expected);
7205
 
7206
		$this->Form->request->data['Contact']['published'] = '';
7207
		$result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
7208
		$expected = array(
7209
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7210
			array('option' => array('value' => '')),
7211
			'/option',
7212
			array('option' => array('value' => '2007')),
7213
			'2007',
7214
			'/option',
7215
			array('option' => array('value' => '2006')),
7216
			'2006',
7217
			'/option',
7218
			'/select',
7219
		);
7220
		$this->assertTags($result, $expected);
7221
 
7222
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7223
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
7224
		$expected = array(
7225
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7226
			array('option' => array('value' => '2007')),
7227
			'2007',
7228
			'/option',
7229
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7230
			'2006',
7231
			'/option',
7232
			'/select',
7233
		);
7234
		$this->assertTags($result, $expected);
7235
 
7236
		$this->Form->request->data['Contact']['published'] = '';
7237
		$result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
7238
		$expected = array(
7239
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7240
			array('option' => array('value' => '')),
7241
			'/option',
7242
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7243
			'2007',
7244
			'/option',
7245
			array('option' => array('value' => '2006')),
7246
			'2006',
7247
			'/option',
7248
			'/select',
7249
		);
7250
		$this->assertTags($result, $expected);
7251
 
7252
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7253
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
7254
		$expected = array(
7255
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7256
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7257
			'2007',
7258
			'/option',
7259
			array('option' => array('value' => '2006')),
7260
			'2006',
7261
			'/option',
7262
			'/select',
7263
		);
7264
		$this->assertTags($result, $expected);
7265
 
7266
		$this->Form->request->data['Contact']['published'] = '';
7267
		$result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
7268
		$expected = array(
7269
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7270
			array('option' => array('value' => '2008')),
7271
			'2008',
7272
			'/option',
7273
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7274
			'2007',
7275
			'/option',
7276
			array('option' => array('value' => '2006')),
7277
			'2006',
7278
			'/option',
7279
			'/select',
7280
		);
7281
		$this->assertTags($result, $expected);
7282
 
7283
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7284
		$result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
7285
		$expected = array(
7286
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7287
			array('option' => array('value' => '2008')),
7288
			'2008',
7289
			'/option',
7290
			array('option' => array('value' => '2007')),
7291
			'2007',
7292
			'/option',
7293
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7294
			'2006',
7295
			'/option',
7296
			'/select',
7297
		);
7298
		$this->assertTags($result, $expected);
7299
 
7300
		$this->Form->request->data = array();
7301
		$this->Form->create('Contact');
7302
		$result = $this->Form->year('published', 2006, 2008, array('empty' => false));
7303
		$expected = array(
7304
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7305
			array('option' => array('value' => '2008')),
7306
			'2008',
7307
			'/option',
7308
			array('option' => array('value' => '2007')),
7309
			'2007',
7310
			'/option',
7311
			array('option' => array('value' => '2006')),
7312
			'2006',
7313
			'/option',
7314
			'/select',
7315
		);
7316
		$this->assertTags($result, $expected);
7317
 
7318
		$result = $this->Form->year('published', array(), array(), array('empty' => false));
7319
		$this->assertContains('data[Contact][published][year]', $result);
7320
 
7321
		$this->Form->request->data['Contact']['published'] = '2014ee';
7322
		$result = $this->Form->year('Contact.published', 2010, 2011);
7323
		$expected = array(
7324
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7325
			array('option' => array('value' => '')),
7326
			'/option',
7327
			array('option' => array('value' => '2011')),
7328
			'2011',
7329
			'/option',
7330
			array('option' => array('value' => '2010')),
7331
			'2010',
7332
			'/option',
7333
			'/select',
7334
		);
7335
		$this->assertTags($result, $expected);
7336
	}
7337
 
7338
/**
7339
 * testYearAutoExpandRange method
7340
 *
7341
 * @return void
7342
 */
7343
	public function testYearAutoExpandRange() {
7344
		$this->Form->request->data['User']['birthday'] = '1930-10-10';
7345
		$result = $this->Form->year('User.birthday');
7346
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7347
 
7348
		$result = $matches[1];
7349
		$expected = range(date('Y') + 20, 1930);
7350
		$this->assertEquals($expected, $result);
7351
 
7352
		$this->Form->request->data['Project']['release'] = '2050-10-10';
7353
		$result = $this->Form->year('Project.release');
7354
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7355
 
7356
		$result = $matches[1];
7357
		$expected = range(2050, date('Y') - 20);
7358
		$this->assertEquals($expected, $result);
7359
 
7360
		$this->Form->request->data['Project']['release'] = '1881-10-10';
7361
		$result = $this->Form->year('Project.release', 1890, 1900);
7362
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7363
 
7364
		$result = $matches[1];
7365
		$expected = range(1900, 1881);
7366
		$this->assertEquals($expected, $result);
7367
	}
7368
 
7369
/**
7370
 * testInputDate method
7371
 *
7372
 * Test various inputs with type date and different dateFormat values.
7373
 * Failing to provide a dateFormat key should not error.
7374
 * It should simply not pre-select any value then.
7375
 *
7376
 * @return void
7377
 */
7378
	public function testInputDate() {
7379
		$this->Form->request->data = array(
7380
			'User' => array(
7381
				'month_year' => array('month' => date('m')),
7382
				'just_year' => array('month' => date('m')),
7383
				'just_month' => array('year' => date('Y')),
7384
				'just_day' => array('month' => date('m')),
7385
			)
7386
		);
7387
		$this->Form->create('User');
7388
		$result = $this->Form->input('month_year',
7389
				array(
7390
					'label' => false,
7391
					'div' => false,
7392
					'type' => 'date',
7393
					'dateFormat' => 'MY',
7394
					'minYear' => 2006,
7395
					'maxYear' => 2008
7396
				)
7397
		);
7398
		$this->assertContains('value="' . date('m') . '" selected="selected"', $result);
7399
		$this->assertNotContains('value="2008" selected="selected"', $result);
7400
 
7401
		$result = $this->Form->input('just_year',
7402
			array(
7403
				'type' => 'date',
7404
				'label' => false,
7405
				'dateFormat' => 'Y',
7406
				'minYear' => date('Y'),
7407
				'maxYear' => date('Y', strtotime('+20 years'))
7408
			)
7409
		);
7410
		$this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
7411
 
7412
		$result = $this->Form->input('just_month',
7413
			array(
7414
				'type' => 'date',
7415
				'label' => false,
7416
				'dateFormat' => 'M',
7417
				'empty' => false,
7418
			)
7419
		);
7420
		$this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
7421
 
7422
		$result = $this->Form->input('just_day',
7423
			array(
7424
				'type' => 'date',
7425
				'label' => false,
7426
				'dateFormat' => 'D',
7427
				'empty' => false,
7428
			)
7429
		);
7430
		$this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
7431
	}
7432
 
7433
/**
7434
 * testInputDate method
7435
 *
7436
 * Test various inputs with type date and different option attributes.
7437
 *
7438
 * @return void
7439
 */
7440
	public function testInputDateOptions() {
7441
		$this->Form->create('User');
7442
 
7443
		$result = $this->Form->input('date',
7444
			array(
7445
				'label' => false,
7446
				'type' => 'day',
7447
				'class' => 'form-control'
7448
			)
7449
		);
7450
		$this->assertContains('class="form-control"', $result);
7451
 
7452
		$result = $this->Form->input('date',
7453
			array(
7454
				'label' => false,
7455
				'type' => 'month',
7456
				'class' => 'form-control'
7457
			)
7458
		);
7459
		$this->assertContains('class="form-control"', $result);
7460
 
7461
		$result = $this->Form->input('date',
7462
			array(
7463
				'label' => false,
7464
				'type' => 'year',
7465
				'class' => 'form-control'
7466
			)
7467
		);
7468
		$this->assertContains('class="form-control"', $result);
7469
 
7470
		$result = $this->Form->input('date',
7471
			array(
7472
				'label' => false,
7473
				'type' => 'hour',
7474
				'class' => 'form-control'
7475
			)
7476
		);
7477
		$this->assertContains('class="form-control"', $result);
7478
	}
7479
 
7480
/**
7481
 * testInputDateMaxYear method
7482
 *
7483
 * Let's say we want to only allow users born from 2006 to 2008 to register
7484
 * This being the first singup page, we still don't have any data
7485
 *
7486
 * @return void
7487
 */
7488
	public function testInputDateMaxYear() {
7489
		$this->Form->request->data = array();
7490
		$this->Form->create('User');
7491
		$result = $this->Form->input('birthday',
7492
				array(
7493
					'label' => false,
7494
					'div' => false,
7495
					'type' => 'date',
7496
					'dateFormat' => 'DMY',
7497
					'minYear' => 2006,
7498
					'maxYear' => 2008
7499
				)
7500
		);
7501
		$this->assertContains('value="2008" selected="selected"', $result);
7502
	}
7503
 
7504
/**
7505
 * testTextArea method
7506
 *
7507
 * @return void
7508
 */
7509
	public function testTextArea() {
7510
		$this->Form->request->data = array('Model' => array('field' => 'some test data'));
7511
		$result = $this->Form->textarea('Model.field');
7512
		$expected = array(
7513
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7514
			'some test data',
7515
			'/textarea',
7516
		);
7517
		$this->assertTags($result, $expected);
7518
 
7519
		$result = $this->Form->textarea('Model.tmp');
7520
		$expected = array(
7521
			'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
7522
			'/textarea',
7523
		);
7524
		$this->assertTags($result, $expected);
7525
 
7526
		$this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
7527
		$result = $this->Form->textarea('Model.field');
7528
		$expected = array(
7529
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7530
			htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
7531
			'/textarea',
7532
		);
7533
		$this->assertTags($result, $expected);
7534
 
7535
		$this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
7536
		$result = $this->Form->textarea('Model.field', array('escape' => false));
7537
		$expected = array(
7538
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7539
			'some <strong>test</strong> data with <a href="#">HTML</a> chars',
7540
			'/textarea',
7541
		);
7542
		$this->assertTags($result, $expected);
7543
 
7544
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
7545
		$result = $this->Form->textarea('Model.0.OtherModel.field');
7546
		$expected = array(
7547
			'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
7548
			'/textarea'
7549
		);
7550
		$this->assertTags($result, $expected);
7551
	}
7552
 
7553
/**
7554
 * Test textareas maxlength reading from schema.
7555
 *
7556
 * @return void
7557
 */
7558
	public function testTextAreaMaxLength() {
7559
		$result = $this->Form->input('UserForm.other', array('type' => 'textarea'));
7560
		$expected = array(
7561
			'div' => array('class' => 'input textarea'),
7562
				'label' => array('for' => 'UserFormOther'),
7563
					'Other',
7564
				'/label',
7565
				'textarea' => array('name' => 'data[UserForm][other]', 'cols' => '30', 'rows' => '6', 'id' => 'UserFormOther'),
7566
				'/textarea',
7567
			'/div'
7568
		);
7569
		$this->assertTags($result, $expected);
7570
 
7571
		$result = $this->Form->input('UserForm.stuff', array('type' => 'textarea'));
7572
		$expected = array(
7573
			'div' => array('class' => 'input textarea'),
7574
				'label' => array('for' => 'UserFormStuff'),
7575
					'Stuff',
7576
				'/label',
7577
				'textarea' => array('name' => 'data[UserForm][stuff]', 'maxlength' => 10, 'cols' => '30', 'rows' => '6', 'id' => 'UserFormStuff'),
7578
				'/textarea',
7579
			'/div'
7580
		);
7581
		$this->assertTags($result, $expected);
7582
	}
7583
 
7584
/**
7585
 * testTextAreaWithStupidCharacters method
7586
 *
7587
 * test text area with non-ascii characters
7588
 *
7589
 * @return void
7590
 */
7591
	public function testTextAreaWithStupidCharacters() {
7592
		$this->loadFixtures('Post');
7593
		$result = $this->Form->input('Post.content', array(
7594
			'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
7595
		));
7596
		$expected = array(
7597
			'div' => array('class' => 'input textarea'),
7598
				'label' => array('for' => 'PostContent'),
7599
					'Current Text',
7600
				'/label',
7601
				'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
7602
				'GREAT®',
7603
				'/textarea',
7604
			'/div'
7605
		);
7606
		$this->assertTags($result, $expected);
7607
	}
7608
 
7609
/**
7610
 * testHiddenField method
7611
 *
7612
 * @return void
7613
 */
7614
	public function testHiddenField() {
7615
		$Contact = ClassRegistry::getObject('Contact');
7616
		$Contact->validationErrors['field'] = 1;
7617
		$this->Form->request->data['Contact']['field'] = 'test';
7618
		$result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
7619
		$this->assertTags($result, array(
7620
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
7621
		);
7622
	}
7623
 
7624
/**
7625
 * testFileUploadField method
7626
 *
7627
 * @return void
7628
 */
7629
	public function testFileUploadField() {
7630
		$result = $this->Form->file('Model.upload');
7631
		$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
7632
 
7633
		$this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
7634
		$result = $this->Form->input('Model.upload', array('type' => 'file'));
7635
		$expected = array(
7636
			'div' => array('class' => 'input file'),
7637
			'label' => array('for' => 'ModelUpload'),
7638
			'Upload',
7639
			'/label',
7640
			'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
7641
			'/div'
7642
		);
7643
		$this->assertTags($result, $expected);
7644
 
7645
		$this->Form->request->data['Model']['upload'] = 'no data should be set in value';
7646
		$result = $this->Form->file('Model.upload');
7647
		$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
7648
	}
7649
 
7650
/**
7651
 * test File upload input on a model not used in create();
7652
 *
7653
 * @return void
7654
 */
7655
	public function testFileUploadOnOtherModel() {
7656
		$this->Form->create('ValidateUser', array('type' => 'file'));
7657
		$result = $this->Form->file('ValidateProfile.city');
7658
		$expected = array(
7659
			'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
7660
		);
7661
		$this->assertTags($result, $expected);
7662
	}
7663
 
7664
/**
7665
 * testButton method
7666
 *
7667
 * @return void
7668
 */
7669
	public function testButton() {
7670
		$result = $this->Form->button('Hi');
7671
		$this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
7672
 
7673
		$result = $this->Form->button('Clear Form >', array('type' => 'reset'));
7674
		$this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
7675
 
7676
		$result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
7677
		$this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
7678
 
7679
		$result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
7680
		$this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
7681
 
7682
		$result = $this->Form->button('No type', array('type' => false));
7683
		$this->assertTags($result, array('button' => array(), 'No type', '/button'));
7684
 
7685
		$result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
7686
		$this->assertNotRegExp('/\&039/', $result);
7687
	}
7688
 
7689
/**
7690
 * Test that button() makes unlocked fields by default.
7691
 *
7692
 * @return void
7693
 */
7694
	public function testButtonUnlockedByDefault() {
7695
		$this->Form->request->params['_Token']['key'] = 'secured';
7696
		$this->Form->button('Save', array('name' => 'save'));
7697
		$this->Form->button('Clear');
7698
 
7699
		$result = $this->Form->unlockField();
7700
		$this->assertEquals(array('save'), $result);
7701
	}
7702
 
7703
/**
7704
 * testPostButton method
7705
 *
7706
 * @return void
7707
 */
7708
	public function testPostButton() {
7709
		$result = $this->Form->postButton('Hi', '/controller/action');
7710
		$this->assertTags($result, array(
7711
			'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
7712
			'div' => array('style' => 'display:none;'),
7713
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7714
			'/div',
7715
			'button' => array('type' => 'submit'),
7716
			'Hi',
7717
			'/button',
7718
			'/form'
7719
		));
7720
 
7721
		$result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
7722
		$this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
7723
	}
7724
 
7725
/**
7726
 * Test using postButton with N dimensional data.
7727
 *
7728
 * @return void
7729
 */
7730
	public function testPostButtonNestedData() {
7731
		$data = array(
7732
			'one' => array(
7733
				'two' => array(
7734
					3, 4, 5
7735
				)
7736
			)
7737
		);
7738
		$result = $this->Form->postButton('Send', '/', array('data' => $data));
7739
		$this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
7740
		$this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
7741
		$this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
7742
	}
7743
 
7744
/**
7745
 * Test that postButton adds _Token fields.
7746
 *
7747
 * @return void
7748
 */
7749
	public function testSecurePostButton() {
7750
		$this->Form->request->params['_Token'] = array('key' => 'testkey');
7751
 
7752
		$result = $this->Form->postButton('Delete', '/posts/delete/1');
7753
		$expected = array(
7754
			'form' => array(
7755
				'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
7756
			),
7757
			array('div' => array('style' => 'display:none;')),
7758
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7759
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
7760
			'/div',
7761
			'button' => array('type' => 'submit'),
7762
			'Delete',
7763
			'/button',
7764
			array('div' => array('style' => 'display:none;')),
7765
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
7766
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
7767
			'/div',
7768
			'/form',
7769
		);
7770
		$this->assertTags($result, $expected);
7771
	}
7772
 
7773
/**
7774
 * testPostLink method
7775
 *
7776
 * @return void
7777
 */
7778
	public function testPostLink() {
7779
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7780
		$this->assertTags($result, array(
7781
			'form' => array(
7782
				'method' => 'post', 'action' => '/posts/delete/1',
7783
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7784
			),
7785
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7786
			'/form',
7787
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7788
			'Delete',
7789
			'/a'
7790
		));
7791
 
7792
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
7793
		$this->assertTags($result, array(
7794
			'form' => array(
7795
				'method' => 'post', 'action' => '/posts/delete/1',
7796
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7797
			),
7798
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
7799
			'/form',
7800
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7801
			'Delete',
7802
			'/a'
7803
		));
7804
 
7805
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
7806
		$this->assertTags($result, array(
7807
			'form' => array(
7808
				'method' => 'post', 'action' => '/posts/delete/1',
7809
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7810
			),
7811
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7812
			'/form',
7813
			'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
7814
			'Delete',
7815
			'/a'
7816
		));
7817
 
7818
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
7819
		$this->assertTags($result, array(
7820
			'form' => array(
7821
				'method' => 'post', 'action' => '/posts/delete/1',
7822
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7823
			),
7824
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7825
			'/form',
7826
			'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
7827
			'Delete',
7828
			'/a'
7829
		));
7830
 
7831
		$result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
7832
		$this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
7833
 
7834
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
7835
		$this->assertTags($result, array(
7836
			'form' => array(
7837
				'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
7838
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7839
			),
7840
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7841
			'/form',
7842
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7843
			'Delete',
7844
			'/a'
7845
		));
7846
 
7847
		$result = $this->Form->postLink(
7848
			'',
7849
			array('controller' => 'items', 'action' => 'delete', 10),
7850
			array('class' => 'btn btn-danger', 'escape' => false),
7851
			'Confirm thing'
7852
		);
7853
		$this->assertTags($result, array(
7854
			'form' => array(
7855
				'method' => 'post', 'action' => '/items/delete/10',
7856
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7857
			),
7858
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7859
			'/form',
7860
			'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
7861
			'/a'
7862
		));
7863
	}
7864
 
7865
/**
7866
 * Test that security hashes for postLink include the url.
7867
 *
7868
 * @return void
7869
 */
7870
	public function testPostLinkSecurityHash() {
7871
		$hash = Security::hash(
7872
			'/posts/delete/1' .
7873
			serialize(array()) .
7874
			'' .
7875
			Configure::read('Security.salt')
7876
		);
7877
		$hash .= '%3A';
7878
		$this->Form->request->params['_Token']['key'] = 'test';
7879
 
7880
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7881
		$this->assertTags($result, array(
7882
			'form' => array(
7883
				'method' => 'post', 'action' => '/posts/delete/1',
7884
				'name', 'id', 'style' => 'display:none;'
7885
			),
7886
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7887
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'test', 'id')),
7888
			'div' => array('style' => 'display:none;'),
7889
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => $hash, 'id')),
7890
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id')),
7891
			'/div',
7892
			'/form',
7893
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7894
			'Delete',
7895
			'/a'
7896
		));
7897
	}
7898
 
7899
/**
7900
 * Test using postLink with N dimensional data.
7901
 *
7902
 * @return void
7903
 */
7904
	public function testPostLinkNestedData() {
7905
		$data = array(
7906
			'one' => array(
7907
				'two' => array(
7908
					3, 4, 5
7909
				)
7910
			)
7911
		);
7912
		$result = $this->Form->postLink('Send', '/', array('data' => $data));
7913
		$this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
7914
		$this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
7915
		$this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
7916
	}
7917
 
7918
/**
7919
 * test creating postLinks after a GET form.
7920
 *
7921
 * @return void
7922
 */
7923
	public function testPostLinkAfterGetForm() {
7924
		$this->Form->request->params['_Token']['key'] = 'testkey';
7925
		$this->Form->create('User', array('type' => 'get'));
7926
		$this->Form->end();
7927
 
7928
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7929
		$this->assertTags($result, array(
7930
			'form' => array(
7931
				'method' => 'post', 'action' => '/posts/delete/1',
7932
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7933
			),
7934
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7935
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
7936
			'div' => array('style' => 'display:none;'),
7937
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
7938
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
7939
			'/div',
7940
			'/form',
7941
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7942
			'Delete',
7943
			'/a'
7944
		));
7945
	}
7946
 
7947
/**
7948
 * Test that postLink adds _Token fields.
7949
 *
7950
 * @return void
7951
 */
7952
	public function testSecurePostLink() {
7953
		$this->Form->request->params['_Token'] = array('key' => 'testkey');
7954
 
7955
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7956
		$expected = array(
7957
			'form' => array(
7958
				'method' => 'post', 'action' => '/posts/delete/1',
7959
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7960
			),
7961
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7962
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
7963
			'div' => array('style' => 'display:none;'),
7964
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
7965
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
7966
			'/div',
7967
			'/form',
7968
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7969
			'Delete',
7970
			'/a'
7971
		);
7972
		$this->assertTags($result, $expected);
7973
	}
7974
 
7975
/**
7976
 * Test that postLink adds form tags to view block
7977
 *
7978
 * @return void
7979
 */
7980
	public function testPostLinkFormBuffer() {
7981
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
7982
		$this->assertTags($result, array(
7983
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7984
			'Delete',
7985
			'/a'
7986
		));
7987
 
7988
		$result = $this->View->fetch('postLink');
7989
		$this->assertTags($result, array(
7990
			'form' => array(
7991
				'method' => 'post', 'action' => '/posts/delete/1',
7992
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7993
			),
7994
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7995
			'/form'
7996
		));
7997
 
7998
		$result = $this->Form->postLink('Delete', '/posts/delete/2',
7999
			array('inline' => false, 'method' => 'DELETE')
8000
		);
8001
		$this->assertTags($result, array(
8002
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8003
			'Delete',
8004
			'/a'
8005
		));
8006
 
8007
		$result = $this->View->fetch('postLink');
8008
		$this->assertTags($result, array(
8009
			'form' => array(
8010
				'method' => 'post', 'action' => '/posts/delete/1',
8011
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8012
			),
8013
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8014
			'/form',
8015
			array(
8016
				'form' => array(
8017
					'method' => 'post', 'action' => '/posts/delete/2',
8018
					'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8019
				),
8020
			),
8021
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
8022
			'/form'
8023
		));
8024
 
8025
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
8026
		$this->assertTags($result, array(
8027
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8028
			'Delete',
8029
			'/a'
8030
		));
8031
 
8032
		$result = $this->View->fetch('foobar');
8033
		$this->assertTags($result, array(
8034
			'form' => array(
8035
				'method' => 'post', 'action' => '/posts/delete/1',
8036
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8037
			),
8038
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8039
			'/form'
8040
		));
8041
	}
8042
 
8043
/**
8044
 * testSubmitButton method
8045
 *
8046
 * @return void
8047
 */
8048
	public function testSubmitButton() {
8049
		$result = $this->Form->submit('');
8050
		$expected = array(
8051
			'div' => array('class' => 'submit'),
8052
			'input' => array('type' => 'submit', 'value' => ''),
8053
			'/div'
8054
		);
8055
		$this->assertTags($result, $expected);
8056
 
8057
		$result = $this->Form->submit('Test Submit');
8058
		$expected = array(
8059
			'div' => array('class' => 'submit'),
8060
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
8061
			'/div'
8062
		);
8063
		$this->assertTags($result, $expected);
8064
 
8065
		$result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
8066
		$expected = array(
8067
			'span' => array('class' => 'submit'),
8068
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
8069
			'/span'
8070
		);
8071
		$this->assertTags($result, $expected);
8072
 
8073
		$result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
8074
		$expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
8075
		$this->assertTags($result, $expected);
8076
 
8077
		$result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
8078
		$expected = array(
8079
			'div' => array('class' => 'submit', 'id' => 'SaveButton'),
8080
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
8081
			'/div'
8082
		);
8083
		$this->assertTags($result, $expected);
8084
 
8085
		$result = $this->Form->submit('Next >');
8086
		$expected = array(
8087
			'div' => array('class' => 'submit'),
8088
			'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
8089
			'/div'
8090
		);
8091
		$this->assertTags($result, $expected);
8092
 
8093
		$result = $this->Form->submit('Next >', array('escape' => false));
8094
		$expected = array(
8095
			'div' => array('class' => 'submit'),
8096
			'input' => array('type' => 'submit', 'value' => 'Next >'),
8097
			'/div'
8098
		);
8099
		$this->assertTags($result, $expected);
8100
 
8101
		$result = $this->Form->submit('Reset!', array('type' => 'reset'));
8102
		$expected = array(
8103
			'div' => array('class' => 'submit'),
8104
			'input' => array('type' => 'reset', 'value' => 'Reset!'),
8105
			'/div'
8106
		);
8107
		$this->assertTags($result, $expected);
8108
 
8109
		$before = '--before--';
8110
		$after = '--after--';
8111
		$result = $this->Form->submit('Test', array('before' => $before));
8112
		$expected = array(
8113
			'div' => array('class' => 'submit'),
8114
			'--before--',
8115
			'input' => array('type' => 'submit', 'value' => 'Test'),
8116
			'/div'
8117
		);
8118
		$this->assertTags($result, $expected);
8119
 
8120
		$result = $this->Form->submit('Test', array('after' => $after));
8121
		$expected = array(
8122
			'div' => array('class' => 'submit'),
8123
			'input' => array('type' => 'submit', 'value' => 'Test'),
8124
			'--after--',
8125
			'/div'
8126
		);
8127
		$this->assertTags($result, $expected);
8128
 
8129
		$result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
8130
		$expected = array(
8131
			'div' => array('class' => 'submit'),
8132
			'--before--',
8133
			'input' => array('type' => 'submit', 'value' => 'Test'),
8134
			'--after--',
8135
			'/div'
8136
		);
8137
		$this->assertTags($result, $expected);
8138
	}
8139
 
8140
/**
8141
 * test image submit types.
8142
 *
8143
 * @return void
8144
 */
8145
	public function testSubmitImage() {
8146
		$result = $this->Form->submit('http://example.com/cake.power.gif');
8147
		$expected = array(
8148
			'div' => array('class' => 'submit'),
8149
			'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
8150
			'/div'
8151
		);
8152
		$this->assertTags($result, $expected);
8153
 
8154
		$result = $this->Form->submit('/relative/cake.power.gif');
8155
		$expected = array(
8156
			'div' => array('class' => 'submit'),
8157
			'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
8158
			'/div'
8159
		);
8160
		$this->assertTags($result, $expected);
8161
 
8162
		$result = $this->Form->submit('cake.power.gif');
8163
		$expected = array(
8164
			'div' => array('class' => 'submit'),
8165
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8166
			'/div'
8167
		);
8168
		$this->assertTags($result, $expected);
8169
 
8170
		$result = $this->Form->submit('Not.an.image');
8171
		$expected = array(
8172
			'div' => array('class' => 'submit'),
8173
			'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
8174
			'/div'
8175
		);
8176
		$this->assertTags($result, $expected);
8177
 
8178
		$after = '--after--';
8179
		$before = '--before--';
8180
		$result = $this->Form->submit('cake.power.gif', array('after' => $after));
8181
		$expected = array(
8182
			'div' => array('class' => 'submit'),
8183
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8184
			'--after--',
8185
			'/div'
8186
		);
8187
		$this->assertTags($result, $expected);
8188
 
8189
		$result = $this->Form->submit('cake.power.gif', array('before' => $before));
8190
		$expected = array(
8191
			'div' => array('class' => 'submit'),
8192
			'--before--',
8193
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8194
			'/div'
8195
		);
8196
		$this->assertTags($result, $expected);
8197
 
8198
		$result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
8199
		$expected = array(
8200
			'div' => array('class' => 'submit'),
8201
			'--before--',
8202
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8203
			'--after--',
8204
			'/div'
8205
		);
8206
		$this->assertTags($result, $expected);
8207
 
8208
		$result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
8209
		$expected = array(
8210
			'div' => array('class' => 'submit'),
8211
			'--before--',
8212
			'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
8213
			'--after--',
8214
			'/div'
8215
		);
8216
		$this->assertTags($result, $expected);
8217
	}
8218
 
8219
/**
8220
 * Submit buttons should be unlocked by default as there could be multiples, and only one will
8221
 * be submitted at a time.
8222
 *
8223
 * @return void
8224
 */
8225
	public function testSubmitUnlockedByDefault() {
8226
		$this->Form->request->params['_Token']['key'] = 'secured';
8227
		$this->Form->submit('Go go');
8228
		$this->Form->submit('Save', array('name' => 'save'));
8229
 
8230
		$result = $this->Form->unlockField();
8231
		$this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
8232
	}
8233
 
8234
/**
8235
 * Test submit image with timestamps.
8236
 *
8237
 * @return void
8238
 */
8239
	public function testSubmitImageTimestamp() {
8240
		Configure::write('Asset.timestamp', 'force');
8241
 
8242
		$result = $this->Form->submit('cake.power.gif');
8243
		$expected = array(
8244
			'div' => array('class' => 'submit'),
8245
			'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
8246
			'/div'
8247
		);
8248
		$this->assertTags($result, $expected);
8249
	}
8250
 
8251
/**
8252
 * test the create() method
8253
 *
8254
 * @return void
8255
 */
8256
	public function testCreate() {
8257
		$result = $this->Form->create('Contact');
8258
		$encoding = strtolower(Configure::read('App.encoding'));
8259
		$expected = array(
8260
			'form' => array(
8261
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8262
				'accept-charset' => $encoding
8263
			),
8264
			'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
8265
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8266
			'/div'
8267
		);
8268
		$this->assertTags($result, $expected);
8269
 
8270
		$result = $this->Form->create('Contact', array('type' => 'GET'));
8271
		$expected = array('form' => array(
8272
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8273
			'accept-charset' => $encoding
8274
		));
8275
		$this->assertTags($result, $expected);
8276
 
8277
		$result = $this->Form->create('Contact', array('type' => 'get'));
8278
		$expected = array('form' => array(
8279
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8280
			'accept-charset' => $encoding
8281
		));
8282
		$this->assertTags($result, $expected);
8283
 
8284
		$result = $this->Form->create('Contact', array('type' => 'put'));
8285
		$expected = array(
8286
			'form' => array(
8287
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8288
				'accept-charset' => $encoding
8289
			),
8290
			'div' => array('style' => 'display:none;'),
8291
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8292
			'/div'
8293
		);
8294
		$this->assertTags($result, $expected);
8295
 
8296
		$result = $this->Form->create('Contact', array('type' => 'file'));
8297
		$expected = array(
8298
			'form' => array(
8299
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8300
				'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
8301
			),
8302
			'div' => array('style' => 'display:none;'),
8303
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8304
			'/div'
8305
		);
8306
		$this->assertTags($result, $expected);
8307
 
8308
		$this->Form->request->data['Contact']['id'] = 1;
8309
		$this->Form->request->here = '/contacts/edit/1';
8310
		$this->Form->request['action'] = 'edit';
8311
		$result = $this->Form->create('Contact');
8312
		$expected = array(
8313
			'form' => array(
8314
				'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
8315
				'accept-charset' => $encoding
8316
			),
8317
			'div' => array('style' => 'display:none;'),
8318
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8319
			'/div'
8320
		);
8321
		$this->assertTags($result, $expected);
8322
 
8323
		$this->Form->request->data['Contact']['id'] = 1;
8324
		$this->Form->request->here = '/contacts/edit/1';
8325
		$this->Form->request['action'] = 'edit';
8326
		$result = $this->Form->create('Contact', array('type' => 'file'));
8327
		$expected = array(
8328
			'form' => array(
8329
				'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
8330
				'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
8331
			),
8332
			'div' => array('style' => 'display:none;'),
8333
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8334
			'/div'
8335
		);
8336
		$this->assertTags($result, $expected);
8337
 
8338
		$this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
8339
		$result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
8340
		$expected = array(
8341
			'form' => array(
8342
				'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
8343
				'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
8344
			),
8345
			'div' => array('style' => 'display:none;'),
8346
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8347
			'/div'
8348
		);
8349
		$this->assertTags($result, $expected);
8350
 
8351
		$result = $this->Form->create('Contact', array('id' => 'TestId'));
8352
		$expected = array(
8353
			'form' => array(
8354
				'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
8355
				'accept-charset' => $encoding
8356
			),
8357
			'div' => array('style' => 'display:none;'),
8358
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8359
			'/div'
8360
		);
8361
		$this->assertTags($result, $expected);
8362
 
8363
		$this->Form->request['action'] = 'add';
8364
		$result = $this->Form->create('User', array('url' => array('action' => 'login')));
8365
		$expected = array(
8366
			'form' => array(
8367
				'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
8368
				'accept-charset' => $encoding
8369
			),
8370
			'div' => array('style' => 'display:none;'),
8371
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8372
			'/div'
8373
		);
8374
		$this->assertTags($result, $expected);
8375
 
8376
		$result = $this->Form->create('User', array('action' => 'login'));
8377
		$expected = array(
8378
			'form' => array(
8379
				'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
8380
				'accept-charset' => $encoding
8381
			),
8382
			'div' => array('style' => 'display:none;'),
8383
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8384
			'/div'
8385
		);
8386
		$this->assertTags($result, $expected);
8387
 
8388
		$result = $this->Form->create('User', array('url' => '/users/login'));
8389
		$expected = array(
8390
			'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
8391
			'div' => array('style' => 'display:none;'),
8392
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8393
			'/div'
8394
		);
8395
		$this->assertTags($result, $expected);
8396
 
8397
		$this->Form->request['controller'] = 'pages';
8398
		$result = $this->Form->create('User', array('action' => 'signup'));
8399
		$expected = array(
8400
			'form' => array(
8401
				'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
8402
				'accept-charset' => $encoding
8403
			),
8404
			'div' => array('style' => 'display:none;'),
8405
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8406
			'/div'
8407
		);
8408
		$this->assertTags($result, $expected);
8409
 
8410
		$this->Form->request->data = array();
8411
		$this->Form->request['controller'] = 'contacts';
8412
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8413
		$result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
8414
		$expected = array(
8415
			'form' => array(
8416
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
8417
				'accept-charset' => 'utf-8'
8418
			),
8419
			'div' => array('style' => 'display:none;'),
8420
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8421
			'/div'
8422
		);
8423
		$this->assertTags($result, $expected);
8424
	}
8425
 
8426
/**
8427
 * Test the onsubmit option for create()
8428
 *
8429
 * @return void
8430
 */
8431
	public function testCreateOnSubmit() {
8432
		$this->Form->request->data = array();
8433
		$this->Form->request['controller'] = 'contacts';
8434
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8435
		$result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
8436
		$expected = array(
8437
			'form' => array(
8438
				'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
8439
				'accept-charset' => 'utf-8'
8440
			),
8441
			'div' => array('style' => 'display:none;'),
8442
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8443
			'/div'
8444
		);
8445
		$this->assertTags($result, $expected);
8446
 
8447
		$this->Form->request->data = array();
8448
		$this->Form->request['controller'] = 'contacts';
8449
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8450
		$result = $this->Form->create(array(
8451
			'url' => array('action' => 'index', 'param'),
8452
			'default' => false,
8453
			'onsubmit' => 'someFunction();'
8454
		));
8455
 
8456
		$expected = array(
8457
			'form' => array(
8458
				'id' => 'ContactAddForm', 'method' => 'post',
8459
				'onsubmit' => 'someFunction();event.returnValue = false; return false;',
8460
				'action' => '/contacts/index/param',
8461
				'accept-charset' => 'utf-8'
8462
			),
8463
			'div' => array('style' => 'display:none;'),
8464
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8465
			'/div'
8466
		);
8467
		$this->assertTags($result, $expected);
8468
	}
8469
 
8470
/**
8471
 * test create() with automatic url generation
8472
 *
8473
 * @return void
8474
 */
8475
	public function testCreateAutoUrl() {
8476
		Router::setRequestInfo(array(array(), array('base' => '/base_url')));
8477
		$this->Form->request->here = '/base_url/contacts/add/Contact:1';
8478
		$this->Form->request->base = '/base_url';
8479
		$result = $this->Form->create('Contact');
8480
		$expected = array(
8481
			'form' => array(
8482
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
8483
				'accept-charset' => 'utf-8'
8484
			),
8485
			'div' => array('style' => 'display:none;'),
8486
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8487
			'/div'
8488
		);
8489
		$this->assertTags($result, $expected);
8490
 
8491
		$this->Form->request['action'] = 'delete';
8492
		$this->Form->request->here = '/base_url/contacts/delete/10/User:42';
8493
		$this->Form->request->base = '/base_url';
8494
		$result = $this->Form->create('Contact');
8495
		$expected = array(
8496
			'form' => array(
8497
				'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
8498
				'accept-charset' => 'utf-8'
8499
			),
8500
			'div' => array('style' => 'display:none;'),
8501
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8502
			'/div'
8503
		);
8504
		$this->assertTags($result, $expected);
8505
	}
8506
 
8507
/**
8508
 * test create() with a custom route
8509
 *
8510
 * @return void
8511
 */
8512
	public function testCreateCustomRoute() {
8513
		Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
8514
		$encoding = strtolower(Configure::read('App.encoding'));
8515
 
8516
		$result = $this->Form->create('User', array('action' => 'login'));
8517
		$expected = array(
8518
			'form' => array(
8519
				'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
8520
				'accept-charset' => $encoding
8521
			),
8522
			'div' => array('style' => 'display:none;'),
8523
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8524
			'/div'
8525
		);
8526
		$this->assertTags($result, $expected);
8527
	}
8528
 
8529
/**
8530
 * test that inputDefaults are stored and used.
8531
 *
8532
 * @return void
8533
 */
8534
	public function testCreateWithInputDefaults() {
8535
		$this->Form->create('User', array(
8536
			'inputDefaults' => array(
8537
				'div' => false,
8538
				'label' => false,
8539
				'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
8540
				'format' => array('before', 'label', 'between', 'input', 'after', 'error')
8541
			)
8542
		));
8543
		$result = $this->Form->input('username');
8544
		$expected = array(
8545
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
8546
		);
8547
		$this->assertTags($result, $expected);
8548
 
8549
		$result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
8550
		$expected = array(
8551
			'div' => array('class' => 'input text'),
8552
			'label' => array('for' => 'UserUsername'), 'username', '/label',
8553
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8554
			'/div'
8555
		);
8556
		$this->assertTags($result, $expected);
8557
 
8558
		$result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
8559
		$expected = array(
8560
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8561
			'label' => array('for' => 'UserUsername'), 'Username', '/label',
8562
		);
8563
		$this->assertTags($result, $expected);
8564
 
8565
		$this->Form->create('User', array(
8566
			'inputDefaults' => array(
8567
				'div' => false,
8568
				'label' => array('class' => 'nice', 'for' => 'changed'),
8569
			)
8570
		));
8571
		$result = $this->Form->input('username', array('div' => true));
8572
		$expected = array(
8573
			'div' => array('class' => 'input text'),
8574
			'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
8575
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8576
			'/div'
8577
		);
8578
		$this->assertTags($result, $expected);
8579
	}
8580
 
8581
/**
8582
 * test automatic accept-charset overriding
8583
 *
8584
 * @return void
8585
 */
8586
	public function testCreateWithAcceptCharset() {
8587
		$result = $this->Form->create('UserForm', array(
8588
				'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
8589
			)
8590
		);
8591
		$expected = array(
8592
			'form' => array(
8593
				'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
8594
				'accept-charset' => 'iso-8859-1'
8595
			),
8596
			'div' => array('style' => 'display:none;'),
8597
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8598
			'/div'
8599
		);
8600
		$this->assertTags($result, $expected);
8601
	}
8602
 
8603
/**
8604
 * Test base form URL when url param is passed with multiple parameters (&)
8605
 *
8606
 * @return void
8607
 */
8608
	public function testCreateQuerystringrequest() {
8609
		$encoding = strtolower(Configure::read('App.encoding'));
8610
		$result = $this->Form->create('Contact', array(
8611
			'type' => 'post',
8612
			'escape' => false,
8613
			'url' => array(
8614
				'controller' => 'controller',
8615
				'action' => 'action',
8616
				'?' => array('param1' => 'value1', 'param2' => 'value2')
8617
			)
8618
		));
8619
		$expected = array(
8620
			'form' => array(
8621
				'id' => 'ContactAddForm',
8622
				'method' => 'post',
8623
				'action' => '/controller/action?param1=value1&amp;param2=value2',
8624
				'accept-charset' => $encoding
8625
			),
8626
			'div' => array('style' => 'display:none;'),
8627
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8628
			'/div'
8629
		);
8630
		$this->assertTags($result, $expected);
8631
 
8632
		$result = $this->Form->create('Contact', array(
8633
			'type' => 'post',
8634
			'url' => array(
8635
				'controller' => 'controller',
8636
				'action' => 'action',
8637
				'?' => array('param1' => 'value1', 'param2' => 'value2')
8638
			)
8639
		));
8640
		$expected = array(
8641
			'form' => array(
8642
				'id' => 'ContactAddForm',
8643
				'method' => 'post',
8644
				'action' => '/controller/action?param1=value1&amp;param2=value2',
8645
				'accept-charset' => $encoding
8646
			),
8647
			'div' => array('style' => 'display:none;'),
8648
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8649
			'/div'
8650
		);
8651
		$this->assertTags($result, $expected);
8652
	}
8653
 
8654
/**
8655
 * test that create() doesn't cause errors by multiple id's being in the primary key
8656
 * as could happen with multiple select or checkboxes.
8657
 *
8658
 * @return void
8659
 */
8660
	public function testCreateWithMultipleIdInData() {
8661
		$encoding = strtolower(Configure::read('App.encoding'));
8662
 
8663
		$this->Form->request->data['Contact']['id'] = array(1, 2);
8664
		$result = $this->Form->create('Contact');
8665
		$expected = array(
8666
			'form' => array(
8667
				'id' => 'ContactAddForm',
8668
				'method' => 'post',
8669
				'action' => '/contacts/add',
8670
				'accept-charset' => $encoding
8671
			),
8672
			'div' => array('style' => 'display:none;'),
8673
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8674
			'/div'
8675
		);
8676
		$this->assertTags($result, $expected);
8677
	}
8678
 
8679
/**
8680
 * test that create() doesn't add in extra passed params.
8681
 *
8682
 * @return void
8683
 */
8684
	public function testCreatePassedArgs() {
8685
		$encoding = strtolower(Configure::read('App.encoding'));
8686
		$this->Form->request->data['Contact']['id'] = 1;
8687
		$result = $this->Form->create('Contact', array(
8688
			'type' => 'post',
8689
			'escape' => false,
8690
			'url' => array(
8691
				'action' => 'edit',
8692
				'0',
8693
				'myparam'
8694
			)
8695
		));
8696
		$expected = array(
8697
			'form' => array(
8698
				'id' => 'ContactAddForm',
8699
				'method' => 'post',
8700
				'action' => '/contacts/edit/0/myparam',
8701
				'accept-charset' => $encoding
8702
			),
8703
			'div' => array('style' => 'display:none;'),
8704
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8705
			'/div'
8706
		);
8707
		$this->assertTags($result, $expected);
8708
	}
8709
 
8710
/**
8711
 * test that create() works without raising errors with a Mock Model
8712
 *
8713
 * @return void
8714
 */
8715
	public function testCreateNoErrorsWithMockModel() {
8716
		$encoding = strtolower(Configure::read('App.encoding'));
8717
		$ContactMock = $this->getMockBuilder('Contact')
8718
			->disableOriginalConstructor()
8719
			->getMock();
8720
		ClassRegistry::removeObject('Contact');
8721
		ClassRegistry::addObject('Contact', $ContactMock);
8722
		$result = $this->Form->create('Contact', array('type' => 'GET'));
8723
		$expected = array('form' => array(
8724
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8725
			'accept-charset' => $encoding
8726
		));
8727
		$this->assertTags($result, $expected);
8728
	}
8729
 
8730
/**
8731
 * test creating a get form, and get form inputs.
8732
 *
8733
 * @return void
8734
 */
8735
	public function testGetFormCreate() {
8736
		$encoding = strtolower(Configure::read('App.encoding'));
8737
		$result = $this->Form->create('Contact', array('type' => 'get'));
8738
		$this->assertTags($result, array('form' => array(
8739
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8740
			'accept-charset' => $encoding
8741
		)));
8742
 
8743
		$result = $this->Form->text('Contact.name');
8744
		$this->assertTags($result, array('input' => array(
8745
			'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
8746
		)));
8747
 
8748
		$result = $this->Form->password('password');
8749
		$this->assertTags($result, array('input' => array(
8750
			'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
8751
		)));
8752
		$this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
8753
 
8754
		$result = $this->Form->text('user_form');
8755
		$this->assertTags($result, array('input' => array(
8756
			'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
8757
		)));
8758
	}
8759
 
8760
/**
8761
 * test get form, and inputs when the model param is false
8762
 *
8763
 * @return void
8764
 */
8765
	public function testGetFormWithFalseModel() {
8766
		$encoding = strtolower(Configure::read('App.encoding'));
8767
		$this->Form->request['controller'] = 'contact_test';
8768
		$result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
8769
 
8770
		$expected = array('form' => array(
8771
			'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
8772
			'accept-charset' => $encoding
8773
		));
8774
		$this->assertTags($result, $expected);
8775
 
8776
		$result = $this->Form->text('reason');
8777
		$expected = array(
8778
			'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
8779
		);
8780
		$this->assertTags($result, $expected);
8781
	}
8782
 
8783
/**
8784
 * test that datetime() works with GET style forms.
8785
 *
8786
 * @return void
8787
 */
8788
	public function testDateTimeWithGetForms() {
8789
		extract($this->dateRegex);
8790
		$this->Form->create('Contact', array('type' => 'get'));
8791
		$result = $this->Form->datetime('created');
8792
 
8793
		$this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
8794
		$this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
8795
		$this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
8796
		$this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
8797
		$this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
8798
		$this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
8799
	}
8800
 
8801
/**
8802
 * testEditFormWithData method
8803
 *
8804
 * test auto populating form elements from submitted data.
8805
 *
8806
 * @return void
8807
 */
8808
	public function testEditFormWithData() {
8809
		$this->Form->request->data = array('Person' => array(
8810
			'id' => 1,
8811
			'first_name' => 'Nate',
8812
			'last_name' => 'Abele',
8813
			'email' => 'nate@example.com'
8814
		));
8815
		$this->Form->request->addParams(array(
8816
			'models' => array('Person'),
8817
			'controller' => 'people',
8818
			'action' => 'add'
8819
		));
8820
		$options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
8821
 
8822
		$this->Form->create();
8823
		$result = $this->Form->select('People.People', $options, array('multiple' => true));
8824
		$expected = array(
8825
			'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
8826
			'select' => array(
8827
				'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
8828
			),
8829
			array('option' => array('value' => 1)), 'Nate', '/option',
8830
			array('option' => array('value' => 2)), 'Garrett', '/option',
8831
			array('option' => array('value' => 3)), 'Larry', '/option',
8832
			'/select'
8833
		);
8834
		$this->assertTags($result, $expected);
8835
	}
8836
 
8837
/**
8838
 * Test that required fields are created for various types of validation.
8839
 *
8840
 * @return void
8841
 */
8842
	public function testFormInputRequiredDetection() {
8843
		$this->Form->create('Contact');
8844
 
8845
		$result = $this->Form->input('Contact.non_existing');
8846
		$expected = array(
8847
			'div' => array('class' => 'input text'),
8848
			'label' => array('for' => 'ContactNonExisting'),
8849
			'Non Existing',
8850
			'/label',
8851
			'input' => array(
8852
				'type' => 'text', 'name' => 'data[Contact][non_existing]',
8853
				'id' => 'ContactNonExisting'
8854
			),
8855
			'/div'
8856
		);
8857
		$this->assertTags($result, $expected);
8858
 
8859
		$result = $this->Form->input('Contact.imrequired');
8860
		$expected = array(
8861
			'div' => array('class' => 'input text required'),
8862
			'label' => array('for' => 'ContactImrequired'),
8863
			'Imrequired',
8864
			'/label',
8865
			'input' => array(
8866
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
8867
				'id' => 'ContactImrequired',
8868
				'required' => 'required'
8869
			),
8870
			'/div'
8871
		);
8872
		$this->assertTags($result, $expected);
8873
 
8874
		$result = $this->Form->input('Contact.imalsorequired');
8875
		$expected = array(
8876
			'div' => array('class' => 'input text required'),
8877
			'label' => array('for' => 'ContactImalsorequired'),
8878
			'Imalsorequired',
8879
			'/label',
8880
			'input' => array(
8881
				'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
8882
				'id' => 'ContactImalsorequired',
8883
				'required' => 'required'
8884
			),
8885
			'/div'
8886
		);
8887
		$this->assertTags($result, $expected);
8888
 
8889
		$result = $this->Form->input('Contact.imrequiredtoo');
8890
		$expected = array(
8891
			'div' => array('class' => 'input text required'),
8892
			'label' => array('for' => 'ContactImrequiredtoo'),
8893
			'Imrequiredtoo',
8894
			'/label',
8895
			'input' => array(
8896
				'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
8897
				'id' => 'ContactImrequiredtoo',
8898
				'required' => 'required'
8899
			),
8900
			'/div'
8901
		);
8902
		$this->assertTags($result, $expected);
8903
 
8904
		$result = $this->Form->input('Contact.required_one');
8905
		$expected = array(
8906
			'div' => array('class' => 'input text required'),
8907
			'label' => array('for' => 'ContactRequiredOne'),
8908
			'Required One',
8909
			'/label',
8910
			'input' => array(
8911
				'type' => 'text', 'name' => 'data[Contact][required_one]',
8912
				'id' => 'ContactRequiredOne',
8913
				'required' => 'required'
8914
			),
8915
			'/div'
8916
		);
8917
		$this->assertTags($result, $expected);
8918
 
8919
		$result = $this->Form->input('Contact.string_required');
8920
		$expected = array(
8921
			'div' => array('class' => 'input text required'),
8922
			'label' => array('for' => 'ContactStringRequired'),
8923
			'String Required',
8924
			'/label',
8925
			'input' => array(
8926
				'type' => 'text', 'name' => 'data[Contact][string_required]',
8927
				'id' => 'ContactStringRequired',
8928
				'required' => 'required'
8929
			),
8930
			'/div'
8931
		);
8932
		$this->assertTags($result, $expected);
8933
 
8934
		$result = $this->Form->input('Contact.imnotrequired');
8935
		$expected = array(
8936
			'div' => array('class' => 'input text'),
8937
			'label' => array('for' => 'ContactImnotrequired'),
8938
			'Imnotrequired',
8939
			'/label',
8940
			'input' => array(
8941
				'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
8942
				'id' => 'ContactImnotrequired'
8943
			),
8944
			'/div'
8945
		);
8946
		$this->assertTags($result, $expected);
8947
 
8948
		$result = $this->Form->input('Contact.imalsonotrequired');
8949
		$expected = array(
8950
			'div' => array('class' => 'input text'),
8951
			'label' => array('for' => 'ContactImalsonotrequired'),
8952
			'Imalsonotrequired',
8953
			'/label',
8954
			'input' => array(
8955
				'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
8956
				'id' => 'ContactImalsonotrequired'
8957
			),
8958
			'/div'
8959
		);
8960
		$this->assertTags($result, $expected);
8961
 
8962
		$result = $this->Form->input('Contact.imalsonotrequired2');
8963
		$expected = array(
8964
			'div' => array('class' => 'input text'),
8965
			'label' => array('for' => 'ContactImalsonotrequired2'),
8966
			'Imalsonotrequired2',
8967
			'/label',
8968
			'input' => array(
8969
				'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
8970
				'id' => 'ContactImalsonotrequired2'
8971
			),
8972
			'/div'
8973
		);
8974
		$this->assertTags($result, $expected);
8975
 
8976
		$result = $this->Form->input('Contact.imnotrequiredeither');
8977
		$expected = array(
8978
			'div' => array('class' => 'input text'),
8979
			'label' => array('for' => 'ContactImnotrequiredeither'),
8980
			'Imnotrequiredeither',
8981
			'/label',
8982
			'input' => array(
8983
				'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
8984
				'id' => 'ContactImnotrequiredeither'
8985
			),
8986
			'/div'
8987
		);
8988
		$this->assertTags($result, $expected);
8989
 
8990
		$result = $this->Form->input('Contact.iamrequiredalways');
8991
		$expected = array(
8992
			'div' => array('class' => 'input text required'),
8993
			'label' => array('for' => 'ContactIamrequiredalways'),
8994
			'Iamrequiredalways',
8995
			'/label',
8996
			'input' => array(
8997
				'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
8998
				'id' => 'ContactIamrequiredalways',
8999
				'required' => 'required'
9000
			),
9001
			'/div'
9002
		);
9003
		$this->assertTags($result, $expected);
9004
 
9005
		$result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
9006
		$expected = array(
9007
			'div' => array('class' => 'input checkbox required'),
9008
			array('input' => array(
9009
				'type' => 'hidden',
9010
				'name' => 'data[Contact][boolean_field]',
9011
				'id' => 'ContactBooleanField_',
9012
				'value' => '0'
9013
			)),
9014
			array('input' => array(
9015
				'type' => 'checkbox',
9016
				'name' => 'data[Contact][boolean_field]',
9017
				'value' => '1',
9018
				'id' => 'ContactBooleanField'
9019
			)),
9020
			'label' => array('for' => 'ContactBooleanField'),
9021
			'Boolean Field',
9022
			'/label',
9023
			'/div'
9024
		);
9025
		$this->assertTags($result, $expected);
9026
 
9027
		$result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
9028
		$expected = array(
9029
			'div' => array('class' => 'input checkbox required'),
9030
			array('input' => array(
9031
				'type' => 'hidden',
9032
				'name' => 'data[Contact][boolean_field]',
9033
				'id' => 'ContactBooleanField_',
9034
				'value' => '0'
9035
			)),
9036
			array('input' => array(
9037
				'type' => 'checkbox',
9038
				'name' => 'data[Contact][boolean_field]',
9039
				'value' => '1',
9040
				'id' => 'ContactBooleanField',
9041
				'required' => 'required'
9042
			)),
9043
			'label' => array('for' => 'ContactBooleanField'),
9044
			'Boolean Field',
9045
			'/label',
9046
			'/div'
9047
		);
9048
		$this->assertTags($result, $expected);
9049
 
9050
		$result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
9051
		$expected = array(
9052
			'div' => array('class' => 'input file required'),
9053
			'label' => array('for' => 'ContactIamrequiredalways'),
9054
			'Iamrequiredalways',
9055
			'/label',
9056
			'input' => array(
9057
				'type' => 'file',
9058
				'name' => 'data[Contact][iamrequiredalways]',
9059
				'id' => 'ContactIamrequiredalways',
9060
				'required' => 'required'
9061
			),
9062
			'/div'
9063
		);
9064
		$this->assertTags($result, $expected);
9065
	}
9066
 
9067
/**
9068
 * Test that required fields are created when only using ModelValidator::add().
9069
 *
9070
 * @return void
9071
 */
9072
	public function testFormInputRequiredDetectionModelValidator() {
9073
		ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
9074
 
9075
		$this->Form->create('ContactTag');
9076
		$result = $this->Form->input('ContactTag.iwillberequired');
9077
		$expected = array(
9078
			'div' => array('class' => 'input text required'),
9079
			'label' => array('for' => 'ContactTagIwillberequired'),
9080
			'Iwillberequired',
9081
			'/label',
9082
			'input' => array(
9083
				'name' => 'data[ContactTag][iwillberequired]',
9084
				'type' => 'text',
9085
				'id' => 'ContactTagIwillberequired',
9086
				'required' => 'required'
9087
			),
9088
			'/div'
9089
		);
9090
		$this->assertTags($result, $expected);
9091
	}
9092
 
9093
/**
9094
 * testFormMagicInput method
9095
 *
9096
 * @return void
9097
 */
9098
	public function testFormMagicInput() {
9099
		$encoding = strtolower(Configure::read('App.encoding'));
9100
		$result = $this->Form->create('Contact');
9101
		$expected = array(
9102
			'form' => array(
9103
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
9104
				'accept-charset' => $encoding
9105
			),
9106
			'div' => array('style' => 'display:none;'),
9107
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
9108
			'/div'
9109
		);
9110
		$this->assertTags($result, $expected);
9111
 
9112
		$result = $this->Form->input('name');
9113
		$expected = array(
9114
			'div' => array('class' => 'input text'),
9115
			'label' => array('for' => 'ContactName'),
9116
			'Name',
9117
			'/label',
9118
			'input' => array(
9119
				'type' => 'text', 'name' => 'data[Contact][name]',
9120
				'id' => 'ContactName', 'maxlength' => '255'
9121
			),
9122
			'/div'
9123
		);
9124
		$this->assertTags($result, $expected);
9125
 
9126
		$result = $this->Form->input('non_existing_field_in_contact_model');
9127
		$expected = array(
9128
			'div' => array('class' => 'input text'),
9129
			'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
9130
			'Non Existing Field In Contact Model',
9131
			'/label',
9132
			'input' => array(
9133
				'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
9134
				'id' => 'ContactNonExistingFieldInContactModel'
9135
			),
9136
			'/div'
9137
		);
9138
		$this->assertTags($result, $expected);
9139
 
9140
		$result = $this->Form->input('Address.street');
9141
		$expected = array(
9142
			'div' => array('class' => 'input text'),
9143
			'label' => array('for' => 'AddressStreet'),
9144
			'Street',
9145
			'/label',
9146
			'input' => array(
9147
				'type' => 'text', 'name' => 'data[Address][street]',
9148
				'id' => 'AddressStreet'
9149
			),
9150
			'/div'
9151
		);
9152
		$this->assertTags($result, $expected);
9153
 
9154
		$result = $this->Form->input('Address.non_existing_field_in_model');
9155
		$expected = array(
9156
			'div' => array('class' => 'input text'),
9157
			'label' => array('for' => 'AddressNonExistingFieldInModel'),
9158
			'Non Existing Field In Model',
9159
			'/label',
9160
			'input' => array(
9161
				'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
9162
				'id' => 'AddressNonExistingFieldInModel'
9163
			),
9164
			'/div'
9165
		);
9166
		$this->assertTags($result, $expected);
9167
 
9168
		$result = $this->Form->input('name', array('div' => false));
9169
		$expected = array(
9170
			'label' => array('for' => 'ContactName'),
9171
			'Name',
9172
			'/label',
9173
			'input' => array(
9174
				'type' => 'text', 'name' => 'data[Contact][name]',
9175
				'id' => 'ContactName', 'maxlength' => '255'
9176
			)
9177
		);
9178
		$this->assertTags($result, $expected);
9179
 
9180
		extract($this->dateRegex);
9181
		$now = strtotime('now');
9182
 
9183
		$result = $this->Form->input('Contact.published', array('div' => false));
9184
		$expected = array(
9185
			'label' => array('for' => 'ContactPublishedMonth'),
9186
			'Published',
9187
			'/label',
9188
			array('select' => array(
9189
				'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
9190
			)),
9191
			$monthsRegex,
9192
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
9193
			date('F', $now),
9194
			'/option',
9195
			'*/select',
9196
			'-',
9197
			array('select' => array(
9198
				'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
9199
			)),
9200
			$daysRegex,
9201
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
9202
			date('j', $now),
9203
			'/option',
9204
			'*/select',
9205
			'-',
9206
			array('select' => array(
9207
				'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
9208
			)),
9209
			$yearsRegex,
9210
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
9211
			date('Y', $now),
9212
			'*/select'
9213
		);
9214
		$this->assertTags($result, $expected);
9215
 
9216
		$result = $this->Form->input('Contact.updated', array('div' => false));
9217
		$expected = array(
9218
			'label' => array('for' => 'ContactUpdatedMonth'),
9219
			'Updated',
9220
			'/label',
9221
			array('select' => array(
9222
				'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
9223
			)),
9224
			$monthsRegex,
9225
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
9226
			date('F', $now),
9227
			'/option',
9228
			'*/select',
9229
			'-',
9230
			array('select' => array(
9231
				'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
9232
			)),
9233
			$daysRegex,
9234
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
9235
			date('j', $now),
9236
			'/option',
9237
			'*/select',
9238
			'-',
9239
			array('select' => array(
9240
				'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
9241
			)),
9242
			$yearsRegex,
9243
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
9244
			date('Y', $now),
9245
			'*/select'
9246
		);
9247
		$this->assertTags($result, $expected);
9248
 
9249
		$result = $this->Form->input('UserForm.stuff');
9250
		$expected = array(
9251
			'div' => array('class' => 'input text'),
9252
			'label' => array('for' => 'UserFormStuff'),
9253
			'Stuff',
9254
			'/label',
9255
			'input' => array(
9256
				'type' => 'text', 'name' => 'data[UserForm][stuff]',
9257
				'id' => 'UserFormStuff', 'maxlength' => 10
9258
			),
9259
			'/div'
9260
		);
9261
		$this->assertTags($result, $expected);
9262
	}
9263
 
9264
/**
9265
 * testForMagicInputNonExistingNorValidated method
9266
 *
9267
 * @return void
9268
 */
9269
	public function testForMagicInputNonExistingNorValidated() {
9270
		$encoding = strtolower(Configure::read('App.encoding'));
9271
		$result = $this->Form->create('Contact');
9272
		$expected = array(
9273
			'form' => array(
9274
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
9275
				'accept-charset' => $encoding
9276
			),
9277
			'div' => array('style' => 'display:none;'),
9278
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
9279
			'/div'
9280
		);
9281
		$this->assertTags($result, $expected);
9282
 
9283
		$result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
9284
		$expected = array(
9285
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9286
			'Non Existing Nor Validated',
9287
			'/label',
9288
			'input' => array(
9289
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9290
				'id' => 'ContactNonExistingNorValidated'
9291
			)
9292
		);
9293
		$this->assertTags($result, $expected);
9294
 
9295
		$result = $this->Form->input('Contact.non_existing_nor_validated', array(
9296
			'div' => false, 'value' => 'my value'
9297
		));
9298
		$expected = array(
9299
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9300
			'Non Existing Nor Validated',
9301
			'/label',
9302
			'input' => array(
9303
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9304
				'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
9305
			)
9306
		);
9307
		$this->assertTags($result, $expected);
9308
 
9309
		$this->Form->request->data = array(
9310
			'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
9311
		));
9312
		$result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
9313
		$expected = array(
9314
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9315
			'Non Existing Nor Validated',
9316
			'/label',
9317
			'input' => array(
9318
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9319
				'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
9320
			)
9321
		);
9322
		$this->assertTags($result, $expected);
9323
	}
9324
 
9325
/**
9326
 * testFormMagicInputLabel method
9327
 *
9328
 * @return void
9329
 */
9330
	public function testFormMagicInputLabel() {
9331
		$encoding = strtolower(Configure::read('App.encoding'));
9332
		$result = $this->Form->create('Contact');
9333
		$expected = array(
9334
			'form' => array(
9335
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
9336
				'accept-charset' => $encoding
9337
			),
9338
			'div' => array('style' => 'display:none;'),
9339
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
9340
			'/div'
9341
		);
9342
		$this->assertTags($result, $expected);
9343
 
9344
		$result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
9345
		$this->assertTags($result, array('input' => array(
9346
			'name' => 'data[Contact][name]', 'type' => 'text',
9347
			'id' => 'ContactName', 'maxlength' => '255')
9348
		));
9349
 
9350
		$result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
9351
		$expected = array(
9352
			'label' => array('for' => 'ContactName'),
9353
			'My label',
9354
			'/label',
9355
			'input' => array(
9356
				'type' => 'text', 'name' => 'data[Contact][name]',
9357
				'id' => 'ContactName', 'maxlength' => '255'
9358
			)
9359
		);
9360
		$this->assertTags($result, $expected);
9361
 
9362
		$result = $this->Form->input('Contact.name', array(
9363
			'div' => false, 'label' => array('class' => 'mandatory')
9364
		));
9365
		$expected = array(
9366
			'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
9367
			'Name',
9368
			'/label',
9369
			'input' => array(
9370
				'type' => 'text', 'name' => 'data[Contact][name]',
9371
				'id' => 'ContactName', 'maxlength' => '255'
9372
			)
9373
		);
9374
		$this->assertTags($result, $expected);
9375
 
9376
		$result = $this->Form->input('Contact.name', array(
9377
			'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
9378
		));
9379
		$expected = array(
9380
			'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
9381
			'My label',
9382
			'/label',
9383
			'input' => array(
9384
				'type' => 'text', 'name' => 'data[Contact][name]',
9385
				'id' => 'ContactName', 'maxlength' => '255'
9386
			)
9387
		);
9388
		$this->assertTags($result, $expected);
9389
 
9390
		$result = $this->Form->input('Contact.name', array(
9391
			'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
9392
		));
9393
		$expected = array(
9394
			'label' => array('for' => 'my_id'),
9395
			'Name',
9396
			'/label',
9397
			'input' => array(
9398
				'type' => 'text', 'name' => 'data[Contact][name]',
9399
				'id' => 'my_id', 'maxlength' => '255'
9400
			)
9401
		);
9402
		$this->assertTags($result, $expected);
9403
 
9404
		$result = $this->Form->input('1.id');
9405
		$this->assertTags($result, array('input' => array(
9406
			'type' => 'hidden', 'name' => 'data[Contact][1][id]',
9407
			'id' => 'Contact1Id'
9408
		)));
9409
 
9410
		$result = $this->Form->input("1.name");
9411
		$expected = array(
9412
			'div' => array('class' => 'input text'),
9413
			'label' => array('for' => 'Contact1Name'),
9414
			'Name',
9415
			'/label',
9416
			'input' => array(
9417
				'type' => 'text', 'name' => 'data[Contact][1][name]',
9418
				'id' => 'Contact1Name', 'maxlength' => '255'
9419
			),
9420
			'/div'
9421
		);
9422
		$this->assertTags($result, $expected);
9423
 
9424
		$result = $this->Form->input('Contact.1.id');
9425
		$this->assertTags($result, array(
9426
			'input' => array(
9427
				'type' => 'hidden', 'name' => 'data[Contact][1][id]',
9428
				'id' => 'Contact1Id'
9429
			)
9430
		));
9431
 
9432
		$result = $this->Form->input("Model.1.name");
9433
		$expected = array(
9434
			'div' => array('class' => 'input text'),
9435
			'label' => array('for' => 'Model1Name'),
9436
			'Name',
9437
			'/label',
9438
			'input' => array(
9439
				'type' => 'text', 'name' => 'data[Model][1][name]',
9440
				'id' => 'Model1Name'
9441
			),
9442
			'/div'
9443
		);
9444
		$this->assertTags($result, $expected);
9445
	}
9446
 
9447
/**
9448
 * testFormEnd method
9449
 *
9450
 * @return void
9451
 */
9452
	public function testFormEnd() {
9453
		$this->assertEquals('</form>', $this->Form->end());
9454
 
9455
		$result = $this->Form->end('', array('form' => 'form-name'));
9456
		$expected = array(
9457
			'div' => array('class' => 'submit'),
9458
			'input' => array('type' => 'submit', 'value' => ''),
9459
			'/div',
9460
			'/form'
9461
		);
9462
		$this->assertTags($result, $expected);
9463
 
9464
		$result = $this->Form->end('');
9465
		$expected = array(
9466
			'div' => array('class' => 'submit'),
9467
			'input' => array('type' => 'submit', 'value' => ''),
9468
			'/div',
9469
			'/form'
9470
		);
9471
		$this->assertTags($result, $expected);
9472
 
9473
		$result = $this->Form->end(array('label' => ''));
9474
		$expected = array(
9475
			'div' => array('class' => 'submit'),
9476
			'input' => array('type' => 'submit', 'value' => ''),
9477
			'/div',
9478
			'/form'
9479
		);
9480
		$this->assertTags($result, $expected);
9481
 
9482
		$result = $this->Form->end('save');
9483
		$expected = array(
9484
			'div' => array('class' => 'submit'),
9485
			'input' => array('type' => 'submit', 'value' => 'save'),
9486
			'/div',
9487
			'/form'
9488
		);
9489
		$this->assertTags($result, $expected);
9490
 
9491
		$result = $this->Form->end(array('label' => 'save'));
9492
		$expected = array(
9493
			'div' => array('class' => 'submit'),
9494
			'input' => array('type' => 'submit', 'value' => 'save'),
9495
			'/div',
9496
			'/form'
9497
		);
9498
		$this->assertTags($result, $expected);
9499
 
9500
		$result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
9501
		$expected = array(
9502
			'div' => array('class' => 'submit'),
9503
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9504
			'/div',
9505
			'/form'
9506
		);
9507
		$this->assertTags($result, $expected);
9508
 
9509
		$result = $this->Form->end(array('name' => 'Whatever'));
9510
		$expected = array(
9511
			'div' => array('class' => 'submit'),
9512
			'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
9513
			'/div',
9514
			'/form'
9515
		);
9516
		$this->assertTags($result, $expected);
9517
 
9518
		$result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
9519
		$expected = array(
9520
			'div' => array('class' => 'good'),
9521
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9522
			'/div',
9523
			'/form'
9524
		);
9525
		$this->assertTags($result, $expected);
9526
 
9527
		$result = $this->Form->end(array(
9528
			'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
9529
		));
9530
		$expected = array(
9531
			'div' => array('class' => 'good'),
9532
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9533
			'/div',
9534
			'/form'
9535
		);
9536
		$this->assertTags($result, $expected);
9537
	}
9538
 
9539
/**
9540
 * testMultipleFormWithIdFields method
9541
 *
9542
 * @return void
9543
 */
9544
	public function testMultipleFormWithIdFields() {
9545
		$this->Form->create('UserForm');
9546
 
9547
		$result = $this->Form->input('id');
9548
		$this->assertTags($result, array('input' => array(
9549
			'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
9550
		)));
9551
 
9552
		$result = $this->Form->input('ValidateItem.id');
9553
		$this->assertTags($result, array('input' => array(
9554
			'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
9555
			'id' => 'ValidateItemId'
9556
		)));
9557
 
9558
		$result = $this->Form->input('ValidateUser.id');
9559
		$this->assertTags($result, array('input' => array(
9560
			'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
9561
			'id' => 'ValidateUserId'
9562
		)));
9563
	}
9564
 
9565
/**
9566
 * testDbLessModel method
9567
 *
9568
 * @return void
9569
 */
9570
	public function testDbLessModel() {
9571
		$this->Form->create('TestMail');
9572
 
9573
		$result = $this->Form->input('name');
9574
		$expected = array(
9575
			'div' => array('class' => 'input text'),
9576
			'label' => array('for' => 'TestMailName'),
9577
			'Name',
9578
			'/label',
9579
			'input' => array(
9580
				'name' => 'data[TestMail][name]', 'type' => 'text',
9581
				'id' => 'TestMailName'
9582
			),
9583
			'/div'
9584
		);
9585
		$this->assertTags($result, $expected);
9586
 
9587
		ClassRegistry::init('TestMail');
9588
		$this->Form->create('TestMail');
9589
		$result = $this->Form->input('name');
9590
		$expected = array(
9591
			'div' => array('class' => 'input text'),
9592
			'label' => array('for' => 'TestMailName'),
9593
			'Name',
9594
			'/label',
9595
			'input' => array(
9596
				'name' => 'data[TestMail][name]', 'type' => 'text',
9597
				'id' => 'TestMailName'
9598
			),
9599
			'/div'
9600
		);
9601
		$this->assertTags($result, $expected);
9602
	}
9603
 
9604
/**
9605
 * testBrokenness method
9606
 *
9607
 * @return void
9608
 */
9609
	public function testBrokenness() {
9610
		/*
9611
		 * #4 This test has two parents and four children. By default (as of r7117) both
9612
		 * parents are show but the first parent is missing a child. This is the inconsistency
9613
		 * in the default behaviour - one parent has all children, the other does not - dependent
9614
		 * on the data values.
9615
		 */
9616
		$result = $this->Form->select('Model.field', array(
9617
			'Fred' => array(
9618
				'freds_son_1' => 'Fred',
9619
				'freds_son_2' => 'Freddie'
9620
			),
9621
			'Bert' => array(
9622
				'berts_son_1' => 'Albert',
9623
				'berts_son_2' => 'Bertie')
9624
			),
9625
			array('showParents' => true, 'empty' => false)
9626
		);
9627
 
9628
		$expected = array(
9629
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
9630
				array('optgroup' => array('label' => 'Fred')),
9631
					array('option' => array('value' => 'freds_son_1')),
9632
						'Fred',
9633
					'/option',
9634
					array('option' => array('value' => 'freds_son_2')),
9635
						'Freddie',
9636
					'/option',
9637
				'/optgroup',
9638
				array('optgroup' => array('label' => 'Bert')),
9639
					array('option' => array('value' => 'berts_son_1')),
9640
						'Albert',
9641
					'/option',
9642
					array('option' => array('value' => 'berts_son_2')),
9643
						'Bertie',
9644
					'/option',
9645
				'/optgroup',
9646
			'/select'
9647
		);
9648
		$this->assertTags($result, $expected);
9649
 
9650
		/*
9651
		 * #2 This is structurally identical to the test above (#1) - only the parent name has
9652
		 * changed, so we should expect the same select list data, just with a different name
9653
		 * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
9654
		 * This is where data corruption can occur, because when a select value is missing from
9655
		 * a list a form will substitute the first value in the list - without the user knowing.
9656
		 * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
9657
		 * affect the availability of 3 => 'Three' as a valid option.
9658
		 */
9659
		$options = array(1 => 'One', 2 => 'Two', 'Three' => array(
9660
			3 => 'Three', 4 => 'Four', 5 => 'Five'
9661
		));
9662
		$result = $this->Form->select(
9663
			'Model.field', $options, array('showParents' => true, 'empty' => false)
9664
		);
9665
 
9666
		$expected = array(
9667
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
9668
				array('option' => array('value' => 1)),
9669
					'One',
9670
				'/option',
9671
				array('option' => array('value' => 2)),
9672
					'Two',
9673
				'/option',
9674
				array('optgroup' => array('label' => 'Three')),
9675
					array('option' => array('value' => 3)),
9676
						'Three',
9677
					'/option',
9678
					array('option' => array('value' => 4)),
9679
						'Four',
9680
					'/option',
9681
					array('option' => array('value' => 5)),
9682
						'Five',
9683
					'/option',
9684
				'/optgroup',
9685
			'/select'
9686
		);
9687
		$this->assertTags($result, $expected);
9688
	}
9689
 
9690
/**
9691
 * Test the generation of fields for a multi record form.
9692
 *
9693
 * @return void
9694
 */
9695
	public function testMultiRecordForm() {
9696
		$this->Form->create('ValidateProfile');
9697
		$this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
9698
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
9699
		$expected = array(
9700
			'div' => array('class' => 'input textarea'),
9701
				'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
9702
					'Name',
9703
				'/label',
9704
				'textarea' => array(
9705
					'id' => 'ValidateProfile1ValidateItem2Name',
9706
					'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
9707
					'maxlength' => 255,
9708
					'cols' => 30,
9709
					'rows' => 6
9710
				),
9711
				'Value',
9712
				'/textarea',
9713
			'/div'
9714
		);
9715
		$this->assertTags($result, $expected);
9716
 
9717
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
9718
		$expected = array(
9719
			'div' => array('class' => 'input date'),
9720
			'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
9721
			'Created',
9722
			'/label',
9723
			array('select' => array(
9724
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
9725
				'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
9726
				)
9727
			),
9728
			array('option' => array('value' => '')), '/option',
9729
			$this->dateRegex['monthsRegex'],
9730
			'/select', '-',
9731
			array('select' => array(
9732
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
9733
				'id' => 'ValidateProfile1ValidateItem2CreatedDay'
9734
				)
9735
			),
9736
			array('option' => array('value' => '')), '/option',
9737
			$this->dateRegex['daysRegex'],
9738
			'/select', '-',
9739
			array('select' => array(
9740
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
9741
				'id' => 'ValidateProfile1ValidateItem2CreatedYear'
9742
				)
9743
			),
9744
			array('option' => array('value' => '')), '/option',
9745
			$this->dateRegex['yearsRegex'],
9746
			'/select',
9747
			'/div'
9748
		);
9749
		$this->assertTags($result, $expected);
9750
 
9751
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
9752
		$ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
9753
		$this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
9754
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
9755
		$expected = array(
9756
			'div' => array('class' => 'input select error'),
9757
			'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
9758
			'Profile',
9759
			'/label',
9760
			'select' => array(
9761
				'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
9762
				'id' => 'ValidateProfile1ValidateItem2ProfileId',
9763
				'class' => 'form-error'
9764
			),
9765
			'/select',
9766
			array('div' => array('class' => 'error-message')),
9767
			'Error',
9768
			'/div',
9769
			'/div'
9770
		);
9771
		$this->assertTags($result, $expected);
9772
	}
9773
 
9774
/**
9775
 * test the correct display of multi-record form validation errors.
9776
 *
9777
 * @return void
9778
 */
9779
	public function testMultiRecordFormValidationErrors() {
9780
		$this->Form->create('ValidateProfile');
9781
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
9782
		$ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
9783
		$result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
9784
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
9785
 
9786
		$ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
9787
		$result = $this->Form->error('ValidateProfile.2.city');
9788
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9789
 
9790
		$result = $this->Form->error('2.city');
9791
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9792
	}
9793
 
9794
/**
9795
 * test the correct display of multi-record form validation errors.
9796
 *
9797
 * @return void
9798
 */
9799
	public function testSaveManyRecordFormValidationErrors() {
9800
		$this->Form->create('ValidateUser');
9801
		$ValidateUser = ClassRegistry::getObject('ValidateUser');
9802
		$ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
9803
 
9804
		$result = $this->Form->error('0.ValidateUser.ValidateItem.name');
9805
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
9806
 
9807
		$ValidateUser->validationErrors[0]['city'] = array('Error in field city');
9808
		$result = $this->Form->error('ValidateUser.0.city');
9809
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9810
	}
9811
 
9812
/**
9813
 * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
9814
 *
9815
 * @return void
9816
 */
9817
	public function testInputTemplate() {
9818
		$result = $this->Form->input('Contact.email', array(
9819
			'type' => 'text', 'format' => array('input')
9820
		));
9821
		$expected = array(
9822
			'div' => array('class' => 'input text'),
9823
			'input' => array(
9824
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]',
9825
				'id' => 'ContactEmail'
9826
			),
9827
			'/div'
9828
		);
9829
		$this->assertTags($result, $expected);
9830
 
9831
		$result = $this->Form->input('Contact.email', array(
9832
			'type' => 'text', 'format' => array('input', 'label'),
9833
			'label' => '<em>Email (required)</em>'
9834
		));
9835
		$expected = array(
9836
			'div' => array('class' => 'input text'),
9837
			array('input' => array(
9838
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]',
9839
				'id' => 'ContactEmail'
9840
			)),
9841
			'label' => array('for' => 'ContactEmail'),
9842
			'em' => array(),
9843
			'Email (required)',
9844
			'/em',
9845
			'/label',
9846
			'/div'
9847
		);
9848
		$this->assertTags($result, $expected);
9849
 
9850
		$result = $this->Form->input('Contact.email', array(
9851
			'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
9852
			'between' => '<div>Something in the middle</div>',
9853
			'after' => '<span>Some text at the end</span>'
9854
		));
9855
		$expected = array(
9856
			'div' => array('class' => 'input text'),
9857
			array('input' => array(
9858
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]',
9859
				'id' => 'ContactEmail'
9860
			)),
9861
			array('div' => array()),
9862
			'Something in the middle',
9863
			'/div',
9864
			'label' => array('for' => 'ContactEmail'),
9865
			'Email',
9866
			'/label',
9867
			'span' => array(),
9868
			'Some text at the end',
9869
			'/span',
9870
			'/div'
9871
		);
9872
		$this->assertTags($result, $expected);
9873
 
9874
		$result = $this->Form->input('Contact.method', array(
9875
			'type' => 'radio',
9876
			'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
9877
			'between' => 'I am between',
9878
		));
9879
		$expected = array(
9880
			'div' => array('class' => 'input radio'),
9881
			'fieldset' => array(),
9882
			'legend' => array(),
9883
			'Method',
9884
			'/legend',
9885
			'I am between',
9886
			'input' => array(
9887
				'type' => 'hidden', 'name' => 'data[Contact][method]',
9888
				'value' => '', 'id' => 'ContactMethod_'
9889
			),
9890
			array('input' => array(
9891
				'type' => 'radio', 'name' => 'data[Contact][method]',
9892
				'value' => 'email', 'id' => 'ContactMethodEmail'
9893
			)),
9894
			array('label' => array('for' => 'ContactMethodEmail')),
9895
			'Email',
9896
			'/label',
9897
			array('input' => array(
9898
				'type' => 'radio', 'name' => 'data[Contact][method]',
9899
				'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
9900
			)),
9901
			array('label' => array('for' => 'ContactMethodPigeon')),
9902
			'Pigeon',
9903
			'/label',
9904
			'/fieldset',
9905
			'/div',
9906
		);
9907
		$this->assertTags($result, $expected);
9908
	}
9909
 
9910
/**
9911
 * test that some html5 inputs + FormHelper::__call() work
9912
 *
9913
 * @return void
9914
 */
9915
	public function testHtml5Inputs() {
9916
		$result = $this->Form->email('User.email');
9917
		$expected = array(
9918
			'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
9919
		);
9920
		$this->assertTags($result, $expected);
9921
 
9922
		$result = $this->Form->search('User.query');
9923
		$expected = array(
9924
			'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
9925
		);
9926
		$this->assertTags($result, $expected);
9927
 
9928
		$result = $this->Form->search('User.query', array('value' => 'test'));
9929
		$expected = array(
9930
			'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
9931
		);
9932
		$this->assertTags($result, $expected);
9933
 
9934
		$result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
9935
		$expected = array(
9936
			'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
9937
		);
9938
		$this->assertTags($result, $expected);
9939
 
9940
		$result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
9941
		$expected = array(
9942
			'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
9943
		);
9944
		$this->assertTags($result, $expected);
9945
	}
9946
 
9947
/**
9948
 * @expectedException CakeException
9949
 * @return void
9950
 */
9951
	public function testHtml5InputException() {
9952
		$this->Form->email();
9953
	}
9954
 
9955
/**
9956
 * Tests that a model can be loaded from the model names passed in the request object
9957
 *
9958
 * @return void
9959
 */
9960
	public function testIntrospectModelFromRequest() {
9961
		$this->loadFixtures('Post');
9962
		App::build(array(
9963
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
9964
		));
9965
		CakePlugin::load('TestPlugin');
9966
		$this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
9967
 
9968
		$this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
9969
		$this->Form->create('TestPluginPost');
9970
		$this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
9971
		$this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
9972
 
9973
		CakePlugin::unload();
9974
		App::build();
9975
	}
9976
 
9977
/**
9978
 * Tests that it is possible to set the validation errors directly in the helper for a field
9979
 *
9980
 * @return void
9981
 */
9982
	public function testCustomValidationErrors() {
9983
		$this->Form->validationErrors['Thing']['field'] = 'Badness!';
9984
		$result = $this->Form->error('Thing.field', null, array('wrap' => false));
9985
		$this->assertEquals('Badness!', $result);
9986
	}
9987
 
9988
/**
9989
 * Tests that the 'on' key validates as expected on create
9990
 *
9991
 * @return void
9992
 */
9993
	public function testRequiredOnCreate() {
9994
		$this->Form->create('Contact');
9995
 
9996
		$result = $this->Form->input('Contact.imrequiredonupdate');
9997
		$expected = array(
9998
			'div' => array('class' => 'input text'),
9999
			'label' => array('for' => 'ContactImrequiredonupdate'),
10000
			'Imrequiredonupdate',
10001
			'/label',
10002
			'input' => array(
10003
				'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
10004
				'id' => 'ContactImrequiredonupdate'
10005
			),
10006
			'/div'
10007
		);
10008
		$this->assertTags($result, $expected);
10009
 
10010
		$result = $this->Form->input('Contact.imrequiredoncreate');
10011
		$expected = array(
10012
			'div' => array('class' => 'input text required'),
10013
			'label' => array('for' => 'ContactImrequiredoncreate'),
10014
			'Imrequiredoncreate',
10015
			'/label',
10016
			'input' => array(
10017
				'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
10018
				'id' => 'ContactImrequiredoncreate',
10019
				'required' => 'required'
10020
			),
10021
			'/div'
10022
		);
10023
		$this->assertTags($result, $expected);
10024
 
10025
		$result = $this->Form->input('Contact.imrequiredonboth');
10026
		$expected = array(
10027
			'div' => array('class' => 'input text required'),
10028
			'label' => array('for' => 'ContactImrequiredonboth'),
10029
			'Imrequiredonboth',
10030
			'/label',
10031
			'input' => array(
10032
				'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
10033
				'id' => 'ContactImrequiredonboth',
10034
				'required' => 'required'
10035
			),
10036
			'/div'
10037
		);
10038
		$this->assertTags($result, $expected);
10039
 
10040
		$this->Form->inputDefaults(array('required' => false));
10041
		$result = $this->Form->input('Contact.imrequired');
10042
		$expected = array(
10043
			'div' => array('class' => 'input text'),
10044
			'label' => array('for' => 'ContactImrequired'),
10045
			'Imrequired',
10046
			'/label',
10047
			'input' => array(
10048
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
10049
				'id' => 'ContactImrequired'
10050
			),
10051
			'/div'
10052
		);
10053
		$this->assertTags($result, $expected);
10054
 
10055
		$result = $this->Form->input('Contact.imrequired', array('required' => false));
10056
		$this->assertTags($result, $expected);
10057
 
10058
		$result = $this->Form->input('Contact.imrequired', array('required' => true));
10059
		$expected = array(
10060
			'div' => array('class' => 'input text required'),
10061
			'label' => array('for' => 'ContactImrequired'),
10062
			'Imrequired',
10063
			'/label',
10064
			'input' => array(
10065
				'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
10066
				'id' => 'ContactImrequired'
10067
			),
10068
			'/div'
10069
		);
10070
		$this->assertTags($result, $expected);
10071
 
10072
		$result = $this->Form->input('Contact.imrequired', array('required' => null));
10073
		$this->assertTags($result, $expected);
10074
	}
10075
 
10076
/**
10077
 * Tests that the 'on' key validates as expected on update
10078
 *
10079
 * @return void
10080
 */
10081
	public function testRequiredOnUpdate() {
10082
		$this->Form->request->data['Contact']['id'] = 1;
10083
		$this->Form->create('Contact');
10084
 
10085
		$result = $this->Form->input('Contact.imrequiredonupdate');
10086
		$expected = array(
10087
			'div' => array('class' => 'input text required'),
10088
			'label' => array('for' => 'ContactImrequiredonupdate'),
10089
			'Imrequiredonupdate',
10090
			'/label',
10091
			'input' => array(
10092
				'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
10093
				'id' => 'ContactImrequiredonupdate',
10094
				'required' => 'required'
10095
			),
10096
			'/div'
10097
		);
10098
		$this->assertTags($result, $expected);
10099
		$result = $this->Form->input('Contact.imrequiredoncreate');
10100
		$expected = array(
10101
			'div' => array('class' => 'input text'),
10102
			'label' => array('for' => 'ContactImrequiredoncreate'),
10103
			'Imrequiredoncreate',
10104
			'/label',
10105
			'input' => array(
10106
				'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
10107
				'id' => 'ContactImrequiredoncreate'
10108
			),
10109
			'/div'
10110
		);
10111
		$this->assertTags($result, $expected);
10112
 
10113
		$result = $this->Form->input('Contact.imrequiredonboth');
10114
		$expected = array(
10115
			'div' => array('class' => 'input text required'),
10116
			'label' => array('for' => 'ContactImrequiredonboth'),
10117
			'Imrequiredonboth',
10118
			'/label',
10119
			'input' => array(
10120
				'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
10121
				'id' => 'ContactImrequiredonboth',
10122
				'required' => 'required'
10123
			),
10124
			'/div'
10125
		);
10126
		$this->assertTags($result, $expected);
10127
 
10128
		$result = $this->Form->input('Contact.imrequired');
10129
		$expected = array(
10130
			'div' => array('class' => 'input text required'),
10131
			'label' => array('for' => 'ContactImrequired'),
10132
			'Imrequired',
10133
			'/label',
10134
			'input' => array(
10135
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
10136
				'id' => 'ContactImrequired',
10137
				'required' => 'required'
10138
			),
10139
			'/div'
10140
		);
10141
		$this->assertTags($result, $expected);
10142
	}
10143
 
10144
/**
10145
 * Test inputDefaults setter and getter
10146
 *
10147
 * @return void
10148
 */
10149
	public function testInputDefaults() {
10150
		$this->Form->create('Contact');
10151
 
10152
		$this->Form->inputDefaults(array(
10153
			'label' => false,
10154
			'div' => array(
10155
				'style' => 'color: #000;'
10156
			)
10157
		));
10158
		$result = $this->Form->input('Contact.field1');
10159
		$expected = array(
10160
			'div' => array('class' => 'input text', 'style' => 'color: #000;'),
10161
			'input' => array(
10162
				'type' => 'text', 'name' => 'data[Contact][field1]',
10163
				'id' => 'ContactField1'
10164
			),
10165
			'/div'
10166
		);
10167
		$this->assertTags($result, $expected);
10168
 
10169
		$this->Form->inputDefaults(array(
10170
			'div' => false,
10171
			'label' => 'Label',
10172
		));
10173
		$result = $this->Form->input('Contact.field1');
10174
		$expected = array(
10175
			'label' => array('for' => 'ContactField1'),
10176
			'Label',
10177
			'/label',
10178
			'input' => array(
10179
				'type' => 'text', 'name' => 'data[Contact][field1]',
10180
				'id' => 'ContactField1'
10181
			),
10182
		);
10183
		$this->assertTags($result, $expected);
10184
 
10185
		$this->Form->inputDefaults(array(
10186
			'label' => false,
10187
		), true);
10188
		$result = $this->Form->input('Contact.field1');
10189
		$expected = array(
10190
			'input' => array(
10191
				'type' => 'text', 'name' => 'data[Contact][field1]',
10192
				'id' => 'ContactField1'
10193
			),
10194
		);
10195
		$this->assertTags($result, $expected);
10196
 
10197
		$result = $this->Form->inputDefaults();
10198
		$expected = array(
10199
			'div' => false,
10200
			'label' => false,
10201
		);
10202
		$this->assertEquals($expected, $result);
10203
	}
10204
 
10205
}