Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 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('notBlank' => 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' => 'notBlank',
90
		'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
91
		'imrequiredtoo' => array('rule' => 'notBlank'),
92
		'required_one' => array('required' => array('rule' => array('notBlank'))),
93
		'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
94
		'imalsonotrequired' => array(
95
			'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
96
			'between' => array('rule' => array('between', 5, 30)),
97
		),
98
		'imalsonotrequired2' => array(
99
			'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
100
			'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
101
		),
102
		'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
103
		'iamrequiredalways' => array(
104
			'email' => array('rule' => 'email'),
105
			'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
106
			'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
107
		),
108
		'boolean_field' => array('rule' => 'boolean')
109
	);
110
 
111
/**
112
 * schema method
113
 *
114
 * @return void
115
 */
116
	public function setSchema($schema) {
117
		$this->_schema = $schema;
118
	}
119
 
120
/**
121
 * hasAndBelongsToMany property
122
 *
123
 * @var array
124
 */
125
	public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
126
 
127
/**
128
 * hasAndBelongsToMany property
129
 *
130
 * @var array
131
 */
132
	public $belongsTo = array('User' => array('className' => 'UserForm'));
133
}
134
 
135
/**
136
 * ContactTagsContact class
137
 *
138
 * @package       Cake.Test.Case.View.Helper
139
 */
140
class ContactTagsContact extends CakeTestModel {
141
 
142
/**
143
 * useTable property
144
 *
145
 * @var bool
146
 */
147
	public $useTable = false;
148
 
149
/**
150
 * Default schema
151
 *
152
 * @var array
153
 */
154
	protected $_schema = array(
155
		'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
156
		'contact_tag_id' => array(
157
			'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
158
		)
159
	);
160
 
161
/**
162
 * schema method
163
 *
164
 * @return void
165
 */
166
	public function setSchema($schema) {
167
		$this->_schema = $schema;
168
	}
169
 
170
}
171
 
172
/**
173
 * ContactNonStandardPk class
174
 *
175
 * @package       Cake.Test.Case.View.Helper
176
 */
177
class ContactNonStandardPk extends Contact {
178
 
179
/**
180
 * primaryKey property
181
 *
182
 * @var string
183
 */
184
	public $primaryKey = 'pk';
185
 
186
/**
187
 * schema method
188
 *
189
 * @return void
190
 */
191
	public function schema($field = false) {
192
		$this->_schema = parent::schema();
193
		$this->_schema['pk'] = $this->_schema['id'];
194
		unset($this->_schema['id']);
195
		return $this->_schema;
196
	}
197
 
198
}
199
 
200
/**
201
 * ContactTag class
202
 *
203
 * @package       Cake.Test.Case.View.Helper
204
 */
205
class ContactTag extends Model {
206
 
207
/**
208
 * useTable property
209
 *
210
 * @var bool
211
 */
212
	public $useTable = false;
213
 
214
/**
215
 * schema definition
216
 *
217
 * @var array
218
 */
219
	protected $_schema = array(
220
		'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
221
		'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
222
		'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
223
		'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
224
	);
225
}
226
 
227
/**
228
 * UserForm class
229
 *
230
 * @package       Cake.Test.Case.View.Helper
231
 */
232
class UserForm extends CakeTestModel {
233
 
234
/**
235
 * useTable property
236
 *
237
 * @var bool
238
 */
239
	public $useTable = false;
240
 
241
/**
242
 * hasMany property
243
 *
244
 * @var array
245
 */
246
	public $hasMany = array(
247
		'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
248
	));
249
 
250
/**
251
 * schema definition
252
 *
253
 * @var array
254
 */
255
	protected $_schema = array(
256
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
257
		'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
258
		'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
259
		'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
260
		'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
261
		'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
262
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
263
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
264
	);
265
}
266
 
267
/**
268
 * OpenidUrl class
269
 *
270
 * @package       Cake.Test.Case.View.Helper
271
 */
272
class OpenidUrl extends CakeTestModel {
273
 
274
/**
275
 * useTable property
276
 *
277
 * @var bool
278
 */
279
	public $useTable = false;
280
 
281
/**
282
 * belongsTo property
283
 *
284
 * @var array
285
 */
286
	public $belongsTo = array('UserForm' => array(
287
		'className' => 'UserForm', 'foreignKey' => 'user_form_id'
288
	));
289
 
290
/**
291
 * validate property
292
 *
293
 * @var array
294
 */
295
	public $validate = array('openid_not_registered' => array());
296
 
297
/**
298
 * schema method
299
 *
300
 * @var array
301
 */
302
	protected $_schema = array(
303
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
304
		'user_form_id' => array(
305
			'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
306
		),
307
		'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
308
	);
309
 
310
/**
311
 * beforeValidate method
312
 *
313
 * @return void
314
 */
315
	public function beforeValidate($options = array()) {
316
		$this->invalidate('openid_not_registered');
317
		return true;
318
	}
319
 
320
}
321
 
322
/**
323
 * ValidateUser class
324
 *
325
 * @package       Cake.Test.Case.View.Helper
326
 */
327
class ValidateUser extends CakeTestModel {
328
 
329
/**
330
 * useTable property
331
 *
332
 * @var bool
333
 */
334
	public $useTable = false;
335
 
336
/**
337
 * hasOne property
338
 *
339
 * @var array
340
 */
341
	public $hasOne = array('ValidateProfile' => array(
342
		'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
343
	));
344
 
345
/**
346
 * schema method
347
 *
348
 * @var array
349
 */
350
	protected $_schema = array(
351
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
352
		'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
353
		'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
354
		'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
355
		'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
356
		'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
357
		'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
358
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
359
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
360
	);
361
 
362
/**
363
 * beforeValidate method
364
 *
365
 * @return void
366
 */
367
	public function beforeValidate($options = array()) {
368
		$this->invalidate('email');
369
		return false;
370
	}
371
 
372
}
373
 
374
/**
375
 * ValidateProfile class
376
 *
377
 * @package       Cake.Test.Case.View.Helper
378
 */
379
class ValidateProfile extends CakeTestModel {
380
 
381
/**
382
 * useTable property
383
 *
384
 * @var bool
385
 */
386
	public $useTable = false;
387
 
388
/**
389
 * schema property
390
 *
391
 * @var array
392
 */
393
	protected $_schema = array(
394
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
395
		'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
396
		'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
397
		'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
398
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
399
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
400
	);
401
 
402
/**
403
 * hasOne property
404
 *
405
 * @var array
406
 */
407
	public $hasOne = array('ValidateItem' => array(
408
		'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
409
	));
410
 
411
/**
412
 * belongsTo property
413
 *
414
 * @var array
415
 */
416
	public $belongsTo = array('ValidateUser' => array(
417
		'className' => 'ValidateUser', 'foreignKey' => 'user_id'
418
	));
419
 
420
/**
421
 * beforeValidate method
422
 *
423
 * @return void
424
 */
425
	public function beforeValidate($options = array()) {
426
		$this->invalidate('full_name');
427
		$this->invalidate('city');
428
		return false;
429
	}
430
 
431
}
432
 
433
/**
434
 * ValidateItem class
435
 *
436
 * @package       Cake.Test.Case.View.Helper
437
 */
438
class ValidateItem extends CakeTestModel {
439
 
440
/**
441
 * useTable property
442
 *
443
 * @var bool
444
 */
445
	public $useTable = false;
446
 
447
/**
448
 * schema property
449
 *
450
 * @var array
451
 */
452
	protected $_schema = array(
453
		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
454
		'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
455
		'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
456
		'description' => array(
457
			'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
458
		),
459
		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
460
		'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
461
	);
462
 
463
/**
464
 * belongsTo property
465
 *
466
 * @var array
467
 */
468
	public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
469
 
470
/**
471
 * beforeValidate method
472
 *
473
 * @return void
474
 */
475
	public function beforeValidate($options = array()) {
476
		$this->invalidate('description');
477
		return false;
478
	}
479
 
480
}
481
 
482
/**
483
 * TestMail class
484
 *
485
 * @package       Cake.Test.Case.View.Helper
486
 */
487
class TestMail extends CakeTestModel {
488
 
489
/**
490
 * useTable property
491
 *
492
 * @var bool
493
 */
494
	public $useTable = false;
495
 
496
}
497
 
498
/**
499
 * FormHelperTest class
500
 *
501
 * @package       Cake.Test.Case.View.Helper
502
 * @property FormHelper $Form
503
 */
504
class FormHelperTest extends CakeTestCase {
505
 
506
/**
507
 * Fixtures to be used
508
 *
509
 * @var array
510
 */
511
	public $fixtures = array('core.post');
512
 
513
/**
514
 * Do not load the fixtures by default
515
 *
516
 * @var bool
517
 */
518
	public $autoFixtures = false;
519
 
520
/**
521
 * setUp method
522
 *
523
 * @return void
524
 */
525
	public function setUp() {
526
		parent::setUp();
527
 
528
		Configure::write('Config.language', 'eng');
529
		Configure::write('App.base', '');
530
		Configure::delete('Asset');
531
		$this->Controller = new ContactTestController();
532
		$this->View = new View($this->Controller);
533
 
534
		$this->Form = new FormHelper($this->View);
535
		$this->Form->Html = new HtmlHelper($this->View);
536
		$this->Form->request = new CakeRequest('contacts/add', false);
537
		$this->Form->request->here = '/contacts/add';
538
		$this->Form->request['action'] = 'add';
539
		$this->Form->request->webroot = '';
540
		$this->Form->request->base = '';
541
 
542
		ClassRegistry::addObject('Contact', new Contact());
543
		ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
544
		ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
545
		ClassRegistry::addObject('User', new UserForm());
546
		ClassRegistry::addObject('ValidateItem', new ValidateItem());
547
		ClassRegistry::addObject('ValidateUser', new ValidateUser());
548
		ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
549
 
550
		$this->oldSalt = Configure::read('Security.salt');
551
 
552
		$this->dateRegex = array(
553
			'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
554
			'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
555
			'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
556
			'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
557
			'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
558
			'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
559
		);
560
 
561
		Configure::write('Security.salt', 'foo!');
562
	}
563
 
564
/**
565
 * tearDown method
566
 *
567
 * @return void
568
 */
569
	public function tearDown() {
570
		parent::tearDown();
571
		unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
572
		Configure::write('Security.salt', $this->oldSalt);
573
	}
574
 
575
/**
576
 * testFormCreateWithSecurity method
577
 *
578
 * Test form->create() with security key.
579
 *
580
 * @return void
581
 */
582
	public function testCreateWithSecurity() {
583
		$this->Form->request['_Token'] = array('key' => 'testKey');
584
		$encoding = strtolower(Configure::read('App.encoding'));
585
		$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
586
		$expected = array(
587
			'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
588
			'div' => array('style' => 'display:none;'),
589
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
590
			array('input' => array(
591
				'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
592
			)),
593
			'/div'
594
		);
595
		$this->assertTags($result, $expected);
596
 
597
		$result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
598
		$expected['form']['id'] = 'MyForm';
599
		$this->assertTags($result, $expected);
600
	}
601
 
602
/**
603
 * testFormCreateGetNoSecurity method
604
 *
605
 * Test form->create() with no security key as its a get form
606
 *
607
 * @return void
608
 */
609
	public function testCreateEndGetNoSecurity() {
610
		$this->Form->request['_Token'] = array('key' => 'testKey');
611
		$encoding = strtolower(Configure::read('App.encoding'));
612
		$result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
613
		$this->assertNotContains('Token', $result);
614
 
615
		$result = $this->Form->end('Save');
616
		$this->assertNotContains('Token', $result);
617
	}
618
 
619
/**
620
 * test that create() clears the fields property so it starts fresh
621
 *
622
 * @return void
623
 */
624
	public function testCreateClearingFields() {
625
		$this->Form->fields = array('model_id');
626
		$this->Form->create('Contact');
627
		$this->assertEquals(array(), $this->Form->fields);
628
	}
629
 
630
/**
631
 * Tests form hash generation with model-less data
632
 *
633
 * @return void
634
 */
635
	public function testValidateHashNoModel() {
636
		$this->Form->request['_Token'] = array('key' => 'foo');
637
		$result = $this->Form->secure(array('anything'));
638
		$this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
639
	}
640
 
641
/**
642
 * Tests that models with identical field names get resolved properly
643
 *
644
 * @return void
645
 */
646
	public function testDuplicateFieldNameResolution() {
647
		$result = $this->Form->create('ValidateUser');
648
		$this->assertEquals(array('ValidateUser'), $this->Form->entity());
649
 
650
		$result = $this->Form->input('ValidateItem.name');
651
		$this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
652
 
653
		$result = $this->Form->input('ValidateUser.name');
654
		$this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
655
		$this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
656
		$this->assertRegExp('/type="text"/', $result);
657
 
658
		$result = $this->Form->input('ValidateItem.name');
659
		$this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
660
		$this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
661
		$this->assertRegExp('/<textarea/', $result);
662
 
663
		$result = $this->Form->input('name');
664
		$this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
665
		$this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
666
		$this->assertRegExp('/type="text"/', $result);
667
	}
668
 
669
/**
670
 * Tests that hidden fields generated for checkboxes don't get locked
671
 *
672
 * @return void
673
 */
674
	public function testNoCheckboxLocking() {
675
		$this->Form->request['_Token'] = array('key' => 'foo');
676
		$this->assertSame(array(), $this->Form->fields);
677
 
678
		$this->Form->checkbox('check', array('value' => '1'));
679
		$this->assertSame($this->Form->fields, array('check'));
680
	}
681
 
682
/**
683
 * testFormSecurityFields method
684
 *
685
 * Test generation of secure form hash generation.
686
 *
687
 * @return void
688
 */
689
	public function testFormSecurityFields() {
690
		$key = 'testKey';
691
		$fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
692
		$secureAttributes = array('form' => 'MyTestForm');
693
 
694
		$this->Form->request['_Token'] = array('key' => $key);
695
		$result = $this->Form->secure($fields, $secureAttributes);
696
 
697
		$hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
698
		$hash .= ':' . 'Model.valid';
699
		$hash = urlencode($hash);
700
 
701
		$expected = array(
702
			'div' => array('style' => 'display:none;'),
703
			array('input' => array(
704
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
705
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/',
706
				'form' => 'MyTestForm',
707
			)),
708
			array('input' => array(
709
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
710
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/',
711
				'form' => 'MyTestForm',
712
			)),
713
			'/div'
714
		);
715
		$this->assertTags($result, $expected);
716
	}
717
 
718
/**
719
 * Tests correct generation of number fields for double and float fields
720
 *
721
 * @return void
722
 */
723
	public function testTextFieldGenerationForFloats() {
724
		$model = ClassRegistry::getObject('Contact');
725
		$model->setSchema(array('foo' => array(
726
			'type' => 'float',
727
			'null' => false,
728
			'default' => null,
729
			'length' => 10
730
		)));
731
 
732
		$this->Form->create('Contact');
733
		$result = $this->Form->input('foo');
734
		$expected = array(
735
			'div' => array('class' => 'input number'),
736
			'label' => array('for' => 'ContactFoo'),
737
			'Foo',
738
			'/label',
739
			array('input' => array(
740
				'type' => 'number',
741
				'name' => 'data[Contact][foo]',
742
				'id' => 'ContactFoo',
743
				'step' => 'any'
744
			)),
745
			'/div'
746
		);
747
		$this->assertTags($result, $expected);
748
 
749
		$result = $this->Form->input('foo', array('step' => 0.5));
750
		$expected = array(
751
			'div' => array('class' => 'input number'),
752
			'label' => array('for' => 'ContactFoo'),
753
			'Foo',
754
			'/label',
755
			array('input' => array(
756
				'type' => 'number',
757
				'name' => 'data[Contact][foo]',
758
				'id' => 'ContactFoo',
759
				'step' => '0.5'
760
			)),
761
			'/div'
762
		);
763
		$this->assertTags($result, $expected);
764
	}
765
 
766
/**
767
 * Tests correct generation of decimal fields as text inputs
768
 *
769
 * @return void
770
 */
771
	public function testTextFieldGenerationForDecimalAsText() {
772
		$this->Form->create('ValidateUser');
773
		$result = $this->Form->input('cost_decimal', array(
774
			'type' => 'text'
775
		));
776
		$expected = array(
777
			'div' => array('class' => 'input text'),
778
			'label' => array('for' => 'ValidateUserCostDecimal'),
779
			'Cost Decimal',
780
			'/label',
781
			array('input' => array(
782
				'type' => 'text',
783
				'name' => 'data[ValidateUser][cost_decimal]',
784
				'id' => 'ValidateUserCostDecimal',
785
			)),
786
			'/div'
787
		);
788
		$this->assertTags($result, $expected);
789
	}
790
 
791
/**
792
 * Tests correct generation of number fields for integer fields
793
 *
794
 * @return void
795
 */
796
	public function testTextFieldTypeNumberGenerationForIntegers() {
797
		$model = ClassRegistry::getObject('Contact');
798
		$model->setSchema(array('foo' => array(
799
			'type' => 'integer',
800
			'null' => false,
801
			'default' => null,
802
			'length' => null
803
		)));
804
 
805
		$this->Form->create('Contact');
806
		$result = $this->Form->input('foo');
807
		$expected = array(
808
			'div' => array('class' => 'input number'),
809
			'label' => array('for' => 'ContactFoo'),
810
			'Foo',
811
			'/label',
812
			array('input' => array(
813
				'type' => 'number', 'name' => 'data[Contact][foo]',
814
				'id' => 'ContactFoo'
815
			)),
816
			'/div'
817
		);
818
		$this->assertTags($result, $expected);
819
	}
820
 
821
/**
822
 * Tests correct generation of file upload fields for binary fields
823
 *
824
 * @return void
825
 */
826
	public function testFileUploadFieldTypeGenerationForBinaries() {
827
		$model = ClassRegistry::getObject('Contact');
828
		$model->setSchema(array('foo' => array(
829
			'type' => 'binary',
830
			'null' => false,
831
			'default' => null,
832
			'length' => 1024
833
		)));
834
 
835
		$this->Form->create('Contact');
836
		$result = $this->Form->input('foo');
837
		$expected = array(
838
			'div' => array('class' => 'input file'),
839
			'label' => array('for' => 'ContactFoo'),
840
			'Foo',
841
			'/label',
842
			array('input' => array(
843
				'type' => 'file', 'name' => 'data[Contact][foo]',
844
				'id' => 'ContactFoo'
845
			)),
846
			'/div'
847
		);
848
		$this->assertTags($result, $expected);
849
	}
850
 
851
/**
852
 * testFormSecurityMultipleFields method
853
 *
854
 * Test secure() with multiple row form. Ensure hash is correct.
855
 *
856
 * @return void
857
 */
858
	public function testFormSecurityMultipleFields() {
859
		$key = 'testKey';
860
 
861
		$fields = array(
862
			'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
863
			'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
864
			'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
865
		);
866
		$this->Form->request['_Token'] = array('key' => $key);
867
		$result = $this->Form->secure($fields);
868
 
869
		$hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
870
		$hash .= '%7CModel.1.hidden%7CModel.1.valid';
871
 
872
		$expected = array(
873
			'div' => array('style' => 'display:none;'),
874
			array('input' => array(
875
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
876
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
877
			)),
878
			array('input' => array(
879
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
880
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
881
			)),
882
			'/div'
883
		);
884
		$this->assertTags($result, $expected);
885
	}
886
 
887
/**
888
 * testFormSecurityMultipleSubmitButtons
889
 *
890
 * test form submit generation and ensure that _Token is only created on end()
891
 *
892
 * @return void
893
 */
894
	public function testFormSecurityMultipleSubmitButtons() {
895
		$key = 'testKey';
896
		$this->Form->request['_Token'] = array('key' => $key);
897
 
898
		$this->Form->create('Addresses');
899
		$this->Form->input('Address.title');
900
		$this->Form->input('Address.first_name');
901
 
902
		$result = $this->Form->submit('Save', array('name' => 'save'));
903
		$expected = array(
904
			'div' => array('class' => 'submit'),
905
			'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
906
			'/div',
907
		);
908
		$this->assertTags($result, $expected);
909
 
910
		$result = $this->Form->submit('Cancel', array('name' => 'cancel'));
911
		$expected = array(
912
			'div' => array('class' => 'submit'),
913
			'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
914
			'/div',
915
		);
916
		$this->assertTags($result, $expected);
917
		$result = $this->Form->end(null);
918
 
919
		$expected = array(
920
			'div' => array('style' => 'display:none;'),
921
			array('input' => array(
922
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
923
				'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
924
			)),
925
			array('input' => array(
926
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
927
				'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
928
			)),
929
			'/div'
930
		);
931
		$this->assertTags($result, $expected);
932
	}
933
 
934
/**
935
 * Test that buttons created with foo[bar] name attributes are unlocked correctly.
936
 *
937
 * @return void
938
 */
939
	public function testSecurityButtonNestedNamed() {
940
		$key = 'testKey';
941
		$this->Form->request['_Token'] = array('key' => $key);
942
 
943
		$this->Form->create('Addresses');
944
		$this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
945
		$result = $this->Form->unlockField();
946
		$this->assertEquals(array('Address.button'), $result);
947
	}
948
 
949
/**
950
 * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
951
 *
952
 * @return void
953
 */
954
	public function testSecuritySubmitNestedNamed() {
955
		$key = 'testKey';
956
		$this->Form->request['_Token'] = array('key' => $key);
957
 
958
		$this->Form->create('Addresses');
959
		$this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
960
		$result = $this->Form->unlockField();
961
		$this->assertEquals(array('Address.button'), $result);
962
	}
963
 
964
/**
965
 * Test that the correct fields are unlocked for image submits with no names.
966
 *
967
 * @return void
968
 */
969
	public function testSecuritySubmitImageNoName() {
970
		$key = 'testKey';
971
		$this->Form->request['_Token'] = array('key' => $key);
972
 
973
		$this->Form->create('User');
974
		$result = $this->Form->submit('save.png');
975
		$expected = array(
976
			'div' => array('class' => 'submit'),
977
			'input' => array('type' => 'image', 'src' => 'img/save.png'),
978
			'/div'
979
		);
980
		$this->assertTags($result, $expected);
981
		$this->assertEquals(array('x', 'y'), $this->Form->unlockField());
982
	}
983
 
984
/**
985
 * Test that the correct fields are unlocked for image submits with names.
986
 *
987
 * @return void
988
 */
989
	public function testSecuritySubmitImageName() {
990
		$key = 'testKey';
991
		$this->Form->request['_Token'] = array('key' => $key);
992
 
993
		$this->Form->create('User');
994
		$result = $this->Form->submit('save.png', array('name' => 'test'));
995
		$expected = array(
996
			'div' => array('class' => 'submit'),
997
			'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
998
			'/div'
999
		);
1000
		$this->assertTags($result, $expected);
1001
		$this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
1002
	}
1003
 
1004
/**
1005
 * testFormSecurityMultipleInputFields method
1006
 *
1007
 * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
1008
 *
1009
 * @return void
1010
 */
1011
	public function testFormSecurityMultipleInputFields() {
1012
		$key = 'testKey';
1013
 
1014
		$this->Form->request['_Token'] = array('key' => $key);
1015
		$this->Form->create('Addresses');
1016
 
1017
		$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
1018
		$this->Form->input('Addresses.0.title');
1019
		$this->Form->input('Addresses.0.first_name');
1020
		$this->Form->input('Addresses.0.last_name');
1021
		$this->Form->input('Addresses.0.address');
1022
		$this->Form->input('Addresses.0.city');
1023
		$this->Form->input('Addresses.0.phone');
1024
		$this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
1025
 
1026
		$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1027
		$this->Form->input('Addresses.1.title');
1028
		$this->Form->input('Addresses.1.first_name');
1029
		$this->Form->input('Addresses.1.last_name');
1030
		$this->Form->input('Addresses.1.address');
1031
		$this->Form->input('Addresses.1.city');
1032
		$this->Form->input('Addresses.1.phone');
1033
		$this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
1034
 
1035
		$result = $this->Form->secure($this->Form->fields);
1036
 
1037
		$hash = 'a3b9b2ba1cb688838f92818a5970e17dd7943a78%3AAddresses.0.id%7CAddresses.1.id';
1038
 
1039
		$expected = array(
1040
			'div' => array('style' => 'display:none;'),
1041
			array('input' => array(
1042
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1043
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1044
			)),
1045
			array('input' => array(
1046
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1047
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
1048
			)),
1049
			'/div'
1050
		);
1051
		$this->assertTags($result, $expected);
1052
	}
1053
 
1054
/**
1055
 * Test form security with Model.field.0 style inputs
1056
 *
1057
 * @return void
1058
 */
1059
	public function testFormSecurityArrayFields() {
1060
		$key = 'testKey';
1061
 
1062
		$this->Form->request->params['_Token']['key'] = $key;
1063
		$this->Form->create('Address');
1064
		$this->Form->input('Address.primary.1');
1065
		$this->assertEquals('Address.primary', $this->Form->fields[0]);
1066
 
1067
		$this->Form->input('Address.secondary.1.0');
1068
		$this->assertEquals('Address.secondary', $this->Form->fields[1]);
1069
	}
1070
 
1071
/**
1072
 * testFormSecurityMultipleInputDisabledFields method
1073
 *
1074
 * test secure form generation with multiple records and disabled fields.
1075
 *
1076
 * @return void
1077
 */
1078
	public function testFormSecurityMultipleInputDisabledFields() {
1079
		$key = 'testKey';
1080
		$this->Form->request->params['_Token'] = array(
1081
			'key' => $key,
1082
			'unlockedFields' => array('first_name', 'address')
1083
		);
1084
		$this->Form->create();
1085
 
1086
		$this->Form->hidden('Addresses.0.id', array('value' => '123456'));
1087
		$this->Form->input('Addresses.0.title');
1088
		$this->Form->input('Addresses.0.first_name');
1089
		$this->Form->input('Addresses.0.last_name');
1090
		$this->Form->input('Addresses.0.address');
1091
		$this->Form->input('Addresses.0.city');
1092
		$this->Form->input('Addresses.0.phone');
1093
		$this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1094
		$this->Form->input('Addresses.1.title');
1095
		$this->Form->input('Addresses.1.first_name');
1096
		$this->Form->input('Addresses.1.last_name');
1097
		$this->Form->input('Addresses.1.address');
1098
		$this->Form->input('Addresses.1.city');
1099
		$this->Form->input('Addresses.1.phone');
1100
 
1101
		$result = $this->Form->secure($this->Form->fields);
1102
		$hash = '5c9cadf9da008cc444d3960b481391a425a5d979%3AAddresses.0.id%7CAddresses.1.id';
1103
 
1104
		$expected = array(
1105
			'div' => array('style' => 'display:none;'),
1106
			array('input' => array(
1107
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1108
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1109
			)),
1110
			array('input' => array(
1111
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1112
				'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
1113
			)),
1114
			'/div'
1115
		);
1116
		$this->assertTags($result, $expected);
1117
	}
1118
 
1119
/**
1120
 * testFormSecurityInputDisabledFields method
1121
 *
1122
 * Test single record form with disabled fields.
1123
 *
1124
 * @return void
1125
 */
1126
	public function testFormSecurityInputUnlockedFields() {
1127
		$key = 'testKey';
1128
		$this->Form->request['_Token'] = array(
1129
			'key' => $key,
1130
			'unlockedFields' => array('first_name', 'address')
1131
		);
1132
		$this->Form->create();
1133
		$this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
1134
 
1135
		$this->Form->hidden('Addresses.id', array('value' => '123456'));
1136
		$this->Form->input('Addresses.title');
1137
		$this->Form->input('Addresses.first_name');
1138
		$this->Form->input('Addresses.last_name');
1139
		$this->Form->input('Addresses.address');
1140
		$this->Form->input('Addresses.city');
1141
		$this->Form->input('Addresses.phone');
1142
 
1143
		$result = $this->Form->fields;
1144
		$expected = array(
1145
			'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
1146
			'Addresses.city', 'Addresses.phone'
1147
		);
1148
		$this->assertEquals($expected, $result);
1149
 
1150
		$result = $this->Form->secure($expected);
1151
 
1152
		$hash = '40289bd07811587887ff56585a8526ff9da59d7a%3AAddresses.id';
1153
		$expected = array(
1154
			'div' => array('style' => 'display:none;'),
1155
			array('input' => array(
1156
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1157
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1158
			)),
1159
			array('input' => array(
1160
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1161
				'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
1162
			)),
1163
			'/div'
1164
		);
1165
		$this->assertTags($result, $expected);
1166
	}
1167
 
1168
/**
1169
 * test securing inputs with custom name attributes.
1170
 *
1171
 * @return void
1172
 */
1173
	public function testFormSecureWithCustomNameAttribute() {
1174
		$this->Form->request->params['_Token']['key'] = 'testKey';
1175
 
1176
		$this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
1177
		$this->assertEquals('User.custom', $this->Form->fields[0]);
1178
 
1179
		$this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
1180
		$this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
1181
	}
1182
 
1183
/**
1184
 * testFormSecuredInput method
1185
 *
1186
 * Test generation of entire secure form, assertions made on input() output.
1187
 *
1188
 * @return void
1189
 */
1190
	public function testFormSecuredInput() {
1191
		$this->Form->request['_Token'] = array('key' => 'testKey');
1192
 
1193
		$result = $this->Form->create('Contact', array('url' => '/contacts/add'));
1194
		$encoding = strtolower(Configure::read('App.encoding'));
1195
		$expected = array(
1196
			'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
1197
			'div' => array('style' => 'display:none;'),
1198
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
1199
			array('input' => array(
1200
				'type' => 'hidden', 'name' => 'data[_Token][key]',
1201
				'value' => 'testKey', 'id' => 'preg:/Token\d+/'
1202
			)),
1203
			'/div'
1204
		);
1205
		$this->assertTags($result, $expected);
1206
 
1207
		$result = $this->Form->input('UserForm.published', array('type' => 'text'));
1208
		$expected = array(
1209
			'div' => array('class' => 'input text'),
1210
			'label' => array('for' => 'UserFormPublished'),
1211
			'Published',
1212
			'/label',
1213
			array('input' => array(
1214
				'type' => 'text', 'name' => 'data[UserForm][published]',
1215
				'id' => 'UserFormPublished'
1216
			)),
1217
			'/div'
1218
		);
1219
		$this->assertTags($result, $expected);
1220
 
1221
		$result = $this->Form->input('UserForm.other', array('type' => 'text'));
1222
		$expected = array(
1223
			'div' => array('class' => 'input text'),
1224
			'label' => array('for' => 'UserFormOther'),
1225
			'Other',
1226
			'/label',
1227
			array('input' => array(
1228
				'type' => 'text', 'name' => 'data[UserForm][other]',
1229
				'id' => 'UserFormOther'
1230
			)),
1231
			'/div'
1232
		);
1233
		$this->assertTags($result, $expected);
1234
 
1235
		$result = $this->Form->hidden('UserForm.stuff');
1236
		$expected = array(
1237
			'input' => array(
1238
				'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
1239
				'id' => 'UserFormStuff'
1240
		));
1241
		$this->assertTags($result, $expected);
1242
 
1243
		$result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
1244
		$expected = array('input' => array(
1245
			'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
1246
			'value' => '0', 'id' => 'UserFormHidden'
1247
		));
1248
		$this->assertTags($result, $expected);
1249
 
1250
		$result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
1251
		$expected = array(
1252
			'div' => array('class' => 'input checkbox'),
1253
			array('input' => array(
1254
				'type' => 'hidden', 'name' => 'data[UserForm][something]',
1255
				'value' => '0', 'id' => 'UserFormSomething_'
1256
			)),
1257
			array('input' => array(
1258
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
1259
				'value' => '1', 'id' => 'UserFormSomething'
1260
			)),
1261
			'label' => array('for' => 'UserFormSomething'),
1262
			'Something',
1263
			'/label',
1264
			'/div'
1265
		);
1266
		$this->assertTags($result, $expected);
1267
 
1268
		$result = $this->Form->fields;
1269
		$expected = array(
1270
			'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
1271
			'UserForm.hidden' => '0', 'UserForm.something'
1272
		);
1273
		$this->assertEquals($expected, $result);
1274
 
1275
		$hash = '6014b4e1c4f39eb62389712111dbe6435bec66cb%3AUserForm.hidden%7CUserForm.stuff';
1276
 
1277
		$result = $this->Form->secure($this->Form->fields);
1278
		$expected = array(
1279
			'div' => array('style' => 'display:none;'),
1280
			array('input' => array(
1281
				'type' => 'hidden', 'name' => 'data[_Token][fields]',
1282
				'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1283
			)),
1284
			array('input' => array(
1285
				'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
1286
				'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
1287
			)),
1288
			'/div'
1289
		);
1290
		$this->assertTags($result, $expected);
1291
	}
1292
 
1293
/**
1294
 * Test that a hidden field followed by a visible field
1295
 * undoes the hidden field locking.
1296
 *
1297
 * @return void
1298
 */
1299
	public function testSecuredInputDuplicate() {
1300
		$this->Form->request['_Token'] = array('key' => 'testKey');
1301
		$this->assertEquals(array(), $this->Form->fields);
1302
 
1303
		$this->Form->input('text_val', array(
1304
			'type' => 'hidden',
1305
			'value' => 'some text',
1306
		));
1307
		$expected = array('text_val' => 'some text');
1308
		$this->assertEquals($expected, $this->Form->fields);
1309
 
1310
		$this->Form->input('text_val', array(
1311
			'type' => 'text',
1312
		));
1313
		$expected = array('text_val');
1314
		$this->assertEquals($expected, $this->Form->fields);
1315
	}
1316
 
1317
/**
1318
 * Test secured inputs with custom names.
1319
 *
1320
 * @return void
1321
 */
1322
	public function testSecuredInputCustomName() {
1323
		$this->Form->request['_Token'] = array('key' => 'testKey');
1324
		$this->assertEquals(array(), $this->Form->fields);
1325
 
1326
		$this->Form->input('text_input', array(
1327
			'name' => 'data[Option][General.default_role]',
1328
		));
1329
		$expected = array('Option.General.default_role');
1330
		$this->assertEquals($expected, $this->Form->fields);
1331
 
1332
		$this->Form->input('select_box', array(
1333
			'name' => 'data[Option][General.select_role]',
1334
			'type' => 'select',
1335
			'options' => array(1, 2),
1336
		));
1337
		$expected = array('Option.General.default_role', 'Option.General.select_role');
1338
		$this->assertEquals($expected, $this->Form->fields);
1339
	}
1340
 
1341
/**
1342
 * Tests that the correct keys are added to the field hash index
1343
 *
1344
 * @return void
1345
 */
1346
	public function testSecuredFileInput() {
1347
		$this->Form->request['_Token'] = array('key' => 'testKey');
1348
		$this->assertEquals(array(), $this->Form->fields);
1349
 
1350
		$this->Form->file('Attachment.file');
1351
		$expected = array(
1352
			'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
1353
			'Attachment.file.error', 'Attachment.file.size'
1354
		);
1355
		$this->assertEquals($expected, $this->Form->fields);
1356
	}
1357
 
1358
/**
1359
 * test that multiple selects keys are added to field hash
1360
 *
1361
 * @return void
1362
 */
1363
	public function testSecuredMultipleSelect() {
1364
		$this->Form->request['_Token'] = array('key' => 'testKey');
1365
		$this->assertEquals(array(), $this->Form->fields);
1366
		$options = array('1' => 'one', '2' => 'two');
1367
 
1368
		$this->Form->select('Model.select', $options);
1369
		$expected = array('Model.select');
1370
		$this->assertEquals($expected, $this->Form->fields);
1371
 
1372
		$this->Form->fields = array();
1373
		$this->Form->select('Model.select', $options, array('multiple' => true));
1374
		$this->assertEquals($expected, $this->Form->fields);
1375
	}
1376
 
1377
/**
1378
 * testFormSecuredRadio method
1379
 *
1380
 * @return void
1381
 */
1382
	public function testSecuredRadio() {
1383
		$this->Form->request['_Token'] = array('key' => 'testKey');
1384
		$this->assertEquals(array(), $this->Form->fields);
1385
		$options = array('1' => 'option1', '2' => 'option2');
1386
 
1387
		$this->Form->radio('Test.test', $options);
1388
		$expected = array('Test.test');
1389
		$this->assertEquals($expected, $this->Form->fields);
1390
 
1391
		$this->Form->radio('Test.all', $options, array(
1392
			'disabled' => array('option1', 'option2')
1393
		));
1394
		$expected = array('Test.test', 'Test.all' => '');
1395
		$this->assertEquals($expected, $this->Form->fields);
1396
 
1397
		$this->Form->radio('Test.some', $options, array(
1398
			'disabled' => array('option1')
1399
		));
1400
		$expected = array('Test.test', 'Test.all' => '', 'Test.some');
1401
		$this->assertEquals($expected, $this->Form->fields);
1402
	}
1403
 
1404
/**
1405
 * Test that when disabled is in a list based attribute array it works.
1406
 *
1407
 * @return void
1408
 */
1409
	public function testSecuredAndDisabledNotAssoc() {
1410
		$this->Form->request['_Token'] = array('key' => 'testKey');
1411
 
1412
		$this->Form->select('Model.select', array(1, 2), array('disabled'));
1413
		$this->Form->checkbox('Model.checkbox', array('disabled'));
1414
		$this->Form->text('Model.text', array('disabled'));
1415
		$this->Form->textarea('Model.textarea', array('disabled'));
1416
		$this->Form->password('Model.password', array('disabled'));
1417
		$this->Form->radio('Model.radio', array(1, 2), array('disabled'));
1418
 
1419
		$expected = array(
1420
			'Model.radio' => ''
1421
		);
1422
		$this->assertEquals($expected, $this->Form->fields);
1423
	}
1424
 
1425
/**
1426
 * test that forms with disabled inputs + secured forms leave off the inputs from the form
1427
 * hashing.
1428
 *
1429
 * @return void
1430
 */
1431
	public function testSecuredAndDisabled() {
1432
		$this->Form->request['_Token'] = array('key' => 'testKey');
1433
 
1434
		$this->Form->checkbox('Model.checkbox', array('disabled' => true));
1435
		$this->Form->text('Model.text', array('disabled' => true));
1436
		$this->Form->text('Model.text2', array('disabled' => 'disabled'));
1437
		$this->Form->password('Model.password', array('disabled' => true));
1438
		$this->Form->textarea('Model.textarea', array('disabled' => true));
1439
		$this->Form->select('Model.select', array(1, 2), array('disabled' => true));
1440
		$this->Form->select('Model.select', array(1, 2), array('disabled' => array(1, 2)));
1441
		$this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
1442
		$this->Form->year('Model.year', null, null, array('disabled' => true));
1443
		$this->Form->month('Model.month', array('disabled' => true));
1444
		$this->Form->day('Model.day', array('disabled' => true));
1445
		$this->Form->hour('Model.hour', false, array('disabled' => true));
1446
		$this->Form->minute('Model.minute', array('disabled' => true));
1447
		$this->Form->meridian('Model.meridian', array('disabled' => true));
1448
 
1449
		$expected = array(
1450
			'Model.radio' => ''
1451
		);
1452
		$this->assertEquals($expected, $this->Form->fields);
1453
	}
1454
 
1455
/**
1456
 * Test that only the path + query elements of a form's URL show up in their hash.
1457
 *
1458
 * @return void
1459
 */
1460
	public function testSecuredFormUrlIgnoresHost() {
1461
		$this->Form->request['_Token'] = array('key' => 'testKey');
1462
 
1463
		$expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
1464
		$this->Form->create('Address', array(
1465
			'url' => array('controller' => 'articles', 'action' => 'view', 1, '?' => array('page' => 1))
1466
		));
1467
		$result = $this->Form->secure();
1468
		$this->assertContains($expected, $result);
1469
 
1470
		$this->Form->create('Address', array('url' => 'http://localhost/articles/view/1?page=1'));
1471
		$result = $this->Form->secure();
1472
		$this->assertContains($expected, $result, 'Full URL should only use path and query.');
1473
 
1474
		$this->Form->create('Address', array('url' => '/articles/view/1?page=1'));
1475
		$result = $this->Form->secure();
1476
		$this->assertContains($expected, $result, 'URL path + query should work.');
1477
 
1478
		$this->Form->create('Address', array('url' => '/articles/view/1'));
1479
		$result = $this->Form->secure();
1480
		$this->assertNotContains($expected, $result, 'URL is different');
1481
	}
1482
 
1483
/**
1484
 * Ensure named parameters work correctly with hash generation.
1485
 *
1486
 * @return void
1487
 */
1488
	public function testSecuredFormUrlWorksWithNamedParameter() {
1489
		$this->Form->request['_Token'] = array('key' => 'testKey');
1490
 
1491
		$expected = 'c890c5f041b1d83d1610dee8f52cd257df7ce618%3A';
1492
		$this->Form->create('Address', array(
1493
			'url' => array('controller' => 'articles', 'action' => 'view', 1, 'type' => 'red')
1494
		));
1495
		$result = $this->Form->secure();
1496
		$this->assertContains($expected, $result);
1497
	}
1498
 
1499
/**
1500
 * Test that URL, HTML and identifier show up in their hashs.
1501
 *
1502
 * @return void
1503
 */
1504
	public function testSecuredFormUrlHasHtmlAndIdentifier() {
1505
		$this->Form->request['_Token'] = array('key' => 'testKey');
1506
 
1507
		$expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
1508
		$this->Form->create('Address', array(
1509
			'url' => array(
1510
				'controller' => 'articles',
1511
				'action' => 'view',
1512
				'?' => array(
1513
					'page' => 1,
1514
					'limit' => 10,
1515
					'html' => '<>"',
1516
				),
1517
				'#' => 'result',
1518
			),
1519
		));
1520
		$result = $this->Form->secure();
1521
		$this->assertContains($expected, $result);
1522
 
1523
		$this->Form->create('Address', array('url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'));
1524
		$result = $this->Form->secure();
1525
		$this->assertContains($expected, $result, 'Full URL should only use path and query.');
1526
 
1527
		$this->Form->create('Address', array('url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'));
1528
		$result = $this->Form->secure();
1529
		$this->assertContains($expected, $result, 'URL path + query should work.');
1530
	}
1531
 
1532
/**
1533
 * testDisableSecurityUsingForm method
1534
 *
1535
 * @return void
1536
 */
1537
	public function testDisableSecurityUsingForm() {
1538
		$this->Form->request['_Token'] = array(
1539
			'key' => 'testKey',
1540
			'disabledFields' => array()
1541
		);
1542
		$this->Form->create();
1543
 
1544
		$this->Form->hidden('Addresses.id', array('value' => '123456'));
1545
		$this->Form->input('Addresses.title');
1546
		$this->Form->input('Addresses.first_name', array('secure' => false));
1547
		$this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
1548
		$this->Form->input('Addresses.zip', array(
1549
			'type' => 'select', 'options' => array(1, 2), 'secure' => false
1550
		));
1551
 
1552
		$result = $this->Form->fields;
1553
		$expected = array(
1554
			'Addresses.id' => '123456', 'Addresses.title',
1555
		);
1556
		$this->assertEquals($expected, $result);
1557
	}
1558
 
1559
/**
1560
 * test disableField
1561
 *
1562
 * @return void
1563
 */
1564
	public function testUnlockFieldAddsToList() {
1565
		$this->Form->request['_Token'] = array(
1566
			'key' => 'testKey',
1567
			'unlockedFields' => array()
1568
		);
1569
		$this->Form->create('Contact');
1570
		$this->Form->unlockField('Contact.name');
1571
		$this->Form->text('Contact.name');
1572
 
1573
		$this->assertEquals(array('Contact.name'), $this->Form->unlockField());
1574
		$this->assertEquals(array(), $this->Form->fields);
1575
	}
1576
 
1577
/**
1578
 * test unlockField removing from fields array.
1579
 *
1580
 * @return void
1581
 */
1582
	public function testUnlockFieldRemovingFromFields() {
1583
		$this->Form->request['_Token'] = array(
1584
			'key' => 'testKey',
1585
			'unlockedFields' => array()
1586
		);
1587
		$this->Form->create('Contact');
1588
		$this->Form->hidden('Contact.id', array('value' => 1));
1589
		$this->Form->text('Contact.name');
1590
 
1591
		$this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
1592
		$this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
1593
 
1594
		$this->Form->unlockField('Contact.name');
1595
		$this->Form->unlockField('Contact.id');
1596
		$this->assertEquals(array(), $this->Form->fields);
1597
	}
1598
 
1599
/**
1600
 * testTagIsInvalid method
1601
 *
1602
 * @return void
1603
 */
1604
	public function testTagIsInvalid() {
1605
		$Contact = ClassRegistry::getObject('Contact');
1606
		$Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
1607
 
1608
		$this->Form->setEntity('Contact.0.email');
1609
		$result = $this->Form->tagIsInvalid();
1610
		$this->assertEquals($expected, $result);
1611
 
1612
		$this->Form->setEntity('Contact.1.email');
1613
		$result = $this->Form->tagIsInvalid();
1614
		$this->assertFalse($result);
1615
 
1616
		$this->Form->setEntity('Contact.0.name');
1617
		$result = $this->Form->tagIsInvalid();
1618
		$this->assertFalse($result);
1619
	}
1620
 
1621
/**
1622
 * Test tagIsInvalid with validation errors from a saveMany
1623
 *
1624
 * @return void
1625
 */
1626
	public function testTagIsInvalidSaveMany() {
1627
		$Contact = ClassRegistry::getObject('Contact');
1628
		$Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
1629
 
1630
		$this->Form->create('Contact');
1631
 
1632
		$this->Form->setEntity('0.email');
1633
		$result = $this->Form->tagIsInvalid();
1634
		$this->assertEquals($expected, $result);
1635
 
1636
		$this->Form->setEntity('0.Contact.email');
1637
		$result = $this->Form->tagIsInvalid();
1638
		$this->assertEquals($expected, $result);
1639
	}
1640
 
1641
/**
1642
 * Test validation errors.
1643
 *
1644
 * @return void
1645
 */
1646
	public function testPasswordValidation() {
1647
		$Contact = ClassRegistry::getObject('Contact');
1648
		$Contact->validationErrors['password'] = array('Please provide a password');
1649
 
1650
		$result = $this->Form->input('Contact.password');
1651
		$expected = array(
1652
			'div' => array('class' => 'input password error'),
1653
			'label' => array('for' => 'ContactPassword'),
1654
			'Password',
1655
			'/label',
1656
			'input' => array(
1657
				'type' => 'password', 'name' => 'data[Contact][password]',
1658
				'id' => 'ContactPassword', 'class' => 'form-error'
1659
			),
1660
			array('div' => array('class' => 'error-message')),
1661
			'Please provide a password',
1662
			'/div',
1663
			'/div'
1664
		);
1665
		$this->assertTags($result, $expected);
1666
 
1667
		$result = $this->Form->input('Contact.password', array('errorMessage' => false));
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
			'/div'
1678
		);
1679
		$this->assertTags($result, $expected);
1680
	}
1681
 
1682
/**
1683
 * Test validation errors, when validation message is an empty string.
1684
 *
1685
 * @return void
1686
 */
1687
	public function testEmptyErrorValidation() {
1688
		$this->Form->validationErrors['Contact']['password'] = '';
1689
 
1690
		$result = $this->Form->input('Contact.password');
1691
		$expected = array(
1692
			'div' => array('class' => 'input password error'),
1693
			'label' => array('for' => 'ContactPassword'),
1694
			'Password',
1695
			'/label',
1696
			'input' => array(
1697
				'type' => 'password', 'name' => 'data[Contact][password]',
1698
				'id' => 'ContactPassword', 'class' => 'form-error'
1699
			),
1700
			array('div' => array('class' => 'error-message')),
1701
			array(),
1702
			'/div',
1703
			'/div'
1704
		);
1705
		$this->assertTags($result, $expected);
1706
 
1707
		$result = $this->Form->input('Contact.password', array('errorMessage' => false));
1708
		$expected = array(
1709
			'div' => array('class' => 'input password error'),
1710
			'label' => array('for' => 'ContactPassword'),
1711
			'Password',
1712
			'/label',
1713
			'input' => array(
1714
				'type' => 'password', 'name' => 'data[Contact][password]',
1715
				'id' => 'ContactPassword', 'class' => 'form-error'
1716
			),
1717
			'/div'
1718
		);
1719
		$this->assertTags($result, $expected);
1720
	}
1721
 
1722
/**
1723
 * Test validation errors, when calling input() overriding validation message by an empty string.
1724
 *
1725
 * @return void
1726
 */
1727
	public function testEmptyInputErrorValidation() {
1728
		$this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
1729
 
1730
		$result = $this->Form->input('Contact.password', array('error' => ''));
1731
		$expected = array(
1732
			'div' => array('class' => 'input password error'),
1733
			'label' => array('for' => 'ContactPassword'),
1734
			'Password',
1735
			'/label',
1736
			'input' => array(
1737
				'type' => 'password', 'name' => 'data[Contact][password]',
1738
				'id' => 'ContactPassword', 'class' => 'form-error'
1739
			),
1740
			array('div' => array('class' => 'error-message')),
1741
			array(),
1742
			'/div',
1743
			'/div'
1744
		);
1745
		$this->assertTags($result, $expected);
1746
 
1747
		$result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
1748
		$expected = array(
1749
			'div' => array('class' => 'input password error'),
1750
			'label' => array('for' => 'ContactPassword'),
1751
			'Password',
1752
			'/label',
1753
			'input' => array(
1754
				'type' => 'password', 'name' => 'data[Contact][password]',
1755
				'id' => 'ContactPassword', 'class' => 'form-error'
1756
			),
1757
			'/div'
1758
		);
1759
		$this->assertTags($result, $expected);
1760
	}
1761
 
1762
/**
1763
 * testFormValidationAssociated method
1764
 *
1765
 * test display of form errors in conjunction with model::validates.
1766
 *
1767
 * @return void
1768
 */
1769
	public function testFormValidationAssociated() {
1770
		$this->UserForm = ClassRegistry::getObject('UserForm');
1771
		$this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
1772
 
1773
		$data = array(
1774
			'UserForm' => array('name' => 'user'),
1775
			'OpenidUrl' => array('url' => 'http://www.cakephp.org')
1776
		);
1777
 
1778
		$result = $this->UserForm->OpenidUrl->create($data);
1779
		$this->assertFalse(empty($result));
1780
		$this->assertFalse($this->UserForm->OpenidUrl->validates());
1781
 
1782
		$result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
1783
		$encoding = strtolower(Configure::read('App.encoding'));
1784
		$expected = array(
1785
			'form' => array(
1786
				'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
1787
				'accept-charset' => $encoding
1788
			),
1789
			'div' => array('style' => 'display:none;'),
1790
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1791
			'/div'
1792
		);
1793
		$this->assertTags($result, $expected);
1794
 
1795
		$result = $this->Form->error(
1796
			'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
1797
		);
1798
		$this->assertEquals('Error, not registered', $result);
1799
 
1800
		unset($this->UserForm->OpenidUrl, $this->UserForm);
1801
	}
1802
 
1803
/**
1804
 * testFormValidationAssociatedFirstLevel method
1805
 *
1806
 * test form error display with associated model.
1807
 *
1808
 * @return void
1809
 */
1810
	public function testFormValidationAssociatedFirstLevel() {
1811
		$this->ValidateUser = ClassRegistry::getObject('ValidateUser');
1812
		$this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
1813
 
1814
		$data = array(
1815
			'ValidateUser' => array('name' => 'mariano'),
1816
			'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
1817
		);
1818
 
1819
		$result = $this->ValidateUser->create($data);
1820
		$this->assertFalse(empty($result));
1821
		$this->assertFalse($this->ValidateUser->validates());
1822
		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1823
 
1824
		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1825
		$encoding = strtolower(Configure::read('App.encoding'));
1826
		$expected = array(
1827
			'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
1828
			'div' => array('style' => 'display:none;'),
1829
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1830
			'/div'
1831
		);
1832
		$this->assertTags($result, $expected);
1833
 
1834
		$result = $this->Form->error(
1835
			'ValidateUser.email', 'Invalid email', array('wrap' => false)
1836
		);
1837
		$this->assertEquals('Invalid email', $result);
1838
 
1839
		$result = $this->Form->error(
1840
			'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
1841
		);
1842
		$this->assertEquals('Invalid name', $result);
1843
 
1844
		$result = $this->Form->error(
1845
			'ValidateProfile.city', 'Invalid city', array('wrap' => false)
1846
		);
1847
		$this->assertEquals('Invalid city', $result);
1848
 
1849
		unset($this->ValidateUser->ValidateProfile);
1850
		unset($this->ValidateUser);
1851
	}
1852
 
1853
/**
1854
 * testFormValidationAssociatedSecondLevel method
1855
 *
1856
 * test form error display with associated model.
1857
 *
1858
 * @return void
1859
 */
1860
	public function testFormValidationAssociatedSecondLevel() {
1861
		$this->ValidateUser = ClassRegistry::getObject('ValidateUser');
1862
		$this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
1863
		$this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
1864
 
1865
		$data = array(
1866
			'ValidateUser' => array('name' => 'mariano'),
1867
			'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
1868
			'ValidateItem' => array('name' => 'Item')
1869
		);
1870
 
1871
		$result = $this->ValidateUser->create($data);
1872
		$this->assertFalse(empty($result));
1873
		$this->assertFalse($this->ValidateUser->validates());
1874
		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1875
		$this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
1876
 
1877
		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1878
		$encoding = strtolower(Configure::read('App.encoding'));
1879
		$expected = array(
1880
			'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
1881
			'div' => array('style' => 'display:none;'),
1882
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1883
			'/div'
1884
		);
1885
		$this->assertTags($result, $expected);
1886
 
1887
		$result = $this->Form->error(
1888
			'ValidateUser.email', 'Invalid email', array('wrap' => false)
1889
		);
1890
		$this->assertEquals('Invalid email', $result);
1891
 
1892
		$result = $this->Form->error(
1893
			'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
1894
		);
1895
		$this->assertEquals('Invalid name', $result);
1896
 
1897
		$result = $this->Form->error(
1898
			'ValidateProfile.city', 'Invalid city', array('wrap' => false)
1899
		);
1900
 
1901
		$result = $this->Form->error(
1902
			'ValidateItem.description', 'Invalid description', array('wrap' => false)
1903
		);
1904
		$this->assertEquals('Invalid description', $result);
1905
 
1906
		unset($this->ValidateUser->ValidateProfile->ValidateItem);
1907
		unset($this->ValidateUser->ValidateProfile);
1908
		unset($this->ValidateUser);
1909
	}
1910
 
1911
/**
1912
 * testFormValidationMultiRecord method
1913
 *
1914
 * test form error display with multiple records.
1915
 *
1916
 * @return void
1917
 */
1918
	public function testFormValidationMultiRecord() {
1919
		$Contact = ClassRegistry::getObject('Contact');
1920
		$Contact->validationErrors[2] = array(
1921
			'name' => array('This field cannot be left blank')
1922
		);
1923
		$result = $this->Form->input('Contact.2.name');
1924
		$expected = array(
1925
			'div' => array('class' => 'input text error'),
1926
			'label' => array('for' => 'Contact2Name'),
1927
			'Name',
1928
			'/label',
1929
			'input' => array(
1930
				'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
1931
				'class' => 'form-error', 'maxlength' => 255
1932
			),
1933
			array('div' => array('class' => 'error-message')),
1934
			'This field cannot be left blank',
1935
			'/div',
1936
			'/div'
1937
		);
1938
		$this->assertTags($result, $expected);
1939
	}
1940
 
1941
/**
1942
 * testMultipleInputValidation method
1943
 *
1944
 * test multiple record form validation error display.
1945
 *
1946
 * @return void
1947
 */
1948
	public function testMultipleInputValidation() {
1949
		$Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
1950
		$Address->validationErrors[0] = array(
1951
			'title' => array('This field cannot be empty'),
1952
			'first_name' => array('This field cannot be empty')
1953
		);
1954
		$Address->validationErrors[1] = array(
1955
			'last_name' => array('You must have a last name')
1956
		);
1957
		$this->Form->create();
1958
 
1959
		$result = $this->Form->input('Address.0.title');
1960
		$expected = array(
1961
			'div' => array('class'),
1962
			'label' => array('for'),
1963
			'preg:/[^<]+/',
1964
			'/label',
1965
			'input' => array(
1966
				'type' => 'text', 'name', 'id', 'class' => 'form-error'
1967
			),
1968
			array('div' => array('class' => 'error-message')),
1969
			'This field cannot be empty',
1970
			'/div',
1971
			'/div'
1972
		);
1973
		$this->assertTags($result, $expected);
1974
 
1975
		$result = $this->Form->input('Address.0.first_name');
1976
		$expected = array(
1977
			'div' => array('class'),
1978
			'label' => array('for'),
1979
			'preg:/[^<]+/',
1980
			'/label',
1981
			'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
1982
			array('div' => array('class' => 'error-message')),
1983
			'This field cannot be empty',
1984
			'/div',
1985
			'/div'
1986
		);
1987
		$this->assertTags($result, $expected);
1988
 
1989
		$result = $this->Form->input('Address.0.last_name');
1990
		$expected = array(
1991
			'div' => array('class'),
1992
			'label' => array('for'),
1993
			'preg:/[^<]+/',
1994
			'/label',
1995
			'input' => array('type' => 'text', 'name', 'id'),
1996
			'/div'
1997
		);
1998
		$this->assertTags($result, $expected);
1999
 
2000
		$result = $this->Form->input('Address.1.last_name');
2001
		$expected = array(
2002
			'div' => array('class'),
2003
			'label' => array('for'),
2004
			'preg:/[^<]+/',
2005
			'/label',
2006
			'input' => array(
2007
				'type' => 'text', 'name', 'id',
2008
				'class' => 'form-error'
2009
			),
2010
			array('div' => array('class' => 'error-message')),
2011
			'You must have a last name',
2012
			'/div',
2013
			'/div'
2014
		);
2015
		$this->assertTags($result, $expected);
2016
	}
2017
 
2018
/**
2019
 * testInput method
2020
 *
2021
 * Test various incarnations of input().
2022
 *
2023
 * @return void
2024
 */
2025
	public function testInput() {
2026
		$result = $this->Form->input('ValidateUser.balance');
2027
		$expected = array(
2028
			'div' => array('class'),
2029
			'label' => array('for'),
2030
			'Balance',
2031
			'/label',
2032
			'input' => array('name', 'type' => 'number', 'id', 'step'),
2033
			'/div',
2034
		);
2035
		$this->assertTags($result, $expected);
2036
 
2037
		$result = $this->Form->input('ValidateUser.cost_decimal');
2038
		$expected = array(
2039
			'div' => array('class'),
2040
			'label' => array('for'),
2041
			'Cost Decimal',
2042
			'/label',
2043
			'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
2044
			'/div',
2045
		);
2046
		$this->assertTags($result, $expected);
2047
 
2048
		$result = $this->Form->input('ValidateUser.ratio');
2049
		$expected = array(
2050
			'div' => array('class'),
2051
			'label' => array('for'),
2052
			'Ratio',
2053
			'/label',
2054
			'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
2055
			'/div',
2056
		);
2057
		$this->assertTags($result, $expected);
2058
 
2059
		$result = $this->Form->input('ValidateUser.population');
2060
		$expected = array(
2061
			'div' => array('class'),
2062
			'label' => array('for'),
2063
			'Population',
2064
			'/label',
2065
			'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
2066
			'/div',
2067
		);
2068
		$this->assertTags($result, $expected);
2069
 
2070
		$result = $this->Form->input('Contact.email', array('id' => 'custom'));
2071
		$expected = array(
2072
			'div' => array('class' => 'input email'),
2073
			'label' => array('for' => 'custom'),
2074
			'Email',
2075
			'/label',
2076
			array('input' => array(
2077
				'type' => 'email', 'name' => 'data[Contact][email]',
2078
				'id' => 'custom', 'maxlength' => 255
2079
			)),
2080
			'/div'
2081
		);
2082
		$this->assertTags($result, $expected);
2083
 
2084
		$result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
2085
		$expected = array(
2086
			'<div',
2087
			'label' => array('for' => 'ContactEmail'),
2088
			'Email',
2089
			'/label',
2090
			array('input' => array(
2091
				'type' => 'email', 'name' => 'data[Contact][email]',
2092
				'id' => 'ContactEmail', 'maxlength' => 255
2093
			)),
2094
			'/div'
2095
		);
2096
		$this->assertTags($result, $expected);
2097
 
2098
		$result = $this->Form->hidden('Contact.idontexist');
2099
		$expected = array('input' => array(
2100
				'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
2101
				'id' => 'ContactIdontexist'
2102
		));
2103
		$this->assertTags($result, $expected);
2104
 
2105
		$result = $this->Form->input('Contact.email', array('type' => 'text'));
2106
		$expected = array(
2107
			'div' => array('class' => 'input text'),
2108
			'label' => array('for' => 'ContactEmail'),
2109
			'Email',
2110
			'/label',
2111
			array('input' => array(
2112
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]',
2113
				'id' => 'ContactEmail'
2114
			)),
2115
			'/div'
2116
		);
2117
		$this->assertTags($result, $expected);
2118
 
2119
		$result = $this->Form->input('Contact.5.email', array('type' => 'text'));
2120
		$expected = array(
2121
			'div' => array('class' => 'input text'),
2122
			'label' => array('for' => 'Contact5Email'),
2123
			'Email',
2124
			'/label',
2125
			array('input' => array(
2126
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][5][email]',
2127
				'id' => 'Contact5Email'
2128
			)),
2129
			'/div'
2130
		);
2131
		$this->assertTags($result, $expected);
2132
 
2133
		$result = $this->Form->input('Contact.password');
2134
		$expected = array(
2135
			'div' => array('class' => 'input password'),
2136
			'label' => array('for' => 'ContactPassword'),
2137
			'Password',
2138
			'/label',
2139
			array('input' => array(
2140
				'type' => 'password', 'name' => 'data[Contact][password]',
2141
				'id' => 'ContactPassword'
2142
			)),
2143
			'/div'
2144
		);
2145
		$this->assertTags($result, $expected);
2146
 
2147
		$result = $this->Form->input('Contact.email', array(
2148
			'type' => 'file', 'class' => 'textbox'
2149
		));
2150
		$expected = array(
2151
			'div' => array('class' => 'input file'),
2152
			'label' => array('for' => 'ContactEmail'),
2153
			'Email',
2154
			'/label',
2155
			array('input' => array(
2156
				'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
2157
				'id' => 'ContactEmail'
2158
			)),
2159
			'/div'
2160
		);
2161
		$this->assertTags($result, $expected);
2162
 
2163
		$this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
2164
		$result = $this->Form->input('Contact.phone');
2165
		$expected = array(
2166
			'div' => array('class' => 'input tel'),
2167
			'label' => array('for' => 'ContactPhone'),
2168
			'Phone',
2169
			'/label',
2170
			array('input' => array(
2171
				'type' => 'tel', 'name' => 'data[Contact][phone]',
2172
				'value' => 'Hello &amp; World &gt; weird chars',
2173
				'id' => 'ContactPhone', 'maxlength' => 255
2174
			)),
2175
			'/div'
2176
		);
2177
		$this->assertTags($result, $expected);
2178
 
2179
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
2180
		$result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
2181
		$expected = array(
2182
			'div' => array('class' => 'input text'),
2183
			'label' => array('for' => 'myId'),
2184
			'Field',
2185
			'/label',
2186
			'input' => array(
2187
				'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
2188
				'value' => 'My value', 'id' => 'myId'
2189
			),
2190
			'/div'
2191
		);
2192
		$this->assertTags($result, $expected);
2193
 
2194
		unset($this->Form->request->data);
2195
 
2196
		$Contact = ClassRegistry::getObject('Contact');
2197
		$Contact->validationErrors['field'] = array('Badness!');
2198
		$result = $this->Form->input('Contact.field');
2199
		$expected = array(
2200
			'div' => array('class' => 'input text error'),
2201
			'label' => array('for' => 'ContactField'),
2202
			'Field',
2203
			'/label',
2204
			'input' => array(
2205
				'type' => 'text', 'name' => 'data[Contact][field]',
2206
				'id' => 'ContactField', 'class' => 'form-error'
2207
			),
2208
			array('div' => array('class' => 'error-message')),
2209
			'Badness!',
2210
			'/div',
2211
			'/div'
2212
		);
2213
		$this->assertTags($result, $expected);
2214
 
2215
		$result = $this->Form->input('Contact.field', array(
2216
			'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
2217
		));
2218
		$expected = array(
2219
			'label' => array('for' => 'ContactField'),
2220
			'Field',
2221
			'/label',
2222
			'input' => array(
2223
				'type' => 'text', 'name' => 'data[Contact][field]',
2224
				'id' => 'ContactField', 'class' => 'form-error'
2225
			),
2226
			array('span' => array('class' => 'error-message')),
2227
			'Badness!',
2228
			'/span'
2229
		);
2230
		$this->assertTags($result, $expected);
2231
 
2232
		$result = $this->Form->input('Contact.field', array(
2233
			'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
2234
		));
2235
		$expected = array(
2236
			'div' => array('class' => 'input text error'),
2237
			'label' => array('for' => 'ContactField'),
2238
			'Field',
2239
			'/label',
2240
			'input' => array(
2241
				'type' => 'text', 'name' => 'data[Contact][field]',
2242
				'id' => 'ContactField', 'class' => 'form-error'
2243
			),
2244
			array('div' => array('class' => 'error')),
2245
			'Badness!',
2246
			'/div'
2247
		);
2248
		$this->assertTags($result, $expected);
2249
 
2250
		$result = $this->Form->input('Contact.field', array(
2251
			'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
2252
		));
2253
		$expected = array(
2254
			'span' => array('class' => 'input text error'),
2255
			'label' => array('for' => 'ContactField'),
2256
			'Field',
2257
			'/label',
2258
			'input' => array(
2259
				'type' => 'text', 'name' => 'data[Contact][field]',
2260
				'id' => 'ContactField', 'class' => 'form-error'
2261
			),
2262
			'Badness!',
2263
			'/span'
2264
		);
2265
		$this->assertTags($result, $expected);
2266
 
2267
		$result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
2268
		$expected = array(
2269
			'div' => array('class' => 'input text error'),
2270
			'label' => array('for' => 'ContactField'),
2271
			'Field',
2272
			'/label',
2273
			'input' => array(
2274
				'type' => 'text', 'name' => 'data[Contact][field]',
2275
				'id' => 'ContactField', 'class' => 'form-error'
2276
			),
2277
			'A message to you, Rudy',
2278
			array('div' => array('class' => 'error-message')),
2279
			'Badness!',
2280
			'/div',
2281
			'/div'
2282
		);
2283
		$this->assertTags($result, $expected);
2284
 
2285
		$this->Form->setEntity(null);
2286
		$this->Form->setEntity('Contact.field');
2287
		$result = $this->Form->input('Contact.field', array(
2288
			'after' => 'A message to you, Rudy', 'error' => false
2289
		));
2290
		$expected = array(
2291
			'div' => array('class' => 'input text'),
2292
			'label' => array('for' => 'ContactField'),
2293
			'Field',
2294
			'/label',
2295
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2296
			'A message to you, Rudy',
2297
			'/div'
2298
		);
2299
		$this->assertTags($result, $expected);
2300
 
2301
		$result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
2302
		$expected = array(
2303
			'div' => array('class' => 'input text'),
2304
			'label' => array('for' => 'ObjectField'),
2305
			'Field',
2306
			'/label',
2307
			'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
2308
			'A message to you, Rudy',
2309
			'/div'
2310
		);
2311
		$this->assertTags($result, $expected);
2312
 
2313
		$Contact->validationErrors['field'] = array('minLength');
2314
		$result = $this->Form->input('Contact.field', array(
2315
			'error' => array(
2316
				'minLength' => 'Le login doit contenir au moins 2 caractères',
2317
				'maxLength' => 'login too large'
2318
			)
2319
		));
2320
		$expected = array(
2321
			'div' => array('class' => 'input text error'),
2322
			'label' => array('for' => 'ContactField'),
2323
			'Field',
2324
			'/label',
2325
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2326
			array('div' => array('class' => 'error-message')),
2327
			'Le login doit contenir au moins 2 caractères',
2328
			'/div',
2329
			'/div'
2330
		);
2331
		$this->assertTags($result, $expected);
2332
 
2333
		$Contact->validationErrors['field'] = array('maxLength');
2334
		$result = $this->Form->input('Contact.field', array(
2335
			'error' => array(
2336
				'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
2337
				'minLength' => 'Le login doit contenir au moins 2 caractères',
2338
				'maxLength' => 'login too large',
2339
			)
2340
		));
2341
		$expected = array(
2342
			'div' => array('class' => 'input text error'),
2343
			'label' => array('for' => 'ContactField'),
2344
			'Field',
2345
			'/label',
2346
			'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
2347
			array('span' => array('class' => 'error-message', 'rel' => 'fake')),
2348
			'login too large',
2349
			'/span',
2350
			'/div'
2351
		);
2352
		$this->assertTags($result, $expected);
2353
	}
2354
 
2355
/**
2356
 * Test that inputs with 0 can be created.
2357
 *
2358
 * @return void
2359
 */
2360
	public function testInputZero() {
2361
		$this->Form->create('User');
2362
		$result = $this->Form->input('0');
2363
		$expected = array(
2364
			'div' => array('class' => 'input text'),
2365
			'label' => array('for' => 'User0'), '/label',
2366
			'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
2367
			'/div'
2368
		);
2369
		$this->assertTags($result, $expected);
2370
	}
2371
 
2372
/**
2373
 * test input() with checkbox creation
2374
 *
2375
 * @return void
2376
 */
2377
	public function testInputCheckbox() {
2378
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
2379
		$expected = array(
2380
			'div' => array('class' => 'input checkbox'),
2381
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2382
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2383
			'/div'
2384
		);
2385
		$this->assertTags($result, $expected);
2386
 
2387
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
2388
		$expected = array(
2389
			'div' => array('class' => 'input checkbox'),
2390
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2391
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2392
			'/div'
2393
		);
2394
		$this->assertTags($result, $expected);
2395
 
2396
		$result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
2397
		$expected = array(
2398
			'div' => array('class' => 'input checkbox'),
2399
			'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
2400
			array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
2401
			'/div'
2402
		);
2403
		$this->assertTags($result, $expected);
2404
 
2405
		$result = $this->Form->input('User.disabled', array(
2406
			'label' => 'Disabled',
2407
			'type' => 'checkbox',
2408
			'data-foo' => 'disabled'
2409
		));
2410
		$expected = array(
2411
			'div' => array('class' => 'input checkbox'),
2412
			'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
2413
			array('input' => array(
2414
				'type' => 'checkbox',
2415
				'name' => 'data[User][disabled]',
2416
				'value' => '1',
2417
				'id' => 'UserDisabled',
2418
				'data-foo' => 'disabled'
2419
			)),
2420
			'label' => array('for' => 'UserDisabled'),
2421
			'Disabled',
2422
			'/label',
2423
			'/div'
2424
		);
2425
		$this->assertTags($result, $expected);
2426
	}
2427
 
2428
/**
2429
 * test form->input() with time types.
2430
 *
2431
 * @return void
2432
 */
2433
	public function testInputTime() {
2434
		extract($this->dateRegex);
2435
		$result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
2436
		$result = explode(':', $result);
2437
		$this->assertRegExp('/option value="23"/', $result[0]);
2438
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2439
 
2440
		$result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
2441
		$result = explode(':', $result);
2442
		$this->assertRegExp('/option value="23"/', $result[0]);
2443
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2444
 
2445
		$result = $this->Form->input('Model.field', array(
2446
			'type' => 'time', 'timeFormat' => 24, 'interval' => 15
2447
		));
2448
		$result = explode(':', $result);
2449
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2450
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2451
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2452
 
2453
		$result = $this->Form->input('Model.field', array(
2454
			'type' => 'time', 'timeFormat' => 12, 'interval' => 15
2455
		));
2456
		$result = explode(':', $result);
2457
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2458
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2459
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2460
 
2461
		$result = $this->Form->input('prueba', array(
2462
			'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
2463
			'maxYear' => date('Y') + 1, 'interval' => 15
2464
		));
2465
		$result = explode(':', $result);
2466
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2467
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2468
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2469
		$this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
2470
 
2471
		$result = $this->Form->input('Random.start_time', array(
2472
			'type' => 'time',
2473
			'selected' => '18:15'
2474
		));
2475
		$this->assertContains('<option value="06" selected="selected">6</option>', $result);
2476
		$this->assertContains('<option value="15" selected="selected">15</option>', $result);
2477
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2478
 
2479
		$result = $this->Form->input('published', array('type' => 'time'));
2480
		$now = strtotime('now');
2481
		$this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
2482
 
2483
		$now = strtotime('2013-03-09 00:42:21');
2484
		$result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
2485
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2486
		$this->assertContains('<option value="42" selected="selected">42</option>', $result);
2487
	}
2488
 
2489
/**
2490
 * Test interval + selected near the hour roll over.
2491
 *
2492
 * @return void
2493
 */
2494
	public function testTimeSelectedWithInterval() {
2495
		$result = $this->Form->input('Model.start_time', array(
2496
			'type' => 'time',
2497
			'interval' => 15,
2498
			'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
2499
		));
2500
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2501
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2502
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2503
 
2504
		$result = $this->Form->input('Model.start_time', array(
2505
			'type' => 'time',
2506
			'interval' => 15,
2507
			'selected' => '2012-10-23 15:57:00'
2508
		));
2509
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2510
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2511
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2512
 
2513
		$result = $this->Form->input('Model.start_time', array(
2514
			'timeFormat' => 24,
2515
			'type' => 'time',
2516
			'interval' => 15,
2517
			'selected' => '15:57'
2518
		));
2519
		$this->assertContains('<option value="16" selected="selected">16</option>', $result);
2520
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2521
 
2522
		$result = $this->Form->input('Model.start_time', array(
2523
			'timeFormat' => 24,
2524
			'type' => 'time',
2525
			'interval' => 15,
2526
			'selected' => '23:57'
2527
		));
2528
		$this->assertContains('<option value="00" selected="selected">0</option>', $result);
2529
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2530
 
2531
		$result = $this->Form->input('Model.created', array(
2532
			'timeFormat' => 24,
2533
			'type' => 'datetime',
2534
			'interval' => 15,
2535
			'selected' => '2012-09-30 23:56'
2536
		));
2537
		$this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
2538
		$this->assertContains('<option value="10" selected="selected">October</option>', $result);
2539
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2540
		$this->assertContains('<option value="00" selected="selected">0</option>', $result);
2541
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2542
	}
2543
 
2544
/**
2545
 * Test interval + value near the hour roll over.
2546
 *
2547
 * @return void
2548
 */
2549
	public function testTimeValueWithInterval() {
2550
		$result = $this->Form->input('Model.start_time', array(
2551
			'type' => 'time',
2552
			'interval' => 15,
2553
			'value' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
2554
		));
2555
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2556
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2557
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2558
 
2559
		$result = $this->Form->input('Model.start_time', array(
2560
			'type' => 'time',
2561
			'interval' => 15,
2562
			'value' => '2012-10-23 15:57:00'
2563
		));
2564
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2565
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2566
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2567
 
2568
		$result = $this->Form->input('Model.start_time', array(
2569
			'timeFormat' => 24,
2570
			'type' => 'time',
2571
			'interval' => 15,
2572
			'value' => '15:57'
2573
		));
2574
		$this->assertContains('<option value="16" selected="selected">16</option>', $result);
2575
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2576
 
2577
		$result = $this->Form->input('Model.start_time', array(
2578
			'timeFormat' => 24,
2579
			'type' => 'time',
2580
			'interval' => 15,
2581
			'value' => '23:57'
2582
		));
2583
		$this->assertContains('<option value="00" selected="selected">0</option>', $result);
2584
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2585
 
2586
		$result = $this->Form->input('Model.created', array(
2587
			'timeFormat' => 24,
2588
			'type' => 'datetime',
2589
			'interval' => 15,
2590
			'value' => '2012-09-30 23:56'
2591
		));
2592
		$this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
2593
		$this->assertContains('<option value="10" selected="selected">October</option>', $result);
2594
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2595
		$this->assertContains('<option value="00" selected="selected">0</option>', $result);
2596
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2597
	}
2598
 
2599
/**
2600
 * Test time with selected values around 12:xx:xx
2601
 *
2602
 * @return void
2603
 */
2604
	public function testTimeSelectedWithIntervalTwelve() {
2605
		$result = $this->Form->input('Model.start_time', array(
2606
			'type' => 'time',
2607
			'timeFormat' => 12,
2608
			'interval' => 15,
2609
			'selected' => '00:00:00'
2610
		));
2611
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2612
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2613
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2614
 
2615
		$result = $this->Form->input('Model.start_time', array(
2616
			'type' => 'time',
2617
			'timeFormat' => 12,
2618
			'interval' => 15,
2619
			'selected' => '12:00:00'
2620
		));
2621
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2622
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2623
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2624
 
2625
		$result = $this->Form->input('Model.start_time', array(
2626
			'type' => 'time',
2627
			'timeFormat' => 12,
2628
			'interval' => 15,
2629
			'selected' => '12:15:00'
2630
		));
2631
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2632
		$this->assertContains('<option value="15" selected="selected">15</option>', $result);
2633
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2634
	}
2635
 
2636
/**
2637
 * Test time with selected values around 12:xx:xx
2638
 *
2639
 * @return void
2640
 */
2641
	public function testTimeValueWithIntervalTwelve() {
2642
		$result = $this->Form->input('Model.start_time', array(
2643
			'type' => 'time',
2644
			'timeFormat' => 12,
2645
			'interval' => 15,
2646
			'value' => '00:00:00'
2647
		));
2648
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2649
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2650
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2651
 
2652
		$result = $this->Form->input('Model.start_time', array(
2653
			'type' => 'time',
2654
			'timeFormat' => 12,
2655
			'interval' => 15,
2656
			'value' => '12:00:00'
2657
		));
2658
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2659
		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
2660
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2661
 
2662
		$result = $this->Form->input('Model.start_time', array(
2663
			'type' => 'time',
2664
			'timeFormat' => 12,
2665
			'interval' => 15,
2666
			'value' => '12:15:00'
2667
		));
2668
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2669
		$this->assertContains('<option value="15" selected="selected">15</option>', $result);
2670
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2671
	}
2672
 
2673
/**
2674
 * Test interval & timeFormat = 12
2675
 *
2676
 * @return void
2677
 */
2678
	public function testInputTimeWithIntervalAnd12HourFormat() {
2679
		$result = $this->Form->input('Model.start_time', array(
2680
			'type' => 'time',
2681
			'timeFormat' => 12,
2682
			'interval' => 5,
2683
			'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
2684
		));
2685
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2686
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2687
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2688
 
2689
		$result = $this->Form->input('Model.start_time', array(
2690
			'type' => 'time',
2691
			'timeFormat' => '12',
2692
			'interval' => 5,
2693
			'selected' => '2013-04-19 16:30:00'
2694
		));
2695
		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
2696
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2697
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2698
 
2699
		$result = $this->Form->input('Model.start_time', array(
2700
			'type' => 'time',
2701
			'timeFormat' => '12',
2702
			'interval' => 10,
2703
			'selected' => '2013-05-19 00:33:00'
2704
		));
2705
		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
2706
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2707
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2708
 
2709
		$result = $this->Form->input('Model.start_time', array(
2710
			'type' => 'time',
2711
			'timeFormat' => '12',
2712
			'interval' => 10,
2713
			'selected' => '2013-05-19 13:33:00'
2714
		));
2715
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2716
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2717
		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2718
 
2719
		$result = $this->Form->input('Model.start_time', array(
2720
			'type' => 'time',
2721
			'timeFormat' => '12',
2722
			'interval' => 10,
2723
			'selected' => '2013-05-19 01:33:00'
2724
		));
2725
		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
2726
		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
2727
		$this->assertContains('<option value="am" selected="selected">am</option>', $result);
2728
	}
2729
 
2730
/**
2731
 * test form->input() with datetime, date and time types
2732
 *
2733
 * @return void
2734
 */
2735
	public function testInputDatetime() {
2736
		extract($this->dateRegex);
2737
		$result = $this->Form->input('prueba', array(
2738
			'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
2739
			'maxYear' => date('Y') + 1, 'interval' => 15
2740
		));
2741
		$result = explode(':', $result);
2742
		$this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
2743
		$this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
2744
		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
2745
		$this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
2746
 
2747
		//related to ticket #5013
2748
		$result = $this->Form->input('Contact.date', array(
2749
			'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
2750
		));
2751
		$this->assertRegExp('/class="customClass"/', $result);
2752
		$this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
2753
 
2754
		$result = $this->Form->input('Contact.date', array(
2755
			'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
2756
		));
2757
		$this->assertRegExp('/id="customIdDay"/', $result);
2758
		$this->assertRegExp('/id="customIdMonth"/', $result);
2759
		$this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
2760
 
2761
		$result = $this->Form->input('Model.field', array(
2762
			'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
2763
		));
2764
		$this->assertRegExp('/id="customIDDay"/', $result);
2765
		$this->assertRegExp('/id="customIDHour"/', $result);
2766
		$result = explode('</select><select', $result);
2767
		$result = explode(':', $result[1]);
2768
		$this->assertRegExp('/option value="23"/', $result[0]);
2769
		$this->assertNotRegExp('/option value="24"/', $result[0]);
2770
 
2771
		$result = $this->Form->input('Model.field', array(
2772
			'type' => 'datetime', 'timeFormat' => 12
2773
		));
2774
		$result = explode('</select><select', $result);
2775
		$result = explode(':', $result[1]);
2776
		$this->assertRegExp('/option value="12"/', $result[0]);
2777
		$this->assertNotRegExp('/option value="13"/', $result[0]);
2778
 
2779
		$this->Form->request->data = array('Contact' => array('created' => null));
2780
		$result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
2781
		$expected = array(
2782
			'div' => array('class' => 'input date'),
2783
			'label' => array('for' => 'ContactCreatedMonth'),
2784
			'Created',
2785
			'/label',
2786
			array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
2787
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2788
			$monthsRegex,
2789
			'/select', '-',
2790
			array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
2791
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2792
			$daysRegex,
2793
			'/select', '-',
2794
			array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
2795
			array('option' => array('value' => '')), 'Date Unknown', '/option',
2796
			$yearsRegex,
2797
			'/select',
2798
			'/div'
2799
		);
2800
		$this->assertTags($result, $expected);
2801
 
2802
		$this->Form->request->data = array('Contact' => array('created' => null));
2803
		$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
2804
		$this->assertRegExp('/for\="ContactCreatedHour"/', $result);
2805
 
2806
		$this->Form->request->data = array('Contact' => array('created' => null));
2807
		$result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
2808
		$this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
2809
 
2810
		$result = $this->Form->input('Contact.created', array(
2811
			'type' => 'date',
2812
			'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
2813
			'timeFormat' => 'NONE'
2814
		));
2815
		$this->assertRegExp('/for\="created-month"/', $result);
2816
	}
2817
 
2818
/**
2819
 * Test generating checkboxes in a loop.
2820
 *
2821
 * @return void
2822
 */
2823
	public function testInputCheckboxesInLoop() {
2824
		for ($i = 1; $i < 5; $i++) {
2825
			$result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
2826
			$expected = array(
2827
				'div' => array('class' => 'input checkbox'),
2828
				'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
2829
				array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
2830
				'label' => array('for' => "Contact{$i}Email"),
2831
				'Email',
2832
				'/label',
2833
				'/div'
2834
			);
2835
			$this->assertTags($result, $expected);
2836
		}
2837
	}
2838
 
2839
/**
2840
 * Test generating checkboxes with disabled elements.
2841
 *
2842
 * @return void
2843
 */
2844
	public function testInputCheckboxWithDisabledElements() {
2845
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
2846
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
2847
 
2848
		$expected = array(
2849
			array('div' => array('class' => 'input select')),
2850
			array('label' => array('for' => "ContactMultiple")),
2851
			'Multiple',
2852
			'/label',
2853
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple", 'disabled' => 'disabled')),
2854
			array('div' => array('class' => 'checkbox')),
2855
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
2856
			array('label' => array('for' => "ContactMultiple1")),
2857
			'One',
2858
			'/label',
2859
			'/div',
2860
			array('div' => array('class' => 'checkbox')),
2861
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
2862
			array('label' => array('for' => "ContactMultiple2")),
2863
			'Two',
2864
			'/label',
2865
			'/div',
2866
			array('div' => array('class' => 'checkbox')),
2867
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
2868
			array('label' => array('for' => "ContactMultiple3")),
2869
			'Three',
2870
			'/label',
2871
			'/div',
2872
			'/div'
2873
		);
2874
		$this->assertTags($result, $expected);
2875
 
2876
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
2877
		$this->assertTags($result, $expected);
2878
 
2879
		$disabled = array('2', 3);
2880
 
2881
		$expected = array(
2882
			array('div' => array('class' => 'input select')),
2883
			array('label' => array('for' => "ContactMultiple")),
2884
			'Multiple',
2885
			'/label',
2886
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
2887
			array('div' => array('class' => 'checkbox')),
2888
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
2889
			array('label' => array('for' => "ContactMultiple1")),
2890
			'One',
2891
			'/label',
2892
			'/div',
2893
			array('div' => array('class' => 'checkbox')),
2894
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
2895
			array('label' => array('for' => "ContactMultiple2")),
2896
			'Two',
2897
			'/label',
2898
			'/div',
2899
			array('div' => array('class' => 'checkbox')),
2900
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
2901
			array('label' => array('for' => "ContactMultiple3")),
2902
			'Three',
2903
			'/label',
2904
			'/div',
2905
			'/div'
2906
		);
2907
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
2908
		$this->assertTags($result, $expected);
2909
 
2910
		// make sure 50 does only disable 50, and not 50f5c0cf
2911
		$options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
2912
		$disabled = array(50);
2913
 
2914
		$expected = array(
2915
			array('div' => array('class' => 'input select')),
2916
			array('label' => array('for' => "ContactMultiple")),
2917
			'Multiple',
2918
			'/label',
2919
			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
2920
			array('div' => array('class' => 'checkbox')),
2921
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
2922
			array('label' => array('for' => "ContactMultiple50")),
2923
			'Fifty',
2924
			'/label',
2925
			'/div',
2926
			array('div' => array('class' => 'checkbox')),
2927
			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
2928
			array('label' => array('for' => "ContactMultiple50f5c0cf")),
2929
			'Stringy',
2930
			'/label',
2931
			'/div',
2932
			'/div'
2933
		);
2934
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
2935
		$this->assertTags($result, $expected);
2936
	}
2937
 
2938
/**
2939
 * test input name with leading integer, ensure attributes are generated correctly.
2940
 *
2941
 * @return void
2942
 */
2943
	public function testInputWithLeadingInteger() {
2944
		$result = $this->Form->text('0.Node.title');
2945
		$expected = array(
2946
			'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
2947
		);
2948
		$this->assertTags($result, $expected);
2949
	}
2950
 
2951
/**
2952
 * test form->input() with select type inputs.
2953
 *
2954
 * @return void
2955
 */
2956
	public function testInputSelectType() {
2957
		$result = $this->Form->input('email', array(
2958
			'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
2959
		);
2960
		$expected = array(
2961
			'div' => array('class' => 'input select'),
2962
			'label' => array('for' => 'email'),
2963
			'Email',
2964
			'/label',
2965
			array('select' => array('name' => 'data[email]', 'id' => 'email')),
2966
			array('option' => array('value' => '')),
2967
			'/option',
2968
			array('option' => array('value' => 'è')),
2969
			'Firést',
2970
			'/option',
2971
			array('option' => array('value' => 'é')),
2972
			'Secoènd',
2973
			'/option',
2974
			'/select',
2975
			'/div'
2976
		);
2977
		$this->assertTags($result, $expected);
2978
 
2979
		$result = $this->Form->input('email', array(
2980
			'options' => array('First', 'Second'), 'empty' => true)
2981
		);
2982
		$expected = array(
2983
			'div' => array('class' => 'input select'),
2984
			'label' => array('for' => 'email'),
2985
			'Email',
2986
			'/label',
2987
			array('select' => array('name' => 'data[email]', 'id' => 'email')),
2988
			array('option' => array('value' => '')),
2989
			'/option',
2990
			array('option' => array('value' => '0')),
2991
			'First',
2992
			'/option',
2993
			array('option' => array('value' => '1')),
2994
			'Second',
2995
			'/option',
2996
			'/select',
2997
			'/div'
2998
		);
2999
		$this->assertTags($result, $expected);
3000
 
3001
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3002
		$this->Form->request->data = array('Model' => array('user_id' => 'value'));
3003
 
3004
		$result = $this->Form->input('Model.user_id', array('empty' => true));
3005
		$expected = array(
3006
			'div' => array('class' => 'input select'),
3007
			'label' => array('for' => 'ModelUserId'),
3008
			'User',
3009
			'/label',
3010
			'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
3011
			array('option' => array('value' => '')),
3012
			'/option',
3013
			array('option' => array('value' => 'value', 'selected' => 'selected')),
3014
			'good',
3015
			'/option',
3016
			array('option' => array('value' => 'other')),
3017
			'bad',
3018
			'/option',
3019
			'/select',
3020
			'/div'
3021
		);
3022
		$this->assertTags($result, $expected);
3023
 
3024
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3025
		$this->Form->request->data = array('Thing' => array('user_id' => null));
3026
		$result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
3027
		$expected = array(
3028
			'div' => array('class' => 'input select'),
3029
			'label' => array('for' => 'ThingUserId'),
3030
			'User',
3031
			'/label',
3032
			'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
3033
			array('option' => array('value' => '')),
3034
			'Some Empty',
3035
			'/option',
3036
			array('option' => array('value' => 'value')),
3037
			'good',
3038
			'/option',
3039
			array('option' => array('value' => 'other')),
3040
			'bad',
3041
			'/option',
3042
			'/select',
3043
			'/div'
3044
		);
3045
		$this->assertTags($result, $expected);
3046
 
3047
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3048
		$this->Form->request->data = array('Thing' => array('user_id' => 'value'));
3049
		$result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
3050
		$expected = array(
3051
			'div' => array('class' => 'input select'),
3052
			'label' => array('for' => 'ThingUserId'),
3053
			'User',
3054
			'/label',
3055
			'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
3056
			array('option' => array('value' => '')),
3057
			'Some Empty',
3058
			'/option',
3059
			array('option' => array('value' => 'value', 'selected' => 'selected')),
3060
			'good',
3061
			'/option',
3062
			array('option' => array('value' => 'other')),
3063
			'bad',
3064
			'/option',
3065
			'/select',
3066
			'/div'
3067
		);
3068
		$this->assertTags($result, $expected);
3069
 
3070
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3071
		$this->Form->request->data = array('User' => array('User' => array('value')));
3072
		$result = $this->Form->input('User.User', array('empty' => true));
3073
		$expected = array(
3074
			'div' => array('class' => 'input select'),
3075
			'label' => array('for' => 'UserUser'),
3076
			'User',
3077
			'/label',
3078
			'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
3079
			'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
3080
			array('option' => array('value' => '')),
3081
			'/option',
3082
			array('option' => array('value' => 'value', 'selected' => 'selected')),
3083
			'good',
3084
			'/option',
3085
			array('option' => array('value' => 'other')),
3086
			'bad',
3087
			'/option',
3088
			'/select',
3089
			'/div'
3090
		);
3091
		$this->assertTags($result, $expected);
3092
 
3093
		$this->Form->data = array();
3094
		$result = $this->Form->input('Publisher.id', array(
3095
				'label'		=> 'Publisher',
3096
				'type'		=> 'select',
3097
				'multiple'	=> 'checkbox',
3098
				'options'	=> array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
3099
		));
3100
		$expected = array(
3101
			array('div' => array('class' => 'input select')),
3102
				array('label' => array('for' => 'PublisherId')),
3103
				'Publisher',
3104
				'/label',
3105
				'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
3106
				array('div' => array('class' => 'checkbox')),
3107
				array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
3108
				array('label' => array('for' => 'PublisherIdValue1')),
3109
				'Label 1',
3110
				'/label',
3111
				'/div',
3112
				array('div' => array('class' => 'checkbox')),
3113
				array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
3114
				array('label' => array('for' => 'PublisherIdValue2')),
3115
				'Label 2',
3116
				'/label',
3117
				'/div',
3118
			'/div'
3119
		);
3120
		$this->assertTags($result, $expected);
3121
	}
3122
 
3123
/**
3124
 * test that input() and a non standard primary key makes a hidden input by default.
3125
 *
3126
 * @return void
3127
 */
3128
	public function testInputWithNonStandardPrimaryKeyMakesHidden() {
3129
		$this->Form->create('User');
3130
		$this->Form->fieldset = array(
3131
			'User' => array(
3132
				'fields' => array(
3133
					'model_id' => array('type' => 'integer')
3134
				),
3135
				'validates' => array(),
3136
				'key' => 'model_id'
3137
			)
3138
		);
3139
		$result = $this->Form->input('model_id');
3140
		$expected = array(
3141
			'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
3142
		);
3143
		$this->assertTags($result, $expected);
3144
	}
3145
 
3146
/**
3147
 * test that overriding the magic select type widget is possible
3148
 *
3149
 * @return void
3150
 */
3151
	public function testInputOverridingMagicSelectType() {
3152
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3153
		$result = $this->Form->input('Model.user_id', array('type' => 'text'));
3154
		$expected = array(
3155
			'div' => array('class' => 'input text'),
3156
			'label' => array('for' => 'ModelUserId'), 'User', '/label',
3157
			'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
3158
			'/div'
3159
		);
3160
		$this->assertTags($result, $expected);
3161
 
3162
		//Check that magic types still work for plural/singular vars
3163
		$this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
3164
		$result = $this->Form->input('Model.type');
3165
		$expected = array(
3166
			'div' => array('class' => 'input select'),
3167
			'label' => array('for' => 'ModelType'), 'Type', '/label',
3168
			'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
3169
			array('option' => array('value' => 'value')), 'good', '/option',
3170
			array('option' => array('value' => 'other')), 'bad', '/option',
3171
			'/select',
3172
			'/div'
3173
		);
3174
		$this->assertTags($result, $expected);
3175
	}
3176
 
3177
/**
3178
 * Test that inferred types do not override developer input
3179
 *
3180
 * @return void
3181
 */
3182
	public function testInputMagicTypeDoesNotOverride() {
3183
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3184
		$result = $this->Form->input('Model.user', array('type' => 'checkbox'));
3185
		$expected = array(
3186
			'div' => array('class' => 'input checkbox'),
3187
			array('input' => array(
3188
				'type' => 'hidden',
3189
				'name' => 'data[Model][user]',
3190
				'id' => 'ModelUser_',
3191
				'value' => 0,
3192
			)),
3193
			array('input' => array(
3194
				'name' => 'data[Model][user]',
3195
				'type' => 'checkbox',
3196
				'id' => 'ModelUser',
3197
				'value' => 1
3198
			)),
3199
			'label' => array('for' => 'ModelUser'), 'User', '/label',
3200
			'/div'
3201
		);
3202
		$this->assertTags($result, $expected);
3203
	}
3204
 
3205
/**
3206
 * Test that magic input() selects are created for type=number
3207
 *
3208
 * @return void
3209
 */
3210
	public function testInputMagicSelectForTypeNumber() {
3211
		$this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
3212
		$this->Form->request->data = array('ValidateUser' => array('balance' => 1));
3213
		$result = $this->Form->input('ValidateUser.balance');
3214
		$expected = array(
3215
			'div' => array('class' => 'input select'),
3216
			'label' => array('for' => 'ValidateUserBalance'),
3217
			'Balance',
3218
			'/label',
3219
			'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
3220
			array('option' => array('value' => '0')),
3221
			'nothing',
3222
			'/option',
3223
			array('option' => array('value' => '1', 'selected' => 'selected')),
3224
			'some',
3225
			'/option',
3226
			array('option' => array('value' => '100')),
3227
			'a lot',
3228
			'/option',
3229
			'/select',
3230
			'/div'
3231
		);
3232
		$this->assertTags($result, $expected);
3233
	}
3234
 
3235
/**
3236
 * Test that magic input() selects can easily be converted into radio types without error.
3237
 *
3238
 * @return void
3239
 */
3240
	public function testInputMagicSelectChangeToRadio() {
3241
		$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
3242
		$result = $this->Form->input('Model.user_id', array('type' => 'radio'));
3243
		$this->assertRegExp('/input type="radio"/', $result);
3244
	}
3245
 
3246
/**
3247
 * fields with the same name as the model should work.
3248
 *
3249
 * @return void
3250
 */
3251
	public function testInputWithMatchingFieldAndModelName() {
3252
		$this->Form->create('User');
3253
		$this->Form->fieldset = array(
3254
			'User' => array(
3255
				'fields' => array(
3256
					'User' => array('type' => 'text')
3257
				),
3258
				'validates' => array(),
3259
				'key' => 'id'
3260
			)
3261
		);
3262
		$this->Form->request->data['User']['User'] = 'ABC, Inc.';
3263
		$result = $this->Form->input('User', array('type' => 'text'));
3264
		$expected = array(
3265
			'div' => array('class' => 'input text'),
3266
			'label' => array('for' => 'UserUser'), 'User', '/label',
3267
			'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
3268
			'/div'
3269
		);
3270
		$this->assertTags($result, $expected);
3271
	}
3272
 
3273
/**
3274
 * testFormInputs method
3275
 *
3276
 * test correct results from form::inputs().
3277
 *
3278
 * @return void
3279
 */
3280
	public function testFormInputs() {
3281
		$this->Form->create('Contact');
3282
		$result = $this->Form->inputs('The Legend');
3283
		$expected = array(
3284
			'<fieldset',
3285
			'<legend',
3286
			'The Legend',
3287
			'/legend',
3288
			'*/fieldset',
3289
		);
3290
		$this->assertTags($result, $expected);
3291
 
3292
		$result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
3293
		$expected = array(
3294
			'fieldset' => array('class' => 'classy-stuff'),
3295
			'<legend',
3296
			'Field of Dreams',
3297
			'/legend',
3298
			'*/fieldset'
3299
		);
3300
		$this->assertTags($result, $expected);
3301
 
3302
		$result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
3303
		$this->assertTags($result, $expected);
3304
 
3305
		$result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
3306
		$this->assertTags($result, $expected);
3307
 
3308
		$this->Form->create('Contact');
3309
		$this->Form->request['prefix'] = 'admin';
3310
		$this->Form->request['action'] = 'admin_edit';
3311
		$result = $this->Form->inputs();
3312
		$expected = array(
3313
			'<fieldset',
3314
			'<legend',
3315
			'Edit Contact',
3316
			'/legend',
3317
			'*/fieldset',
3318
		);
3319
		$this->assertTags($result, $expected);
3320
 
3321
		$this->Form->create('Contact');
3322
		$result = $this->Form->inputs(false);
3323
		$expected = array(
3324
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3325
			array('div' => array('class' => 'input text')),
3326
			'*/div',
3327
			array('div' => array('class' => 'input email')),
3328
			'*/div',
3329
			array('div' => array('class' => 'input tel')),
3330
			'*/div',
3331
			array('div' => array('class' => 'input password')),
3332
			'*/div',
3333
			array('div' => array('class' => 'input date')),
3334
			'*/div',
3335
			array('div' => array('class' => 'input date')),
3336
			'*/div',
3337
			array('div' => array('class' => 'input datetime')),
3338
			'*/div',
3339
			array('div' => array('class' => 'input number')),
3340
			'*/div',
3341
			array('div' => array('class' => 'input select')),
3342
			'*/div',
3343
		);
3344
		$this->assertTags($result, $expected);
3345
 
3346
		$this->Form->create('Contact');
3347
		$result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
3348
		$expected = array(
3349
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3350
			array('div' => array('class' => 'input text')),
3351
			'*/div',
3352
			array('div' => array('class' => 'input email')),
3353
			'*/div',
3354
			array('div' => array('class' => 'input tel')),
3355
			'*/div',
3356
			array('div' => array('class' => 'input password')),
3357
			'*/div',
3358
			array('div' => array('class' => 'input date')),
3359
			'*/div',
3360
			array('div' => array('class' => 'input date')),
3361
			'*/div',
3362
			array('div' => array('class' => 'input datetime')),
3363
			'*/div',
3364
			array('div' => array('class' => 'input number')),
3365
			'*/div',
3366
			array('div' => array('class' => 'input select')),
3367
			'*/div',
3368
		);
3369
		$this->assertTags($result, $expected);
3370
 
3371
		$this->Form->create('Contact');
3372
		$result = $this->Form->inputs(null, null, array('fieldset' => false));
3373
		$this->assertTags($result, $expected);
3374
 
3375
		$this->Form->create('Contact');
3376
		$result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
3377
		$expected = array(
3378
			'fieldset' => array(),
3379
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3380
			array('div' => array('class' => 'input text')),
3381
			'*/div',
3382
			array('div' => array('class' => 'input email')),
3383
			'*/div',
3384
			array('div' => array('class' => 'input tel')),
3385
			'*/div',
3386
			array('div' => array('class' => 'input password')),
3387
			'*/div',
3388
			array('div' => array('class' => 'input date')),
3389
			'*/div',
3390
			array('div' => array('class' => 'input date')),
3391
			'*/div',
3392
			array('div' => array('class' => 'input datetime')),
3393
			'*/div',
3394
			array('div' => array('class' => 'input number')),
3395
			'*/div',
3396
			array('div' => array('class' => 'input select')),
3397
			'*/div',
3398
			'/fieldset'
3399
		);
3400
		$this->assertTags($result, $expected);
3401
 
3402
		$this->Form->create('Contact');
3403
		$result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
3404
		$expected = array(
3405
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3406
			array('div' => array('class' => 'input text')),
3407
			'*/div',
3408
			array('div' => array('class' => 'input email')),
3409
			'*/div',
3410
			array('div' => array('class' => 'input tel')),
3411
			'*/div',
3412
			array('div' => array('class' => 'input password')),
3413
			'*/div',
3414
			array('div' => array('class' => 'input date')),
3415
			'*/div',
3416
			array('div' => array('class' => 'input date')),
3417
			'*/div',
3418
			array('div' => array('class' => 'input datetime')),
3419
			'*/div',
3420
			array('div' => array('class' => 'input number')),
3421
			'*/div',
3422
			array('div' => array('class' => 'input select')),
3423
			'*/div',
3424
		);
3425
		$this->assertTags($result, $expected);
3426
 
3427
		$this->Form->create('Contact');
3428
		$result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
3429
		$this->assertTags($result, $expected);
3430
 
3431
		$this->Form->create('Contact');
3432
		$result = $this->Form->inputs('Hello');
3433
		$expected = array(
3434
			'fieldset' => array(),
3435
			'legend' => array(),
3436
			'Hello',
3437
			'/legend',
3438
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3439
			array('div' => array('class' => 'input text')),
3440
			'*/div',
3441
			array('div' => array('class' => 'input email')),
3442
			'*/div',
3443
			array('div' => array('class' => 'input tel')),
3444
			'*/div',
3445
			array('div' => array('class' => 'input password')),
3446
			'*/div',
3447
			array('div' => array('class' => 'input date')),
3448
			'*/div',
3449
			array('div' => array('class' => 'input date')),
3450
			'*/div',
3451
			array('div' => array('class' => 'input datetime')),
3452
			'*/div',
3453
			array('div' => array('class' => 'input number')),
3454
			'*/div',
3455
			array('div' => array('class' => 'input select')),
3456
			'*/div',
3457
			'/fieldset'
3458
		);
3459
		$this->assertTags($result, $expected);
3460
 
3461
		$this->Form->create('Contact');
3462
		$result = $this->Form->inputs(array('legend' => 'Hello'));
3463
		$expected = array(
3464
			'fieldset' => array(),
3465
			'legend' => array(),
3466
			'Hello',
3467
			'/legend',
3468
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
3469
			array('div' => array('class' => 'input text')),
3470
			'*/div',
3471
			array('div' => array('class' => 'input email')),
3472
			'*/div',
3473
			array('div' => array('class' => 'input tel')),
3474
			'*/div',
3475
			array('div' => array('class' => 'input password')),
3476
			'*/div',
3477
			array('div' => array('class' => 'input date')),
3478
			'*/div',
3479
			array('div' => array('class' => 'input date')),
3480
			'*/div',
3481
			array('div' => array('class' => 'input datetime')),
3482
			'*/div',
3483
			array('div' => array('class' => 'input number')),
3484
			'*/div',
3485
			array('div' => array('class' => 'input select')),
3486
			'*/div',
3487
			'/fieldset'
3488
		);
3489
		$this->assertTags($result, $expected);
3490
 
3491
		$this->Form->create('Contact');
3492
		$result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
3493
		$this->assertTags($result, $expected);
3494
		$this->Form->end();
3495
 
3496
		$this->Form->create(false);
3497
		$expected = array(
3498
			'fieldset' => array(),
3499
			array('div' => array('class' => 'input text')),
3500
			'label' => array('for' => 'foo'),
3501
			'Foo',
3502
			'/label',
3503
			'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
3504
			'*/div',
3505
			'/fieldset'
3506
		);
3507
		$result = $this->Form->inputs(
3508
			array('foo' => array('type' => 'text')),
3509
			array(),
3510
			array('legend' => false)
3511
		);
3512
		$this->assertTags($result, $expected);
3513
	}
3514
 
3515
/**
3516
 * Tests inputs() works with plugin models
3517
 *
3518
 * @return void
3519
 */
3520
	public function testInputsPluginModel() {
3521
		$this->loadFixtures('Post');
3522
		App::build(array(
3523
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
3524
		));
3525
		CakePlugin::load('TestPlugin');
3526
		$this->Form->request['models'] = array(
3527
			'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
3528
		);
3529
		$this->Form->create('TestPlugin.TestPluginPost');
3530
		$result = $this->Form->inputs();
3531
 
3532
		$this->assertContains('data[TestPluginPost][id]', $result);
3533
		$this->assertContains('data[TestPluginPost][author_id]', $result);
3534
		$this->assertContains('data[TestPluginPost][title]', $result);
3535
		$this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
3536
		$this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
3537
		$this->assertEquals('TestPluginPost', $this->Form->model());
3538
	}
3539
 
3540
/**
3541
 * testSelectAsCheckbox method
3542
 *
3543
 * test multi-select widget with checkbox formatting.
3544
 *
3545
 * @return void
3546
 */
3547
	public function testSelectAsCheckbox() {
3548
		$result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
3549
		$expected = array(
3550
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3551
			array('div' => array('class' => 'checkbox')),
3552
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
3553
			array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
3554
			'first',
3555
			'/label',
3556
			'/div',
3557
			array('div' => array('class' => 'checkbox')),
3558
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
3559
			array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
3560
			'second',
3561
			'/label',
3562
			'/div',
3563
			array('div' => array('class' => 'checkbox')),
3564
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
3565
			array('label' => array('for' => 'ModelMultiField2')),
3566
			'third',
3567
			'/label',
3568
			'/div',
3569
		);
3570
		$this->assertTags($result, $expected);
3571
 
3572
		$result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
3573
		$expected = array(
3574
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3575
			array('div' => array('class' => 'checkbox')),
3576
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
3577
			array('label' => array('for' => 'ModelMultiField12')),
3578
			'half',
3579
			'/label',
3580
			'/div',
3581
		);
3582
		$this->assertTags($result, $expected);
3583
	}
3584
 
3585
/**
3586
 * testLabel method
3587
 *
3588
 * test label generation.
3589
 *
3590
 * @return void
3591
 */
3592
	public function testLabel() {
3593
		$this->Form->text('Person.name');
3594
		$result = $this->Form->label();
3595
		$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
3596
 
3597
		$this->Form->text('Person.name');
3598
		$result = $this->Form->label();
3599
		$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
3600
 
3601
		$result = $this->Form->label('Person.first_name');
3602
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
3603
 
3604
		$result = $this->Form->label('Person.first_name', 'Your first name');
3605
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
3606
 
3607
		$result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
3608
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
3609
 
3610
		$result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
3611
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
3612
 
3613
		$result = $this->Form->label('Person.first_name', '');
3614
		$this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
3615
 
3616
		$result = $this->Form->label('Person.2.name', '');
3617
		$this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
3618
	}
3619
 
3620
/**
3621
 * testTextbox method
3622
 *
3623
 * test textbox element generation
3624
 *
3625
 * @return void
3626
 */
3627
	public function testTextbox() {
3628
		$result = $this->Form->text('Model.field');
3629
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
3630
 
3631
		$result = $this->Form->text('Model.field', array('type' => 'password'));
3632
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
3633
 
3634
		$result = $this->Form->text('Model.field', array('id' => 'theID'));
3635
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
3636
 
3637
		$this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
3638
		$result = $this->Form->text('Model.text');
3639
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
3640
 
3641
		$Contact = ClassRegistry::getObject('Contact');
3642
		$Contact->validationErrors['text'] = array(true);
3643
		$this->Form->request->data['Contact']['text'] = 'test';
3644
		$result = $this->Form->text('Contact.text', array('id' => 'theID'));
3645
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
3646
 
3647
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
3648
		$result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
3649
		$expected = array(
3650
			'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
3651
		);
3652
		$this->assertTags($result, $expected);
3653
	}
3654
 
3655
/**
3656
 * testDefaultValue method
3657
 *
3658
 * Test default value setting
3659
 *
3660
 * @return void
3661
 */
3662
	public function testDefaultValue() {
3663
		$this->Form->request->data['Model']['field'] = 'test';
3664
		$result = $this->Form->text('Model.field', array('default' => 'default value'));
3665
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
3666
 
3667
		unset($this->Form->request->data['Model']['field']);
3668
		$result = $this->Form->text('Model.field', array('default' => 'default value'));
3669
		$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
3670
	}
3671
 
3672
/**
3673
 * testCheckboxDefaultValue method
3674
 *
3675
 * Test default value setting on checkbox() method
3676
 *
3677
 * @return void
3678
 */
3679
	public function testCheckboxDefaultValue() {
3680
		$this->Form->request->data['Model']['field'] = false;
3681
		$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
3682
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
3683
 
3684
		unset($this->Form->request->data['Model']['field']);
3685
		$result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
3686
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
3687
 
3688
		$this->Form->request->data['Model']['field'] = true;
3689
		$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
3690
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
3691
 
3692
		unset($this->Form->request->data['Model']['field']);
3693
		$result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
3694
		$this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
3695
	}
3696
 
3697
/**
3698
 * testError method
3699
 *
3700
 * Test field error generation
3701
 *
3702
 * @return void
3703
 */
3704
	public function testError() {
3705
		$Contact = ClassRegistry::getObject('Contact');
3706
		$Contact->validationErrors['field'] = array(1);
3707
		$result = $this->Form->error('Contact.field');
3708
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
3709
 
3710
		$result = $this->Form->error('Contact.field', null, array('wrap' => false));
3711
		$this->assertEquals('Error in field Field', $result);
3712
 
3713
		$Contact->validationErrors['field'] = array("This field contains invalid input");
3714
		$result = $this->Form->error('Contact.field', null, array('wrap' => false));
3715
		$this->assertEquals('This field contains invalid input', $result);
3716
 
3717
		$Contact->validationErrors['field'] = array("This field contains invalid input");
3718
		$result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
3719
		$this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
3720
 
3721
		$result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
3722
		$this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
3723
 
3724
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
3725
		$this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
3726
 
3727
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
3728
		$this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
3729
 
3730
		$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
3731
		$this->assertEquals('<strong>Badness!</strong>', $result);
3732
 
3733
		$Contact->validationErrors['field'] = array("email");
3734
		$result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
3735
		$expected = array(
3736
			'div' => array('class' => 'field-error'),
3737
			'No good!',
3738
			'/div'
3739
		);
3740
		$this->assertTags($result, $expected);
3741
 
3742
		$Contact->validationErrors['field'] = array('notBlank', 'email', 'Something else');
3743
		$result = $this->Form->error('Contact.field', array(
3744
			'notBlank' => 'Cannot be empty',
3745
			'email' => 'No good!'
3746
		));
3747
		$expected = array(
3748
			'div' => array('class' => 'error-message'),
3749
				'ul' => array(),
3750
					'<li', 'Cannot be empty', '/li',
3751
					'<li', 'No good!', '/li',
3752
					'<li', 'Something else', '/li',
3753
				'/ul',
3754
			'/div'
3755
		);
3756
		$this->assertTags($result, $expected);
3757
 
3758
		// Testing error messages list options
3759
		$Contact->validationErrors['field'] = array('notBlank', 'email');
3760
 
3761
		$result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
3762
		$expected = array(
3763
			'div' => array('class' => 'error-message'),
3764
				'ol' => array(),
3765
					'<li', 'notBlank', '/li',
3766
					'<li', 'email', '/li',
3767
				'/ol',
3768
			'/div'
3769
		);
3770
		$this->assertTags($result, $expected);
3771
 
3772
		$result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
3773
		$expected = array(
3774
			'div' => array('class' => 'error-message'),
3775
				'ol' => array(),
3776
					'<li', 'notBlank', '/li',
3777
					'<li', 'email', '/li',
3778
				'/ol',
3779
			'/div'
3780
		);
3781
		$this->assertTags($result, $expected);
3782
 
3783
		$result = $this->Form->error('Contact.field', null, array(
3784
			'listOptions' => array(
3785
				'class' => 'ul-class',
3786
				'itemOptions' => array(
3787
					'class' => 'li-class'
3788
				)
3789
			)
3790
		));
3791
		$expected = array(
3792
			'div' => array('class' => 'error-message'),
3793
				'ul' => array('class' => 'ul-class'),
3794
					array('li' => array('class' => 'li-class')), 'notBlank', '/li',
3795
					array('li' => array('class' => 'li-class')), 'email', '/li',
3796
				'/ul',
3797
			'/div'
3798
		);
3799
		$this->assertTags($result, $expected);
3800
	}
3801
 
3802
/**
3803
 * test error options when using form->input();
3804
 *
3805
 * @return void
3806
 */
3807
	public function testInputErrorEscape() {
3808
		$this->Form->create('ValidateProfile');
3809
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
3810
		$ValidateProfile->validationErrors['city'] = array('required<br>');
3811
		$result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
3812
		$this->assertRegExp('/required&lt;br&gt;/', $result);
3813
 
3814
		$result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
3815
		$this->assertRegExp('/required<br>/', $result);
3816
	}
3817
 
3818
/**
3819
 * testPassword method
3820
 *
3821
 * Test password element generation
3822
 *
3823
 * @return void
3824
 */
3825
	public function testPassword() {
3826
		$Contact = ClassRegistry::getObject('Contact');
3827
		$result = $this->Form->password('Contact.field');
3828
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
3829
 
3830
		$Contact->validationErrors['passwd'] = 1;
3831
		$this->Form->request->data['Contact']['passwd'] = 'test';
3832
		$result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
3833
		$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
3834
	}
3835
 
3836
/**
3837
 * testRadio method
3838
 *
3839
 * Test radio element set generation
3840
 *
3841
 * @return void
3842
 */
3843
	public function testRadio() {
3844
		$result = $this->Form->radio('Model.field', array('option A'));
3845
		$expected = array(
3846
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3847
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3848
			'label' => array('for' => 'ModelField0'),
3849
			'option A',
3850
			'/label'
3851
		);
3852
		$this->assertTags($result, $expected);
3853
 
3854
		$result = $this->Form->radio('Model.field', array('1/2' => 'half'));
3855
		$expected = array(
3856
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3857
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
3858
			'label' => array('for' => 'ModelField12'),
3859
			'half',
3860
			'/label'
3861
		);
3862
		$this->assertTags($result, $expected);
3863
 
3864
		$result = $this->Form->radio('Model.field', array('option A', 'option B'));
3865
		$expected = array(
3866
			'fieldset' => array(),
3867
			'legend' => array(),
3868
			'Field',
3869
			'/legend',
3870
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3871
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3872
			array('label' => array('for' => 'ModelField0')),
3873
			'option A',
3874
			'/label',
3875
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
3876
			array('label' => array('for' => 'ModelField1')),
3877
			'option B',
3878
			'/label',
3879
			'/fieldset'
3880
		);
3881
		$this->assertTags($result, $expected);
3882
 
3883
		$result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
3884
		$expected = array(
3885
			'fieldset' => array(),
3886
			'legend' => array(),
3887
			'Field',
3888
			'/legend',
3889
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
3890
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
3891
			array('label' => array('for' => 'ModelField0')),
3892
			'option A',
3893
			'/label',
3894
			'br' => array(),
3895
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
3896
			array('label' => array('for' => 'ModelField1')),
3897
			'option B',
3898
			'/label',
3899
			'/fieldset'
3900
		);
3901
		$this->assertTags($result, $expected);
3902
 
3903
		$result = $this->Form->radio(
3904
			'Employee.gender',
3905
			array('male' => 'Male', 'female' => 'Female'),
3906
			array('form' => 'my-form')
3907
		);
3908
		$expected = array(
3909
			'fieldset' => array(),
3910
			'legend' => array(),
3911
			'Gender',
3912
			'/legend',
3913
			'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_', 'form' => 'my-form'),
3914
			array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')),
3915
			array('label' => array('for' => 'EmployeeGenderMale')),
3916
			'Male',
3917
			'/label',
3918
			array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale', 'form' => 'my-form')),
3919
			array('label' => array('for' => 'EmployeeGenderFemale')),
3920
			'Female',
3921
			'/label',
3922
			'/fieldset',
3923
		);
3924
		$this->assertTags($result, $expected);
3925
 
3926
		$result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
3927
		$expected = array(
3928
			'fieldset' => array(),
3929
			'legend' => array(),
3930
			'Gender',
3931
			'/legend',
3932
			'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
3933
			array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
3934
			array('label' => array('for' => 'OfficerGenderMale')),
3935
			'Male',
3936
			'/label',
3937
			array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
3938
			array('label' => array('for' => 'OfficerGenderFemale')),
3939
			'Female',
3940
			'/label',
3941
			'/fieldset',
3942
		);
3943
		$this->assertTags($result, $expected);
3944
 
3945
		$result = $this->Form->radio('Contact.1.imrequired', array('option A'));
3946
		$expected = array(
3947
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
3948
			array('input' => array(
3949
				'type' => 'radio',
3950
				'name' => 'data[Contact][1][imrequired]',
3951
				'value' => '0',
3952
				'id' => 'Contact1Imrequired0',
3953
				'required' => 'required'
3954
			)),
3955
			'label' => array('for' => 'Contact1Imrequired0'),
3956
			'option A',
3957
			'/label'
3958
		);
3959
		$this->assertTags($result, $expected);
3960
 
3961
		$result = $this->Form->radio('Model.1.field', array('option A'));
3962
		$expected = array(
3963
			'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
3964
			array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
3965
			'label' => array('for' => 'Model1Field0'),
3966
			'option A',
3967
			'/label'
3968
		);
3969
		$this->assertTags($result, $expected);
3970
 
3971
		$result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
3972
		$expected = array(
3973
			'fieldset' => array(),
3974
			'legend' => array(),
3975
			'Field',
3976
			'/legend',
3977
			'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
3978
			array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
3979
			array('label' => array('for' => 'ModelField0')),
3980
			'option A',
3981
			'/label',
3982
			array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
3983
			array('label' => array('for' => 'ModelField1')),
3984
			'option B',
3985
			'/label',
3986
			'/fieldset'
3987
		);
3988
		$this->assertTags($result, $expected);
3989
 
3990
		$result = $this->Form->radio(
3991
			'Model.field',
3992
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
3993
		);
3994
		$expected = array(
3995
			'fieldset' => array(),
3996
			'legend' => array(),
3997
			'Field',
3998
			'/legend',
3999
			'input' => array(
4000
				'type' => 'hidden', 'name' => 'data[Model][field]',
4001
				'id' => 'ModelField_', 'value' => '',
4002
			),
4003
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
4004
				'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
4005
			array('label' => array('for' => 'ModelFieldAB')),
4006
			'first',
4007
			'/label',
4008
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
4009
				'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
4010
			array('label' => array('for' => 'ModelFieldAB1')),
4011
			'second',
4012
			'/label',
4013
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
4014
				'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
4015
			array('label' => array('for' => 'ModelFieldAB2')),
4016
			'third',
4017
			'/label',
4018
			'/fieldset'
4019
		);
4020
		$this->assertTags($result, $expected);
4021
 
4022
		$result = $this->Form->radio(
4023
			'Model.multibyte',
4024
			array('男性' => '男性')
4025
		);
4026
		$expected = array(
4027
			'input' => array(
4028
				'type' => 'hidden', 'name' => 'data[Model][multibyte]',
4029
				'id' => 'ModelMultibyte_', 'value' => '',
4030
			),
4031
			array('input' => array(
4032
				'type' => 'radio', 'name' => 'data[Model][multibyte]',
4033
				'id' => 'ModelMultibyte男性', 'value' => '男性')
4034
			),
4035
			array('label' => array('for' => 'ModelMultibyte男性')),
4036
			'男性',
4037
			'/label',
4038
		);
4039
		$this->assertTags($result, $expected);
4040
	}
4041
 
4042
/**
4043
 * testRadioDifferentModel
4044
 * Refs #2911
4045
 *
4046
 * @return void
4047
 */
4048
	public function testRadioDifferentModel() {
4049
		$this->Form->create('User');
4050
 
4051
		$result = $this->Form->radio(
4052
			'Model.field',
4053
			array('v1' => 'option A', 'v2' => 'option B'),
4054
			array('label' => true, 'legend' => false, 'value' => false)
4055
		);
4056
		$expected = array(
4057
			array('input' => array(
4058
				'type' => 'radio', 'name' => 'data[Model][field]',
4059
				'value' => 'v1', 'id' => 'ModelFieldV1'
4060
			)),
4061
			array('label' => array('for' => 'ModelFieldV1')),
4062
			'option A',
4063
			'/label',
4064
			array('input' => array(
4065
				'type' => 'radio', 'name' => 'data[Model][field]',
4066
				'value' => 'v2', 'id' => 'ModelFieldV2'
4067
			)),
4068
			array('label' => array('for' => 'ModelFieldV2')),
4069
			'option B',
4070
			'/label'
4071
		);
4072
		$this->assertTags($result, $expected);
4073
	}
4074
 
4075
/**
4076
 * Test radio inputs with between as string or array. Also ensure
4077
 * that an array with less between elements works.
4078
 *
4079
 * @return void
4080
 */
4081
	public function testRadioBetween() {
4082
		$result = $this->Form->radio(
4083
			'Model.field',
4084
			array('option A', 'option B'),
4085
			array('between' => 'I am between')
4086
		);
4087
		$expected = array(
4088
			'fieldset' => array(),
4089
			'legend' => array(),
4090
			'Field',
4091
			'/legend',
4092
			'I am between',
4093
			'input' => array(
4094
				'type' => 'hidden', 'name' => 'data[Model][field]',
4095
				'value' => '', 'id' => 'ModelField_'
4096
			),
4097
			array('input' => array(
4098
				'type' => 'radio', 'name' => 'data[Model][field]',
4099
				'value' => '0', 'id' => 'ModelField0'
4100
			)),
4101
			array('label' => array('for' => 'ModelField0')),
4102
			'option A',
4103
			'/label',
4104
			array('input' => array(
4105
				'type' => 'radio', 'name' => 'data[Model][field]',
4106
				'value' => '1', 'id' => 'ModelField1'
4107
			)),
4108
			array('label' => array('for' => 'ModelField1')),
4109
			'option B',
4110
			'/label',
4111
			'/fieldset'
4112
		);
4113
		$this->assertTags($result, $expected);
4114
 
4115
		$result = $this->Form->radio(
4116
			'Model.field',
4117
			array('option A', 'option B', 'option C'),
4118
			array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
4119
		);
4120
 
4121
		$expected = array(
4122
			'fieldset' => array(),
4123
			'legend' => array(),
4124
			'Field',
4125
			'/legend',
4126
			'input' => array(
4127
				'type' => 'hidden', 'name' => 'data[Model][field]',
4128
				'value' => '', 'id' => 'ModelField_'
4129
			),
4130
			array('input' => array(
4131
				'type' => 'radio', 'name' => 'data[Model][field]',
4132
				'value' => '0', 'id' => 'ModelField0'
4133
			)),
4134
			array('label' => array('for' => 'ModelField0')),
4135
			'option A',
4136
			'/label',
4137
			'between A',
4138
			'--separator--',
4139
			array('input' => array(
4140
				'type' => 'radio', 'name' => 'data[Model][field]',
4141
				'value' => '1', 'id' => 'ModelField1'
4142
			)),
4143
			array('label' => array('for' => 'ModelField1')),
4144
			'option B',
4145
			'/label',
4146
			'between B',
4147
			'--separator--',
4148
			array('input' => array(
4149
				'type' => 'radio', 'name' => 'data[Model][field]',
4150
				'value' => '2', 'id' => 'ModelField2'
4151
			)),
4152
			array('label' => array('for' => 'ModelField2')),
4153
			'option C',
4154
			'/label',
4155
			'between C',
4156
			'/fieldset'
4157
		);
4158
		$this->assertTags($result, $expected);
4159
 
4160
		$result = $this->Form->input('Model.field', array(
4161
			'options' => array('1' => 'first', '2' => 'second'),
4162
			'type' => 'radio',
4163
			'before' => '--before--',
4164
			'after' => '--after--',
4165
			'separator' => '--separator--',
4166
			'between' => array('--between first--', '--between second--')
4167
		));
4168
 
4169
		$expected = array(
4170
			'div' => array('class' => 'input radio'),
4171
			'--before--',
4172
			'fieldset' => array(),
4173
			'legend' => array(),
4174
			'Field',
4175
			'/legend',
4176
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4177
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4178
			array('label' => array('for' => 'ModelField1')),
4179
			'first',
4180
			'/label',
4181
			'--between first--',
4182
			'--separator--',
4183
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
4184
			array('label' => array('for' => 'ModelField2')),
4185
			'second',
4186
			'/label',
4187
			'--between second--',
4188
			'/fieldset',
4189
			'--after--',
4190
			'/div'
4191
		);
4192
		$this->assertTags($result, $expected);
4193
 
4194
		$result = $this->Form->input('Model.field', array(
4195
			'options' => array('1' => 'first', '2' => 'second'),
4196
			'type' => 'radio',
4197
			'before' => '--before--',
4198
			'after' => '--after--',
4199
			'separator' => '--separator--',
4200
			'between' => array('--between first--')
4201
		));
4202
 
4203
		$expected = array(
4204
			'div' => array('class' => 'input radio'),
4205
			'--before--',
4206
			'fieldset' => array(),
4207
			'legend' => array(),
4208
			'Field',
4209
			'/legend',
4210
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4211
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4212
			array('label' => array('for' => 'ModelField1')),
4213
			'first',
4214
			'/label',
4215
			'--between first--',
4216
			'--separator--',
4217
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
4218
			array('label' => array('for' => 'ModelField2')),
4219
			'second',
4220
			'/label',
4221
			'/fieldset',
4222
			'--after--',
4223
			'/div'
4224
		);
4225
		$this->assertTags($result, $expected);
4226
	}
4227
 
4228
/**
4229
 * Test that radios with a 0 value are selected under the correct conditions.
4230
 * Also ensure that values that are booleanish are handled correctly.
4231
 *
4232
 * @return void
4233
 */
4234
	public function testRadioOptionWithBooleanishValues() {
4235
		$expected = array(
4236
			'fieldset' => array(),
4237
			'legend' => array(),
4238
			'Field',
4239
			'/legend',
4240
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4241
			array('label' => array('for' => 'ModelField1')),
4242
			'Yes',
4243
			'/label',
4244
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
4245
			array('label' => array('for' => 'ModelField0')),
4246
			'No',
4247
			'/label',
4248
			'/fieldset'
4249
		);
4250
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
4251
		$this->assertTags($result, $expected);
4252
 
4253
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
4254
		$this->assertTags($result, $expected);
4255
 
4256
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
4257
		$this->assertTags($result, $expected);
4258
 
4259
		$expected = array(
4260
			'fieldset' => array(),
4261
			'legend' => array(),
4262
			'Field',
4263
			'/legend',
4264
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
4265
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4266
			array('label' => array('for' => 'ModelField1')),
4267
			'Yes',
4268
			'/label',
4269
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
4270
			array('label' => array('for' => 'ModelField0')),
4271
			'No',
4272
			'/label',
4273
			'/fieldset'
4274
		);
4275
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
4276
		$this->assertTags($result, $expected);
4277
 
4278
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
4279
		$this->assertTags($result, $expected);
4280
 
4281
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
4282
		$this->assertTags($result, $expected);
4283
 
4284
		$expected = array(
4285
			'fieldset' => array(),
4286
			'legend' => array(),
4287
			'Field',
4288
			'/legend',
4289
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
4290
			array('label' => array('for' => 'ModelField1')),
4291
			'Yes',
4292
			'/label',
4293
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
4294
			array('label' => array('for' => 'ModelField0')),
4295
			'No',
4296
			'/label',
4297
			'/fieldset'
4298
		);
4299
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
4300
		$this->assertTags($result, $expected);
4301
 
4302
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
4303
		$this->assertTags($result, $expected);
4304
 
4305
		$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
4306
		$this->assertTags($result, $expected);
4307
	}
4308
 
4309
/**
4310
 * test disabled radio options
4311
 *
4312
 * @return void
4313
 */
4314
	public function testRadioDisabled() {
4315
		$result = $this->Form->radio(
4316
			'Model.field',
4317
			array('option A', 'option B'),
4318
			array('disabled' => array(0), 'value' => '0')
4319
		);
4320
		$expected = array(
4321
			'fieldset' => array(),
4322
			'legend' => array(),
4323
			'Field',
4324
			'/legend',
4325
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4326
			array('label' => array('for' => 'ModelField0')),
4327
			'option A',
4328
			'/label',
4329
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4330
			array('label' => array('for' => 'ModelField1')),
4331
			'option B',
4332
			'/label',
4333
			'/fieldset'
4334
		);
4335
		$this->assertTags($result, $expected);
4336
 
4337
		$result = $this->Form->radio(
4338
			'Model.field',
4339
			array('option A', 'option B'),
4340
			array('disabled' => true, 'value' => '0')
4341
		);
4342
		$expected = array(
4343
			'fieldset' => array(),
4344
			'legend' => array(),
4345
			'Field',
4346
			'/legend',
4347
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4348
			array('label' => array('for' => 'ModelField0')),
4349
			'option A',
4350
			'/label',
4351
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
4352
			array('label' => array('for' => 'ModelField1')),
4353
			'option B',
4354
			'/label',
4355
			'/fieldset'
4356
		);
4357
		$this->assertTags($result, $expected);
4358
 
4359
		$result = $this->Form->radio(
4360
			'Model.field',
4361
			array('option A', 'option B'),
4362
			array('disabled' => 'disabled', 'value' => '0')
4363
		);
4364
		$expected = array(
4365
			'fieldset' => array(),
4366
			'legend' => array(),
4367
			'Field',
4368
			'/legend',
4369
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
4370
			array('label' => array('for' => 'ModelField0')),
4371
			'option A',
4372
			'/label',
4373
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
4374
			array('label' => array('for' => 'ModelField1')),
4375
			'option B',
4376
			'/label',
4377
			'/fieldset'
4378
		);
4379
		$this->assertTags($result, $expected);
4380
 
4381
		$result = $this->Form->input('Model.field', array(
4382
			'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
4383
			'type' => 'radio',
4384
			'disabled' => array(2, '3x'),
4385
		));
4386
 
4387
		$expected = array(
4388
			'div' => array('class' => 'input radio'),
4389
			'fieldset' => array(),
4390
			'legend' => array(),
4391
			'Field',
4392
			'/legend',
4393
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4394
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
4395
			array('label' => array('for' => 'ModelField1')),
4396
			'first',
4397
			'/label',
4398
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
4399
			array('label' => array('for' => 'ModelField2')),
4400
			'second',
4401
			'/label',
4402
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
4403
			array('label' => array('for' => 'ModelField2x')),
4404
			'2x',
4405
			'/label',
4406
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
4407
			array('label' => array('for' => 'ModelField3')),
4408
			'third',
4409
			'/label',
4410
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
4411
			array('label' => array('for' => 'ModelField3x')),
4412
			'3x',
4413
			'/label',
4414
			'/fieldset',
4415
			'/div'
4416
		);
4417
		$this->assertTags($result, $expected);
4418
 
4419
		$result = $this->Form->input('Model.field', array(
4420
			'type' => 'radio',
4421
			'options' => array(
4422
				1 => 'A',
4423
				2 => 'B',
4424
				3 => 'C'
4425
			),
4426
			'disabled' => array(1)
4427
		));
4428
 
4429
		$expected = array(
4430
			'div' => array('class' => 'input radio'),
4431
			'fieldset' => array(),
4432
			'legend' => array(),
4433
			'Field',
4434
			'/legend',
4435
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4436
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
4437
			array('label' => array('for' => 'ModelField1')),
4438
			'A',
4439
			'/label',
4440
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
4441
			array('label' => array('for' => 'ModelField2')),
4442
			'B',
4443
			'/label',
4444
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
4445
			array('label' => array('for' => 'ModelField3')),
4446
			'C',
4447
			'/label',
4448
			'/fieldset',
4449
			'/div'
4450
		);
4451
		$this->assertTags($result, $expected);
4452
	}
4453
 
4454
/**
4455
 * test disabling the hidden input for radio buttons
4456
 *
4457
 * @return void
4458
 */
4459
	public function testRadioHiddenInputDisabling() {
4460
		$result = $this->Form->input('Model.1.field', array(
4461
				'type' => 'radio',
4462
				'options' => array('option A'),
4463
				'hiddenField' => false
4464
			)
4465
		);
4466
		$expected = array(
4467
			'div' => array('class' => 'input radio'),
4468
			'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
4469
			'label' => array('for' => 'Model1Field0'),
4470
			'option A',
4471
			'/label',
4472
			'/div'
4473
		);
4474
		$this->assertTags($result, $expected);
4475
 
4476
		$result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
4477
		$expected = array(
4478
			'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
4479
			'label' => array('for' => 'Model1Field0'),
4480
			'option A',
4481
			'/label'
4482
		);
4483
		$this->assertTags($result, $expected);
4484
	}
4485
 
4486
/**
4487
 * test adding an empty option for radio buttons
4488
 *
4489
 * @return void
4490
 */
4491
	public function testRadioAddEmptyOption() {
4492
		$result = $this->Form->input('Model.1.field', array(
4493
			'type' => 'radio',
4494
			'options' => array('option A'),
4495
			'empty' => true,
4496
			'hiddenField' => false
4497
		));
4498
		$expected = array(
4499
			'div' => array('class' => 'input radio'),
4500
				'fieldset' => array(),
4501
					'legend' => array(),
4502
						'Field',
4503
					'/legend',
4504
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
4505
					array('label' => array('for' => 'Model1Field')),
4506
						__('empty'),
4507
					'/label',
4508
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
4509
					array('label' => array('for' => 'Model1Field0')),
4510
						'option A',
4511
					'/label',
4512
				'/fieldset',
4513
			'/div'
4514
		);
4515
		$this->assertTags($result, $expected);
4516
 
4517
		$result = $this->Form->input('Model.1.field', array(
4518
			'type' => 'radio',
4519
			'options' => array('option A'),
4520
			'empty' => 'CustomEmptyLabel',
4521
			'hiddenField' => false
4522
		));
4523
		$expected = array(
4524
			'div' => array('class' => 'input radio'),
4525
				'fieldset' => array(),
4526
					'legend' => array(),
4527
						'Field',
4528
					'/legend',
4529
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
4530
					array('label' => array('for' => 'Model1Field')),
4531
						'CustomEmptyLabel',
4532
					'/label',
4533
					array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
4534
					array('label' => array('for' => 'Model1Field0')),
4535
						'option A',
4536
					'/label',
4537
				'/fieldset',
4538
			'/div'
4539
		);
4540
		$this->assertTags($result, $expected);
4541
 
4542
		$result = $this->Form->input('Model.1.field', array(
4543
			'type' => 'radio',
4544
			'options' => array('option A'),
4545
			'empty' => false,
4546
			'hiddenField' => false
4547
		));
4548
		$this->assertTextNotContains('"Model1Field"', $result);
4549
	}
4550
 
4551
/**
4552
 * Test that radio() accepts a deep array for options
4553
 *
4554
 * @return void
4555
 */
4556
	public function testRadioOptionsArray() {
4557
		$result = $this->Form->input('Model.field', array(
4558
			'type' => 'radio',
4559
			'legend' => false,
4560
			'div' => false,
4561
			'options' => array(
4562
				'1' => array('name' => 'Option A', 'title' => 'A Title'),
4563
				'2' => array('name' => 'Option B', 'data-foo' => 'bar'))
4564
		));
4565
		$expected = array(
4566
			array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
4567
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'value' => '1', 'title' => 'A Title')),
4568
			array('label' => array('for' => 'ModelField1')),
4569
			'Option A',
4570
			'/label',
4571
			array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2', 'data-foo' => 'bar')),
4572
			array('label' => array('for' => 'ModelField2')),
4573
			'Option B',
4574
			'/label'
4575
		);
4576
		$this->assertTags($result, $expected);
4577
	}
4578
 
4579
/**
4580
 * Test that radio() accepts an array for label
4581
 *
4582
 * @return void
4583
 */
4584
	public function testRadioLabelArray() {
4585
		$result = $this->Form->input('Model.field', array(
4586
			'type' => 'radio',
4587
			'legend' => false,
4588
			'label' => array(
4589
				'class' => 'checkbox float-left',
4590
			),
4591
			'options' => array('1' => 'Option A', '2' => 'Option B.')
4592
		));
4593
		$this->assertTextContains(
4594
			'<label for="ModelField1" class="checkbox float-left">Option A</label>',
4595
			$result
4596
		);
4597
	}
4598
 
4599
/**
4600
 * Test that label id's match the input element id's when radio is called after create().
4601
 *
4602
 * @return void
4603
 */
4604
	public function testRadioWithCreate() {
4605
		$this->Form->create('Model');
4606
		$result = $this->Form->radio('recipient',
4607
			array('1' => '1', '2' => '2', '3' => '3'),
4608
			array('legend' => false, 'value' => '1')
4609
		);
4610
		$this->assertTextNotContains(
4611
			'<label for="ModelModelRecipient1">1</label>',
4612
			$result
4613
		);
4614
		$this->assertTextContains(
4615
			'<label for="ModelRecipient1">1</label>',
4616
			$result
4617
		);
4618
	}
4619
 
4620
/**
4621
 * testDomIdSuffix method
4622
 *
4623
 * @return void
4624
 */
4625
	public function testDomIdSuffix() {
4626
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
4627
		$this->assertEquals('1StringWith1DollarSigns', $result);
4628
 
4629
		$result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
4630
		$this->assertEquals('AbcXFooYBar', $result);
4631
 
4632
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
4633
		$this->assertEquals('1StringWith1$-dollarSigns', $result);
4634
 
4635
		$result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
4636
		$this->assertEquals('AbcX=FooY=Bar', $result);
4637
	}
4638
 
4639
/**
4640
 * testDomIdSuffixCollisionResolvement()
4641
 *
4642
 * @return void
4643
 */
4644
	public function testDomIdSuffixCollisionResolvement() {
4645
		$result = $this->Form->domIdSuffix('a>b');
4646
		$this->assertEquals('AB', $result);
4647
 
4648
		$result = $this->Form->domIdSuffix('a<b');
4649
		$this->assertEquals('AB1', $result);
4650
 
4651
		$result = $this->Form->domIdSuffix('a\'b');
4652
		$this->assertEquals('AB2', $result);
4653
 
4654
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4655
		$this->assertEquals('1StringWith1Dollar', $result);
4656
 
4657
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4658
		$this->assertEquals('1StringWith1Dollar1', $result);
4659
 
4660
		$result = $this->Form->domIdSuffix('1 string with 1$-dollar');
4661
		$this->assertEquals('1StringWith1Dollar2', $result);
4662
	}
4663
 
4664
/**
4665
 * testSelect method
4666
 *
4667
 * Test select element generation.
4668
 *
4669
 * @return void
4670
 */
4671
	public function testSelect() {
4672
		$result = $this->Form->select('Model.field', array());
4673
		$expected = array(
4674
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4675
			array('option' => array('value' => '')),
4676
			'/option',
4677
			'/select'
4678
		);
4679
		$this->assertTags($result, $expected);
4680
 
4681
		$this->Form->request->data = array('Model' => array('field' => 'value'));
4682
		$result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
4683
		$expected = array(
4684
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4685
			array('option' => array('value' => '')),
4686
			'/option',
4687
			array('option' => array('value' => 'value', 'selected' => 'selected')),
4688
			'good',
4689
			'/option',
4690
			array('option' => array('value' => 'other')),
4691
			'bad',
4692
			'/option',
4693
			'/select'
4694
		);
4695
		$this->assertTags($result, $expected);
4696
 
4697
		$this->Form->request->data = array();
4698
		$result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
4699
		$expected = array(
4700
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4701
			array('option' => array('value' => '')),
4702
			'/option',
4703
			array('option' => array('value' => 'value')),
4704
			'good',
4705
			'/option',
4706
			array('option' => array('value' => 'other')),
4707
			'bad',
4708
			'/option',
4709
			'/select'
4710
		);
4711
		$this->assertTags($result, $expected);
4712
 
4713
		$result = $this->Form->select(
4714
			'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
4715
			array('empty' => false)
4716
		);
4717
		$expected = array(
4718
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4719
			array('option' => array('value' => 'first')),
4720
			'first &quot;html&quot; &lt;chars&gt;',
4721
			'/option',
4722
			array('option' => array('value' => 'second')),
4723
			'value',
4724
			'/option',
4725
			'/select'
4726
		);
4727
		$this->assertTags($result, $expected);
4728
 
4729
		$result = $this->Form->select(
4730
			'Model.field',
4731
			array('first' => 'first "html" <chars>', 'second' => 'value'),
4732
			array('escape' => false, 'empty' => false)
4733
		);
4734
		$expected = array(
4735
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4736
			array('option' => array('value' => 'first')),
4737
			'first "html" <chars>',
4738
			'/option',
4739
			array('option' => array('value' => 'second')),
4740
			'value',
4741
			'/option',
4742
			'/select'
4743
		);
4744
		$this->assertTags($result, $expected);
4745
 
4746
		$options = array(
4747
			array('value' => 'first', 'name' => 'First'),
4748
			array('value' => 'first', 'name' => 'Another First'),
4749
		);
4750
		$result = $this->Form->select(
4751
			'Model.field',
4752
			$options,
4753
			array('escape' => false, 'empty' => false)
4754
		);
4755
		$expected = array(
4756
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4757
			array('option' => array('value' => 'first')),
4758
			'First',
4759
			'/option',
4760
			array('option' => array('value' => 'first')),
4761
			'Another First',
4762
			'/option',
4763
			'/select'
4764
		);
4765
		$this->assertTags($result, $expected);
4766
 
4767
		$this->Form->request->data = array('Model' => array('contact_id' => 228));
4768
		$result = $this->Form->select(
4769
			'Model.contact_id',
4770
			array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
4771
			array('escape' => false, 'empty' => 'pick something')
4772
		);
4773
 
4774
		$expected = array(
4775
			'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
4776
			array('option' => array('value' => '')), 'pick something', '/option',
4777
			array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
4778
			array('option' => array('value' => '228-1')), '228-1 value', '/option',
4779
			array('option' => array('value' => '228-2')), '228-2 value', '/option',
4780
			'/select'
4781
		);
4782
		$this->assertTags($result, $expected);
4783
 
4784
		$this->Form->request->data['Model']['field'] = 0;
4785
		$result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
4786
		$expected = array(
4787
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4788
			array('option' => array('value' => '')), '/option',
4789
			array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
4790
			array('option' => array('value' => '1')), 'Yes', '/option',
4791
			'/select'
4792
		);
4793
		$this->assertTags($result, $expected);
4794
 
4795
		$this->Form->request->data['Model']['field'] = 50;
4796
		$result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
4797
		$expected = array(
4798
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4799
			array('option' => array('value' => '')), '/option',
4800
			array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
4801
			array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
4802
			'/select'
4803
		);
4804
		$this->assertTags($result, $expected);
4805
 
4806
		$result = $this->Form->select('Contact.required_one', array('option A'));
4807
		$expected = array(
4808
			'select' => array(
4809
				'name' => 'data[Contact][required_one]',
4810
				'id' => 'ContactRequiredOne',
4811
				'required' => 'required'
4812
			),
4813
			array('option' => array('value' => '')), '/option',
4814
			array('option' => array('value' => '0')), 'option A', '/option',
4815
			'/select'
4816
		);
4817
		$this->assertTags($result, $expected);
4818
 
4819
		$result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
4820
		$expected = array(
4821
			'select' => array(
4822
				'name' => 'data[Contact][required_one]',
4823
				'id' => 'ContactRequiredOne',
4824
				'disabled' => 'disabled'
4825
			),
4826
			array('option' => array('value' => '')), '/option',
4827
			array('option' => array('value' => '0')), 'option A', '/option',
4828
			'/select'
4829
		);
4830
		$this->assertTags($result, $expected);
4831
	}
4832
 
4833
/**
4834
 * test that select() with optiongroups listens to the escape param.
4835
 *
4836
 * @return void
4837
 */
4838
	public function testSelectOptionGroupEscaping() {
4839
		$options = array(
4840
			'>< Key' => array(
4841
				1 => 'One',
4842
				2 => 'Two'
4843
			)
4844
		);
4845
		$result = $this->Form->select('Model.field', $options, array('empty' => false));
4846
		$expected = array(
4847
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4848
			'optgroup' => array('label' => '&gt;&lt; Key'),
4849
			array('option' => array('value' => '1')), 'One', '/option',
4850
			array('option' => array('value' => '2')), 'Two', '/option',
4851
			'/optgroup',
4852
			'/select'
4853
		);
4854
		$this->assertTags($result, $expected);
4855
 
4856
		$options = array(
4857
			'>< Key' => array(
4858
				1 => 'One',
4859
				2 => 'Two'
4860
			)
4861
		);
4862
		$result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
4863
		$expected = array(
4864
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4865
			'optgroup' => array('label' => '>< Key'),
4866
			array('option' => array('value' => '1')), 'One', '/option',
4867
			array('option' => array('value' => '2')), 'Two', '/option',
4868
			'/optgroup',
4869
			'/select'
4870
		);
4871
		$this->assertTags($result, $expected);
4872
	}
4873
 
4874
/**
4875
 * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
4876
 *
4877
 * @return void
4878
 */
4879
	public function testSelectWithNullAttributes() {
4880
		$result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
4881
		$expected = array(
4882
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4883
			array('option' => array('value' => '0')),
4884
			'first',
4885
			'/option',
4886
			array('option' => array('value' => '1')),
4887
			'second',
4888
			'/option',
4889
			'/select'
4890
		);
4891
		$this->assertTags($result, $expected);
4892
	}
4893
 
4894
/**
4895
 * testNestedSelect method
4896
 *
4897
 * test select element generation with optgroups
4898
 *
4899
 * @return void
4900
 */
4901
	public function testNestedSelect() {
4902
		$result = $this->Form->select(
4903
			'Model.field',
4904
			array(1 => 'One', 2 => 'Two', 'Three' => array(
4905
				3 => 'Three', 4 => 'Four', 5 => 'Five'
4906
			)), array('empty' => false)
4907
		);
4908
		$expected = array(
4909
			'select' => array('name' => 'data[Model][field]',
4910
					'id' => 'ModelField'),
4911
					array('option' => array('value' => 1)),
4912
					'One',
4913
					'/option',
4914
					array('option' => array('value' => 2)),
4915
					'Two',
4916
					'/option',
4917
					array('optgroup' => array('label' => 'Three')),
4918
						array('option' => array('value' => 4)),
4919
						'Four',
4920
						'/option',
4921
						array('option' => array('value' => 5)),
4922
						'Five',
4923
						'/option',
4924
					'/optgroup',
4925
					'/select'
4926
					);
4927
		$this->assertTags($result, $expected);
4928
 
4929
		$result = $this->Form->select(
4930
			'Model.field',
4931
			array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
4932
			array('showParents' => true, 'empty' => false)
4933
		);
4934
 
4935
		$expected = array(
4936
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
4937
				array('option' => array('value' => 1)),
4938
				'One',
4939
				'/option',
4940
				array('option' => array('value' => 2)),
4941
				'Two',
4942
				'/option',
4943
				array('optgroup' => array('label' => 'Three')),
4944
					array('option' => array('value' => 3)),
4945
					'Three',
4946
					'/option',
4947
					array('option' => array('value' => 4)),
4948
					'Four',
4949
					'/option',
4950
				'/optgroup',
4951
			'/select'
4952
		);
4953
		$this->assertTags($result, $expected);
4954
	}
4955
 
4956
/**
4957
 * testSelectMultiple method
4958
 *
4959
 * test generation of multiple select elements
4960
 *
4961
 * @return void
4962
 */
4963
	public function testSelectMultiple() {
4964
		$options = array('first', 'second', 'third');
4965
		$result = $this->Form->select(
4966
			'Model.multi_field', $options, array('multiple' => true)
4967
		);
4968
		$expected = array(
4969
			'input' => array(
4970
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
4971
			),
4972
			'select' => array(
4973
				'name' => 'data[Model][multi_field][]',
4974
				'id' => 'ModelMultiField', 'multiple' => 'multiple'
4975
			),
4976
			array('option' => array('value' => '0')),
4977
			'first',
4978
			'/option',
4979
			array('option' => array('value' => '1')),
4980
			'second',
4981
			'/option',
4982
			array('option' => array('value' => '2')),
4983
			'third',
4984
			'/option',
4985
			'/select'
4986
		);
4987
		$this->assertTags($result, $expected);
4988
 
4989
		$result = $this->Form->select(
4990
			'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple')
4991
		);
4992
		$expected = array(
4993
			'input' => array(
4994
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_',
4995
				'form' => 'my-form',
4996
			),
4997
			'select' => array(
4998
				'name' => 'data[Model][multi_field][]',
4999
				'id' => 'ModelMultiField',
5000
				'multiple' => 'multiple',
5001
				'form' => 'my-form',
5002
			),
5003
			array('option' => array('value' => '0')),
5004
			'first',
5005
			'/option',
5006
			array('option' => array('value' => '1')),
5007
			'second',
5008
			'/option',
5009
			array('option' => array('value' => '2')),
5010
			'third',
5011
			'/option',
5012
			'/select'
5013
		);
5014
		$this->assertTags($result, $expected);
5015
 
5016
		$result = $this->Form->select(
5017
			'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
5018
		);
5019
		$expected = array(
5020
			'input' => array(
5021
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
5022
			),
5023
			'select' => array(
5024
				'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
5025
				'multiple' => 'multiple'
5026
			),
5027
			array('option' => array('value' => '0', 'selected' => 'selected')),
5028
			'first',
5029
			'/option',
5030
			array('option' => array('value' => '1', 'selected' => 'selected')),
5031
			'second',
5032
			'/option',
5033
			array('option' => array('value' => '2')),
5034
			'third',
5035
			'/option',
5036
			'/select'
5037
		);
5038
		$this->assertTags($result, $expected);
5039
 
5040
		$result = $this->Form->select(
5041
			'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
5042
		);
5043
		$expected = array(
5044
			'select' => array(
5045
				'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
5046
			),
5047
			array('option' => array('value' => '0', 'selected' => 'selected')),
5048
			'first',
5049
			'/option',
5050
			array('option' => array('value' => '1', 'selected' => 'selected')),
5051
			'second',
5052
			'/option',
5053
			array('option' => array('value' => '2')),
5054
			'third',
5055
			'/option',
5056
			'/select'
5057
		);
5058
		$this->assertTags($result, $expected);
5059
 
5060
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5061
		$selected = array('2', '3x');
5062
		$result = $this->Form->select(
5063
			'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
5064
		);
5065
		$expected = array(
5066
			'input' => array(
5067
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
5068
			),
5069
			'select' => array(
5070
				'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
5071
			),
5072
			array('option' => array('value' => '1')),
5073
			'One',
5074
			'/option',
5075
			array('option' => array('value' => '2', 'selected' => 'selected')),
5076
			'Two',
5077
			'/option',
5078
			array('option' => array('value' => '3')),
5079
			'Three',
5080
			'/option',
5081
			array('option' => array('value' => '3x', 'selected' => 'selected')),
5082
			'Stringy',
5083
			'/option',
5084
			'/select'
5085
		);
5086
		$this->assertTags($result, $expected);
5087
 
5088
		$result = $this->Form->select('Contact.required_one', array(
5089
			'1' => 'option A',
5090
			'2' => 'option B'
5091
		), array('multiple' => true));
5092
		$expected = array(
5093
			'input' => array(
5094
				'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
5095
			),
5096
			'select' => array(
5097
				'name' => 'data[Contact][required_one][]',
5098
				'id' => 'ContactRequiredOne',
5099
				'required' => 'required',
5100
				'multiple' => 'multiple'
5101
			),
5102
			array('option' => array('value' => '1')), 'option A', '/option',
5103
			array('option' => array('value' => '2')), 'option B', '/option',
5104
			'/select'
5105
		);
5106
		$this->assertTags($result, $expected);
5107
 
5108
		$result = $this->Form->select(
5109
			'Model.multi_field',
5110
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
5111
			array('multiple' => true)
5112
		);
5113
		$expected = array(
5114
			'input' => array(
5115
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
5116
				'id' => 'ModelMultiField_'
5117
			),
5118
			array('select' => array('name' => 'data[Model][multi_field][]',
5119
				'multiple' => 'multiple', 'id' => 'ModelMultiField'
5120
			)),
5121
			array('option' => array('value' => 'a&gt;b')),
5122
			'first',
5123
			'/option',
5124
			array('option' => array('value' => 'a&lt;b')),
5125
			'second',
5126
			'/option',
5127
			array('option' => array(
5128
				'value' => 'a&quot;b'
5129
			)),
5130
			'third',
5131
			'/option',
5132
			'/select'
5133
		);
5134
		$this->assertTags($result, $expected);
5135
	}
5136
 
5137
/**
5138
 * Test generating multiple select with disabled elements.
5139
 *
5140
 * @return void
5141
 */
5142
	public function testSelectMultipleWithDisabledElements() {
5143
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5144
		$disabled = array(1);
5145
		$result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
5146
		$expected = array(
5147
			'input' => array(
5148
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
5149
			),
5150
			'select' => array(
5151
				'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
5152
			),
5153
			array('option' => array('value' => '1', 'disabled' => 'disabled')),
5154
			'One',
5155
			'/option',
5156
			array('option' => array('value' => '2')),
5157
			'Two',
5158
			'/option',
5159
			array('option' => array('value' => '3')),
5160
			'Three',
5161
			'/option',
5162
			array('option' => array('value' => '3x')),
5163
			'Stringy',
5164
			'/option',
5165
			'/select'
5166
		);
5167
		$this->assertTags($result, $expected);
5168
 
5169
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5170
		$disabled = array('2', '3x');
5171
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
5172
		$expected = array(
5173
			array('div' => array('class' => 'input select')),
5174
			array('label' => array('for' => 'ContactMultiple')),
5175
			'Multiple',
5176
			'/label',
5177
			'input' => array(
5178
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
5179
			),
5180
			'select' => array(
5181
				'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
5182
			),
5183
			array('option' => array('value' => '1')),
5184
			'One',
5185
			'/option',
5186
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
5187
			'Two',
5188
			'/option',
5189
			array('option' => array('value' => '3')),
5190
			'Three',
5191
			'/option',
5192
			array('option' => array('value' => '3x', 'disabled' => 'disabled')),
5193
			'Stringy',
5194
			'/option',
5195
			'/select',
5196
			'/div'
5197
		);
5198
		$this->assertTags($result, $expected);
5199
 
5200
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5201
		$disabled = true;
5202
		$result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
5203
		$expected = array(
5204
			array('div' => array('class' => 'input select')),
5205
			array('label' => array('for' => 'ContactMultiple')),
5206
			'Multiple',
5207
			'/label',
5208
			'input' => array(
5209
				'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_', 'disabled' => 'disabled'
5210
			),
5211
			'select' => array(
5212
				'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
5213
			),
5214
			array('option' => array('value' => '1')),
5215
			'One',
5216
			'/option',
5217
			array('option' => array('value' => '2')),
5218
			'Two',
5219
			'/option',
5220
			array('option' => array('value' => '3')),
5221
			'Three',
5222
			'/option',
5223
			array('option' => array('value' => '3x')),
5224
			'Stringy',
5225
			'/option',
5226
			'/select',
5227
			'/div'
5228
		);
5229
		$this->assertTags($result, $expected);
5230
	}
5231
 
5232
/**
5233
 * Test generating select with disabled elements.
5234
 *
5235
 * @return void
5236
 */
5237
	public function testSelectWithDisabledElements() {
5238
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5239
		$disabled = array(2, 3);
5240
		$result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
5241
		$expected = array(
5242
			'select' => array(
5243
				'name' => 'data[Model][field]', 'id' => 'ModelField'
5244
			),
5245
			array('option' => array('value' => '')),
5246
			'/option',
5247
			array('option' => array('value' => '1')),
5248
			'One',
5249
			'/option',
5250
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
5251
			'Two',
5252
			'/option',
5253
			array('option' => array('value' => '3', 'disabled' => 'disabled')),
5254
			'Three',
5255
			'/option',
5256
			array('option' => array('value' => '3x')),
5257
			'Stringy',
5258
			'/option',
5259
			'/select'
5260
		);
5261
		$this->assertTags($result, $expected);
5262
 
5263
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5264
		$disabled = array('2', '3x');
5265
		$result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
5266
		$expected = array(
5267
			array('div' => array('class' => 'input select')),
5268
			array('label' => array('for' => 'ModelField')),
5269
			'Field',
5270
			'/label',
5271
			'select' => array(
5272
				'name' => 'data[Model][field]', 'id' => 'ModelField'
5273
			),
5274
			array('option' => array('value' => '1')),
5275
			'One',
5276
			'/option',
5277
			array('option' => array('value' => '2', 'disabled' => 'disabled')),
5278
			'Two',
5279
			'/option',
5280
			array('option' => array('value' => '3')),
5281
			'Three',
5282
			'/option',
5283
			array('option' => array('value' => '3x', 'disabled' => 'disabled')),
5284
			'Stringy',
5285
			'/option',
5286
			'/select',
5287
			'/div'
5288
		);
5289
		$this->assertTags($result, $expected);
5290
 
5291
		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
5292
		$disabled = true;
5293
		$result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
5294
		$expected = array(
5295
			array('div' => array('class' => 'input select')),
5296
			array('label' => array('for' => 'ModelField')),
5297
			'Field',
5298
			'/label',
5299
			'select' => array(
5300
				'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
5301
			),
5302
			array('option' => array('value' => '1')),
5303
			'One',
5304
			'/option',
5305
			array('option' => array('value' => '2')),
5306
			'Two',
5307
			'/option',
5308
			array('option' => array('value' => '3')),
5309
			'Three',
5310
			'/option',
5311
			array('option' => array('value' => '3x')),
5312
			'Stringy',
5313
			'/option',
5314
			'/select',
5315
			'/div'
5316
		);
5317
		$this->assertTags($result, $expected);
5318
	}
5319
 
5320
/**
5321
 * test generation of habtm select boxes.
5322
 *
5323
 * @return void
5324
 */
5325
	public function testHabtmSelectBox() {
5326
		$this->View->viewVars['contactTags'] = array(
5327
			1 => 'blue',
5328
			2 => 'red',
5329
			3 => 'green'
5330
		);
5331
		$this->Form->request->data = array(
5332
			'Contact' => array(),
5333
			'ContactTag' => array(
5334
				array(
5335
					'id' => '1',
5336
					'name' => 'blue'
5337
				),
5338
				array(
5339
					'id' => 3,
5340
					'name' => 'green'
5341
				)
5342
			)
5343
		);
5344
		$this->Form->create('Contact');
5345
		$result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
5346
		$expected = array(
5347
			'input' => array(
5348
				'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
5349
			),
5350
			'select' => array(
5351
				'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
5352
				'multiple' => 'multiple'
5353
			),
5354
			array('option' => array('value' => '1', 'selected' => 'selected')),
5355
			'blue',
5356
			'/option',
5357
			array('option' => array('value' => '2')),
5358
			'red',
5359
			'/option',
5360
			array('option' => array('value' => '3', 'selected' => 'selected')),
5361
			'green',
5362
			'/option',
5363
			'/select'
5364
		);
5365
		$this->assertTags($result, $expected);
5366
 
5367
		// make sure only 50 is selected, and not 50f5c0cf
5368
		$this->View->viewVars['contactTags'] = array(
5369
			'1' => 'blue',
5370
			'50f5c0cf' => 'red',
5371
			'50' => 'green'
5372
		);
5373
		$this->Form->request->data = array(
5374
			'Contact' => array(),
5375
			'ContactTag' => array(
5376
				array(
5377
					'id' => 1,
5378
					'name' => 'blue'
5379
				),
5380
				array(
5381
					'id' => 50,
5382
					'name' => 'green'
5383
				)
5384
			)
5385
		);
5386
		$this->Form->create('Contact');
5387
		$result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
5388
		$expected = array(
5389
			'input' => array(
5390
				'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
5391
			),
5392
			'select' => array(
5393
				'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
5394
				'multiple' => 'multiple'
5395
			),
5396
			array('option' => array('value' => '1', 'selected' => 'selected')),
5397
			'blue',
5398
			'/option',
5399
			array('option' => array('value' => '50f5c0cf')),
5400
			'red',
5401
			'/option',
5402
			array('option' => array('value' => '50', 'selected' => 'selected')),
5403
			'green',
5404
			'/option',
5405
			'/select'
5406
		);
5407
		$this->assertTags($result, $expected);
5408
	}
5409
 
5410
/**
5411
 * test generation of multi select elements in checkbox format
5412
 *
5413
 * @return void
5414
 */
5415
	public function testSelectMultipleCheckboxes() {
5416
		$result = $this->Form->select(
5417
			'Model.multi_field',
5418
			array('first', 'second', 'third'),
5419
			array('multiple' => 'checkbox')
5420
		);
5421
 
5422
		$expected = array(
5423
			'input' => array(
5424
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5425
			),
5426
			array('div' => array('class' => 'checkbox')),
5427
			array('input' => array(
5428
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5429
				'value' => '0', 'id' => 'ModelMultiField0'
5430
			)),
5431
			array('label' => array('for' => 'ModelMultiField0')),
5432
			'first',
5433
			'/label',
5434
			'/div',
5435
			array('div' => array('class' => 'checkbox')),
5436
			array('input' => array(
5437
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5438
				'value' => '1', 'id' => 'ModelMultiField1'
5439
			)),
5440
			array('label' => array('for' => 'ModelMultiField1')),
5441
			'second',
5442
			'/label',
5443
			'/div',
5444
			array('div' => array('class' => 'checkbox')),
5445
			array('input' => array(
5446
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5447
				'value' => '2', 'id' => 'ModelMultiField2'
5448
			)),
5449
			array('label' => array('for' => 'ModelMultiField2')),
5450
			'third',
5451
			'/label',
5452
			'/div'
5453
		);
5454
		$this->assertTags($result, $expected);
5455
 
5456
		$result = $this->Form->select(
5457
			'Model.multi_field',
5458
			array('a' => 'first', 'b' => 'second', 'c' => 'third'),
5459
			array('multiple' => 'checkbox')
5460
		);
5461
		$expected = array(
5462
			'input' => array(
5463
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5464
			),
5465
			array('div' => array('class' => 'checkbox')),
5466
			array('input' => array(
5467
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5468
				'value' => 'a', 'id' => 'ModelMultiFieldA'
5469
			)),
5470
			array('label' => array('for' => 'ModelMultiFieldA')),
5471
			'first',
5472
			'/label',
5473
			'/div',
5474
			array('div' => array('class' => 'checkbox')),
5475
			array('input' => array(
5476
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5477
				'value' => 'b', 'id' => 'ModelMultiFieldB'
5478
			)),
5479
			array('label' => array('for' => 'ModelMultiFieldB')),
5480
			'second',
5481
			'/label',
5482
			'/div',
5483
			array('div' => array('class' => 'checkbox')),
5484
			array('input' => array(
5485
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5486
				'value' => 'c', 'id' => 'ModelMultiFieldC'
5487
			)),
5488
			array('label' => array('for' => 'ModelMultiFieldC')),
5489
			'third',
5490
			'/label',
5491
			'/div'
5492
		);
5493
		$this->assertTags($result, $expected);
5494
 
5495
		$result = $this->Form->select(
5496
			'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
5497
		);
5498
		$expected = array(
5499
			'input' => array(
5500
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5501
			),
5502
			array('div' => array('class' => 'checkbox')),
5503
			array('input' => array(
5504
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5505
				'value' => '1', 'id' => 'ModelMultiField1'
5506
			)),
5507
			array('label' => array('for' => 'ModelMultiField1')),
5508
			'first',
5509
			'/label',
5510
			'/div'
5511
		);
5512
		$this->assertTags($result, $expected);
5513
 
5514
		$this->Form->request->data = array('Model' => array('tags' => array(1)));
5515
		$result = $this->Form->select(
5516
			'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
5517
		);
5518
		$expected = array(
5519
			'input' => array(
5520
				'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
5521
			),
5522
			array('div' => array('class' => 'checkbox')),
5523
			array('input' => array(
5524
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5525
				'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
5526
			)),
5527
			array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
5528
			'first',
5529
			'/label',
5530
			'/div',
5531
 
5532
			array('div' => array('class' => 'checkbox')),
5533
			array('input' => array(
5534
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5535
				'value' => 'Array', 'id' => 'ModelTagsArray'
5536
			)),
5537
			array('label' => array('for' => 'ModelTagsArray')),
5538
			'Array',
5539
			'/label',
5540
			'/div'
5541
		);
5542
		$this->assertTags($result, $expected);
5543
 
5544
		$result = $this->Form->select(
5545
			'Model.multi_field',
5546
			array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
5547
			array('multiple' => 'checkbox')
5548
		);
5549
		$expected = array(
5550
			'input' => array(
5551
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5552
			),
5553
			array('div' => array('class' => 'checkbox')),
5554
			array('input' => array(
5555
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5556
				'value' => 'a+', 'id' => 'ModelMultiFieldA2'
5557
			)),
5558
			array('label' => array('for' => 'ModelMultiFieldA2')),
5559
			'first',
5560
			'/label',
5561
			'/div',
5562
			array('div' => array('class' => 'checkbox')),
5563
			array('input' => array(
5564
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5565
				'value' => 'a++', 'id' => 'ModelMultiFieldA1'
5566
			)),
5567
			array('label' => array('for' => 'ModelMultiFieldA1')),
5568
			'second',
5569
			'/label',
5570
			'/div',
5571
			array('div' => array('class' => 'checkbox')),
5572
			array('input' => array(
5573
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5574
				'value' => 'a+++', 'id' => 'ModelMultiFieldA'
5575
			)),
5576
			array('label' => array('for' => 'ModelMultiFieldA')),
5577
			'third',
5578
			'/label',
5579
			'/div'
5580
		);
5581
 
5582
		$this->assertTags($result, $expected);
5583
 
5584
		$result = $this->Form->select(
5585
			'Model.multi_field',
5586
			array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
5587
			array('multiple' => 'checkbox')
5588
		);
5589
		$expected = array(
5590
			'input' => array(
5591
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
5592
			),
5593
			array('div' => array('class' => 'checkbox')),
5594
			array('input' => array(
5595
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5596
				'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
5597
			)),
5598
			array('label' => array('for' => 'ModelMultiFieldAB2')),
5599
			'first',
5600
			'/label',
5601
			'/div',
5602
			array('div' => array('class' => 'checkbox')),
5603
			array('input' => array(
5604
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5605
				'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
5606
			)),
5607
			array('label' => array('for' => 'ModelMultiFieldAB1')),
5608
			'second',
5609
			'/label',
5610
			'/div',
5611
			array('div' => array('class' => 'checkbox')),
5612
			array('input' => array(
5613
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5614
				'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
5615
			)),
5616
			array('label' => array('for' => 'ModelMultiFieldAB')),
5617
			'third',
5618
			'/label',
5619
			'/div'
5620
		);
5621
		$this->assertTags($result, $expected);
5622
	}
5623
 
5624
/**
5625
 * test multiple checkboxes with div styles.
5626
 *
5627
 * @return void
5628
 */
5629
	public function testSelectMultipleCheckboxDiv() {
5630
		$result = $this->Form->select(
5631
			'Model.tags',
5632
			array('first', 'second'),
5633
			array('multiple' => 'checkbox', 'class' => 'my-class')
5634
		);
5635
		$expected = array(
5636
			'input' => array(
5637
				'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
5638
			),
5639
			array('div' => array('class' => 'my-class')),
5640
			array('input' => array(
5641
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5642
				'value' => '0', 'id' => 'ModelTags0'
5643
			)),
5644
			array('label' => array('for' => 'ModelTags0')), 'first', '/label',
5645
			'/div',
5646
 
5647
			array('div' => array('class' => 'my-class')),
5648
			array('input' => array(
5649
				'type' => 'checkbox', 'name' => 'data[Model][tags][]',
5650
				'value' => '1', 'id' => 'ModelTags1'
5651
			)),
5652
			array('label' => array('for' => 'ModelTags1')), 'second', '/label',
5653
			'/div'
5654
		);
5655
		$this->assertTags($result, $expected);
5656
 
5657
		$result = $this->Form->input('Model.tags', array(
5658
			'options' => array('first', 'second'),
5659
			'multiple' => 'checkbox',
5660
			'class' => 'my-class',
5661
			'div' => false,
5662
			'label' => false
5663
		));
5664
		$this->assertTags($result, $expected);
5665
 
5666
		$Contact = ClassRegistry::getObject('Contact');
5667
		$Contact->validationErrors['tags'] = 'Select atleast one option';
5668
		$result = $this->Form->input('Contact.tags', array(
5669
			'options' => array('one'),
5670
			'multiple' => 'checkbox',
5671
			'label' => false,
5672
			'div' => false
5673
		));
5674
		$expected = array(
5675
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
5676
			array('div' => array('class' => 'checkbox form-error')),
5677
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
5678
			array('label' => array('for' => 'ContactTags0')),
5679
			'one',
5680
			'/label',
5681
			'/div'
5682
		);
5683
		$this->assertTags($result, $expected);
5684
 
5685
		$result = $this->Form->input('Contact.tags', array(
5686
			'options' => array('one'),
5687
			'multiple' => 'checkbox',
5688
			'class' => 'mycheckbox',
5689
			'label' => false,
5690
			'div' => false
5691
		));
5692
		$expected = array(
5693
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
5694
			array('div' => array('class' => 'mycheckbox form-error')),
5695
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
5696
			array('label' => array('for' => 'ContactTags0')),
5697
			'one',
5698
			'/label',
5699
			'/div'
5700
		);
5701
		$this->assertTags($result, $expected);
5702
	}
5703
 
5704
/**
5705
 * Checks the security hash array generated for multiple-input checkbox elements
5706
 *
5707
 * @return void
5708
 */
5709
	public function testSelectMultipleCheckboxSecurity() {
5710
		$this->Form->request['_Token'] = array('key' => 'testKey');
5711
		$this->assertEquals(array(), $this->Form->fields);
5712
 
5713
		$result = $this->Form->select(
5714
			'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
5715
			array('multiple' => 'checkbox')
5716
		);
5717
		$this->assertEquals(array('Model.multi_field'), $this->Form->fields);
5718
 
5719
		$result = $this->Form->secure($this->Form->fields);
5720
		$key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
5721
		$this->assertRegExp('/"' . $key . '"/', $result);
5722
	}
5723
 
5724
/**
5725
 * Multiple select elements should always be secured as they always participate
5726
 * in the POST data.
5727
 *
5728
 * @return void
5729
 */
5730
	public function testSelectMultipleSecureWithNoOptions() {
5731
		$this->Form->request['_Token'] = array('key' => 'testkey');
5732
		$this->assertEquals(array(), $this->Form->fields);
5733
 
5734
		$this->Form->select(
5735
			'Model.select',
5736
			array(),
5737
			array('multiple' => true)
5738
		);
5739
		$this->assertEquals(array('Model.select'), $this->Form->fields);
5740
	}
5741
/**
5742
 * When a select box has no options it should not be added to the fields list
5743
 * as it always fail post validation.
5744
 *
5745
 * @return void
5746
 */
5747
	public function testSelectNoSecureWithNoOptions() {
5748
		$this->Form->request['_Token'] = array('key' => 'testkey');
5749
		$this->assertEquals(array(), $this->Form->fields);
5750
 
5751
		$this->Form->select(
5752
			'Model.select',
5753
			array()
5754
		);
5755
		$this->assertEquals(array(), $this->Form->fields);
5756
 
5757
		$this->Form->select(
5758
			'Model.select',
5759
			array(),
5760
			array('empty' => true)
5761
		);
5762
		$this->assertEquals(array('Model.select'), $this->Form->fields);
5763
	}
5764
 
5765
/**
5766
 * testInputMultipleCheckboxes method
5767
 *
5768
 * test input() resulting in multi select elements being generated.
5769
 *
5770
 * @return void
5771
 */
5772
	public function testInputMultipleCheckboxes() {
5773
		$result = $this->Form->input('Model.multi_field', array(
5774
			'options' => array('first', 'second', 'third'),
5775
			'multiple' => 'checkbox'
5776
		));
5777
		$expected = array(
5778
			array('div' => array('class' => 'input select')),
5779
			array('label' => array('for' => 'ModelMultiField')),
5780
			'Multi Field',
5781
			'/label',
5782
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5783
			array('div' => array('class' => 'checkbox')),
5784
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5785
			array('label' => array('for' => 'ModelMultiField0')),
5786
			'first',
5787
			'/label',
5788
			'/div',
5789
			array('div' => array('class' => 'checkbox')),
5790
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5791
			array('label' => array('for' => 'ModelMultiField1')),
5792
			'second',
5793
			'/label',
5794
			'/div',
5795
			array('div' => array('class' => 'checkbox')),
5796
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
5797
			array('label' => array('for' => 'ModelMultiField2')),
5798
			'third',
5799
			'/label',
5800
			'/div',
5801
			'/div'
5802
		);
5803
		$this->assertTags($result, $expected);
5804
 
5805
		$result = $this->Form->input('Model.multi_field', array(
5806
			'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
5807
			'multiple' => 'checkbox'
5808
		));
5809
		$expected = array(
5810
			array('div' => array('class' => 'input select')),
5811
			array('label' => array('for' => 'ModelMultiField')),
5812
			'Multi Field',
5813
			'/label',
5814
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5815
			array('div' => array('class' => 'checkbox')),
5816
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
5817
			array('label' => array('for' => 'ModelMultiFieldA')),
5818
			'first',
5819
			'/label',
5820
			'/div',
5821
			array('div' => array('class' => 'checkbox')),
5822
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
5823
			array('label' => array('for' => 'ModelMultiFieldB')),
5824
			'second',
5825
			'/label',
5826
			'/div',
5827
			array('div' => array('class' => 'checkbox')),
5828
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
5829
			array('label' => array('for' => 'ModelMultiFieldC')),
5830
			'third',
5831
			'/label',
5832
			'/div',
5833
			'/div'
5834
		);
5835
		$this->assertTags($result, $expected);
5836
 
5837
		$result = $this->Form->input('Model.multi_field', array(
5838
			'options' => array('1' => 'first'),
5839
			'multiple' => 'checkbox',
5840
			'label' => false,
5841
			'div' => false
5842
		));
5843
		$expected = array(
5844
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5845
			array('div' => array('class' => 'checkbox')),
5846
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5847
			array('label' => array('for' => 'ModelMultiField1')),
5848
			'first',
5849
			'/label',
5850
			'/div'
5851
		);
5852
		$this->assertTags($result, $expected);
5853
 
5854
		$result = $this->Form->input('Model.multi_field', array(
5855
			'options' => array('2' => 'second'),
5856
			'multiple' => 'checkbox',
5857
			'label' => false,
5858
			'div' => false
5859
		));
5860
		$expected = array(
5861
			'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
5862
			array('div' => array('class' => 'checkbox')),
5863
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
5864
			array('label' => array('for' => 'ModelMultiField2')),
5865
			'second',
5866
			'/label',
5867
			'/div'
5868
		);
5869
		$this->assertTags($result, $expected);
5870
	}
5871
 
5872
/**
5873
 * testSelectHiddenFieldOmission method
5874
 *
5875
 * test that select() with 'hiddenField' => false omits the hidden field
5876
 *
5877
 * @return void
5878
 */
5879
	public function testSelectHiddenFieldOmission() {
5880
		$result = $this->Form->select('Model.multi_field',
5881
			array('first', 'second'),
5882
			array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
5883
		);
5884
		$expected = array(
5885
			array('div' => array('class' => 'checkbox')),
5886
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5887
			array('label' => array('for' => 'ModelMultiField0')),
5888
			'first',
5889
			'/label',
5890
			'/div',
5891
			array('div' => array('class' => 'checkbox')),
5892
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5893
			array('label' => array('for' => 'ModelMultiField1')),
5894
			'second',
5895
			'/label',
5896
			'/div'
5897
		);
5898
		$this->assertTags($result, $expected);
5899
 
5900
		$result = $this->Form->input('Model.multi_field', array(
5901
			'options' => array('first', 'second'),
5902
			'multiple' => 'checkbox',
5903
			'hiddenField' => false
5904
		));
5905
		$expected = array(
5906
			array('div' => array('class' => 'input select')),
5907
			array('label' => array('for' => 'ModelMultiField')),
5908
			'Multi Field',
5909
			'/label',
5910
			array('div' => array('class' => 'checkbox')),
5911
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
5912
			array('label' => array('for' => 'ModelMultiField0')),
5913
			'first',
5914
			'/label',
5915
			'/div',
5916
			array('div' => array('class' => 'checkbox')),
5917
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
5918
			array('label' => array('for' => 'ModelMultiField1')),
5919
			'second',
5920
			'/label',
5921
			'/div',
5922
			'/div'
5923
		);
5924
		$this->assertTags($result, $expected);
5925
	}
5926
 
5927
/**
5928
 * test that select() with multiple = checkbox works with overriding name attribute.
5929
 *
5930
 * @return void
5931
 */
5932
	public function testSelectCheckboxMultipleOverrideName() {
5933
		$result = $this->Form->input('category', array(
5934
			'type' => 'select',
5935
			'multiple' => 'checkbox',
5936
			'name' => 'data[fish]',
5937
			'options' => array('1', '2'),
5938
			'div' => false,
5939
			'label' => false,
5940
		));
5941
		$expected = array(
5942
			'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
5943
			array('div' => array('class' => 'checkbox')),
5944
				array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
5945
				array('label' => array('for' => 'Category0')), '1', '/label',
5946
			'/div',
5947
			array('div' => array('class' => 'checkbox')),
5948
				array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
5949
				array('label' => array('for' => 'Category1')), '2', '/label',
5950
			'/div'
5951
		);
5952
		$this->assertTags($result, $expected);
5953
	}
5954
 
5955
/**
5956
 * Test that 'id' overrides all the checkbox id's as well.
5957
 *
5958
 * @return void
5959
 */
5960
	public function testSelectCheckboxMultipleId() {
5961
		$result = $this->Form->select(
5962
			'Model.multi_field',
5963
			array('first', 'second', 'third'),
5964
			array('multiple' => 'checkbox', 'id' => 'CustomId')
5965
		);
5966
 
5967
		$expected = array(
5968
			'input' => array(
5969
				'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
5970
			),
5971
			array('div' => array('class' => 'checkbox')),
5972
			array('input' => array(
5973
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5974
				'value' => '0', 'id' => 'CustomId0'
5975
			)),
5976
			array('label' => array('for' => 'CustomId0')),
5977
			'first',
5978
			'/label',
5979
			'/div',
5980
			array('div' => array('class' => 'checkbox')),
5981
			array('input' => array(
5982
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5983
				'value' => '1', 'id' => 'CustomId1'
5984
			)),
5985
			array('label' => array('for' => 'CustomId1')),
5986
			'second',
5987
			'/label',
5988
			'/div',
5989
			array('div' => array('class' => 'checkbox')),
5990
			array('input' => array(
5991
				'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
5992
				'value' => '2', 'id' => 'CustomId2'
5993
			)),
5994
			array('label' => array('for' => 'CustomId2')),
5995
			'third',
5996
			'/label',
5997
			'/div'
5998
		);
5999
		$this->assertTags($result, $expected);
6000
	}
6001
 
6002
/**
6003
 * testCheckbox method
6004
 *
6005
 * Test generation of checkboxes
6006
 *
6007
 * @return void
6008
 */
6009
	public function testCheckbox() {
6010
		$result = $this->Form->checkbox('Model.field');
6011
		$expected = array(
6012
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
6013
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
6014
		);
6015
		$this->assertTags($result, $expected);
6016
 
6017
		$result = $this->Form->checkbox('Model.field', array(
6018
			'id' => 'theID',
6019
			'value' => 'myvalue',
6020
			'form' => 'my-form',
6021
		));
6022
		$expected = array(
6023
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_', 'form' => 'my-form'),
6024
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form'))
6025
		);
6026
		$this->assertTags($result, $expected);
6027
 
6028
		$Contact = ClassRegistry::getObject('Contact');
6029
		$Contact->validationErrors['field'] = 1;
6030
		$this->Form->request->data['Contact']['field'] = 'myvalue';
6031
		$result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
6032
		$expected = array(
6033
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
6034
			array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
6035
		);
6036
		$this->assertTags($result, $expected);
6037
 
6038
		$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
6039
		$expected = array(
6040
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
6041
			array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
6042
		);
6043
		$this->assertTags($result, $expected);
6044
 
6045
		$this->Form->request->data['Contact']['field'] = '';
6046
		$result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
6047
		$expected = array(
6048
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
6049
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
6050
		);
6051
		$this->assertTags($result, $expected);
6052
 
6053
		$Contact->validationErrors = array();
6054
		$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
6055
		$expected = array(
6056
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
6057
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
6058
		);
6059
		$this->assertTags($result, $expected);
6060
 
6061
		$this->Form->request->data['Contact']['published'] = 1;
6062
		$result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
6063
		$expected = array(
6064
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
6065
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
6066
		);
6067
		$this->assertTags($result, $expected);
6068
 
6069
		$this->Form->request->data['Contact']['published'] = 0;
6070
		$result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
6071
		$expected = array(
6072
			'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
6073
			array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
6074
		);
6075
		$this->assertTags($result, $expected);
6076
 
6077
		$result = $this->Form->checkbox('Model.CustomField.1.value');
6078
		$expected = array(
6079
			'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
6080
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
6081
		);
6082
		$this->assertTags($result, $expected);
6083
 
6084
		$result = $this->Form->checkbox('CustomField.1.value');
6085
		$expected = array(
6086
			'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
6087
			array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
6088
		);
6089
		$this->assertTags($result, $expected);
6090
	}
6091
 
6092
/**
6093
 * test checkbox() with a custom name attribute
6094
 *
6095
 * @return void
6096
 */
6097
	public function testCheckboxCustomNameAttribute() {
6098
		$result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
6099
		$expected = array(
6100
				'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
6101
				array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
6102
			);
6103
		$this->assertTags($result, $expected);
6104
	}
6105
 
6106
/**
6107
 * test the checked option for checkboxes.
6108
 *
6109
 * @return void
6110
 */
6111
	public function testCheckboxCheckedOption() {
6112
		$result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
6113
		$expected = array(
6114
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
6115
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
6116
		);
6117
		$this->assertTags($result, $expected);
6118
 
6119
		$result = $this->Form->checkbox('Model.field', array('checked' => 1));
6120
		$expected = array(
6121
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
6122
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
6123
		);
6124
		$this->assertTags($result, $expected);
6125
 
6126
		$result = $this->Form->checkbox('Model.field', array('checked' => true));
6127
		$expected = array(
6128
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
6129
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
6130
		);
6131
		$this->assertTags($result, $expected);
6132
 
6133
		$result = $this->Form->checkbox('Model.field', array('checked' => false));
6134
		$expected = array(
6135
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
6136
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
6137
		);
6138
		$this->assertTags($result, $expected);
6139
 
6140
		$this->Form->request->data['Model']['field'] = 1;
6141
		$result = $this->Form->checkbox('Model.field', array('checked' => false));
6142
		$expected = array(
6143
			'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
6144
			array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
6145
		);
6146
		$this->assertTags($result, $expected);
6147
	}
6148
 
6149
/**
6150
 * Test that disabled attribute works on both the checkbox and hidden input.
6151
 *
6152
 * @return void
6153
 */
6154
	public function testCheckboxDisabling() {
6155
		$result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
6156
		$expected = array(
6157
			array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
6158
			array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
6159
		);
6160
		$this->assertTags($result, $expected);
6161
 
6162
		$result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
6163
		$expected = array(
6164
			array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
6165
			array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
6166
		);
6167
		$this->assertTags($result, $expected);
6168
	}
6169
 
6170
/**
6171
 * Test that the hidden input for checkboxes can be omitted or set to a
6172
 * specific value.
6173
 *
6174
 * @return void
6175
 */
6176
	public function testCheckboxHiddenField() {
6177
		$result = $this->Form->input('UserForm.something', array(
6178
			'type' => 'checkbox',
6179
			'hiddenField' => false
6180
		));
6181
		$expected = array(
6182
			'div' => array('class' => 'input checkbox'),
6183
			array('input' => array(
6184
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
6185
				'value' => '1', 'id' => 'UserFormSomething'
6186
			)),
6187
			'label' => array('for' => 'UserFormSomething'),
6188
			'Something',
6189
			'/label',
6190
			'/div'
6191
		);
6192
		$this->assertTags($result, $expected);
6193
 
6194
		$result = $this->Form->input('UserForm.something', array(
6195
			'type' => 'checkbox',
6196
			'value' => 'Y',
6197
			'hiddenField' => 'N',
6198
		));
6199
		$expected = array(
6200
			'div' => array('class' => 'input checkbox'),
6201
			array('input' => array(
6202
				'type' => 'hidden', 'name' => 'data[UserForm][something]',
6203
				'value' => 'N', 'id' => 'UserFormSomething_'
6204
			)),
6205
			array('input' => array(
6206
				'type' => 'checkbox', 'name' => 'data[UserForm][something]',
6207
				'value' => 'Y', 'id' => 'UserFormSomething'
6208
			)),
6209
			'label' => array('for' => 'UserFormSomething'),
6210
			'Something',
6211
			'/label',
6212
			'/div'
6213
		);
6214
		$this->assertTags($result, $expected);
6215
	}
6216
 
6217
/**
6218
 * Test that a checkbox can have 0 for the value and 1 for the hidden input.
6219
 *
6220
 * @return void
6221
 */
6222
	public function testCheckboxZeroValue() {
6223
		$result = $this->Form->input('User.get_spam', array(
6224
			'type' => 'checkbox',
6225
			'value' => '0',
6226
			'hiddenField' => '1',
6227
		));
6228
		$expected = array(
6229
			'div' => array('class' => 'input checkbox'),
6230
			array('input' => array(
6231
				'type' => 'hidden', 'name' => 'data[User][get_spam]',
6232
				'value' => '1', 'id' => 'UserGetSpam_'
6233
			)),
6234
			array('input' => array(
6235
				'type' => 'checkbox', 'name' => 'data[User][get_spam]',
6236
				'value' => '0', 'id' => 'UserGetSpam'
6237
			)),
6238
			'label' => array('for' => 'UserGetSpam'),
6239
			'Get Spam',
6240
			'/label',
6241
			'/div'
6242
		);
6243
		$this->assertTags($result, $expected);
6244
	}
6245
 
6246
/**
6247
 * testDateTime method
6248
 *
6249
 * Test generation of date/time select elements
6250
 *
6251
 * @return void
6252
 */
6253
	public function testDateTime() {
6254
		extract($this->dateRegex);
6255
 
6256
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
6257
		$now = strtotime('now');
6258
		$expected = array(
6259
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6260
			$daysRegex,
6261
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6262
			date('j', $now),
6263
			'/option',
6264
			'*/select',
6265
			'-',
6266
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6267
			$monthsRegex,
6268
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6269
			date('F', $now),
6270
			'/option',
6271
			'*/select',
6272
			'-',
6273
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6274
			$yearsRegex,
6275
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6276
			date('Y', $now),
6277
			'/option',
6278
			'*/select',
6279
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6280
			$hoursRegex,
6281
			array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
6282
			date('g', $now),
6283
			'/option',
6284
			'*/select',
6285
			':',
6286
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6287
			$minutesRegex,
6288
			array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
6289
			date('i', $now),
6290
			'/option',
6291
			'*/select',
6292
			' ',
6293
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6294
			$meridianRegex,
6295
			array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
6296
			date('a', $now),
6297
			'/option',
6298
			'*/select'
6299
		);
6300
		$this->assertTags($result, $expected);
6301
 
6302
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12');
6303
		$expected = array(
6304
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6305
			$daysRegex,
6306
			array('option' => array('value' => '')),
6307
			'/option',
6308
			'*/select',
6309
			'-',
6310
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6311
			$monthsRegex,
6312
			array('option' => array('value' => '')),
6313
			'/option',
6314
			'*/select',
6315
			'-',
6316
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6317
			$yearsRegex,
6318
			array('option' => array('value' => '')),
6319
			'/option',
6320
			'*/select',
6321
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6322
			$hoursRegex,
6323
			array('option' => array('value' => '')),
6324
			'/option',
6325
			'*/select',
6326
			':',
6327
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6328
			$minutesRegex,
6329
			array('option' => array('value' => '')),
6330
			'/option',
6331
			'*/select',
6332
			' ',
6333
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6334
			$meridianRegex,
6335
			array('option' => array('value' => '')),
6336
			'/option',
6337
			'*/select'
6338
		);
6339
		$this->assertTags($result, $expected);
6340
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6341
 
6342
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
6343
		$this->assertTags($result, $expected);
6344
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6345
 
6346
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
6347
		$this->assertTags($result, $expected);
6348
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6349
 
6350
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
6351
		$expected = array(
6352
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6353
			$daysRegex,
6354
			array('option' => array('value' => '')),
6355
			'/option',
6356
			'*/select',
6357
			'-',
6358
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6359
			$monthsRegex,
6360
			array('option' => array('value' => '')),
6361
			'/option',
6362
			'*/select',
6363
			'-',
6364
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6365
			$yearsRegex,
6366
			array('option' => array('value' => '')),
6367
			'/option',
6368
			'*/select',
6369
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6370
			$hoursRegex,
6371
			array('option' => array('value' => '')),
6372
			'/option',
6373
			'*/select',
6374
			':',
6375
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6376
			$minutesRegex,
6377
			array('option' => array('value' => '')),
6378
			'/option',
6379
			array('option' => array('value' => '00')),
6380
			'00',
6381
			'/option',
6382
			array('option' => array('value' => '05')),
6383
			'05',
6384
			'/option',
6385
			array('option' => array('value' => '10')),
6386
			'10',
6387
			'/option',
6388
			'*/select',
6389
			' ',
6390
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6391
			$meridianRegex,
6392
			array('option' => array('value' => '')),
6393
			'/option',
6394
			'*/select'
6395
		);
6396
		$this->assertTags($result, $expected);
6397
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6398
 
6399
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
6400
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
6401
 
6402
		$this->Form->request->data['Contact']['data'] = null;
6403
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12');
6404
		$expected = array(
6405
			array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
6406
			$daysRegex,
6407
			array('option' => array('value' => '')),
6408
			'/option',
6409
			'*/select',
6410
			'-',
6411
			array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
6412
			$monthsRegex,
6413
			array('option' => array('value' => '')),
6414
			'/option',
6415
			'*/select',
6416
			'-',
6417
			array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
6418
			$yearsRegex,
6419
			array('option' => array('value' => '')),
6420
			'/option',
6421
			'*/select',
6422
			array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
6423
			$hoursRegex,
6424
			array('option' => array('value' => '')),
6425
			'/option',
6426
			'*/select',
6427
			':',
6428
			array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
6429
			$minutesRegex,
6430
			array('option' => array('value' => '')),
6431
			'/option',
6432
			'*/select',
6433
			' ',
6434
			array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
6435
			$meridianRegex,
6436
			array('option' => array('value' => '')),
6437
			'/option',
6438
			'*/select'
6439
		);
6440
		$this->assertTags($result, $expected);
6441
		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
6442
 
6443
		$this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
6444
		$now = strtotime($this->Form->data['Model']['field']);
6445
		$result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
6446
		$expected = array(
6447
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6448
			$daysRegex,
6449
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6450
			date('j', $now),
6451
			'/option',
6452
			'*/select',
6453
			'-',
6454
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6455
			$monthsRegex,
6456
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6457
			date('F', $now),
6458
			'/option',
6459
			'*/select',
6460
			'-',
6461
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
6462
			$yearsRegex,
6463
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6464
			date('Y', $now),
6465
			'/option',
6466
			'*/select',
6467
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
6468
			$hoursRegex,
6469
			array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
6470
			date('g', $now),
6471
			'/option',
6472
			'*/select',
6473
			':',
6474
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
6475
			$minutesRegex,
6476
			array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
6477
			date('i', $now),
6478
			'/option',
6479
			'*/select',
6480
			' ',
6481
			array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
6482
			$meridianRegex,
6483
			array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
6484
			date('a', $now),
6485
			'/option',
6486
			'*/select'
6487
		);
6488
		$this->assertTags($result, $expected);
6489
 
6490
		$selected = strtotime('2008-10-26 12:33:00');
6491
		$result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
6492
		$this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
6493
		$this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
6494
		$this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
6495
		$this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
6496
		$this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
6497
		$this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
6498
 
6499
		$this->Form->create('Contact');
6500
		$result = $this->Form->input('published');
6501
		$now = strtotime('now');
6502
		$expected = array(
6503
			'div' => array('class' => 'input date'),
6504
			'label' => array('for' => 'ContactPublishedMonth'),
6505
			'Published',
6506
			'/label',
6507
			array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
6508
			$monthsRegex,
6509
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6510
			date('F', $now),
6511
			'/option',
6512
			'*/select',
6513
			'-',
6514
			array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
6515
			$daysRegex,
6516
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6517
			date('j', $now),
6518
			'/option',
6519
			'*/select',
6520
			'-',
6521
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
6522
			$yearsRegex,
6523
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6524
			date('Y', $now),
6525
			'/option',
6526
			'*/select',
6527
			'/div'
6528
		);
6529
		$this->assertTags($result, $expected);
6530
 
6531
		$result = $this->Form->input('published2', array('type' => 'date'));
6532
		$now = strtotime('now');
6533
		$expected = array(
6534
			'div' => array('class' => 'input date'),
6535
			'label' => array('for' => 'ContactPublished2Month'),
6536
			'Published2',
6537
			'/label',
6538
			array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
6539
			$monthsRegex,
6540
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6541
			date('F', $now),
6542
			'/option',
6543
			'*/select',
6544
			'-',
6545
			array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
6546
			$daysRegex,
6547
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6548
			date('j', $now),
6549
			'/option',
6550
			'*/select',
6551
			'-',
6552
			array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
6553
			$yearsRegex,
6554
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6555
			date('Y', $now),
6556
			'/option',
6557
			'*/select',
6558
			'/div'
6559
		);
6560
		$this->assertTags($result, $expected);
6561
 
6562
		$this->Form->create('Contact');
6563
		$result = $this->Form->input('published', array('monthNames' => false));
6564
		$now = strtotime('now');
6565
		$expected = array(
6566
			'div' => array('class' => 'input date'),
6567
			'label' => array('for' => 'ContactPublishedMonth'),
6568
			'Published',
6569
			'/label',
6570
			array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
6571
			'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
6572
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6573
			date('m', $now),
6574
			'/option',
6575
			'*/select',
6576
			'-',
6577
			array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
6578
			$daysRegex,
6579
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6580
			date('j', $now),
6581
			'/option',
6582
			'*/select',
6583
			'-',
6584
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
6585
			$yearsRegex,
6586
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6587
			date('Y', $now),
6588
			'/option',
6589
			'*/select',
6590
			'/div'
6591
		);
6592
		$this->assertTags($result, $expected);
6593
 
6594
		$result = $this->Form->input('published', array(
6595
			'timeFormat' => 24,
6596
			'interval' => 5,
6597
			'selected' => strtotime('2009-09-03 13:37:00'),
6598
			'type' => 'datetime'
6599
		));
6600
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6601
		$this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
6602
		$this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
6603
		$this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
6604
		$this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
6605
	}
6606
 
6607
/**
6608
 * Test dateTime with rounding
6609
 *
6610
 * @return void
6611
 */
6612
	public function testDateTimeRounding() {
6613
		$this->Form->request->data['Contact'] = array(
6614
			'date' => array(
6615
				'day' => '13',
6616
				'month' => '12',
6617
				'year' => '2010',
6618
				'hour' => '04',
6619
				'min' => '19',
6620
				'meridian' => 'AM'
6621
			)
6622
		);
6623
 
6624
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
6625
		$this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
6626
 
6627
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
6628
		$this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
6629
 
6630
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
6631
		$this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
6632
	}
6633
 
6634
/**
6635
 * Test that empty values don't trigger errors.
6636
 *
6637
 * @return void
6638
 */
6639
	public function testDateTimeNoErrorsOnEmptyData() {
6640
		$this->Form->request->data['Contact'] = array(
6641
			'date' => array(
6642
				'day' => '',
6643
				'month' => '',
6644
				'year' => '',
6645
				'hour' => '',
6646
				'min' => '',
6647
				'meridian' => ''
6648
			)
6649
		);
6650
		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
6651
		$this->assertNotEmpty($result);
6652
	}
6653
 
6654
/**
6655
 * test that datetime() and default values work.
6656
 *
6657
 * @return void
6658
 */
6659
	public function testDatetimeWithDefault() {
6660
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
6661
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6662
		$this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
6663
		$this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
6664
 
6665
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
6666
			'default' => '2009-06-01 11:15:30'
6667
		));
6668
		$this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
6669
		$this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
6670
		$this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
6671
	}
6672
 
6673
/**
6674
 * test that bogus non-date time data doesn't cause errors.
6675
 *
6676
 * @return void
6677
 */
6678
	public function testDateTimeWithBogusData() {
6679
		$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
6680
		$this->assertNotRegExp('/selected="selected">\d/', $result);
6681
	}
6682
 
6683
/**
6684
 * testDateTime all zeros
6685
 *
6686
 * @return void
6687
 */
6688
	public function testDateTimeAllZeros() {
6689
		$result = $this->Form->dateTime('Contact.date',
6690
			'DMY',
6691
			false,
6692
			array(
6693
				'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
6694
				'value' => '0000-00-00'
6695
			)
6696
		);
6697
 
6698
		$this->assertRegExp('/<option value="">-<\/option>/', $result);
6699
		$this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
6700
	}
6701
 
6702
/**
6703
 * testDateTimeEmptyAsArray
6704
 *
6705
 * @return void
6706
 */
6707
	public function testDateTimeEmptyAsArray() {
6708
		$result = $this->Form->dateTime('Contact.date',
6709
			'DMY',
6710
			'12',
6711
			array(
6712
				'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
6713
					'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
6714
				)
6715
			)
6716
		);
6717
 
6718
		$this->assertRegExp('/<option value="">DAY<\/option>/', $result);
6719
		$this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
6720
		$this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
6721
		$this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
6722
		$this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
6723
		$this->assertNotRegExp('/<option value=""><\/option>/', $result);
6724
 
6725
		$result = $this->Form->dateTime('Contact.date',
6726
			'DMY',
6727
			'12',
6728
			array(
6729
				'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
6730
			)
6731
		);
6732
 
6733
		$this->assertRegExp('/<option value="">DAY<\/option>/', $result);
6734
		$this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
6735
		$this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
6736
		$this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
6737
		$this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
6738
		$this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
6739
	}
6740
 
6741
/**
6742
 * testFormDateTimeMulti method
6743
 *
6744
 * test multiple datetime element generation
6745
 *
6746
 * @return void
6747
 */
6748
	public function testFormDateTimeMulti() {
6749
		extract($this->dateRegex);
6750
 
6751
		$result = $this->Form->dateTime('Contact.1.updated');
6752
		$expected = array(
6753
			array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
6754
			$daysRegex,
6755
			array('option' => array('value' => '')),
6756
			'/option',
6757
			'*/select',
6758
			'-',
6759
			array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
6760
			$monthsRegex,
6761
			array('option' => array('value' => '')),
6762
			'/option',
6763
			'*/select',
6764
			'-',
6765
			array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
6766
			$yearsRegex,
6767
			array('option' => array('value' => '')),
6768
			'/option',
6769
			'*/select',
6770
			array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
6771
			$hoursRegex,
6772
			array('option' => array('value' => '')),
6773
			'/option',
6774
			'*/select',
6775
			':',
6776
			array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
6777
			$minutesRegex,
6778
			array('option' => array('value' => '')),
6779
			'/option',
6780
			'*/select',
6781
			' ',
6782
			array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
6783
			$meridianRegex,
6784
			array('option' => array('value' => '')),
6785
			'/option',
6786
			'*/select'
6787
		);
6788
		$this->assertTags($result, $expected);
6789
 
6790
		$result = $this->Form->dateTime('Contact.2.updated');
6791
		$expected = array(
6792
			array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
6793
			$daysRegex,
6794
			array('option' => array('value' => '')),
6795
			'/option',
6796
			'*/select',
6797
			'-',
6798
			array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
6799
			$monthsRegex,
6800
			array('option' => array('value' => '')),
6801
			'/option',
6802
			'*/select',
6803
			'-',
6804
			array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
6805
			$yearsRegex,
6806
			array('option' => array('value' => '')),
6807
			'/option',
6808
			'*/select',
6809
			array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
6810
			$hoursRegex,
6811
			array('option' => array('value' => '')),
6812
			'/option',
6813
			'*/select',
6814
			':',
6815
			array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
6816
			$minutesRegex,
6817
			array('option' => array('value' => '')),
6818
			'/option',
6819
			'*/select',
6820
			' ',
6821
			array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
6822
			$meridianRegex,
6823
			array('option' => array('value' => '')),
6824
			'/option',
6825
			'*/select'
6826
		);
6827
		$this->assertTags($result, $expected);
6828
	}
6829
 
6830
/**
6831
 * When changing the date format, the label should always focus the first select box when
6832
 * clicked.
6833
 *
6834
 * @return void
6835
 */
6836
	public function testDateTimeLabelIdMatchesFirstInput() {
6837
		$result = $this->Form->input('Model.date', array('type' => 'date'));
6838
		$this->assertContains('label for="ModelDateMonth"', $result);
6839
 
6840
		$result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
6841
		$this->assertContains('label for="ModelDateDay"', $result);
6842
 
6843
		$result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
6844
		$this->assertContains('label for="ModelDateYear"', $result);
6845
	}
6846
 
6847
/**
6848
 * testMonth method
6849
 *
6850
 * @return void
6851
 */
6852
	public function testMonth() {
6853
		$result = $this->Form->month('Model.field');
6854
		$expected = array(
6855
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6856
			array('option' => array('value' => '')),
6857
			'/option',
6858
			array('option' => array('value' => '01')),
6859
			date('F', strtotime('2008-01-01 00:00:00')),
6860
			'/option',
6861
			array('option' => array('value' => '02')),
6862
			date('F', strtotime('2008-02-01 00:00:00')),
6863
			'/option',
6864
			'*/select',
6865
		);
6866
		$this->assertTags($result, $expected);
6867
 
6868
		$result = $this->Form->month('Model.field', array('empty' => true));
6869
		$expected = array(
6870
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6871
			array('option' => array('value' => '')),
6872
			'/option',
6873
			array('option' => array('value' => '01')),
6874
			date('F', strtotime('2008-01-01 00:00:00')),
6875
			'/option',
6876
			array('option' => array('value' => '02')),
6877
			date('F', strtotime('2008-02-01 00:00:00')),
6878
			'/option',
6879
			'*/select',
6880
		);
6881
		$this->assertTags($result, $expected);
6882
 
6883
		$result = $this->Form->month('Model.field', array('monthNames' => false));
6884
		$expected = array(
6885
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6886
			array('option' => array('value' => '')),
6887
			'/option',
6888
			array('option' => array('value' => '01')),
6889
			'01',
6890
			'/option',
6891
			array('option' => array('value' => '02')),
6892
			'02',
6893
			'/option',
6894
			'*/select',
6895
		);
6896
		$this->assertTags($result, $expected);
6897
 
6898
		$monthNames = array(
6899
			'01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
6900
			'07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
6901
		$result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
6902
		$expected = array(
6903
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6904
			array('option' => array('value' => '')),
6905
			'/option',
6906
			array('option' => array('value' => '01')),
6907
			'Jan',
6908
			'/option',
6909
			array('option' => array('value' => '02')),
6910
			'Feb',
6911
			'/option',
6912
			'*/select',
6913
		);
6914
		$this->assertTags($result, $expected);
6915
 
6916
		$this->Form->request->data['Project']['release'] = '2050-02-10';
6917
		$result = $this->Form->month('Project.release');
6918
 
6919
		$expected = array(
6920
			array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
6921
			array('option' => array('value' => '')),
6922
			'/option',
6923
			array('option' => array('value' => '01')),
6924
			'January',
6925
			'/option',
6926
			array('option' => array('value' => '02', 'selected' => 'selected')),
6927
			'February',
6928
			'/option',
6929
			'*/select',
6930
		);
6931
		$this->assertTags($result, $expected);
6932
 
6933
		$this->Form->request->data['Model']['field'] = '12a';
6934
		$result = $this->Form->month('Model.field');
6935
		$expected = array(
6936
			array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
6937
			array('option' => array('value' => '')),
6938
			'/option',
6939
			array('option' => array('value' => '01')),
6940
			date('F', strtotime('2008-01-01 00:00:00')),
6941
			'/option',
6942
			array('option' => array('value' => '02')),
6943
			date('F', strtotime('2008-02-01 00:00:00')),
6944
			'/option',
6945
			'*/select',
6946
		);
6947
		$this->assertTags($result, $expected);
6948
	}
6949
 
6950
/**
6951
 * testDay method
6952
 *
6953
 * @return void
6954
 */
6955
	public function testDay() {
6956
		extract($this->dateRegex);
6957
 
6958
		$result = $this->Form->day('Model.field', array('value' => false));
6959
		$expected = array(
6960
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6961
			array('option' => array('value' => '')),
6962
			'/option',
6963
			array('option' => array('value' => '01')),
6964
			'1',
6965
			'/option',
6966
			array('option' => array('value' => '02')),
6967
			'2',
6968
			'/option',
6969
			$daysRegex,
6970
			'/select',
6971
		);
6972
		$this->assertTags($result, $expected);
6973
 
6974
		$this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
6975
		$result = $this->Form->day('Model.field');
6976
		$expected = array(
6977
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6978
			array('option' => array('value' => '')),
6979
			'/option',
6980
			array('option' => array('value' => '01')),
6981
			'1',
6982
			'/option',
6983
			array('option' => array('value' => '02')),
6984
			'2',
6985
			'/option',
6986
			$daysRegex,
6987
			array('option' => array('value' => '10', 'selected' => 'selected')),
6988
			'10',
6989
			'/option',
6990
			$daysRegex,
6991
			'/select',
6992
		);
6993
		$this->assertTags($result, $expected);
6994
 
6995
		$this->Form->request->data['Model']['field'] = '';
6996
		$result = $this->Form->day('Model.field', array('value' => '10'));
6997
		$expected = array(
6998
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
6999
			array('option' => array('value' => '')),
7000
			'/option',
7001
			array('option' => array('value' => '01')),
7002
			'1',
7003
			'/option',
7004
			array('option' => array('value' => '02')),
7005
			'2',
7006
			'/option',
7007
			$daysRegex,
7008
			array('option' => array('value' => '10', 'selected' => 'selected')),
7009
			'10',
7010
			'/option',
7011
			$daysRegex,
7012
			'/select',
7013
		);
7014
		$this->assertTags($result, $expected);
7015
 
7016
		$this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
7017
		$result = $this->Form->day('Model.field', array('value' => true));
7018
		$expected = array(
7019
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
7020
			array('option' => array('value' => '')),
7021
			'/option',
7022
			array('option' => array('value' => '01')),
7023
			'1',
7024
			'/option',
7025
			array('option' => array('value' => '02')),
7026
			'2',
7027
			'/option',
7028
			$daysRegex,
7029
			array('option' => array('value' => '10', 'selected' => 'selected')),
7030
			'10',
7031
			'/option',
7032
			$daysRegex,
7033
			'/select',
7034
		);
7035
		$this->assertTags($result, $expected);
7036
 
7037
		$this->Form->request->data['Project']['release'] = '2050-10-10';
7038
		$result = $this->Form->day('Project.release');
7039
 
7040
		$expected = array(
7041
			array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
7042
			array('option' => array('value' => '')),
7043
			'/option',
7044
			array('option' => array('value' => '01')),
7045
			'1',
7046
			'/option',
7047
			array('option' => array('value' => '02')),
7048
			'2',
7049
			'/option',
7050
			$daysRegex,
7051
			array('option' => array('value' => '10', 'selected' => 'selected')),
7052
			'10',
7053
			'/option',
7054
			$daysRegex,
7055
			'/select',
7056
		);
7057
		$this->assertTags($result, $expected);
7058
 
7059
		$this->Form->request->data['Model']['field'] = '12e';
7060
		$result = $this->Form->day('Model.field');
7061
		$expected = array(
7062
			array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
7063
			array('option' => array('value' => '')),
7064
			'/option',
7065
			array('option' => array('value' => '01')),
7066
			'1',
7067
			'/option',
7068
			array('option' => array('value' => '02')),
7069
			'2',
7070
			'/option',
7071
			$daysRegex,
7072
			'/select',
7073
		);
7074
		$this->assertTags($result, $expected);
7075
	}
7076
 
7077
/**
7078
 * testMinute method
7079
 *
7080
 * @return void
7081
 */
7082
	public function testMinute() {
7083
		extract($this->dateRegex);
7084
 
7085
		$result = $this->Form->minute('Model.field');
7086
		$expected = array(
7087
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
7088
			array('option' => array('value' => '')),
7089
			'/option',
7090
			array('option' => array('value' => '00')),
7091
			'00',
7092
			'/option',
7093
			array('option' => array('value' => '01')),
7094
			'01',
7095
			'/option',
7096
			array('option' => array('value' => '02')),
7097
			'02',
7098
			'/option',
7099
			$minutesRegex,
7100
			'/select',
7101
		);
7102
		$this->assertTags($result, $expected);
7103
 
7104
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
7105
		$result = $this->Form->minute('Model.field');
7106
		$expected = array(
7107
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
7108
			array('option' => array('value' => '')),
7109
			'/option',
7110
			array('option' => array('value' => '00')),
7111
			'00',
7112
			'/option',
7113
			array('option' => array('value' => '01')),
7114
			'01',
7115
			'/option',
7116
			array('option' => array('value' => '02')),
7117
			'02',
7118
			'/option',
7119
			$minutesRegex,
7120
			array('option' => array('value' => '12', 'selected' => 'selected')),
7121
			'12',
7122
			'/option',
7123
			$minutesRegex,
7124
			'/select',
7125
		);
7126
		$this->assertTags($result, $expected);
7127
 
7128
		$this->Form->request->data['Model']['field'] = '';
7129
		$result = $this->Form->minute('Model.field', array('interval' => 5));
7130
		$expected = array(
7131
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
7132
			array('option' => array('value' => '')),
7133
			'/option',
7134
			array('option' => array('value' => '00')),
7135
			'00',
7136
			'/option',
7137
			array('option' => array('value' => '05')),
7138
			'05',
7139
			'/option',
7140
			array('option' => array('value' => '10')),
7141
			'10',
7142
			'/option',
7143
			$minutesRegex,
7144
			'/select',
7145
		);
7146
		$this->assertTags($result, $expected);
7147
 
7148
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
7149
		$result = $this->Form->minute('Model.field', array('interval' => 5));
7150
		$expected = array(
7151
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
7152
			array('option' => array('value' => '')),
7153
			'/option',
7154
			array('option' => array('value' => '00')),
7155
			'00',
7156
			'/option',
7157
			array('option' => array('value' => '05')),
7158
			'05',
7159
			'/option',
7160
			array('option' => array('value' => '10', 'selected' => 'selected')),
7161
			'10',
7162
			'/option',
7163
			$minutesRegex,
7164
			'/select',
7165
		);
7166
		$this->assertTags($result, $expected);
7167
 
7168
		$result = $this->Form->minute('Model.field', array('value' => '#invalid#'));
7169
		$expected = array(
7170
			array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
7171
			array('option' => array('value' => '')),
7172
			'/option',
7173
			array('option' => array('value' => '00')),
7174
			'00',
7175
			'/option',
7176
			array('option' => array('value' => '01')),
7177
			'01',
7178
			'/option',
7179
			array('option' => array('value' => '02')),
7180
			'02',
7181
			'/option',
7182
			$minutesRegex,
7183
			'/select',
7184
		);
7185
		$this->assertTags($result, $expected);
7186
	}
7187
 
7188
/**
7189
 * testHour method
7190
 *
7191
 * @return void
7192
 */
7193
	public function testHour() {
7194
		extract($this->dateRegex);
7195
 
7196
		$result = $this->Form->hour('Model.field', false);
7197
		$expected = array(
7198
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7199
			array('option' => array('value' => '')),
7200
			'/option',
7201
			array('option' => array('value' => '01')),
7202
			'1',
7203
			'/option',
7204
			array('option' => array('value' => '02')),
7205
			'2',
7206
			'/option',
7207
			$hoursRegex,
7208
			'/select',
7209
		);
7210
		$this->assertTags($result, $expected);
7211
 
7212
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
7213
		$result = $this->Form->hour('Model.field', false);
7214
		$expected = array(
7215
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7216
			array('option' => array('value' => '')),
7217
			'/option',
7218
			array('option' => array('value' => '01')),
7219
			'1',
7220
			'/option',
7221
			array('option' => array('value' => '02')),
7222
			'2',
7223
			'/option',
7224
			$hoursRegex,
7225
			array('option' => array('value' => '12', 'selected' => 'selected')),
7226
			'12',
7227
			'/option',
7228
			'/select',
7229
		);
7230
		$this->assertTags($result, $expected);
7231
 
7232
		$this->Form->request->data['Model']['field'] = '';
7233
		$result = $this->Form->hour('Model.field', true, array('value' => '23'));
7234
		$this->assertContains('<option value="23" selected="selected">23</option>', $result);
7235
 
7236
		$result = $this->Form->hour('Model.field', false, array('value' => '23'));
7237
		$this->assertContains('<option value="11" selected="selected">11</option>', $result);
7238
 
7239
		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
7240
		$result = $this->Form->hour('Model.field', true);
7241
		$expected = array(
7242
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7243
			array('option' => array('value' => '')),
7244
			'/option',
7245
			array('option' => array('value' => '00', 'selected' => 'selected')),
7246
			'0',
7247
			'/option',
7248
			array('option' => array('value' => '01')),
7249
			'1',
7250
			'/option',
7251
			array('option' => array('value' => '02')),
7252
			'2',
7253
			'/option',
7254
			$hoursRegex,
7255
			'/select',
7256
		);
7257
		$this->assertTags($result, $expected);
7258
 
7259
		unset($this->Form->request->data['Model']['field']);
7260
		$result = $this->Form->hour('Model.field', true, array('value' => 'now'));
7261
		$thisHour = date('H');
7262
		$optValue = date('G');
7263
		$this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
7264
 
7265
		$this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
7266
		$result = $this->Form->hour('Model.field', true);
7267
		$expected = array(
7268
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7269
			array('option' => array('value' => '')),
7270
			'/option',
7271
			array('option' => array('value' => '00')),
7272
			'0',
7273
			'/option',
7274
			array('option' => array('value' => '01', 'selected' => 'selected')),
7275
			'1',
7276
			'/option',
7277
			array('option' => array('value' => '02')),
7278
			'2',
7279
			'/option',
7280
			$hoursRegex,
7281
			'/select',
7282
		);
7283
		$this->assertTags($result, $expected);
7284
 
7285
		$this->Form->request->data['Model']['field'] = '18a';
7286
		$result = $this->Form->hour('Model.field', false);
7287
		$expected = array(
7288
			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
7289
			array('option' => array('value' => '')),
7290
			'/option',
7291
			array('option' => array('value' => '01')),
7292
			'1',
7293
			'/option',
7294
			array('option' => array('value' => '02')),
7295
			'2',
7296
			'/option',
7297
			$hoursRegex,
7298
			'/select',
7299
		);
7300
		$this->assertTags($result, $expected);
7301
	}
7302
 
7303
/**
7304
 * testYear method
7305
 *
7306
 * @return void
7307
 */
7308
	public function testYear() {
7309
		$result = $this->Form->year('Model.field', 2006, 2007);
7310
		$expected = array(
7311
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
7312
			array('option' => array('value' => '')),
7313
			'/option',
7314
			array('option' => array('value' => '2007')),
7315
			'2007',
7316
			'/option',
7317
			array('option' => array('value' => '2006')),
7318
			'2006',
7319
			'/option',
7320
			'/select',
7321
		);
7322
		$this->assertTags($result, $expected);
7323
 
7324
		$result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
7325
		$expected = array(
7326
			array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
7327
			array('option' => array('value' => '')),
7328
			'/option',
7329
			array('option' => array('value' => '2006')),
7330
			'2006',
7331
			'/option',
7332
			array('option' => array('value' => '2007')),
7333
			'2007',
7334
			'/option',
7335
			'/select',
7336
		);
7337
		$this->assertTags($result, $expected);
7338
 
7339
		$this->request->data['Contact']['published'] = '';
7340
		$result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
7341
		$expected = array(
7342
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
7343
			array('option' => array('value' => '')),
7344
			'/option',
7345
			array('option' => array('value' => '2007')),
7346
			'2007',
7347
			'/option',
7348
			array('option' => array('value' => '2006')),
7349
			'2006',
7350
			'/option',
7351
			'/select',
7352
		);
7353
		$this->assertTags($result, $expected);
7354
 
7355
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7356
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
7357
		$expected = array(
7358
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7359
			array('option' => array('value' => '2007')),
7360
			'2007',
7361
			'/option',
7362
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7363
			'2006',
7364
			'/option',
7365
			'/select',
7366
		);
7367
		$this->assertTags($result, $expected);
7368
 
7369
		$this->Form->request->data['Contact']['published'] = '';
7370
		$result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
7371
		$expected = array(
7372
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7373
			array('option' => array('value' => '')),
7374
			'/option',
7375
			array('option' => array('value' => '2007')),
7376
			'2007',
7377
			'/option',
7378
			array('option' => array('value' => '2006')),
7379
			'2006',
7380
			'/option',
7381
			'/select',
7382
		);
7383
		$this->assertTags($result, $expected);
7384
 
7385
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7386
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
7387
		$expected = array(
7388
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7389
			array('option' => array('value' => '2007')),
7390
			'2007',
7391
			'/option',
7392
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7393
			'2006',
7394
			'/option',
7395
			'/select',
7396
		);
7397
		$this->assertTags($result, $expected);
7398
 
7399
		$this->Form->request->data['Contact']['published'] = '';
7400
		$result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
7401
		$expected = array(
7402
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7403
			array('option' => array('value' => '')),
7404
			'/option',
7405
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7406
			'2007',
7407
			'/option',
7408
			array('option' => array('value' => '2006')),
7409
			'2006',
7410
			'/option',
7411
			'/select',
7412
		);
7413
		$this->assertTags($result, $expected);
7414
 
7415
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7416
		$result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
7417
		$expected = array(
7418
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7419
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7420
			'2007',
7421
			'/option',
7422
			array('option' => array('value' => '2006')),
7423
			'2006',
7424
			'/option',
7425
			'/select',
7426
		);
7427
		$this->assertTags($result, $expected);
7428
 
7429
		$this->Form->request->data['Contact']['published'] = '';
7430
		$result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
7431
		$expected = array(
7432
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7433
			array('option' => array('value' => '2008')),
7434
			'2008',
7435
			'/option',
7436
			array('option' => array('value' => '2007', 'selected' => 'selected')),
7437
			'2007',
7438
			'/option',
7439
			array('option' => array('value' => '2006')),
7440
			'2006',
7441
			'/option',
7442
			'/select',
7443
		);
7444
		$this->assertTags($result, $expected);
7445
 
7446
		$this->Form->request->data['Contact']['published'] = '2006-10-10';
7447
		$result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
7448
		$expected = array(
7449
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7450
			array('option' => array('value' => '2008')),
7451
			'2008',
7452
			'/option',
7453
			array('option' => array('value' => '2007')),
7454
			'2007',
7455
			'/option',
7456
			array('option' => array('value' => '2006', 'selected' => 'selected')),
7457
			'2006',
7458
			'/option',
7459
			'/select',
7460
		);
7461
		$this->assertTags($result, $expected);
7462
 
7463
		$this->Form->request->data = array();
7464
		$this->Form->create('Contact');
7465
		$result = $this->Form->year('published', 2006, 2008, array('empty' => false));
7466
		$expected = array(
7467
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7468
			array('option' => array('value' => '2008')),
7469
			'2008',
7470
			'/option',
7471
			array('option' => array('value' => '2007')),
7472
			'2007',
7473
			'/option',
7474
			array('option' => array('value' => '2006')),
7475
			'2006',
7476
			'/option',
7477
			'/select',
7478
		);
7479
		$this->assertTags($result, $expected);
7480
 
7481
		$result = $this->Form->year('published', array(), array(), array('empty' => false));
7482
		$this->assertContains('data[Contact][published][year]', $result);
7483
 
7484
		$this->Form->request->data['Contact']['published'] = '2014ee';
7485
		$result = $this->Form->year('Contact.published', 2010, 2011);
7486
		$expected = array(
7487
			array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
7488
			array('option' => array('value' => '')),
7489
			'/option',
7490
			array('option' => array('value' => '2011')),
7491
			'2011',
7492
			'/option',
7493
			array('option' => array('value' => '2010')),
7494
			'2010',
7495
			'/option',
7496
			'/select',
7497
		);
7498
		$this->assertTags($result, $expected);
7499
	}
7500
 
7501
/**
7502
 * testYearAutoExpandRange method
7503
 *
7504
 * @return void
7505
 */
7506
	public function testYearAutoExpandRange() {
7507
		$this->Form->request->data['User']['birthday'] = '1930-10-10';
7508
		$result = $this->Form->year('User.birthday');
7509
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7510
 
7511
		$result = $matches[1];
7512
		$expected = range(date('Y') + 20, 1930);
7513
		$this->assertEquals($expected, $result);
7514
 
7515
		$this->Form->request->data['Project']['release'] = '2050-10-10';
7516
		$result = $this->Form->year('Project.release');
7517
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7518
 
7519
		$result = $matches[1];
7520
		$expected = range(2050, date('Y') - 20);
7521
		$this->assertEquals($expected, $result);
7522
 
7523
		$this->Form->request->data['Project']['release'] = '1881-10-10';
7524
		$result = $this->Form->year('Project.release', 1890, 1900);
7525
		preg_match_all('/<option value="([\d]+)"/', $result, $matches);
7526
 
7527
		$result = $matches[1];
7528
		$expected = range(1900, 1881);
7529
		$this->assertEquals($expected, $result);
7530
	}
7531
 
7532
/**
7533
 * testInputDate method
7534
 *
7535
 * Test various inputs with type date and different dateFormat values.
7536
 * Failing to provide a dateFormat key should not error.
7537
 * It should simply not pre-select any value then.
7538
 *
7539
 * @return void
7540
 */
7541
	public function testInputDate() {
7542
		$this->Form->request->data = array(
7543
			'User' => array(
7544
				'month_year' => array('month' => date('m')),
7545
				'just_year' => array('month' => date('m')),
7546
				'just_month' => array('year' => date('Y')),
7547
				'just_day' => array('month' => date('m')),
7548
			)
7549
		);
7550
		$this->Form->create('User');
7551
		$result = $this->Form->input('month_year',
7552
				array(
7553
					'label' => false,
7554
					'div' => false,
7555
					'type' => 'date',
7556
					'dateFormat' => 'MY',
7557
					'minYear' => 2006,
7558
					'maxYear' => 2008
7559
				)
7560
		);
7561
		$this->assertContains('value="' . date('m') . '" selected="selected"', $result);
7562
		$this->assertNotContains('value="2008" selected="selected"', $result);
7563
 
7564
		$result = $this->Form->input('just_year',
7565
			array(
7566
				'type' => 'date',
7567
				'label' => false,
7568
				'dateFormat' => 'Y',
7569
				'minYear' => date('Y'),
7570
				'maxYear' => date('Y', strtotime('+20 years'))
7571
			)
7572
		);
7573
		$this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
7574
 
7575
		$result = $this->Form->input('just_month',
7576
			array(
7577
				'type' => 'date',
7578
				'label' => false,
7579
				'dateFormat' => 'M',
7580
				'empty' => false,
7581
			)
7582
		);
7583
		$this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
7584
 
7585
		$result = $this->Form->input('just_day',
7586
			array(
7587
				'type' => 'date',
7588
				'label' => false,
7589
				'dateFormat' => 'D',
7590
				'empty' => false,
7591
			)
7592
		);
7593
		$this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
7594
	}
7595
 
7596
/**
7597
 * testInputDate method
7598
 *
7599
 * Test various inputs with type date and different option attributes.
7600
 *
7601
 * @return void
7602
 */
7603
	public function testInputDateOptions() {
7604
		$this->Form->create('User');
7605
 
7606
		$result = $this->Form->input('date',
7607
			array(
7608
				'label' => false,
7609
				'type' => 'day',
7610
				'class' => 'form-control'
7611
			)
7612
		);
7613
		$this->assertContains('class="form-control"', $result);
7614
 
7615
		$result = $this->Form->input('date',
7616
			array(
7617
				'label' => false,
7618
				'type' => 'month',
7619
				'class' => 'form-control'
7620
			)
7621
		);
7622
		$this->assertContains('class="form-control"', $result);
7623
 
7624
		$result = $this->Form->input('date',
7625
			array(
7626
				'label' => false,
7627
				'type' => 'year',
7628
				'class' => 'form-control'
7629
			)
7630
		);
7631
		$this->assertContains('class="form-control"', $result);
7632
 
7633
		$result = $this->Form->input('date',
7634
			array(
7635
				'label' => false,
7636
				'type' => 'hour',
7637
				'class' => 'form-control'
7638
			)
7639
		);
7640
		$this->assertContains('class="form-control"', $result);
7641
	}
7642
 
7643
/**
7644
 * testInputDateMaxYear method
7645
 *
7646
 * Let's say we want to only allow users born from 2006 to 2008 to register
7647
 * This being the first singup page, we still don't have any data
7648
 *
7649
 * @return void
7650
 */
7651
	public function testInputDateMaxYear() {
7652
		$this->Form->request->data = array();
7653
		$this->Form->create('User');
7654
		$result = $this->Form->input('birthday',
7655
				array(
7656
					'label' => false,
7657
					'div' => false,
7658
					'type' => 'date',
7659
					'dateFormat' => 'DMY',
7660
					'minYear' => 2006,
7661
					'maxYear' => 2008
7662
				)
7663
		);
7664
		$this->assertContains('value="2008" selected="selected"', $result);
7665
	}
7666
 
7667
/**
7668
 * testTextArea method
7669
 *
7670
 * @return void
7671
 */
7672
	public function testTextArea() {
7673
		$this->Form->request->data = array('Model' => array('field' => 'some test data'));
7674
		$result = $this->Form->textarea('Model.field');
7675
		$expected = array(
7676
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7677
			'some test data',
7678
			'/textarea',
7679
		);
7680
		$this->assertTags($result, $expected);
7681
 
7682
		$result = $this->Form->textarea('Model.tmp');
7683
		$expected = array(
7684
			'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
7685
			'/textarea',
7686
		);
7687
		$this->assertTags($result, $expected);
7688
 
7689
		$this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
7690
		$result = $this->Form->textarea('Model.field');
7691
		$expected = array(
7692
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7693
			htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
7694
			'/textarea',
7695
		);
7696
		$this->assertTags($result, $expected);
7697
 
7698
		$this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
7699
		$result = $this->Form->textarea('Model.field', array('escape' => false));
7700
		$expected = array(
7701
			'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
7702
			'some <strong>test</strong> data with <a href="#">HTML</a> chars',
7703
			'/textarea',
7704
		);
7705
		$this->assertTags($result, $expected);
7706
 
7707
		$this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
7708
		$result = $this->Form->textarea('Model.0.OtherModel.field');
7709
		$expected = array(
7710
			'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
7711
			'/textarea'
7712
		);
7713
		$this->assertTags($result, $expected);
7714
	}
7715
 
7716
/**
7717
 * Test textareas maxlength reading from schema.
7718
 *
7719
 * @return void
7720
 */
7721
	public function testTextAreaMaxLength() {
7722
		$result = $this->Form->input('UserForm.other', array('type' => 'textarea'));
7723
		$expected = array(
7724
			'div' => array('class' => 'input textarea'),
7725
				'label' => array('for' => 'UserFormOther'),
7726
					'Other',
7727
				'/label',
7728
				'textarea' => array('name' => 'data[UserForm][other]', 'cols' => '30', 'rows' => '6', 'id' => 'UserFormOther'),
7729
				'/textarea',
7730
			'/div'
7731
		);
7732
		$this->assertTags($result, $expected);
7733
 
7734
		$result = $this->Form->input('UserForm.stuff', array('type' => 'textarea'));
7735
		$expected = array(
7736
			'div' => array('class' => 'input textarea'),
7737
				'label' => array('for' => 'UserFormStuff'),
7738
					'Stuff',
7739
				'/label',
7740
				'textarea' => array('name' => 'data[UserForm][stuff]', 'maxlength' => 10, 'cols' => '30', 'rows' => '6', 'id' => 'UserFormStuff'),
7741
				'/textarea',
7742
			'/div'
7743
		);
7744
		$this->assertTags($result, $expected);
7745
	}
7746
 
7747
/**
7748
 * testTextAreaWithStupidCharacters method
7749
 *
7750
 * test text area with non-ascii characters
7751
 *
7752
 * @return void
7753
 */
7754
	public function testTextAreaWithStupidCharacters() {
7755
		$this->loadFixtures('Post');
7756
		$result = $this->Form->input('Post.content', array(
7757
			'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
7758
		));
7759
		$expected = array(
7760
			'div' => array('class' => 'input textarea'),
7761
				'label' => array('for' => 'PostContent'),
7762
					'Current Text',
7763
				'/label',
7764
				'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
7765
				'GREAT®',
7766
				'/textarea',
7767
			'/div'
7768
		);
7769
		$this->assertTags($result, $expected);
7770
	}
7771
 
7772
/**
7773
 * testHiddenField method
7774
 *
7775
 * @return void
7776
 */
7777
	public function testHiddenField() {
7778
		$Contact = ClassRegistry::getObject('Contact');
7779
		$Contact->validationErrors['field'] = 1;
7780
		$this->Form->request->data['Contact']['field'] = 'test';
7781
		$result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
7782
		$this->assertTags($result, array(
7783
			'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
7784
		);
7785
	}
7786
 
7787
/**
7788
 * testFileUploadField method
7789
 *
7790
 * @return void
7791
 */
7792
	public function testFileUploadField() {
7793
		$result = $this->Form->file('Model.upload');
7794
		$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
7795
 
7796
		$this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
7797
		$result = $this->Form->input('Model.upload', array('type' => 'file'));
7798
		$expected = array(
7799
			'div' => array('class' => 'input file'),
7800
			'label' => array('for' => 'ModelUpload'),
7801
			'Upload',
7802
			'/label',
7803
			'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
7804
			'/div'
7805
		);
7806
		$this->assertTags($result, $expected);
7807
 
7808
		$this->Form->request->data['Model']['upload'] = 'no data should be set in value';
7809
		$result = $this->Form->file('Model.upload');
7810
		$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
7811
	}
7812
 
7813
/**
7814
 * test File upload input on a model not used in create();
7815
 *
7816
 * @return void
7817
 */
7818
	public function testFileUploadOnOtherModel() {
7819
		$this->Form->create('ValidateUser', array('type' => 'file'));
7820
		$result = $this->Form->file('ValidateProfile.city');
7821
		$expected = array(
7822
			'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
7823
		);
7824
		$this->assertTags($result, $expected);
7825
	}
7826
 
7827
/**
7828
 * testButton method
7829
 *
7830
 * @return void
7831
 */
7832
	public function testButton() {
7833
		$result = $this->Form->button('Hi');
7834
		$this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
7835
 
7836
		$result = $this->Form->button('Clear Form >', array('type' => 'reset'));
7837
		$this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
7838
 
7839
		$result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
7840
		$this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
7841
 
7842
		$result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
7843
		$this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
7844
 
7845
		$result = $this->Form->button('No type', array('type' => false));
7846
		$this->assertTags($result, array('button' => array(), 'No type', '/button'));
7847
 
7848
		$result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
7849
		$this->assertNotRegExp('/\&039/', $result);
7850
	}
7851
 
7852
/**
7853
 * Test that button() makes unlocked fields by default.
7854
 *
7855
 * @return void
7856
 */
7857
	public function testButtonUnlockedByDefault() {
7858
		$this->Form->request->params['_Token']['key'] = 'secured';
7859
		$this->Form->button('Save', array('name' => 'save'));
7860
		$this->Form->button('Clear');
7861
 
7862
		$result = $this->Form->unlockField();
7863
		$this->assertEquals(array('save'), $result);
7864
	}
7865
 
7866
/**
7867
 * testPostButton method
7868
 *
7869
 * @return void
7870
 */
7871
	public function testPostButton() {
7872
		$result = $this->Form->postButton('Hi', '/controller/action');
7873
		$this->assertTags($result, array(
7874
			'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
7875
			'div' => array('style' => 'display:none;'),
7876
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7877
			'/div',
7878
			'button' => array('type' => 'submit'),
7879
			'Hi',
7880
			'/button',
7881
			'/form'
7882
		));
7883
 
7884
		$result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
7885
		$this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
7886
	}
7887
 
7888
/**
7889
 * Test using postButton with N dimensional data.
7890
 *
7891
 * @return void
7892
 */
7893
	public function testPostButtonNestedData() {
7894
		$data = array(
7895
			'one' => array(
7896
				'two' => array(
7897
					3, 4, 5
7898
				)
7899
			)
7900
		);
7901
		$result = $this->Form->postButton('Send', '/', array('data' => $data));
7902
		$this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
7903
		$this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
7904
		$this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
7905
	}
7906
 
7907
/**
7908
 * Test that postButton adds _Token fields.
7909
 *
7910
 * @return void
7911
 */
7912
	public function testSecurePostButton() {
7913
		$this->Form->request->params['_Token'] = array('key' => 'testkey');
7914
 
7915
		$result = $this->Form->postButton('Delete', '/posts/delete/1');
7916
		$expected = array(
7917
			'form' => array(
7918
				'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
7919
			),
7920
			array('div' => array('style' => 'display:none;')),
7921
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
7922
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
7923
			'/div',
7924
			'button' => array('type' => 'submit'),
7925
			'Delete',
7926
			'/button',
7927
			array('div' => array('style' => 'display:none;')),
7928
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
7929
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
7930
			'/div',
7931
			'/form',
7932
		);
7933
		$this->assertTags($result, $expected);
7934
	}
7935
 
7936
/**
7937
 * testPostLink method
7938
 *
7939
 * @return void
7940
 */
7941
	public function testPostLink() {
7942
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
7943
		$this->assertTags($result, array(
7944
			'form' => array(
7945
				'method' => 'post', 'action' => '/posts/delete/1',
7946
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7947
			),
7948
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7949
			'/form',
7950
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7951
			'Delete',
7952
			'/a'
7953
		));
7954
 
7955
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
7956
		$this->assertTags($result, array(
7957
			'form' => array(
7958
				'method' => 'post', 'action' => '/posts/delete/1',
7959
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7960
			),
7961
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
7962
			'/form',
7963
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
7964
			'Delete',
7965
			'/a'
7966
		));
7967
 
7968
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
7969
		$this->assertTags($result, array(
7970
			'form' => array(
7971
				'method' => 'post', 'action' => '/posts/delete/1',
7972
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7973
			),
7974
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7975
			'/form',
7976
			'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
7977
			'Delete',
7978
			'/a'
7979
		));
7980
 
7981
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
7982
		$this->assertTags($result, array(
7983
			'form' => array(
7984
				'method' => 'post', 'action' => '/posts/delete/1',
7985
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
7986
			),
7987
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
7988
			'/form',
7989
			'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;/'),
7990
			'Delete',
7991
			'/a'
7992
		));
7993
 
7994
		$result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
7995
		$this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
7996
 
7997
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
7998
		$this->assertTags($result, array(
7999
			'form' => array(
8000
				'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
8001
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8002
			),
8003
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8004
			'/form',
8005
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8006
			'Delete',
8007
			'/a'
8008
		));
8009
 
8010
		$result = $this->Form->postLink(
8011
			'',
8012
			array('controller' => 'items', 'action' => 'delete', 10),
8013
			array('class' => 'btn btn-danger', 'escape' => false),
8014
			'Confirm thing'
8015
		);
8016
		$this->assertTags($result, array(
8017
			'form' => array(
8018
				'method' => 'post', 'action' => '/items/delete/10',
8019
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8020
			),
8021
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8022
			'/form',
8023
			'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
8024
			'/a'
8025
		));
8026
	}
8027
 
8028
/**
8029
 * Test that security hashes for postLink include the url.
8030
 *
8031
 * @return void
8032
 */
8033
	public function testPostLinkSecurityHash() {
8034
		$hash = Security::hash(
8035
			'/posts/delete/1' .
8036
			serialize(array()) .
8037
			'' .
8038
			Configure::read('Security.salt')
8039
		);
8040
		$hash .= '%3A';
8041
		$this->Form->request->params['_Token']['key'] = 'test';
8042
 
8043
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
8044
		$this->assertTags($result, array(
8045
			'form' => array(
8046
				'method' => 'post', 'action' => '/posts/delete/1',
8047
				'name', 'id', 'style' => 'display:none;'
8048
			),
8049
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
8050
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'test', 'id')),
8051
			'div' => array('style' => 'display:none;'),
8052
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => $hash, 'id')),
8053
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id')),
8054
			'/div',
8055
			'/form',
8056
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8057
			'Delete',
8058
			'/a'
8059
		));
8060
	}
8061
 
8062
/**
8063
 * Test using postLink with N dimensional data.
8064
 *
8065
 * @return void
8066
 */
8067
	public function testPostLinkNestedData() {
8068
		$data = array(
8069
			'one' => array(
8070
				'two' => array(
8071
					3, 4, 5
8072
				)
8073
			)
8074
		);
8075
		$result = $this->Form->postLink('Send', '/', array('data' => $data));
8076
		$this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
8077
		$this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
8078
		$this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
8079
	}
8080
 
8081
/**
8082
 * test creating postLinks after a GET form.
8083
 *
8084
 * @return void
8085
 */
8086
	public function testPostLinkAfterGetForm() {
8087
		$this->Form->request->params['_Token']['key'] = 'testkey';
8088
		$this->Form->create('User', array('type' => 'get'));
8089
		$this->Form->end();
8090
 
8091
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
8092
		$this->assertTags($result, array(
8093
			'form' => array(
8094
				'method' => 'post', 'action' => '/posts/delete/1',
8095
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8096
			),
8097
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
8098
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
8099
			'div' => array('style' => 'display:none;'),
8100
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
8101
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
8102
			'/div',
8103
			'/form',
8104
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8105
			'Delete',
8106
			'/a'
8107
		));
8108
	}
8109
 
8110
/**
8111
 * Test that postLink adds _Token fields.
8112
 *
8113
 * @return void
8114
 */
8115
	public function testSecurePostLink() {
8116
		$this->Form->request->params['_Token'] = array('key' => 'testkey');
8117
 
8118
		$result = $this->Form->postLink('Delete', '/posts/delete/1');
8119
		$expected = array(
8120
			'form' => array(
8121
				'method' => 'post', 'action' => '/posts/delete/1',
8122
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8123
			),
8124
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
8125
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
8126
			'div' => array('style' => 'display:none;'),
8127
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
8128
			array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
8129
			'/div',
8130
			'/form',
8131
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8132
			'Delete',
8133
			'/a'
8134
		);
8135
		$this->assertTags($result, $expected);
8136
	}
8137
 
8138
/**
8139
 * Test that postLink adds form tags to view block
8140
 *
8141
 * @return void
8142
 */
8143
	public function testPostLinkFormBuffer() {
8144
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
8145
		$this->assertTags($result, array(
8146
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8147
			'Delete',
8148
			'/a'
8149
		));
8150
 
8151
		$result = $this->View->fetch('postLink');
8152
		$this->assertTags($result, array(
8153
			'form' => array(
8154
				'method' => 'post', 'action' => '/posts/delete/1',
8155
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8156
			),
8157
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8158
			'/form'
8159
		));
8160
 
8161
		$result = $this->Form->postLink('Delete', '/posts/delete/2',
8162
			array('inline' => false, 'method' => 'DELETE')
8163
		);
8164
		$this->assertTags($result, array(
8165
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8166
			'Delete',
8167
			'/a'
8168
		));
8169
 
8170
		$result = $this->View->fetch('postLink');
8171
		$this->assertTags($result, array(
8172
			'form' => array(
8173
				'method' => 'post', 'action' => '/posts/delete/1',
8174
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8175
			),
8176
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8177
			'/form',
8178
			array(
8179
				'form' => array(
8180
					'method' => 'post', 'action' => '/posts/delete/2',
8181
					'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8182
				),
8183
			),
8184
			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
8185
			'/form'
8186
		));
8187
 
8188
		$result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
8189
		$this->assertTags($result, array(
8190
			'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
8191
			'Delete',
8192
			'/a'
8193
		));
8194
 
8195
		$result = $this->View->fetch('foobar');
8196
		$this->assertTags($result, array(
8197
			'form' => array(
8198
				'method' => 'post', 'action' => '/posts/delete/1',
8199
				'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
8200
			),
8201
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8202
			'/form'
8203
		));
8204
	}
8205
 
8206
/**
8207
 * testSubmitButton method
8208
 *
8209
 * @return void
8210
 */
8211
	public function testSubmitButton() {
8212
		$result = $this->Form->submit('');
8213
		$expected = array(
8214
			'div' => array('class' => 'submit'),
8215
			'input' => array('type' => 'submit', 'value' => ''),
8216
			'/div'
8217
		);
8218
		$this->assertTags($result, $expected);
8219
 
8220
		$result = $this->Form->submit('Test Submit');
8221
		$expected = array(
8222
			'div' => array('class' => 'submit'),
8223
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
8224
			'/div'
8225
		);
8226
		$this->assertTags($result, $expected);
8227
 
8228
		$result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
8229
		$expected = array(
8230
			'span' => array('class' => 'submit'),
8231
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
8232
			'/span'
8233
		);
8234
		$this->assertTags($result, $expected);
8235
 
8236
		$result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
8237
		$expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
8238
		$this->assertTags($result, $expected);
8239
 
8240
		$result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
8241
		$expected = array(
8242
			'div' => array('class' => 'submit', 'id' => 'SaveButton'),
8243
			'input' => array('type' => 'submit', 'value' => 'Test Submit'),
8244
			'/div'
8245
		);
8246
		$this->assertTags($result, $expected);
8247
 
8248
		$result = $this->Form->submit('Next >');
8249
		$expected = array(
8250
			'div' => array('class' => 'submit'),
8251
			'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
8252
			'/div'
8253
		);
8254
		$this->assertTags($result, $expected);
8255
 
8256
		$result = $this->Form->submit('Next >', array('escape' => false));
8257
		$expected = array(
8258
			'div' => array('class' => 'submit'),
8259
			'input' => array('type' => 'submit', 'value' => 'Next >'),
8260
			'/div'
8261
		);
8262
		$this->assertTags($result, $expected);
8263
 
8264
		$result = $this->Form->submit('Reset!', array('type' => 'reset'));
8265
		$expected = array(
8266
			'div' => array('class' => 'submit'),
8267
			'input' => array('type' => 'reset', 'value' => 'Reset!'),
8268
			'/div'
8269
		);
8270
		$this->assertTags($result, $expected);
8271
 
8272
		$before = '--before--';
8273
		$after = '--after--';
8274
		$result = $this->Form->submit('Test', array('before' => $before));
8275
		$expected = array(
8276
			'div' => array('class' => 'submit'),
8277
			'--before--',
8278
			'input' => array('type' => 'submit', 'value' => 'Test'),
8279
			'/div'
8280
		);
8281
		$this->assertTags($result, $expected);
8282
 
8283
		$result = $this->Form->submit('Test', array('after' => $after));
8284
		$expected = array(
8285
			'div' => array('class' => 'submit'),
8286
			'input' => array('type' => 'submit', 'value' => 'Test'),
8287
			'--after--',
8288
			'/div'
8289
		);
8290
		$this->assertTags($result, $expected);
8291
 
8292
		$result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
8293
		$expected = array(
8294
			'div' => array('class' => 'submit'),
8295
			'--before--',
8296
			'input' => array('type' => 'submit', 'value' => 'Test'),
8297
			'--after--',
8298
			'/div'
8299
		);
8300
		$this->assertTags($result, $expected);
8301
	}
8302
 
8303
/**
8304
 * test image submit types.
8305
 *
8306
 * @return void
8307
 */
8308
	public function testSubmitImage() {
8309
		$result = $this->Form->submit('http://example.com/cake.power.gif');
8310
		$expected = array(
8311
			'div' => array('class' => 'submit'),
8312
			'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
8313
			'/div'
8314
		);
8315
		$this->assertTags($result, $expected);
8316
 
8317
		$result = $this->Form->submit('/relative/cake.power.gif');
8318
		$expected = array(
8319
			'div' => array('class' => 'submit'),
8320
			'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
8321
			'/div'
8322
		);
8323
		$this->assertTags($result, $expected);
8324
 
8325
		$result = $this->Form->submit('cake.power.gif');
8326
		$expected = array(
8327
			'div' => array('class' => 'submit'),
8328
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8329
			'/div'
8330
		);
8331
		$this->assertTags($result, $expected);
8332
 
8333
		$result = $this->Form->submit('Not.an.image');
8334
		$expected = array(
8335
			'div' => array('class' => 'submit'),
8336
			'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
8337
			'/div'
8338
		);
8339
		$this->assertTags($result, $expected);
8340
 
8341
		$after = '--after--';
8342
		$before = '--before--';
8343
		$result = $this->Form->submit('cake.power.gif', array('after' => $after));
8344
		$expected = array(
8345
			'div' => array('class' => 'submit'),
8346
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8347
			'--after--',
8348
			'/div'
8349
		);
8350
		$this->assertTags($result, $expected);
8351
 
8352
		$result = $this->Form->submit('cake.power.gif', array('before' => $before));
8353
		$expected = array(
8354
			'div' => array('class' => 'submit'),
8355
			'--before--',
8356
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8357
			'/div'
8358
		);
8359
		$this->assertTags($result, $expected);
8360
 
8361
		$result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
8362
		$expected = array(
8363
			'div' => array('class' => 'submit'),
8364
			'--before--',
8365
			'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
8366
			'--after--',
8367
			'/div'
8368
		);
8369
		$this->assertTags($result, $expected);
8370
 
8371
		$result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
8372
		$expected = array(
8373
			'div' => array('class' => 'submit'),
8374
			'--before--',
8375
			'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
8376
			'--after--',
8377
			'/div'
8378
		);
8379
		$this->assertTags($result, $expected);
8380
	}
8381
 
8382
/**
8383
 * Submit buttons should be unlocked by default as there could be multiples, and only one will
8384
 * be submitted at a time.
8385
 *
8386
 * @return void
8387
 */
8388
	public function testSubmitUnlockedByDefault() {
8389
		$this->Form->request->params['_Token']['key'] = 'secured';
8390
		$this->Form->submit('Go go');
8391
		$this->Form->submit('Save', array('name' => 'save'));
8392
 
8393
		$result = $this->Form->unlockField();
8394
		$this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
8395
	}
8396
 
8397
/**
8398
 * Test submit image with timestamps.
8399
 *
8400
 * @return void
8401
 */
8402
	public function testSubmitImageTimestamp() {
8403
		Configure::write('Asset.timestamp', 'force');
8404
 
8405
		$result = $this->Form->submit('cake.power.gif');
8406
		$expected = array(
8407
			'div' => array('class' => 'submit'),
8408
			'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
8409
			'/div'
8410
		);
8411
		$this->assertTags($result, $expected);
8412
	}
8413
 
8414
/**
8415
 * test the create() method
8416
 *
8417
 * @return void
8418
 */
8419
	public function testCreate() {
8420
		$result = $this->Form->create('Contact');
8421
		$encoding = strtolower(Configure::read('App.encoding'));
8422
		$expected = array(
8423
			'form' => array(
8424
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8425
				'accept-charset' => $encoding
8426
			),
8427
			'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
8428
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8429
			'/div'
8430
		);
8431
		$this->assertTags($result, $expected);
8432
 
8433
		$result = $this->Form->create('Contact', array('type' => 'GET'));
8434
		$expected = array('form' => array(
8435
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8436
			'accept-charset' => $encoding
8437
		));
8438
		$this->assertTags($result, $expected);
8439
 
8440
		$result = $this->Form->create('Contact', array('type' => 'get'));
8441
		$expected = array('form' => array(
8442
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8443
			'accept-charset' => $encoding
8444
		));
8445
		$this->assertTags($result, $expected);
8446
 
8447
		$result = $this->Form->create('Contact', array('type' => 'put'));
8448
		$expected = array(
8449
			'form' => array(
8450
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8451
				'accept-charset' => $encoding
8452
			),
8453
			'div' => array('style' => 'display:none;'),
8454
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8455
			'/div'
8456
		);
8457
		$this->assertTags($result, $expected);
8458
 
8459
		$result = $this->Form->create('Contact', array('type' => 'file'));
8460
		$expected = array(
8461
			'form' => array(
8462
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
8463
				'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
8464
			),
8465
			'div' => array('style' => 'display:none;'),
8466
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8467
			'/div'
8468
		);
8469
		$this->assertTags($result, $expected);
8470
 
8471
		$this->Form->request->data['Contact']['id'] = 1;
8472
		$this->Form->request->here = '/contacts/edit/1';
8473
		$this->Form->request['action'] = 'edit';
8474
		$result = $this->Form->create('Contact');
8475
		$expected = array(
8476
			'form' => array(
8477
				'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
8478
				'accept-charset' => $encoding
8479
			),
8480
			'div' => array('style' => 'display:none;'),
8481
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8482
			'/div'
8483
		);
8484
		$this->assertTags($result, $expected);
8485
 
8486
		$this->Form->request->data['Contact']['id'] = 1;
8487
		$this->Form->request->here = '/contacts/edit/1';
8488
		$this->Form->request['action'] = 'edit';
8489
		$result = $this->Form->create('Contact', array('type' => 'file'));
8490
		$expected = array(
8491
			'form' => array(
8492
				'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
8493
				'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
8494
			),
8495
			'div' => array('style' => 'display:none;'),
8496
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8497
			'/div'
8498
		);
8499
		$this->assertTags($result, $expected);
8500
 
8501
		$this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
8502
		$result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
8503
		$expected = array(
8504
			'form' => array(
8505
				'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
8506
				'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
8507
			),
8508
			'div' => array('style' => 'display:none;'),
8509
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8510
			'/div'
8511
		);
8512
		$this->assertTags($result, $expected);
8513
 
8514
		$result = $this->Form->create('Contact', array('id' => 'TestId'));
8515
		$expected = array(
8516
			'form' => array(
8517
				'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
8518
				'accept-charset' => $encoding
8519
			),
8520
			'div' => array('style' => 'display:none;'),
8521
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
8522
			'/div'
8523
		);
8524
		$this->assertTags($result, $expected);
8525
 
8526
		$this->Form->request['action'] = 'add';
8527
		$result = $this->Form->create('User', array('url' => array('action' => 'login')));
8528
		$expected = array(
8529
			'form' => array(
8530
				'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
8531
				'accept-charset' => $encoding
8532
			),
8533
			'div' => array('style' => 'display:none;'),
8534
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8535
			'/div'
8536
		);
8537
		$this->assertTags($result, $expected);
8538
 
8539
		$result = $this->Form->create('User', array('action' => 'login'));
8540
		$expected = array(
8541
			'form' => array(
8542
				'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
8543
				'accept-charset' => $encoding
8544
			),
8545
			'div' => array('style' => 'display:none;'),
8546
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8547
			'/div'
8548
		);
8549
		$this->assertTags($result, $expected);
8550
 
8551
		$result = $this->Form->create('User', array('url' => '/users/login'));
8552
		$expected = array(
8553
			'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
8554
			'div' => array('style' => 'display:none;'),
8555
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8556
			'/div'
8557
		);
8558
		$this->assertTags($result, $expected);
8559
 
8560
		$this->Form->request['controller'] = 'pages';
8561
		$result = $this->Form->create('User', array('action' => 'signup'));
8562
		$expected = array(
8563
			'form' => array(
8564
				'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
8565
				'accept-charset' => $encoding
8566
			),
8567
			'div' => array('style' => 'display:none;'),
8568
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8569
			'/div'
8570
		);
8571
		$this->assertTags($result, $expected);
8572
 
8573
		$this->Form->request->data = array();
8574
		$this->Form->request['controller'] = 'contacts';
8575
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8576
		$result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
8577
		$expected = array(
8578
			'form' => array(
8579
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
8580
				'accept-charset' => 'utf-8'
8581
			),
8582
			'div' => array('style' => 'display:none;'),
8583
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8584
			'/div'
8585
		);
8586
		$this->assertTags($result, $expected);
8587
	}
8588
 
8589
/**
8590
 * Test the onsubmit option for create()
8591
 *
8592
 * @return void
8593
 */
8594
	public function testCreateOnSubmit() {
8595
		$this->Form->request->data = array();
8596
		$this->Form->request['controller'] = 'contacts';
8597
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8598
		$result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
8599
		$expected = array(
8600
			'form' => array(
8601
				'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
8602
				'accept-charset' => 'utf-8'
8603
			),
8604
			'div' => array('style' => 'display:none;'),
8605
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8606
			'/div'
8607
		);
8608
		$this->assertTags($result, $expected);
8609
 
8610
		$this->Form->request->data = array();
8611
		$this->Form->request['controller'] = 'contacts';
8612
		$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
8613
		$result = $this->Form->create(array(
8614
			'url' => array('action' => 'index', 'param'),
8615
			'default' => false,
8616
			'onsubmit' => 'someFunction();'
8617
		));
8618
 
8619
		$expected = array(
8620
			'form' => array(
8621
				'id' => 'ContactAddForm', 'method' => 'post',
8622
				'onsubmit' => 'someFunction();event.returnValue = false; return false;',
8623
				'action' => '/contacts/index/param',
8624
				'accept-charset' => 'utf-8'
8625
			),
8626
			'div' => array('style' => 'display:none;'),
8627
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8628
			'/div'
8629
		);
8630
		$this->assertTags($result, $expected);
8631
	}
8632
 
8633
/**
8634
 * test create() with automatic url generation
8635
 *
8636
 * @return void
8637
 */
8638
	public function testCreateAutoUrl() {
8639
		Router::setRequestInfo(array(array(), array('base' => '/base_url')));
8640
		$this->Form->request->here = '/base_url/contacts/add/Contact:1';
8641
		$this->Form->request->base = '/base_url';
8642
		$result = $this->Form->create('Contact');
8643
		$expected = array(
8644
			'form' => array(
8645
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
8646
				'accept-charset' => 'utf-8'
8647
			),
8648
			'div' => array('style' => 'display:none;'),
8649
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8650
			'/div'
8651
		);
8652
		$this->assertTags($result, $expected);
8653
 
8654
		$this->Form->request['action'] = 'delete';
8655
		$this->Form->request->here = '/base_url/contacts/delete/10/User:42';
8656
		$this->Form->request->base = '/base_url';
8657
		$result = $this->Form->create('Contact');
8658
		$expected = array(
8659
			'form' => array(
8660
				'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
8661
				'accept-charset' => 'utf-8'
8662
			),
8663
			'div' => array('style' => 'display:none;'),
8664
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8665
			'/div'
8666
		);
8667
		$this->assertTags($result, $expected);
8668
	}
8669
 
8670
/**
8671
 * test create() with a custom route
8672
 *
8673
 * @return void
8674
 */
8675
	public function testCreateCustomRoute() {
8676
		Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
8677
		$encoding = strtolower(Configure::read('App.encoding'));
8678
 
8679
		$result = $this->Form->create('User', array('action' => 'login'));
8680
		$expected = array(
8681
			'form' => array(
8682
				'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
8683
				'accept-charset' => $encoding
8684
			),
8685
			'div' => array('style' => 'display:none;'),
8686
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8687
			'/div'
8688
		);
8689
		$this->assertTags($result, $expected);
8690
	}
8691
 
8692
/**
8693
 * test that inputDefaults are stored and used.
8694
 *
8695
 * @return void
8696
 */
8697
	public function testCreateWithInputDefaults() {
8698
		$this->Form->create('User', array(
8699
			'inputDefaults' => array(
8700
				'div' => false,
8701
				'label' => false,
8702
				'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
8703
				'format' => array('before', 'label', 'between', 'input', 'after', 'error')
8704
			)
8705
		));
8706
		$result = $this->Form->input('username');
8707
		$expected = array(
8708
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
8709
		);
8710
		$this->assertTags($result, $expected);
8711
 
8712
		$result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
8713
		$expected = array(
8714
			'div' => array('class' => 'input text'),
8715
			'label' => array('for' => 'UserUsername'), 'username', '/label',
8716
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8717
			'/div'
8718
		);
8719
		$this->assertTags($result, $expected);
8720
 
8721
		$result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
8722
		$expected = array(
8723
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8724
			'label' => array('for' => 'UserUsername'), 'Username', '/label',
8725
		);
8726
		$this->assertTags($result, $expected);
8727
 
8728
		$this->Form->create('User', array(
8729
			'inputDefaults' => array(
8730
				'div' => false,
8731
				'label' => array('class' => 'nice', 'for' => 'changed'),
8732
			)
8733
		));
8734
		$result = $this->Form->input('username', array('div' => true));
8735
		$expected = array(
8736
			'div' => array('class' => 'input text'),
8737
			'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
8738
			'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
8739
			'/div'
8740
		);
8741
		$this->assertTags($result, $expected);
8742
	}
8743
 
8744
/**
8745
 * test automatic accept-charset overriding
8746
 *
8747
 * @return void
8748
 */
8749
	public function testCreateWithAcceptCharset() {
8750
		$result = $this->Form->create('UserForm', array(
8751
				'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
8752
			)
8753
		);
8754
		$expected = array(
8755
			'form' => array(
8756
				'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
8757
				'accept-charset' => 'iso-8859-1'
8758
			),
8759
			'div' => array('style' => 'display:none;'),
8760
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8761
			'/div'
8762
		);
8763
		$this->assertTags($result, $expected);
8764
	}
8765
 
8766
/**
8767
 * Test base form URL when url param is passed with multiple parameters (&)
8768
 *
8769
 * @return void
8770
 */
8771
	public function testCreateQuerystringrequest() {
8772
		$encoding = strtolower(Configure::read('App.encoding'));
8773
		$result = $this->Form->create('Contact', array(
8774
			'type' => 'post',
8775
			'escape' => false,
8776
			'url' => array(
8777
				'controller' => 'controller',
8778
				'action' => 'action',
8779
				'?' => array('param1' => 'value1', 'param2' => 'value2')
8780
			)
8781
		));
8782
		$expected = array(
8783
			'form' => array(
8784
				'id' => 'ContactAddForm',
8785
				'method' => 'post',
8786
				'action' => '/controller/action?param1=value1&amp;param2=value2',
8787
				'accept-charset' => $encoding
8788
			),
8789
			'div' => array('style' => 'display:none;'),
8790
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8791
			'/div'
8792
		);
8793
		$this->assertTags($result, $expected);
8794
 
8795
		$result = $this->Form->create('Contact', array(
8796
			'type' => 'post',
8797
			'url' => array(
8798
				'controller' => 'controller',
8799
				'action' => 'action',
8800
				'?' => array('param1' => 'value1', 'param2' => 'value2')
8801
			)
8802
		));
8803
		$expected = array(
8804
			'form' => array(
8805
				'id' => 'ContactAddForm',
8806
				'method' => 'post',
8807
				'action' => '/controller/action?param1=value1&amp;param2=value2',
8808
				'accept-charset' => $encoding
8809
			),
8810
			'div' => array('style' => 'display:none;'),
8811
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8812
			'/div'
8813
		);
8814
		$this->assertTags($result, $expected);
8815
	}
8816
 
8817
/**
8818
 * test that create() doesn't cause errors by multiple id's being in the primary key
8819
 * as could happen with multiple select or checkboxes.
8820
 *
8821
 * @return void
8822
 */
8823
	public function testCreateWithMultipleIdInData() {
8824
		$encoding = strtolower(Configure::read('App.encoding'));
8825
 
8826
		$this->Form->request->data['Contact']['id'] = array(1, 2);
8827
		$result = $this->Form->create('Contact');
8828
		$expected = array(
8829
			'form' => array(
8830
				'id' => 'ContactAddForm',
8831
				'method' => 'post',
8832
				'action' => '/contacts/add',
8833
				'accept-charset' => $encoding
8834
			),
8835
			'div' => array('style' => 'display:none;'),
8836
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8837
			'/div'
8838
		);
8839
		$this->assertTags($result, $expected);
8840
	}
8841
 
8842
/**
8843
 * test that create() doesn't add in extra passed params.
8844
 *
8845
 * @return void
8846
 */
8847
	public function testCreatePassedArgs() {
8848
		$encoding = strtolower(Configure::read('App.encoding'));
8849
		$this->Form->request->data['Contact']['id'] = 1;
8850
		$result = $this->Form->create('Contact', array(
8851
			'type' => 'post',
8852
			'escape' => false,
8853
			'url' => array(
8854
				'action' => 'edit',
8855
				'0',
8856
				'myparam'
8857
			)
8858
		));
8859
		$expected = array(
8860
			'form' => array(
8861
				'id' => 'ContactAddForm',
8862
				'method' => 'post',
8863
				'action' => '/contacts/edit/0/myparam',
8864
				'accept-charset' => $encoding
8865
			),
8866
			'div' => array('style' => 'display:none;'),
8867
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
8868
			'/div'
8869
		);
8870
		$this->assertTags($result, $expected);
8871
	}
8872
 
8873
/**
8874
 * test that create() works without raising errors with a Mock Model
8875
 *
8876
 * @return void
8877
 */
8878
	public function testCreateNoErrorsWithMockModel() {
8879
		$encoding = strtolower(Configure::read('App.encoding'));
8880
		$ContactMock = $this->getMockBuilder('Contact')
8881
			->disableOriginalConstructor()
8882
			->getMock();
8883
		ClassRegistry::removeObject('Contact');
8884
		ClassRegistry::addObject('Contact', $ContactMock);
8885
		$result = $this->Form->create('Contact', array('type' => 'GET'));
8886
		$expected = array('form' => array(
8887
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8888
			'accept-charset' => $encoding
8889
		));
8890
		$this->assertTags($result, $expected);
8891
	}
8892
 
8893
/**
8894
 * test creating a get form, and get form inputs.
8895
 *
8896
 * @return void
8897
 */
8898
	public function testGetFormCreate() {
8899
		$encoding = strtolower(Configure::read('App.encoding'));
8900
		$result = $this->Form->create('Contact', array('type' => 'get'));
8901
		$this->assertTags($result, array('form' => array(
8902
			'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
8903
			'accept-charset' => $encoding
8904
		)));
8905
 
8906
		$result = $this->Form->text('Contact.name');
8907
		$this->assertTags($result, array('input' => array(
8908
			'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
8909
		)));
8910
 
8911
		$result = $this->Form->password('password');
8912
		$this->assertTags($result, array('input' => array(
8913
			'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
8914
		)));
8915
		$this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
8916
 
8917
		$result = $this->Form->text('user_form');
8918
		$this->assertTags($result, array('input' => array(
8919
			'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
8920
		)));
8921
	}
8922
 
8923
/**
8924
 * test get form, and inputs when the model param is false
8925
 *
8926
 * @return void
8927
 */
8928
	public function testGetFormWithFalseModel() {
8929
		$encoding = strtolower(Configure::read('App.encoding'));
8930
		$this->Form->request['controller'] = 'contact_test';
8931
		$result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
8932
 
8933
		$expected = array('form' => array(
8934
			'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
8935
			'accept-charset' => $encoding
8936
		));
8937
		$this->assertTags($result, $expected);
8938
 
8939
		$result = $this->Form->text('reason');
8940
		$expected = array(
8941
			'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
8942
		);
8943
		$this->assertTags($result, $expected);
8944
	}
8945
 
8946
/**
8947
 * test that datetime() works with GET style forms.
8948
 *
8949
 * @return void
8950
 */
8951
	public function testDateTimeWithGetForms() {
8952
		extract($this->dateRegex);
8953
		$this->Form->create('Contact', array('type' => 'get'));
8954
		$result = $this->Form->datetime('created');
8955
 
8956
		$this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
8957
		$this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
8958
		$this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
8959
		$this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
8960
		$this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
8961
		$this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
8962
	}
8963
 
8964
/**
8965
 * testEditFormWithData method
8966
 *
8967
 * test auto populating form elements from submitted data.
8968
 *
8969
 * @return void
8970
 */
8971
	public function testEditFormWithData() {
8972
		$this->Form->request->data = array('Person' => array(
8973
			'id' => 1,
8974
			'first_name' => 'Nate',
8975
			'last_name' => 'Abele',
8976
			'email' => 'nate@example.com'
8977
		));
8978
		$this->Form->request->addParams(array(
8979
			'models' => array('Person'),
8980
			'controller' => 'people',
8981
			'action' => 'add'
8982
		));
8983
		$options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
8984
 
8985
		$this->Form->create();
8986
		$result = $this->Form->select('People.People', $options, array('multiple' => true));
8987
		$expected = array(
8988
			'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
8989
			'select' => array(
8990
				'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
8991
			),
8992
			array('option' => array('value' => 1)), 'Nate', '/option',
8993
			array('option' => array('value' => 2)), 'Garrett', '/option',
8994
			array('option' => array('value' => 3)), 'Larry', '/option',
8995
			'/select'
8996
		);
8997
		$this->assertTags($result, $expected);
8998
	}
8999
 
9000
/**
9001
 * Test that required fields are created for various types of validation.
9002
 *
9003
 * @return void
9004
 */
9005
	public function testFormInputRequiredDetection() {
9006
		$this->Form->create('Contact');
9007
 
9008
		$result = $this->Form->input('Contact.non_existing');
9009
		$expected = array(
9010
			'div' => array('class' => 'input text'),
9011
			'label' => array('for' => 'ContactNonExisting'),
9012
			'Non Existing',
9013
			'/label',
9014
			'input' => array(
9015
				'type' => 'text', 'name' => 'data[Contact][non_existing]',
9016
				'id' => 'ContactNonExisting'
9017
			),
9018
			'/div'
9019
		);
9020
		$this->assertTags($result, $expected);
9021
 
9022
		$result = $this->Form->input('Contact.imrequired');
9023
		$expected = array(
9024
			'div' => array('class' => 'input text required'),
9025
			'label' => array('for' => 'ContactImrequired'),
9026
			'Imrequired',
9027
			'/label',
9028
			'input' => array(
9029
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
9030
				'id' => 'ContactImrequired',
9031
				'required' => 'required'
9032
			),
9033
			'/div'
9034
		);
9035
		$this->assertTags($result, $expected);
9036
 
9037
		$result = $this->Form->input('Contact.imalsorequired');
9038
		$expected = array(
9039
			'div' => array('class' => 'input text required'),
9040
			'label' => array('for' => 'ContactImalsorequired'),
9041
			'Imalsorequired',
9042
			'/label',
9043
			'input' => array(
9044
				'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
9045
				'id' => 'ContactImalsorequired',
9046
				'required' => 'required'
9047
			),
9048
			'/div'
9049
		);
9050
		$this->assertTags($result, $expected);
9051
 
9052
		$result = $this->Form->input('Contact.imrequiredtoo');
9053
		$expected = array(
9054
			'div' => array('class' => 'input text required'),
9055
			'label' => array('for' => 'ContactImrequiredtoo'),
9056
			'Imrequiredtoo',
9057
			'/label',
9058
			'input' => array(
9059
				'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
9060
				'id' => 'ContactImrequiredtoo',
9061
				'required' => 'required'
9062
			),
9063
			'/div'
9064
		);
9065
		$this->assertTags($result, $expected);
9066
 
9067
		$result = $this->Form->input('Contact.required_one');
9068
		$expected = array(
9069
			'div' => array('class' => 'input text required'),
9070
			'label' => array('for' => 'ContactRequiredOne'),
9071
			'Required One',
9072
			'/label',
9073
			'input' => array(
9074
				'type' => 'text', 'name' => 'data[Contact][required_one]',
9075
				'id' => 'ContactRequiredOne',
9076
				'required' => 'required'
9077
			),
9078
			'/div'
9079
		);
9080
		$this->assertTags($result, $expected);
9081
 
9082
		$result = $this->Form->input('Contact.string_required');
9083
		$expected = array(
9084
			'div' => array('class' => 'input text required'),
9085
			'label' => array('for' => 'ContactStringRequired'),
9086
			'String Required',
9087
			'/label',
9088
			'input' => array(
9089
				'type' => 'text', 'name' => 'data[Contact][string_required]',
9090
				'id' => 'ContactStringRequired',
9091
				'required' => 'required'
9092
			),
9093
			'/div'
9094
		);
9095
		$this->assertTags($result, $expected);
9096
 
9097
		$result = $this->Form->input('Contact.imnotrequired');
9098
		$expected = array(
9099
			'div' => array('class' => 'input text'),
9100
			'label' => array('for' => 'ContactImnotrequired'),
9101
			'Imnotrequired',
9102
			'/label',
9103
			'input' => array(
9104
				'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
9105
				'id' => 'ContactImnotrequired'
9106
			),
9107
			'/div'
9108
		);
9109
		$this->assertTags($result, $expected);
9110
 
9111
		$result = $this->Form->input('Contact.imalsonotrequired');
9112
		$expected = array(
9113
			'div' => array('class' => 'input text'),
9114
			'label' => array('for' => 'ContactImalsonotrequired'),
9115
			'Imalsonotrequired',
9116
			'/label',
9117
			'input' => array(
9118
				'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
9119
				'id' => 'ContactImalsonotrequired'
9120
			),
9121
			'/div'
9122
		);
9123
		$this->assertTags($result, $expected);
9124
 
9125
		$result = $this->Form->input('Contact.imalsonotrequired2');
9126
		$expected = array(
9127
			'div' => array('class' => 'input text'),
9128
			'label' => array('for' => 'ContactImalsonotrequired2'),
9129
			'Imalsonotrequired2',
9130
			'/label',
9131
			'input' => array(
9132
				'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
9133
				'id' => 'ContactImalsonotrequired2'
9134
			),
9135
			'/div'
9136
		);
9137
		$this->assertTags($result, $expected);
9138
 
9139
		$result = $this->Form->input('Contact.imnotrequiredeither');
9140
		$expected = array(
9141
			'div' => array('class' => 'input text'),
9142
			'label' => array('for' => 'ContactImnotrequiredeither'),
9143
			'Imnotrequiredeither',
9144
			'/label',
9145
			'input' => array(
9146
				'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
9147
				'id' => 'ContactImnotrequiredeither'
9148
			),
9149
			'/div'
9150
		);
9151
		$this->assertTags($result, $expected);
9152
 
9153
		$result = $this->Form->input('Contact.iamrequiredalways');
9154
		$expected = array(
9155
			'div' => array('class' => 'input text required'),
9156
			'label' => array('for' => 'ContactIamrequiredalways'),
9157
			'Iamrequiredalways',
9158
			'/label',
9159
			'input' => array(
9160
				'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
9161
				'id' => 'ContactIamrequiredalways',
9162
				'required' => 'required'
9163
			),
9164
			'/div'
9165
		);
9166
		$this->assertTags($result, $expected);
9167
 
9168
		$result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
9169
		$expected = array(
9170
			'div' => array('class' => 'input checkbox required'),
9171
			array('input' => array(
9172
				'type' => 'hidden',
9173
				'name' => 'data[Contact][boolean_field]',
9174
				'id' => 'ContactBooleanField_',
9175
				'value' => '0'
9176
			)),
9177
			array('input' => array(
9178
				'type' => 'checkbox',
9179
				'name' => 'data[Contact][boolean_field]',
9180
				'value' => '1',
9181
				'id' => 'ContactBooleanField'
9182
			)),
9183
			'label' => array('for' => 'ContactBooleanField'),
9184
			'Boolean Field',
9185
			'/label',
9186
			'/div'
9187
		);
9188
		$this->assertTags($result, $expected);
9189
 
9190
		$result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
9191
		$expected = array(
9192
			'div' => array('class' => 'input checkbox required'),
9193
			array('input' => array(
9194
				'type' => 'hidden',
9195
				'name' => 'data[Contact][boolean_field]',
9196
				'id' => 'ContactBooleanField_',
9197
				'value' => '0'
9198
			)),
9199
			array('input' => array(
9200
				'type' => 'checkbox',
9201
				'name' => 'data[Contact][boolean_field]',
9202
				'value' => '1',
9203
				'id' => 'ContactBooleanField',
9204
				'required' => 'required'
9205
			)),
9206
			'label' => array('for' => 'ContactBooleanField'),
9207
			'Boolean Field',
9208
			'/label',
9209
			'/div'
9210
		);
9211
		$this->assertTags($result, $expected);
9212
 
9213
		$result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
9214
		$expected = array(
9215
			'div' => array('class' => 'input file required'),
9216
			'label' => array('for' => 'ContactIamrequiredalways'),
9217
			'Iamrequiredalways',
9218
			'/label',
9219
			'input' => array(
9220
				'type' => 'file',
9221
				'name' => 'data[Contact][iamrequiredalways]',
9222
				'id' => 'ContactIamrequiredalways',
9223
				'required' => 'required'
9224
			),
9225
			'/div'
9226
		);
9227
		$this->assertTags($result, $expected);
9228
	}
9229
 
9230
/**
9231
 * Test that required fields are created when only using ModelValidator::add().
9232
 *
9233
 * @return void
9234
 */
9235
	public function testFormInputRequiredDetectionModelValidator() {
9236
		ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notBlank'));
9237
 
9238
		$this->Form->create('ContactTag');
9239
		$result = $this->Form->input('ContactTag.iwillberequired');
9240
		$expected = array(
9241
			'div' => array('class' => 'input text required'),
9242
			'label' => array('for' => 'ContactTagIwillberequired'),
9243
			'Iwillberequired',
9244
			'/label',
9245
			'input' => array(
9246
				'name' => 'data[ContactTag][iwillberequired]',
9247
				'type' => 'text',
9248
				'id' => 'ContactTagIwillberequired',
9249
				'required' => 'required'
9250
			),
9251
			'/div'
9252
		);
9253
		$this->assertTags($result, $expected);
9254
	}
9255
 
9256
/**
9257
 * testFormMagicInput method
9258
 *
9259
 * @return void
9260
 */
9261
	public function testFormMagicInput() {
9262
		$encoding = strtolower(Configure::read('App.encoding'));
9263
		$result = $this->Form->create('Contact');
9264
		$expected = array(
9265
			'form' => array(
9266
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
9267
				'accept-charset' => $encoding
9268
			),
9269
			'div' => array('style' => 'display:none;'),
9270
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
9271
			'/div'
9272
		);
9273
		$this->assertTags($result, $expected);
9274
 
9275
		$result = $this->Form->input('name');
9276
		$expected = array(
9277
			'div' => array('class' => 'input text'),
9278
			'label' => array('for' => 'ContactName'),
9279
			'Name',
9280
			'/label',
9281
			'input' => array(
9282
				'type' => 'text', 'name' => 'data[Contact][name]',
9283
				'id' => 'ContactName', 'maxlength' => '255'
9284
			),
9285
			'/div'
9286
		);
9287
		$this->assertTags($result, $expected);
9288
 
9289
		$result = $this->Form->input('non_existing_field_in_contact_model');
9290
		$expected = array(
9291
			'div' => array('class' => 'input text'),
9292
			'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
9293
			'Non Existing Field In Contact Model',
9294
			'/label',
9295
			'input' => array(
9296
				'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
9297
				'id' => 'ContactNonExistingFieldInContactModel'
9298
			),
9299
			'/div'
9300
		);
9301
		$this->assertTags($result, $expected);
9302
 
9303
		$result = $this->Form->input('Address.street');
9304
		$expected = array(
9305
			'div' => array('class' => 'input text'),
9306
			'label' => array('for' => 'AddressStreet'),
9307
			'Street',
9308
			'/label',
9309
			'input' => array(
9310
				'type' => 'text', 'name' => 'data[Address][street]',
9311
				'id' => 'AddressStreet'
9312
			),
9313
			'/div'
9314
		);
9315
		$this->assertTags($result, $expected);
9316
 
9317
		$result = $this->Form->input('Address.non_existing_field_in_model');
9318
		$expected = array(
9319
			'div' => array('class' => 'input text'),
9320
			'label' => array('for' => 'AddressNonExistingFieldInModel'),
9321
			'Non Existing Field In Model',
9322
			'/label',
9323
			'input' => array(
9324
				'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
9325
				'id' => 'AddressNonExistingFieldInModel'
9326
			),
9327
			'/div'
9328
		);
9329
		$this->assertTags($result, $expected);
9330
 
9331
		$result = $this->Form->input('name', array('div' => false));
9332
		$expected = array(
9333
			'label' => array('for' => 'ContactName'),
9334
			'Name',
9335
			'/label',
9336
			'input' => array(
9337
				'type' => 'text', 'name' => 'data[Contact][name]',
9338
				'id' => 'ContactName', 'maxlength' => '255'
9339
			)
9340
		);
9341
		$this->assertTags($result, $expected);
9342
 
9343
		extract($this->dateRegex);
9344
		$now = strtotime('now');
9345
 
9346
		$result = $this->Form->input('Contact.published', array('div' => false));
9347
		$expected = array(
9348
			'label' => array('for' => 'ContactPublishedMonth'),
9349
			'Published',
9350
			'/label',
9351
			array('select' => array(
9352
				'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
9353
			)),
9354
			$monthsRegex,
9355
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
9356
			date('F', $now),
9357
			'/option',
9358
			'*/select',
9359
			'-',
9360
			array('select' => array(
9361
				'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
9362
			)),
9363
			$daysRegex,
9364
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
9365
			date('j', $now),
9366
			'/option',
9367
			'*/select',
9368
			'-',
9369
			array('select' => array(
9370
				'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
9371
			)),
9372
			$yearsRegex,
9373
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
9374
			date('Y', $now),
9375
			'*/select'
9376
		);
9377
		$this->assertTags($result, $expected);
9378
 
9379
		$result = $this->Form->input('Contact.updated', array('div' => false));
9380
		$expected = array(
9381
			'label' => array('for' => 'ContactUpdatedMonth'),
9382
			'Updated',
9383
			'/label',
9384
			array('select' => array(
9385
				'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
9386
			)),
9387
			$monthsRegex,
9388
			array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
9389
			date('F', $now),
9390
			'/option',
9391
			'*/select',
9392
			'-',
9393
			array('select' => array(
9394
				'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
9395
			)),
9396
			$daysRegex,
9397
			array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
9398
			date('j', $now),
9399
			'/option',
9400
			'*/select',
9401
			'-',
9402
			array('select' => array(
9403
				'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
9404
			)),
9405
			$yearsRegex,
9406
			array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
9407
			date('Y', $now),
9408
			'*/select'
9409
		);
9410
		$this->assertTags($result, $expected);
9411
 
9412
		$result = $this->Form->input('UserForm.stuff');
9413
		$expected = array(
9414
			'div' => array('class' => 'input text'),
9415
			'label' => array('for' => 'UserFormStuff'),
9416
			'Stuff',
9417
			'/label',
9418
			'input' => array(
9419
				'type' => 'text', 'name' => 'data[UserForm][stuff]',
9420
				'id' => 'UserFormStuff', 'maxlength' => 10
9421
			),
9422
			'/div'
9423
		);
9424
		$this->assertTags($result, $expected);
9425
	}
9426
 
9427
/**
9428
 * testForMagicInputNonExistingNorValidated method
9429
 *
9430
 * @return void
9431
 */
9432
	public function testForMagicInputNonExistingNorValidated() {
9433
		$encoding = strtolower(Configure::read('App.encoding'));
9434
		$result = $this->Form->create('Contact');
9435
		$expected = array(
9436
			'form' => array(
9437
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
9438
				'accept-charset' => $encoding
9439
			),
9440
			'div' => array('style' => 'display:none;'),
9441
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
9442
			'/div'
9443
		);
9444
		$this->assertTags($result, $expected);
9445
 
9446
		$result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
9447
		$expected = array(
9448
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9449
			'Non Existing Nor Validated',
9450
			'/label',
9451
			'input' => array(
9452
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9453
				'id' => 'ContactNonExistingNorValidated'
9454
			)
9455
		);
9456
		$this->assertTags($result, $expected);
9457
 
9458
		$result = $this->Form->input('Contact.non_existing_nor_validated', array(
9459
			'div' => false, 'value' => 'my value'
9460
		));
9461
		$expected = array(
9462
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9463
			'Non Existing Nor Validated',
9464
			'/label',
9465
			'input' => array(
9466
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9467
				'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
9468
			)
9469
		);
9470
		$this->assertTags($result, $expected);
9471
 
9472
		$this->Form->request->data = array(
9473
			'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
9474
		));
9475
		$result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
9476
		$expected = array(
9477
			'label' => array('for' => 'ContactNonExistingNorValidated'),
9478
			'Non Existing Nor Validated',
9479
			'/label',
9480
			'input' => array(
9481
				'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
9482
				'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
9483
			)
9484
		);
9485
		$this->assertTags($result, $expected);
9486
	}
9487
 
9488
/**
9489
 * testFormMagicInputLabel method
9490
 *
9491
 * @return void
9492
 */
9493
	public function testFormMagicInputLabel() {
9494
		$encoding = strtolower(Configure::read('App.encoding'));
9495
		$result = $this->Form->create('Contact');
9496
		$expected = array(
9497
			'form' => array(
9498
				'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
9499
				'accept-charset' => $encoding
9500
			),
9501
			'div' => array('style' => 'display:none;'),
9502
			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
9503
			'/div'
9504
		);
9505
		$this->assertTags($result, $expected);
9506
 
9507
		$result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
9508
		$this->assertTags($result, array('input' => array(
9509
			'name' => 'data[Contact][name]', 'type' => 'text',
9510
			'id' => 'ContactName', 'maxlength' => '255')
9511
		));
9512
 
9513
		$result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
9514
		$expected = array(
9515
			'label' => array('for' => 'ContactName'),
9516
			'My label',
9517
			'/label',
9518
			'input' => array(
9519
				'type' => 'text', 'name' => 'data[Contact][name]',
9520
				'id' => 'ContactName', 'maxlength' => '255'
9521
			)
9522
		);
9523
		$this->assertTags($result, $expected);
9524
 
9525
		$result = $this->Form->input('Contact.name', array(
9526
			'div' => false, 'label' => array('class' => 'mandatory')
9527
		));
9528
		$expected = array(
9529
			'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
9530
			'Name',
9531
			'/label',
9532
			'input' => array(
9533
				'type' => 'text', 'name' => 'data[Contact][name]',
9534
				'id' => 'ContactName', 'maxlength' => '255'
9535
			)
9536
		);
9537
		$this->assertTags($result, $expected);
9538
 
9539
		$result = $this->Form->input('Contact.name', array(
9540
			'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
9541
		));
9542
		$expected = array(
9543
			'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
9544
			'My label',
9545
			'/label',
9546
			'input' => array(
9547
				'type' => 'text', 'name' => 'data[Contact][name]',
9548
				'id' => 'ContactName', 'maxlength' => '255'
9549
			)
9550
		);
9551
		$this->assertTags($result, $expected);
9552
 
9553
		$result = $this->Form->input('Contact.name', array(
9554
			'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
9555
		));
9556
		$expected = array(
9557
			'label' => array('for' => 'my_id'),
9558
			'Name',
9559
			'/label',
9560
			'input' => array(
9561
				'type' => 'text', 'name' => 'data[Contact][name]',
9562
				'id' => 'my_id', 'maxlength' => '255'
9563
			)
9564
		);
9565
		$this->assertTags($result, $expected);
9566
 
9567
		$result = $this->Form->input('1.id');
9568
		$this->assertTags($result, array('input' => array(
9569
			'type' => 'hidden', 'name' => 'data[Contact][1][id]',
9570
			'id' => 'Contact1Id'
9571
		)));
9572
 
9573
		$result = $this->Form->input("1.name");
9574
		$expected = array(
9575
			'div' => array('class' => 'input text'),
9576
			'label' => array('for' => 'Contact1Name'),
9577
			'Name',
9578
			'/label',
9579
			'input' => array(
9580
				'type' => 'text', 'name' => 'data[Contact][1][name]',
9581
				'id' => 'Contact1Name', 'maxlength' => '255'
9582
			),
9583
			'/div'
9584
		);
9585
		$this->assertTags($result, $expected);
9586
 
9587
		$result = $this->Form->input('Contact.1.id');
9588
		$this->assertTags($result, array(
9589
			'input' => array(
9590
				'type' => 'hidden', 'name' => 'data[Contact][1][id]',
9591
				'id' => 'Contact1Id'
9592
			)
9593
		));
9594
 
9595
		$result = $this->Form->input("Model.1.name");
9596
		$expected = array(
9597
			'div' => array('class' => 'input text'),
9598
			'label' => array('for' => 'Model1Name'),
9599
			'Name',
9600
			'/label',
9601
			'input' => array(
9602
				'type' => 'text', 'name' => 'data[Model][1][name]',
9603
				'id' => 'Model1Name'
9604
			),
9605
			'/div'
9606
		);
9607
		$this->assertTags($result, $expected);
9608
	}
9609
 
9610
/**
9611
 * testFormEnd method
9612
 *
9613
 * @return void
9614
 */
9615
	public function testFormEnd() {
9616
		$this->assertEquals('</form>', $this->Form->end());
9617
 
9618
		$result = $this->Form->end('', array('form' => 'form-name'));
9619
		$expected = array(
9620
			'div' => array('class' => 'submit'),
9621
			'input' => array('type' => 'submit', 'value' => ''),
9622
			'/div',
9623
			'/form'
9624
		);
9625
		$this->assertTags($result, $expected);
9626
 
9627
		$result = $this->Form->end('');
9628
		$expected = array(
9629
			'div' => array('class' => 'submit'),
9630
			'input' => array('type' => 'submit', 'value' => ''),
9631
			'/div',
9632
			'/form'
9633
		);
9634
		$this->assertTags($result, $expected);
9635
 
9636
		$result = $this->Form->end(array('label' => ''));
9637
		$expected = array(
9638
			'div' => array('class' => 'submit'),
9639
			'input' => array('type' => 'submit', 'value' => ''),
9640
			'/div',
9641
			'/form'
9642
		);
9643
		$this->assertTags($result, $expected);
9644
 
9645
		$result = $this->Form->end('save');
9646
		$expected = array(
9647
			'div' => array('class' => 'submit'),
9648
			'input' => array('type' => 'submit', 'value' => 'save'),
9649
			'/div',
9650
			'/form'
9651
		);
9652
		$this->assertTags($result, $expected);
9653
 
9654
		$result = $this->Form->end(array('label' => 'save'));
9655
		$expected = array(
9656
			'div' => array('class' => 'submit'),
9657
			'input' => array('type' => 'submit', 'value' => 'save'),
9658
			'/div',
9659
			'/form'
9660
		);
9661
		$this->assertTags($result, $expected);
9662
 
9663
		$result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
9664
		$expected = array(
9665
			'div' => array('class' => 'submit'),
9666
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9667
			'/div',
9668
			'/form'
9669
		);
9670
		$this->assertTags($result, $expected);
9671
 
9672
		$result = $this->Form->end(array('name' => 'Whatever'));
9673
		$expected = array(
9674
			'div' => array('class' => 'submit'),
9675
			'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
9676
			'/div',
9677
			'/form'
9678
		);
9679
		$this->assertTags($result, $expected);
9680
 
9681
		$result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
9682
		$expected = array(
9683
			'div' => array('class' => 'good'),
9684
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9685
			'/div',
9686
			'/form'
9687
		);
9688
		$this->assertTags($result, $expected);
9689
 
9690
		$result = $this->Form->end(array(
9691
			'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
9692
		));
9693
		$expected = array(
9694
			'div' => array('class' => 'good'),
9695
			'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
9696
			'/div',
9697
			'/form'
9698
		);
9699
		$this->assertTags($result, $expected);
9700
	}
9701
 
9702
/**
9703
 * testMultipleFormWithIdFields method
9704
 *
9705
 * @return void
9706
 */
9707
	public function testMultipleFormWithIdFields() {
9708
		$this->Form->create('UserForm');
9709
 
9710
		$result = $this->Form->input('id');
9711
		$this->assertTags($result, array('input' => array(
9712
			'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
9713
		)));
9714
 
9715
		$result = $this->Form->input('ValidateItem.id');
9716
		$this->assertTags($result, array('input' => array(
9717
			'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
9718
			'id' => 'ValidateItemId'
9719
		)));
9720
 
9721
		$result = $this->Form->input('ValidateUser.id');
9722
		$this->assertTags($result, array('input' => array(
9723
			'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
9724
			'id' => 'ValidateUserId'
9725
		)));
9726
	}
9727
 
9728
/**
9729
 * testDbLessModel method
9730
 *
9731
 * @return void
9732
 */
9733
	public function testDbLessModel() {
9734
		$this->Form->create('TestMail');
9735
 
9736
		$result = $this->Form->input('name');
9737
		$expected = array(
9738
			'div' => array('class' => 'input text'),
9739
			'label' => array('for' => 'TestMailName'),
9740
			'Name',
9741
			'/label',
9742
			'input' => array(
9743
				'name' => 'data[TestMail][name]', 'type' => 'text',
9744
				'id' => 'TestMailName'
9745
			),
9746
			'/div'
9747
		);
9748
		$this->assertTags($result, $expected);
9749
 
9750
		ClassRegistry::init('TestMail');
9751
		$this->Form->create('TestMail');
9752
		$result = $this->Form->input('name');
9753
		$expected = array(
9754
			'div' => array('class' => 'input text'),
9755
			'label' => array('for' => 'TestMailName'),
9756
			'Name',
9757
			'/label',
9758
			'input' => array(
9759
				'name' => 'data[TestMail][name]', 'type' => 'text',
9760
				'id' => 'TestMailName'
9761
			),
9762
			'/div'
9763
		);
9764
		$this->assertTags($result, $expected);
9765
	}
9766
 
9767
/**
9768
 * testBrokenness method
9769
 *
9770
 * @return void
9771
 */
9772
	public function testBrokenness() {
9773
		/*
9774
		 * #4 This test has two parents and four children. By default (as of r7117) both
9775
		 * parents are show but the first parent is missing a child. This is the inconsistency
9776
		 * in the default behaviour - one parent has all children, the other does not - dependent
9777
		 * on the data values.
9778
		 */
9779
		$result = $this->Form->select('Model.field', array(
9780
			'Fred' => array(
9781
				'freds_son_1' => 'Fred',
9782
				'freds_son_2' => 'Freddie'
9783
			),
9784
			'Bert' => array(
9785
				'berts_son_1' => 'Albert',
9786
				'berts_son_2' => 'Bertie')
9787
			),
9788
			array('showParents' => true, 'empty' => false)
9789
		);
9790
 
9791
		$expected = array(
9792
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
9793
				array('optgroup' => array('label' => 'Fred')),
9794
					array('option' => array('value' => 'freds_son_1')),
9795
						'Fred',
9796
					'/option',
9797
					array('option' => array('value' => 'freds_son_2')),
9798
						'Freddie',
9799
					'/option',
9800
				'/optgroup',
9801
				array('optgroup' => array('label' => 'Bert')),
9802
					array('option' => array('value' => 'berts_son_1')),
9803
						'Albert',
9804
					'/option',
9805
					array('option' => array('value' => 'berts_son_2')),
9806
						'Bertie',
9807
					'/option',
9808
				'/optgroup',
9809
			'/select'
9810
		);
9811
		$this->assertTags($result, $expected);
9812
 
9813
		/*
9814
		 * #2 This is structurally identical to the test above (#1) - only the parent name has
9815
		 * changed, so we should expect the same select list data, just with a different name
9816
		 * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
9817
		 * This is where data corruption can occur, because when a select value is missing from
9818
		 * a list a form will substitute the first value in the list - without the user knowing.
9819
		 * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
9820
		 * affect the availability of 3 => 'Three' as a valid option.
9821
		 */
9822
		$options = array(1 => 'One', 2 => 'Two', 'Three' => array(
9823
			3 => 'Three', 4 => 'Four', 5 => 'Five'
9824
		));
9825
		$result = $this->Form->select(
9826
			'Model.field', $options, array('showParents' => true, 'empty' => false)
9827
		);
9828
 
9829
		$expected = array(
9830
			'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
9831
				array('option' => array('value' => 1)),
9832
					'One',
9833
				'/option',
9834
				array('option' => array('value' => 2)),
9835
					'Two',
9836
				'/option',
9837
				array('optgroup' => array('label' => 'Three')),
9838
					array('option' => array('value' => 3)),
9839
						'Three',
9840
					'/option',
9841
					array('option' => array('value' => 4)),
9842
						'Four',
9843
					'/option',
9844
					array('option' => array('value' => 5)),
9845
						'Five',
9846
					'/option',
9847
				'/optgroup',
9848
			'/select'
9849
		);
9850
		$this->assertTags($result, $expected);
9851
	}
9852
 
9853
/**
9854
 * Test the generation of fields for a multi record form.
9855
 *
9856
 * @return void
9857
 */
9858
	public function testMultiRecordForm() {
9859
		$this->Form->create('ValidateProfile');
9860
		$this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
9861
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
9862
		$expected = array(
9863
			'div' => array('class' => 'input textarea'),
9864
				'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
9865
					'Name',
9866
				'/label',
9867
				'textarea' => array(
9868
					'id' => 'ValidateProfile1ValidateItem2Name',
9869
					'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
9870
					'maxlength' => 255,
9871
					'cols' => 30,
9872
					'rows' => 6
9873
				),
9874
				'Value',
9875
				'/textarea',
9876
			'/div'
9877
		);
9878
		$this->assertTags($result, $expected);
9879
 
9880
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
9881
		$expected = array(
9882
			'div' => array('class' => 'input date'),
9883
			'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
9884
			'Created',
9885
			'/label',
9886
			array('select' => array(
9887
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
9888
				'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
9889
				)
9890
			),
9891
			array('option' => array('value' => '')), '/option',
9892
			$this->dateRegex['monthsRegex'],
9893
			'/select', '-',
9894
			array('select' => array(
9895
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
9896
				'id' => 'ValidateProfile1ValidateItem2CreatedDay'
9897
				)
9898
			),
9899
			array('option' => array('value' => '')), '/option',
9900
			$this->dateRegex['daysRegex'],
9901
			'/select', '-',
9902
			array('select' => array(
9903
				'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
9904
				'id' => 'ValidateProfile1ValidateItem2CreatedYear'
9905
				)
9906
			),
9907
			array('option' => array('value' => '')), '/option',
9908
			$this->dateRegex['yearsRegex'],
9909
			'/select',
9910
			'/div'
9911
		);
9912
		$this->assertTags($result, $expected);
9913
 
9914
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
9915
		$ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
9916
		$this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
9917
		$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
9918
		$expected = array(
9919
			'div' => array('class' => 'input select error'),
9920
			'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
9921
			'Profile',
9922
			'/label',
9923
			'select' => array(
9924
				'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
9925
				'id' => 'ValidateProfile1ValidateItem2ProfileId',
9926
				'class' => 'form-error'
9927
			),
9928
			'/select',
9929
			array('div' => array('class' => 'error-message')),
9930
			'Error',
9931
			'/div',
9932
			'/div'
9933
		);
9934
		$this->assertTags($result, $expected);
9935
	}
9936
 
9937
/**
9938
 * test the correct display of multi-record form validation errors.
9939
 *
9940
 * @return void
9941
 */
9942
	public function testMultiRecordFormValidationErrors() {
9943
		$this->Form->create('ValidateProfile');
9944
		$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
9945
		$ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
9946
		$result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
9947
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
9948
 
9949
		$ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
9950
		$result = $this->Form->error('ValidateProfile.2.city');
9951
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9952
 
9953
		$result = $this->Form->error('2.city');
9954
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9955
	}
9956
 
9957
/**
9958
 * test the correct display of multi-record form validation errors.
9959
 *
9960
 * @return void
9961
 */
9962
	public function testSaveManyRecordFormValidationErrors() {
9963
		$this->Form->create('ValidateUser');
9964
		$ValidateUser = ClassRegistry::getObject('ValidateUser');
9965
		$ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
9966
 
9967
		$result = $this->Form->error('0.ValidateUser.ValidateItem.name');
9968
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
9969
 
9970
		$ValidateUser->validationErrors[0]['city'] = array('Error in field city');
9971
		$result = $this->Form->error('ValidateUser.0.city');
9972
		$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
9973
	}
9974
 
9975
/**
9976
 * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
9977
 *
9978
 * @return void
9979
 */
9980
	public function testInputTemplate() {
9981
		$result = $this->Form->input('Contact.email', array(
9982
			'type' => 'text', 'format' => array('input')
9983
		));
9984
		$expected = array(
9985
			'div' => array('class' => 'input text'),
9986
			'input' => array(
9987
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]',
9988
				'id' => 'ContactEmail'
9989
			),
9990
			'/div'
9991
		);
9992
		$this->assertTags($result, $expected);
9993
 
9994
		$result = $this->Form->input('Contact.email', array(
9995
			'type' => 'text', 'format' => array('input', 'label'),
9996
			'label' => '<em>Email (required)</em>'
9997
		));
9998
		$expected = array(
9999
			'div' => array('class' => 'input text'),
10000
			array('input' => array(
10001
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]',
10002
				'id' => 'ContactEmail'
10003
			)),
10004
			'label' => array('for' => 'ContactEmail'),
10005
			'em' => array(),
10006
			'Email (required)',
10007
			'/em',
10008
			'/label',
10009
			'/div'
10010
		);
10011
		$this->assertTags($result, $expected);
10012
 
10013
		$result = $this->Form->input('Contact.email', array(
10014
			'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
10015
			'between' => '<div>Something in the middle</div>',
10016
			'after' => '<span>Some text at the end</span>'
10017
		));
10018
		$expected = array(
10019
			'div' => array('class' => 'input text'),
10020
			array('input' => array(
10021
				'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]',
10022
				'id' => 'ContactEmail'
10023
			)),
10024
			array('div' => array()),
10025
			'Something in the middle',
10026
			'/div',
10027
			'label' => array('for' => 'ContactEmail'),
10028
			'Email',
10029
			'/label',
10030
			'span' => array(),
10031
			'Some text at the end',
10032
			'/span',
10033
			'/div'
10034
		);
10035
		$this->assertTags($result, $expected);
10036
 
10037
		$result = $this->Form->input('Contact.method', array(
10038
			'type' => 'radio',
10039
			'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
10040
			'between' => 'I am between',
10041
		));
10042
		$expected = array(
10043
			'div' => array('class' => 'input radio'),
10044
			'fieldset' => array(),
10045
			'legend' => array(),
10046
			'Method',
10047
			'/legend',
10048
			'I am between',
10049
			'input' => array(
10050
				'type' => 'hidden', 'name' => 'data[Contact][method]',
10051
				'value' => '', 'id' => 'ContactMethod_'
10052
			),
10053
			array('input' => array(
10054
				'type' => 'radio', 'name' => 'data[Contact][method]',
10055
				'value' => 'email', 'id' => 'ContactMethodEmail'
10056
			)),
10057
			array('label' => array('for' => 'ContactMethodEmail')),
10058
			'Email',
10059
			'/label',
10060
			array('input' => array(
10061
				'type' => 'radio', 'name' => 'data[Contact][method]',
10062
				'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
10063
			)),
10064
			array('label' => array('for' => 'ContactMethodPigeon')),
10065
			'Pigeon',
10066
			'/label',
10067
			'/fieldset',
10068
			'/div',
10069
		);
10070
		$this->assertTags($result, $expected);
10071
	}
10072
 
10073
/**
10074
 * test that some html5 inputs + FormHelper::__call() work
10075
 *
10076
 * @return void
10077
 */
10078
	public function testHtml5Inputs() {
10079
		$result = $this->Form->email('User.email');
10080
		$expected = array(
10081
			'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
10082
		);
10083
		$this->assertTags($result, $expected);
10084
 
10085
		$result = $this->Form->search('User.query');
10086
		$expected = array(
10087
			'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
10088
		);
10089
		$this->assertTags($result, $expected);
10090
 
10091
		$result = $this->Form->search('User.query', array('value' => 'test'));
10092
		$expected = array(
10093
			'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
10094
		);
10095
		$this->assertTags($result, $expected);
10096
 
10097
		$result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
10098
		$expected = array(
10099
			'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
10100
		);
10101
		$this->assertTags($result, $expected);
10102
 
10103
		$result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
10104
		$expected = array(
10105
			'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
10106
		);
10107
		$this->assertTags($result, $expected);
10108
	}
10109
 
10110
/**
10111
 * @expectedException CakeException
10112
 * @return void
10113
 */
10114
	public function testHtml5InputException() {
10115
		$this->Form->email();
10116
	}
10117
 
10118
/**
10119
 * Tests that a model can be loaded from the model names passed in the request object
10120
 *
10121
 * @return void
10122
 */
10123
	public function testIntrospectModelFromRequest() {
10124
		$this->loadFixtures('Post');
10125
		App::build(array(
10126
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
10127
		));
10128
		CakePlugin::load('TestPlugin');
10129
		$this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
10130
 
10131
		$this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
10132
		$this->Form->create('TestPluginPost');
10133
		$this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
10134
		$this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
10135
 
10136
		CakePlugin::unload();
10137
		App::build();
10138
	}
10139
 
10140
/**
10141
 * Tests that it is possible to set the validation errors directly in the helper for a field
10142
 *
10143
 * @return void
10144
 */
10145
	public function testCustomValidationErrors() {
10146
		$this->Form->validationErrors['Thing']['field'] = 'Badness!';
10147
		$result = $this->Form->error('Thing.field', null, array('wrap' => false));
10148
		$this->assertEquals('Badness!', $result);
10149
	}
10150
 
10151
/**
10152
 * Tests that the 'on' key validates as expected on create
10153
 *
10154
 * @return void
10155
 */
10156
	public function testRequiredOnCreate() {
10157
		$this->Form->create('Contact');
10158
 
10159
		$result = $this->Form->input('Contact.imrequiredonupdate');
10160
		$expected = array(
10161
			'div' => array('class' => 'input text'),
10162
			'label' => array('for' => 'ContactImrequiredonupdate'),
10163
			'Imrequiredonupdate',
10164
			'/label',
10165
			'input' => array(
10166
				'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
10167
				'id' => 'ContactImrequiredonupdate'
10168
			),
10169
			'/div'
10170
		);
10171
		$this->assertTags($result, $expected);
10172
 
10173
		$result = $this->Form->input('Contact.imrequiredoncreate');
10174
		$expected = array(
10175
			'div' => array('class' => 'input text required'),
10176
			'label' => array('for' => 'ContactImrequiredoncreate'),
10177
			'Imrequiredoncreate',
10178
			'/label',
10179
			'input' => array(
10180
				'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
10181
				'id' => 'ContactImrequiredoncreate',
10182
				'required' => 'required'
10183
			),
10184
			'/div'
10185
		);
10186
		$this->assertTags($result, $expected);
10187
 
10188
		$result = $this->Form->input('Contact.imrequiredonboth');
10189
		$expected = array(
10190
			'div' => array('class' => 'input text required'),
10191
			'label' => array('for' => 'ContactImrequiredonboth'),
10192
			'Imrequiredonboth',
10193
			'/label',
10194
			'input' => array(
10195
				'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
10196
				'id' => 'ContactImrequiredonboth',
10197
				'required' => 'required'
10198
			),
10199
			'/div'
10200
		);
10201
		$this->assertTags($result, $expected);
10202
 
10203
		$this->Form->inputDefaults(array('required' => false));
10204
		$result = $this->Form->input('Contact.imrequired');
10205
		$expected = array(
10206
			'div' => array('class' => 'input text'),
10207
			'label' => array('for' => 'ContactImrequired'),
10208
			'Imrequired',
10209
			'/label',
10210
			'input' => array(
10211
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
10212
				'id' => 'ContactImrequired'
10213
			),
10214
			'/div'
10215
		);
10216
		$this->assertTags($result, $expected);
10217
 
10218
		$result = $this->Form->input('Contact.imrequired', array('required' => false));
10219
		$this->assertTags($result, $expected);
10220
 
10221
		$result = $this->Form->input('Contact.imrequired', array('required' => true));
10222
		$expected = array(
10223
			'div' => array('class' => 'input text required'),
10224
			'label' => array('for' => 'ContactImrequired'),
10225
			'Imrequired',
10226
			'/label',
10227
			'input' => array(
10228
				'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
10229
				'id' => 'ContactImrequired'
10230
			),
10231
			'/div'
10232
		);
10233
		$this->assertTags($result, $expected);
10234
 
10235
		$result = $this->Form->input('Contact.imrequired', array('required' => null));
10236
		$this->assertTags($result, $expected);
10237
	}
10238
 
10239
/**
10240
 * Tests that the 'on' key validates as expected on update
10241
 *
10242
 * @return void
10243
 */
10244
	public function testRequiredOnUpdate() {
10245
		$this->Form->request->data['Contact']['id'] = 1;
10246
		$this->Form->create('Contact');
10247
 
10248
		$result = $this->Form->input('Contact.imrequiredonupdate');
10249
		$expected = array(
10250
			'div' => array('class' => 'input text required'),
10251
			'label' => array('for' => 'ContactImrequiredonupdate'),
10252
			'Imrequiredonupdate',
10253
			'/label',
10254
			'input' => array(
10255
				'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
10256
				'id' => 'ContactImrequiredonupdate',
10257
				'required' => 'required'
10258
			),
10259
			'/div'
10260
		);
10261
		$this->assertTags($result, $expected);
10262
		$result = $this->Form->input('Contact.imrequiredoncreate');
10263
		$expected = array(
10264
			'div' => array('class' => 'input text'),
10265
			'label' => array('for' => 'ContactImrequiredoncreate'),
10266
			'Imrequiredoncreate',
10267
			'/label',
10268
			'input' => array(
10269
				'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
10270
				'id' => 'ContactImrequiredoncreate'
10271
			),
10272
			'/div'
10273
		);
10274
		$this->assertTags($result, $expected);
10275
 
10276
		$result = $this->Form->input('Contact.imrequiredonboth');
10277
		$expected = array(
10278
			'div' => array('class' => 'input text required'),
10279
			'label' => array('for' => 'ContactImrequiredonboth'),
10280
			'Imrequiredonboth',
10281
			'/label',
10282
			'input' => array(
10283
				'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
10284
				'id' => 'ContactImrequiredonboth',
10285
				'required' => 'required'
10286
			),
10287
			'/div'
10288
		);
10289
		$this->assertTags($result, $expected);
10290
 
10291
		$result = $this->Form->input('Contact.imrequired');
10292
		$expected = array(
10293
			'div' => array('class' => 'input text required'),
10294
			'label' => array('for' => 'ContactImrequired'),
10295
			'Imrequired',
10296
			'/label',
10297
			'input' => array(
10298
				'type' => 'text', 'name' => 'data[Contact][imrequired]',
10299
				'id' => 'ContactImrequired',
10300
				'required' => 'required'
10301
			),
10302
			'/div'
10303
		);
10304
		$this->assertTags($result, $expected);
10305
	}
10306
 
10307
/**
10308
 * Test inputDefaults setter and getter
10309
 *
10310
 * @return void
10311
 */
10312
	public function testInputDefaults() {
10313
		$this->Form->create('Contact');
10314
 
10315
		$this->Form->inputDefaults(array(
10316
			'label' => false,
10317
			'div' => array(
10318
				'style' => 'color: #000;'
10319
			)
10320
		));
10321
		$result = $this->Form->input('Contact.field1');
10322
		$expected = array(
10323
			'div' => array('class' => 'input text', 'style' => 'color: #000;'),
10324
			'input' => array(
10325
				'type' => 'text', 'name' => 'data[Contact][field1]',
10326
				'id' => 'ContactField1'
10327
			),
10328
			'/div'
10329
		);
10330
		$this->assertTags($result, $expected);
10331
 
10332
		$this->Form->inputDefaults(array(
10333
			'div' => false,
10334
			'label' => 'Label',
10335
		));
10336
		$result = $this->Form->input('Contact.field1');
10337
		$expected = array(
10338
			'label' => array('for' => 'ContactField1'),
10339
			'Label',
10340
			'/label',
10341
			'input' => array(
10342
				'type' => 'text', 'name' => 'data[Contact][field1]',
10343
				'id' => 'ContactField1'
10344
			),
10345
		);
10346
		$this->assertTags($result, $expected);
10347
 
10348
		$this->Form->inputDefaults(array(
10349
			'label' => false,
10350
		), true);
10351
		$result = $this->Form->input('Contact.field1');
10352
		$expected = array(
10353
			'input' => array(
10354
				'type' => 'text', 'name' => 'data[Contact][field1]',
10355
				'id' => 'ContactField1'
10356
			),
10357
		);
10358
		$this->assertTags($result, $expected);
10359
 
10360
		$result = $this->Form->inputDefaults();
10361
		$expected = array(
10362
			'div' => false,
10363
			'label' => false,
10364
		);
10365
		$this->assertEquals($expected, $result);
10366
	}
10367
 
10368
}