Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 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 number fields for integer fields
768
 *
769
 * @return void
770
 */
771
	public function testTextFieldTypeNumberGenerationForIntegers() {
772
		$model = ClassRegistry::getObject('Contact');
773
		$model->setSchema(array('foo' => array(
774
			'type' => 'integer',
775
			'null' => false,
776
			'default' => null,
777
			'length' => null
778
		)));
779
 
780
		$this->Form->create('Contact');
781
		$result = $this->Form->input('foo');
782
		$expected = array(
783
			'div' => array('class' => 'input number'),
784
			'label' => array('for' => 'ContactFoo'),
785
			'Foo',
786
			'/label',
787
			array('input' => array(
788
				'type' => 'number', 'name' => 'data[Contact][foo]',
789
				'id' => 'ContactFoo'
790
			)),
791
			'/div'
792
		);
793
		$this->assertTags($result, $expected);
794
	}
795
 
796
/**
797
 * Tests correct generation of file upload fields for binary fields
798
 *
799
 * @return void
800
 */
801
	public function testFileUploadFieldTypeGenerationForBinaries() {
802
		$model = ClassRegistry::getObject('Contact');
803
		$model->setSchema(array('foo' => array(
804
			'type' => 'binary',
805
			'null' => false,
806
			'default' => null,
807
			'length' => 1024
808
		)));
809
 
810
		$this->Form->create('Contact');
811
		$result = $this->Form->input('foo');
812
		$expected = array(
813
			'div' => array('class' => 'input file'),
814
			'label' => array('for' => 'ContactFoo'),
815
			'Foo',
816
			'/label',
817
			array('input' => array(
818
				'type' => 'file', 'name' => 'data[Contact][foo]',
819
				'id' => 'ContactFoo'
820
			)),
821
			'/div'
822
		);
823
		$this->assertTags($result, $expected);
824
	}
825
 
826
/**
827
 * testFormSecurityMultipleFields method
828
 *
829
 * Test secure() with multiple row form. Ensure hash is correct.
830
 *
831
 * @return void
832
 */
833
	public function testFormSecurityMultipleFields() {
834
		$key = 'testKey';
835
 
836
		$fields = array(
837
			'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
838
			'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
839
			'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
840
		);
841
		$this->Form->request['_Token'] = array('key' => $key);
842
		$result = $this->Form->secure($fields);
843
 
844
		$hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
845
		$hash .= '%7CModel.1.hidden%7CModel.1.valid';
846
 
847
		$expected = array(
848
			'div' => array('style' => 'display:none;'),
849
			array('input' => array(
850
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
851
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
852
			)),
853
			array('input' => array(
854
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
855
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
856
			)),
857
			'/div'
858
		);
859
		$this->assertTags($result, $expected);
860
	}
861
 
862
/**
863
 * testFormSecurityMultipleSubmitButtons
864
 *
865
 * test form submit generation and ensure that _Token is only created on end()
866
 *
867
 * @return void
868
 */
869
	public function testFormSecurityMultipleSubmitButtons() {
870
		$key = 'testKey';
871
		$this->Form->request['_Token'] = array('key' => $key);
872
 
873
		$this->Form->create('Addresses');
874
		$this->Form->input('Address.title');
875
		$this->Form->input('Address.first_name');
876
 
877
		$result = $this->Form->submit('Save', array('name' => 'save'));
878
		$expected = array(
879
			'div' => array('class' => 'submit'),
880
			'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
881
			'/div',
882
		);
883
		$this->assertTags($result, $expected);
884
 
885
		$result = $this->Form->submit('Cancel', array('name' => 'cancel'));
886
		$expected = array(
887
			'div' => array('class' => 'submit'),
888
			'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
889
			'/div',
890
		);
891
		$this->assertTags($result, $expected);
892
		$result = $this->Form->end(null);
893
 
894
		$expected = array(
895
			'div' => array('style' => 'display:none;'),
896
			array('input' => array(
897
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
898
				'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
899
			)),
900
			array('input' => array(
901
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
902
				'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
903
			)),
904
			'/div'
905
		);
906
		$this->assertTags($result, $expected);
907
	}
908
 
909
/**
910
 * Test that buttons created with foo[bar] name attributes are unlocked correctly.
911
 *
912
 * @return void
913
 */
914
	public function testSecurityButtonNestedNamed() {
915
		$key = 'testKey';
916
		$this->Form->request['_Token'] = array('key' => $key);
917
 
918
		$this->Form->create('Addresses');
919
		$this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
920
		$result = $this->Form->unlockField();
921
		$this->assertEquals(array('Address.button'), $result);
922
	}
923
 
924
/**
925
 * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
926
 *
927
 * @return void
928
 */
929
	public function testSecuritySubmitNestedNamed() {
930
		$key = 'testKey';
931
		$this->Form->request['_Token'] = array('key' => $key);
932
 
933
		$this->Form->create('Addresses');
934
		$this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
935
		$result = $this->Form->unlockField();
936
		$this->assertEquals(array('Address.button'), $result);
937
	}
938
 
939
/**
940
 * Test that the correct fields are unlocked for image submits with no names.
941
 *
942
 * @return void
943
 */
944
	public function testSecuritySubmitImageNoName() {
945
		$key = 'testKey';
946
		$this->Form->request['_Token'] = array('key' => $key);
947
 
948
		$this->Form->create('User');
949
		$result = $this->Form->submit('save.png');
950
		$expected = array(
951
			'div' => array('class' => 'submit'),
952
			'input' => array('type' => 'image', 'src' => 'img/save.png'),
953
			'/div'
954
		);
955
		$this->assertTags($result, $expected);
956
		$this->assertEquals(array('x', 'y'), $this->Form->unlockField());
957
	}
958
 
959
/**
960
 * Test that the correct fields are unlocked for image submits with names.
961
 *
962
 * @return void
963
 */
964
	public function testSecuritySubmitImageName() {
965
		$key = 'testKey';
966
		$this->Form->request['_Token'] = array('key' => $key);
967
 
968
		$this->Form->create('User');
969
		$result = $this->Form->submit('save.png', array('name' => 'test'));
970
		$expected = array(
971
			'div' => array('class' => 'submit'),
972
			'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
973
			'/div'
974
		);
975
		$this->assertTags($result, $expected);
976
		$this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
977
	}
978
 
979
/**
980
 * testFormSecurityMultipleInputFields method
981
 *
982
 * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
983
 *
984
 * @return void
985
 */
986
	public function testFormSecurityMultipleInputFields() {
987
		$key = 'testKey';
988
 
989
		$this->Form->request['_Token'] = array('key' => $key);
990
		$this->Form->create('Addresses');
991
 
992
		$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
993
		$this->Form->input('Addresses.0.title');
994
		$this->Form->input('Addresses.0.first_name');
995
		$this->Form->input('Addresses.0.last_name');
996
		$this->Form->input('Addresses.0.address');
997
		$this->Form->input('Addresses.0.city');
998
		$this->Form->input('Addresses.0.phone');
999
		$this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
1000
 
1001
		$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1002
		$this->Form->input('Addresses.1.title');
1003
		$this->Form->input('Addresses.1.first_name');
1004
		$this->Form->input('Addresses.1.last_name');
1005
		$this->Form->input('Addresses.1.address');
1006
		$this->Form->input('Addresses.1.city');
1007
		$this->Form->input('Addresses.1.phone');
1008
		$this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
1009
 
1010
		$result = $this->Form->secure($this->Form->fields);
1011
 
1012
		$hash = 'a3b9b2ba1cb688838f92818a5970e17dd7943a78%3AAddresses.0.id%7CAddresses.1.id';
1013
 
1014
		$expected = array(
1015
			'div' => array('style' => 'display:none;'),
1016
			array('input' => array(
1017
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1018
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1019
			)),
1020
			array('input' => array(
1021
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1022
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
1023
			)),
1024
			'/div'
1025
		);
1026
		$this->assertTags($result, $expected);
1027
	}
1028
 
1029
/**
1030
 * Test form security with Model.field.0 style inputs
1031
 *
1032
 * @return void
1033
 */
1034
	public function testFormSecurityArrayFields() {
1035
		$key = 'testKey';
1036
 
1037
		$this->Form->request->params['_Token']['key'] = $key;
1038
		$this->Form->create('Address');
1039
		$this->Form->input('Address.primary.1');
1040
		$this->assertEquals('Address.primary', $this->Form->fields[0]);
1041
 
1042
		$this->Form->input('Address.secondary.1.0');
1043
		$this->assertEquals('Address.secondary', $this->Form->fields[1]);
1044
	}
1045
 
1046
/**
1047
 * testFormSecurityMultipleInputDisabledFields method
1048
 *
1049
 * test secure form generation with multiple records and disabled fields.
1050
 *
1051
 * @return void
1052
 */
1053
	public function testFormSecurityMultipleInputDisabledFields() {
1054
		$key = 'testKey';
1055
		$this->Form->request->params['_Token'] = array(
1056
			'key' => $key,
1057
			'unlockedFields' => array('first_name', 'address')
1058
		);
1059
		$this->Form->create();
1060
 
1061
		$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
1062
		$this->Form->input('Addresses.0.title');
1063
		$this->Form->input('Addresses.0.first_name');
1064
		$this->Form->input('Addresses.0.last_name');
1065
		$this->Form->input('Addresses.0.address');
1066
		$this->Form->input('Addresses.0.city');
1067
		$this->Form->input('Addresses.0.phone');
1068
		$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1069
		$this->Form->input('Addresses.1.title');
1070
		$this->Form->input('Addresses.1.first_name');
1071
		$this->Form->input('Addresses.1.last_name');
1072
		$this->Form->input('Addresses.1.address');
1073
		$this->Form->input('Addresses.1.city');
1074
		$this->Form->input('Addresses.1.phone');
1075
 
1076
		$result = $this->Form->secure($this->Form->fields);
1077
		$hash = '5c9cadf9da008cc444d3960b481391a425a5d979%3AAddresses.0.id%7CAddresses.1.id';
1078
 
1079
		$expected = array(
1080
			'div' => array('style' => 'display:none;'),
1081
			array('input' => array(
1082
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1083
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1084
			)),
1085
			array('input' => array(
1086
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1087
				'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
1088
			)),
1089
			'/div'
1090
		);
1091
		$this->assertTags($result, $expected);
1092
	}
1093
 
1094
/**
1095
 * testFormSecurityInputDisabledFields method
1096
 *
1097
 * Test single record form with disabled fields.
1098
 *
1099
 * @return void
1100
 */
1101
	public function testFormSecurityInputUnlockedFields() {
1102
		$key = 'testKey';
1103
		$this->Form->request['_Token'] = array(
1104
			'key' => $key,
1105
			'unlockedFields' => array('first_name', 'address')
1106
		);
1107
		$this->Form->create();
1108
		$this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
1109
 
1110
		$this->Form->hidden('Addresses.id', array('value' => '123456'));
1111
		$this->Form->input('Addresses.title');
1112
		$this->Form->input('Addresses.first_name');
1113
		$this->Form->input('Addresses.last_name');
1114
		$this->Form->input('Addresses.address');
1115
		$this->Form->input('Addresses.city');
1116
		$this->Form->input('Addresses.phone');
1117
 
1118
		$result = $this->Form->fields;
1119
		$expected = array(
1120
			'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
1121
			'Addresses.city', 'Addresses.phone'
1122
		);
1123
		$this->assertEquals($expected, $result);
1124
 
1125
		$result = $this->Form->secure($expected);
1126
 
1127
		$hash = '40289bd07811587887ff56585a8526ff9da59d7a%3AAddresses.id';
1128
		$expected = array(
1129
			'div' => array('style' => 'display:none;'),
1130
			array('input' => array(
1131
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1132
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1133
			)),
1134
			array('input' => array(
1135
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1136
				'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
1137
			)),
1138
			'/div'
1139
		);
1140
		$this->assertTags($result, $expected);
1141
	}
1142
 
1143
/**
1144
 * test securing inputs with custom name attributes.
1145
 *
1146
 * @return void
1147
 */
1148
	public function testFormSecureWithCustomNameAttribute() {
1149
		$this->Form->request->params['_Token']['key'] = 'testKey';
1150
 
1151
		$this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
1152
		$this->assertEquals('User.custom', $this->Form->fields[0]);
1153
 
1154
		$this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
1155
		$this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
1156
	}
1157
 
1158
/**
1159
 * testFormSecuredInput method
1160
 *
1161
 * Test generation of entire secure form, assertions made on input() output.
1162
 *
1163
 * @return void
1164
 */
1165
	public function testFormSecuredInput() {
1166
		$this->Form->request['_Token'] = array('key' => 'testKey');
1167
 
1168
		$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
1169
		$encoding = strtolower(Configure::read('App.encoding'));
1170
		$expected = array(
1171
			'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
1172
			'div' => array('style' => 'display:none;'),
1173
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
1174
			array('input' => array(
1175
				'type' => 'hidden', 'name' => 'data[_Token][key]',
1176
				'value' => 'testKey', 'id' => 'preg:/Token\d+/'
1177
			)),
1178
			'/div'
1179
		);
1180
		$this->assertTags($result, $expected);
1181
 
1182
		$result = $this->Form->input('UserForm.published', array('type' => 'text'));
1183
		$expected = array(
1184
			'div' => array('class' => 'input text'),
1185
			'label' => array('for' => 'UserFormPublished'),
1186
			'Published',
1187
			'/label',
1188
			array('input' => array(
1189
				'type' => 'text', 'name' => 'data[UserForm][published]',
1190
				'id' => 'UserFormPublished'
1191
			)),
1192
			'/div'
1193
		);
1194
		$this->assertTags($result, $expected);
1195
 
1196
		$result = $this->Form->input('UserForm.other', array('type' => 'text'));
1197
		$expected = array(
1198
			'div' => array('class' => 'input text'),
1199
			'label' => array('for' => 'UserFormOther'),
1200
			'Other',
1201
			'/label',
1202
			array('input' => array(
1203
				'type' => 'text', 'name' => 'data[UserForm][other]',
1204
				'id' => 'UserFormOther'
1205
			)),
1206
			'/div'
1207
		);
1208
		$this->assertTags($result, $expected);
1209
 
1210
		$result = $this->Form->hidden('UserForm.stuff');
1211
		$expected = array(
1212
			'input' => array(
1213
				'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
1214
				'id' => 'UserFormStuff'
1215
		));
1216
		$this->assertTags($result, $expected);
1217
 
1218
		$result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
1219
		$expected = array('input' => array(
1220
			'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
1221
			'value' => '0', 'id' => 'UserFormHidden'
1222
		));
1223
		$this->assertTags($result, $expected);
1224
 
1225
		$result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
1226
		$expected = array(
1227
			'div' => array('class' => 'input checkbox'),
1228
			array('input' => array(
1229
				'type' => 'hidden', 'name' => 'data[UserForm][something]',
1230
				'value' => '0', 'id' => 'UserFormSomething_'
1231
			)),
1232
			array('input' => array(
1233
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
1234
				'value' => '1', 'id' => 'UserFormSomething'
1235
			)),
1236
			'label' => array('for' => 'UserFormSomething'),
1237
			'Something',
1238
			'/label',
1239
			'/div'
1240
		);
1241
		$this->assertTags($result, $expected);
1242
 
1243
		$result = $this->Form->fields;
1244
		$expected = array(
1245
			'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
1246
			'UserForm.hidden' => '0', 'UserForm.something'
1247
		);
1248
		$this->assertEquals($expected, $result);
1249
 
1250
		$hash = '6014b4e1c4f39eb62389712111dbe6435bec66cb%3AUserForm.hidden%7CUserForm.stuff';
1251
 
1252
		$result = $this->Form->secure($this->Form->fields);
1253
		$expected = array(
1254
			'div' => array('style' => 'display:none;'),
1255
			array('input' => array(
1256
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1257
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1258
			)),
1259
			array('input' => array(
1260
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1261
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
1262
			)),
1263
			'/div'
1264
		);
1265
		$this->assertTags($result, $expected);
1266
	}
1267
 
1268
/**
1269
 * Test secured inputs with custom names.
1270
 *
1271
 * @return void
1272
 */
1273
	public function testSecuredInputCustomName() {
1274
		$this->Form->request['_Token'] = array('key' => 'testKey');
1275
		$this->assertEquals(array(), $this->Form->fields);
1276
 
1277
		$this->Form->input('text_input', array(
1278
			'name' => 'data[Option][General.default_role]',
1279
		));
1280
		$expected = array('Option.General.default_role');
1281
		$this->assertEquals($expected, $this->Form->fields);
1282
 
1283
		$this->Form->input('select_box', array(
1284
			'name' => 'data[Option][General.select_role]',
1285
			'type' => 'select',
1286
			'options' => array(1, 2),
1287
		));
1288
		$expected = array('Option.General.default_role', 'Option.General.select_role');
1289
		$this->assertEquals($expected, $this->Form->fields);
1290
	}
1291
 
1292
/**
1293
 * Tests that the correct keys are added to the field hash index
1294
 *
1295
 * @return void
1296
 */
1297
	public function testSecuredFileInput() {
1298
		$this->Form->request['_Token'] = array('key' => 'testKey');
1299
		$this->assertEquals(array(), $this->Form->fields);
1300
 
1301
		$this->Form->file('Attachment.file');
1302
		$expected = array(
1303
			'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
1304
			'Attachment.file.error', 'Attachment.file.size'
1305
		);
1306
		$this->assertEquals($expected, $this->Form->fields);
1307
	}
1308
 
1309
/**
1310
 * test that multiple selects keys are added to field hash
1311
 *
1312
 * @return void
1313
 */
1314
	public function testSecuredMultipleSelect() {
1315
		$this->Form->request['_Token'] = array('key' => 'testKey');
1316
		$this->assertEquals(array(), $this->Form->fields);
1317
		$options = array('1' => 'one', '2' => 'two');
1318
 
1319
		$this->Form->select('Model.select', $options);
1320
		$expected = array('Model.select');
1321
		$this->assertEquals($expected, $this->Form->fields);
1322
 
1323
		$this->Form->fields = array();
1324
		$this->Form->select('Model.select', $options, array('multiple' => true));
1325
		$this->assertEquals($expected, $this->Form->fields);
1326
	}
1327
 
1328
/**
1329
 * testFormSecuredRadio method
1330
 *
1331
 * @return void
1332
 */
1333
	public function testSecuredRadio() {
1334
		$this->Form->request['_Token'] = array('key' => 'testKey');
1335
		$this->assertEquals(array(), $this->Form->fields);
1336
		$options = array('1' => 'option1', '2' => 'option2');
1337
 
1338
		$this->Form->radio('Test.test', $options);
1339
		$expected = array('Test.test');
1340
		$this->assertEquals($expected, $this->Form->fields);
1341
	}
1342
 
1343
/**
1344
 * Test that when disabled is in a list based attribute array it works.
1345
 *
1346
 * @return void
1347
 */
1348
	public function testSecuredAndDisabledNotAssoc() {
1349
		$this->Form->request['_Token'] = array('key' => 'testKey');
1350
 
1351
		$this->Form->select('Model.select', array(1, 2), array('disabled'));
1352
		$this->Form->checkbox('Model.checkbox', array('disabled'));
1353
		$this->Form->text('Model.text', array('disabled'));
1354
		$this->Form->textarea('Model.textarea', array('disabled'));
1355
		$this->Form->password('Model.password', array('disabled'));
1356
		$this->Form->radio('Model.radio', array(1, 2), array('disabled'));
1357
 
1358
		$expected = array(
1359
			'Model.radio' => ''
1360
		);
1361
		$this->assertEquals($expected, $this->Form->fields);
1362
	}
1363
 
1364
/**
1365
 * test that forms with disabled inputs + secured forms leave off the inputs from the form
1366
 * hashing.
1367
 *
1368
 * @return void
1369
 */
1370
	public function testSecuredAndDisabled() {
1371
		$this->Form->request['_Token'] = array('key' => 'testKey');
1372
 
1373
		$this->Form->checkbox('Model.checkbox', array('disabled' => true));
1374
		$this->Form->text('Model.text', array('disabled' => true));
1375
		$this->Form->password('Model.text', array('disabled' => true));
1376
		$this->Form->textarea('Model.textarea', array('disabled' => true));
1377
		$this->Form->select('Model.select', array(1, 2), array('disabled' => true));
1378
		$this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
1379
		$this->Form->year('Model.year', null, null, array('disabled' => true));
1380
		$this->Form->month('Model.month', array('disabled' => true));
1381
		$this->Form->day('Model.day', array('disabled' => true));
1382
		$this->Form->hour('Model.hour', false, array('disabled' => true));
1383
		$this->Form->minute('Model.minute', array('disabled' => true));
1384
		$this->Form->meridian('Model.meridian', array('disabled' => true));
1385
 
1386
		$expected = array(
1387
			'Model.radio' => ''
1388
		);
1389
		$this->assertEquals($expected, $this->Form->fields);
1390
	}
1391
 
1392
/**
1393
 * Test that only the path + query elements of a form's URL show up in their hash.
1394
 *
1395
 * @return void
1396
 */
1397
	public function testSecuredFormUrlIgnoresHost() {
1398
		$this->Form->request['_Token'] = array('key' => 'testKey');
1399
 
1400
		$expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
1401
		$this->Form->create('Address', array(
1402
			'url' => array('controller' => 'articles', 'action' => 'view', 1, '?' => array('page' => 1))
1403
		));
1404
		$result = $this->Form->secure();
1405
		$this->assertContains($expected, $result);
1406
 
1407
		$this->Form->create('Address', array('url' => 'http://localhost/articles/view/1?page=1'));
1408
		$result = $this->Form->secure();
1409
		$this->assertContains($expected, $result, 'Full URL should only use path and query.');
1410
 
1411
		$this->Form->create('Address', array('url' => '/articles/view/1?page=1'));
1412
		$result = $this->Form->secure();
1413
		$this->assertContains($expected, $result, 'URL path + query should work.');
1414
 
1415
		$this->Form->create('Address', array('url' => '/articles/view/1'));
1416
		$result = $this->Form->secure();
1417
		$this->assertNotContains($expected, $result, 'URL is different');
1418
	}
1419
 
1420
/**
1421
 * Ensure named parameters work correctly with hash generation.
1422
 *
1423
 * @return void
1424
 */
1425
	public function testSecuredFormUrlWorksWithNamedParameter() {
1426
		$this->Form->request['_Token'] = array('key' => 'testKey');
1427
 
1428
		$expected = 'c890c5f041b1d83d1610dee8f52cd257df7ce618%3A';
1429
		$this->Form->create('Address', array(
1430
			'url' => array('controller' => 'articles', 'action' => 'view', 1, 'type' => 'red')
1431
		));
1432
		$result = $this->Form->secure();
1433
		$this->assertContains($expected, $result);
1434
	}
1435
 
1436
/**
1437
 * Test that URL, HTML and identifier show up in their hashs.
1438
 *
1439
 * @return void
1440
 */
1441
	public function testSecuredFormUrlHasHtmlAndIdentifier() {
1442
		$this->Form->request['_Token'] = array('key' => 'testKey');
1443
 
1444
		$expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
1445
		$this->Form->create('Address', array(
1446
			'url' => array(
1447
				'controller' => 'articles',
1448
				'action' => 'view',
1449
				'?' => array(
1450
					'page' => 1,
1451
					'limit' => 10,
1452
					'html' => '<>"',
1453
				),
1454
				'#' => 'result',
1455
			),
1456
		));
1457
		$result = $this->Form->secure();
1458
		$this->assertContains($expected, $result);
1459
 
1460
		$this->Form->create('Address', array('url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'));
1461
		$result = $this->Form->secure();
1462
		$this->assertContains($expected, $result, 'Full URL should only use path and query.');
1463
 
1464
		$this->Form->create('Address', array('url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'));
1465
		$result = $this->Form->secure();
1466
		$this->assertContains($expected, $result, 'URL path + query should work.');
1467
	}
1468
 
1469
/**
1470
 * testDisableSecurityUsingForm method
1471
 *
1472
 * @return void
1473
 */
1474
	public function testDisableSecurityUsingForm() {
1475
		$this->Form->request['_Token'] = array(
1476
			'key' => 'testKey',
1477
			'disabledFields' => array()
1478
		);
1479
		$this->Form->create();
1480
 
1481
		$this->Form->hidden('Addresses.id', array('value' => '123456'));
1482
		$this->Form->input('Addresses.title');
1483
		$this->Form->input('Addresses.first_name', array('secure' => false));
1484
		$this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
1485
		$this->Form->input('Addresses.zip', array(
1486
			'type' => 'select', 'options' => array(1, 2), 'secure' => false
1487
		));
1488
 
1489
		$result = $this->Form->fields;
1490
		$expected = array(
1491
			'Addresses.id' => '123456', 'Addresses.title',
1492
		);
1493
		$this->assertEquals($expected, $result);
1494
	}
1495
 
1496
/**
1497
 * test disableField
1498
 *
1499
 * @return void
1500
 */
1501
	public function testUnlockFieldAddsToList() {
1502
		$this->Form->request['_Token'] = array(
1503
			'key' => 'testKey',
1504
			'unlockedFields' => array()
1505
		);
1506
		$this->Form->create('Contact');
1507
		$this->Form->unlockField('Contact.name');
1508
		$this->Form->text('Contact.name');
1509
 
1510
		$this->assertEquals(array('Contact.name'), $this->Form->unlockField());
1511
		$this->assertEquals(array(), $this->Form->fields);
1512
	}
1513
 
1514
/**
1515
 * test unlockField removing from fields array.
1516
 *
1517
 * @return void
1518
 */
1519
	public function testUnlockFieldRemovingFromFields() {
1520
		$this->Form->request['_Token'] = array(
1521
			'key' => 'testKey',
1522
			'unlockedFields' => array()
1523
		);
1524
		$this->Form->create('Contact');
1525
		$this->Form->hidden('Contact.id', array('value' => 1));
1526
		$this->Form->text('Contact.name');
1527
 
1528
		$this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
1529
		$this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
1530
 
1531
		$this->Form->unlockField('Contact.name');
1532
		$this->Form->unlockField('Contact.id');
1533
		$this->assertEquals(array(), $this->Form->fields);
1534
	}
1535
 
1536
/**
1537
 * testTagIsInvalid method
1538
 *
1539
 * @return void
1540
 */
1541
	public function testTagIsInvalid() {
1542
		$Contact = ClassRegistry::getObject('Contact');
1543
		$Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
1544
 
1545
		$this->Form->setEntity('Contact.0.email');
1546
		$result = $this->Form->tagIsInvalid();
1547
		$this->assertEquals($expected, $result);
1548
 
1549
		$this->Form->setEntity('Contact.1.email');
1550
		$result = $this->Form->tagIsInvalid();
1551
		$this->assertFalse($result);
1552
 
1553
		$this->Form->setEntity('Contact.0.name');
1554
		$result = $this->Form->tagIsInvalid();
1555
		$this->assertFalse($result);
1556
	}
1557
 
1558
/**
1559
 * Test tagIsInvalid with validation errors from a saveMany
1560
 *
1561
 * @return void
1562
 */
1563
	public function testTagIsInvalidSaveMany() {
1564
		$Contact = ClassRegistry::getObject('Contact');
1565
		$Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
1566
 
1567
		$this->Form->create('Contact');
1568
 
1569
		$this->Form->setEntity('0.email');
1570
		$result = $this->Form->tagIsInvalid();
1571
		$this->assertEquals($expected, $result);
1572
 
1573
		$this->Form->setEntity('0.Contact.email');
1574
		$result = $this->Form->tagIsInvalid();
1575
		$this->assertEquals($expected, $result);
1576
	}
1577
 
1578
/**
1579
 * Test validation errors.
1580
 *
1581
 * @return void
1582
 */
1583
	public function testPasswordValidation() {
1584
		$Contact = ClassRegistry::getObject('Contact');
1585
		$Contact->validationErrors['password'] = array('Please provide a password');
1586
 
1587
		$result = $this->Form->input('Contact.password');
1588
		$expected = array(
1589
			'div' => array('class' => 'input password error'),
1590
			'label' => array('for' => 'ContactPassword'),
1591
			'Password',
1592
			'/label',
1593
			'input' => array(
1594
				'type' => 'password', 'name' => 'data[Contact][password]',
1595
				'id' => 'ContactPassword', 'class' => 'form-error'
1596
			),
1597
			array('div' => array('class' => 'error-message')),
1598
			'Please provide a password',
1599
			'/div',
1600
			'/div'
1601
		);
1602
		$this->assertTags($result, $expected);
1603
 
1604
		$result = $this->Form->input('Contact.password', array('errorMessage' => false));
1605
		$expected = array(
1606
			'div' => array('class' => 'input password error'),
1607
			'label' => array('for' => 'ContactPassword'),
1608
			'Password',
1609
			'/label',
1610
			'input' => array(
1611
				'type' => 'password', 'name' => 'data[Contact][password]',
1612
				'id' => 'ContactPassword', 'class' => 'form-error'
1613
			),
1614
			'/div'
1615
		);
1616
		$this->assertTags($result, $expected);
1617
	}
1618
 
1619
/**
1620
 * Test validation errors, when validation message is an empty string.
1621
 *
1622
 * @return void
1623
 */
1624
	public function testEmptyErrorValidation() {
1625
		$this->Form->validationErrors['Contact']['password'] = '';
1626
 
1627
		$result = $this->Form->input('Contact.password');
1628
		$expected = array(
1629
			'div' => array('class' => 'input password error'),
1630
			'label' => array('for' => 'ContactPassword'),
1631
			'Password',
1632
			'/label',
1633
			'input' => array(
1634
				'type' => 'password', 'name' => 'data[Contact][password]',
1635
				'id' => 'ContactPassword', 'class' => 'form-error'
1636
			),
1637
			array('div' => array('class' => 'error-message')),
1638
			array(),
1639
			'/div',
1640
			'/div'
1641
		);
1642
		$this->assertTags($result, $expected);
1643
 
1644
		$result = $this->Form->input('Contact.password', array('errorMessage' => false));
1645
		$expected = array(
1646
			'div' => array('class' => 'input password error'),
1647
			'label' => array('for' => 'ContactPassword'),
1648
			'Password',
1649
			'/label',
1650
			'input' => array(
1651
				'type' => 'password', 'name' => 'data[Contact][password]',
1652
				'id' => 'ContactPassword', 'class' => 'form-error'
1653
			),
1654
			'/div'
1655
		);
1656
		$this->assertTags($result, $expected);
1657
	}
1658
 
1659
/**
1660
 * Test validation errors, when calling input() overriding validation message by an empty string.
1661
 *
1662
 * @return void
1663
 */
1664
	public function testEmptyInputErrorValidation() {
1665
		$this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
1666
 
1667
		$result = $this->Form->input('Contact.password', array('error' => ''));
1668
		$expected = array(
1669
			'div' => array('class' => 'input password error'),
1670
			'label' => array('for' => 'ContactPassword'),
1671
			'Password',
1672
			'/label',
1673
			'input' => array(
1674
				'type' => 'password', 'name' => 'data[Contact][password]',
1675
				'id' => 'ContactPassword', 'class' => 'form-error'
1676
			),
1677
			array('div' => array('class' => 'error-message')),
1678
			array(),
1679
			'/div',
1680
			'/div'
1681
		);
1682
		$this->assertTags($result, $expected);
1683
 
1684
		$result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
1685
		$expected = array(
1686
			'div' => array('class' => 'input password error'),
1687
			'label' => array('for' => 'ContactPassword'),
1688
			'Password',
1689
			'/label',
1690
			'input' => array(
1691
				'type' => 'password', 'name' => 'data[Contact][password]',
1692
				'id' => 'ContactPassword', 'class' => 'form-error'
1693
			),
1694
			'/div'
1695
		);
1696
		$this->assertTags($result, $expected);
1697
	}
1698
 
1699
/**
1700
 * testFormValidationAssociated method
1701
 *
1702
 * test display of form errors in conjunction with model::validates.
1703
 *
1704
 * @return void
1705
 */
1706
	public function testFormValidationAssociated() {
1707
		$this->UserForm = ClassRegistry::getObject('UserForm');
1708
		$this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
1709
 
1710
		$data = array(
1711
			'UserForm' => array('name' => 'user'),
1712
			'OpenidUrl' => array('url' => 'http://www.cakephp.org')
1713
		);
1714
 
1715
		$result = $this->UserForm->OpenidUrl->create($data);
1716
		$this->assertFalse(empty($result));
1717
		$this->assertFalse($this->UserForm->OpenidUrl->validates());
1718
 
1719
		$result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
1720
		$encoding = strtolower(Configure::read('App.encoding'));
1721
		$expected = array(
1722
			'form' => array(
1723
				'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
1724
				'accept-charset' => $encoding
1725
			),
1726
			'div' => array('style' => 'display:none;'),
1727
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1728
			'/div'
1729
		);
1730
		$this->assertTags($result, $expected);
1731
 
1732
		$result = $this->Form->error(
1733
			'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
1734
		);
1735
		$this->assertEquals('Error, not registered', $result);
1736
 
1737
		unset($this->UserForm->OpenidUrl, $this->UserForm);
1738
	}
1739
 
1740
/**
1741
 * testFormValidationAssociatedFirstLevel method
1742
 *
1743
 * test form error display with associated model.
1744
 *
1745
 * @return void
1746
 */
1747
	public function testFormValidationAssociatedFirstLevel() {
1748
		$this->ValidateUser = ClassRegistry::getObject('ValidateUser');
1749
		$this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
1750
 
1751
		$data = array(
1752
			'ValidateUser' => array('name' => 'mariano'),
1753
			'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
1754
		);
1755
 
1756
		$result = $this->ValidateUser->create($data);
1757
		$this->assertFalse(empty($result));
1758
		$this->assertFalse($this->ValidateUser->validates());
1759
		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1760
 
1761
		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1762
		$encoding = strtolower(Configure::read('App.encoding'));
1763
		$expected = array(
1764
			'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
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
			'ValidateUser.email', 'Invalid email', array('wrap' => false)
1773
		);
1774
		$this->assertEquals('Invalid email', $result);
1775
 
1776
		$result = $this->Form->error(
1777
			'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
1778
		);
1779
		$this->assertEquals('Invalid name', $result);
1780
 
1781
		$result = $this->Form->error(
1782
			'ValidateProfile.city', 'Invalid city', array('wrap' => false)
1783
		);
1784
		$this->assertEquals('Invalid city', $result);
1785
 
1786
		unset($this->ValidateUser->ValidateProfile);
1787
		unset($this->ValidateUser);
1788
	}
1789
 
1790
/**
1791
 * testFormValidationAssociatedSecondLevel method
1792
 *
1793
 * test form error display with associated model.
1794
 *
1795
 * @return void
1796
 */
1797
	public function testFormValidationAssociatedSecondLevel() {
1798
		$this->ValidateUser = ClassRegistry::getObject('ValidateUser');
1799
		$this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
1800
		$this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
1801
 
1802
		$data = array(
1803
			'ValidateUser' => array('name' => 'mariano'),
1804
			'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
1805
			'ValidateItem' => array('name' => 'Item')
1806
		);
1807
 
1808
		$result = $this->ValidateUser->create($data);
1809
		$this->assertFalse(empty($result));
1810
		$this->assertFalse($this->ValidateUser->validates());
1811
		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1812
		$this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
1813
 
1814
		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1815
		$encoding = strtolower(Configure::read('App.encoding'));
1816
		$expected = array(
1817
			'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
1818
			'div' => array('style' => 'display:none;'),
1819
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1820
			'/div'
1821
		);
1822
		$this->assertTags($result, $expected);
1823
 
1824
		$result = $this->Form->error(
1825
			'ValidateUser.email', 'Invalid email', array('wrap' => false)
1826
		);
1827
		$this->assertEquals('Invalid email', $result);
1828
 
1829
		$result = $this->Form->error(
1830
			'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
1831
		);
1832
		$this->assertEquals('Invalid name', $result);
1833
 
1834
		$result = $this->Form->error(
1835
			'ValidateProfile.city', 'Invalid city', array('wrap' => false)
1836
		);
1837
 
1838
		$result = $this->Form->error(
1839
			'ValidateItem.description', 'Invalid description', array('wrap' => false)
1840
		);
1841
		$this->assertEquals('Invalid description', $result);
1842
 
1843
		unset($this->ValidateUser->ValidateProfile->ValidateItem);
1844
		unset($this->ValidateUser->ValidateProfile);
1845
		unset($this->ValidateUser);
1846
	}
1847
 
1848
/**
1849
 * testFormValidationMultiRecord method
1850
 *
1851
 * test form error display with multiple records.
1852
 *
1853
 * @return void
1854
 */
1855
	public function testFormValidationMultiRecord() {
1856
		$Contact = ClassRegistry::getObject('Contact');
1857
		$Contact->validationErrors[2] = array(
1858
			'name' => array('This field cannot be left blank')
1859
		);
1860
		$result = $this->Form->input('Contact.2.name');
1861
		$expected = array(
1862
			'div' => array('class' => 'input text error'),
1863
			'label' => array('for' => 'Contact2Name'),
1864
			'Name',
1865
			'/label',
1866
			'input' => array(
1867
				'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
1868
				'class' => 'form-error', 'maxlength' => 255
1869
			),
1870
			array('div' => array('class' => 'error-message')),
1871
			'This field cannot be left blank',
1872
			'/div',
1873
			'/div'
1874
		);
1875
		$this->assertTags($result, $expected);
1876
	}
1877
 
1878
/**
1879
 * testMultipleInputValidation method
1880
 *
1881
 * test multiple record form validation error display.
1882
 *
1883
 * @return void
1884
 */
1885
	public function testMultipleInputValidation() {
1886
		$Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
1887
		$Address->validationErrors[0] = array(
1888
			'title' => array('This field cannot be empty'),
1889
			'first_name' => array('This field cannot be empty')
1890
		);
1891
		$Address->validationErrors[1] = array(
1892
			'last_name' => array('You must have a last name')
1893
		);
1894
		$this->Form->create();
1895
 
1896
		$result = $this->Form->input('Address.0.title');
1897
		$expected = array(
1898
			'div' => array('class'),
1899
			'label' => array('for'),
1900
			'preg:/[^<]+/',
1901
			'/label',
1902
			'input' => array(
1903
				'type' => 'text', 'name', 'id', 'class' => 'form-error'
1904
			),
1905
			array('div' => array('class' => 'error-message')),
1906
			'This field cannot be empty',
1907
			'/div',
1908
			'/div'
1909
		);
1910
		$this->assertTags($result, $expected);
1911
 
1912
		$result = $this->Form->input('Address.0.first_name');
1913
		$expected = array(
1914
			'div' => array('class'),
1915
			'label' => array('for'),
1916
			'preg:/[^<]+/',
1917
			'/label',
1918
			'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
1919
			array('div' => array('class' => 'error-message')),
1920
			'This field cannot be empty',
1921
			'/div',
1922
			'/div'
1923
		);
1924
		$this->assertTags($result, $expected);
1925
 
1926
		$result = $this->Form->input('Address.0.last_name');
1927
		$expected = array(
1928
			'div' => array('class'),
1929
			'label' => array('for'),
1930
			'preg:/[^<]+/',
1931
			'/label',
1932
			'input' => array('type' => 'text', 'name', 'id'),
1933
			'/div'
1934
		);
1935
		$this->assertTags($result, $expected);
1936
 
1937
		$result = $this->Form->input('Address.1.last_name');
1938
		$expected = array(
1939
			'div' => array('class'),
1940
			'label' => array('for'),
1941
			'preg:/[^<]+/',
1942
			'/label',
1943
			'input' => array(
1944
				'type' => 'text', 'name', 'id',
1945
				'class' => 'form-error'
1946
			),
1947
			array('div' => array('class' => 'error-message')),
1948
			'You must have a last name',
1949
			'/div',
1950
			'/div'
1951
		);
1952
		$this->assertTags($result, $expected);
1953
	}
1954
 
1955
/**
1956
 * testInput method
1957
 *
1958
 * Test various incarnations of input().
1959
 *
1960
 * @return void
1961
 */
1962
	public function testInput() {
1963
		$result = $this->Form->input('ValidateUser.balance');
1964
		$expected = array(
1965
			'div' => array('class'),
1966
			'label' => array('for'),
1967
			'Balance',
1968
			'/label',
1969
			'input' => array('name', 'type' => 'number', 'id', 'step'),
1970
			'/div',
1971
		);
1972
		$this->assertTags($result, $expected);
1973
 
1974
		$result = $this->Form->input('ValidateUser.cost_decimal');
1975
		$expected = array(
1976
			'div' => array('class'),
1977
			'label' => array('for'),
1978
			'Cost Decimal',
1979
			'/label',
1980
			'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
1981
			'/div',
1982
		);
1983
		$this->assertTags($result, $expected);
1984
 
1985
		$result = $this->Form->input('ValidateUser.ratio');
1986
		$expected = array(
1987
			'div' => array('class'),
1988
			'label' => array('for'),
1989
			'Ratio',
1990
			'/label',
1991
			'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
1992
			'/div',
1993
		);
1994
		$this->assertTags($result, $expected);
1995
 
1996
		$result = $this->Form->input('ValidateUser.population');
1997
		$expected = array(
1998
			'div' => array('class'),
1999
			'label' => array('for'),
2000
			'Population',
2001
			'/label',
2002
			'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
2003
			'/div',
2004
		);
2005
		$this->assertTags($result, $expected);
2006
 
2007
		$result = $this->Form->input('Contact.email', array('id' => 'custom'));
2008
		$expected = array(
2009
			'div' => array('class' => 'input email'),
2010
			'label' => array('for' => 'custom'),
2011
			'Email',
2012
			'/label',
2013
			array('input' => array(
2014
				'type' => 'email', 'name' => 'data[Contact][email]',
2015
				'id' => 'custom', 'maxlength' => 255
2016
			)),
2017
			'/div'
2018
		);
2019
		$this->assertTags($result, $expected);
2020
 
2021
		$result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
2022
		$expected = array(
2023
			'<div',
2024
			'label' => array('for' => 'ContactEmail'),
2025
			'Email',
2026
			'/label',
2027
			array('input' => array(
2028
				'type' => 'email', 'name' => 'data[Contact][email]',
2029
				'id' => 'ContactEmail', 'maxlength' => 255
2030
			)),
2031
			'/div'
2032
		);
2033
		$this->assertTags($result, $expected);
2034
 
2035
		$result = $this->Form->hidden('Contact.idontexist');
2036
		$expected = array('input' => array(
2037
				'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
2038
				'id' => 'ContactIdontexist'
2039
		));
2040
		$this->assertTags($result, $expected);
2041
 
2042
		$result = $this->Form->input('Contact.email', array('type' => 'text'));
2043
		$expected = array(
2044
			'div' => array('class' => 'input text'),
2045
			'label' => array('for' => 'ContactEmail'),
2046
			'Email',
2047
			'/label',
2048
			array('input' => array(
2049
				'type' => 'text', 'name' => 'data[Contact][email]',
2050
				'id' => 'ContactEmail'
2051
			)),
2052
			'/div'
2053
		);
2054
		$this->assertTags($result, $expected);
2055
 
2056
		$result = $this->Form->input('Contact.5.email', array('type' => 'text'));
2057
		$expected = array(
2058
			'div' => array('class' => 'input text'),
2059
			'label' => array('for' => 'Contact5Email'),
2060
			'Email',
2061
			'/label',
2062
			array('input' => array(
2063
				'type' => 'text', 'name' => 'data[Contact][5][email]',
2064
				'id' => 'Contact5Email'
2065
			)),
2066
			'/div'
2067
		);
2068
		$this->assertTags($result, $expected);
2069
 
2070
		$result = $this->Form->input('Contact.password');
2071
		$expected = array(
2072
			'div' => array('class' => 'input password'),
2073
			'label' => array('for' => 'ContactPassword'),
2074
			'Password',
2075
			'/label',
2076
			array('input' => array(
2077
				'type' => 'password', 'name' => 'data[Contact][password]',
2078
				'id' => 'ContactPassword'
2079
			)),
2080
			'/div'
2081
		);
2082
		$this->assertTags($result, $expected);
2083
 
2084
		$result = $this->Form->input('Contact.email', array(
2085
			'type' => 'file', 'class' => 'textbox'
2086
		));
2087
		$expected = array(
2088
			'div' => array('class' => 'input file'),
2089
			'label' => array('for' => 'ContactEmail'),
2090
			'Email',
2091
			'/label',
2092
			array('input' => array(
2093
				'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
2094
				'id' => 'ContactEmail'
2095
			)),
2096
			'/div'
2097
		);
2098
		$this->assertTags($result, $expected);
2099
 
2100
		$this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
2101
		$result = $this->Form->input('Contact.phone');
2102
		$expected = array(
2103
			'div' => array('class' => 'input tel'),
2104
			'label' => array('for' => 'ContactPhone'),
2105
			'Phone',
2106
			'/label',
2107
			array('input' => array(
2108
				'type' => 'tel', 'name' => 'data[Contact][phone]',
2109
				'value' => 'Hello &amp; World &gt; weird chars',
2110
				'id' => 'ContactPhone', 'maxlength' => 255
2111
			)),
2112
			'/div'
2113
		);
2114
		$this->assertTags($result, $expected);
2115
 
2116
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
2117
		$result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
2118
		$expected = array(
2119
			'div' => array('class' => 'input text'),
2120
			'label' => array('for' => 'myId'),
2121
			'Field',
2122
			'/label',
2123
			'input' => array(
2124
				'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
2125
				'value' => 'My value', 'id' => 'myId'
2126
			),
2127
			'/div'
2128
		);
2129
		$this->assertTags($result, $expected);
2130
 
2131
		unset($this->Form->request->data);
2132
 
2133
		$Contact = ClassRegistry::getObject('Contact');
2134
		$Contact->validationErrors['field'] = array('Badness!');
2135
		$result = $this->Form->input('Contact.field');
2136
		$expected = array(
2137
			'div' => array('class' => 'input text error'),
2138
			'label' => array('for' => 'ContactField'),
2139
			'Field',
2140
			'/label',
2141
			'input' => array(
2142
				'type' => 'text', 'name' => 'data[Contact][field]',
2143
				'id' => 'ContactField', 'class' => 'form-error'
2144
			),
2145
			array('div' => array('class' => 'error-message')),
2146
			'Badness!',
2147
			'/div',
2148
			'/div'
2149
		);
2150
		$this->assertTags($result, $expected);
2151
 
2152
		$result = $this->Form->input('Contact.field', array(
2153
			'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
2154
		));
2155
		$expected = array(
2156
			'label' => array('for' => 'ContactField'),
2157
			'Field',
2158
			'/label',
2159
			'input' => array(
2160
				'type' => 'text', 'name' => 'data[Contact][field]',
2161
				'id' => 'ContactField', 'class' => 'form-error'
2162
			),
2163
			array('span' => array('class' => 'error-message')),
2164
			'Badness!',
2165
			'/span'
2166
		);
2167
		$this->assertTags($result, $expected);
2168
 
2169
		$result = $this->Form->input('Contact.field', array(
2170
			'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
2171
		));
2172
		$expected = array(
2173
			'div' => array('class' => 'input text error'),
2174
			'label' => array('for' => 'ContactField'),
2175
			'Field',
2176
			'/label',
2177
			'input' => array(
2178
				'type' => 'text', 'name' => 'data[Contact][field]',
2179
				'id' => 'ContactField', 'class' => 'form-error'
2180
			),
2181
			array('div' => array('class' => 'error')),
2182
			'Badness!',
2183
			'/div'
2184
		);
2185
		$this->assertTags($result, $expected);
2186
 
2187
		$result = $this->Form->input('Contact.field', array(
2188
			'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
2189
		));
2190
		$expected = array(
2191
			'span' => array('class' => 'input text error'),
2192
			'label' => array('for' => 'ContactField'),
2193
			'Field',
2194
			'/label',
2195
			'input' => array(
2196
				'type' => 'text', 'name' => 'data[Contact][field]',
2197
				'id' => 'ContactField', 'class' => 'form-error'
2198
			),
2199
			'Badness!',
2200
			'/span'
2201
		);
2202
		$this->assertTags($result, $expected);
2203
 
2204
		$result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
2205
		$expected = array(
2206
			'div' => array('class' => 'input text error'),
2207
			'label' => array('for' => 'ContactField'),
2208
			'Field',
2209
			'/label',
2210
			'input' => array(
2211
				'type' => 'text', 'name' => 'data[Contact][field]',
2212
				'id' => 'ContactField', 'class' => 'form-error'
2213
			),
2214
			'A message to you, Rudy',
2215
			array('div' => array('class' => 'error-message')),
2216
			'Badness!',
2217
			'/div',
2218
			'/div'
2219
		);
2220
		$this->assertTags($result, $expected);
2221
 
2222
		$this->Form->setEntity(null);
2223
		$this->Form->setEntity('Contact.field');
2224
		$result = $this->Form->input('Contact.field', array(
2225
			'after' => 'A message to you, Rudy', 'error' => false
2226
		));
2227
		$expected = array(
2228
			'div' => array('class' => 'input text'),
2229
			'label' => array('for' => 'ContactField'),
2230
			'Field',
2231
			'/label',
2232
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2233
			'A message to you, Rudy',
2234
			'/div'
2235
		);
2236
		$this->assertTags($result, $expected);
2237
 
2238
		$result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
2239
		$expected = array(
2240
			'div' => array('class' => 'input text'),
2241
			'label' => array('for' => 'ObjectField'),
2242
			'Field',
2243
			'/label',
2244
			'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
2245
			'A message to you, Rudy',
2246
			'/div'
2247
		);
2248
		$this->assertTags($result, $expected);
2249
 
2250
		$Contact->validationErrors['field'] = array('minLength');
2251
		$result = $this->Form->input('Contact.field', array(
2252
			'error' => array(
2253
				'minLength' => 'Le login doit contenir au moins 2 caractères',
2254
				'maxLength' => 'login too large'
2255
			)
2256
		));
2257
		$expected = array(
2258
			'div' => array('class' => 'input text error'),
2259
			'label' => array('for' => 'ContactField'),
2260
			'Field',
2261
			'/label',
2262
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2263
			array('div' => array('class' => 'error-message')),
2264
			'Le login doit contenir au moins 2 caractères',
2265
			'/div',
2266
			'/div'
2267
		);
2268
		$this->assertTags($result, $expected);
2269
 
2270
		$Contact->validationErrors['field'] = array('maxLength');
2271
		$result = $this->Form->input('Contact.field', array(
2272
			'error' => array(
2273
				'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
2274
				'minLength' => 'Le login doit contenir au moins 2 caractères',
2275
				'maxLength' => 'login too large',
2276
			)
2277
		));
2278
		$expected = array(
2279
			'div' => array('class' => 'input text error'),
2280
			'label' => array('for' => 'ContactField'),
2281
			'Field',
2282
			'/label',
2283
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2284
			array('span' => array('class' => 'error-message', 'rel' => 'fake')),
2285
			'login too large',
2286
			'/span',
2287
			'/div'
2288
		);
2289
		$this->assertTags($result, $expected);
2290
	}
2291
 
2292
/**
2293
 * Test that inputs with 0 can be created.
2294
 *
2295
 * @return void
2296
 */
2297
	public function testInputZero() {
2298
		$this->Form->create('User');
2299
		$result = $this->Form->input('0');
2300
		$expected = array(
2301
			'div' => array('class' => 'input text'),
2302
			'label' => array('for' => 'User0'), '/label',
2303
			'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
2304
			'/div'
2305
		);
2306
		$this->assertTags($result, $expected);
2307
	}
2308
 
2309
/**
2310
 * test input() with checkbox creation
2311
 *
2312
 * @return void
2313
 */
2314
	public function testInputCheckbox() {
2315
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
2316
		$expected = array(
2317
			'div' => array('class' => 'input checkbox'),
2318
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2319
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2320
			'/div'
2321
		);
2322
		$this->assertTags($result, $expected);
2323
 
2324
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
2325
		$expected = array(
2326
			'div' => array('class' => 'input checkbox'),
2327
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2328
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2329
			'/div'
2330
		);
2331
		$this->assertTags($result, $expected);
2332
 
2333
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
2334
		$expected = array(
2335
			'div' => array('class' => 'input checkbox'),
2336
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2337
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2338
			'/div'
2339
		);
2340
		$this->assertTags($result, $expected);
2341
 
2342
		$result = $this->Form->input('User.disabled', array(
2343
			'label' => 'Disabled',
2344
			'type' => 'checkbox',
2345
			'data-foo' => 'disabled'
2346
		));
2347
		$expected = array(
2348
			'div' => array('class' => 'input checkbox'),
2349
			'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
2350
			array('input' => array(
2351
				'type' => 'checkbox',
2352
				'name' => 'data[User][disabled]',
2353
				'value' => '1',
2354
				'id' => 'UserDisabled',
2355
				'data-foo' => 'disabled'
2356
			)),
2357
			'label' => array('for' => 'UserDisabled'),
2358
			'Disabled',
2359
			'/label',
2360
			'/div'
2361
		);
2362
		$this->assertTags($result, $expected);
2363
	}
2364
 
2365
/**
2366
 * test form->input() with time types.
2367
 *
2368
 * @return void
2369
 */
2370
	public function testInputTime() {
2371
		extract($this->dateRegex);
2372
		$result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
2373
		$result = explode(':', $result);
2374
		$this->assertRegExp('/option value="23"/', $result[0]);
2375
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2376
 
2377
		$result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
2378
		$result = explode(':', $result);
2379
		$this->assertRegExp('/option value="23"/', $result[0]);
2380
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2381
 
2382
		$result = $this->Form->input('Model.field', array(
2383
			'type' => 'time', 'timeFormat' => 24, 'interval' => 15
2384
		));
2385
		$result = explode(':', $result);
2386
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2387
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2388
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2389
 
2390
		$result = $this->Form->input('Model.field', array(
2391
			'type' => 'time', 'timeFormat' => 12, 'interval' => 15
2392
		));
2393
		$result = explode(':', $result);
2394
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2395
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2396
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2397
 
2398
		$result = $this->Form->input('prueba', array(
2399
			'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
2400
			'maxYear' => date('Y') + 1, 'interval' => 15
2401
		));
2402
		$result = explode(':', $result);
2403
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2404
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2405
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2406
		$this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
2407
 
2408
		$result = $this->Form->input('Random.start_time', array(
2409
			'type' => 'time',
2410
			'selected' => '18:15'
2411
		));
2412
		$this->assertContains('<option value="06" selected="selected">6</option>', $result);
2413
		$this->assertContains('<option value="15" selected="selected">15</option>', $result);
2414
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2415
 
2416
		$result = $this->Form->input('published', array('type' => 'time'));
2417
		$now = strtotime('now');
2418
		$this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
2419
 
2420
		$now = strtotime('2013-03-09 00:42:21');
2421
		$result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
2422
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2423
		$this->assertContains('<option value="42" selected="selected">42</option>', $result);
2424
	}
2425
 
2426
/**
2427
 * Test interval + selected near the hour roll over.
2428
 *
2429
 * @return void
2430
 */
2431
	public function testTimeSelectedWithInterval() {
2432
		$result = $this->Form->input('Model.start_time', array(
2433
			'type' => 'time',
2434
			'interval' => 15,
2435
			'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
2436
		));
2437
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2438
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2439
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2440
 
2441
		$result = $this->Form->input('Model.start_time', array(
2442
			'type' => 'time',
2443
			'interval' => 15,
2444
			'selected' => '2012-10-23 15:57:00'
2445
		));
2446
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2447
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2448
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2449
 
2450
		$result = $this->Form->input('Model.start_time', array(
2451
			'timeFormat' => 24,
2452
			'type' => 'time',
2453
			'interval' => 15,
2454
			'selected' => '15:57'
2455
		));
2456
		$this->assertContains('<option value="16" selected="selected">16</option>', $result);
2457
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2458
 
2459
		$result = $this->Form->input('Model.start_time', array(
2460
			'timeFormat' => 24,
2461
			'type' => 'time',
2462
			'interval' => 15,
2463
			'selected' => '23:57'
2464
		));
2465
		$this->assertContains('<option value="00" selected="selected">0</option>', $result);
2466
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2467
 
2468
		$result = $this->Form->input('Model.created', array(
2469
			'timeFormat' => 24,
2470
			'type' => 'datetime',
2471
			'interval' => 15,
2472
			'selected' => '2012-09-30 23:56'
2473
		));
2474
		$this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
2475
		$this->assertContains('<option value="10" selected="selected">October</option>', $result);
2476
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2477
		$this->assertContains('<option value="00" selected="selected">0</option>', $result);
2478
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2479
	}
2480
 
2481
/**
2482
 * Test time with selected values around 12:xx:xx
2483
 *
2484
 * @return void
2485
 */
2486
	public function testTimeSelectedWithIntervalTwelve() {
2487
		$result = $this->Form->input('Model.start_time', array(
2488
			'type' => 'time',
2489
			'timeFormat' => 12,
2490
			'interval' => 15,
2491
			'selected' => '00:00:00'
2492
		));
2493
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2494
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2495
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2496
 
2497
		$result = $this->Form->input('Model.start_time', array(
2498
			'type' => 'time',
2499
			'timeFormat' => 12,
2500
			'interval' => 15,
2501
			'selected' => '12:00:00'
2502
		));
2503
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2504
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2505
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2506
 
2507
		$result = $this->Form->input('Model.start_time', array(
2508
			'type' => 'time',
2509
			'timeFormat' => 12,
2510
			'interval' => 15,
2511
			'selected' => '12:15:00'
2512
		));
2513
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2514
		$this->assertContains('<option value="15" selected="selected">15</option>', $result);
2515
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2516
	}
2517
 
2518
/**
2519
 * Test interval & timeFormat = 12
2520
 *
2521
 * @return void
2522
 */
2523
	public function testInputTimeWithIntervalAnd12HourFormat() {
2524
		$result = $this->Form->input('Model.start_time', array(
2525
			'type' => 'time',
2526
			'timeFormat' => 12,
2527
			'interval' => 5,
2528
			'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
2529
		));
2530
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2531
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2532
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2533
 
2534
		$result = $this->Form->input('Model.start_time', array(
2535
			'type' => 'time',
2536
			'timeFormat' => '12',
2537
			'interval' => 5,
2538
			'selected' => '2013-04-19 16:30:00'
2539
		));
2540
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2541
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2542
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2543
 
2544
		$result = $this->Form->input('Model.start_time', array(
2545
			'type' => 'time',
2546
			'timeFormat' => '12',
2547
			'interval' => 10,
2548
			'selected' => '2013-05-19 00:33:00'
2549
		));
2550
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2551
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2552
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2553
 
2554
		$result = $this->Form->input('Model.start_time', array(
2555
			'type' => 'time',
2556
			'timeFormat' => '12',
2557
			'interval' => 10,
2558
			'selected' => '2013-05-19 13:33:00'
2559
		));
2560
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2561
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2562
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2563
 
2564
		$result = $this->Form->input('Model.start_time', array(
2565
			'type' => 'time',
2566
			'timeFormat' => '12',
2567
			'interval' => 10,
2568
			'selected' => '2013-05-19 01:33:00'
2569
		));
2570
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2571
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2572
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2573
	}
2574
 
2575
/**
2576
 * test form->input() with datetime, date and time types
2577
 *
2578
 * @return void
2579
 */
2580
	public function testInputDatetime() {
2581
		extract($this->dateRegex);
2582
		$result = $this->Form->input('prueba', array(
2583
			'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
2584
			'maxYear' => date('Y') + 1, 'interval' => 15
2585
		));
2586
		$result = explode(':', $result);
2587
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2588
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2589
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2590
		$this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
2591
 
2592
		//related to ticket #5013
2593
		$result = $this->Form->input('Contact.date', array(
2594
			'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
2595
		));
2596
		$this->assertRegExp('/class="customClass"/', $result);
2597
		$this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
2598
 
2599
		$result = $this->Form->input('Contact.date', array(
2600
			'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
2601
		));
2602
		$this->assertRegExp('/id="customIdDay"/', $result);
2603
		$this->assertRegExp('/id="customIdMonth"/', $result);
2604
		$this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
2605
 
2606
		$result = $this->Form->input('Model.field', array(
2607
			'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
2608
		));
2609
		$this->assertRegExp('/id="customIDDay"/', $result);
2610
		$this->assertRegExp('/id="customIDHour"/', $result);
2611
		$result = explode('</select><select', $result);
2612
		$result = explode(':', $result[1]);
2613
		$this->assertRegExp('/option value="23"/', $result[0]);
2614
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2615
 
2616
		$result = $this->Form->input('Model.field', array(
2617
			'type' => 'datetime', 'timeFormat' => 12
2618
		));
2619
		$result = explode('</select><select', $result);
2620
		$result = explode(':', $result[1]);
2621
		$this->assertRegExp('/option value="12"/', $result[0]);
2622
		$this->assertNotRegExp('/option value="13"/', $result[0]);
2623
 
2624
		$this->Form->request->data = array('Contact' => array('created' => null));
2625
		$result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
2626
		$expected = array(
2627
			'div' => array('class' => 'input date'),
2628
			'label' => array('for' => 'ContactCreatedMonth'),
2629
			'Created',
2630
			'/label',
2631
			array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
2632
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2633
			$monthsRegex,
2634
			'/select', '-',
2635
			array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
2636
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2637
			$daysRegex,
2638
			'/select', '-',
2639
			array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
2640
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2641
			$yearsRegex,
2642
			'/select',
2643
			'/div'
2644
		);
2645
		$this->assertTags($result, $expected);
2646
 
2647
		$this->Form->request->data = array('Contact' => array('created' => null));
2648
		$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
2649
		$this->assertRegExp('/for\="ContactCreatedHour"/', $result);
2650
 
2651
		$this->Form->request->data = array('Contact' => array('created' => null));
2652
		$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
2653
		$this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
2654
 
2655
		$result = $this->Form->input('Contact.created', array(
2656
			'type' => 'date',
2657
			'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
2658
			'timeFormat' => 'NONE'
2659
		));
2660
		$this->assertRegExp('/for\="created-month"/', $result);
2661
	}
2662
 
2663
/**
2664
 * Test generating checkboxes in a loop.
2665
 *
2666
 * @return void
2667
 */
2668
	public function testInputCheckboxesInLoop() {
2669
		for ($i = 1; $i < 5; $i++) {
2670
			$result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
2671
			$expected = array(
2672
				'div' => array('class' => 'input checkbox'),
2673
				'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
2674
				array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
2675
				'label' => array('for' => "Contact{$i}Email"),
2676
				'Email',
2677
				'/label',
2678
				'/div'
2679
			);
2680
			$this->assertTags($result, $expected);
2681
		}
2682
	}
2683
 
2684
/**
2685
 * Test generating checkboxes with disabled elements.
2686
 *
2687
 * @return void
2688
 */
2689
	public function testInputCheckboxWithDisabledElements() {
2690
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
2691
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
2692
 
2693
		$expected = array(
2694
			array('div' => array('class' => 'input select')),
2695
			array('label' => array('for' => "ContactMultiple")),
2696
			'Multiple',
2697
			'/label',
2698
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
2699
			array('div' => array('class' => 'checkbox')),
2700
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
2701
			array('label' => array('for' => "ContactMultiple1")),
2702
			'One',
2703
			'/label',
2704
			'/div',
2705
			array('div' => array('class' => 'checkbox')),
2706
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
2707
			array('label' => array('for' => "ContactMultiple2")),
2708
			'Two',
2709
			'/label',
2710
			'/div',
2711
			array('div' => array('class' => 'checkbox')),
2712
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
2713
			array('label' => array('for' => "ContactMultiple3")),
2714
			'Three',
2715
			'/label',
2716
			'/div',
2717
			'/div'
2718
		);
2719
		$this->assertTags($result, $expected);
2720
 
2721
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
2722
		$this->assertTags($result, $expected);
2723
 
2724
		$disabled = array('2', 3);
2725
 
2726
		$expected = array(
2727
			array('div' => array('class' => 'input select')),
2728
			array('label' => array('for' => "ContactMultiple")),
2729
			'Multiple',
2730
			'/label',
2731
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
2732
			array('div' => array('class' => 'checkbox')),
2733
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
2734
			array('label' => array('for' => "ContactMultiple1")),
2735
			'One',
2736
			'/label',
2737
			'/div',
2738
			array('div' => array('class' => 'checkbox')),
2739
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
2740
			array('label' => array('for' => "ContactMultiple2")),
2741
			'Two',
2742
			'/label',
2743
			'/div',
2744
			array('div' => array('class' => 'checkbox')),
2745
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
2746
			array('label' => array('for' => "ContactMultiple3")),
2747
			'Three',
2748
			'/label',
2749
			'/div',
2750
			'/div'
2751
		);
2752
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
2753
		$this->assertTags($result, $expected);
2754
 
2755
		// make sure 50 does only disable 50, and not 50f5c0cf
2756
		$options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
2757
		$disabled = array(50);
2758
 
2759
		$expected = array(
2760
			array('div' => array('class' => 'input select')),
2761
			array('label' => array('for' => "ContactMultiple")),
2762
			'Multiple',
2763
			'/label',
2764
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
2765
			array('div' => array('class' => 'checkbox')),
2766
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
2767
			array('label' => array('for' => "ContactMultiple50")),
2768
			'Fifty',
2769
			'/label',
2770
			'/div',
2771
			array('div' => array('class' => 'checkbox')),
2772
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
2773
			array('label' => array('for' => "ContactMultiple50f5c0cf")),
2774
			'Stringy',
2775
			'/label',
2776
			'/div',
2777
			'/div'
2778
		);
2779
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
2780
		$this->assertTags($result, $expected);
2781
	}
2782
 
2783
/**
2784
 * test input name with leading integer, ensure attributes are generated correctly.
2785
 *
2786
 * @return void
2787
 */
2788
	public function testInputWithLeadingInteger() {
2789
		$result = $this->Form->text('0.Node.title');
2790
		$expected = array(
2791
			'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
2792
		);
2793
		$this->assertTags($result, $expected);
2794
	}
2795
 
2796
/**
2797
 * test form->input() with select type inputs.
2798
 *
2799
 * @return void
2800
 */
2801
	public function testInputSelectType() {
2802
		$result = $this->Form->input('email', array(
2803
			'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
2804
		);
2805
		$expected = array(
2806
			'div' => array('class' => 'input select'),
2807
			'label' => array('for' => 'email'),
2808
			'Email',
2809
			'/label',
2810
			array('select' => array('name' => 'data[email]', 'id' => 'email')),
2811
			array('option' => array('value' => '')),
2812
			'/option',
2813
			array('option' => array('value' => 'è')),
2814
			'Firést',
2815
			'/option',
2816
			array('option' => array('value' => 'é')),
2817
			'Secoènd',
2818
			'/option',
2819
			'/select',
2820
			'/div'
2821
		);
2822
		$this->assertTags($result, $expected);
2823
 
2824
		$result = $this->Form->input('email', array(
2825
			'options' => array('First', 'Second'), 'empty' => true)
2826
		);
2827
		$expected = array(
2828
			'div' => array('class' => 'input select'),
2829
			'label' => array('for' => 'email'),
2830
			'Email',
2831
			'/label',
2832
			array('select' => array('name' => 'data[email]', 'id' => 'email')),
2833
			array('option' => array('value' => '')),
2834
			'/option',
2835
			array('option' => array('value' => '0')),
2836
			'First',
2837
			'/option',
2838
			array('option' => array('value' => '1')),
2839
			'Second',
2840
			'/option',
2841
			'/select',
2842
			'/div'
2843
		);
2844
		$this->assertTags($result, $expected);
2845
 
2846
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2847
		$this->Form->request->data = array('Model' => array('user_id' => 'value'));
2848
 
2849
		$result = $this->Form->input('Model.user_id', array('empty' => true));
2850
		$expected = array(
2851
			'div' => array('class' => 'input select'),
2852
			'label' => array('for' => 'ModelUserId'),
2853
			'User',
2854
			'/label',
2855
			'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
2856
			array('option' => array('value' => '')),
2857
			'/option',
2858
			array('option' => array('value' => 'value', 'selected' => 'selected')),
2859
			'good',
2860
			'/option',
2861
			array('option' => array('value' => 'other')),
2862
			'bad',
2863
			'/option',
2864
			'/select',
2865
			'/div'
2866
		);
2867
		$this->assertTags($result, $expected);
2868
 
2869
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2870
		$this->Form->request->data = array('Thing' => array('user_id' => null));
2871
		$result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
2872
		$expected = array(
2873
			'div' => array('class' => 'input select'),
2874
			'label' => array('for' => 'ThingUserId'),
2875
			'User',
2876
			'/label',
2877
			'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
2878
			array('option' => array('value' => '')),
2879
			'Some Empty',
2880
			'/option',
2881
			array('option' => array('value' => 'value')),
2882
			'good',
2883
			'/option',
2884
			array('option' => array('value' => 'other')),
2885
			'bad',
2886
			'/option',
2887
			'/select',
2888
			'/div'
2889
		);
2890
		$this->assertTags($result, $expected);
2891
 
2892
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2893
		$this->Form->request->data = array('Thing' => array('user_id' => 'value'));
2894
		$result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
2895
		$expected = array(
2896
			'div' => array('class' => 'input select'),
2897
			'label' => array('for' => 'ThingUserId'),
2898
			'User',
2899
			'/label',
2900
			'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
2901
			array('option' => array('value' => '')),
2902
			'Some Empty',
2903
			'/option',
2904
			array('option' => array('value' => 'value', 'selected' => 'selected')),
2905
			'good',
2906
			'/option',
2907
			array('option' => array('value' => 'other')),
2908
			'bad',
2909
			'/option',
2910
			'/select',
2911
			'/div'
2912
		);
2913
		$this->assertTags($result, $expected);
2914
 
2915
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2916
		$this->Form->request->data = array('User' => array('User' => array('value')));
2917
		$result = $this->Form->input('User.User', array('empty' => true));
2918
		$expected = array(
2919
			'div' => array('class' => 'input select'),
2920
			'label' => array('for' => 'UserUser'),
2921
			'User',
2922
			'/label',
2923
			'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
2924
			'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
2925
			array('option' => array('value' => '')),
2926
			'/option',
2927
			array('option' => array('value' => 'value', 'selected' => 'selected')),
2928
			'good',
2929
			'/option',
2930
			array('option' => array('value' => 'other')),
2931
			'bad',
2932
			'/option',
2933
			'/select',
2934
			'/div'
2935
		);
2936
		$this->assertTags($result, $expected);
2937
 
2938
		$this->Form->data = array();
2939
		$result = $this->Form->input('Publisher.id', array(
2940
				'label'		=> 'Publisher',
2941
				'type'		=> 'select',
2942
				'multiple'	=> 'checkbox',
2943
				'options'	=> array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
2944
		));
2945
		$expected = array(
2946
			array('div' => array('class' => 'input select')),
2947
				array('label' => array('for' => 'PublisherId')),
2948
				'Publisher',
2949
				'/label',
2950
				'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
2951
				array('div' => array('class' => 'checkbox')),
2952
				array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
2953
				array('label' => array('for' => 'PublisherIdValue1')),
2954
				'Label 1',
2955
				'/label',
2956
				'/div',
2957
				array('div' => array('class' => 'checkbox')),
2958
				array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
2959
				array('label' => array('for' => 'PublisherIdValue2')),
2960
				'Label 2',
2961
				'/label',
2962
				'/div',
2963
			'/div'
2964
		);
2965
		$this->assertTags($result, $expected);
2966
	}
2967
 
2968
/**
2969
 * test that input() and a non standard primary key makes a hidden input by default.
2970
 *
2971
 * @return void
2972
 */
2973
	public function testInputWithNonStandardPrimaryKeyMakesHidden() {
2974
		$this->Form->create('User');
2975
		$this->Form->fieldset = array(
2976
			'User' => array(
2977
				'fields' => array(
2978
					'model_id' => array('type' => 'integer')
2979
				),
2980
				'validates' => array(),
2981
				'key' => 'model_id'
2982
			)
2983
		);
2984
		$result = $this->Form->input('model_id');
2985
		$expected = array(
2986
			'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
2987
		);
2988
		$this->assertTags($result, $expected);
2989
	}
2990
 
2991
/**
2992
 * test that overriding the magic select type widget is possible
2993
 *
2994
 * @return void
2995
 */
2996
	public function testInputOverridingMagicSelectType() {
2997
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2998
		$result = $this->Form->input('Model.user_id', array('type' => 'text'));
2999
		$expected = array(
3000
			'div' => array('class' => 'input text'),
3001
			'label' => array('for' => 'ModelUserId'), 'User', '/label',
3002
			'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
3003
			'/div'
3004
		);
3005
		$this->assertTags($result, $expected);
3006
 
3007
		//Check that magic types still work for plural/singular vars
3008
		$this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
3009
		$result = $this->Form->input('Model.type');
3010
		$expected = array(
3011
			'div' => array('class' => 'input select'),
3012
			'label' => array('for' => 'ModelType'), 'Type', '/label',
3013
			'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
3014
			array('option' => array('value' => 'value')), 'good', '/option',
3015
			array('option' => array('value' => 'other')), 'bad', '/option',
3016
			'/select',
3017
			'/div'
3018
		);
3019
		$this->assertTags($result, $expected);
3020
	}
3021
 
3022
/**
3023
 * Test that inferred types do not override developer input
3024
 *
3025
 * @return void
3026
 */
3027
	public function testInputMagicTypeDoesNotOverride() {
3028
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3029
		$result = $this->Form->input('Model.user', array('type' => 'checkbox'));
3030
		$expected = array(
3031
			'div' => array('class' => 'input checkbox'),
3032
			array('input' => array(
3033
				'type' => 'hidden',
3034
				'name' => 'data[Model][user]',
3035
				'id' => 'ModelUser_',
3036
				'value' => 0,
3037
			)),
3038
			array('input' => array(
3039
				'name' => 'data[Model][user]',
3040
				'type' => 'checkbox',
3041
				'id' => 'ModelUser',
3042
				'value' => 1
3043
			)),
3044
			'label' => array('for' => 'ModelUser'), 'User', '/label',
3045
			'/div'
3046
		);
3047
		$this->assertTags($result, $expected);
3048
	}
3049
 
3050
/**
3051
 * Test that magic input() selects are created for type=number
3052
 *
3053
 * @return void
3054
 */
3055
	public function testInputMagicSelectForTypeNumber() {
3056
		$this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
3057
		$this->Form->request->data = array('ValidateUser' => array('balance' => 1));
3058
		$result = $this->Form->input('ValidateUser.balance');
3059
		$expected = array(
3060
			'div' => array('class' => 'input select'),
3061
			'label' => array('for' => 'ValidateUserBalance'),
3062
			'Balance',
3063
			'/label',
3064
			'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
3065
			array('option' => array('value' => '0')),
3066
			'nothing',
3067
			'/option',
3068
			array('option' => array('value' => '1', 'selected' => 'selected')),
3069
			'some',
3070
			'/option',
3071
			array('option' => array('value' => '100')),
3072
			'a lot',
3073
			'/option',
3074
			'/select',
3075
			'/div'
3076
		);
3077
		$this->assertTags($result, $expected);
3078
	}
3079
 
3080
/**
3081
 * Test that magic input() selects can easily be converted into radio types without error.
3082
 *
3083
 * @return void
3084
 */
3085
	public function testInputMagicSelectChangeToRadio() {
3086
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3087
		$result = $this->Form->input('Model.user_id', array('type' => 'radio'));
3088
		$this->assertRegExp('/input type="radio"/', $result);
3089
	}
3090
 
3091
/**
3092
 * fields with the same name as the model should work.
3093
 *
3094
 * @return void
3095
 */
3096
	public function testInputWithMatchingFieldAndModelName() {
3097
		$this->Form->create('User');
3098
		$this->Form->fieldset = array(
3099
			'User' => array(
3100
				'fields' => array(
3101
					'User' => array('type' => 'text')
3102
				),
3103
				'validates' => array(),
3104
				'key' => 'id'
3105
			)
3106
		);
3107
		$this->Form->request->data['User']['User'] = 'ABC, Inc.';
3108
		$result = $this->Form->input('User', array('type' => 'text'));
3109
		$expected = array(
3110
			'div' => array('class' => 'input text'),
3111
			'label' => array('for' => 'UserUser'), 'User', '/label',
3112
			'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
3113
			'/div'
3114
		);
3115
		$this->assertTags($result, $expected);
3116
	}
3117
 
3118
/**
3119
 * testFormInputs method
3120
 *
3121
 * test correct results from form::inputs().
3122
 *
3123
 * @return void
3124
 */
3125
	public function testFormInputs() {
3126
		$this->Form->create('Contact');
3127
		$result = $this->Form->inputs('The Legend');
3128
		$expected = array(
3129
			'<fieldset',
3130
			'<legend',
3131
			'The Legend',
3132
			'/legend',
3133
			'*/fieldset',
3134
		);
3135
		$this->assertTags($result, $expected);
3136
 
3137
		$result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
3138
		$expected = array(
3139
			'fieldset' => array('class' => 'classy-stuff'),
3140
			'<legend',
3141
			'Field of Dreams',
3142
			'/legend',
3143
			'*/fieldset'
3144
		);
3145
		$this->assertTags($result, $expected);
3146
 
3147
		$result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
3148
		$this->assertTags($result, $expected);
3149
 
3150
		$result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
3151
		$this->assertTags($result, $expected);
3152
 
3153
		$this->Form->create('Contact');
3154
		$this->Form->request['prefix'] = 'admin';
3155
		$this->Form->request['action'] = 'admin_edit';
3156
		$result = $this->Form->inputs();
3157
		$expected = array(
3158
			'<fieldset',
3159
			'<legend',
3160
			'Edit Contact',
3161
			'/legend',
3162
			'*/fieldset',
3163
		);
3164
		$this->assertTags($result, $expected);
3165
 
3166
		$this->Form->create('Contact');
3167
		$result = $this->Form->inputs(false);
3168
		$expected = array(
3169
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3170
			array('div' => array('class' => 'input text')),
3171
			'*/div',
3172
			array('div' => array('class' => 'input email')),
3173
			'*/div',
3174
			array('div' => array('class' => 'input tel')),
3175
			'*/div',
3176
			array('div' => array('class' => 'input password')),
3177
			'*/div',
3178
			array('div' => array('class' => 'input date')),
3179
			'*/div',
3180
			array('div' => array('class' => 'input date')),
3181
			'*/div',
3182
			array('div' => array('class' => 'input datetime')),
3183
			'*/div',
3184
			array('div' => array('class' => 'input number')),
3185
			'*/div',
3186
			array('div' => array('class' => 'input select')),
3187
			'*/div',
3188
		);
3189
		$this->assertTags($result, $expected);
3190
 
3191
		$this->Form->create('Contact');
3192
		$result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
3193
		$expected = array(
3194
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3195
			array('div' => array('class' => 'input text')),
3196
			'*/div',
3197
			array('div' => array('class' => 'input email')),
3198
			'*/div',
3199
			array('div' => array('class' => 'input tel')),
3200
			'*/div',
3201
			array('div' => array('class' => 'input password')),
3202
			'*/div',
3203
			array('div' => array('class' => 'input date')),
3204
			'*/div',
3205
			array('div' => array('class' => 'input date')),
3206
			'*/div',
3207
			array('div' => array('class' => 'input datetime')),
3208
			'*/div',
3209
			array('div' => array('class' => 'input number')),
3210
			'*/div',
3211
			array('div' => array('class' => 'input select')),
3212
			'*/div',
3213
		);
3214
		$this->assertTags($result, $expected);
3215
 
3216
		$this->Form->create('Contact');
3217
		$result = $this->Form->inputs(null, null, array('fieldset' => false));
3218
		$this->assertTags($result, $expected);
3219
 
3220
		$this->Form->create('Contact');
3221
		$result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
3222
		$expected = array(
3223
			'fieldset' => array(),
3224
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3225
			array('div' => array('class' => 'input text')),
3226
			'*/div',
3227
			array('div' => array('class' => 'input email')),
3228
			'*/div',
3229
			array('div' => array('class' => 'input tel')),
3230
			'*/div',
3231
			array('div' => array('class' => 'input password')),
3232
			'*/div',
3233
			array('div' => array('class' => 'input date')),
3234
			'*/div',
3235
			array('div' => array('class' => 'input date')),
3236
			'*/div',
3237
			array('div' => array('class' => 'input datetime')),
3238
			'*/div',
3239
			array('div' => array('class' => 'input number')),
3240
			'*/div',
3241
			array('div' => array('class' => 'input select')),
3242
			'*/div',
3243
			'/fieldset'
3244
		);
3245
		$this->assertTags($result, $expected);
3246
 
3247
		$this->Form->create('Contact');
3248
		$result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
3249
		$expected = array(
3250
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3251
			array('div' => array('class' => 'input text')),
3252
			'*/div',
3253
			array('div' => array('class' => 'input email')),
3254
			'*/div',
3255
			array('div' => array('class' => 'input tel')),
3256
			'*/div',
3257
			array('div' => array('class' => 'input password')),
3258
			'*/div',
3259
			array('div' => array('class' => 'input date')),
3260
			'*/div',
3261
			array('div' => array('class' => 'input date')),
3262
			'*/div',
3263
			array('div' => array('class' => 'input datetime')),
3264
			'*/div',
3265
			array('div' => array('class' => 'input number')),
3266
			'*/div',
3267
			array('div' => array('class' => 'input select')),
3268
			'*/div',
3269
		);
3270
		$this->assertTags($result, $expected);
3271
 
3272
		$this->Form->create('Contact');
3273
		$result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
3274
		$this->assertTags($result, $expected);
3275
 
3276
		$this->Form->create('Contact');
3277
		$result = $this->Form->inputs('Hello');
3278
		$expected = array(
3279
			'fieldset' => array(),
3280
			'legend' => array(),
3281
			'Hello',
3282
			'/legend',
3283
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3284
			array('div' => array('class' => 'input text')),
3285
			'*/div',
3286
			array('div' => array('class' => 'input email')),
3287
			'*/div',
3288
			array('div' => array('class' => 'input tel')),
3289
			'*/div',
3290
			array('div' => array('class' => 'input password')),
3291
			'*/div',
3292
			array('div' => array('class' => 'input date')),
3293
			'*/div',
3294
			array('div' => array('class' => 'input date')),
3295
			'*/div',
3296
			array('div' => array('class' => 'input datetime')),
3297
			'*/div',
3298
			array('div' => array('class' => 'input number')),
3299
			'*/div',
3300
			array('div' => array('class' => 'input select')),
3301
			'*/div',
3302
			'/fieldset'
3303
		);
3304
		$this->assertTags($result, $expected);
3305
 
3306
		$this->Form->create('Contact');
3307
		$result = $this->Form->inputs(array('legend' => 'Hello'));
3308
		$expected = array(
3309
			'fieldset' => array(),
3310
			'legend' => array(),
3311
			'Hello',
3312
			'/legend',
3313
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3314
			array('div' => array('class' => 'input text')),
3315
			'*/div',
3316
			array('div' => array('class' => 'input email')),
3317
			'*/div',
3318
			array('div' => array('class' => 'input tel')),
3319
			'*/div',
3320
			array('div' => array('class' => 'input password')),
3321
			'*/div',
3322
			array('div' => array('class' => 'input date')),
3323
			'*/div',
3324
			array('div' => array('class' => 'input date')),
3325
			'*/div',
3326
			array('div' => array('class' => 'input datetime')),
3327
			'*/div',
3328
			array('div' => array('class' => 'input number')),
3329
			'*/div',
3330
			array('div' => array('class' => 'input select')),
3331
			'*/div',
3332
			'/fieldset'
3333
		);
3334
		$this->assertTags($result, $expected);
3335
 
3336
		$this->Form->create('Contact');
3337
		$result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
3338
		$this->assertTags($result, $expected);
3339
		$this->Form->end();
3340
 
3341
		$this->Form->create(false);
3342
		$expected = array(
3343
			'fieldset' => array(),
3344
			array('div' => array('class' => 'input text')),
3345
			'label' => array('for' => 'foo'),
3346
			'Foo',
3347
			'/label',
3348
			'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
3349
			'*/div',
3350
			'/fieldset'
3351
		);
3352
		$result = $this->Form->inputs(
3353
			array('foo' => array('type' => 'text')),
3354
			array(),
3355
			array('legend' => false)
3356
		);
3357
		$this->assertTags($result, $expected);
3358
	}
3359
 
3360
/**
3361
 * Tests inputs() works with plugin models
3362
 *
3363
 * @return void
3364
 */
3365
	public function testInputsPluginModel() {
3366
		$this->loadFixtures('Post');
3367
		App::build(array(
3368
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
3369
		));
3370
		CakePlugin::load('TestPlugin');
3371
		$this->Form->request['models'] = array(
3372
			'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
3373
		);
3374
		$this->Form->create('TestPlugin.TestPluginPost');
3375
		$result = $this->Form->inputs();
3376
 
3377
		$this->assertContains('data[TestPluginPost][id]', $result);
3378
		$this->assertContains('data[TestPluginPost][author_id]', $result);
3379
		$this->assertContains('data[TestPluginPost][title]', $result);
3380
		$this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
3381
		$this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
3382
		$this->assertEquals('TestPluginPost', $this->Form->model());
3383
	}
3384
 
3385
/**
3386
 * testSelectAsCheckbox method
3387
 *
3388
 * test multi-select widget with checkbox formatting.
3389
 *
3390
 * @return void
3391
 */
3392
	public function testSelectAsCheckbox() {
3393
		$result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
3394
		$expected = array(
3395
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3396
			array('div' => array('class' => 'checkbox')),
3397
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
3398
			array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
3399
			'first',
3400
			'/label',
3401
			'/div',
3402
			array('div' => array('class' => 'checkbox')),
3403
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
3404
			array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
3405
			'second',
3406
			'/label',
3407
			'/div',
3408
			array('div' => array('class' => 'checkbox')),
3409
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
3410
			array('label' => array('for' => 'ModelMultiField2')),
3411
			'third',
3412
			'/label',
3413
			'/div',
3414
		);
3415
		$this->assertTags($result, $expected);
3416
 
3417
		$result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
3418
		$expected = array(
3419
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3420
			array('div' => array('class' => 'checkbox')),
3421
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
3422
			array('label' => array('for' => 'ModelMultiField12')),
3423
			'half',
3424
			'/label',
3425
			'/div',
3426
		);
3427
		$this->assertTags($result, $expected);
3428
	}
3429
 
3430
/**
3431
 * testLabel method
3432
 *
3433
 * test label generation.
3434
 *
3435
 * @return void
3436
 */
3437
	public function testLabel() {
3438
		$this->Form->text('Person.name');
3439
		$result = $this->Form->label();
3440
		$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
3441
 
3442
		$this->Form->text('Person.name');
3443
		$result = $this->Form->label();
3444
		$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
3445
 
3446
		$result = $this->Form->label('Person.first_name');
3447
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
3448
 
3449
		$result = $this->Form->label('Person.first_name', 'Your first name');
3450
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
3451
 
3452
		$result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
3453
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
3454
 
3455
		$result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
3456
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
3457
 
3458
		$result = $this->Form->label('Person.first_name', '');
3459
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
3460
 
3461
		$result = $this->Form->label('Person.2.name', '');
3462
		$this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
3463
	}
3464
 
3465
/**
3466
 * testTextbox method
3467
 *
3468
 * test textbox element generation
3469
 *
3470
 * @return void
3471
 */
3472
	public function testTextbox() {
3473
		$result = $this->Form->text('Model.field');
3474
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
3475
 
3476
		$result = $this->Form->text('Model.field', array('type' => 'password'));
3477
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
3478
 
3479
		$result = $this->Form->text('Model.field', array('id' => 'theID'));
3480
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
3481
 
3482
		$this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
3483
		$result = $this->Form->text('Model.text');
3484
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
3485
 
3486
		$Contact = ClassRegistry::getObject('Contact');
3487
		$Contact->validationErrors['text'] = array(true);
3488
		$this->Form->request->data['Contact']['text'] = 'test';
3489
		$result = $this->Form->text('Contact.text', array('id' => 'theID'));
3490
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
3491
 
3492
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
3493
		$result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
3494
		$expected = array(
3495
			'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
3496
		);
3497
		$this->assertTags($result, $expected);
3498
	}
3499
 
3500
/**
3501
 * testDefaultValue method
3502
 *
3503
 * Test default value setting
3504
 *
3505
 * @return void
3506
 */
3507
	public function testDefaultValue() {
3508
		$this->Form->request->data['Model']['field'] = 'test';
3509
		$result = $this->Form->text('Model.field', array('default' => 'default value'));
3510
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
3511
 
3512
		unset($this->Form->request->data['Model']['field']);
3513
		$result = $this->Form->text('Model.field', array('default' => 'default value'));
3514
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
3515
	}
3516
 
3517
/**
3518
 * testCheckboxDefaultValue method
3519
 *
3520
 * Test default value setting on checkbox() method
3521
 *
3522
 * @return void
3523
 */
3524
	public function testCheckboxDefaultValue() {
3525
		$this->Form->request->data['Model']['field'] = false;
3526
		$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
3527
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
3528
 
3529
		unset($this->Form->request->data['Model']['field']);
3530
		$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
3531
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
3532
 
3533
		$this->Form->request->data['Model']['field'] = true;
3534
		$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
3535
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
3536
 
3537
		unset($this->Form->request->data['Model']['field']);
3538
		$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
3539
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
3540
	}
3541
 
3542
/**
3543
 * testError method
3544
 *
3545
 * Test field error generation
3546
 *
3547
 * @return void
3548
 */
3549
	public function testError() {
3550
		$Contact = ClassRegistry::getObject('Contact');
3551
		$Contact->validationErrors['field'] = array(1);
3552
		$result = $this->Form->error('Contact.field');
3553
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
3554
 
3555
		$result = $this->Form->error('Contact.field', null, array('wrap' => false));
3556
		$this->assertEquals('Error in field Field', $result);
3557
 
3558
		$Contact->validationErrors['field'] = array("This field contains invalid input");
3559
		$result = $this->Form->error('Contact.field', null, array('wrap' => false));
3560
		$this->assertEquals('This field contains invalid input', $result);
3561
 
3562
		$Contact->validationErrors['field'] = array("This field contains invalid input");
3563
		$result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
3564
		$this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
3565
 
3566
		$result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
3567
		$this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
3568
 
3569
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
3570
		$this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
3571
 
3572
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
3573
		$this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
3574
 
3575
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
3576
		$this->assertEquals('<strong>Badness!</strong>', $result);
3577
 
3578
		$Contact->validationErrors['field'] = array("email");
3579
		$result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
3580
		$expected = array(
3581
			'div' => array('class' => 'field-error'),
3582
			'No good!',
3583
			'/div'
3584
		);
3585
		$this->assertTags($result, $expected);
3586
 
3587
		$Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
3588
		$result = $this->Form->error('Contact.field', array(
3589
			'notEmpty' => 'Cannot be empty',
3590
			'email' => 'No good!'
3591
		));
3592
		$expected = array(
3593
			'div' => array('class' => 'error-message'),
3594
				'ul' => array(),
3595
					'<li', 'Cannot be empty', '/li',
3596
					'<li', 'No good!', '/li',
3597
					'<li', 'Something else', '/li',
3598
				'/ul',
3599
			'/div'
3600
		);
3601
		$this->assertTags($result, $expected);
3602
 
3603
		// Testing error messages list options
3604
		$Contact->validationErrors['field'] = array('notEmpty', 'email');
3605
 
3606
		$result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
3607
		$expected = array(
3608
			'div' => array('class' => 'error-message'),
3609
				'ol' => array(),
3610
					'<li', 'notEmpty', '/li',
3611
					'<li', 'email', '/li',
3612
				'/ol',
3613
			'/div'
3614
		);
3615
		$this->assertTags($result, $expected);
3616
 
3617
		$result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
3618
		$expected = array(
3619
			'div' => array('class' => 'error-message'),
3620
				'ol' => array(),
3621
					'<li', 'notEmpty', '/li',
3622
					'<li', 'email', '/li',
3623
				'/ol',
3624
			'/div'
3625
		);
3626
		$this->assertTags($result, $expected);
3627
 
3628
		$result = $this->Form->error('Contact.field', null, array(
3629
			'listOptions' => array(
3630
				'class' => 'ul-class',
3631
				'itemOptions' => array(
3632
					'class' => 'li-class'
3633
				)
3634
			)
3635
		));
3636
		$expected = array(
3637
			'div' => array('class' => 'error-message'),
3638
				'ul' => array('class' => 'ul-class'),
3639
					array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
3640
					array('li' => array('class' => 'li-class')), 'email', '/li',
3641
				'/ul',
3642
			'/div'
3643
		);
3644
		$this->assertTags($result, $expected);
3645
	}
3646
 
3647
/**
3648
 * test error options when using form->input();
3649
 *
3650
 * @return void
3651
 */
3652
	public function testInputErrorEscape() {
3653
		$this->Form->create('ValidateProfile');
3654
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
3655
		$ValidateProfile->validationErrors['city'] = array('required<br>');
3656
		$result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
3657
		$this->assertRegExp('/required&lt;br&gt;/', $result);
3658
 
3659
		$result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
3660
		$this->assertRegExp('/required<br>/', $result);
3661
	}
3662
 
3663
/**
3664
 * testPassword method
3665
 *
3666
 * Test password element generation
3667
 *
3668
 * @return void
3669
 */
3670
	public function testPassword() {
3671
		$Contact = ClassRegistry::getObject('Contact');
3672
		$result = $this->Form->password('Contact.field');
3673
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
3674
 
3675
		$Contact->validationErrors['passwd'] = 1;
3676
		$this->Form->request->data['Contact']['passwd'] = 'test';
3677
		$result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
3678
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
3679
	}
3680
 
3681
/**
3682
 * testRadio method
3683
 *
3684
 * Test radio element set generation
3685
 *
3686
 * @return void
3687
 */
3688
	public function testRadio() {
3689
		$result = $this->Form->radio('Model.field', array('option A'));
3690
		$expected = array(
3691
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3692
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3693
			'label' => array('for' => 'ModelField0'),
3694
			'option A',
3695
			'/label'
3696
		);
3697
		$this->assertTags($result, $expected);
3698
 
3699
		$result = $this->Form->radio('Model.field', array('1/2' => 'half'));
3700
		$expected = array(
3701
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3702
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
3703
			'label' => array('for' => 'ModelField12'),
3704
			'half',
3705
			'/label'
3706
		);
3707
		$this->assertTags($result, $expected);
3708
 
3709
		$result = $this->Form->radio('Model.field', array('option A', 'option B'));
3710
		$expected = array(
3711
			'fieldset' => array(),
3712
			'legend' => array(),
3713
			'Field',
3714
			'/legend',
3715
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3716
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3717
			array('label' => array('for' => 'ModelField0')),
3718
			'option A',
3719
			'/label',
3720
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
3721
			array('label' => array('for' => 'ModelField1')),
3722
			'option B',
3723
			'/label',
3724
			'/fieldset'
3725
		);
3726
		$this->assertTags($result, $expected);
3727
 
3728
		$result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
3729
		$expected = array(
3730
			'fieldset' => array(),
3731
			'legend' => array(),
3732
			'Field',
3733
			'/legend',
3734
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3735
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3736
			array('label' => array('for' => 'ModelField0')),
3737
			'option A',
3738
			'/label',
3739
			'br' => array(),
3740
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
3741
			array('label' => array('for' => 'ModelField1')),
3742
			'option B',
3743
			'/label',
3744
			'/fieldset'
3745
		);
3746
		$this->assertTags($result, $expected);
3747
 
3748
		$result = $this->Form->radio(
3749
			'Employee.gender',
3750
			array('male' => 'Male', 'female' => 'Female'),
3751
			array('form' => 'my-form')
3752
		);
3753
		$expected = array(
3754
			'fieldset' => array(),
3755
			'legend' => array(),
3756
			'Gender',
3757
			'/legend',
3758
			'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_', 'form' => 'my-form'),
3759
			array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')),
3760
			array('label' => array('for' => 'EmployeeGenderMale')),
3761
			'Male',
3762
			'/label',
3763
			array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale', 'form' => 'my-form')),
3764
			array('label' => array('for' => 'EmployeeGenderFemale')),
3765
			'Female',
3766
			'/label',
3767
			'/fieldset',
3768
		);
3769
		$this->assertTags($result, $expected);
3770
 
3771
		$result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
3772
		$expected = array(
3773
			'fieldset' => array(),
3774
			'legend' => array(),
3775
			'Gender',
3776
			'/legend',
3777
			'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
3778
			array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
3779
			array('label' => array('for' => 'OfficerGenderMale')),
3780
			'Male',
3781
			'/label',
3782
			array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
3783
			array('label' => array('for' => 'OfficerGenderFemale')),
3784
			'Female',
3785
			'/label',
3786
			'/fieldset',
3787
		);
3788
		$this->assertTags($result, $expected);
3789
 
3790
		$result = $this->Form->radio('Contact.1.imrequired', array('option A'));
3791
		$expected = array(
3792
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
3793
			array('input' => array(
3794
				'type' => 'radio',
3795
				'name' => 'data[Contact][1][imrequired]',
3796
				'value' => '0',
3797
				'id' => 'Contact1Imrequired0',
3798
				'required' => 'required'
3799
			)),
3800
			'label' => array('for' => 'Contact1Imrequired0'),
3801
			'option A',
3802
			'/label'
3803
		);
3804
		$this->assertTags($result, $expected);
3805
 
3806
		$result = $this->Form->radio('Model.1.field', array('option A'));
3807
		$expected = array(
3808
			'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
3809
			array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
3810
			'label' => array('for' => 'Model1Field0'),
3811
			'option A',
3812
			'/label'
3813
		);
3814
		$this->assertTags($result, $expected);
3815
 
3816
		$result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
3817
		$expected = array(
3818
			'fieldset' => array(),
3819
			'legend' => array(),
3820
			'Field',
3821
			'/legend',
3822
			'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
3823
			array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
3824
			array('label' => array('for' => 'ModelField0')),
3825
			'option A',
3826
			'/label',
3827
			array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
3828
			array('label' => array('for' => 'ModelField1')),
3829
			'option B',
3830
			'/label',
3831
			'/fieldset'
3832
		);
3833
		$this->assertTags($result, $expected);
3834
 
3835
		$result = $this->Form->radio(
3836
			'Model.field',
3837
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
3838
		);
3839
		$expected = array(
3840
			'fieldset' => array(),
3841
			'legend' => array(),
3842
			'Field',
3843
			'/legend',
3844
			'input' => array(
3845
				'type' => 'hidden', 'name' => 'data[Model][field]',
3846
				'id' => 'ModelField_', 'value' => '',
3847
			),
3848
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
3849
				'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
3850
			array('label' => array('for' => 'ModelFieldAB')),
3851
			'first',
3852
			'/label',
3853
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
3854
				'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
3855
			array('label' => array('for' => 'ModelFieldAB1')),
3856
			'second',
3857
			'/label',
3858
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
3859
				'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
3860
			array('label' => array('for' => 'ModelFieldAB2')),
3861
			'third',
3862
			'/label',
3863
			'/fieldset'
3864
		);
3865
		$this->assertTags($result, $expected);
3866
	}
3867
 
3868
/**
3869
 * testRadioDifferentModel
3870
 * Refs #2911
3871
 *
3872
 * @return void
3873
 */
3874
	public function testRadioDifferentModel() {
3875
		$this->Form->create('User');
3876
 
3877
		$result = $this->Form->radio(
3878
			'Model.field',
3879
			array('v1' => 'option A', 'v2' => 'option B'),
3880
			array('label' => true, 'legend' => false, 'value' => false)
3881
		);
3882
		$expected = array(
3883
			array('input' => array(
3884
				'type' => 'radio', 'name' => 'data[Model][field]',
3885
				'value' => 'v1', 'id' => 'ModelFieldV1'
3886
			)),
3887
			array('label' => array('for' => 'ModelFieldV1')),
3888
			'option A',
3889
			'/label',
3890
			array('input' => array(
3891
				'type' => 'radio', 'name' => 'data[Model][field]',
3892
				'value' => 'v2', 'id' => 'ModelFieldV2'
3893
			)),
3894
			array('label' => array('for' => 'ModelFieldV2')),
3895
			'option B',
3896
			'/label'
3897
		);
3898
		$this->assertTags($result, $expected);
3899
	}
3900
 
3901
/**
3902
 * Test radio inputs with between as string or array. Also ensure
3903
 * that an array with less between elements works.
3904
 *
3905
 * @return void
3906
 */
3907
	public function testRadioBetween() {
3908
		$result = $this->Form->radio(
3909
			'Model.field',
3910
			array('option A', 'option B'),
3911
			array('between' => 'I am between')
3912
		);
3913
		$expected = array(
3914
			'fieldset' => array(),
3915
			'legend' => array(),
3916
			'Field',
3917
			'/legend',
3918
			'I am between',
3919
			'input' => array(
3920
				'type' => 'hidden', 'name' => 'data[Model][field]',
3921
				'value' => '', 'id' => 'ModelField_'
3922
			),
3923
			array('input' => array(
3924
				'type' => 'radio', 'name' => 'data[Model][field]',
3925
				'value' => '0', 'id' => 'ModelField0'
3926
			)),
3927
			array('label' => array('for' => 'ModelField0')),
3928
			'option A',
3929
			'/label',
3930
			array('input' => array(
3931
				'type' => 'radio', 'name' => 'data[Model][field]',
3932
				'value' => '1', 'id' => 'ModelField1'
3933
			)),
3934
			array('label' => array('for' => 'ModelField1')),
3935
			'option B',
3936
			'/label',
3937
			'/fieldset'
3938
		);
3939
		$this->assertTags($result, $expected);
3940
 
3941
		$result = $this->Form->radio(
3942
			'Model.field',
3943
			array('option A', 'option B', 'option C'),
3944
			array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
3945
		);
3946
 
3947
		$expected = array(
3948
			'fieldset' => array(),
3949
			'legend' => array(),
3950
			'Field',
3951
			'/legend',
3952
			'input' => array(
3953
				'type' => 'hidden', 'name' => 'data[Model][field]',
3954
				'value' => '', 'id' => 'ModelField_'
3955
			),
3956
			array('input' => array(
3957
				'type' => 'radio', 'name' => 'data[Model][field]',
3958
				'value' => '0', 'id' => 'ModelField0'
3959
			)),
3960
			array('label' => array('for' => 'ModelField0')),
3961
			'option A',
3962
			'/label',
3963
			'between A',
3964
			'--separator--',
3965
			array('input' => array(
3966
				'type' => 'radio', 'name' => 'data[Model][field]',
3967
				'value' => '1', 'id' => 'ModelField1'
3968
			)),
3969
			array('label' => array('for' => 'ModelField1')),
3970
			'option B',
3971
			'/label',
3972
			'between B',
3973
			'--separator--',
3974
			array('input' => array(
3975
				'type' => 'radio', 'name' => 'data[Model][field]',
3976
				'value' => '2', 'id' => 'ModelField2'
3977
			)),
3978
			array('label' => array('for' => 'ModelField2')),
3979
			'option C',
3980
			'/label',
3981
			'between C',
3982
			'/fieldset'
3983
		);
3984
		$this->assertTags($result, $expected);
3985
 
3986
		$result = $this->Form->input('Model.field', array(
3987
			'options' => array('1' => 'first', '2' => 'second'),
3988
			'type' => 'radio',
3989
			'before' => '--before--',
3990
			'after' => '--after--',
3991
			'separator' => '--separator--',
3992
			'between' => array('--between first--', '--between second--')
3993
		));
3994
 
3995
		$expected = array(
3996
			'div' => array('class' => 'input radio'),
3997
			'--before--',
3998
			'fieldset' => array(),
3999
			'legend' => array(),
4000
			'Field',
4001
			'/legend',
4002
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4003
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4004
			array('label' => array('for' => 'ModelField1')),
4005
			'first',
4006
			'/label',
4007
			'--between first--',
4008
			'--separator--',
4009
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
4010
			array('label' => array('for' => 'ModelField2')),
4011
			'second',
4012
			'/label',
4013
			'--between second--',
4014
			'/fieldset',
4015
			'--after--',
4016
			'/div'
4017
		);
4018
		$this->assertTags($result, $expected);
4019
 
4020
		$result = $this->Form->input('Model.field', array(
4021
			'options' => array('1' => 'first', '2' => 'second'),
4022
			'type' => 'radio',
4023
			'before' => '--before--',
4024
			'after' => '--after--',
4025
			'separator' => '--separator--',
4026
			'between' => array('--between first--')
4027
		));
4028
 
4029
		$expected = array(
4030
			'div' => array('class' => 'input radio'),
4031
			'--before--',
4032
			'fieldset' => array(),
4033
			'legend' => array(),
4034
			'Field',
4035
			'/legend',
4036
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4037
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4038
			array('label' => array('for' => 'ModelField1')),
4039
			'first',
4040
			'/label',
4041
			'--between first--',
4042
			'--separator--',
4043
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
4044
			array('label' => array('for' => 'ModelField2')),
4045
			'second',
4046
			'/label',
4047
			'/fieldset',
4048
			'--after--',
4049
			'/div'
4050
		);
4051
		$this->assertTags($result, $expected);
4052
	}
4053
 
4054
/**
4055
 * Test that radios with a 0 value are selected under the correct conditions.
4056
 * Also ensure that values that are booleanish are handled correctly.
4057
 *
4058
 * @return void
4059
 */
4060
	public function testRadioOptionWithBooleanishValues() {
4061
		$expected = array(
4062
			'fieldset' => array(),
4063
			'legend' => array(),
4064
			'Field',
4065
			'/legend',
4066
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4067
			array('label' => array('for' => 'ModelField1')),
4068
			'Yes',
4069
			'/label',
4070
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
4071
			array('label' => array('for' => 'ModelField0')),
4072
			'No',
4073
			'/label',
4074
			'/fieldset'
4075
		);
4076
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
4077
		$this->assertTags($result, $expected);
4078
 
4079
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
4080
		$this->assertTags($result, $expected);
4081
 
4082
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
4083
		$this->assertTags($result, $expected);
4084
 
4085
		$expected = array(
4086
			'fieldset' => array(),
4087
			'legend' => array(),
4088
			'Field',
4089
			'/legend',
4090
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
4091
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4092
			array('label' => array('for' => 'ModelField1')),
4093
			'Yes',
4094
			'/label',
4095
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
4096
			array('label' => array('for' => 'ModelField0')),
4097
			'No',
4098
			'/label',
4099
			'/fieldset'
4100
		);
4101
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
4102
		$this->assertTags($result, $expected);
4103
 
4104
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
4105
		$this->assertTags($result, $expected);
4106
 
4107
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
4108
		$this->assertTags($result, $expected);
4109
 
4110
		$expected = array(
4111
			'fieldset' => array(),
4112
			'legend' => array(),
4113
			'Field',
4114
			'/legend',
4115
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
4116
			array('label' => array('for' => 'ModelField1')),
4117
			'Yes',
4118
			'/label',
4119
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
4120
			array('label' => array('for' => 'ModelField0')),
4121
			'No',
4122
			'/label',
4123
			'/fieldset'
4124
		);
4125
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
4126
		$this->assertTags($result, $expected);
4127
 
4128
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
4129
		$this->assertTags($result, $expected);
4130
 
4131
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
4132
		$this->assertTags($result, $expected);
4133
	}
4134
 
4135
/**
4136
 * test disabled radio options
4137
 *
4138
 * @return void
4139
 */
4140
	public function testRadioDisabled() {
4141
		$result = $this->Form->radio(
4142
			'Model.field',
4143
			array('option A', 'option B'),
4144
			array('disabled' => array(0), 'value' => '0')
4145
		);
4146
		$expected = array(
4147
			'fieldset' => array(),
4148
			'legend' => array(),
4149
			'Field',
4150
			'/legend',
4151
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4152
			array('label' => array('for' => 'ModelField0')),
4153
			'option A',
4154
			'/label',
4155
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4156
			array('label' => array('for' => 'ModelField1')),
4157
			'option B',
4158
			'/label',
4159
			'/fieldset'
4160
		);
4161
		$this->assertTags($result, $expected);
4162
 
4163
		$result = $this->Form->radio(
4164
			'Model.field',
4165
			array('option A', 'option B'),
4166
			array('disabled' => true, 'value' => '0')
4167
		);
4168
		$expected = array(
4169
			'fieldset' => array(),
4170
			'legend' => array(),
4171
			'Field',
4172
			'/legend',
4173
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4174
			array('label' => array('for' => 'ModelField0')),
4175
			'option A',
4176
			'/label',
4177
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
4178
			array('label' => array('for' => 'ModelField1')),
4179
			'option B',
4180
			'/label',
4181
			'/fieldset'
4182
		);
4183
		$this->assertTags($result, $expected);
4184
 
4185
		$result = $this->Form->radio(
4186
			'Model.field',
4187
			array('option A', 'option B'),
4188
			array('disabled' => 'disabled', 'value' => '0')
4189
		);
4190
		$expected = array(
4191
			'fieldset' => array(),
4192
			'legend' => array(),
4193
			'Field',
4194
			'/legend',
4195
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4196
			array('label' => array('for' => 'ModelField0')),
4197
			'option A',
4198
			'/label',
4199
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
4200
			array('label' => array('for' => 'ModelField1')),
4201
			'option B',
4202
			'/label',
4203
			'/fieldset'
4204
		);
4205
		$this->assertTags($result, $expected);
4206
 
4207
		$result = $this->Form->input('Model.field', array(
4208
			'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
4209
			'type' => 'radio',
4210
			'disabled' => array(2, '3x'),
4211
		));
4212
 
4213
		$expected = array(
4214
			'div' => array('class' => 'input radio'),
4215
			'fieldset' => array(),
4216
			'legend' => array(),
4217
			'Field',
4218
			'/legend',
4219
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4220
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4221
			array('label' => array('for' => 'ModelField1')),
4222
			'first',
4223
			'/label',
4224
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
4225
			array('label' => array('for' => 'ModelField2')),
4226
			'second',
4227
			'/label',
4228
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
4229
			array('label' => array('for' => 'ModelField2x')),
4230
			'2x',
4231
			'/label',
4232
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
4233
			array('label' => array('for' => 'ModelField3')),
4234
			'third',
4235
			'/label',
4236
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
4237
			array('label' => array('for' => 'ModelField3x')),
4238
			'3x',
4239
			'/label',
4240
			'/fieldset',
4241
			'/div'
4242
		);
4243
		$this->assertTags($result, $expected);
4244
 
4245
		$result = $this->Form->input('Model.field', array(
4246
			'type' => 'radio',
4247
			'options' => array(
4248
				1 => 'A',
4249
				2 => 'B',
4250
				3 => 'C'
4251
			),
4252
			'disabled' => array(1)
4253
		));
4254
 
4255
		$expected = array(
4256
			'div' => array('class' => 'input radio'),
4257
			'fieldset' => array(),
4258
			'legend' => array(),
4259
			'Field',
4260
			'/legend',
4261
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4262
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
4263
			array('label' => array('for' => 'ModelField1')),
4264
			'A',
4265
			'/label',
4266
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
4267
			array('label' => array('for' => 'ModelField2')),
4268
			'B',
4269
			'/label',
4270
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
4271
			array('label' => array('for' => 'ModelField3')),
4272
			'C',
4273
			'/label',
4274
			'/fieldset',
4275
			'/div'
4276
		);
4277
		$this->assertTags($result, $expected);
4278
	}
4279
 
4280
/**
4281
 * test disabling the hidden input for radio buttons
4282
 *
4283
 * @return void
4284
 */
4285
	public function testRadioHiddenInputDisabling() {
4286
		$result = $this->Form->input('Model.1.field', array(
4287
				'type' => 'radio',
4288
				'options' => array('option A'),
4289
				'hiddenField' => false
4290
			)
4291
		);
4292
		$expected = array(
4293
			'div' => array('class' => 'input radio'),
4294
			'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
4295
			'label' => array('for' => 'Model1Field0'),
4296
			'option A',
4297
			'/label',
4298
			'/div'
4299
		);
4300
		$this->assertTags($result, $expected);
4301
 
4302
		$result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
4303
		$expected = array(
4304
			'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
4305
			'label' => array('for' => 'Model1Field0'),
4306
			'option A',
4307
			'/label'
4308
		);
4309
		$this->assertTags($result, $expected);
4310
	}
4311
 
4312
/**
4313
 * test adding an empty option for radio buttons
4314
 *
4315
 * @return void
4316
 */
4317
	public function testRadioAddEmptyOption() {
4318
		$result = $this->Form->input('Model.1.field', array(
4319
			'type' => 'radio',
4320
			'options' => array('option A'),
4321
			'empty' => true,
4322
			'hiddenField' => false
4323
		));
4324
		$expected = array(
4325
			'div' => array('class' => 'input radio'),
4326
				'fieldset' => array(),
4327
					'legend' => array(),
4328
						'Field',
4329
					'/legend',
4330
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
4331
					array('label' => array('for' => 'Model1Field')),
4332
						__('empty'),
4333
					'/label',
4334
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
4335
					array('label' => array('for' => 'Model1Field0')),
4336
						'option A',
4337
					'/label',
4338
				'/fieldset',
4339
			'/div'
4340
		);
4341
		$this->assertTags($result, $expected);
4342
 
4343
		$result = $this->Form->input('Model.1.field', array(
4344
			'type' => 'radio',
4345
			'options' => array('option A'),
4346
			'empty' => 'CustomEmptyLabel',
4347
			'hiddenField' => false
4348
		));
4349
		$expected = array(
4350
			'div' => array('class' => 'input radio'),
4351
				'fieldset' => array(),
4352
					'legend' => array(),
4353
						'Field',
4354
					'/legend',
4355
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
4356
					array('label' => array('for' => 'Model1Field')),
4357
						'CustomEmptyLabel',
4358
					'/label',
4359
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
4360
					array('label' => array('for' => 'Model1Field0')),
4361
						'option A',
4362
					'/label',
4363
				'/fieldset',
4364
			'/div'
4365
		);
4366
		$this->assertTags($result, $expected);
4367
 
4368
		$result = $this->Form->input('Model.1.field', array(
4369
			'type' => 'radio',
4370
			'options' => array('option A'),
4371
			'empty' => false,
4372
			'hiddenField' => false
4373
		));
4374
		$this->assertTextNotContains('"Model1Field"', $result);
4375
	}
4376
 
4377
/**
4378
 * Test that radio() accepts an array for label
4379
 *
4380
 * @return void
4381
 */
4382
	public function testRadioLabelArray() {
4383
		$result = $this->Form->input('Model.field', array(
4384
			'type' => 'radio',
4385
			'legend' => false,
4386
			'label' => array(
4387
				'class' => 'checkbox float-left',
4388
			),
4389
			'options' => array('1' => 'Option A', '2' => 'Option B.')
4390
		));
4391
		$this->assertTextContains(
4392
			'<label for="ModelField1" class="checkbox float-left">Option A</label>',
4393
			$result
4394
		);
4395
	}
4396
 
4397
/**
4398
 * Test that label id's match the input element id's when radio is called after create().
4399
 *
4400
 * @return void
4401
 */
4402
	public function testRadioWithCreate() {
4403
		$this->Form->create('Model');
4404
		$result = $this->Form->radio('recipient',
4405
			array('1' => '1', '2' => '2', '3' => '3'),
4406
			array('legend' => false, 'value' => '1')
4407
		);
4408
		$this->assertTextNotContains(
4409
			'<label for="ModelModelRecipient1">1</label>',
4410
			$result
4411
		);
4412
		$this->assertTextContains(
4413
			'<label for="ModelRecipient1">1</label>',
4414
			$result
4415
		);
4416
	}
4417
 
4418
/**
4419
 * testDomIdSuffix method
4420
 *
4421
 * @return void
4422
 */
4423
	public function testDomIdSuffix() {
4424
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
4425
		$this->assertEquals('1StringWith1DollarSigns', $result);
4426
 
4427
		$result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
4428
		$this->assertEquals('AbcXFooYBar', $result);
4429
 
4430
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
4431
		$this->assertEquals('1StringWith1$-dollarSigns', $result);
4432
 
4433
		$result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
4434
		$this->assertEquals('AbcX=FooY=Bar', $result);
4435
	}
4436
 
4437
/**
4438
 * testDomIdSuffixCollisionResolvement()
4439
 *
4440
 * @return void
4441
 */
4442
	public function testDomIdSuffixCollisionResolvement() {
4443
		$result = $this->Form->domIdSuffix('a>b');
4444
		$this->assertEquals('AB', $result);
4445
 
4446
		$result = $this->Form->domIdSuffix('a<b');
4447
		$this->assertEquals('AB1', $result);
4448
 
4449
		$result = $this->Form->domIdSuffix('a\'b');
4450
		$this->assertEquals('AB2', $result);
4451
 
4452
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4453
		$this->assertEquals('1StringWith1Dollar', $result);
4454
 
4455
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4456
		$this->assertEquals('1StringWith1Dollar1', $result);
4457
 
4458
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4459
		$this->assertEquals('1StringWith1Dollar2', $result);
4460
	}
4461
 
4462
/**
4463
 * testSelect method
4464
 *
4465
 * Test select element generation.
4466
 *
4467
 * @return void
4468
 */
4469
	public function testSelect() {
4470
		$result = $this->Form->select('Model.field', array());
4471
		$expected = array(
4472
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4473
			array('option' => array('value' => '')),
4474
			'/option',
4475
			'/select'
4476
		);
4477
		$this->assertTags($result, $expected);
4478
 
4479
		$this->Form->request->data = array('Model' => array('field' => 'value'));
4480
		$result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
4481
		$expected = array(
4482
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4483
			array('option' => array('value' => '')),
4484
			'/option',
4485
			array('option' => array('value' => 'value', 'selected' => 'selected')),
4486
			'good',
4487
			'/option',
4488
			array('option' => array('value' => 'other')),
4489
			'bad',
4490
			'/option',
4491
			'/select'
4492
		);
4493
		$this->assertTags($result, $expected);
4494
 
4495
		$this->Form->request->data = array();
4496
		$result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
4497
		$expected = array(
4498
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4499
			array('option' => array('value' => '')),
4500
			'/option',
4501
			array('option' => array('value' => 'value')),
4502
			'good',
4503
			'/option',
4504
			array('option' => array('value' => 'other')),
4505
			'bad',
4506
			'/option',
4507
			'/select'
4508
		);
4509
		$this->assertTags($result, $expected);
4510
 
4511
		$result = $this->Form->select(
4512
			'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
4513
			array('empty' => false)
4514
		);
4515
		$expected = array(
4516
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4517
			array('option' => array('value' => 'first')),
4518
			'first &quot;html&quot; &lt;chars&gt;',
4519
			'/option',
4520
			array('option' => array('value' => 'second')),
4521
			'value',
4522
			'/option',
4523
			'/select'
4524
		);
4525
		$this->assertTags($result, $expected);
4526
 
4527
		$result = $this->Form->select(
4528
			'Model.field',
4529
			array('first' => 'first "html" <chars>', 'second' => 'value'),
4530
			array('escape' => false, 'empty' => false)
4531
		);
4532
		$expected = array(
4533
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4534
			array('option' => array('value' => 'first')),
4535
			'first "html" <chars>',
4536
			'/option',
4537
			array('option' => array('value' => 'second')),
4538
			'value',
4539
			'/option',
4540
			'/select'
4541
		);
4542
		$this->assertTags($result, $expected);
4543
 
4544
		$options = array(
4545
			array('value' => 'first', 'name' => 'First'),
4546
			array('value' => 'first', 'name' => 'Another First'),
4547
		);
4548
		$result = $this->Form->select(
4549
			'Model.field',
4550
			$options,
4551
			array('escape' => false, 'empty' => false)
4552
		);
4553
		$expected = array(
4554
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4555
			array('option' => array('value' => 'first')),
4556
			'First',
4557
			'/option',
4558
			array('option' => array('value' => 'first')),
4559
			'Another First',
4560
			'/option',
4561
			'/select'
4562
		);
4563
		$this->assertTags($result, $expected);
4564
 
4565
		$this->Form->request->data = array('Model' => array('contact_id' => 228));
4566
		$result = $this->Form->select(
4567
			'Model.contact_id',
4568
			array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
4569
			array('escape' => false, 'empty' => 'pick something')
4570
		);
4571
 
4572
		$expected = array(
4573
			'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
4574
			array('option' => array('value' => '')), 'pick something', '/option',
4575
			array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
4576
			array('option' => array('value' => '228-1')), '228-1 value', '/option',
4577
			array('option' => array('value' => '228-2')), '228-2 value', '/option',
4578
			'/select'
4579
		);
4580
		$this->assertTags($result, $expected);
4581
 
4582
		$this->Form->request->data['Model']['field'] = 0;
4583
		$result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
4584
		$expected = array(
4585
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4586
			array('option' => array('value' => '')), '/option',
4587
			array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
4588
			array('option' => array('value' => '1')), 'Yes', '/option',
4589
			'/select'
4590
		);
4591
		$this->assertTags($result, $expected);
4592
 
4593
		$this->Form->request->data['Model']['field'] = 50;
4594
		$result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
4595
		$expected = array(
4596
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4597
			array('option' => array('value' => '')), '/option',
4598
			array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
4599
			array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
4600
			'/select'
4601
		);
4602
		$this->assertTags($result, $expected);
4603
 
4604
		$result = $this->Form->select('Contact.required_one', array('option A'));
4605
		$expected = array(
4606
			'select' => array(
4607
				'name' => 'data[Contact][required_one]',
4608
				'id' => 'ContactRequiredOne',
4609
				'required' => 'required'
4610
			),
4611
			array('option' => array('value' => '')), '/option',
4612
			array('option' => array('value' => '0')), 'option A', '/option',
4613
			'/select'
4614
		);
4615
		$this->assertTags($result, $expected);
4616
 
4617
		$result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
4618
		$expected = array(
4619
			'select' => array(
4620
				'name' => 'data[Contact][required_one]',
4621
				'id' => 'ContactRequiredOne',
4622
				'disabled' => 'disabled'
4623
			),
4624
			array('option' => array('value' => '')), '/option',
4625
			array('option' => array('value' => '0')), 'option A', '/option',
4626
			'/select'
4627
		);
4628
		$this->assertTags($result, $expected);
4629
	}
4630
 
4631
/**
4632
 * test that select() with optiongroups listens to the escape param.
4633
 *
4634
 * @return void
4635
 */
4636
	public function testSelectOptionGroupEscaping() {
4637
		$options = array(
4638
			'>< Key' => array(
4639
				1 => 'One',
4640
				2 => 'Two'
4641
			)
4642
		);
4643
		$result = $this->Form->select('Model.field', $options, array('empty' => false));
4644
		$expected = array(
4645
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4646
			'optgroup' => array('label' => '&gt;&lt; Key'),
4647
			array('option' => array('value' => '1')), 'One', '/option',
4648
			array('option' => array('value' => '2')), 'Two', '/option',
4649
			'/optgroup',
4650
			'/select'
4651
		);
4652
		$this->assertTags($result, $expected);
4653
 
4654
		$options = array(
4655
			'>< Key' => array(
4656
				1 => 'One',
4657
				2 => 'Two'
4658
			)
4659
		);
4660
		$result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
4661
		$expected = array(
4662
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4663
			'optgroup' => array('label' => '>< Key'),
4664
			array('option' => array('value' => '1')), 'One', '/option',
4665
			array('option' => array('value' => '2')), 'Two', '/option',
4666
			'/optgroup',
4667
			'/select'
4668
		);
4669
		$this->assertTags($result, $expected);
4670
	}
4671
 
4672
/**
4673
 * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
4674
 *
4675
 * @return void
4676
 */
4677
	public function testSelectWithNullAttributes() {
4678
		$result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
4679
		$expected = array(
4680
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4681
			array('option' => array('value' => '0')),
4682
			'first',
4683
			'/option',
4684
			array('option' => array('value' => '1')),
4685
			'second',
4686
			'/option',
4687
			'/select'
4688
		);
4689
		$this->assertTags($result, $expected);
4690
	}
4691
 
4692
/**
4693
 * testNestedSelect method
4694
 *
4695
 * test select element generation with optgroups
4696
 *
4697
 * @return void
4698
 */
4699
	public function testNestedSelect() {
4700
		$result = $this->Form->select(
4701
			'Model.field',
4702
			array(1 => 'One', 2 => 'Two', 'Three' => array(
4703
				3 => 'Three', 4 => 'Four', 5 => 'Five'
4704
			)), array('empty' => false)
4705
		);
4706
		$expected = array(
4707
			'select' => array('name' => 'data[Model][field]',
4708
					'id' => 'ModelField'),
4709
					array('option' => array('value' => 1)),
4710
					'One',
4711
					'/option',
4712
					array('option' => array('value' => 2)),
4713
					'Two',
4714
					'/option',
4715
					array('optgroup' => array('label' => 'Three')),
4716
						array('option' => array('value' => 4)),
4717
						'Four',
4718
						'/option',
4719
						array('option' => array('value' => 5)),
4720
						'Five',
4721
						'/option',
4722
					'/optgroup',
4723
					'/select'
4724
					);
4725
		$this->assertTags($result, $expected);
4726
 
4727
		$result = $this->Form->select(
4728
			'Model.field',
4729
			array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
4730
			array('showParents' => true, 'empty' => false)
4731
		);
4732
 
4733
		$expected = array(
4734
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4735
				array('option' => array('value' => 1)),
4736
				'One',
4737
				'/option',
4738
				array('option' => array('value' => 2)),
4739
				'Two',
4740
				'/option',
4741
				array('optgroup' => array('label' => 'Three')),
4742
					array('option' => array('value' => 3)),
4743
					'Three',
4744
					'/option',
4745
					array('option' => array('value' => 4)),
4746
					'Four',
4747
					'/option',
4748
				'/optgroup',
4749
			'/select'
4750
		);
4751
		$this->assertTags($result, $expected);
4752
	}
4753
 
4754
/**
4755
 * testSelectMultiple method
4756
 *
4757
 * test generation of multiple select elements
4758
 *
4759
 * @return void
4760
 */
4761
	public function testSelectMultiple() {
4762
		$options = array('first', 'second', 'third');
4763
		$result = $this->Form->select(
4764
			'Model.multi_field', $options, array('multiple' => true)
4765
		);
4766
		$expected = array(
4767
			'input' => array(
4768
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
4769
			),
4770
			'select' => array(
4771
				'name' => 'data[Model][multi_field][]',
4772
				'id' => 'ModelMultiField', 'multiple' => 'multiple'
4773
			),
4774
			array('option' => array('value' => '0')),
4775
			'first',
4776
			'/option',
4777
			array('option' => array('value' => '1')),
4778
			'second',
4779
			'/option',
4780
			array('option' => array('value' => '2')),
4781
			'third',
4782
			'/option',
4783
			'/select'
4784
		);
4785
		$this->assertTags($result, $expected);
4786
 
4787
		$result = $this->Form->select(
4788
			'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple')
4789
		);
4790
		$expected = array(
4791
			'input' => array(
4792
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_',
4793
				'form' => 'my-form',
4794
			),
4795
			'select' => array(
4796
				'name' => 'data[Model][multi_field][]',
4797
				'id' => 'ModelMultiField',
4798
				'multiple' => 'multiple',
4799
				'form' => 'my-form',
4800
			),
4801
			array('option' => array('value' => '0')),
4802
			'first',
4803
			'/option',
4804
			array('option' => array('value' => '1')),
4805
			'second',
4806
			'/option',
4807
			array('option' => array('value' => '2')),
4808
			'third',
4809
			'/option',
4810
			'/select'
4811
		);
4812
		$this->assertTags($result, $expected);
4813
 
4814
		$result = $this->Form->select(
4815
			'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
4816
		);
4817
		$expected = array(
4818
			'input' => array(
4819
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
4820
			),
4821
			'select' => array(
4822
				'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
4823
				'multiple' => 'multiple'
4824
			),
4825
			array('option' => array('value' => '0', 'selected' => 'selected')),
4826
			'first',
4827
			'/option',
4828
			array('option' => array('value' => '1', 'selected' => 'selected')),
4829
			'second',
4830
			'/option',
4831
			array('option' => array('value' => '2')),
4832
			'third',
4833
			'/option',
4834
			'/select'
4835
		);
4836
		$this->assertTags($result, $expected);
4837
 
4838
		$result = $this->Form->select(
4839
			'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
4840
		);
4841
		$expected = array(
4842
			'select' => array(
4843
				'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
4844
			),
4845
			array('option' => array('value' => '0', 'selected' => 'selected')),
4846
			'first',
4847
			'/option',
4848
			array('option' => array('value' => '1', 'selected' => 'selected')),
4849
			'second',
4850
			'/option',
4851
			array('option' => array('value' => '2')),
4852
			'third',
4853
			'/option',
4854
			'/select'
4855
		);
4856
		$this->assertTags($result, $expected);
4857
 
4858
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
4859
		$selected = array('2', '3x');
4860
		$result = $this->Form->select(
4861
			'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
4862
		);
4863
		$expected = array(
4864
			'input' => array(
4865
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
4866
			),
4867
			'select' => array(
4868
				'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
4869
			),
4870
			array('option' => array('value' => '1')),
4871
			'One',
4872
			'/option',
4873
			array('option' => array('value' => '2', 'selected' => 'selected')),
4874
			'Two',
4875
			'/option',
4876
			array('option' => array('value' => '3')),
4877
			'Three',
4878
			'/option',
4879
			array('option' => array('value' => '3x', 'selected' => 'selected')),
4880
			'Stringy',
4881
			'/option',
4882
			'/select'
4883
		);
4884
		$this->assertTags($result, $expected);
4885
 
4886
		$result = $this->Form->select('Contact.required_one', array(
4887
			'1' => 'option A',
4888
			'2' => 'option B'
4889
		), array('multiple' => true));
4890
		$expected = array(
4891
			'input' => array(
4892
				'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
4893
			),
4894
			'select' => array(
4895
				'name' => 'data[Contact][required_one][]',
4896
				'id' => 'ContactRequiredOne',
4897
				'required' => 'required',
4898
				'multiple' => 'multiple'
4899
			),
4900
			array('option' => array('value' => '1')), 'option A', '/option',
4901
			array('option' => array('value' => '2')), 'option B', '/option',
4902
			'/select'
4903
		);
4904
		$this->assertTags($result, $expected);
4905
 
4906
		$result = $this->Form->select(
4907
			'Model.multi_field',
4908
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
4909
			array('multiple' => true)
4910
		);
4911
		$expected = array(
4912
			'input' => array(
4913
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
4914
				'id' => 'ModelMultiField_'
4915
			),
4916
			array('select' => array('name' => 'data[Model][multi_field][]',
4917
				'multiple' => 'multiple', 'id' => 'ModelMultiField'
4918
			)),
4919
			array('option' => array('value' => 'a&gt;b')),
4920
			'first',
4921
			'/option',
4922
			array('option' => array('value' => 'a&lt;b')),
4923
			'second',
4924
			'/option',
4925
			array('option' => array(
4926
				'value' => 'a&quot;b'
4927
			)),
4928
			'third',
4929
			'/option',
4930
			'/select'
4931
		);
4932
		$this->assertTags($result, $expected);
4933
	}
4934
 
4935
/**
4936
 * Test generating multiple select with disabled elements.
4937
 *
4938
 * @return void
4939
 */
4940
	public function testSelectMultipleWithDisabledElements() {
4941
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
4942
		$disabled = array(1);
4943
		$result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
4944
		$expected = array(
4945
			'input' => array(
4946
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
4947
			),
4948
			'select' => array(
4949
				'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
4950
			),
4951
			array('option' => array('value' => '1', 'disabled' => 'disabled')),
4952
			'One',
4953
			'/option',
4954
			array('option' => array('value' => '2')),
4955
			'Two',
4956
			'/option',
4957
			array('option' => array('value' => '3')),
4958
			'Three',
4959
			'/option',
4960
			array('option' => array('value' => '3x')),
4961
			'Stringy',
4962
			'/option',
4963
			'/select'
4964
		);
4965
		$this->assertTags($result, $expected);
4966
 
4967
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
4968
		$disabled = array('2', '3x');
4969
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
4970
		$expected = array(
4971
			array('div' => array('class' => 'input select')),
4972
			array('label' => array('for' => 'ContactMultiple')),
4973
			'Multiple',
4974
			'/label',
4975
			'input' => array(
4976
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
4977
			),
4978
			'select' => array(
4979
				'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
4980
			),
4981
			array('option' => array('value' => '1')),
4982
			'One',
4983
			'/option',
4984
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
4985
			'Two',
4986
			'/option',
4987
			array('option' => array('value' => '3')),
4988
			'Three',
4989
			'/option',
4990
			array('option' => array('value' => '3x', 'disabled' => 'disabled')),
4991
			'Stringy',
4992
			'/option',
4993
			'/select',
4994
			'/div'
4995
		);
4996
		$this->assertTags($result, $expected);
4997
 
4998
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
4999
		$disabled = true;
5000
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
5001
		$expected = array(
5002
			array('div' => array('class' => 'input select')),
5003
			array('label' => array('for' => 'ContactMultiple')),
5004
			'Multiple',
5005
			'/label',
5006
			'input' => array(
5007
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
5008
			),
5009
			'select' => array(
5010
				'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
5011
			),
5012
			array('option' => array('value' => '1')),
5013
			'One',
5014
			'/option',
5015
			array('option' => array('value' => '2')),
5016
			'Two',
5017
			'/option',
5018
			array('option' => array('value' => '3')),
5019
			'Three',
5020
			'/option',
5021
			array('option' => array('value' => '3x')),
5022
			'Stringy',
5023
			'/option',
5024
			'/select',
5025
			'/div'
5026
		);
5027
		$this->assertTags($result, $expected);
5028
	}
5029
 
5030
/**
5031
 * Test generating select with disabled elements.
5032
 *
5033
 * @return void
5034
 */
5035
	public function testSelectWithDisabledElements() {
5036
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5037
		$disabled = array(2, 3);
5038
		$result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
5039
		$expected = array(
5040
			'select' => array(
5041
				'name' => 'data[Model][field]', 'id' => 'ModelField'
5042
			),
5043
			array('option' => array('value' => '')),
5044
			'/option',
5045
			array('option' => array('value' => '1')),
5046
			'One',
5047
			'/option',
5048
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
5049
			'Two',
5050
			'/option',
5051
			array('option' => array('value' => '3', 'disabled' => 'disabled')),
5052
			'Three',
5053
			'/option',
5054
			array('option' => array('value' => '3x')),
5055
			'Stringy',
5056
			'/option',
5057
			'/select'
5058
		);
5059
		$this->assertTags($result, $expected);
5060
 
5061
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5062
		$disabled = array('2', '3x');
5063
		$result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
5064
		$expected = array(
5065
			array('div' => array('class' => 'input select')),
5066
			array('label' => array('for' => 'ModelField')),
5067
			'Field',
5068
			'/label',
5069
			'select' => array(
5070
				'name' => 'data[Model][field]', 'id' => 'ModelField'
5071
			),
5072
			array('option' => array('value' => '1')),
5073
			'One',
5074
			'/option',
5075
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
5076
			'Two',
5077
			'/option',
5078
			array('option' => array('value' => '3')),
5079
			'Three',
5080
			'/option',
5081
			array('option' => array('value' => '3x', 'disabled' => 'disabled')),
5082
			'Stringy',
5083
			'/option',
5084
			'/select',
5085
			'/div'
5086
		);
5087
		$this->assertTags($result, $expected);
5088
 
5089
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5090
		$disabled = true;
5091
		$result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
5092
		$expected = array(
5093
			array('div' => array('class' => 'input select')),
5094
			array('label' => array('for' => 'ModelField')),
5095
			'Field',
5096
			'/label',
5097
			'select' => array(
5098
				'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
5099
			),
5100
			array('option' => array('value' => '1')),
5101
			'One',
5102
			'/option',
5103
			array('option' => array('value' => '2')),
5104
			'Two',
5105
			'/option',
5106
			array('option' => array('value' => '3')),
5107
			'Three',
5108
			'/option',
5109
			array('option' => array('value' => '3x')),
5110
			'Stringy',
5111
			'/option',
5112
			'/select',
5113
			'/div'
5114
		);
5115
		$this->assertTags($result, $expected);
5116
	}
5117
 
5118
/**
5119
 * test generation of habtm select boxes.
5120
 *
5121
 * @return void
5122
 */
5123
	public function testHabtmSelectBox() {
5124
		$this->View->viewVars['contactTags'] = array(
5125
			1 => 'blue',
5126
			2 => 'red',
5127
			3 => 'green'
5128
		);
5129
		$this->Form->request->data = array(
5130
			'Contact' => array(),
5131
			'ContactTag' => array(
5132
				array(
5133
					'id' => '1',
5134
					'name' => 'blue'
5135
				),
5136
				array(
5137
					'id' => 3,
5138
					'name' => 'green'
5139
				)
5140
			)
5141
		);
5142
		$this->Form->create('Contact');
5143
		$result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
5144
		$expected = array(
5145
			'input' => array(
5146
				'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
5147
			),
5148
			'select' => array(
5149
				'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
5150
				'multiple' => 'multiple'
5151
			),
5152
			array('option' => array('value' => '1', 'selected' => 'selected')),
5153
			'blue',
5154
			'/option',
5155
			array('option' => array('value' => '2')),
5156
			'red',
5157
			'/option',
5158
			array('option' => array('value' => '3', 'selected' => 'selected')),
5159
			'green',
5160
			'/option',
5161
			'/select'
5162
		);
5163
		$this->assertTags($result, $expected);
5164
 
5165
		// make sure only 50 is selected, and not 50f5c0cf
5166
		$this->View->viewVars['contactTags'] = array(
5167
			'1' => 'blue',
5168
			'50f5c0cf' => 'red',
5169
			'50' => 'green'
5170
		);
5171
		$this->Form->request->data = array(
5172
			'Contact' => array(),
5173
			'ContactTag' => array(
5174
				array(
5175
					'id' => 1,
5176
					'name' => 'blue'
5177
				),
5178
				array(
5179
					'id' => 50,
5180
					'name' => 'green'
5181
				)
5182
			)
5183
		);
5184
		$this->Form->create('Contact');
5185
		$result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
5186
		$expected = array(
5187
			'input' => array(
5188
				'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
5189
			),
5190
			'select' => array(
5191
				'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
5192
				'multiple' => 'multiple'
5193
			),
5194
			array('option' => array('value' => '1', 'selected' => 'selected')),
5195
			'blue',
5196
			'/option',
5197
			array('option' => array('value' => '50f5c0cf')),
5198
			'red',
5199
			'/option',
5200
			array('option' => array('value' => '50', 'selected' => 'selected')),
5201
			'green',
5202
			'/option',
5203
			'/select'
5204
		);
5205
		$this->assertTags($result, $expected);
5206
	}
5207
 
5208
/**
5209
 * test generation of multi select elements in checkbox format
5210
 *
5211
 * @return void
5212
 */
5213
	public function testSelectMultipleCheckboxes() {
5214
		$result = $this->Form->select(
5215
			'Model.multi_field',
5216
			array('first', 'second', 'third'),
5217
			array('multiple' => 'checkbox')
5218
		);
5219
 
5220
		$expected = array(
5221
			'input' => array(
5222
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5223
			),
5224
			array('div' => array('class' => 'checkbox')),
5225
			array('input' => array(
5226
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5227
				'value' => '0', 'id' => 'ModelMultiField0'
5228
			)),
5229
			array('label' => array('for' => 'ModelMultiField0')),
5230
			'first',
5231
			'/label',
5232
			'/div',
5233
			array('div' => array('class' => 'checkbox')),
5234
			array('input' => array(
5235
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5236
				'value' => '1', 'id' => 'ModelMultiField1'
5237
			)),
5238
			array('label' => array('for' => 'ModelMultiField1')),
5239
			'second',
5240
			'/label',
5241
			'/div',
5242
			array('div' => array('class' => 'checkbox')),
5243
			array('input' => array(
5244
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5245
				'value' => '2', 'id' => 'ModelMultiField2'
5246
			)),
5247
			array('label' => array('for' => 'ModelMultiField2')),
5248
			'third',
5249
			'/label',
5250
			'/div'
5251
		);
5252
		$this->assertTags($result, $expected);
5253
 
5254
		$result = $this->Form->select(
5255
			'Model.multi_field',
5256
			array('a' => 'first', 'b' => 'second', 'c' => 'third'),
5257
			array('multiple' => 'checkbox')
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' => 'a', 'id' => 'ModelMultiFieldA'
5267
			)),
5268
			array('label' => array('for' => 'ModelMultiFieldA')),
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' => 'b', 'id' => 'ModelMultiFieldB'
5276
			)),
5277
			array('label' => array('for' => 'ModelMultiFieldB')),
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' => 'c', 'id' => 'ModelMultiFieldC'
5285
			)),
5286
			array('label' => array('for' => 'ModelMultiFieldC')),
5287
			'third',
5288
			'/label',
5289
			'/div'
5290
		);
5291
		$this->assertTags($result, $expected);
5292
 
5293
		$result = $this->Form->select(
5294
			'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
5295
		);
5296
		$expected = array(
5297
			'input' => array(
5298
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5299
			),
5300
			array('div' => array('class' => 'checkbox')),
5301
			array('input' => array(
5302
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5303
				'value' => '1', 'id' => 'ModelMultiField1'
5304
			)),
5305
			array('label' => array('for' => 'ModelMultiField1')),
5306
			'first',
5307
			'/label',
5308
			'/div'
5309
		);
5310
		$this->assertTags($result, $expected);
5311
 
5312
		$this->Form->request->data = array('Model' => array('tags' => array(1)));
5313
		$result = $this->Form->select(
5314
			'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
5315
		);
5316
		$expected = array(
5317
			'input' => array(
5318
				'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
5319
			),
5320
			array('div' => array('class' => 'checkbox')),
5321
			array('input' => array(
5322
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5323
				'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
5324
			)),
5325
			array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
5326
			'first',
5327
			'/label',
5328
			'/div',
5329
 
5330
			array('div' => array('class' => 'checkbox')),
5331
			array('input' => array(
5332
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5333
				'value' => 'Array', 'id' => 'ModelTagsArray'
5334
			)),
5335
			array('label' => array('for' => 'ModelTagsArray')),
5336
			'Array',
5337
			'/label',
5338
			'/div'
5339
		);
5340
		$this->assertTags($result, $expected);
5341
 
5342
		$result = $this->Form->select(
5343
			'Model.multi_field',
5344
			array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
5345
			array('multiple' => 'checkbox')
5346
		);
5347
		$expected = array(
5348
			'input' => array(
5349
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5350
			),
5351
			array('div' => array('class' => 'checkbox')),
5352
			array('input' => array(
5353
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5354
				'value' => 'a+', 'id' => 'ModelMultiFieldA2'
5355
			)),
5356
			array('label' => array('for' => 'ModelMultiFieldA2')),
5357
			'first',
5358
			'/label',
5359
			'/div',
5360
			array('div' => array('class' => 'checkbox')),
5361
			array('input' => array(
5362
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5363
				'value' => 'a++', 'id' => 'ModelMultiFieldA1'
5364
			)),
5365
			array('label' => array('for' => 'ModelMultiFieldA1')),
5366
			'second',
5367
			'/label',
5368
			'/div',
5369
			array('div' => array('class' => 'checkbox')),
5370
			array('input' => array(
5371
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5372
				'value' => 'a+++', 'id' => 'ModelMultiFieldA'
5373
			)),
5374
			array('label' => array('for' => 'ModelMultiFieldA')),
5375
			'third',
5376
			'/label',
5377
			'/div'
5378
		);
5379
 
5380
		$this->assertTags($result, $expected);
5381
 
5382
		$result = $this->Form->select(
5383
			'Model.multi_field',
5384
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
5385
			array('multiple' => 'checkbox')
5386
		);
5387
		$expected = array(
5388
			'input' => array(
5389
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5390
			),
5391
			array('div' => array('class' => 'checkbox')),
5392
			array('input' => array(
5393
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5394
				'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
5395
			)),
5396
			array('label' => array('for' => 'ModelMultiFieldAB2')),
5397
			'first',
5398
			'/label',
5399
			'/div',
5400
			array('div' => array('class' => 'checkbox')),
5401
			array('input' => array(
5402
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5403
				'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
5404
			)),
5405
			array('label' => array('for' => 'ModelMultiFieldAB1')),
5406
			'second',
5407
			'/label',
5408
			'/div',
5409
			array('div' => array('class' => 'checkbox')),
5410
			array('input' => array(
5411
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5412
				'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
5413
			)),
5414
			array('label' => array('for' => 'ModelMultiFieldAB')),
5415
			'third',
5416
			'/label',
5417
			'/div'
5418
		);
5419
		$this->assertTags($result, $expected);
5420
	}
5421
 
5422
/**
5423
 * test multiple checkboxes with div styles.
5424
 *
5425
 * @return void
5426
 */
5427
	public function testSelectMultipleCheckboxDiv() {
5428
		$result = $this->Form->select(
5429
			'Model.tags',
5430
			array('first', 'second'),
5431
			array('multiple' => 'checkbox', 'class' => 'my-class')
5432
		);
5433
		$expected = array(
5434
			'input' => array(
5435
				'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
5436
			),
5437
			array('div' => array('class' => 'my-class')),
5438
			array('input' => array(
5439
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5440
				'value' => '0', 'id' => 'ModelTags0'
5441
			)),
5442
			array('label' => array('for' => 'ModelTags0')), 'first', '/label',
5443
			'/div',
5444
 
5445
			array('div' => array('class' => 'my-class')),
5446
			array('input' => array(
5447
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5448
				'value' => '1', 'id' => 'ModelTags1'
5449
			)),
5450
			array('label' => array('for' => 'ModelTags1')), 'second', '/label',
5451
			'/div'
5452
		);
5453
		$this->assertTags($result, $expected);
5454
 
5455
		$result = $this->Form->input('Model.tags', array(
5456
			'options' => array('first', 'second'),
5457
			'multiple' => 'checkbox',
5458
			'class' => 'my-class',
5459
			'div' => false,
5460
			'label' => false
5461
		));
5462
		$this->assertTags($result, $expected);
5463
 
5464
		$Contact = ClassRegistry::getObject('Contact');
5465
		$Contact->validationErrors['tags'] = 'Select atleast one option';
5466
		$result = $this->Form->input('Contact.tags', array(
5467
			'options' => array('one'),
5468
			'multiple' => 'checkbox',
5469
			'label' => false,
5470
			'div' => false
5471
		));
5472
		$expected = array(
5473
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
5474
			array('div' => array('class' => 'checkbox form-error')),
5475
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
5476
			array('label' => array('for' => 'ContactTags0')),
5477
			'one',
5478
			'/label',
5479
			'/div'
5480
		);
5481
		$this->assertTags($result, $expected);
5482
 
5483
		$result = $this->Form->input('Contact.tags', array(
5484
			'options' => array('one'),
5485
			'multiple' => 'checkbox',
5486
			'class' => 'mycheckbox',
5487
			'label' => false,
5488
			'div' => false
5489
		));
5490
		$expected = array(
5491
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
5492
			array('div' => array('class' => 'mycheckbox form-error')),
5493
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
5494
			array('label' => array('for' => 'ContactTags0')),
5495
			'one',
5496
			'/label',
5497
			'/div'
5498
		);
5499
		$this->assertTags($result, $expected);
5500
	}
5501
 
5502
/**
5503
 * Checks the security hash array generated for multiple-input checkbox elements
5504
 *
5505
 * @return void
5506
 */
5507
	public function testSelectMultipleCheckboxSecurity() {
5508
		$this->Form->request['_Token'] = array('key' => 'testKey');
5509
		$this->assertEquals(array(), $this->Form->fields);
5510
 
5511
		$result = $this->Form->select(
5512
			'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
5513
			array('multiple' => 'checkbox')
5514
		);
5515
		$this->assertEquals(array('Model.multi_field'), $this->Form->fields);
5516
 
5517
		$result = $this->Form->secure($this->Form->fields);
5518
		$key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
5519
		$this->assertRegExp('/"' . $key . '"/', $result);
5520
	}
5521
 
5522
/**
5523
 * Multiple select elements should always be secured as they always participate
5524
 * in the POST data.
5525
 *
5526
 * @return void
5527
 */
5528
	public function testSelectMultipleSecureWithNoOptions() {
5529
		$this->Form->request['_Token'] = array('key' => 'testkey');
5530
		$this->assertEquals(array(), $this->Form->fields);
5531
 
5532
		$this->Form->select(
5533
			'Model.select',
5534
			array(),
5535
			array('multiple' => true)
5536
		);
5537
		$this->assertEquals(array('Model.select'), $this->Form->fields);
5538
	}
5539
/**
5540
 * When a select box has no options it should not be added to the fields list
5541
 * as it always fail post validation.
5542
 *
5543
 * @return void
5544
 */
5545
	public function testSelectNoSecureWithNoOptions() {
5546
		$this->Form->request['_Token'] = array('key' => 'testkey');
5547
		$this->assertEquals(array(), $this->Form->fields);
5548
 
5549
		$this->Form->select(
5550
			'Model.select',
5551
			array()
5552
		);
5553
		$this->assertEquals(array(), $this->Form->fields);
5554
 
5555
		$this->Form->select(
5556
			'Model.select',
5557
			array(),
5558
			array('empty' => true)
5559
		);
5560
		$this->assertEquals(array('Model.select'), $this->Form->fields);
5561
	}
5562
 
5563
/**
5564
 * testInputMultipleCheckboxes method
5565
 *
5566
 * test input() resulting in multi select elements being generated.
5567
 *
5568
 * @return void
5569
 */
5570
	public function testInputMultipleCheckboxes() {
5571
		$result = $this->Form->input('Model.multi_field', array(
5572
			'options' => array('first', 'second', 'third'),
5573
			'multiple' => 'checkbox'
5574
		));
5575
		$expected = array(
5576
			array('div' => array('class' => 'input select')),
5577
			array('label' => array('for' => 'ModelMultiField')),
5578
			'Multi Field',
5579
			'/label',
5580
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5581
			array('div' => array('class' => 'checkbox')),
5582
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5583
			array('label' => array('for' => 'ModelMultiField0')),
5584
			'first',
5585
			'/label',
5586
			'/div',
5587
			array('div' => array('class' => 'checkbox')),
5588
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5589
			array('label' => array('for' => 'ModelMultiField1')),
5590
			'second',
5591
			'/label',
5592
			'/div',
5593
			array('div' => array('class' => 'checkbox')),
5594
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
5595
			array('label' => array('for' => 'ModelMultiField2')),
5596
			'third',
5597
			'/label',
5598
			'/div',
5599
			'/div'
5600
		);
5601
		$this->assertTags($result, $expected);
5602
 
5603
		$result = $this->Form->input('Model.multi_field', array(
5604
			'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
5605
			'multiple' => 'checkbox'
5606
		));
5607
		$expected = array(
5608
			array('div' => array('class' => 'input select')),
5609
			array('label' => array('for' => 'ModelMultiField')),
5610
			'Multi Field',
5611
			'/label',
5612
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5613
			array('div' => array('class' => 'checkbox')),
5614
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
5615
			array('label' => array('for' => 'ModelMultiFieldA')),
5616
			'first',
5617
			'/label',
5618
			'/div',
5619
			array('div' => array('class' => 'checkbox')),
5620
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
5621
			array('label' => array('for' => 'ModelMultiFieldB')),
5622
			'second',
5623
			'/label',
5624
			'/div',
5625
			array('div' => array('class' => 'checkbox')),
5626
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
5627
			array('label' => array('for' => 'ModelMultiFieldC')),
5628
			'third',
5629
			'/label',
5630
			'/div',
5631
			'/div'
5632
		);
5633
		$this->assertTags($result, $expected);
5634
 
5635
		$result = $this->Form->input('Model.multi_field', array(
5636
			'options' => array('1' => 'first'),
5637
			'multiple' => 'checkbox',
5638
			'label' => false,
5639
			'div' => false
5640
		));
5641
		$expected = array(
5642
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5643
			array('div' => array('class' => 'checkbox')),
5644
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5645
			array('label' => array('for' => 'ModelMultiField1')),
5646
			'first',
5647
			'/label',
5648
			'/div'
5649
		);
5650
		$this->assertTags($result, $expected);
5651
 
5652
		$result = $this->Form->input('Model.multi_field', array(
5653
			'options' => array('2' => 'second'),
5654
			'multiple' => 'checkbox',
5655
			'label' => false,
5656
			'div' => false
5657
		));
5658
		$expected = array(
5659
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5660
			array('div' => array('class' => 'checkbox')),
5661
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
5662
			array('label' => array('for' => 'ModelMultiField2')),
5663
			'second',
5664
			'/label',
5665
			'/div'
5666
		);
5667
		$this->assertTags($result, $expected);
5668
	}
5669
 
5670
/**
5671
 * testSelectHiddenFieldOmission method
5672
 *
5673
 * test that select() with 'hiddenField' => false omits the hidden field
5674
 *
5675
 * @return void
5676
 */
5677
	public function testSelectHiddenFieldOmission() {
5678
		$result = $this->Form->select('Model.multi_field',
5679
			array('first', 'second'),
5680
			array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
5681
		);
5682
		$expected = array(
5683
			array('div' => array('class' => 'checkbox')),
5684
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5685
			array('label' => array('for' => 'ModelMultiField0')),
5686
			'first',
5687
			'/label',
5688
			'/div',
5689
			array('div' => array('class' => 'checkbox')),
5690
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5691
			array('label' => array('for' => 'ModelMultiField1')),
5692
			'second',
5693
			'/label',
5694
			'/div'
5695
		);
5696
		$this->assertTags($result, $expected);
5697
 
5698
		$result = $this->Form->input('Model.multi_field', array(
5699
			'options' => array('first', 'second'),
5700
			'multiple' => 'checkbox',
5701
			'hiddenField' => false
5702
		));
5703
		$expected = array(
5704
			array('div' => array('class' => 'input select')),
5705
			array('label' => array('for' => 'ModelMultiField')),
5706
			'Multi Field',
5707
			'/label',
5708
			array('div' => array('class' => 'checkbox')),
5709
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5710
			array('label' => array('for' => 'ModelMultiField0')),
5711
			'first',
5712
			'/label',
5713
			'/div',
5714
			array('div' => array('class' => 'checkbox')),
5715
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5716
			array('label' => array('for' => 'ModelMultiField1')),
5717
			'second',
5718
			'/label',
5719
			'/div',
5720
			'/div'
5721
		);
5722
		$this->assertTags($result, $expected);
5723
	}
5724
 
5725
/**
5726
 * test that select() with multiple = checkbox works with overriding name attribute.
5727
 *
5728
 * @return void
5729
 */
5730
	public function testSelectCheckboxMultipleOverrideName() {
5731
		$result = $this->Form->input('category', array(
5732
			'type' => 'select',
5733
			'multiple' => 'checkbox',
5734
			'name' => 'data[fish]',
5735
			'options' => array('1', '2'),
5736
			'div' => false,
5737
			'label' => false,
5738
		));
5739
		$expected = array(
5740
			'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
5741
			array('div' => array('class' => 'checkbox')),
5742
				array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
5743
				array('label' => array('for' => 'Category0')), '1', '/label',
5744
			'/div',
5745
			array('div' => array('class' => 'checkbox')),
5746
				array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
5747
				array('label' => array('for' => 'Category1')), '2', '/label',
5748
			'/div'
5749
		);
5750
		$this->assertTags($result, $expected);
5751
	}
5752
 
5753
/**
5754
 * Test that 'id' overrides all the checkbox id's as well.
5755
 *
5756
 * @return void
5757
 */
5758
	public function testSelectCheckboxMultipleId() {
5759
		$result = $this->Form->select(
5760
			'Model.multi_field',
5761
			array('first', 'second', 'third'),
5762
			array('multiple' => 'checkbox', 'id' => 'CustomId')
5763
		);
5764
 
5765
		$expected = array(
5766
			'input' => array(
5767
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
5768
			),
5769
			array('div' => array('class' => 'checkbox')),
5770
			array('input' => array(
5771
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5772
				'value' => '0', 'id' => 'CustomId0'
5773
			)),
5774
			array('label' => array('for' => 'CustomId0')),
5775
			'first',
5776
			'/label',
5777
			'/div',
5778
			array('div' => array('class' => 'checkbox')),
5779
			array('input' => array(
5780
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5781
				'value' => '1', 'id' => 'CustomId1'
5782
			)),
5783
			array('label' => array('for' => 'CustomId1')),
5784
			'second',
5785
			'/label',
5786
			'/div',
5787
			array('div' => array('class' => 'checkbox')),
5788
			array('input' => array(
5789
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5790
				'value' => '2', 'id' => 'CustomId2'
5791
			)),
5792
			array('label' => array('for' => 'CustomId2')),
5793
			'third',
5794
			'/label',
5795
			'/div'
5796
		);
5797
		$this->assertTags($result, $expected);
5798
	}
5799
 
5800
/**
5801
 * testCheckbox method
5802
 *
5803
 * Test generation of checkboxes
5804
 *
5805
 * @return void
5806
 */
5807
	public function testCheckbox() {
5808
		$result = $this->Form->checkbox('Model.field');
5809
		$expected = array(
5810
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5811
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
5812
		);
5813
		$this->assertTags($result, $expected);
5814
 
5815
		$result = $this->Form->checkbox('Model.field', array(
5816
			'id' => 'theID',
5817
			'value' => 'myvalue',
5818
			'form' => 'my-form',
5819
		));
5820
		$expected = array(
5821
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_', 'form' => 'my-form'),
5822
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form'))
5823
		);
5824
		$this->assertTags($result, $expected);
5825
 
5826
		$Contact = ClassRegistry::getObject('Contact');
5827
		$Contact->validationErrors['field'] = 1;
5828
		$this->Form->request->data['Contact']['field'] = 'myvalue';
5829
		$result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
5830
		$expected = array(
5831
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
5832
			array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
5833
		);
5834
		$this->assertTags($result, $expected);
5835
 
5836
		$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
5837
		$expected = array(
5838
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
5839
			array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
5840
		);
5841
		$this->assertTags($result, $expected);
5842
 
5843
		$this->Form->request->data['Contact']['field'] = '';
5844
		$result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
5845
		$expected = array(
5846
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
5847
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
5848
		);
5849
		$this->assertTags($result, $expected);
5850
 
5851
		$Contact->validationErrors = array();
5852
		$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
5853
		$expected = array(
5854
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
5855
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
5856
		);
5857
		$this->assertTags($result, $expected);
5858
 
5859
		$this->Form->request->data['Contact']['published'] = 1;
5860
		$result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
5861
		$expected = array(
5862
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
5863
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
5864
		);
5865
		$this->assertTags($result, $expected);
5866
 
5867
		$this->Form->request->data['Contact']['published'] = 0;
5868
		$result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
5869
		$expected = array(
5870
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
5871
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
5872
		);
5873
		$this->assertTags($result, $expected);
5874
 
5875
		$result = $this->Form->checkbox('Model.CustomField.1.value');
5876
		$expected = array(
5877
			'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
5878
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
5879
		);
5880
		$this->assertTags($result, $expected);
5881
 
5882
		$result = $this->Form->checkbox('CustomField.1.value');
5883
		$expected = array(
5884
			'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
5885
			array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
5886
		);
5887
		$this->assertTags($result, $expected);
5888
	}
5889
 
5890
/**
5891
 * test checkbox() with a custom name attribute
5892
 *
5893
 * @return void
5894
 */
5895
	public function testCheckboxCustomNameAttribute() {
5896
		$result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
5897
		$expected = array(
5898
				'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
5899
				array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
5900
			);
5901
		$this->assertTags($result, $expected);
5902
	}
5903
 
5904
/**
5905
 * test the checked option for checkboxes.
5906
 *
5907
 * @return void
5908
 */
5909
	public function testCheckboxCheckedOption() {
5910
		$result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
5911
		$expected = array(
5912
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5913
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
5914
		);
5915
		$this->assertTags($result, $expected);
5916
 
5917
		$result = $this->Form->checkbox('Model.field', array('checked' => 1));
5918
		$expected = array(
5919
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5920
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
5921
		);
5922
		$this->assertTags($result, $expected);
5923
 
5924
		$result = $this->Form->checkbox('Model.field', array('checked' => true));
5925
		$expected = array(
5926
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5927
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
5928
		);
5929
		$this->assertTags($result, $expected);
5930
 
5931
		$result = $this->Form->checkbox('Model.field', array('checked' => false));
5932
		$expected = array(
5933
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5934
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
5935
		);
5936
		$this->assertTags($result, $expected);
5937
 
5938
		$this->Form->request->data['Model']['field'] = 1;
5939
		$result = $this->Form->checkbox('Model.field', array('checked' => false));
5940
		$expected = array(
5941
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
5942
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
5943
		);
5944
		$this->assertTags($result, $expected);
5945
	}
5946
 
5947
/**
5948
 * Test that disabled attribute works on both the checkbox and hidden input.
5949
 *
5950
 * @return void
5951
 */
5952
	public function testCheckboxDisabling() {
5953
		$result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
5954
		$expected = array(
5955
			array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
5956
			array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
5957
		);
5958
		$this->assertTags($result, $expected);
5959
 
5960
		$result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
5961
		$expected = array(
5962
			array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
5963
			array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
5964
		);
5965
		$this->assertTags($result, $expected);
5966
	}
5967
 
5968
/**
5969
 * Test that the hidden input for checkboxes can be omitted or set to a
5970
 * specific value.
5971
 *
5972
 * @return void
5973
 */
5974
	public function testCheckboxHiddenField() {
5975
		$result = $this->Form->input('UserForm.something', array(
5976
			'type' => 'checkbox',
5977
			'hiddenField' => false
5978
		));
5979
		$expected = array(
5980
			'div' => array('class' => 'input checkbox'),
5981
			array('input' => array(
5982
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
5983
				'value' => '1', 'id' => 'UserFormSomething'
5984
			)),
5985
			'label' => array('for' => 'UserFormSomething'),
5986
			'Something',
5987
			'/label',
5988
			'/div'
5989
		);
5990
		$this->assertTags($result, $expected);
5991
 
5992
		$result = $this->Form->input('UserForm.something', array(
5993
			'type' => 'checkbox',
5994
			'value' => 'Y',
5995
			'hiddenField' => 'N',
5996
		));
5997
		$expected = array(
5998
			'div' => array('class' => 'input checkbox'),
5999
			array('input' => array(
6000
				'type' => 'hidden', 'name' => 'data[UserForm][something]',
6001
				'value' => 'N', 'id' => 'UserFormSomething_'
6002
			)),
6003
			array('input' => array(
6004
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
6005
				'value' => 'Y', 'id' => 'UserFormSomething'
6006
			)),
6007
			'label' => array('for' => 'UserFormSomething'),
6008
			'Something',
6009
			'/label',
6010
			'/div'
6011
		);
6012
		$this->assertTags($result, $expected);
6013
	}
6014
 
6015
/**
6016
 * Test that a checkbox can have 0 for the value and 1 for the hidden input.
6017
 *
6018
 * @return void
6019
 */
6020
	public function testCheckboxZeroValue() {
6021
		$result = $this->Form->input('User.get_spam', array(
6022
			'type' => 'checkbox',
6023
			'value' => '0',
6024
			'hiddenField' => '1',
6025
		));
6026
		$expected = array(
6027
			'div' => array('class' => 'input checkbox'),
6028
			array('input' => array(
6029
				'type' => 'hidden', 'name' => 'data[User][get_spam]',
6030
				'value' => '1', 'id' => 'UserGetSpam_'
6031
			)),
6032
			array('input' => array(
6033
				'type' => 'checkbox', 'name' => 'data[User][get_spam]',
6034
				'value' => '0', 'id' => 'UserGetSpam'
6035
			)),
6036
			'label' => array('for' => 'UserGetSpam'),
6037
			'Get Spam',
6038
			'/label',
6039
			'/div'
6040
		);
6041
		$this->assertTags($result, $expected);
6042
	}
6043
 
6044
/**
6045
 * testDateTime method
6046
 *
6047
 * Test generation of date/time select elements
6048
 *
6049
 * @return void
6050
 */
6051
	public function testDateTime() {
6052
		extract($this->dateRegex);
6053
 
6054
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
6055
		$now = strtotime('now');
6056
		$expected = array(
6057
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6058
			$daysRegex,
6059
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6060
			date('j', $now),
6061
			'/option',
6062
			'*/select',
6063
			'-',
6064
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6065
			$monthsRegex,
6066
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6067
			date('F', $now),
6068
			'/option',
6069
			'*/select',
6070
			'-',
6071
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6072
			$yearsRegex,
6073
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6074
			date('Y', $now),
6075
			'/option',
6076
			'*/select',
6077
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6078
			$hoursRegex,
6079
			array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
6080
			date('g', $now),
6081
			'/option',
6082
			'*/select',
6083
			':',
6084
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6085
			$minutesRegex,
6086
			array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
6087
			date('i', $now),
6088
			'/option',
6089
			'*/select',
6090
			' ',
6091
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6092
			$meridianRegex,
6093
			array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
6094
			date('a', $now),
6095
			'/option',
6096
			'*/select'
6097
		);
6098
		$this->assertTags($result, $expected);
6099
 
6100
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12');
6101
		$expected = array(
6102
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6103
			$daysRegex,
6104
			array('option' => array('value' => '')),
6105
			'/option',
6106
			'*/select',
6107
			'-',
6108
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6109
			$monthsRegex,
6110
			array('option' => array('value' => '')),
6111
			'/option',
6112
			'*/select',
6113
			'-',
6114
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6115
			$yearsRegex,
6116
			array('option' => array('value' => '')),
6117
			'/option',
6118
			'*/select',
6119
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6120
			$hoursRegex,
6121
			array('option' => array('value' => '')),
6122
			'/option',
6123
			'*/select',
6124
			':',
6125
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6126
			$minutesRegex,
6127
			array('option' => array('value' => '')),
6128
			'/option',
6129
			'*/select',
6130
			' ',
6131
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6132
			$meridianRegex,
6133
			array('option' => array('value' => '')),
6134
			'/option',
6135
			'*/select'
6136
		);
6137
		$this->assertTags($result, $expected);
6138
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6139
 
6140
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
6141
		$this->assertTags($result, $expected);
6142
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6143
 
6144
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
6145
		$this->assertTags($result, $expected);
6146
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6147
 
6148
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
6149
		$expected = array(
6150
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6151
			$daysRegex,
6152
			array('option' => array('value' => '')),
6153
			'/option',
6154
			'*/select',
6155
			'-',
6156
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6157
			$monthsRegex,
6158
			array('option' => array('value' => '')),
6159
			'/option',
6160
			'*/select',
6161
			'-',
6162
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6163
			$yearsRegex,
6164
			array('option' => array('value' => '')),
6165
			'/option',
6166
			'*/select',
6167
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6168
			$hoursRegex,
6169
			array('option' => array('value' => '')),
6170
			'/option',
6171
			'*/select',
6172
			':',
6173
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6174
			$minutesRegex,
6175
			array('option' => array('value' => '')),
6176
			'/option',
6177
			array('option' => array('value' => '00')),
6178
			'00',
6179
			'/option',
6180
			array('option' => array('value' => '05')),
6181
			'05',
6182
			'/option',
6183
			array('option' => array('value' => '10')),
6184
			'10',
6185
			'/option',
6186
			'*/select',
6187
			' ',
6188
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6189
			$meridianRegex,
6190
			array('option' => array('value' => '')),
6191
			'/option',
6192
			'*/select'
6193
		);
6194
		$this->assertTags($result, $expected);
6195
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6196
 
6197
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
6198
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
6199
 
6200
		$this->Form->request->data['Contact']['data'] = null;
6201
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12');
6202
		$expected = array(
6203
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6204
			$daysRegex,
6205
			array('option' => array('value' => '')),
6206
			'/option',
6207
			'*/select',
6208
			'-',
6209
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6210
			$monthsRegex,
6211
			array('option' => array('value' => '')),
6212
			'/option',
6213
			'*/select',
6214
			'-',
6215
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6216
			$yearsRegex,
6217
			array('option' => array('value' => '')),
6218
			'/option',
6219
			'*/select',
6220
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6221
			$hoursRegex,
6222
			array('option' => array('value' => '')),
6223
			'/option',
6224
			'*/select',
6225
			':',
6226
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6227
			$minutesRegex,
6228
			array('option' => array('value' => '')),
6229
			'/option',
6230
			'*/select',
6231
			' ',
6232
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6233
			$meridianRegex,
6234
			array('option' => array('value' => '')),
6235
			'/option',
6236
			'*/select'
6237
		);
6238
		$this->assertTags($result, $expected);
6239
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6240
 
6241
		$this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
6242
		$now = strtotime($this->Form->data['Model']['field']);
6243
		$result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
6244
		$expected = array(
6245
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6246
			$daysRegex,
6247
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6248
			date('j', $now),
6249
			'/option',
6250
			'*/select',
6251
			'-',
6252
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6253
			$monthsRegex,
6254
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6255
			date('F', $now),
6256
			'/option',
6257
			'*/select',
6258
			'-',
6259
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
6260
			$yearsRegex,
6261
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6262
			date('Y', $now),
6263
			'/option',
6264
			'*/select',
6265
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
6266
			$hoursRegex,
6267
			array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
6268
			date('g', $now),
6269
			'/option',
6270
			'*/select',
6271
			':',
6272
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6273
			$minutesRegex,
6274
			array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
6275
			date('i', $now),
6276
			'/option',
6277
			'*/select',
6278
			' ',
6279
			array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
6280
			$meridianRegex,
6281
			array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
6282
			date('a', $now),
6283
			'/option',
6284
			'*/select'
6285
		);
6286
		$this->assertTags($result, $expected);
6287
 
6288
		$selected = strtotime('2008-10-26 12:33:00');
6289
		$result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
6290
		$this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
6291
		$this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
6292
		$this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
6293
		$this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
6294
		$this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
6295
		$this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
6296
 
6297
		$this->Form->create('Contact');
6298
		$result = $this->Form->input('published');
6299
		$now = strtotime('now');
6300
		$expected = array(
6301
			'div' => array('class' => 'input date'),
6302
			'label' => array('for' => 'ContactPublishedMonth'),
6303
			'Published',
6304
			'/label',
6305
			array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
6306
			$monthsRegex,
6307
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6308
			date('F', $now),
6309
			'/option',
6310
			'*/select',
6311
			'-',
6312
			array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
6313
			$daysRegex,
6314
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6315
			date('j', $now),
6316
			'/option',
6317
			'*/select',
6318
			'-',
6319
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
6320
			$yearsRegex,
6321
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6322
			date('Y', $now),
6323
			'/option',
6324
			'*/select',
6325
			'/div'
6326
		);
6327
		$this->assertTags($result, $expected);
6328
 
6329
		$result = $this->Form->input('published2', array('type' => 'date'));
6330
		$now = strtotime('now');
6331
		$expected = array(
6332
			'div' => array('class' => 'input date'),
6333
			'label' => array('for' => 'ContactPublished2Month'),
6334
			'Published2',
6335
			'/label',
6336
			array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
6337
			$monthsRegex,
6338
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6339
			date('F', $now),
6340
			'/option',
6341
			'*/select',
6342
			'-',
6343
			array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
6344
			$daysRegex,
6345
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6346
			date('j', $now),
6347
			'/option',
6348
			'*/select',
6349
			'-',
6350
			array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
6351
			$yearsRegex,
6352
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6353
			date('Y', $now),
6354
			'/option',
6355
			'*/select',
6356
			'/div'
6357
		);
6358
		$this->assertTags($result, $expected);
6359
 
6360
		$this->Form->create('Contact');
6361
		$result = $this->Form->input('published', array('monthNames' => false));
6362
		$now = strtotime('now');
6363
		$expected = array(
6364
			'div' => array('class' => 'input date'),
6365
			'label' => array('for' => 'ContactPublishedMonth'),
6366
			'Published',
6367
			'/label',
6368
			array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
6369
			'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
6370
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6371
			date('m', $now),
6372
			'/option',
6373
			'*/select',
6374
			'-',
6375
			array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
6376
			$daysRegex,
6377
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6378
			date('j', $now),
6379
			'/option',
6380
			'*/select',
6381
			'-',
6382
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
6383
			$yearsRegex,
6384
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6385
			date('Y', $now),
6386
			'/option',
6387
			'*/select',
6388
			'/div'
6389
		);
6390
		$this->assertTags($result, $expected);
6391
 
6392
		$result = $this->Form->input('published', array(
6393
			'timeFormat' => 24,
6394
			'interval' => 5,
6395
			'selected' => strtotime('2009-09-03 13:37:00'),
6396
			'type' => 'datetime'
6397
		));
6398
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6399
		$this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
6400
		$this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
6401
		$this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
6402
		$this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
6403
	}
6404
 
6405
/**
6406
 * Test dateTime with rounding
6407
 *
6408
 * @return void
6409
 */
6410
	public function testDateTimeRounding() {
6411
		$this->Form->request->data['Contact'] = array(
6412
			'date' => array(
6413
				'day' => '13',
6414
				'month' => '12',
6415
				'year' => '2010',
6416
				'hour' => '04',
6417
				'min' => '19',
6418
				'meridian' => 'AM'
6419
			)
6420
		);
6421
 
6422
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
6423
		$this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
6424
 
6425
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
6426
		$this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
6427
 
6428
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
6429
		$this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
6430
	}
6431
 
6432
/**
6433
 * Test that empty values don't trigger errors.
6434
 *
6435
 * @return void
6436
 */
6437
	public function testDateTimeNoErrorsOnEmptyData() {
6438
		$this->Form->request->data['Contact'] = array(
6439
			'date' => array(
6440
				'day' => '',
6441
				'month' => '',
6442
				'year' => '',
6443
				'hour' => '',
6444
				'min' => '',
6445
				'meridian' => ''
6446
			)
6447
		);
6448
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
6449
		$this->assertNotEmpty($result);
6450
	}
6451
 
6452
/**
6453
 * test that datetime() and default values work.
6454
 *
6455
 * @return void
6456
 */
6457
	public function testDatetimeWithDefault() {
6458
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
6459
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6460
		$this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
6461
		$this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
6462
 
6463
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
6464
			'default' => '2009-06-01 11:15:30'
6465
		));
6466
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6467
		$this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
6468
		$this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
6469
	}
6470
 
6471
/**
6472
 * test that bogus non-date time data doesn't cause errors.
6473
 *
6474
 * @return void
6475
 */
6476
	public function testDateTimeWithBogusData() {
6477
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
6478
		$this->assertNotRegExp('/selected="selected">\d/', $result);
6479
	}
6480
 
6481
/**
6482
 * testDateTime all zeros
6483
 *
6484
 * @return void
6485
 */
6486
	public function testDateTimeAllZeros() {
6487
		$result = $this->Form->dateTime('Contact.date',
6488
			'DMY',
6489
			false,
6490
			array(
6491
				'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
6492
				'value' => '0000-00-00'
6493
			)
6494
		);
6495
 
6496
		$this->assertRegExp('/<option value="">-<\/option>/', $result);
6497
		$this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
6498
	}
6499
 
6500
/**
6501
 * testDateTimeEmptyAsArray
6502
 *
6503
 * @return void
6504
 */
6505
	public function testDateTimeEmptyAsArray() {
6506
		$result = $this->Form->dateTime('Contact.date',
6507
			'DMY',
6508
			'12',
6509
			array(
6510
				'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
6511
					'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
6512
				)
6513
			)
6514
		);
6515
 
6516
		$this->assertRegExp('/<option value="">DAY<\/option>/', $result);
6517
		$this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
6518
		$this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
6519
		$this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
6520
		$this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
6521
		$this->assertNotRegExp('/<option value=""><\/option>/', $result);
6522
 
6523
		$result = $this->Form->dateTime('Contact.date',
6524
			'DMY',
6525
			'12',
6526
			array(
6527
				'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
6528
			)
6529
		);
6530
 
6531
		$this->assertRegExp('/<option value="">DAY<\/option>/', $result);
6532
		$this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
6533
		$this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
6534
		$this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
6535
		$this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
6536
		$this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
6537
	}
6538
 
6539
/**
6540
 * testFormDateTimeMulti method
6541
 *
6542
 * test multiple datetime element generation
6543
 *
6544
 * @return void
6545
 */
6546
	public function testFormDateTimeMulti() {
6547
		extract($this->dateRegex);
6548
 
6549
		$result = $this->Form->dateTime('Contact.1.updated');
6550
		$expected = array(
6551
			array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
6552
			$daysRegex,
6553
			array('option' => array('value' => '')),
6554
			'/option',
6555
			'*/select',
6556
			'-',
6557
			array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
6558
			$monthsRegex,
6559
			array('option' => array('value' => '')),
6560
			'/option',
6561
			'*/select',
6562
			'-',
6563
			array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
6564
			$yearsRegex,
6565
			array('option' => array('value' => '')),
6566
			'/option',
6567
			'*/select',
6568
			array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
6569
			$hoursRegex,
6570
			array('option' => array('value' => '')),
6571
			'/option',
6572
			'*/select',
6573
			':',
6574
			array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
6575
			$minutesRegex,
6576
			array('option' => array('value' => '')),
6577
			'/option',
6578
			'*/select',
6579
			' ',
6580
			array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
6581
			$meridianRegex,
6582
			array('option' => array('value' => '')),
6583
			'/option',
6584
			'*/select'
6585
		);
6586
		$this->assertTags($result, $expected);
6587
 
6588
		$result = $this->Form->dateTime('Contact.2.updated');
6589
		$expected = array(
6590
			array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
6591
			$daysRegex,
6592
			array('option' => array('value' => '')),
6593
			'/option',
6594
			'*/select',
6595
			'-',
6596
			array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
6597
			$monthsRegex,
6598
			array('option' => array('value' => '')),
6599
			'/option',
6600
			'*/select',
6601
			'-',
6602
			array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
6603
			$yearsRegex,
6604
			array('option' => array('value' => '')),
6605
			'/option',
6606
			'*/select',
6607
			array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
6608
			$hoursRegex,
6609
			array('option' => array('value' => '')),
6610
			'/option',
6611
			'*/select',
6612
			':',
6613
			array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
6614
			$minutesRegex,
6615
			array('option' => array('value' => '')),
6616
			'/option',
6617
			'*/select',
6618
			' ',
6619
			array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
6620
			$meridianRegex,
6621
			array('option' => array('value' => '')),
6622
			'/option',
6623
			'*/select'
6624
		);
6625
		$this->assertTags($result, $expected);
6626
	}
6627
 
6628
/**
6629
 * When changing the date format, the label should always focus the first select box when
6630
 * clicked.
6631
 *
6632
 * @return void
6633
 */
6634
	public function testDateTimeLabelIdMatchesFirstInput() {
6635
		$result = $this->Form->input('Model.date', array('type' => 'date'));
6636
		$this->assertContains('label for="ModelDateMonth"', $result);
6637
 
6638
		$result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
6639
		$this->assertContains('label for="ModelDateDay"', $result);
6640
 
6641
		$result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
6642
		$this->assertContains('label for="ModelDateYear"', $result);
6643
	}
6644
 
6645
/**
6646
 * testMonth method
6647
 *
6648
 * @return void
6649
 */
6650
	public function testMonth() {
6651
		$result = $this->Form->month('Model.field');
6652
		$expected = array(
6653
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6654
			array('option' => array('value' => '')),
6655
			'/option',
6656
			array('option' => array('value' => '01')),
6657
			date('F', strtotime('2008-01-01 00:00:00')),
6658
			'/option',
6659
			array('option' => array('value' => '02')),
6660
			date('F', strtotime('2008-02-01 00:00:00')),
6661
			'/option',
6662
			'*/select',
6663
		);
6664
		$this->assertTags($result, $expected);
6665
 
6666
		$result = $this->Form->month('Model.field', array('empty' => true));
6667
		$expected = array(
6668
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6669
			array('option' => array('value' => '')),
6670
			'/option',
6671
			array('option' => array('value' => '01')),
6672
			date('F', strtotime('2008-01-01 00:00:00')),
6673
			'/option',
6674
			array('option' => array('value' => '02')),
6675
			date('F', strtotime('2008-02-01 00:00:00')),
6676
			'/option',
6677
			'*/select',
6678
		);
6679
		$this->assertTags($result, $expected);
6680
 
6681
		$result = $this->Form->month('Model.field', array('monthNames' => false));
6682
		$expected = array(
6683
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6684
			array('option' => array('value' => '')),
6685
			'/option',
6686
			array('option' => array('value' => '01')),
6687
			'01',
6688
			'/option',
6689
			array('option' => array('value' => '02')),
6690
			'02',
6691
			'/option',
6692
			'*/select',
6693
		);
6694
		$this->assertTags($result, $expected);
6695
 
6696
		$monthNames = array(
6697
			'01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
6698
			'07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
6699
		$result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
6700
		$expected = array(
6701
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6702
			array('option' => array('value' => '')),
6703
			'/option',
6704
			array('option' => array('value' => '01')),
6705
			'Jan',
6706
			'/option',
6707
			array('option' => array('value' => '02')),
6708
			'Feb',
6709
			'/option',
6710
			'*/select',
6711
		);
6712
		$this->assertTags($result, $expected);
6713
 
6714
		$this->Form->request->data['Project']['release'] = '2050-02-10';
6715
		$result = $this->Form->month('Project.release');
6716
 
6717
		$expected = array(
6718
			array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
6719
			array('option' => array('value' => '')),
6720
			'/option',
6721
			array('option' => array('value' => '01')),
6722
			'January',
6723
			'/option',
6724
			array('option' => array('value' => '02', 'selected' => 'selected')),
6725
			'February',
6726
			'/option',
6727
			'*/select',
6728
		);
6729
		$this->assertTags($result, $expected);
6730
 
6731
		$this->Form->request->data['Model']['field'] = '12a';
6732
		$result = $this->Form->month('Model.field');
6733
		$expected = array(
6734
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6735
			array('option' => array('value' => '')),
6736
			'/option',
6737
			array('option' => array('value' => '01')),
6738
			date('F', strtotime('2008-01-01 00:00:00')),
6739
			'/option',
6740
			array('option' => array('value' => '02')),
6741
			date('F', strtotime('2008-02-01 00:00:00')),
6742
			'/option',
6743
			'*/select',
6744
		);
6745
		$this->assertTags($result, $expected);
6746
	}
6747
 
6748
/**
6749
 * testDay method
6750
 *
6751
 * @return void
6752
 */
6753
	public function testDay() {
6754
		extract($this->dateRegex);
6755
 
6756
		$result = $this->Form->day('Model.field', array('value' => false));
6757
		$expected = array(
6758
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6759
			array('option' => array('value' => '')),
6760
			'/option',
6761
			array('option' => array('value' => '01')),
6762
			'1',
6763
			'/option',
6764
			array('option' => array('value' => '02')),
6765
			'2',
6766
			'/option',
6767
			$daysRegex,
6768
			'/select',
6769
		);
6770
		$this->assertTags($result, $expected);
6771
 
6772
		$this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
6773
		$result = $this->Form->day('Model.field');
6774
		$expected = array(
6775
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6776
			array('option' => array('value' => '')),
6777
			'/option',
6778
			array('option' => array('value' => '01')),
6779
			'1',
6780
			'/option',
6781
			array('option' => array('value' => '02')),
6782
			'2',
6783
			'/option',
6784
			$daysRegex,
6785
			array('option' => array('value' => '10', 'selected' => 'selected')),
6786
			'10',
6787
			'/option',
6788
			$daysRegex,
6789
			'/select',
6790
		);
6791
		$this->assertTags($result, $expected);
6792
 
6793
		$this->Form->request->data['Model']['field'] = '';
6794
		$result = $this->Form->day('Model.field', array('value' => '10'));
6795
		$expected = array(
6796
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6797
			array('option' => array('value' => '')),
6798
			'/option',
6799
			array('option' => array('value' => '01')),
6800
			'1',
6801
			'/option',
6802
			array('option' => array('value' => '02')),
6803
			'2',
6804
			'/option',
6805
			$daysRegex,
6806
			array('option' => array('value' => '10', 'selected' => 'selected')),
6807
			'10',
6808
			'/option',
6809
			$daysRegex,
6810
			'/select',
6811
		);
6812
		$this->assertTags($result, $expected);
6813
 
6814
		$this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
6815
		$result = $this->Form->day('Model.field', array('value' => true));
6816
		$expected = array(
6817
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6818
			array('option' => array('value' => '')),
6819
			'/option',
6820
			array('option' => array('value' => '01')),
6821
			'1',
6822
			'/option',
6823
			array('option' => array('value' => '02')),
6824
			'2',
6825
			'/option',
6826
			$daysRegex,
6827
			array('option' => array('value' => '10', 'selected' => 'selected')),
6828
			'10',
6829
			'/option',
6830
			$daysRegex,
6831
			'/select',
6832
		);
6833
		$this->assertTags($result, $expected);
6834
 
6835
		$this->Form->request->data['Project']['release'] = '2050-10-10';
6836
		$result = $this->Form->day('Project.release');
6837
 
6838
		$expected = array(
6839
			array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
6840
			array('option' => array('value' => '')),
6841
			'/option',
6842
			array('option' => array('value' => '01')),
6843
			'1',
6844
			'/option',
6845
			array('option' => array('value' => '02')),
6846
			'2',
6847
			'/option',
6848
			$daysRegex,
6849
			array('option' => array('value' => '10', 'selected' => 'selected')),
6850
			'10',
6851
			'/option',
6852
			$daysRegex,
6853
			'/select',
6854
		);
6855
		$this->assertTags($result, $expected);
6856
 
6857
		$this->Form->request->data['Model']['field'] = '12e';
6858
		$result = $this->Form->day('Model.field');
6859
		$expected = array(
6860
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6861
			array('option' => array('value' => '')),
6862
			'/option',
6863
			array('option' => array('value' => '01')),
6864
			'1',
6865
			'/option',
6866
			array('option' => array('value' => '02')),
6867
			'2',
6868
			'/option',
6869
			$daysRegex,
6870
			'/select',
6871
		);
6872
		$this->assertTags($result, $expected);
6873
	}
6874
 
6875
/**
6876
 * testMinute method
6877
 *
6878
 * @return void
6879
 */
6880
	public function testMinute() {
6881
		extract($this->dateRegex);
6882
 
6883
		$result = $this->Form->minute('Model.field');
6884
		$expected = array(
6885
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6886
			array('option' => array('value' => '')),
6887
			'/option',
6888
			array('option' => array('value' => '00')),
6889
			'00',
6890
			'/option',
6891
			array('option' => array('value' => '01')),
6892
			'01',
6893
			'/option',
6894
			array('option' => array('value' => '02')),
6895
			'02',
6896
			'/option',
6897
			$minutesRegex,
6898
			'/select',
6899
		);
6900
		$this->assertTags($result, $expected);
6901
 
6902
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
6903
		$result = $this->Form->minute('Model.field');
6904
		$expected = array(
6905
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6906
			array('option' => array('value' => '')),
6907
			'/option',
6908
			array('option' => array('value' => '00')),
6909
			'00',
6910
			'/option',
6911
			array('option' => array('value' => '01')),
6912
			'01',
6913
			'/option',
6914
			array('option' => array('value' => '02')),
6915
			'02',
6916
			'/option',
6917
			$minutesRegex,
6918
			array('option' => array('value' => '12', 'selected' => 'selected')),
6919
			'12',
6920
			'/option',
6921
			$minutesRegex,
6922
			'/select',
6923
		);
6924
		$this->assertTags($result, $expected);
6925
 
6926
		$this->Form->request->data['Model']['field'] = '';
6927
		$result = $this->Form->minute('Model.field', array('interval' => 5));
6928
		$expected = array(
6929
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6930
			array('option' => array('value' => '')),
6931
			'/option',
6932
			array('option' => array('value' => '00')),
6933
			'00',
6934
			'/option',
6935
			array('option' => array('value' => '05')),
6936
			'05',
6937
			'/option',
6938
			array('option' => array('value' => '10')),
6939
			'10',
6940
			'/option',
6941
			$minutesRegex,
6942
			'/select',
6943
		);
6944
		$this->assertTags($result, $expected);
6945
 
6946
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
6947
		$result = $this->Form->minute('Model.field', array('interval' => 5));
6948
		$expected = array(
6949
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6950
			array('option' => array('value' => '')),
6951
			'/option',
6952
			array('option' => array('value' => '00')),
6953
			'00',
6954
			'/option',
6955
			array('option' => array('value' => '05')),
6956
			'05',
6957
			'/option',
6958
			array('option' => array('value' => '10', 'selected' => 'selected')),
6959
			'10',
6960
			'/option',
6961
			$minutesRegex,
6962
			'/select',
6963
		);
6964
		$this->assertTags($result, $expected);
6965
 
6966
		$result = $this->Form->minute('Model.field', array('value' => '#invalid#'));
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' => '01')),
6975
			'01',
6976
			'/option',
6977
			array('option' => array('value' => '02')),
6978
			'02',
6979
			'/option',
6980
			$minutesRegex,
6981
			'/select',
6982
		);
6983
		$this->assertTags($result, $expected);
6984
	}
6985
 
6986
/**
6987
 * testHour method
6988
 *
6989
 * @return void
6990
 */
6991
	public function testHour() {
6992
		extract($this->dateRegex);
6993
 
6994
		$result = $this->Form->hour('Model.field', false);
6995
		$expected = array(
6996
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
6997
			array('option' => array('value' => '')),
6998
			'/option',
6999
			array('option' => array('value' => '01')),
7000
			'1',
7001
			'/option',
7002
			array('option' => array('value' => '02')),
7003
			'2',
7004
			'/option',
7005
			$hoursRegex,
7006
			'/select',
7007
		);
7008
		$this->assertTags($result, $expected);
7009
 
7010
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
7011
		$result = $this->Form->hour('Model.field', false);
7012
		$expected = array(
7013
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7014
			array('option' => array('value' => '')),
7015
			'/option',
7016
			array('option' => array('value' => '01')),
7017
			'1',
7018
			'/option',
7019
			array('option' => array('value' => '02')),
7020
			'2',
7021
			'/option',
7022
			$hoursRegex,
7023
			array('option' => array('value' => '12', 'selected' => 'selected')),
7024
			'12',
7025
			'/option',
7026
			'/select',
7027
		);
7028
		$this->assertTags($result, $expected);
7029
 
7030
		$this->Form->request->data['Model']['field'] = '';
7031
		$result = $this->Form->hour('Model.field', true, array('value' => '23'));
7032
		$this->assertContains('<option value="23" selected="selected">23</option>', $result);
7033
 
7034
		$result = $this->Form->hour('Model.field', false, array('value' => '23'));
7035
		$this->assertContains('<option value="11" selected="selected">11</option>', $result);
7036
 
7037
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
7038
		$result = $this->Form->hour('Model.field', true);
7039
		$expected = array(
7040
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7041
			array('option' => array('value' => '')),
7042
			'/option',
7043
			array('option' => array('value' => '00', 'selected' => 'selected')),
7044
			'0',
7045
			'/option',
7046
			array('option' => array('value' => '01')),
7047
			'1',
7048
			'/option',
7049
			array('option' => array('value' => '02')),
7050
			'2',
7051
			'/option',
7052
			$hoursRegex,
7053
			'/select',
7054
		);
7055
		$this->assertTags($result, $expected);
7056
 
7057
		unset($this->Form->request->data['Model']['field']);
7058
		$result = $this->Form->hour('Model.field', true, array('value' => 'now'));
7059
		$thisHour = date('H');
7060
		$optValue = date('G');
7061
		$this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
7062
 
7063
		$this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
7064
		$result = $this->Form->hour('Model.field', true);
7065
		$expected = array(
7066
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7067
			array('option' => array('value' => '')),
7068
			'/option',
7069
			array('option' => array('value' => '00')),
7070
			'0',
7071
			'/option',
7072
			array('option' => array('value' => '01', 'selected' => 'selected')),
7073
			'1',
7074
			'/option',
7075
			array('option' => array('value' => '02')),
7076
			'2',
7077
			'/option',
7078
			$hoursRegex,
7079
			'/select',
7080
		);
7081
		$this->assertTags($result, $expected);
7082
 
7083
		$this->Form->request->data['Model']['field'] = '18a';
7084
		$result = $this->Form->hour('Model.field', false);
7085
		$expected = array(
7086
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7087
			array('option' => array('value' => '')),
7088
			'/option',
7089
			array('option' => array('value' => '01')),
7090
			'1',
7091
			'/option',
7092
			array('option' => array('value' => '02')),
7093
			'2',
7094
			'/option',
7095
			$hoursRegex,
7096
			'/select',
7097
		);
7098
		$this->assertTags($result, $expected);
7099
	}
7100
 
7101
/**
7102
 * testYear method
7103
 *
7104
 * @return void
7105
 */
7106
	public function testYear() {
7107
		$result = $this->Form->year('Model.field', 2006, 2007);
7108
		$expected = array(
7109
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
7110
			array('option' => array('value' => '')),
7111
			'/option',
7112
			array('option' => array('value' => '2007')),
7113
			'2007',
7114
			'/option',
7115
			array('option' => array('value' => '2006')),
7116
			'2006',
7117
			'/option',
7118
			'/select',
7119
		);
7120
		$this->assertTags($result, $expected);
7121
 
7122
		$result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
7123
		$expected = array(
7124
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
7125
			array('option' => array('value' => '')),
7126
			'/option',
7127
			array('option' => array('value' => '2006')),
7128
			'2006',
7129
			'/option',
7130
			array('option' => array('value' => '2007')),
7131
			'2007',
7132
			'/option',
7133
			'/select',
7134
		);
7135
		$this->assertTags($result, $expected);
7136
 
7137
		$this->request->data['Contact']['published'] = '';
7138
		$result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
7139
		$expected = array(
7140
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
7141
			array('option' => array('value' => '')),
7142
			'/option',
7143
			array('option' => array('value' => '2007')),
7144
			'2007',
7145
			'/option',
7146
			array('option' => array('value' => '2006')),
7147
			'2006',
7148
			'/option',
7149
			'/select',
7150
		);
7151
		$this->assertTags($result, $expected);
7152
 
7153
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7154
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
7155
		$expected = array(
7156
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7157
			array('option' => array('value' => '2007')),
7158
			'2007',
7159
			'/option',
7160
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7161
			'2006',
7162
			'/option',
7163
			'/select',
7164
		);
7165
		$this->assertTags($result, $expected);
7166
 
7167
		$this->Form->request->data['Contact']['published'] = '';
7168
		$result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
7169
		$expected = array(
7170
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7171
			array('option' => array('value' => '')),
7172
			'/option',
7173
			array('option' => array('value' => '2007')),
7174
			'2007',
7175
			'/option',
7176
			array('option' => array('value' => '2006')),
7177
			'2006',
7178
			'/option',
7179
			'/select',
7180
		);
7181
		$this->assertTags($result, $expected);
7182
 
7183
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7184
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
7185
		$expected = array(
7186
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7187
			array('option' => array('value' => '2007')),
7188
			'2007',
7189
			'/option',
7190
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7191
			'2006',
7192
			'/option',
7193
			'/select',
7194
		);
7195
		$this->assertTags($result, $expected);
7196
 
7197
		$this->Form->request->data['Contact']['published'] = '';
7198
		$result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
7199
		$expected = array(
7200
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7201
			array('option' => array('value' => '')),
7202
			'/option',
7203
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7204
			'2007',
7205
			'/option',
7206
			array('option' => array('value' => '2006')),
7207
			'2006',
7208
			'/option',
7209
			'/select',
7210
		);
7211
		$this->assertTags($result, $expected);
7212
 
7213
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7214
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
7215
		$expected = array(
7216
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7217
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7218
			'2007',
7219
			'/option',
7220
			array('option' => array('value' => '2006')),
7221
			'2006',
7222
			'/option',
7223
			'/select',
7224
		);
7225
		$this->assertTags($result, $expected);
7226
 
7227
		$this->Form->request->data['Contact']['published'] = '';
7228
		$result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
7229
		$expected = array(
7230
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7231
			array('option' => array('value' => '2008')),
7232
			'2008',
7233
			'/option',
7234
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7235
			'2007',
7236
			'/option',
7237
			array('option' => array('value' => '2006')),
7238
			'2006',
7239
			'/option',
7240
			'/select',
7241
		);
7242
		$this->assertTags($result, $expected);
7243
 
7244
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7245
		$result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
7246
		$expected = array(
7247
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7248
			array('option' => array('value' => '2008')),
7249
			'2008',
7250
			'/option',
7251
			array('option' => array('value' => '2007')),
7252
			'2007',
7253
			'/option',
7254
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7255
			'2006',
7256
			'/option',
7257
			'/select',
7258
		);
7259
		$this->assertTags($result, $expected);
7260
 
7261
		$this->Form->request->data = array();
7262
		$this->Form->create('Contact');
7263
		$result = $this->Form->year('published', 2006, 2008, array('empty' => false));
7264
		$expected = array(
7265
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7266
			array('option' => array('value' => '2008')),
7267
			'2008',
7268
			'/option',
7269
			array('option' => array('value' => '2007')),
7270
			'2007',
7271
			'/option',
7272
			array('option' => array('value' => '2006')),
7273
			'2006',
7274
			'/option',
7275
			'/select',
7276
		);
7277
		$this->assertTags($result, $expected);
7278
 
7279
		$result = $this->Form->year('published', array(), array(), array('empty' => false));
7280
		$this->assertContains('data[Contact][published][year]', $result);
7281
 
7282
		$this->Form->request->data['Contact']['published'] = '2014ee';
7283
		$result = $this->Form->year('Contact.published', 2010, 2011);
7284
		$expected = array(
7285
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7286
			array('option' => array('value' => '')),
7287
			'/option',
7288
			array('option' => array('value' => '2011')),
7289
			'2011',
7290
			'/option',
7291
			array('option' => array('value' => '2010')),
7292
			'2010',
7293
			'/option',
7294
			'/select',
7295
		);
7296
		$this->assertTags($result, $expected);
7297
	}
7298
 
7299
/**
7300
 * testYearAutoExpandRange method
7301
 *
7302
 * @return void
7303
 */
7304
	public function testYearAutoExpandRange() {
7305
		$this->Form->request->data['User']['birthday'] = '1930-10-10';
7306
		$result = $this->Form->year('User.birthday');
7307
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7308
 
7309
		$result = $matches[1];
7310
		$expected = range(date('Y') + 20, 1930);
7311
		$this->assertEquals($expected, $result);
7312
 
7313
		$this->Form->request->data['Project']['release'] = '2050-10-10';
7314
		$result = $this->Form->year('Project.release');
7315
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7316
 
7317
		$result = $matches[1];
7318
		$expected = range(2050, date('Y') - 20);
7319
		$this->assertEquals($expected, $result);
7320
 
7321
		$this->Form->request->data['Project']['release'] = '1881-10-10';
7322
		$result = $this->Form->year('Project.release', 1890, 1900);
7323
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7324
 
7325
		$result = $matches[1];
7326
		$expected = range(1900, 1881);
7327
		$this->assertEquals($expected, $result);
7328
	}
7329
 
7330
/**
7331
 * testInputDate method
7332
 *
7333
 * Test various inputs with type date and different dateFormat values.
7334
 * Failing to provide a dateFormat key should not error.
7335
 * It should simply not pre-select any value then.
7336
 *
7337
 * @return void
7338
 */
7339
	public function testInputDate() {
7340
		$this->Form->request->data = array(
7341
			'User' => array(
7342
				'month_year' => array('month' => date('m')),
7343
				'just_year' => array('month' => date('m')),
7344
				'just_month' => array('year' => date('Y')),
7345
				'just_day' => array('month' => date('m')),
7346
			)
7347
		);
7348
		$this->Form->create('User');
7349
		$result = $this->Form->input('month_year',
7350
				array(
7351
					'label' => false,
7352
					'div' => false,
7353
					'type' => 'date',
7354
					'dateFormat' => 'MY',
7355
					'minYear' => 2006,
7356
					'maxYear' => 2008
7357
				)
7358
		);
7359
		$this->assertContains('value="' . date('m') . '" selected="selected"', $result);
7360
		$this->assertNotContains('value="2008" selected="selected"', $result);
7361
 
7362
		$result = $this->Form->input('just_year',
7363
			array(
7364
				'type' => 'date',
7365
				'label' => false,
7366
				'dateFormat' => 'Y',
7367
				'minYear' => date('Y'),
7368
				'maxYear' => date('Y', strtotime('+20 years'))
7369
			)
7370
		);
7371
		$this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
7372
 
7373
		$result = $this->Form->input('just_month',
7374
			array(
7375
				'type' => 'date',
7376
				'label' => false,
7377
				'dateFormat' => 'M',
7378
				'empty' => false,
7379
			)
7380
		);
7381
		$this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
7382
 
7383
		$result = $this->Form->input('just_day',
7384
			array(
7385
				'type' => 'date',
7386
				'label' => false,
7387
				'dateFormat' => 'D',
7388
				'empty' => false,
7389
			)
7390
		);
7391
		$this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
7392
	}
7393
 
7394
/**
7395
 * testInputDateMaxYear method
7396
 *
7397
 * Let's say we want to only allow users born from 2006 to 2008 to register
7398
 * This being the first singup page, we still don't have any data
7399
 *
7400
 * @return void
7401
 */
7402
	public function testInputDateMaxYear() {
7403
		$this->Form->request->data = array();
7404
		$this->Form->create('User');
7405
		$result = $this->Form->input('birthday',
7406
				array(
7407
					'label' => false,
7408
					'div' => false,
7409
					'type' => 'date',
7410
					'dateFormat' => 'DMY',
7411
					'minYear' => 2006,
7412
					'maxYear' => 2008
7413
				)
7414
		);
7415
		$this->assertContains('value="2008" selected="selected"', $result);
7416
	}
7417
 
7418
/**
7419
 * testTextArea method
7420
 *
7421
 * @return void
7422
 */
7423
	public function testTextArea() {
7424
		$this->Form->request->data = array('Model' => array('field' => 'some test data'));
7425
		$result = $this->Form->textarea('Model.field');
7426
		$expected = array(
7427
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7428
			'some test data',
7429
			'/textarea',
7430
		);
7431
		$this->assertTags($result, $expected);
7432
 
7433
		$result = $this->Form->textarea('Model.tmp');
7434
		$expected = array(
7435
			'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
7436
			'/textarea',
7437
		);
7438
		$this->assertTags($result, $expected);
7439
 
7440
		$this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
7441
		$result = $this->Form->textarea('Model.field');
7442
		$expected = array(
7443
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7444
			htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
7445
			'/textarea',
7446
		);
7447
		$this->assertTags($result, $expected);
7448
 
7449
		$this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
7450
		$result = $this->Form->textarea('Model.field', array('escape' => false));
7451
		$expected = array(
7452
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7453
			'some <strong>test</strong> data with <a href="#">HTML</a> chars',
7454
			'/textarea',
7455
		);
7456
		$this->assertTags($result, $expected);
7457
 
7458
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
7459
		$result = $this->Form->textarea('Model.0.OtherModel.field');
7460
		$expected = array(
7461
			'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
7462
			'/textarea'
7463
		);
7464
		$this->assertTags($result, $expected);
7465
	}
7466
 
7467
/**
7468
 * testTextAreaWithStupidCharacters method
7469
 *
7470
 * test text area with non-ascii characters
7471
 *
7472
 * @return void
7473
 */
7474
	public function testTextAreaWithStupidCharacters() {
7475
		$this->loadFixtures('Post');
7476
		$result = $this->Form->input('Post.content', array(
7477
			'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
7478
		));
7479
		$expected = array(
7480
			'div' => array('class' => 'input textarea'),
7481
				'label' => array('for' => 'PostContent'),
7482
					'Current Text',
7483
				'/label',
7484
				'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
7485
				'GREAT®',
7486
				'/textarea',
7487
			'/div'
7488
		);
7489
		$this->assertTags($result, $expected);
7490
	}
7491
 
7492
/**
7493
 * testHiddenField method
7494
 *
7495
 * @return void
7496
 */
7497
	public function testHiddenField() {
7498
		$Contact = ClassRegistry::getObject('Contact');
7499
		$Contact->validationErrors['field'] = 1;
7500
		$this->Form->request->data['Contact']['field'] = 'test';
7501
		$result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
7502
		$this->assertTags($result, array(
7503
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
7504
		);
7505
	}
7506
 
7507
/**
7508
 * testFileUploadField method
7509
 *
7510
 * @return void
7511
 */
7512
	public function testFileUploadField() {
7513
		$result = $this->Form->file('Model.upload');
7514
		$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
7515
 
7516
		$this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
7517
		$result = $this->Form->input('Model.upload', array('type' => 'file'));
7518
		$expected = array(
7519
			'div' => array('class' => 'input file'),
7520
			'label' => array('for' => 'ModelUpload'),
7521
			'Upload',
7522
			'/label',
7523
			'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
7524
			'/div'
7525
		);
7526
		$this->assertTags($result, $expected);
7527
 
7528
		$this->Form->request->data['Model']['upload'] = 'no data should be set in value';
7529
		$result = $this->Form->file('Model.upload');
7530
		$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
7531
	}
7532
 
7533
/**
7534
 * test File upload input on a model not used in create();
7535
 *
7536
 * @return void
7537
 */
7538
	public function testFileUploadOnOtherModel() {
7539
		$this->Form->create('ValidateUser', array('type' => 'file'));
7540
		$result = $this->Form->file('ValidateProfile.city');
7541
		$expected = array(
7542
			'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
7543
		);
7544
		$this->assertTags($result, $expected);
7545
	}
7546
 
7547
/**
7548
 * testButton method
7549
 *
7550
 * @return void
7551
 */
7552
	public function testButton() {
7553
		$result = $this->Form->button('Hi');
7554
		$this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
7555
 
7556
		$result = $this->Form->button('Clear Form >', array('type' => 'reset'));
7557
		$this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
7558
 
7559
		$result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
7560
		$this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
7561
 
7562
		$result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
7563
		$this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
7564
 
7565
		$result = $this->Form->button('No type', array('type' => false));
7566
		$this->assertTags($result, array('button' => array(), 'No type', '/button'));
7567
 
7568
		$result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
7569
		$this->assertNotRegExp('/\&039/', $result);
7570
	}
7571
 
7572
/**
7573
 * Test that button() makes unlocked fields by default.
7574
 *
7575
 * @return void
7576
 */
7577
	public function testButtonUnlockedByDefault() {
7578
		$this->Form->request->params['_Token']['key'] = 'secured';
7579
		$this->Form->button('Save', array('name' => 'save'));
7580
		$this->Form->button('Clear');
7581
 
7582
		$result = $this->Form->unlockField();
7583
		$this->assertEquals(array('save'), $result);
7584
	}
7585
 
7586
/**
7587
 * testPostButton method
7588
 *
7589
 * @return void
7590
 */
7591
	public function testPostButton() {
7592
		$result = $this->Form->postButton('Hi', '/controller/action');
7593
		$this->assertTags($result, array(
7594
			'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
7595
			'div' => array('style' => 'display:none;'),
7596
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7597
			'/div',
7598
			'button' => array('type' => 'submit'),
7599
			'Hi',
7600
			'/button',
7601
			'/form'
7602
		));
7603
 
7604
		$result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
7605
		$this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
7606
	}
7607
 
7608
/**
7609
 * Test using postButton with N dimensional data.
7610
 *
7611
 * @return void
7612
 */
7613
	public function testPostButtonNestedData() {
7614
		$data = array(
7615
			'one' => array(
7616
				'two' => array(
7617
					3, 4, 5
7618
				)
7619
			)
7620
		);
7621
		$result = $this->Form->postButton('Send', '/', array('data' => $data));
7622
		$this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
7623
		$this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
7624
		$this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
7625
	}
7626
 
7627
/**
7628
 * Test that postButton adds _Token fields.
7629
 *
7630
 * @return void
7631
 */
7632
	public function testSecurePostButton() {
7633
		$this->Form->request->params['_Token'] = array('key' => 'testkey');
7634
 
7635
		$result = $this->Form->postButton('Delete', '/posts/delete/1');
7636
		$expected = array(
7637
			'form' => array(
7638
				'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
7639
			),
7640
			array('div' => array('style' => 'display:none;')),
7641
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7642
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
7643
			'/div',
7644
			'button' => array('type' => 'submit'),
7645
			'Delete',
7646
			'/button',
7647
			array('div' => array('style' => 'display:none;')),
7648
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
7649
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
7650
			'/div',
7651
			'/form',
7652
		);
7653
		$this->assertTags($result, $expected);
7654
	}
7655
 
7656
/**
7657
 * testPostLink method
7658
 *
7659
 * @return void
7660
 */
7661
	public function testPostLink() {
7662
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7663
		$this->assertTags($result, array(
7664
			'form' => array(
7665
				'method' => 'post', 'action' => '/posts/delete/1',
7666
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7667
			),
7668
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7669
			'/form',
7670
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7671
			'Delete',
7672
			'/a'
7673
		));
7674
 
7675
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
7676
		$this->assertTags($result, array(
7677
			'form' => array(
7678
				'method' => 'post', 'action' => '/posts/delete/1',
7679
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7680
			),
7681
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
7682
			'/form',
7683
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7684
			'Delete',
7685
			'/a'
7686
		));
7687
 
7688
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
7689
		$this->assertTags($result, array(
7690
			'form' => array(
7691
				'method' => 'post', 'action' => '/posts/delete/1',
7692
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7693
			),
7694
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7695
			'/form',
7696
			'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
7697
			'Delete',
7698
			'/a'
7699
		));
7700
 
7701
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
7702
		$this->assertTags($result, array(
7703
			'form' => array(
7704
				'method' => 'post', 'action' => '/posts/delete/1',
7705
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7706
			),
7707
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7708
			'/form',
7709
			'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;/'),
7710
			'Delete',
7711
			'/a'
7712
		));
7713
 
7714
		$result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
7715
		$this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
7716
 
7717
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
7718
		$this->assertTags($result, array(
7719
			'form' => array(
7720
				'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
7721
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7722
			),
7723
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7724
			'/form',
7725
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7726
			'Delete',
7727
			'/a'
7728
		));
7729
 
7730
		$result = $this->Form->postLink(
7731
			'',
7732
			array('controller' => 'items', 'action' => 'delete', 10),
7733
			array('class' => 'btn btn-danger', 'escape' => false),
7734
			'Confirm thing'
7735
		);
7736
		$this->assertTags($result, array(
7737
			'form' => array(
7738
				'method' => 'post', 'action' => '/items/delete/10',
7739
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7740
			),
7741
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7742
			'/form',
7743
			'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
7744
			'/a'
7745
		));
7746
	}
7747
 
7748
/**
7749
 * Test that security hashes for postLink include the url.
7750
 *
7751
 * @return void
7752
 */
7753
	public function testPostLinkSecurityHash() {
7754
		$hash = Security::hash(
7755
			'/posts/delete/1' .
7756
			serialize(array()) .
7757
			'' .
7758
			Configure::read('Security.salt')
7759
		);
7760
		$hash .= '%3A';
7761
		$this->Form->request->params['_Token']['key'] = 'test';
7762
 
7763
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7764
		$this->assertTags($result, array(
7765
			'form' => array(
7766
				'method' => 'post', 'action' => '/posts/delete/1',
7767
				'name', 'id', 'style' => 'display:none;'
7768
			),
7769
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7770
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'test', 'id')),
7771
			'div' => array('style' => 'display:none;'),
7772
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => $hash, 'id')),
7773
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id')),
7774
			'/div',
7775
			'/form',
7776
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7777
			'Delete',
7778
			'/a'
7779
		));
7780
	}
7781
 
7782
/**
7783
 * Test using postLink with N dimensional data.
7784
 *
7785
 * @return void
7786
 */
7787
	public function testPostLinkNestedData() {
7788
		$data = array(
7789
			'one' => array(
7790
				'two' => array(
7791
					3, 4, 5
7792
				)
7793
			)
7794
		);
7795
		$result = $this->Form->postLink('Send', '/', array('data' => $data));
7796
		$this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
7797
		$this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
7798
		$this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
7799
	}
7800
 
7801
/**
7802
 * test creating postLinks after a GET form.
7803
 *
7804
 * @return void
7805
 */
7806
	public function testPostLinkAfterGetForm() {
7807
		$this->Form->request->params['_Token']['key'] = 'testkey';
7808
		$this->Form->create('User', array('type' => 'get'));
7809
		$this->Form->end();
7810
 
7811
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7812
		$this->assertTags($result, array(
7813
			'form' => array(
7814
				'method' => 'post', 'action' => '/posts/delete/1',
7815
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7816
			),
7817
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7818
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
7819
			'div' => array('style' => 'display:none;'),
7820
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
7821
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
7822
			'/div',
7823
			'/form',
7824
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7825
			'Delete',
7826
			'/a'
7827
		));
7828
	}
7829
 
7830
/**
7831
 * Test that postLink adds _Token fields.
7832
 *
7833
 * @return void
7834
 */
7835
	public function testSecurePostLink() {
7836
		$this->Form->request->params['_Token'] = array('key' => 'testkey');
7837
 
7838
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7839
		$expected = array(
7840
			'form' => array(
7841
				'method' => 'post', 'action' => '/posts/delete/1',
7842
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7843
			),
7844
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7845
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
7846
			'div' => array('style' => 'display:none;'),
7847
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
7848
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
7849
			'/div',
7850
			'/form',
7851
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7852
			'Delete',
7853
			'/a'
7854
		);
7855
		$this->assertTags($result, $expected);
7856
	}
7857
 
7858
/**
7859
 * Test that postLink adds form tags to view block
7860
 *
7861
 * @return void
7862
 */
7863
	public function testPostLinkFormBuffer() {
7864
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
7865
		$this->assertTags($result, array(
7866
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7867
			'Delete',
7868
			'/a'
7869
		));
7870
 
7871
		$result = $this->View->fetch('postLink');
7872
		$this->assertTags($result, array(
7873
			'form' => array(
7874
				'method' => 'post', 'action' => '/posts/delete/1',
7875
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7876
			),
7877
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7878
			'/form'
7879
		));
7880
 
7881
		$result = $this->Form->postLink('Delete', '/posts/delete/2',
7882
			array('inline' => false, 'method' => 'DELETE')
7883
		);
7884
		$this->assertTags($result, array(
7885
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7886
			'Delete',
7887
			'/a'
7888
		));
7889
 
7890
		$result = $this->View->fetch('postLink');
7891
		$this->assertTags($result, array(
7892
			'form' => array(
7893
				'method' => 'post', 'action' => '/posts/delete/1',
7894
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7895
			),
7896
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7897
			'/form',
7898
			array(
7899
				'form' => array(
7900
					'method' => 'post', 'action' => '/posts/delete/2',
7901
					'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7902
				),
7903
			),
7904
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
7905
			'/form'
7906
		));
7907
 
7908
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
7909
		$this->assertTags($result, array(
7910
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7911
			'Delete',
7912
			'/a'
7913
		));
7914
 
7915
		$result = $this->View->fetch('foobar');
7916
		$this->assertTags($result, array(
7917
			'form' => array(
7918
				'method' => 'post', 'action' => '/posts/delete/1',
7919
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7920
			),
7921
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7922
			'/form'
7923
		));
7924
	}
7925
 
7926
/**
7927
 * testSubmitButton method
7928
 *
7929
 * @return void
7930
 */
7931
	public function testSubmitButton() {
7932
		$result = $this->Form->submit('');
7933
		$expected = array(
7934
			'div' => array('class' => 'submit'),
7935
			'input' => array('type' => 'submit', 'value' => ''),
7936
			'/div'
7937
		);
7938
		$this->assertTags($result, $expected);
7939
 
7940
		$result = $this->Form->submit('Test Submit');
7941
		$expected = array(
7942
			'div' => array('class' => 'submit'),
7943
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
7944
			'/div'
7945
		);
7946
		$this->assertTags($result, $expected);
7947
 
7948
		$result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
7949
		$expected = array(
7950
			'span' => array('class' => 'submit'),
7951
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
7952
			'/span'
7953
		);
7954
		$this->assertTags($result, $expected);
7955
 
7956
		$result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
7957
		$expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
7958
		$this->assertTags($result, $expected);
7959
 
7960
		$result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
7961
		$expected = array(
7962
			'div' => array('class' => 'submit', 'id' => 'SaveButton'),
7963
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
7964
			'/div'
7965
		);
7966
		$this->assertTags($result, $expected);
7967
 
7968
		$result = $this->Form->submit('Next >');
7969
		$expected = array(
7970
			'div' => array('class' => 'submit'),
7971
			'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
7972
			'/div'
7973
		);
7974
		$this->assertTags($result, $expected);
7975
 
7976
		$result = $this->Form->submit('Next >', array('escape' => false));
7977
		$expected = array(
7978
			'div' => array('class' => 'submit'),
7979
			'input' => array('type' => 'submit', 'value' => 'Next >'),
7980
			'/div'
7981
		);
7982
		$this->assertTags($result, $expected);
7983
 
7984
		$result = $this->Form->submit('Reset!', array('type' => 'reset'));
7985
		$expected = array(
7986
			'div' => array('class' => 'submit'),
7987
			'input' => array('type' => 'reset', 'value' => 'Reset!'),
7988
			'/div'
7989
		);
7990
		$this->assertTags($result, $expected);
7991
 
7992
		$before = '--before--';
7993
		$after = '--after--';
7994
		$result = $this->Form->submit('Test', array('before' => $before));
7995
		$expected = array(
7996
			'div' => array('class' => 'submit'),
7997
			'--before--',
7998
			'input' => array('type' => 'submit', 'value' => 'Test'),
7999
			'/div'
8000
		);
8001
		$this->assertTags($result, $expected);
8002
 
8003
		$result = $this->Form->submit('Test', array('after' => $after));
8004
		$expected = array(
8005
			'div' => array('class' => 'submit'),
8006
			'input' => array('type' => 'submit', 'value' => 'Test'),
8007
			'--after--',
8008
			'/div'
8009
		);
8010
		$this->assertTags($result, $expected);
8011
 
8012
		$result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
8013
		$expected = array(
8014
			'div' => array('class' => 'submit'),
8015
			'--before--',
8016
			'input' => array('type' => 'submit', 'value' => 'Test'),
8017
			'--after--',
8018
			'/div'
8019
		);
8020
		$this->assertTags($result, $expected);
8021
	}
8022
 
8023
/**
8024
 * test image submit types.
8025
 *
8026
 * @return void
8027
 */
8028
	public function testSubmitImage() {
8029
		$result = $this->Form->submit('http://example.com/cake.power.gif');
8030
		$expected = array(
8031
			'div' => array('class' => 'submit'),
8032
			'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
8033
			'/div'
8034
		);
8035
		$this->assertTags($result, $expected);
8036
 
8037
		$result = $this->Form->submit('/relative/cake.power.gif');
8038
		$expected = array(
8039
			'div' => array('class' => 'submit'),
8040
			'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
8041
			'/div'
8042
		);
8043
		$this->assertTags($result, $expected);
8044
 
8045
		$result = $this->Form->submit('cake.power.gif');
8046
		$expected = array(
8047
			'div' => array('class' => 'submit'),
8048
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8049
			'/div'
8050
		);
8051
		$this->assertTags($result, $expected);
8052
 
8053
		$result = $this->Form->submit('Not.an.image');
8054
		$expected = array(
8055
			'div' => array('class' => 'submit'),
8056
			'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
8057
			'/div'
8058
		);
8059
		$this->assertTags($result, $expected);
8060
 
8061
		$after = '--after--';
8062
		$before = '--before--';
8063
		$result = $this->Form->submit('cake.power.gif', array('after' => $after));
8064
		$expected = array(
8065
			'div' => array('class' => 'submit'),
8066
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8067
			'--after--',
8068
			'/div'
8069
		);
8070
		$this->assertTags($result, $expected);
8071
 
8072
		$result = $this->Form->submit('cake.power.gif', array('before' => $before));
8073
		$expected = array(
8074
			'div' => array('class' => 'submit'),
8075
			'--before--',
8076
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8077
			'/div'
8078
		);
8079
		$this->assertTags($result, $expected);
8080
 
8081
		$result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
8082
		$expected = array(
8083
			'div' => array('class' => 'submit'),
8084
			'--before--',
8085
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8086
			'--after--',
8087
			'/div'
8088
		);
8089
		$this->assertTags($result, $expected);
8090
 
8091
		$result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
8092
		$expected = array(
8093
			'div' => array('class' => 'submit'),
8094
			'--before--',
8095
			'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
8096
			'--after--',
8097
			'/div'
8098
		);
8099
		$this->assertTags($result, $expected);
8100
	}
8101
 
8102
/**
8103
 * Submit buttons should be unlocked by default as there could be multiples, and only one will
8104
 * be submitted at a time.
8105
 *
8106
 * @return void
8107
 */
8108
	public function testSubmitUnlockedByDefault() {
8109
		$this->Form->request->params['_Token']['key'] = 'secured';
8110
		$this->Form->submit('Go go');
8111
		$this->Form->submit('Save', array('name' => 'save'));
8112
 
8113
		$result = $this->Form->unlockField();
8114
		$this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
8115
	}
8116
 
8117
/**
8118
 * Test submit image with timestamps.
8119
 *
8120
 * @return void
8121
 */
8122
	public function testSubmitImageTimestamp() {
8123
		Configure::write('Asset.timestamp', 'force');
8124
 
8125
		$result = $this->Form->submit('cake.power.gif');
8126
		$expected = array(
8127
			'div' => array('class' => 'submit'),
8128
			'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
8129
			'/div'
8130
		);
8131
		$this->assertTags($result, $expected);
8132
	}
8133
 
8134
/**
8135
 * test the create() method
8136
 *
8137
 * @return void
8138
 */
8139
	public function testCreate() {
8140
		$result = $this->Form->create('Contact');
8141
		$encoding = strtolower(Configure::read('App.encoding'));
8142
		$expected = array(
8143
			'form' => array(
8144
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8145
				'accept-charset' => $encoding
8146
			),
8147
			'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
8148
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8149
			'/div'
8150
		);
8151
		$this->assertTags($result, $expected);
8152
 
8153
		$result = $this->Form->create('Contact', array('type' => 'GET'));
8154
		$expected = array('form' => array(
8155
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8156
			'accept-charset' => $encoding
8157
		));
8158
		$this->assertTags($result, $expected);
8159
 
8160
		$result = $this->Form->create('Contact', array('type' => 'get'));
8161
		$expected = array('form' => array(
8162
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8163
			'accept-charset' => $encoding
8164
		));
8165
		$this->assertTags($result, $expected);
8166
 
8167
		$result = $this->Form->create('Contact', array('type' => 'put'));
8168
		$expected = array(
8169
			'form' => array(
8170
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8171
				'accept-charset' => $encoding
8172
			),
8173
			'div' => array('style' => 'display:none;'),
8174
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8175
			'/div'
8176
		);
8177
		$this->assertTags($result, $expected);
8178
 
8179
		$result = $this->Form->create('Contact', array('type' => 'file'));
8180
		$expected = array(
8181
			'form' => array(
8182
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8183
				'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
8184
			),
8185
			'div' => array('style' => 'display:none;'),
8186
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8187
			'/div'
8188
		);
8189
		$this->assertTags($result, $expected);
8190
 
8191
		$this->Form->request->data['Contact']['id'] = 1;
8192
		$this->Form->request->here = '/contacts/edit/1';
8193
		$this->Form->request['action'] = 'edit';
8194
		$result = $this->Form->create('Contact');
8195
		$expected = array(
8196
			'form' => array(
8197
				'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
8198
				'accept-charset' => $encoding
8199
			),
8200
			'div' => array('style' => 'display:none;'),
8201
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8202
			'/div'
8203
		);
8204
		$this->assertTags($result, $expected);
8205
 
8206
		$this->Form->request->data['Contact']['id'] = 1;
8207
		$this->Form->request->here = '/contacts/edit/1';
8208
		$this->Form->request['action'] = 'edit';
8209
		$result = $this->Form->create('Contact', array('type' => 'file'));
8210
		$expected = array(
8211
			'form' => array(
8212
				'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
8213
				'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
8214
			),
8215
			'div' => array('style' => 'display:none;'),
8216
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8217
			'/div'
8218
		);
8219
		$this->assertTags($result, $expected);
8220
 
8221
		$this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
8222
		$result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
8223
		$expected = array(
8224
			'form' => array(
8225
				'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
8226
				'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
8227
			),
8228
			'div' => array('style' => 'display:none;'),
8229
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8230
			'/div'
8231
		);
8232
		$this->assertTags($result, $expected);
8233
 
8234
		$result = $this->Form->create('Contact', array('id' => 'TestId'));
8235
		$expected = array(
8236
			'form' => array(
8237
				'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
8238
				'accept-charset' => $encoding
8239
			),
8240
			'div' => array('style' => 'display:none;'),
8241
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8242
			'/div'
8243
		);
8244
		$this->assertTags($result, $expected);
8245
 
8246
		$this->Form->request['action'] = 'add';
8247
		$result = $this->Form->create('User', array('url' => array('action' => 'login')));
8248
		$expected = array(
8249
			'form' => array(
8250
				'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
8251
				'accept-charset' => $encoding
8252
			),
8253
			'div' => array('style' => 'display:none;'),
8254
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8255
			'/div'
8256
		);
8257
		$this->assertTags($result, $expected);
8258
 
8259
		$result = $this->Form->create('User', array('action' => 'login'));
8260
		$expected = array(
8261
			'form' => array(
8262
				'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
8263
				'accept-charset' => $encoding
8264
			),
8265
			'div' => array('style' => 'display:none;'),
8266
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8267
			'/div'
8268
		);
8269
		$this->assertTags($result, $expected);
8270
 
8271
		$result = $this->Form->create('User', array('url' => '/users/login'));
8272
		$expected = array(
8273
			'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
8274
			'div' => array('style' => 'display:none;'),
8275
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8276
			'/div'
8277
		);
8278
		$this->assertTags($result, $expected);
8279
 
8280
		$this->Form->request['controller'] = 'pages';
8281
		$result = $this->Form->create('User', array('action' => 'signup'));
8282
		$expected = array(
8283
			'form' => array(
8284
				'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
8285
				'accept-charset' => $encoding
8286
			),
8287
			'div' => array('style' => 'display:none;'),
8288
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8289
			'/div'
8290
		);
8291
		$this->assertTags($result, $expected);
8292
 
8293
		$this->Form->request->data = array();
8294
		$this->Form->request['controller'] = 'contacts';
8295
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8296
		$result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
8297
		$expected = array(
8298
			'form' => array(
8299
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
8300
				'accept-charset' => 'utf-8'
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
 
8309
/**
8310
 * Test the onsubmit option for create()
8311
 *
8312
 * @return void
8313
 */
8314
	public function testCreateOnSubmit() {
8315
		$this->Form->request->data = array();
8316
		$this->Form->request['controller'] = 'contacts';
8317
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8318
		$result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
8319
		$expected = array(
8320
			'form' => array(
8321
				'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
8322
				'accept-charset' => 'utf-8'
8323
			),
8324
			'div' => array('style' => 'display:none;'),
8325
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8326
			'/div'
8327
		);
8328
		$this->assertTags($result, $expected);
8329
 
8330
		$this->Form->request->data = array();
8331
		$this->Form->request['controller'] = 'contacts';
8332
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8333
		$result = $this->Form->create(array(
8334
			'url' => array('action' => 'index', 'param'),
8335
			'default' => false,
8336
			'onsubmit' => 'someFunction();'
8337
		));
8338
 
8339
		$expected = array(
8340
			'form' => array(
8341
				'id' => 'ContactAddForm', 'method' => 'post',
8342
				'onsubmit' => 'someFunction();event.returnValue = false; return false;',
8343
				'action' => '/contacts/index/param',
8344
				'accept-charset' => 'utf-8'
8345
			),
8346
			'div' => array('style' => 'display:none;'),
8347
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8348
			'/div'
8349
		);
8350
		$this->assertTags($result, $expected);
8351
	}
8352
 
8353
/**
8354
 * test create() with automatic url generation
8355
 *
8356
 * @return void
8357
 */
8358
	public function testCreateAutoUrl() {
8359
		Router::setRequestInfo(array(array(), array('base' => '/base_url')));
8360
		$this->Form->request->here = '/base_url/contacts/add/Contact:1';
8361
		$this->Form->request->base = '/base_url';
8362
		$result = $this->Form->create('Contact');
8363
		$expected = array(
8364
			'form' => array(
8365
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
8366
				'accept-charset' => 'utf-8'
8367
			),
8368
			'div' => array('style' => 'display:none;'),
8369
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8370
			'/div'
8371
		);
8372
		$this->assertTags($result, $expected);
8373
 
8374
		$this->Form->request['action'] = 'delete';
8375
		$this->Form->request->here = '/base_url/contacts/delete/10/User:42';
8376
		$this->Form->request->base = '/base_url';
8377
		$result = $this->Form->create('Contact');
8378
		$expected = array(
8379
			'form' => array(
8380
				'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
8381
				'accept-charset' => 'utf-8'
8382
			),
8383
			'div' => array('style' => 'display:none;'),
8384
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8385
			'/div'
8386
		);
8387
		$this->assertTags($result, $expected);
8388
	}
8389
 
8390
/**
8391
 * test create() with a custom route
8392
 *
8393
 * @return void
8394
 */
8395
	public function testCreateCustomRoute() {
8396
		Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
8397
		$encoding = strtolower(Configure::read('App.encoding'));
8398
 
8399
		$result = $this->Form->create('User', array('action' => 'login'));
8400
		$expected = array(
8401
			'form' => array(
8402
				'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
8403
				'accept-charset' => $encoding
8404
			),
8405
			'div' => array('style' => 'display:none;'),
8406
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8407
			'/div'
8408
		);
8409
		$this->assertTags($result, $expected);
8410
	}
8411
 
8412
/**
8413
 * test that inputDefaults are stored and used.
8414
 *
8415
 * @return void
8416
 */
8417
	public function testCreateWithInputDefaults() {
8418
		$this->Form->create('User', array(
8419
			'inputDefaults' => array(
8420
				'div' => false,
8421
				'label' => false,
8422
				'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
8423
				'format' => array('before', 'label', 'between', 'input', 'after', 'error')
8424
			)
8425
		));
8426
		$result = $this->Form->input('username');
8427
		$expected = array(
8428
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
8429
		);
8430
		$this->assertTags($result, $expected);
8431
 
8432
		$result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
8433
		$expected = array(
8434
			'div' => array('class' => 'input text'),
8435
			'label' => array('for' => 'UserUsername'), 'username', '/label',
8436
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8437
			'/div'
8438
		);
8439
		$this->assertTags($result, $expected);
8440
 
8441
		$result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
8442
		$expected = array(
8443
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8444
			'label' => array('for' => 'UserUsername'), 'Username', '/label',
8445
		);
8446
		$this->assertTags($result, $expected);
8447
 
8448
		$this->Form->create('User', array(
8449
			'inputDefaults' => array(
8450
				'div' => false,
8451
				'label' => array('class' => 'nice', 'for' => 'changed'),
8452
			)
8453
		));
8454
		$result = $this->Form->input('username', array('div' => true));
8455
		$expected = array(
8456
			'div' => array('class' => 'input text'),
8457
			'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
8458
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8459
			'/div'
8460
		);
8461
		$this->assertTags($result, $expected);
8462
	}
8463
 
8464
/**
8465
 * test automatic accept-charset overriding
8466
 *
8467
 * @return void
8468
 */
8469
	public function testCreateWithAcceptCharset() {
8470
		$result = $this->Form->create('UserForm', array(
8471
				'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
8472
			)
8473
		);
8474
		$expected = array(
8475
			'form' => array(
8476
				'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
8477
				'accept-charset' => 'iso-8859-1'
8478
			),
8479
			'div' => array('style' => 'display:none;'),
8480
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8481
			'/div'
8482
		);
8483
		$this->assertTags($result, $expected);
8484
	}
8485
 
8486
/**
8487
 * Test base form URL when url param is passed with multiple parameters (&)
8488
 *
8489
 * @return void
8490
 */
8491
	public function testCreateQuerystringrequest() {
8492
		$encoding = strtolower(Configure::read('App.encoding'));
8493
		$result = $this->Form->create('Contact', array(
8494
			'type' => 'post',
8495
			'escape' => false,
8496
			'url' => array(
8497
				'controller' => 'controller',
8498
				'action' => 'action',
8499
				'?' => array('param1' => 'value1', 'param2' => 'value2')
8500
			)
8501
		));
8502
		$expected = array(
8503
			'form' => array(
8504
				'id' => 'ContactAddForm',
8505
				'method' => 'post',
8506
				'action' => '/controller/action?param1=value1&amp;param2=value2',
8507
				'accept-charset' => $encoding
8508
			),
8509
			'div' => array('style' => 'display:none;'),
8510
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8511
			'/div'
8512
		);
8513
		$this->assertTags($result, $expected);
8514
 
8515
		$result = $this->Form->create('Contact', array(
8516
			'type' => 'post',
8517
			'url' => array(
8518
				'controller' => 'controller',
8519
				'action' => 'action',
8520
				'?' => array('param1' => 'value1', 'param2' => 'value2')
8521
			)
8522
		));
8523
		$expected = array(
8524
			'form' => array(
8525
				'id' => 'ContactAddForm',
8526
				'method' => 'post',
8527
				'action' => '/controller/action?param1=value1&amp;param2=value2',
8528
				'accept-charset' => $encoding
8529
			),
8530
			'div' => array('style' => 'display:none;'),
8531
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8532
			'/div'
8533
		);
8534
		$this->assertTags($result, $expected);
8535
	}
8536
 
8537
/**
8538
 * test that create() doesn't cause errors by multiple id's being in the primary key
8539
 * as could happen with multiple select or checkboxes.
8540
 *
8541
 * @return void
8542
 */
8543
	public function testCreateWithMultipleIdInData() {
8544
		$encoding = strtolower(Configure::read('App.encoding'));
8545
 
8546
		$this->Form->request->data['Contact']['id'] = array(1, 2);
8547
		$result = $this->Form->create('Contact');
8548
		$expected = array(
8549
			'form' => array(
8550
				'id' => 'ContactAddForm',
8551
				'method' => 'post',
8552
				'action' => '/contacts/add',
8553
				'accept-charset' => $encoding
8554
			),
8555
			'div' => array('style' => 'display:none;'),
8556
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8557
			'/div'
8558
		);
8559
		$this->assertTags($result, $expected);
8560
	}
8561
 
8562
/**
8563
 * test that create() doesn't add in extra passed params.
8564
 *
8565
 * @return void
8566
 */
8567
	public function testCreatePassedArgs() {
8568
		$encoding = strtolower(Configure::read('App.encoding'));
8569
		$this->Form->request->data['Contact']['id'] = 1;
8570
		$result = $this->Form->create('Contact', array(
8571
			'type' => 'post',
8572
			'escape' => false,
8573
			'url' => array(
8574
				'action' => 'edit',
8575
				'myparam'
8576
			)
8577
		));
8578
		$expected = array(
8579
			'form' => array(
8580
				'id' => 'ContactAddForm',
8581
				'method' => 'post',
8582
				'action' => '/contacts/edit/myparam',
8583
				'accept-charset' => $encoding
8584
			),
8585
			'div' => array('style' => 'display:none;'),
8586
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8587
			'/div'
8588
		);
8589
		$this->assertTags($result, $expected);
8590
	}
8591
 
8592
/**
8593
 * test that create() works without raising errors with a Mock Model
8594
 *
8595
 * @return void
8596
 */
8597
	public function testCreateNoErrorsWithMockModel() {
8598
		$encoding = strtolower(Configure::read('App.encoding'));
8599
		$ContactMock = $this->getMockBuilder('Contact')
8600
				->disableOriginalConstructor()
8601
				->getMock();
8602
		ClassRegistry::removeObject('Contact');
8603
		ClassRegistry::addObject('Contact', $ContactMock);
8604
		$result = $this->Form->create('Contact', array('type' => 'GET'));
8605
		$expected = array('form' => array(
8606
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8607
			'accept-charset' => $encoding
8608
		));
8609
		$this->assertTags($result, $expected);
8610
	}
8611
 
8612
/**
8613
 * test creating a get form, and get form inputs.
8614
 *
8615
 * @return void
8616
 */
8617
	public function testGetFormCreate() {
8618
		$encoding = strtolower(Configure::read('App.encoding'));
8619
		$result = $this->Form->create('Contact', array('type' => 'get'));
8620
		$this->assertTags($result, array('form' => array(
8621
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8622
			'accept-charset' => $encoding
8623
		)));
8624
 
8625
		$result = $this->Form->text('Contact.name');
8626
		$this->assertTags($result, array('input' => array(
8627
			'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
8628
		)));
8629
 
8630
		$result = $this->Form->password('password');
8631
		$this->assertTags($result, array('input' => array(
8632
			'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
8633
		)));
8634
		$this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
8635
 
8636
		$result = $this->Form->text('user_form');
8637
		$this->assertTags($result, array('input' => array(
8638
			'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
8639
		)));
8640
	}
8641
 
8642
/**
8643
 * test get form, and inputs when the model param is false
8644
 *
8645
 * @return void
8646
 */
8647
	public function testGetFormWithFalseModel() {
8648
		$encoding = strtolower(Configure::read('App.encoding'));
8649
		$this->Form->request['controller'] = 'contact_test';
8650
		$result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
8651
 
8652
		$expected = array('form' => array(
8653
			'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
8654
			'accept-charset' => $encoding
8655
		));
8656
		$this->assertTags($result, $expected);
8657
 
8658
		$result = $this->Form->text('reason');
8659
		$expected = array(
8660
			'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
8661
		);
8662
		$this->assertTags($result, $expected);
8663
	}
8664
 
8665
/**
8666
 * test that datetime() works with GET style forms.
8667
 *
8668
 * @return void
8669
 */
8670
	public function testDateTimeWithGetForms() {
8671
		extract($this->dateRegex);
8672
		$this->Form->create('Contact', array('type' => 'get'));
8673
		$result = $this->Form->datetime('created');
8674
 
8675
		$this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
8676
		$this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
8677
		$this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
8678
		$this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
8679
		$this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
8680
		$this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
8681
	}
8682
 
8683
/**
8684
 * testEditFormWithData method
8685
 *
8686
 * test auto populating form elements from submitted data.
8687
 *
8688
 * @return void
8689
 */
8690
	public function testEditFormWithData() {
8691
		$this->Form->request->data = array('Person' => array(
8692
			'id' => 1,
8693
			'first_name' => 'Nate',
8694
			'last_name' => 'Abele',
8695
			'email' => 'nate@example.com'
8696
		));
8697
		$this->Form->request->addParams(array(
8698
			'models' => array('Person'),
8699
			'controller' => 'people',
8700
			'action' => 'add'
8701
		));
8702
		$options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
8703
 
8704
		$this->Form->create();
8705
		$result = $this->Form->select('People.People', $options, array('multiple' => true));
8706
		$expected = array(
8707
			'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
8708
			'select' => array(
8709
				'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
8710
			),
8711
			array('option' => array('value' => 1)), 'Nate', '/option',
8712
			array('option' => array('value' => 2)), 'Garrett', '/option',
8713
			array('option' => array('value' => 3)), 'Larry', '/option',
8714
			'/select'
8715
		);
8716
		$this->assertTags($result, $expected);
8717
	}
8718
 
8719
/**
8720
 * Test that required fields are created for various types of validation.
8721
 *
8722
 * @return void
8723
 */
8724
	public function testFormInputRequiredDetection() {
8725
		$this->Form->create('Contact');
8726
 
8727
		$result = $this->Form->input('Contact.non_existing');
8728
		$expected = array(
8729
			'div' => array('class' => 'input text'),
8730
			'label' => array('for' => 'ContactNonExisting'),
8731
			'Non Existing',
8732
			'/label',
8733
			'input' => array(
8734
				'type' => 'text', 'name' => 'data[Contact][non_existing]',
8735
				'id' => 'ContactNonExisting'
8736
			),
8737
			'/div'
8738
		);
8739
		$this->assertTags($result, $expected);
8740
 
8741
		$result = $this->Form->input('Contact.imrequired');
8742
		$expected = array(
8743
			'div' => array('class' => 'input text required'),
8744
			'label' => array('for' => 'ContactImrequired'),
8745
			'Imrequired',
8746
			'/label',
8747
			'input' => array(
8748
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
8749
				'id' => 'ContactImrequired',
8750
				'required' => 'required'
8751
			),
8752
			'/div'
8753
		);
8754
		$this->assertTags($result, $expected);
8755
 
8756
		$result = $this->Form->input('Contact.imalsorequired');
8757
		$expected = array(
8758
			'div' => array('class' => 'input text required'),
8759
			'label' => array('for' => 'ContactImalsorequired'),
8760
			'Imalsorequired',
8761
			'/label',
8762
			'input' => array(
8763
				'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
8764
				'id' => 'ContactImalsorequired',
8765
				'required' => 'required'
8766
			),
8767
			'/div'
8768
		);
8769
		$this->assertTags($result, $expected);
8770
 
8771
		$result = $this->Form->input('Contact.imrequiredtoo');
8772
		$expected = array(
8773
			'div' => array('class' => 'input text required'),
8774
			'label' => array('for' => 'ContactImrequiredtoo'),
8775
			'Imrequiredtoo',
8776
			'/label',
8777
			'input' => array(
8778
				'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
8779
				'id' => 'ContactImrequiredtoo',
8780
				'required' => 'required'
8781
			),
8782
			'/div'
8783
		);
8784
		$this->assertTags($result, $expected);
8785
 
8786
		$result = $this->Form->input('Contact.required_one');
8787
		$expected = array(
8788
			'div' => array('class' => 'input text required'),
8789
			'label' => array('for' => 'ContactRequiredOne'),
8790
			'Required One',
8791
			'/label',
8792
			'input' => array(
8793
				'type' => 'text', 'name' => 'data[Contact][required_one]',
8794
				'id' => 'ContactRequiredOne',
8795
				'required' => 'required'
8796
			),
8797
			'/div'
8798
		);
8799
		$this->assertTags($result, $expected);
8800
 
8801
		$result = $this->Form->input('Contact.string_required');
8802
		$expected = array(
8803
			'div' => array('class' => 'input text required'),
8804
			'label' => array('for' => 'ContactStringRequired'),
8805
			'String Required',
8806
			'/label',
8807
			'input' => array(
8808
				'type' => 'text', 'name' => 'data[Contact][string_required]',
8809
				'id' => 'ContactStringRequired',
8810
				'required' => 'required'
8811
			),
8812
			'/div'
8813
		);
8814
		$this->assertTags($result, $expected);
8815
 
8816
		$result = $this->Form->input('Contact.imnotrequired');
8817
		$expected = array(
8818
			'div' => array('class' => 'input text'),
8819
			'label' => array('for' => 'ContactImnotrequired'),
8820
			'Imnotrequired',
8821
			'/label',
8822
			'input' => array(
8823
				'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
8824
				'id' => 'ContactImnotrequired'
8825
			),
8826
			'/div'
8827
		);
8828
		$this->assertTags($result, $expected);
8829
 
8830
		$result = $this->Form->input('Contact.imalsonotrequired');
8831
		$expected = array(
8832
			'div' => array('class' => 'input text'),
8833
			'label' => array('for' => 'ContactImalsonotrequired'),
8834
			'Imalsonotrequired',
8835
			'/label',
8836
			'input' => array(
8837
				'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
8838
				'id' => 'ContactImalsonotrequired'
8839
			),
8840
			'/div'
8841
		);
8842
		$this->assertTags($result, $expected);
8843
 
8844
		$result = $this->Form->input('Contact.imalsonotrequired2');
8845
		$expected = array(
8846
			'div' => array('class' => 'input text'),
8847
			'label' => array('for' => 'ContactImalsonotrequired2'),
8848
			'Imalsonotrequired2',
8849
			'/label',
8850
			'input' => array(
8851
				'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
8852
				'id' => 'ContactImalsonotrequired2'
8853
			),
8854
			'/div'
8855
		);
8856
		$this->assertTags($result, $expected);
8857
 
8858
		$result = $this->Form->input('Contact.imnotrequiredeither');
8859
		$expected = array(
8860
			'div' => array('class' => 'input text'),
8861
			'label' => array('for' => 'ContactImnotrequiredeither'),
8862
			'Imnotrequiredeither',
8863
			'/label',
8864
			'input' => array(
8865
				'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
8866
				'id' => 'ContactImnotrequiredeither'
8867
			),
8868
			'/div'
8869
		);
8870
		$this->assertTags($result, $expected);
8871
 
8872
		$result = $this->Form->input('Contact.iamrequiredalways');
8873
		$expected = array(
8874
			'div' => array('class' => 'input text required'),
8875
			'label' => array('for' => 'ContactIamrequiredalways'),
8876
			'Iamrequiredalways',
8877
			'/label',
8878
			'input' => array(
8879
				'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
8880
				'id' => 'ContactIamrequiredalways',
8881
				'required' => 'required'
8882
			),
8883
			'/div'
8884
		);
8885
		$this->assertTags($result, $expected);
8886
 
8887
		$result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
8888
		$expected = array(
8889
			'div' => array('class' => 'input checkbox required'),
8890
			array('input' => array(
8891
				'type' => 'hidden',
8892
				'name' => 'data[Contact][boolean_field]',
8893
				'id' => 'ContactBooleanField_',
8894
				'value' => '0'
8895
			)),
8896
			array('input' => array(
8897
				'type' => 'checkbox',
8898
				'name' => 'data[Contact][boolean_field]',
8899
				'value' => '1',
8900
				'id' => 'ContactBooleanField'
8901
			)),
8902
			'label' => array('for' => 'ContactBooleanField'),
8903
			'Boolean Field',
8904
			'/label',
8905
			'/div'
8906
		);
8907
		$this->assertTags($result, $expected);
8908
 
8909
		$result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
8910
		$expected = array(
8911
			'div' => array('class' => 'input checkbox required'),
8912
			array('input' => array(
8913
				'type' => 'hidden',
8914
				'name' => 'data[Contact][boolean_field]',
8915
				'id' => 'ContactBooleanField_',
8916
				'value' => '0'
8917
			)),
8918
			array('input' => array(
8919
				'type' => 'checkbox',
8920
				'name' => 'data[Contact][boolean_field]',
8921
				'value' => '1',
8922
				'id' => 'ContactBooleanField',
8923
				'required' => 'required'
8924
			)),
8925
			'label' => array('for' => 'ContactBooleanField'),
8926
			'Boolean Field',
8927
			'/label',
8928
			'/div'
8929
		);
8930
		$this->assertTags($result, $expected);
8931
 
8932
		$result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
8933
		$expected = array(
8934
			'div' => array('class' => 'input file required'),
8935
			'label' => array('for' => 'ContactIamrequiredalways'),
8936
			'Iamrequiredalways',
8937
			'/label',
8938
			'input' => array(
8939
				'type' => 'file',
8940
				'name' => 'data[Contact][iamrequiredalways]',
8941
				'id' => 'ContactIamrequiredalways',
8942
				'required' => 'required'
8943
			),
8944
			'/div'
8945
		);
8946
		$this->assertTags($result, $expected);
8947
	}
8948
 
8949
/**
8950
 * Test that required fields are created when only using ModelValidator::add().
8951
 *
8952
 * @return void
8953
 */
8954
	public function testFormInputRequiredDetectionModelValidator() {
8955
		ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
8956
 
8957
		$this->Form->create('ContactTag');
8958
		$result = $this->Form->input('ContactTag.iwillberequired');
8959
		$expected = array(
8960
			'div' => array('class' => 'input text required'),
8961
			'label' => array('for' => 'ContactTagIwillberequired'),
8962
			'Iwillberequired',
8963
			'/label',
8964
			'input' => array(
8965
				'name' => 'data[ContactTag][iwillberequired]',
8966
				'type' => 'text',
8967
				'id' => 'ContactTagIwillberequired',
8968
				'required' => 'required'
8969
			),
8970
			'/div'
8971
		);
8972
		$this->assertTags($result, $expected);
8973
	}
8974
 
8975
/**
8976
 * testFormMagicInput method
8977
 *
8978
 * @return void
8979
 */
8980
	public function testFormMagicInput() {
8981
		$encoding = strtolower(Configure::read('App.encoding'));
8982
		$result = $this->Form->create('Contact');
8983
		$expected = array(
8984
			'form' => array(
8985
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8986
				'accept-charset' => $encoding
8987
			),
8988
			'div' => array('style' => 'display:none;'),
8989
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8990
			'/div'
8991
		);
8992
		$this->assertTags($result, $expected);
8993
 
8994
		$result = $this->Form->input('name');
8995
		$expected = array(
8996
			'div' => array('class' => 'input text'),
8997
			'label' => array('for' => 'ContactName'),
8998
			'Name',
8999
			'/label',
9000
			'input' => array(
9001
				'type' => 'text', 'name' => 'data[Contact][name]',
9002
				'id' => 'ContactName', 'maxlength' => '255'
9003
			),
9004
			'/div'
9005
		);
9006
		$this->assertTags($result, $expected);
9007
 
9008
		$result = $this->Form->input('non_existing_field_in_contact_model');
9009
		$expected = array(
9010
			'div' => array('class' => 'input text'),
9011
			'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
9012
			'Non Existing Field In Contact Model',
9013
			'/label',
9014
			'input' => array(
9015
				'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
9016
				'id' => 'ContactNonExistingFieldInContactModel'
9017
			),
9018
			'/div'
9019
		);
9020
		$this->assertTags($result, $expected);
9021
 
9022
		$result = $this->Form->input('Address.street');
9023
		$expected = array(
9024
			'div' => array('class' => 'input text'),
9025
			'label' => array('for' => 'AddressStreet'),
9026
			'Street',
9027
			'/label',
9028
			'input' => array(
9029
				'type' => 'text', 'name' => 'data[Address][street]',
9030
				'id' => 'AddressStreet'
9031
			),
9032
			'/div'
9033
		);
9034
		$this->assertTags($result, $expected);
9035
 
9036
		$result = $this->Form->input('Address.non_existing_field_in_model');
9037
		$expected = array(
9038
			'div' => array('class' => 'input text'),
9039
			'label' => array('for' => 'AddressNonExistingFieldInModel'),
9040
			'Non Existing Field In Model',
9041
			'/label',
9042
			'input' => array(
9043
				'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
9044
				'id' => 'AddressNonExistingFieldInModel'
9045
			),
9046
			'/div'
9047
		);
9048
		$this->assertTags($result, $expected);
9049
 
9050
		$result = $this->Form->input('name', array('div' => false));
9051
		$expected = array(
9052
			'label' => array('for' => 'ContactName'),
9053
			'Name',
9054
			'/label',
9055
			'input' => array(
9056
				'type' => 'text', 'name' => 'data[Contact][name]',
9057
				'id' => 'ContactName', 'maxlength' => '255'
9058
			)
9059
		);
9060
		$this->assertTags($result, $expected);
9061
 
9062
		extract($this->dateRegex);
9063
		$now = strtotime('now');
9064
 
9065
		$result = $this->Form->input('Contact.published', array('div' => false));
9066
		$expected = array(
9067
			'label' => array('for' => 'ContactPublishedMonth'),
9068
			'Published',
9069
			'/label',
9070
			array('select' => array(
9071
				'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
9072
			)),
9073
			$monthsRegex,
9074
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
9075
			date('F', $now),
9076
			'/option',
9077
			'*/select',
9078
			'-',
9079
			array('select' => array(
9080
				'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
9081
			)),
9082
			$daysRegex,
9083
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
9084
			date('j', $now),
9085
			'/option',
9086
			'*/select',
9087
			'-',
9088
			array('select' => array(
9089
				'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
9090
			)),
9091
			$yearsRegex,
9092
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
9093
			date('Y', $now),
9094
			'*/select'
9095
		);
9096
		$this->assertTags($result, $expected);
9097
 
9098
		$result = $this->Form->input('Contact.updated', array('div' => false));
9099
		$expected = array(
9100
			'label' => array('for' => 'ContactUpdatedMonth'),
9101
			'Updated',
9102
			'/label',
9103
			array('select' => array(
9104
				'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
9105
			)),
9106
			$monthsRegex,
9107
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
9108
			date('F', $now),
9109
			'/option',
9110
			'*/select',
9111
			'-',
9112
			array('select' => array(
9113
				'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
9114
			)),
9115
			$daysRegex,
9116
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
9117
			date('j', $now),
9118
			'/option',
9119
			'*/select',
9120
			'-',
9121
			array('select' => array(
9122
				'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
9123
			)),
9124
			$yearsRegex,
9125
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
9126
			date('Y', $now),
9127
			'*/select'
9128
		);
9129
		$this->assertTags($result, $expected);
9130
 
9131
		$result = $this->Form->input('UserForm.stuff');
9132
		$expected = array(
9133
			'div' => array('class' => 'input text'),
9134
			'label' => array('for' => 'UserFormStuff'),
9135
			'Stuff',
9136
			'/label',
9137
			'input' => array(
9138
				'type' => 'text', 'name' => 'data[UserForm][stuff]',
9139
				'id' => 'UserFormStuff', 'maxlength' => 10
9140
			),
9141
			'/div'
9142
		);
9143
		$this->assertTags($result, $expected);
9144
	}
9145
 
9146
/**
9147
 * testForMagicInputNonExistingNorValidated method
9148
 *
9149
 * @return void
9150
 */
9151
	public function testForMagicInputNonExistingNorValidated() {
9152
		$encoding = strtolower(Configure::read('App.encoding'));
9153
		$result = $this->Form->create('Contact');
9154
		$expected = array(
9155
			'form' => array(
9156
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
9157
				'accept-charset' => $encoding
9158
			),
9159
			'div' => array('style' => 'display:none;'),
9160
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
9161
			'/div'
9162
		);
9163
		$this->assertTags($result, $expected);
9164
 
9165
		$result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
9166
		$expected = array(
9167
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9168
			'Non Existing Nor Validated',
9169
			'/label',
9170
			'input' => array(
9171
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9172
				'id' => 'ContactNonExistingNorValidated'
9173
			)
9174
		);
9175
		$this->assertTags($result, $expected);
9176
 
9177
		$result = $this->Form->input('Contact.non_existing_nor_validated', array(
9178
			'div' => false, 'value' => 'my value'
9179
		));
9180
		$expected = array(
9181
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9182
			'Non Existing Nor Validated',
9183
			'/label',
9184
			'input' => array(
9185
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9186
				'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
9187
			)
9188
		);
9189
		$this->assertTags($result, $expected);
9190
 
9191
		$this->Form->request->data = array(
9192
			'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
9193
		));
9194
		$result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
9195
		$expected = array(
9196
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9197
			'Non Existing Nor Validated',
9198
			'/label',
9199
			'input' => array(
9200
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9201
				'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
9202
			)
9203
		);
9204
		$this->assertTags($result, $expected);
9205
	}
9206
 
9207
/**
9208
 * testFormMagicInputLabel method
9209
 *
9210
 * @return void
9211
 */
9212
	public function testFormMagicInputLabel() {
9213
		$encoding = strtolower(Configure::read('App.encoding'));
9214
		$result = $this->Form->create('Contact');
9215
		$expected = array(
9216
			'form' => array(
9217
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
9218
				'accept-charset' => $encoding
9219
			),
9220
			'div' => array('style' => 'display:none;'),
9221
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
9222
			'/div'
9223
		);
9224
		$this->assertTags($result, $expected);
9225
 
9226
		$result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
9227
		$this->assertTags($result, array('input' => array(
9228
			'name' => 'data[Contact][name]', 'type' => 'text',
9229
			'id' => 'ContactName', 'maxlength' => '255')
9230
		));
9231
 
9232
		$result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
9233
		$expected = array(
9234
			'label' => array('for' => 'ContactName'),
9235
			'My label',
9236
			'/label',
9237
			'input' => array(
9238
				'type' => 'text', 'name' => 'data[Contact][name]',
9239
				'id' => 'ContactName', 'maxlength' => '255'
9240
			)
9241
		);
9242
		$this->assertTags($result, $expected);
9243
 
9244
		$result = $this->Form->input('Contact.name', array(
9245
			'div' => false, 'label' => array('class' => 'mandatory')
9246
		));
9247
		$expected = array(
9248
			'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
9249
			'Name',
9250
			'/label',
9251
			'input' => array(
9252
				'type' => 'text', 'name' => 'data[Contact][name]',
9253
				'id' => 'ContactName', 'maxlength' => '255'
9254
			)
9255
		);
9256
		$this->assertTags($result, $expected);
9257
 
9258
		$result = $this->Form->input('Contact.name', array(
9259
			'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
9260
		));
9261
		$expected = array(
9262
			'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
9263
			'My label',
9264
			'/label',
9265
			'input' => array(
9266
				'type' => 'text', 'name' => 'data[Contact][name]',
9267
				'id' => 'ContactName', 'maxlength' => '255'
9268
			)
9269
		);
9270
		$this->assertTags($result, $expected);
9271
 
9272
		$result = $this->Form->input('Contact.name', array(
9273
			'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
9274
		));
9275
		$expected = array(
9276
			'label' => array('for' => 'my_id'),
9277
			'Name',
9278
			'/label',
9279
			'input' => array(
9280
				'type' => 'text', 'name' => 'data[Contact][name]',
9281
				'id' => 'my_id', 'maxlength' => '255'
9282
			)
9283
		);
9284
		$this->assertTags($result, $expected);
9285
 
9286
		$result = $this->Form->input('1.id');
9287
		$this->assertTags($result, array('input' => array(
9288
			'type' => 'hidden', 'name' => 'data[Contact][1][id]',
9289
			'id' => 'Contact1Id'
9290
		)));
9291
 
9292
		$result = $this->Form->input("1.name");
9293
		$expected = array(
9294
			'div' => array('class' => 'input text'),
9295
			'label' => array('for' => 'Contact1Name'),
9296
			'Name',
9297
			'/label',
9298
			'input' => array(
9299
				'type' => 'text', 'name' => 'data[Contact][1][name]',
9300
				'id' => 'Contact1Name', 'maxlength' => '255'
9301
			),
9302
			'/div'
9303
		);
9304
		$this->assertTags($result, $expected);
9305
 
9306
		$result = $this->Form->input('Contact.1.id');
9307
		$this->assertTags($result, array(
9308
			'input' => array(
9309
				'type' => 'hidden', 'name' => 'data[Contact][1][id]',
9310
				'id' => 'Contact1Id'
9311
			)
9312
		));
9313
 
9314
		$result = $this->Form->input("Model.1.name");
9315
		$expected = array(
9316
			'div' => array('class' => 'input text'),
9317
			'label' => array('for' => 'Model1Name'),
9318
			'Name',
9319
			'/label',
9320
			'input' => array(
9321
				'type' => 'text', 'name' => 'data[Model][1][name]',
9322
				'id' => 'Model1Name'
9323
			),
9324
			'/div'
9325
		);
9326
		$this->assertTags($result, $expected);
9327
	}
9328
 
9329
/**
9330
 * testFormEnd method
9331
 *
9332
 * @return void
9333
 */
9334
	public function testFormEnd() {
9335
		$this->assertEquals('</form>', $this->Form->end());
9336
 
9337
		$result = $this->Form->end('', array('form' => 'form-name'));
9338
		$expected = array(
9339
			'div' => array('class' => 'submit'),
9340
			'input' => array('type' => 'submit', 'value' => ''),
9341
			'/div',
9342
			'/form'
9343
		);
9344
		$this->assertTags($result, $expected);
9345
 
9346
		$result = $this->Form->end('');
9347
		$expected = array(
9348
			'div' => array('class' => 'submit'),
9349
			'input' => array('type' => 'submit', 'value' => ''),
9350
			'/div',
9351
			'/form'
9352
		);
9353
		$this->assertTags($result, $expected);
9354
 
9355
		$result = $this->Form->end(array('label' => ''));
9356
		$expected = array(
9357
			'div' => array('class' => 'submit'),
9358
			'input' => array('type' => 'submit', 'value' => ''),
9359
			'/div',
9360
			'/form'
9361
		);
9362
		$this->assertTags($result, $expected);
9363
 
9364
		$result = $this->Form->end('save');
9365
		$expected = array(
9366
			'div' => array('class' => 'submit'),
9367
			'input' => array('type' => 'submit', 'value' => 'save'),
9368
			'/div',
9369
			'/form'
9370
		);
9371
		$this->assertTags($result, $expected);
9372
 
9373
		$result = $this->Form->end(array('label' => 'save'));
9374
		$expected = array(
9375
			'div' => array('class' => 'submit'),
9376
			'input' => array('type' => 'submit', 'value' => 'save'),
9377
			'/div',
9378
			'/form'
9379
		);
9380
		$this->assertTags($result, $expected);
9381
 
9382
		$result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
9383
		$expected = array(
9384
			'div' => array('class' => 'submit'),
9385
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9386
			'/div',
9387
			'/form'
9388
		);
9389
		$this->assertTags($result, $expected);
9390
 
9391
		$result = $this->Form->end(array('name' => 'Whatever'));
9392
		$expected = array(
9393
			'div' => array('class' => 'submit'),
9394
			'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
9395
			'/div',
9396
			'/form'
9397
		);
9398
		$this->assertTags($result, $expected);
9399
 
9400
		$result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
9401
		$expected = array(
9402
			'div' => array('class' => 'good'),
9403
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9404
			'/div',
9405
			'/form'
9406
		);
9407
		$this->assertTags($result, $expected);
9408
 
9409
		$result = $this->Form->end(array(
9410
			'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
9411
		));
9412
		$expected = array(
9413
			'div' => array('class' => 'good'),
9414
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9415
			'/div',
9416
			'/form'
9417
		);
9418
		$this->assertTags($result, $expected);
9419
	}
9420
 
9421
/**
9422
 * testMultipleFormWithIdFields method
9423
 *
9424
 * @return void
9425
 */
9426
	public function testMultipleFormWithIdFields() {
9427
		$this->Form->create('UserForm');
9428
 
9429
		$result = $this->Form->input('id');
9430
		$this->assertTags($result, array('input' => array(
9431
			'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
9432
		)));
9433
 
9434
		$result = $this->Form->input('ValidateItem.id');
9435
		$this->assertTags($result, array('input' => array(
9436
			'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
9437
			'id' => 'ValidateItemId'
9438
		)));
9439
 
9440
		$result = $this->Form->input('ValidateUser.id');
9441
		$this->assertTags($result, array('input' => array(
9442
			'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
9443
			'id' => 'ValidateUserId'
9444
		)));
9445
	}
9446
 
9447
/**
9448
 * testDbLessModel method
9449
 *
9450
 * @return void
9451
 */
9452
	public function testDbLessModel() {
9453
		$this->Form->create('TestMail');
9454
 
9455
		$result = $this->Form->input('name');
9456
		$expected = array(
9457
			'div' => array('class' => 'input text'),
9458
			'label' => array('for' => 'TestMailName'),
9459
			'Name',
9460
			'/label',
9461
			'input' => array(
9462
				'name' => 'data[TestMail][name]', 'type' => 'text',
9463
				'id' => 'TestMailName'
9464
			),
9465
			'/div'
9466
		);
9467
		$this->assertTags($result, $expected);
9468
 
9469
		ClassRegistry::init('TestMail');
9470
		$this->Form->create('TestMail');
9471
		$result = $this->Form->input('name');
9472
		$expected = array(
9473
			'div' => array('class' => 'input text'),
9474
			'label' => array('for' => 'TestMailName'),
9475
			'Name',
9476
			'/label',
9477
			'input' => array(
9478
				'name' => 'data[TestMail][name]', 'type' => 'text',
9479
				'id' => 'TestMailName'
9480
			),
9481
			'/div'
9482
		);
9483
		$this->assertTags($result, $expected);
9484
	}
9485
 
9486
/**
9487
 * testBrokenness method
9488
 *
9489
 * @return void
9490
 */
9491
	public function testBrokenness() {
9492
		/*
9493
		 * #4 This test has two parents and four children. By default (as of r7117) both
9494
		 * parents are show but the first parent is missing a child. This is the inconsistency
9495
		 * in the default behaviour - one parent has all children, the other does not - dependent
9496
		 * on the data values.
9497
		 */
9498
		$result = $this->Form->select('Model.field', array(
9499
			'Fred' => array(
9500
				'freds_son_1' => 'Fred',
9501
				'freds_son_2' => 'Freddie'
9502
			),
9503
			'Bert' => array(
9504
				'berts_son_1' => 'Albert',
9505
				'berts_son_2' => 'Bertie')
9506
			),
9507
			array('showParents' => true, 'empty' => false)
9508
		);
9509
 
9510
		$expected = array(
9511
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
9512
				array('optgroup' => array('label' => 'Fred')),
9513
					array('option' => array('value' => 'freds_son_1')),
9514
						'Fred',
9515
					'/option',
9516
					array('option' => array('value' => 'freds_son_2')),
9517
						'Freddie',
9518
					'/option',
9519
				'/optgroup',
9520
				array('optgroup' => array('label' => 'Bert')),
9521
					array('option' => array('value' => 'berts_son_1')),
9522
						'Albert',
9523
					'/option',
9524
					array('option' => array('value' => 'berts_son_2')),
9525
						'Bertie',
9526
					'/option',
9527
				'/optgroup',
9528
			'/select'
9529
		);
9530
		$this->assertTags($result, $expected);
9531
 
9532
		/*
9533
		 * #2 This is structurally identical to the test above (#1) - only the parent name has
9534
		 * changed, so we should expect the same select list data, just with a different name
9535
		 * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
9536
		 * This is where data corruption can occur, because when a select value is missing from
9537
		 * a list a form will substitute the first value in the list - without the user knowing.
9538
		 * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
9539
		 * affect the availability of 3 => 'Three' as a valid option.
9540
		 */
9541
		$options = array(1 => 'One', 2 => 'Two', 'Three' => array(
9542
			3 => 'Three', 4 => 'Four', 5 => 'Five'
9543
		));
9544
		$result = $this->Form->select(
9545
			'Model.field', $options, array('showParents' => true, 'empty' => false)
9546
		);
9547
 
9548
		$expected = array(
9549
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
9550
				array('option' => array('value' => 1)),
9551
					'One',
9552
				'/option',
9553
				array('option' => array('value' => 2)),
9554
					'Two',
9555
				'/option',
9556
				array('optgroup' => array('label' => 'Three')),
9557
					array('option' => array('value' => 3)),
9558
						'Three',
9559
					'/option',
9560
					array('option' => array('value' => 4)),
9561
						'Four',
9562
					'/option',
9563
					array('option' => array('value' => 5)),
9564
						'Five',
9565
					'/option',
9566
				'/optgroup',
9567
			'/select'
9568
		);
9569
		$this->assertTags($result, $expected);
9570
	}
9571
 
9572
/**
9573
 * Test the generation of fields for a multi record form.
9574
 *
9575
 * @return void
9576
 */
9577
	public function testMultiRecordForm() {
9578
		$this->Form->create('ValidateProfile');
9579
		$this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
9580
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
9581
		$expected = array(
9582
			'div' => array('class' => 'input textarea'),
9583
				'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
9584
					'Name',
9585
				'/label',
9586
				'textarea' => array(
9587
					'id' => 'ValidateProfile1ValidateItem2Name',
9588
					'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
9589
					'cols' => 30,
9590
					'rows' => 6
9591
				),
9592
				'Value',
9593
				'/textarea',
9594
			'/div'
9595
		);
9596
		$this->assertTags($result, $expected);
9597
 
9598
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
9599
		$expected = array(
9600
			'div' => array('class' => 'input date'),
9601
			'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
9602
			'Created',
9603
			'/label',
9604
			array('select' => array(
9605
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
9606
				'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
9607
				)
9608
			),
9609
			array('option' => array('value' => '')), '/option',
9610
			$this->dateRegex['monthsRegex'],
9611
			'/select', '-',
9612
			array('select' => array(
9613
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
9614
				'id' => 'ValidateProfile1ValidateItem2CreatedDay'
9615
				)
9616
			),
9617
			array('option' => array('value' => '')), '/option',
9618
			$this->dateRegex['daysRegex'],
9619
			'/select', '-',
9620
			array('select' => array(
9621
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
9622
				'id' => 'ValidateProfile1ValidateItem2CreatedYear'
9623
				)
9624
			),
9625
			array('option' => array('value' => '')), '/option',
9626
			$this->dateRegex['yearsRegex'],
9627
			'/select',
9628
			'/div'
9629
		);
9630
		$this->assertTags($result, $expected);
9631
 
9632
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
9633
		$ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
9634
		$this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
9635
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
9636
		$expected = array(
9637
			'div' => array('class' => 'input select error'),
9638
			'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
9639
			'Profile',
9640
			'/label',
9641
			'select' => array(
9642
				'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
9643
				'id' => 'ValidateProfile1ValidateItem2ProfileId',
9644
				'class' => 'form-error'
9645
			),
9646
			'/select',
9647
			array('div' => array('class' => 'error-message')),
9648
			'Error',
9649
			'/div',
9650
			'/div'
9651
		);
9652
		$this->assertTags($result, $expected);
9653
	}
9654
 
9655
/**
9656
 * test the correct display of multi-record form validation errors.
9657
 *
9658
 * @return void
9659
 */
9660
	public function testMultiRecordFormValidationErrors() {
9661
		$this->Form->create('ValidateProfile');
9662
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
9663
		$ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
9664
		$result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
9665
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
9666
 
9667
		$ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
9668
		$result = $this->Form->error('ValidateProfile.2.city');
9669
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9670
 
9671
		$result = $this->Form->error('2.city');
9672
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9673
	}
9674
 
9675
/**
9676
 * test the correct display of multi-record form validation errors.
9677
 *
9678
 * @return void
9679
 */
9680
	public function testSaveManyRecordFormValidationErrors() {
9681
		$this->Form->create('ValidateUser');
9682
		$ValidateUser = ClassRegistry::getObject('ValidateUser');
9683
		$ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
9684
 
9685
		$result = $this->Form->error('0.ValidateUser.ValidateItem.name');
9686
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
9687
 
9688
		$ValidateUser->validationErrors[0]['city'] = array('Error in field city');
9689
		$result = $this->Form->error('ValidateUser.0.city');
9690
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9691
	}
9692
 
9693
/**
9694
 * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
9695
 *
9696
 * @return void
9697
 */
9698
	public function testInputTemplate() {
9699
		$result = $this->Form->input('Contact.email', array(
9700
			'type' => 'text', 'format' => array('input')
9701
		));
9702
		$expected = array(
9703
			'div' => array('class' => 'input text'),
9704
			'input' => array(
9705
				'type' => 'text', 'name' => 'data[Contact][email]',
9706
				'id' => 'ContactEmail'
9707
			),
9708
			'/div'
9709
		);
9710
		$this->assertTags($result, $expected);
9711
 
9712
		$result = $this->Form->input('Contact.email', array(
9713
			'type' => 'text', 'format' => array('input', 'label'),
9714
			'label' => '<em>Email (required)</em>'
9715
		));
9716
		$expected = array(
9717
			'div' => array('class' => 'input text'),
9718
			array('input' => array(
9719
				'type' => 'text', 'name' => 'data[Contact][email]',
9720
				'id' => 'ContactEmail'
9721
			)),
9722
			'label' => array('for' => 'ContactEmail'),
9723
			'em' => array(),
9724
			'Email (required)',
9725
			'/em',
9726
			'/label',
9727
			'/div'
9728
		);
9729
		$this->assertTags($result, $expected);
9730
 
9731
		$result = $this->Form->input('Contact.email', array(
9732
			'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
9733
			'between' => '<div>Something in the middle</div>',
9734
			'after' => '<span>Some text at the end</span>'
9735
		));
9736
		$expected = array(
9737
			'div' => array('class' => 'input text'),
9738
			array('input' => array(
9739
				'type' => 'text', 'name' => 'data[Contact][email]',
9740
				'id' => 'ContactEmail'
9741
			)),
9742
			array('div' => array()),
9743
			'Something in the middle',
9744
			'/div',
9745
			'label' => array('for' => 'ContactEmail'),
9746
			'Email',
9747
			'/label',
9748
			'span' => array(),
9749
			'Some text at the end',
9750
			'/span',
9751
			'/div'
9752
		);
9753
		$this->assertTags($result, $expected);
9754
 
9755
		$result = $this->Form->input('Contact.method', array(
9756
			'type' => 'radio',
9757
			'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
9758
			'between' => 'I am between',
9759
		));
9760
		$expected = array(
9761
			'div' => array('class' => 'input radio'),
9762
			'fieldset' => array(),
9763
			'legend' => array(),
9764
			'Method',
9765
			'/legend',
9766
			'I am between',
9767
			'input' => array(
9768
				'type' => 'hidden', 'name' => 'data[Contact][method]',
9769
				'value' => '', 'id' => 'ContactMethod_'
9770
			),
9771
			array('input' => array(
9772
				'type' => 'radio', 'name' => 'data[Contact][method]',
9773
				'value' => 'email', 'id' => 'ContactMethodEmail'
9774
			)),
9775
			array('label' => array('for' => 'ContactMethodEmail')),
9776
			'Email',
9777
			'/label',
9778
			array('input' => array(
9779
				'type' => 'radio', 'name' => 'data[Contact][method]',
9780
				'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
9781
			)),
9782
			array('label' => array('for' => 'ContactMethodPigeon')),
9783
			'Pigeon',
9784
			'/label',
9785
			'/fieldset',
9786
			'/div',
9787
		);
9788
		$this->assertTags($result, $expected);
9789
	}
9790
 
9791
/**
9792
 * test that some html5 inputs + FormHelper::__call() work
9793
 *
9794
 * @return void
9795
 */
9796
	public function testHtml5Inputs() {
9797
		$result = $this->Form->email('User.email');
9798
		$expected = array(
9799
			'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
9800
		);
9801
		$this->assertTags($result, $expected);
9802
 
9803
		$result = $this->Form->search('User.query');
9804
		$expected = array(
9805
			'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
9806
		);
9807
		$this->assertTags($result, $expected);
9808
 
9809
		$result = $this->Form->search('User.query', array('value' => 'test'));
9810
		$expected = array(
9811
			'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
9812
		);
9813
		$this->assertTags($result, $expected);
9814
 
9815
		$result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
9816
		$expected = array(
9817
			'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
9818
		);
9819
		$this->assertTags($result, $expected);
9820
 
9821
		$result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
9822
		$expected = array(
9823
			'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
9824
		);
9825
		$this->assertTags($result, $expected);
9826
	}
9827
 
9828
/**
9829
 * @expectedException CakeException
9830
 * @return void
9831
 */
9832
	public function testHtml5InputException() {
9833
		$this->Form->email();
9834
	}
9835
 
9836
/**
9837
 * Tests that a model can be loaded from the model names passed in the request object
9838
 *
9839
 * @return void
9840
 */
9841
	public function testIntrospectModelFromRequest() {
9842
		$this->loadFixtures('Post');
9843
		App::build(array(
9844
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
9845
		));
9846
		CakePlugin::load('TestPlugin');
9847
		$this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
9848
 
9849
		$this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
9850
		$this->Form->create('TestPluginPost');
9851
		$this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
9852
		$this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
9853
 
9854
		CakePlugin::unload();
9855
		App::build();
9856
	}
9857
 
9858
/**
9859
 * Tests that it is possible to set the validation errors directly in the helper for a field
9860
 *
9861
 * @return void
9862
 */
9863
	public function testCustomValidationErrors() {
9864
		$this->Form->validationErrors['Thing']['field'] = 'Badness!';
9865
		$result = $this->Form->error('Thing.field', null, array('wrap' => false));
9866
		$this->assertEquals('Badness!', $result);
9867
	}
9868
 
9869
/**
9870
 * Tests that the 'on' key validates as expected on create
9871
 *
9872
 * @return void
9873
 */
9874
	public function testRequiredOnCreate() {
9875
		$this->Form->create('Contact');
9876
 
9877
		$result = $this->Form->input('Contact.imrequiredonupdate');
9878
		$expected = array(
9879
			'div' => array('class' => 'input text'),
9880
			'label' => array('for' => 'ContactImrequiredonupdate'),
9881
			'Imrequiredonupdate',
9882
			'/label',
9883
			'input' => array(
9884
				'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
9885
				'id' => 'ContactImrequiredonupdate'
9886
			),
9887
			'/div'
9888
		);
9889
		$this->assertTags($result, $expected);
9890
 
9891
		$result = $this->Form->input('Contact.imrequiredoncreate');
9892
		$expected = array(
9893
			'div' => array('class' => 'input text required'),
9894
			'label' => array('for' => 'ContactImrequiredoncreate'),
9895
			'Imrequiredoncreate',
9896
			'/label',
9897
			'input' => array(
9898
				'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
9899
				'id' => 'ContactImrequiredoncreate',
9900
				'required' => 'required'
9901
			),
9902
			'/div'
9903
		);
9904
		$this->assertTags($result, $expected);
9905
 
9906
		$result = $this->Form->input('Contact.imrequiredonboth');
9907
		$expected = array(
9908
			'div' => array('class' => 'input text required'),
9909
			'label' => array('for' => 'ContactImrequiredonboth'),
9910
			'Imrequiredonboth',
9911
			'/label',
9912
			'input' => array(
9913
				'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
9914
				'id' => 'ContactImrequiredonboth',
9915
				'required' => 'required'
9916
			),
9917
			'/div'
9918
		);
9919
		$this->assertTags($result, $expected);
9920
 
9921
		$this->Form->inputDefaults(array('required' => false));
9922
		$result = $this->Form->input('Contact.imrequired');
9923
		$expected = array(
9924
			'div' => array('class' => 'input text'),
9925
			'label' => array('for' => 'ContactImrequired'),
9926
			'Imrequired',
9927
			'/label',
9928
			'input' => array(
9929
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
9930
				'id' => 'ContactImrequired'
9931
			),
9932
			'/div'
9933
		);
9934
		$this->assertTags($result, $expected);
9935
 
9936
		$result = $this->Form->input('Contact.imrequired', array('required' => false));
9937
		$this->assertTags($result, $expected);
9938
 
9939
		$result = $this->Form->input('Contact.imrequired', array('required' => true));
9940
		$expected = array(
9941
			'div' => array('class' => 'input text required'),
9942
			'label' => array('for' => 'ContactImrequired'),
9943
			'Imrequired',
9944
			'/label',
9945
			'input' => array(
9946
				'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
9947
				'id' => 'ContactImrequired'
9948
			),
9949
			'/div'
9950
		);
9951
		$this->assertTags($result, $expected);
9952
 
9953
		$result = $this->Form->input('Contact.imrequired', array('required' => null));
9954
		$this->assertTags($result, $expected);
9955
	}
9956
 
9957
/**
9958
 * Tests that the 'on' key validates as expected on update
9959
 *
9960
 * @return void
9961
 */
9962
	public function testRequiredOnUpdate() {
9963
		$this->Form->request->data['Contact']['id'] = 1;
9964
		$this->Form->create('Contact');
9965
 
9966
		$result = $this->Form->input('Contact.imrequiredonupdate');
9967
		$expected = array(
9968
			'div' => array('class' => 'input text required'),
9969
			'label' => array('for' => 'ContactImrequiredonupdate'),
9970
			'Imrequiredonupdate',
9971
			'/label',
9972
			'input' => array(
9973
				'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
9974
				'id' => 'ContactImrequiredonupdate',
9975
				'required' => 'required'
9976
			),
9977
			'/div'
9978
		);
9979
		$this->assertTags($result, $expected);
9980
		$result = $this->Form->input('Contact.imrequiredoncreate');
9981
		$expected = array(
9982
			'div' => array('class' => 'input text'),
9983
			'label' => array('for' => 'ContactImrequiredoncreate'),
9984
			'Imrequiredoncreate',
9985
			'/label',
9986
			'input' => array(
9987
				'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
9988
				'id' => 'ContactImrequiredoncreate'
9989
			),
9990
			'/div'
9991
		);
9992
		$this->assertTags($result, $expected);
9993
 
9994
		$result = $this->Form->input('Contact.imrequiredonboth');
9995
		$expected = array(
9996
			'div' => array('class' => 'input text required'),
9997
			'label' => array('for' => 'ContactImrequiredonboth'),
9998
			'Imrequiredonboth',
9999
			'/label',
10000
			'input' => array(
10001
				'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
10002
				'id' => 'ContactImrequiredonboth',
10003
				'required' => 'required'
10004
			),
10005
			'/div'
10006
		);
10007
		$this->assertTags($result, $expected);
10008
 
10009
		$result = $this->Form->input('Contact.imrequired');
10010
		$expected = array(
10011
			'div' => array('class' => 'input text required'),
10012
			'label' => array('for' => 'ContactImrequired'),
10013
			'Imrequired',
10014
			'/label',
10015
			'input' => array(
10016
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
10017
				'id' => 'ContactImrequired',
10018
				'required' => 'required'
10019
			),
10020
			'/div'
10021
		);
10022
		$this->assertTags($result, $expected);
10023
	}
10024
 
10025
/**
10026
 * Test inputDefaults setter and getter
10027
 *
10028
 * @return void
10029
 */
10030
	public function testInputDefaults() {
10031
		$this->Form->create('Contact');
10032
 
10033
		$this->Form->inputDefaults(array(
10034
			'label' => false,
10035
			'div' => array(
10036
				'style' => 'color: #000;'
10037
			)
10038
		));
10039
		$result = $this->Form->input('Contact.field1');
10040
		$expected = array(
10041
			'div' => array('class' => 'input text', 'style' => 'color: #000;'),
10042
			'input' => array(
10043
				'type' => 'text', 'name' => 'data[Contact][field1]',
10044
				'id' => 'ContactField1'
10045
			),
10046
			'/div'
10047
		);
10048
		$this->assertTags($result, $expected);
10049
 
10050
		$this->Form->inputDefaults(array(
10051
			'div' => false,
10052
			'label' => 'Label',
10053
		));
10054
		$result = $this->Form->input('Contact.field1');
10055
		$expected = array(
10056
			'label' => array('for' => 'ContactField1'),
10057
			'Label',
10058
			'/label',
10059
			'input' => array(
10060
				'type' => 'text', 'name' => 'data[Contact][field1]',
10061
				'id' => 'ContactField1'
10062
			),
10063
		);
10064
		$this->assertTags($result, $expected);
10065
 
10066
		$this->Form->inputDefaults(array(
10067
			'label' => false,
10068
		), true);
10069
		$result = $this->Form->input('Contact.field1');
10070
		$expected = array(
10071
			'input' => array(
10072
				'type' => 'text', 'name' => 'data[Contact][field1]',
10073
				'id' => 'ContactField1'
10074
			),
10075
		);
10076
		$this->assertTags($result, $expected);
10077
 
10078
		$result = $this->Form->inputDefaults();
10079
		$expected = array(
10080
			'div' => false,
10081
			'label' => false,
10082
		);
10083
		$this->assertEquals($expected, $result);
10084
	}
10085
 
10086
}