comparison cake/tests/cases/libs/view/helpers/form.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * FormHelperTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2006-2010, Cake Software Foundation, Inc.
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs.view.helpers
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 App::import('Core', array('ClassRegistry', 'Controller', 'View', 'Model', 'Security'));
21 App::import('Helper', 'Html');
22 App::import('Helper', 'Form');
23
24 /**
25 * ContactTestController class
26 *
27 * @package cake
28 * @subpackage cake.tests.cases.libs.view.helpers
29 */
30 class ContactTestController extends Controller {
31
32 /**
33 * name property
34 *
35 * @var string 'ContactTest'
36 * @access public
37 */
38 var $name = 'ContactTest';
39
40 /**
41 * uses property
42 *
43 * @var mixed null
44 * @access public
45 */
46 var $uses = null;
47 }
48
49 /**
50 * Contact class
51 *
52 * @package cake
53 * @subpackage cake.tests.cases.libs.view.helpers
54 */
55 class Contact extends CakeTestModel {
56
57 /**
58 * primaryKey property
59 *
60 * @var string 'id'
61 * @access public
62 */
63 var $primaryKey = 'id';
64
65 /**
66 * useTable property
67 *
68 * @var bool false
69 * @access public
70 */
71 var $useTable = false;
72
73 /**
74 * name property
75 *
76 * @var string 'Contact'
77 * @access public
78 */
79 var $name = 'Contact';
80
81 /**
82 * Default schema
83 *
84 * @var array
85 * @access public
86 */
87 var $_schema = array(
88 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
89 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
90 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
91 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
92 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
93 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
94 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
95 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
96 );
97
98 /**
99 * validate property
100 *
101 * @var array
102 * @access public
103 */
104 var $validate = array(
105 'non_existing' => array(),
106 'idontexist' => array(),
107 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
108 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
109 'imrequiredtoo' => array('rule' => 'notEmpty'),
110 'required_one' => array('required' => array('rule' => array('notEmpty'))),
111 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
112 'imalsonotrequired' => array(
113 'alpha' => array('rule' => 'alphaNumeric','allowEmpty' => true),
114 'between' => array('rule' => array('between', 5, 30)),
115 ),
116 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
117 );
118
119 /**
120 * schema method
121 *
122 * @access public
123 * @return void
124 */
125 function setSchema($schema) {
126 $this->_schema = $schema;
127 }
128
129 /**
130 * hasAndBelongsToMany property
131 *
132 * @var array
133 * @access public
134 */
135 var $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
136
137 /**
138 * hasAndBelongsToMany property
139 *
140 * @var array
141 * @access public
142 */
143 var $belongsTo = array('User' => array('className' => 'UserForm'));
144 }
145
146 /**
147 * ContactTagsContact class
148 *
149 * @package cake
150 * @subpackage cake.tests.cases.libs.view.helpers
151 */
152 class ContactTagsContact extends CakeTestModel {
153
154 /**
155 * useTable property
156 *
157 * @var bool false
158 * @access public
159 */
160 var $useTable = false;
161
162 /**
163 * name property
164 *
165 * @var string 'Contact'
166 * @access public
167 */
168 var $name = 'ContactTagsContact';
169
170 /**
171 * Default schema
172 *
173 * @var array
174 * @access public
175 */
176 var $_schema = array(
177 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
178 'contact_tag_id' => array(
179 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
180 )
181 );
182
183 /**
184 * schema method
185 *
186 * @access public
187 * @return void
188 */
189 function setSchema($schema) {
190 $this->_schema = $schema;
191 }
192 }
193
194 /**
195 * ContactNonStandardPk class
196 *
197 * @package cake
198 * @subpackage cake.tests.cases.libs.view.helpers
199 */
200 class ContactNonStandardPk extends Contact {
201
202 /**
203 * primaryKey property
204 *
205 * @var string 'pk'
206 * @access public
207 */
208 var $primaryKey = 'pk';
209
210 /**
211 * name property
212 *
213 * @var string 'ContactNonStandardPk'
214 * @access public
215 */
216 var $name = 'ContactNonStandardPk';
217
218 /**
219 * schema method
220 *
221 * @access public
222 * @return void
223 */
224 function schema() {
225 $this->_schema = parent::schema();
226 $this->_schema['pk'] = $this->_schema['id'];
227 unset($this->_schema['id']);
228 return $this->_schema;
229 }
230 }
231
232 /**
233 * ContactTag class
234 *
235 * @package cake
236 * @subpackage cake.tests.cases.libs.view.helpers
237 */
238 class ContactTag extends Model {
239
240 /**
241 * useTable property
242 *
243 * @var bool false
244 * @access public
245 */
246 var $useTable = false;
247
248 /**
249 * schema definition
250 *
251 * @var array
252 * @access protected
253 */
254 var $_schema = array(
255 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
256 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
257 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
258 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
259 );
260 }
261
262 /**
263 * UserForm class
264 *
265 * @package cake
266 * @subpackage cake.tests.cases.libs.view.helpers
267 */
268 class UserForm extends CakeTestModel {
269
270 /**
271 * useTable property
272 *
273 * @var bool false
274 * @access public
275 */
276 var $useTable = false;
277
278 /**
279 * primaryKey property
280 *
281 * @var string 'id'
282 * @access public
283 */
284 var $primaryKey = 'id';
285
286 /**
287 * name property
288 *
289 * @var string 'UserForm'
290 * @access public
291 */
292 var $name = 'UserForm';
293
294 /**
295 * hasMany property
296 *
297 * @var array
298 * @access public
299 */
300 var $hasMany = array(
301 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
302 ));
303
304 /**
305 * schema definition
306 *
307 * @var array
308 * @access protected
309 */
310 var $_schema = array(
311 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
312 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
313 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
314 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
315 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
316 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
317 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
318 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
319 );
320 }
321
322 /**
323 * OpenidUrl class
324 *
325 * @package cake
326 * @subpackage cake.tests.cases.libs.view.helpers
327 */
328 class OpenidUrl extends CakeTestModel {
329
330 /**
331 * useTable property
332 *
333 * @var bool false
334 * @access public
335 */
336 var $useTable = false;
337
338 /**
339 * primaryKey property
340 *
341 * @var string 'id'
342 * @access public
343 */
344 var $primaryKey = 'id';
345
346 /**
347 * name property
348 *
349 * @var string 'OpenidUrl'
350 * @access public
351 */
352 var $name = 'OpenidUrl';
353
354 /**
355 * belongsTo property
356 *
357 * @var array
358 * @access public
359 */
360 var $belongsTo = array('UserForm' => array(
361 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
362 ));
363
364 /**
365 * validate property
366 *
367 * @var array
368 * @access public
369 */
370 var $validate = array('openid_not_registered' => array());
371
372 /**
373 * schema method
374 *
375 * @var array
376 * @access protected
377 */
378 var $_schema = array(
379 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
380 'user_form_id' => array(
381 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
382 ),
383 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
384 );
385
386 /**
387 * beforeValidate method
388 *
389 * @access public
390 * @return void
391 */
392 function beforeValidate() {
393 $this->invalidate('openid_not_registered');
394 return true;
395 }
396 }
397
398 /**
399 * ValidateUser class
400 *
401 * @package cake
402 * @subpackage cake.tests.cases.libs.view.helpers
403 */
404 class ValidateUser extends CakeTestModel {
405
406 /**
407 * primaryKey property
408 *
409 * @var string 'id'
410 * @access public
411 */
412 var $primaryKey = 'id';
413
414 /**
415 * useTable property
416 *
417 * @var bool false
418 * @access public
419 */
420 var $useTable = false;
421
422 /**
423 * name property
424 *
425 * @var string 'ValidateUser'
426 * @access public
427 */
428 var $name = 'ValidateUser';
429
430 /**
431 * hasOne property
432 *
433 * @var array
434 * @access public
435 */
436 var $hasOne = array('ValidateProfile' => array(
437 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
438 ));
439
440 /**
441 * schema method
442 *
443 * @var array
444 * @access protected
445 */
446 var $_schema = array(
447 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
448 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
449 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
450 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
451 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
452 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
453 );
454
455 /**
456 * beforeValidate method
457 *
458 * @access public
459 * @return void
460 */
461 function beforeValidate() {
462 $this->invalidate('email');
463 return false;
464 }
465 }
466
467 /**
468 * ValidateProfile class
469 *
470 * @package cake
471 * @subpackage cake.tests.cases.libs.view.helpers
472 */
473 class ValidateProfile extends CakeTestModel {
474
475 /**
476 * primaryKey property
477 *
478 * @var string 'id'
479 * @access public
480 */
481 var $primaryKey = 'id';
482
483 /**
484 * useTable property
485 *
486 * @var bool false
487 * @access public
488 */
489 var $useTable = false;
490
491 /**
492 * schema property
493 *
494 * @var array
495 * @access protected
496 */
497 var $_schema = array(
498 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
499 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
500 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
501 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
502 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
503 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
504 );
505
506 /**
507 * name property
508 *
509 * @var string 'ValidateProfile'
510 * @access public
511 */
512 var $name = 'ValidateProfile';
513
514 /**
515 * hasOne property
516 *
517 * @var array
518 * @access public
519 */
520 var $hasOne = array('ValidateItem' => array(
521 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
522 ));
523
524 /**
525 * belongsTo property
526 *
527 * @var array
528 * @access public
529 */
530 var $belongsTo = array('ValidateUser' => array(
531 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
532 ));
533
534 /**
535 * beforeValidate method
536 *
537 * @access public
538 * @return void
539 */
540 function beforeValidate() {
541 $this->invalidate('full_name');
542 $this->invalidate('city');
543 return false;
544 }
545 }
546
547 /**
548 * ValidateItem class
549 *
550 * @package cake
551 * @subpackage cake.tests.cases.libs.view.helpers
552 */
553 class ValidateItem extends CakeTestModel {
554
555 /**
556 * primaryKey property
557 *
558 * @var string 'id'
559 * @access public
560 */
561 var $primaryKey = 'id';
562
563 /**
564 * useTable property
565 *
566 * @var bool false
567 * @access public
568 */
569 var $useTable = false;
570
571 /**
572 * name property
573 *
574 * @var string 'ValidateItem'
575 * @access public
576 */
577 var $name = 'ValidateItem';
578
579 /**
580 * schema property
581 *
582 * @var array
583 * @access protected
584 */
585 var $_schema = array(
586 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
587 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
588 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
589 'description' => array(
590 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
591 ),
592 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
593 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
594 );
595
596 /**
597 * belongsTo property
598 *
599 * @var array
600 * @access public
601 */
602 var $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
603
604 /**
605 * beforeValidate method
606 *
607 * @access public
608 * @return void
609 */
610 function beforeValidate() {
611 $this->invalidate('description');
612 return false;
613 }
614 }
615
616 /**
617 * TestMail class
618 *
619 * @package cake
620 * @subpackage cake.tests.cases.libs.view.helpers
621 */
622 class TestMail extends CakeTestModel {
623
624 /**
625 * primaryKey property
626 *
627 * @var string 'id'
628 * @access public
629 */
630 var $primaryKey = 'id';
631
632 /**
633 * useTable property
634 *
635 * @var bool false
636 * @access public
637 */
638 var $useTable = false;
639
640 /**
641 * name property
642 *
643 * @var string 'TestMail'
644 * @access public
645 */
646 var $name = 'TestMail';
647 }
648
649 /**
650 * FormHelperTest class
651 *
652 * @package cake
653 * @subpackage cake.tests.cases.libs.view.helpers
654 */
655 class FormHelperTest extends CakeTestCase {
656
657 /**
658 * fixtures property
659 *
660 * @var array
661 * @access public
662 */
663 var $fixtures = array(null);
664
665 /**
666 * setUp method
667 *
668 * @access public
669 * @return void
670 */
671 function setUp() {
672 parent::setUp();
673 Router::reload();
674
675 $this->Form =& new FormHelper();
676 $this->Form->Html =& new HtmlHelper();
677 $this->Controller =& new ContactTestController();
678 $this->View =& new View($this->Controller);
679 $this->Form->params['action'] = 'add';
680
681 ClassRegistry::addObject('view', $view);
682 ClassRegistry::addObject('Contact', new Contact());
683 ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
684 ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
685 ClassRegistry::addObject('UserForm', new UserForm());
686 ClassRegistry::addObject('ValidateItem', new ValidateItem());
687 ClassRegistry::addObject('ValidateUser', new ValidateUser());
688 ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
689
690 $this->oldSalt = Configure::read('Security.salt');
691
692 $this->dateRegex = array(
693 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
694 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
695 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
696 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
697 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
698 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
699 );
700
701 Configure::write('Security.salt', 'foo!');
702 }
703
704 /**
705 * tearDown method
706 *
707 * @access public
708 * @return void
709 */
710 function tearDown() {
711 ClassRegistry::removeObject('view');
712 ClassRegistry::removeObject('Contact');
713 ClassRegistry::removeObject('ContactNonStandardPk');
714 ClassRegistry::removeObject('ContactTag');
715 ClassRegistry::removeObject('OpenidUrl');
716 ClassRegistry::removeObject('UserForm');
717 ClassRegistry::removeObject('ValidateItem');
718 ClassRegistry::removeObject('ValidateUser');
719 ClassRegistry::removeObject('ValidateProfile');
720 unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
721 Configure::write('Security.salt', $this->oldSalt);
722 }
723
724
725
726 /**
727 * testFormCreateWithSecurity method
728 *
729 * Test form->create() with security key.
730 *
731 * @access public
732 * @return void
733 */
734 function testCreateWithSecurity() {
735 $this->Form->params['_Token'] = array('key' => 'testKey');
736 $encoding = strtolower(Configure::read('App.encoding'));
737 $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
738 $expected = array(
739 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding),
740 'div' => array('style' => 'display:none;'),
741 array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
742 array('input' => array(
743 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
744 )),
745 '/div'
746 );
747 $this->assertTags($result, $expected,true);
748
749 $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
750 $expected['form']['id'] = 'MyForm';
751 $this->assertTags($result, $expected);
752 }
753
754 /**
755 * test that create() clears the fields property so it starts fresh
756 *
757 * @return void
758 */
759 function testCreateClearingFields() {
760 $this->Form->fields = array('model_id');
761 $this->Form->create('Contact');
762 $this->assertEqual($this->Form->fields, array());
763 }
764
765 /**
766 * Tests form hash generation with model-less data
767 *
768 * @access public
769 * @return void
770 */
771 function testValidateHashNoModel() {
772 $this->Form->params['_Token'] = array('key' => 'foo');
773 $result = $this->Form->secure(array('anything'));
774 $this->assertPattern('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
775 }
776
777 /**
778 * Tests that models with identical field names get resolved properly
779 *
780 * @access public
781 * @return void
782 */
783 function testDuplicateFieldNameResolution() {
784 $result = $this->Form->create('ValidateUser');
785 $this->assertEqual($this->View->entity(), array('ValidateUser'));
786
787 $result = $this->Form->input('ValidateItem.name');
788 $this->assertEqual($this->View->entity(), array('ValidateItem', 'name'));
789
790 $result = $this->Form->input('ValidateUser.name');
791 $this->assertEqual($this->View->entity(), array('ValidateUser', 'name'));
792 $this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
793 $this->assertPattern('/type="text"/', $result);
794
795 $result = $this->Form->input('ValidateItem.name');
796 $this->assertEqual($this->View->entity(), array('ValidateItem', 'name'));
797 $this->assertPattern('/name="data\[ValidateItem\]\[name\]"/', $result);
798 $this->assertPattern('/<textarea/', $result);
799
800 $result = $this->Form->input('name');
801 $this->assertEqual($this->View->entity(), array('ValidateUser', 'name'));
802 $this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
803 $this->assertPattern('/type="text"/', $result);
804 }
805
806 /**
807 * Tests that hidden fields generated for checkboxes don't get locked
808 *
809 * @access public
810 * @return void
811 */
812 function testNoCheckboxLocking() {
813 $this->Form->params['_Token'] = array('key' => 'foo');
814 $this->assertIdentical($this->Form->fields, array());
815
816 $this->Form->checkbox('check', array('value' => '1'));
817 $this->assertIdentical($this->Form->fields, array('check'));
818 }
819
820 /**
821 * testFormSecurityFields method
822 *
823 * Test generation of secure form hash generation.
824 *
825 * @access public
826 * @return void
827 */
828 function testFormSecurityFields() {
829 $key = 'testKey';
830 $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
831 $this->Form->params['_Token']['key'] = $key;
832 $result = $this->Form->secure($fields);
833
834 $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
835 $expected .= ':' . 'Model.valid';
836
837 $expected = array(
838 'div' => array('style' => 'display:none;'),
839 'input' => array(
840 'type' => 'hidden', 'name' => 'data[_Token][fields]',
841 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
842 ),
843 '/div'
844 );
845 $this->assertTags($result, $expected);
846 }
847
848 /**
849 * Tests correct generation of text fields for double and float fields
850 *
851 * @access public
852 * @return void
853 */
854 function testTextFieldGenerationForFloats() {
855 $model = ClassRegistry::getObject('Contact');
856 $model->setSchema(array('foo' => array(
857 'type' => 'float',
858 'null' => false,
859 'default' => null,
860 'length' => null
861 )));
862
863 $this->Form->create('Contact');
864 $result = $this->Form->input('foo');
865 $expected = array(
866 'div' => array('class' => 'input text'),
867 'label' => array('for' => 'ContactFoo'),
868 'Foo',
869 '/label',
870 array('input' => array(
871 'type' => 'text', 'name' => 'data[Contact][foo]',
872 'id' => 'ContactFoo'
873 )),
874 '/div'
875 );
876 }
877
878 /**
879 * testFormSecurityMultipleFields method
880 *
881 * Test secure() with multiple row form. Ensure hash is correct.
882 *
883 * @access public
884 * @return void
885 */
886 function testFormSecurityMultipleFields() {
887 $key = 'testKey';
888
889 $fields = array(
890 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
891 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
892 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
893 );
894 $this->Form->params['_Token']['key'] = $key;
895 $result = $this->Form->secure($fields);
896
897 $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
898 $hash .= '%7CModel.1.hidden%7CModel.1.valid';
899
900 $expected = array(
901 'div' => array('style' => 'display:none;'),
902 'input' => array(
903 'type' => 'hidden', 'name' => 'data[_Token][fields]',
904 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
905 ),
906 '/div'
907 );
908 $this->assertTags($result, $expected);
909 }
910
911 /**
912 * testFormSecurityMultipleSubmitButtons
913 *
914 * test form submit generation and ensure that _Token is only created on end()
915 *
916 * @return void
917 */
918 function testFormSecurityMultipleSubmitButtons() {
919 $key = 'testKey';
920 $this->Form->params['_Token']['key'] = $key;
921
922 $this->Form->create('Addresses');
923 $this->Form->input('Address.title');
924 $this->Form->input('Address.first_name');
925
926 $result = $this->Form->submit('Save', array('name' => 'save'));
927 $expected = array(
928 'div' => array('class' => 'submit'),
929 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
930 '/div',
931 );
932 $this->assertTags($result, $expected);
933 $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
934 $expected = array(
935 'div' => array('class' => 'submit'),
936 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
937 '/div',
938 );
939 $this->assertTags($result, $expected);
940 $result = $this->Form->end(null);
941
942 $expected = array(
943 'div' => array('style' => 'display:none;'),
944 'input' => array(
945 'type' => 'hidden', 'name' => 'data[_Token][fields]',
946 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
947 ),
948 '/div'
949 );
950 $this->assertTags($result, $expected);
951 }
952
953 /**
954 * testFormSecurityMultipleInputFields method
955 *
956 * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
957 *
958 * @access public
959 * @return void
960 */
961 function testFormSecurityMultipleInputFields() {
962 $key = 'testKey';
963
964 $this->Form->params['_Token']['key'] = $key;
965 $this->Form->create('Addresses');
966
967 $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
968 $this->Form->input('Addresses.0.title');
969 $this->Form->input('Addresses.0.first_name');
970 $this->Form->input('Addresses.0.last_name');
971 $this->Form->input('Addresses.0.address');
972 $this->Form->input('Addresses.0.city');
973 $this->Form->input('Addresses.0.phone');
974 $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
975
976 $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
977 $this->Form->input('Addresses.1.title');
978 $this->Form->input('Addresses.1.first_name');
979 $this->Form->input('Addresses.1.last_name');
980 $this->Form->input('Addresses.1.address');
981 $this->Form->input('Addresses.1.city');
982 $this->Form->input('Addresses.1.phone');
983 $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
984
985 $result = $this->Form->secure($this->Form->fields);
986
987 $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
988
989 $expected = array(
990 'div' => array('style' => 'display:none;'),
991 'input' => array(
992 'type' => 'hidden', 'name' => 'data[_Token][fields]',
993 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
994 ),
995 '/div'
996 );
997 $this->assertTags($result, $expected);
998 }
999
1000 /**
1001 * testFormSecurityMultipleInputDisabledFields method
1002 *
1003 * test secure form generation with multiple records and disabled fields.
1004 *
1005 * @access public
1006 * @return void
1007 */
1008 function testFormSecurityMultipleInputDisabledFields() {
1009 $key = 'testKey';
1010 $this->Form->params['_Token']['key'] = $key;
1011 $this->Form->params['_Token']['disabledFields'] = array('first_name', 'address');
1012 $this->Form->create();
1013
1014 $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
1015 $this->Form->input('Addresses.0.title');
1016 $this->Form->input('Addresses.0.first_name');
1017 $this->Form->input('Addresses.0.last_name');
1018 $this->Form->input('Addresses.0.address');
1019 $this->Form->input('Addresses.0.city');
1020 $this->Form->input('Addresses.0.phone');
1021 $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
1022 $this->Form->input('Addresses.1.title');
1023 $this->Form->input('Addresses.1.first_name');
1024 $this->Form->input('Addresses.1.last_name');
1025 $this->Form->input('Addresses.1.address');
1026 $this->Form->input('Addresses.1.city');
1027 $this->Form->input('Addresses.1.phone');
1028
1029 $result = $this->Form->secure($this->Form->fields);
1030 $hash = '774df31936dc850b7d8a5277dc0b890123788b09%3AAddresses.0.id%7CAddresses.1.id';
1031
1032 $expected = array(
1033 'div' => array('style' => 'display:none;'),
1034 'input' => array(
1035 'type' => 'hidden', 'name' => 'data[_Token][fields]',
1036 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1037 ),
1038 '/div'
1039 );
1040 $this->assertTags($result, $expected);
1041 }
1042
1043 /**
1044 * testFormSecurityInputDisabledFields method
1045 *
1046 * Test single record form with disabled fields.
1047 *
1048 * @access public
1049 * @return void
1050 */
1051 function testFormSecurityInputDisabledFields() {
1052 $key = 'testKey';
1053 $this->Form->params['_Token']['key'] = $key;
1054 $this->Form->params['_Token']['disabledFields'] = array('first_name', 'address');
1055 $this->Form->create();
1056
1057 $this->Form->hidden('Addresses.id', array('value' => '123456'));
1058 $this->Form->input('Addresses.title');
1059 $this->Form->input('Addresses.first_name');
1060 $this->Form->input('Addresses.last_name');
1061 $this->Form->input('Addresses.address');
1062 $this->Form->input('Addresses.city');
1063 $this->Form->input('Addresses.phone');
1064
1065 $result = $this->Form->fields;
1066 $expected = array(
1067 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
1068 'Addresses.city', 'Addresses.phone'
1069 );
1070 $this->assertEqual($result, $expected);
1071
1072 $result = $this->Form->secure($expected);
1073
1074 $hash = '449b7e889128e8e52c5e81d19df68f5346571492%3AAddresses.id';
1075 $expected = array(
1076 'div' => array('style' => 'display:none;'),
1077 'input' => array(
1078 'type' => 'hidden', 'name' => 'data[_Token][fields]',
1079 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1080 ),
1081 '/div'
1082 );
1083 $this->assertTags($result, $expected);
1084 }
1085
1086 /**
1087 * test securing inputs with custom name attributes.
1088 *
1089 * @return void
1090 */
1091 function testFormSecureWithCustomNameAttribute() {
1092 $this->Form->params['_Token']['key'] = 'testKey';
1093
1094 $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
1095 $this->assertEqual('User.custom', $this->Form->fields[0]);
1096
1097 $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
1098 $this->assertEqual('User.custom.another.value', $this->Form->fields[1]);
1099 }
1100
1101 /**
1102 * testFormSecuredInput method
1103 *
1104 * Test generation of entire secure form, assertions made on input() output.
1105 *
1106 * @access public
1107 * @return void
1108 */
1109 function testFormSecuredInput() {
1110 $this->Form->params['_Token']['key'] = 'testKey';
1111
1112 $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
1113 $encoding = strtolower(Configure::read('App.encoding'));
1114 $expected = array(
1115 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding),
1116 'div' => array('style' => 'display:none;'),
1117 array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
1118 array('input' => array(
1119 'type' => 'hidden', 'name' => 'data[_Token][key]',
1120 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
1121 )),
1122 '/div'
1123 );
1124 $this->assertTags($result, $expected);
1125
1126 $result = $this->Form->input('UserForm.published', array('type' => 'text'));
1127 $expected = array(
1128 'div' => array('class' => 'input text'),
1129 'label' => array('for' => 'UserFormPublished'),
1130 'Published',
1131 '/label',
1132 array('input' => array(
1133 'type' => 'text', 'name' => 'data[UserForm][published]',
1134 'id' => 'UserFormPublished'
1135 )),
1136 '/div'
1137 );
1138 $this->assertTags($result, $expected);
1139
1140 $result = $this->Form->input('UserForm.other', array('type' => 'text'));
1141 $expected = array(
1142 'div' => array('class' => 'input text'),
1143 'label' => array('for' => 'UserFormOther'),
1144 'Other',
1145 '/label',
1146 array('input' => array(
1147 'type' => 'text', 'name' => 'data[UserForm][other]',
1148 'id' => 'UserFormOther'
1149 )),
1150 '/div'
1151 );
1152 $this->assertTags($result, $expected);
1153
1154 $result = $this->Form->hidden('UserForm.stuff');
1155 $expected = array('input' => array(
1156 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
1157 'id' => 'UserFormStuff'
1158 ));
1159 $this->assertTags($result, $expected);
1160
1161 $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
1162 $expected = array('input' => array(
1163 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
1164 'value' => '0', 'id' => 'UserFormHidden'
1165 ));
1166 $this->assertTags($result, $expected);
1167
1168 $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
1169 $expected = array(
1170 'div' => array('class' => 'input checkbox'),
1171 array('input' => array(
1172 'type' => 'hidden', 'name' => 'data[UserForm][something]',
1173 'value' => '0', 'id' => 'UserFormSomething_'
1174 )),
1175 array('input' => array(
1176 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
1177 'value' => '1', 'id' => 'UserFormSomething'
1178 )),
1179 'label' => array('for' => 'UserFormSomething'),
1180 'Something',
1181 '/label',
1182 '/div'
1183 );
1184 $this->assertTags($result, $expected);
1185
1186 $result = $this->Form->fields;
1187 $expected = array(
1188 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
1189 'UserForm.hidden' => '0', 'UserForm.something'
1190 );
1191 $this->assertEqual($result, $expected);
1192
1193 $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
1194
1195 $result = $this->Form->secure($this->Form->fields);
1196 $expected = array(
1197 'div' => array('style' => 'display:none;'),
1198 array('input' => array(
1199 'type' => 'hidden', 'name' => 'data[_Token][fields]',
1200 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
1201 )),
1202 '/div'
1203 );
1204 $this->assertTags($result, $expected);
1205 }
1206
1207 /**
1208 * Tests that the correct keys are added to the field hash index
1209 *
1210 * @access public
1211 * @return void
1212 */
1213 function testFormSecuredFileInput() {
1214 $this->Form->params['_Token']['key'] = 'testKey';
1215 $this->assertEqual($this->Form->fields, array());
1216
1217 $result = $this->Form->file('Attachment.file');
1218 $expected = array (
1219 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
1220 'Attachment.file.error', 'Attachment.file.size'
1221 );
1222 $this->assertEqual($this->Form->fields, $expected);
1223 }
1224
1225 /**
1226 * test that multiple selects keys are added to field hash
1227 *
1228 * @access public
1229 * @return void
1230 */
1231 function testFormSecuredMultipleSelect() {
1232 $this->Form->params['_Token']['key'] = 'testKey';
1233 $this->assertEqual($this->Form->fields, array());
1234 $options = array('1' => 'one', '2' => 'two');
1235
1236 $this->Form->select('Model.select', $options);
1237 $expected = array('Model.select');
1238 $this->assertEqual($this->Form->fields, $expected);
1239
1240 $this->Form->fields = array();
1241 $this->Form->select('Model.select', $options, null, array('multiple' => true));
1242 $this->assertEqual($this->Form->fields, $expected);
1243 }
1244
1245 /**
1246 * testFormSecuredRadio method
1247 *
1248 * @access public
1249 * @return void
1250 */
1251 function testFormSecuredRadio() {
1252 $this->Form->params['_Token']['key'] = 'testKey';
1253 $this->assertEqual($this->Form->fields, array());
1254 $options = array('1' => 'option1', '2' => 'option2');
1255
1256 $this->Form->radio('Test.test', $options);
1257 $expected = array('Test.test');
1258 $this->assertEqual($this->Form->fields, $expected);
1259 }
1260
1261 /**
1262 * testDisableSecurityUsingForm method
1263 *
1264 * @access public
1265 * @return void
1266 */
1267 function testDisableSecurityUsingForm() {
1268 $this->Form->params['_Token']['key'] = 'testKey';
1269 $this->Form->params['_Token']['disabledFields'] = array();
1270 $this->Form->create();
1271
1272 $this->Form->hidden('Addresses.id', array('value' => '123456'));
1273 $this->Form->input('Addresses.title');
1274 $this->Form->input('Addresses.first_name', array('secure' => false));
1275 $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
1276 $this->Form->input('Addresses.zip', array(
1277 'type' => 'select', 'options' => array(1,2), 'secure' => false
1278 ));
1279
1280 $result = $this->Form->fields;
1281 $expected = array(
1282 'Addresses.id' => '123456', 'Addresses.title',
1283 );
1284 $this->assertEqual($result, $expected);
1285 }
1286 /**
1287 * testPasswordValidation method
1288 *
1289 * test validation errors on password input.
1290 *
1291 * @access public
1292 * @return void
1293 */
1294 function testPasswordValidation() {
1295 $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
1296 $result = $this->Form->input('Contact.password');
1297 $expected = array(
1298 'div' => array('class' => 'input password error'),
1299 'label' => array('for' => 'ContactPassword'),
1300 'Password',
1301 '/label',
1302 'input' => array(
1303 'type' => 'password', 'name' => 'data[Contact][password]',
1304 'id' => 'ContactPassword', 'class' => 'form-error'
1305 ),
1306 array('div' => array('class' => 'error-message')),
1307 'Please provide a password',
1308 '/div',
1309 '/div'
1310 );
1311 $this->assertTags($result, $expected);
1312 }
1313
1314 /**
1315 * testFormValidationAssociated method
1316 *
1317 * test display of form errors in conjunction with model::validates.
1318 *
1319 * @access public
1320 * @return void
1321 */
1322 function testFormValidationAssociated() {
1323 $this->UserForm =& ClassRegistry::getObject('UserForm');
1324 $this->UserForm->OpenidUrl =& ClassRegistry::getObject('OpenidUrl');
1325
1326 $data = array(
1327 'UserForm' => array('name' => 'user'),
1328 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
1329 );
1330
1331 $this->assertTrue($this->UserForm->OpenidUrl->create($data));
1332 $this->assertFalse($this->UserForm->OpenidUrl->validates());
1333
1334 $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
1335 $encoding = strtolower(Configure::read('App.encoding'));
1336 $expected = array(
1337 'form' => array(
1338 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
1339 'accept-charset' => $encoding
1340 ),
1341 'div' => array('style' => 'display:none;'),
1342 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1343 '/div'
1344 );
1345 $this->assertTags($result, $expected);
1346
1347 $expected = array('OpenidUrl' => array('openid_not_registered' => 1));
1348 $this->assertEqual($this->Form->validationErrors, $expected);
1349
1350 $result = $this->Form->error(
1351 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
1352 );
1353 $this->assertEqual($result, 'Error, not registered');
1354
1355 unset($this->UserForm->OpenidUrl, $this->UserForm);
1356 }
1357
1358 /**
1359 * testFormValidationAssociatedFirstLevel method
1360 *
1361 * test form error display with associated model.
1362 *
1363 * @access public
1364 * @return void
1365 */
1366 function testFormValidationAssociatedFirstLevel() {
1367 $this->ValidateUser =& ClassRegistry::getObject('ValidateUser');
1368 $this->ValidateUser->ValidateProfile =& ClassRegistry::getObject('ValidateProfile');
1369
1370 $data = array(
1371 'ValidateUser' => array('name' => 'mariano'),
1372 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
1373 );
1374
1375 $this->assertTrue($this->ValidateUser->create($data));
1376 $this->assertFalse($this->ValidateUser->validates());
1377 $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1378
1379 $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1380 $encoding = strtolower(Configure::read('App.encoding'));
1381 $expected = array(
1382 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
1383 'div' => array('style' => 'display:none;'),
1384 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1385 '/div'
1386 );
1387 $this->assertTags($result, $expected);
1388
1389 $expected = array(
1390 'ValidateUser' => array('email' => 1),
1391 'ValidateProfile' => array('full_name' => 1, 'city' => 1)
1392 );
1393 $this->assertEqual($this->Form->validationErrors, $expected);
1394
1395 unset($this->ValidateUser->ValidateProfile);
1396 unset($this->ValidateUser);
1397 }
1398
1399 /**
1400 * testFormValidationAssociatedSecondLevel method
1401 *
1402 * test form error display with associated model.
1403 *
1404 * @access public
1405 * @return void
1406 */
1407 function testFormValidationAssociatedSecondLevel() {
1408 $this->ValidateUser =& ClassRegistry::getObject('ValidateUser');
1409 $this->ValidateUser->ValidateProfile =& ClassRegistry::getObject('ValidateProfile');
1410 $this->ValidateUser->ValidateProfile->ValidateItem =& ClassRegistry::getObject('ValidateItem');
1411
1412 $data = array(
1413 'ValidateUser' => array('name' => 'mariano'),
1414 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
1415 'ValidateItem' => array('name' => 'Item')
1416 );
1417
1418 $this->assertTrue($this->ValidateUser->create($data));
1419 $this->assertFalse($this->ValidateUser->validates());
1420 $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
1421 $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
1422
1423 $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
1424 $encoding = strtolower(Configure::read('App.encoding'));
1425 $expected = array(
1426 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
1427 'div' => array('style' => 'display:none;'),
1428 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
1429 '/div'
1430 );
1431 $this->assertTags($result, $expected);
1432
1433 $expected = array(
1434 'ValidateUser' => array('email' => 1),
1435 'ValidateProfile' => array('full_name' => 1, 'city' => 1),
1436 'ValidateItem' => array('description' => 1)
1437 );
1438 $this->assertEqual($this->Form->validationErrors, $expected);
1439
1440 unset($this->ValidateUser->ValidateProfile->ValidateItem);
1441 unset($this->ValidateUser->ValidateProfile);
1442 unset($this->ValidateUser);
1443 }
1444
1445 /**
1446 * testFormValidationMultiRecord method
1447 *
1448 * test form error display with multiple records.
1449 *
1450 * @access public
1451 * @return void
1452 */
1453 function testFormValidationMultiRecord() {
1454 $this->Form->validationErrors['Contact'] = array(2 => array(
1455 'name' => 'This field cannot be left blank'
1456 ));
1457 $result = $this->Form->input('Contact.2.name');
1458 $expected = array(
1459 'div' => array('class'),
1460 'label' => array('for'),
1461 'preg:/[^<]+/',
1462 '/label',
1463 'input' => array(
1464 'type' => 'text', 'name', 'id',
1465 'class' => 'form-error', 'maxlength' => 255
1466 ),
1467 array('div' => array('class' => 'error-message')),
1468 'This field cannot be left blank',
1469 '/div',
1470 '/div'
1471 );
1472 $this->assertTags($result, $expected);
1473
1474 $this->Form->validationErrors['UserForm'] = array(
1475 'OpenidUrl' => array('url' => 'You must provide a URL'
1476 ));
1477 $this->Form->create('UserForm');
1478 $result = $this->Form->input('OpenidUrl.url');
1479 $expected = array(
1480 'div' => array('class'),
1481 'label' => array('for'),
1482 'preg:/[^<]+/',
1483 '/label',
1484 'input' => array(
1485 'type' => 'text', 'name', 'id', 'class' => 'form-error'
1486 ),
1487 array('div' => array('class' => 'error-message')),
1488 'You must provide a URL',
1489 '/div',
1490 '/div'
1491 );
1492 }
1493
1494 /**
1495 * testMultipleInputValidation method
1496 *
1497 * test multiple record form validation error display.
1498 *
1499 * @access public
1500 * @return void
1501 */
1502 function testMultipleInputValidation() {
1503 $this->Form->create();
1504 $this->Form->validationErrors['Address'][0]['title'] = 'This field cannot be empty';
1505 $this->Form->validationErrors['Address'][0]['first_name'] = 'This field cannot be empty';
1506 $this->Form->validationErrors['Address'][1]['last_name'] = 'You must have a last name';
1507
1508 $result = $this->Form->input('Address.0.title');
1509 $expected = array(
1510 'div' => array('class'),
1511 'label' => array('for'),
1512 'preg:/[^<]+/',
1513 '/label',
1514 'input' => array(
1515 'type' => 'text', 'name', 'id', 'class' => 'form-error'
1516 ),
1517 array('div' => array('class' => 'error-message')),
1518 'This field cannot be empty',
1519 '/div',
1520 '/div'
1521 );
1522 $this->assertTags($result, $expected);
1523
1524 $result = $this->Form->input('Address.0.first_name');
1525 $expected = array(
1526 'div' => array('class'),
1527 'label' => array('for'),
1528 'preg:/[^<]+/',
1529 '/label',
1530 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
1531 array('div' => array('class' => 'error-message')),
1532 'This field cannot be empty',
1533 '/div',
1534 '/div'
1535 );
1536 $this->assertTags($result, $expected);
1537
1538 $result = $this->Form->input('Address.0.last_name');
1539 $expected = array(
1540 'div' => array('class'),
1541 'label' => array('for'),
1542 'preg:/[^<]+/',
1543 '/label',
1544 'input' => array('type' => 'text', 'name', 'id'),
1545 '/div'
1546 );
1547 $this->assertTags($result, $expected);
1548
1549 $result = $this->Form->input('Address.1.last_name');
1550 $expected = array(
1551 'div' => array('class'),
1552 'label' => array('for'),
1553 'preg:/[^<]+/',
1554 '/label',
1555 'input' => array(
1556 'type' => 'text', 'name' => 'preg:/[^<]+/',
1557 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
1558 ),
1559 array('div' => array('class' => 'error-message')),
1560 'You must have a last name',
1561 '/div',
1562 '/div'
1563 );
1564 $this->assertTags($result, $expected);
1565 }
1566
1567 /**
1568 * testInput method
1569 *
1570 * Test various incarnations of input().
1571 *
1572 * @access public
1573 * @return void
1574 */
1575 function testInput() {
1576 $result = $this->Form->input('ValidateUser.balance');
1577 $expected = array(
1578 'div' => array('class'),
1579 'label' => array('for'),
1580 'Balance',
1581 '/label',
1582 'input' => array('name', 'type' => 'text', 'maxlength' => 8, 'id'),
1583 '/div',
1584 );
1585 $this->assertTags($result, $expected);
1586
1587 $result = $this->Form->input('Contact.email', array('id' => 'custom'));
1588 $expected = array(
1589 'div' => array('class' => 'input text'),
1590 'label' => array('for' => 'custom'),
1591 'Email',
1592 '/label',
1593 array('input' => array(
1594 'type' => 'text', 'name' => 'data[Contact][email]',
1595 'id' => 'custom', 'maxlength' => 255
1596 )),
1597 '/div'
1598 );
1599 $this->assertTags($result, $expected);
1600
1601 $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
1602 $expected = array(
1603 '<div',
1604 'label' => array('for' => 'ContactEmail'),
1605 'Email',
1606 '/label',
1607 array('input' => array(
1608 'type' => 'text', 'name' => 'data[Contact][email]',
1609 'id' => 'ContactEmail', 'maxlength' => 255
1610 )),
1611 '/div'
1612 );
1613 $this->assertTags($result, $expected);
1614
1615 $result = $this->Form->hidden('Contact.idontexist');
1616 $expected = array('input' => array(
1617 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
1618 'id' => 'ContactIdontexist'
1619 ));
1620 $this->assertTags($result, $expected);
1621
1622 $result = $this->Form->input('Contact.email', array('type' => 'text'));
1623 $expected = array(
1624 'div' => array('class' => 'input text'),
1625 'label' => array('for' => 'ContactEmail'),
1626 'Email',
1627 '/label',
1628 array('input' => array(
1629 'type' => 'text', 'name' => 'data[Contact][email]',
1630 'id' => 'ContactEmail'
1631 )),
1632 '/div'
1633 );
1634 $this->assertTags($result, $expected);
1635
1636 $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
1637 $expected = array(
1638 'div' => array('class' => 'input text'),
1639 'label' => array('for' => 'Contact5Email'),
1640 'Email',
1641 '/label',
1642 array('input' => array(
1643 'type' => 'text', 'name' => 'data[Contact][5][email]',
1644 'id' => 'Contact5Email'
1645 )),
1646 '/div'
1647 );
1648 $this->assertTags($result, $expected);
1649
1650 $result = $this->Form->input('Contact.password');
1651 $expected = array(
1652 'div' => array('class' => 'input password'),
1653 'label' => array('for' => 'ContactPassword'),
1654 'Password',
1655 '/label',
1656 array('input' => array(
1657 'type' => 'password', 'name' => 'data[Contact][password]',
1658 'id' => 'ContactPassword'
1659 )),
1660 '/div'
1661 );
1662 $this->assertTags($result, $expected);
1663
1664 $result = $this->Form->input('Contact.email', array(
1665 'type' => 'file', 'class' => 'textbox'
1666 ));
1667 $expected = array(
1668 'div' => array('class' => 'input file'),
1669 'label' => array('for' => 'ContactEmail'),
1670 'Email',
1671 '/label',
1672 array('input' => array(
1673 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
1674 'id' => 'ContactEmail'
1675 )),
1676 '/div'
1677 );
1678 $this->assertTags($result, $expected);
1679
1680 $this->Form->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
1681 $result = $this->Form->input('Contact.phone');
1682 $expected = array(
1683 'div' => array('class' => 'input text'),
1684 'label' => array('for' => 'ContactPhone'),
1685 'Phone',
1686 '/label',
1687 array('input' => array(
1688 'type' => 'text', 'name' => 'data[Contact][phone]',
1689 'value' => 'Hello &amp; World &gt; weird chars',
1690 'id' => 'ContactPhone', 'maxlength' => 255
1691 )),
1692 '/div'
1693 );
1694 $this->assertTags($result, $expected);
1695
1696 $this->Form->data['Model']['0']['OtherModel']['field'] = 'My value';
1697 $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
1698 $expected = array(
1699 'div' => array('class' => 'input text'),
1700 'label' => array('for' => 'myId'),
1701 'Field',
1702 '/label',
1703 'input' => array(
1704 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
1705 'value' => 'My value', 'id' => 'myId'
1706 ),
1707 '/div'
1708 );
1709 $this->assertTags($result, $expected);
1710
1711 unset($this->Form->data);
1712
1713 $this->Form->validationErrors['Model']['field'] = 'Badness!';
1714 $result = $this->Form->input('Model.field');
1715 $expected = array(
1716 'div' => array('class' => 'input text error'),
1717 'label' => array('for' => 'ModelField'),
1718 'Field',
1719 '/label',
1720 'input' => array(
1721 'type' => 'text', 'name' => 'data[Model][field]',
1722 'id' => 'ModelField', 'class' => 'form-error'
1723 ),
1724 array('div' => array('class' => 'error-message')),
1725 'Badness!',
1726 '/div',
1727 '/div'
1728 );
1729 $this->assertTags($result, $expected);
1730
1731 $result = $this->Form->input('Model.field', array(
1732 'div' => false, 'error' => array('wrap' => 'span')
1733 ));
1734 $expected = array(
1735 'label' => array('for' => 'ModelField'),
1736 'Field',
1737 '/label',
1738 'input' => array(
1739 'type' => 'text', 'name' => 'data[Model][field]',
1740 'id' => 'ModelField', 'class' => 'form-error'
1741 ),
1742 array('span' => array('class' => 'error-message')),
1743 'Badness!',
1744 '/span'
1745 );
1746 $this->assertTags($result, $expected);
1747
1748 $result = $this->Form->input('Model.field', array(
1749 'div' => array('tag' => 'span'), 'error' => array('wrap' => false)
1750 ));
1751 $expected = array(
1752 'span' => array('class' => 'input text error'),
1753 'label' => array('for' => 'ModelField'),
1754 'Field',
1755 '/label',
1756 'input' => array(
1757 'type' => 'text', 'name' => 'data[Model][field]',
1758 'id' => 'ModelField', 'class' => 'form-error'
1759 ),
1760 'Badness!',
1761 '/span'
1762 );
1763 $this->assertTags($result, $expected);
1764
1765 $result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy'));
1766 $expected = array(
1767 'div' => array('class' => 'input text error'),
1768 'label' => array('for' => 'ModelField'),
1769 'Field',
1770 '/label',
1771 'input' => array(
1772 'type' => 'text', 'name' => 'data[Model][field]',
1773 'id' => 'ModelField', 'class' => 'form-error'
1774 ),
1775 'A message to you, Rudy',
1776 array('div' => array('class' => 'error-message')),
1777 'Badness!',
1778 '/div',
1779 '/div'
1780 );
1781 $this->assertTags($result, $expected);
1782
1783 $this->Form->setEntity(null);
1784 $this->Form->setEntity('Model.field');
1785 $result = $this->Form->input('Model.field', array(
1786 'after' => 'A message to you, Rudy', 'error' => false
1787 ));
1788 $expected = array(
1789 'div' => array('class' => 'input text'),
1790 'label' => array('for' => 'ModelField'),
1791 'Field',
1792 '/label',
1793 'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
1794 'A message to you, Rudy',
1795 '/div'
1796 );
1797 $this->assertTags($result, $expected);
1798
1799 unset($this->Form->validationErrors['Model']['field']);
1800 $result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy'));
1801 $expected = array(
1802 'div' => array('class' => 'input text'),
1803 'label' => array('for' => 'ModelField'),
1804 'Field',
1805 '/label',
1806 'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField'),
1807 'A message to you, Rudy',
1808 '/div'
1809 );
1810 $this->assertTags($result, $expected);
1811
1812 $this->Form->validationErrors['Model']['field'] = 'minLength';
1813 $result = $this->Form->input('Model.field', array(
1814 'error' => array(
1815 'minLength' => 'Le login doit contenir au moins 2 caractères',
1816 'maxLength' => 'login too large'
1817 )
1818 ));
1819 $expected = array(
1820 'div' => array('class' => 'input text error'),
1821 'label' => array('for' => 'ModelField'),
1822 'Field',
1823 '/label',
1824 'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
1825 array('div' => array('class' => 'error-message')),
1826 'Le login doit contenir au moins 2 caractères',
1827 '/div',
1828 '/div'
1829 );
1830 $this->assertTags($result, $expected);
1831
1832 $this->Form->validationErrors['Model']['field'] = 'maxLength';
1833 $result = $this->Form->input('Model.field', array(
1834 'error' => array(
1835 'wrap' => 'span',
1836 'attributes' => array('rel' => 'fake'),
1837 'minLength' => 'Le login doit contenir au moins 2 caractères',
1838 'maxLength' => 'login too large',
1839 )
1840 ));
1841 $expected = array(
1842 'div' => array('class' => 'input text error'),
1843 'label' => array('for' => 'ModelField'),
1844 'Field',
1845 '/label',
1846 'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
1847 array('span' => array('class' => 'error-message', 'rel' => 'fake')),
1848 'login too large',
1849 '/span',
1850 '/div'
1851 );
1852 $this->assertTags($result, $expected);
1853 }
1854
1855 /**
1856 * test input() with checkbox creation
1857 *
1858 * @return void
1859 */
1860 function testInputCheckbox() {
1861 $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
1862 $expected = array(
1863 'div' => array('class' => 'input checkbox'),
1864 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
1865 array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
1866 '/div'
1867 );
1868 $this->assertTags($result, $expected);
1869
1870 $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
1871 $expected = array(
1872 'div' => array('class' => 'input checkbox'),
1873 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
1874 array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
1875 '/div'
1876 );
1877 $this->assertTags($result, $expected);
1878
1879 $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
1880 $expected = array(
1881 'div' => array('class' => 'input checkbox'),
1882 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
1883 array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
1884 '/div'
1885 );
1886 $this->assertTags($result, $expected);
1887 }
1888
1889 /**
1890 * test form->input() with time types.
1891 *
1892 */
1893 function testInputTime() {
1894 extract($this->dateRegex);
1895 $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
1896 $result = explode(':', $result);
1897 $this->assertPattern('/option value="23"/', $result[0]);
1898 $this->assertNoPattern('/option value="24"/', $result[0]);
1899
1900 $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
1901 $result = explode(':', $result);
1902 $this->assertPattern('/option value="23"/', $result[0]);
1903 $this->assertNoPattern('/option value="24"/', $result[0]);
1904
1905 $result = $this->Form->input('Model.field', array(
1906 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
1907 ));
1908 $result = explode(':', $result);
1909 $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
1910 $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
1911 $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
1912
1913 $result = $this->Form->input('Model.field', array(
1914 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
1915 ));
1916 $result = explode(':', $result);
1917 $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
1918 $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
1919 $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
1920
1921 $result = $this->Form->input('prueba', array(
1922 'type' => 'time', 'timeFormat'=> 24 , 'dateFormat'=>'DMY' , 'minYear' => 2008,
1923 'maxYear' => date('Y') + 1 ,'interval' => 15
1924 ));
1925 $result = explode(':', $result);
1926 $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
1927 $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
1928 $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
1929 $this->assertPattern('#<option value="30"[^>]*>30</option>#', $result[1]);
1930
1931 $result = $this->Form->input('Random.start_time', array(
1932 'type' => 'time',
1933 'selected' => '18:15'
1934 ));
1935 $this->assertPattern('#<option value="06"[^>]*>6</option>#', $result);
1936 $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result);
1937 $this->assertPattern('#<option value="pm"[^>]*>pm</option>#', $result);
1938 }
1939
1940 /**
1941 * test form->input() with datetime, date and time types
1942 *
1943 * @return void
1944 */
1945 function testInputDatetime() {
1946 extract($this->dateRegex);
1947 $result = $this->Form->input('prueba', array(
1948 'type' => 'datetime', 'timeFormat'=> 24 , 'dateFormat'=>'DMY' , 'minYear' => 2008,
1949 'maxYear' => date('Y') + 1 ,'interval' => 15
1950 ));
1951 $result = explode(':', $result);
1952 $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
1953 $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
1954 $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
1955 $this->assertPattern('#<option value="30"[^>]*>30</option>#', $result[1]);
1956
1957 //related to ticket #5013
1958 $result = $this->Form->input('Contact.date', array(
1959 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
1960 ));
1961 $this->assertPattern('/class="customClass"/', $result);
1962 $this->assertPattern('/onChange="function\(\)\{\}"/', $result);
1963
1964 $result = $this->Form->input('Contact.date', array(
1965 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
1966 ));
1967 $this->assertPattern('/id="customIdDay"/', $result);
1968 $this->assertPattern('/id="customIdMonth"/', $result);
1969 $this->assertPattern('/onChange="function\(\)\{\}"/', $result);
1970
1971 $result = $this->Form->input('Model.field', array(
1972 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
1973 ));
1974 $this->assertPattern('/id="customIDDay"/', $result);
1975 $this->assertPattern('/id="customIDHour"/', $result);
1976 $result = explode('</select><select', $result);
1977 $result = explode(':', $result[1]);
1978 $this->assertPattern('/option value="23"/', $result[0]);
1979 $this->assertNoPattern('/option value="24"/', $result[0]);
1980
1981 $result = $this->Form->input('Model.field', array(
1982 'type' => 'datetime', 'timeFormat' => 12
1983 ));
1984 $result = explode('</select><select', $result);
1985 $result = explode(':', $result[1]);
1986 $this->assertPattern('/option value="12"/', $result[0]);
1987 $this->assertNoPattern('/option value="13"/', $result[0]);
1988
1989 $this->Form->data = array('Contact' => array('created' => null));
1990 $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
1991 $expected = array(
1992 'div' => array('class' => 'input date'),
1993 'label' => array('for' => 'ContactCreatedMonth'),
1994 'Created',
1995 '/label',
1996 array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
1997 array('option' => array('value' => '')), 'Date Unknown', '/option',
1998 $monthsRegex,
1999 '/select', '-',
2000 array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
2001 array('option' => array('value' => '')), 'Date Unknown', '/option',
2002 $daysRegex,
2003 '/select', '-',
2004 array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
2005 array('option' => array('value' => '')), 'Date Unknown', '/option',
2006 $yearsRegex,
2007 '/select',
2008 '/div'
2009 );
2010 $this->assertTags($result, $expected);
2011
2012 $this->Form->data = array('Contact' => array('created' => null));
2013 $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
2014 $this->assertPattern('/for\="ContactCreatedHour"/', $result);
2015
2016 $this->Form->data = array('Contact' => array('created' => null));
2017 $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
2018 $this->assertPattern('/for\="ContactCreatedMonth"/', $result);
2019
2020 $result = $this->Form->input('Contact.created', array(
2021 'type' => 'date',
2022 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
2023 'timeFormat' => 'NONE'
2024 ));
2025 $this->assertPattern('/for\="created-month"/', $result);
2026 }
2027
2028 /**
2029 * Test generating checkboxes in a loop.
2030 *
2031 * @return void
2032 */
2033 function testInputCheckboxesInLoop() {
2034 for ($i = 1; $i < 5; $i++) {
2035 $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
2036 $expected = array(
2037 'div' => array('class' => 'input checkbox'),
2038 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
2039 array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
2040 'label' => array('for' => "Contact{$i}Email"),
2041 'Email',
2042 '/label',
2043 '/div'
2044 );
2045 $this->assertTags($result, $expected);
2046 }
2047 }
2048
2049 /**
2050 * test input name with leading integer, ensure attributes are generated correctly.
2051 *
2052 * @return void
2053 */
2054 function testInputWithLeadingInteger() {
2055 $result = $this->Form->text('0.Node.title');
2056 $expected = array(
2057 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
2058 );
2059 $this->assertTags($result, $expected);
2060 }
2061
2062 /**
2063 * test form->input() with select type inputs.
2064 *
2065 * @return void
2066 */
2067 function testInputSelectType() {
2068 $result = $this->Form->input('email', array(
2069 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
2070 );
2071 $expected = array(
2072 'div' => array('class' => 'input select'),
2073 'label' => array('for' => 'email'),
2074 'Email',
2075 '/label',
2076 array('select' => array('name' => 'data[email]', 'id' => 'email')),
2077 array('option' => array('value' => '')),
2078 '/option',
2079 array('option' => array('value' => 'è')),
2080 'Firést',
2081 '/option',
2082 array('option' => array('value' => 'é')),
2083 'Secoènd',
2084 '/option',
2085 '/select',
2086 '/div'
2087 );
2088 $this->assertTags($result, $expected);
2089
2090 $result = $this->Form->input('email', array(
2091 'options' => array('First', 'Second'), 'empty' => true)
2092 );
2093 $expected = array(
2094 'div' => array('class' => 'input select'),
2095 'label' => array('for' => 'email'),
2096 'Email',
2097 '/label',
2098 array('select' => array('name' => 'data[email]', 'id' => 'email')),
2099 array('option' => array('value' => '')),
2100 '/option',
2101 array('option' => array('value' => '0')),
2102 'First',
2103 '/option',
2104 array('option' => array('value' => '1')),
2105 'Second',
2106 '/option',
2107 '/select',
2108 '/div'
2109 );
2110 $this->assertTags($result, $expected);
2111
2112 $this->Form->data = array('Model' => array('user_id' => 'value'));
2113 $view =& ClassRegistry::getObject('view');
2114 $view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2115 $result = $this->Form->input('Model.user_id', array('empty' => true));
2116 $expected = array(
2117 'div' => array('class' => 'input select'),
2118 'label' => array('for' => 'ModelUserId'),
2119 'User',
2120 '/label',
2121 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
2122 array('option' => array('value' => '')),
2123 '/option',
2124 array('option' => array('value' => 'value', 'selected' => 'selected')),
2125 'good',
2126 '/option',
2127 array('option' => array('value' => 'other')),
2128 'bad',
2129 '/option',
2130 '/select',
2131 '/div'
2132 );
2133 $this->assertTags($result, $expected);
2134
2135 $this->Form->data = array('Model' => array('user_id' => null));
2136 $view =& ClassRegistry::getObject('view');
2137 $view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2138 $result = $this->Form->input('Model.user_id', array('empty' => 'Some Empty'));
2139 $expected = array(
2140 'div' => array('class' => 'input select'),
2141 'label' => array('for' => 'ModelUserId'),
2142 'User',
2143 '/label',
2144 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
2145 array('option' => array('value' => '')),
2146 'Some Empty',
2147 '/option',
2148 array('option' => array('value' => 'value')),
2149 'good',
2150 '/option',
2151 array('option' => array('value' => 'other')),
2152 'bad',
2153 '/option',
2154 '/select',
2155 '/div'
2156 );
2157 $this->assertTags($result, $expected);
2158
2159 $this->Form->data = array('Model' => array('user_id' => 'value'));
2160 $view =& ClassRegistry::getObject('view');
2161 $view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2162 $result = $this->Form->input('Model.user_id', array('empty' => 'Some Empty'));
2163 $expected = array(
2164 'div' => array('class' => 'input select'),
2165 'label' => array('for' => 'ModelUserId'),
2166 'User',
2167 '/label',
2168 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
2169 array('option' => array('value' => '')),
2170 'Some Empty',
2171 '/option',
2172 array('option' => array('value' => 'value', 'selected' => 'selected')),
2173 'good',
2174 '/option',
2175 array('option' => array('value' => 'other')),
2176 'bad',
2177 '/option',
2178 '/select',
2179 '/div'
2180 );
2181 $this->assertTags($result, $expected);
2182
2183 $this->Form->data = array('User' => array('User' => array('value')));
2184 $view =& ClassRegistry::getObject('view');
2185 $view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2186 $result = $this->Form->input('User.User', array('empty' => true));
2187 $expected = array(
2188 'div' => array('class' => 'input select'),
2189 'label' => array('for' => 'UserUser'),
2190 'User',
2191 '/label',
2192 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
2193 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
2194 array('option' => array('value' => '')),
2195 '/option',
2196 array('option' => array('value' => 'value', 'selected' => 'selected')),
2197 'good',
2198 '/option',
2199 array('option' => array('value' => 'other')),
2200 'bad',
2201 '/option',
2202 '/select',
2203 '/div'
2204 );
2205 $this->assertTags($result, $expected);
2206
2207 $this->Form->data = array();
2208 $result = $this->Form->input('Publisher.id', array(
2209 'label' => 'Publisher',
2210 'type' => 'select',
2211 'multiple' => 'checkbox',
2212 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
2213 ));
2214 $expected = array(
2215 array('div' => array('class' => 'input select')),
2216 array('label' => array('for' => 'PublisherId')),
2217 'Publisher',
2218 '/label',
2219 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
2220 array('div' => array('class' => 'checkbox')),
2221 array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
2222 array('label' => array('for' => 'PublisherIdValue1')),
2223 'Label 1',
2224 '/label',
2225 '/div',
2226 array('div' => array('class' => 'checkbox')),
2227 array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
2228 array('label' => array('for' => 'PublisherIdValue2')),
2229 'Label 2',
2230 '/label',
2231 '/div',
2232 '/div'
2233 );
2234 $this->assertTags($result, $expected);
2235 }
2236
2237 /**
2238 * test that input() and a non standard primary key makes a hidden input by default.
2239 *
2240 * @return void
2241 */
2242 function testInputWithNonStandardPrimaryKeyMakesHidden() {
2243 $this->Form->create('User');
2244 $this->Form->fieldset = array(
2245 'User' => array(
2246 'fields' => array(
2247 'model_id' => array('type' => 'integer')
2248 ),
2249 'validates' => array(),
2250 'key' => 'model_id'
2251 )
2252 );
2253 $result = $this->Form->input('model_id');
2254 $expected = array(
2255 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
2256 );
2257 $this->assertTags($result, $expected);
2258 }
2259
2260 /**
2261 * test that overriding the magic select type widget is possible
2262 *
2263 * @return void
2264 */
2265 function testInputOverridingMagicSelectType() {
2266 $view =& ClassRegistry::getObject('view');
2267 $view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2268 $result = $this->Form->input('Model.user_id', array('type' => 'text'));
2269 $expected = array(
2270 'div' => array('class' => 'input text'),
2271 'label' => array('for' => 'ModelUserId'), 'User', '/label',
2272 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
2273 '/div'
2274 );
2275 $this->assertTags($result, $expected);
2276
2277 //Check that magic types still work for plural/singular vars
2278 $view =& ClassRegistry::getObject('view');
2279 $view->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
2280 $result = $this->Form->input('Model.type');
2281 $expected = array(
2282 'div' => array('class' => 'input select'),
2283 'label' => array('for' => 'ModelType'), 'Type', '/label',
2284 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
2285 array('option' => array('value' => 'value')), 'good', '/option',
2286 array('option' => array('value' => 'other')), 'bad', '/option',
2287 '/select',
2288 '/div'
2289 );
2290 $this->assertTags($result, $expected);
2291 }
2292
2293 /**
2294 * Test that magic input() selects can easily be converted into radio types without error.
2295 *
2296 * @return void
2297 */
2298 function testInputMagicSelectChangeToRadio() {
2299 $view =& ClassRegistry::getObject('view');
2300 $view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
2301 $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
2302 $this->assertPattern('/input type="radio"/', $result);
2303 }
2304
2305 /**
2306 * fields with the same name as the model should work.
2307 *
2308 * @return void
2309 */
2310 function testInputWithMatchingFieldAndModelName() {
2311 $this->Form->create('User');
2312 $this->Form->fieldset = array(
2313 'User' => array(
2314 'fields' => array(
2315 'User' => array('type' => 'text')
2316 ),
2317 'validates' => array(),
2318 'key' => 'id'
2319 )
2320 );
2321 $this->Form->data['User']['User'] = 'ABC, Inc.';
2322 $result = $this->Form->input('User', array('type' => 'text'));
2323 $expected = array(
2324 'div' => array('class' => 'input text'),
2325 'label' => array('for' => 'UserUser'), 'User', '/label',
2326 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
2327 '/div'
2328 );
2329 $this->assertTags($result, $expected);
2330 }
2331
2332 /**
2333 * testFormInputs method
2334 *
2335 * test correct results from form::inputs().
2336 *
2337 * @access public
2338 * @return void
2339 */
2340 function testFormInputs() {
2341 $this->Form->create('Contact');
2342 $result = $this->Form->inputs('The Legend');
2343 $expected = array(
2344 '<fieldset',
2345 '<legend',
2346 'The Legend',
2347 '/legend',
2348 '*/fieldset',
2349 );
2350 $this->assertTags($result, $expected);
2351
2352 $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
2353 $expected = array(
2354 'fieldset' => array('class' => 'classy-stuff'),
2355 '<legend',
2356 'Field of Dreams',
2357 '/legend',
2358 '*/fieldset'
2359 );
2360 $this->assertTags($result, $expected);
2361
2362 $View = ClassRegistry::getObject('view');
2363 $this->Form->create('Contact');
2364 $this->Form->params['prefix'] = 'admin';
2365 $this->Form->action = 'admin_edit';
2366 $result = $this->Form->inputs();
2367 $expected = array(
2368 '<fieldset',
2369 '<legend',
2370 'Edit Contact',
2371 '/legend',
2372 '*/fieldset',
2373 );
2374 $this->assertTags($result, $expected);
2375
2376 $this->Form->create('Contact');
2377 $result = $this->Form->inputs(false);
2378 $expected = array(
2379 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
2380 array('div' => array('class' => 'input text')),
2381 '*/div',
2382 array('div' => array('class' => 'input text')),
2383 '*/div',
2384 array('div' => array('class' => 'input text')),
2385 '*/div',
2386 array('div' => array('class' => 'input password')),
2387 '*/div',
2388 array('div' => array('class' => 'input date')),
2389 '*/div',
2390 array('div' => array('class' => 'input date')),
2391 '*/div',
2392 array('div' => array('class' => 'input datetime')),
2393 '*/div',
2394 array('div' => array('class' => 'input select')),
2395 '*/div',
2396 );
2397 $this->assertTags($result, $expected);
2398
2399 $this->Form->create('Contact');
2400 $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
2401 $expected = array(
2402 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
2403 array('div' => array('class' => 'input text')),
2404 '*/div',
2405 array('div' => array('class' => 'input text')),
2406 '*/div',
2407 array('div' => array('class' => 'input text')),
2408 '*/div',
2409 array('div' => array('class' => 'input password')),
2410 '*/div',
2411 array('div' => array('class' => 'input date')),
2412 '*/div',
2413 array('div' => array('class' => 'input date')),
2414 '*/div',
2415 array('div' => array('class' => 'input datetime')),
2416 '*/div',
2417 array('div' => array('class' => 'input select')),
2418 '*/div',
2419 );
2420 $this->assertTags($result, $expected);
2421
2422 $this->Form->create('Contact');
2423 $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
2424 $expected = array(
2425 'fieldset' => array(),
2426 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
2427 array('div' => array('class' => 'input text')),
2428 '*/div',
2429 array('div' => array('class' => 'input text')),
2430 '*/div',
2431 array('div' => array('class' => 'input text')),
2432 '*/div',
2433 array('div' => array('class' => 'input password')),
2434 '*/div',
2435 array('div' => array('class' => 'input date')),
2436 '*/div',
2437 array('div' => array('class' => 'input date')),
2438 '*/div',
2439 array('div' => array('class' => 'input datetime')),
2440 '*/div',
2441 array('div' => array('class' => 'input select')),
2442 '*/div',
2443 '/fieldset'
2444 );
2445 $this->assertTags($result, $expected);
2446
2447 $this->Form->create('Contact');
2448 $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
2449 $expected = array(
2450 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
2451 array('div' => array('class' => 'input text')),
2452 '*/div',
2453 array('div' => array('class' => 'input text')),
2454 '*/div',
2455 array('div' => array('class' => 'input text')),
2456 '*/div',
2457 array('div' => array('class' => 'input password')),
2458 '*/div',
2459 array('div' => array('class' => 'input date')),
2460 '*/div',
2461 array('div' => array('class' => 'input date')),
2462 '*/div',
2463 array('div' => array('class' => 'input datetime')),
2464 '*/div',
2465 array('div' => array('class' => 'input select')),
2466 '*/div',
2467 );
2468 $this->assertTags($result, $expected);
2469
2470 $this->Form->create('Contact');
2471 $result = $this->Form->inputs('Hello');
2472 $expected = array(
2473 'fieldset' => array(),
2474 'legend' => array(),
2475 'Hello',
2476 '/legend',
2477 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
2478 array('div' => array('class' => 'input text')),
2479 '*/div',
2480 array('div' => array('class' => 'input text')),
2481 '*/div',
2482 array('div' => array('class' => 'input text')),
2483 '*/div',
2484 array('div' => array('class' => 'input password')),
2485 '*/div',
2486 array('div' => array('class' => 'input date')),
2487 '*/div',
2488 array('div' => array('class' => 'input date')),
2489 '*/div',
2490 array('div' => array('class' => 'input datetime')),
2491 '*/div',
2492 array('div' => array('class' => 'input select')),
2493 '*/div',
2494 '/fieldset'
2495 );
2496 $this->assertTags($result, $expected);
2497
2498 $this->Form->create('Contact');
2499 $result = $this->Form->inputs(array('legend' => 'Hello'));
2500 $expected = array(
2501 'fieldset' => array(),
2502 'legend' => array(),
2503 'Hello',
2504 '/legend',
2505 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
2506 array('div' => array('class' => 'input text')),
2507 '*/div',
2508 array('div' => array('class' => 'input text')),
2509 '*/div',
2510 array('div' => array('class' => 'input text')),
2511 '*/div',
2512 array('div' => array('class' => 'input password')),
2513 '*/div',
2514 array('div' => array('class' => 'input date')),
2515 '*/div',
2516 array('div' => array('class' => 'input date')),
2517 '*/div',
2518 array('div' => array('class' => 'input datetime')),
2519 '*/div',
2520 array('div' => array('class' => 'input select')),
2521 '*/div',
2522 '/fieldset'
2523 );
2524 $this->assertTags($result, $expected);
2525 }
2526
2527 /**
2528 * testSelectAsCheckbox method
2529 *
2530 * test multi-select widget with checkbox formatting.
2531 *
2532 * @access public
2533 * @return void
2534 */
2535 function testSelectAsCheckbox() {
2536 $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array(0, 1), array('multiple' => 'checkbox'));
2537 $expected = array(
2538 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
2539 array('div' => array('class' => 'checkbox')),
2540 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
2541 array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
2542 'first',
2543 '/label',
2544 '/div',
2545 array('div' => array('class' => 'checkbox')),
2546 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
2547 array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
2548 'second',
2549 '/label',
2550 '/div',
2551 array('div' => array('class' => 'checkbox')),
2552 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
2553 array('label' => array('for' => 'ModelMultiField2')),
2554 'third',
2555 '/label',
2556 '/div',
2557 );
2558 $this->assertTags($result, $expected);
2559
2560 $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), null, array('multiple' => 'checkbox'));
2561 $expected = array(
2562 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
2563 array('div' => array('class' => 'checkbox')),
2564 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
2565 array('label' => array('for' => 'ModelMultiField12')),
2566 'half',
2567 '/label',
2568 '/div',
2569 );
2570 $this->assertTags($result, $expected);
2571 }
2572
2573 /**
2574 * testLabel method
2575 *
2576 * test label generation.
2577 *
2578 * @access public
2579 * @return void
2580 */
2581 function testLabel() {
2582 $this->Form->text('Person.name');
2583 $result = $this->Form->label();
2584 $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
2585
2586 $this->Form->text('Person.name');
2587 $result = $this->Form->label();
2588 $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
2589
2590 $result = $this->Form->label('Person.first_name');
2591 $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
2592
2593 $result = $this->Form->label('Person.first_name', 'Your first name');
2594 $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
2595
2596 $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
2597 $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
2598
2599 $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
2600 $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
2601
2602 $result = $this->Form->label('Person.first_name', '');
2603 $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
2604
2605 $result = $this->Form->label('Person.2.name', '');
2606 $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
2607 }
2608
2609 /**
2610 * testTextbox method
2611 *
2612 * test textbox element generation
2613 *
2614 * @access public
2615 * @return void
2616 */
2617 function testTextbox() {
2618 $result = $this->Form->text('Model.field');
2619 $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
2620
2621 $result = $this->Form->text('Model.field', array('type' => 'password'));
2622 $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
2623
2624 $result = $this->Form->text('Model.field', array('id' => 'theID'));
2625 $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
2626
2627 $this->Form->data['Model']['text'] = 'test <strong>HTML</strong> values';
2628 $result = $this->Form->text('Model.text');
2629 $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
2630
2631 $this->Form->validationErrors['Model']['text'] = 1;
2632 $this->Form->data['Model']['text'] = 'test';
2633 $result = $this->Form->text('Model.text', array('id' => 'theID'));
2634 $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
2635
2636 $this->Form->data['Model']['0']['OtherModel']['field'] = 'My value';
2637 $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
2638 $expected = array(
2639 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
2640 );
2641 $this->assertTags($result, $expected);
2642 }
2643
2644 /**
2645 * testDefaultValue method
2646 *
2647 * Test default value setting
2648 *
2649 * @access public
2650 * @return void
2651 */
2652 function testDefaultValue() {
2653 $this->Form->data['Model']['field'] = 'test';
2654 $result = $this->Form->text('Model.field', array('default' => 'default value'));
2655 $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
2656
2657 unset($this->Form->data['Model']['field']);
2658 $result = $this->Form->text('Model.field', array('default' => 'default value'));
2659 $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
2660 }
2661
2662 /**
2663 * testError method
2664 *
2665 * Test field error generation
2666 *
2667 * @access public
2668 * @return void
2669 */
2670 function testError() {
2671 $this->Form->validationErrors['Model']['field'] = 1;
2672 $result = $this->Form->error('Model.field');
2673 $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
2674
2675 $result = $this->Form->error('Model.field', null, array('wrap' => false));
2676 $this->assertEqual($result, 'Error in field Field');
2677
2678 $this->Form->validationErrors['Model']['field'] = "This field contains invalid input";
2679 $result = $this->Form->error('Model.field', null, array('wrap' => false));
2680 $this->assertEqual($result, 'This field contains invalid input');
2681
2682 $this->Form->validationErrors['Model']['field'] = "This field contains invalid input";
2683 $result = $this->Form->error('Model.field', null, array('wrap' => 'span'));
2684 $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
2685
2686 $result = $this->Form->error('Model.field', 'There is an error fool!', array('wrap' => 'span'));
2687 $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
2688
2689 $result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false));
2690 $this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
2691
2692 $result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
2693 $this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
2694
2695 $result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
2696 $this->assertEqual($result, '<strong>Badness!</strong>');
2697
2698 $this->Form->validationErrors['Model']['field'] = "email";
2699 $result = $this->Form->error('Model.field', array('class' => 'field-error', 'email' => 'No good!'));
2700 $expected = array(
2701 'div' => array('class' => 'field-error'),
2702 'No good!',
2703 '/div'
2704 );
2705 $this->assertTags($result, $expected);
2706 }
2707
2708 /**
2709 * test error options when using form->input();
2710 *
2711 * @return void
2712 */
2713 function testInputErrorEscape() {
2714 $this->Form->create('ValidateProfile');
2715 $this->Form->validationErrors['ValidateProfile']['city'] = 'required<br>';
2716 $result = $this->Form->input('city',array('error' => array('escape' => true)));
2717 $this->assertPattern('/required&lt;br&gt;/', $result);
2718
2719 $result = $this->Form->input('city',array('error' => array('escape' => false)));
2720 $this->assertPattern('/required<br>/', $result);
2721 }
2722
2723 /**
2724 * testPassword method
2725 *
2726 * Test password element generation
2727 *
2728 * @access public
2729 * @return void
2730 */
2731 function testPassword() {
2732 $result = $this->Form->password('Model.field');
2733 $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
2734
2735 $this->Form->validationErrors['Model']['passwd'] = 1;
2736 $this->Form->data['Model']['passwd'] = 'test';
2737 $result = $this->Form->password('Model.passwd', array('id' => 'theID'));
2738 $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
2739 }
2740
2741 /**
2742 * testRadio method
2743 *
2744 * Test radio element set generation
2745 *
2746 * @access public
2747 * @return void
2748 */
2749 function testRadio() {
2750 $result = $this->Form->radio('Model.field', array('option A'));
2751 $expected = array(
2752 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
2753 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
2754 'label' => array('for' => 'ModelField0'),
2755 'option A',
2756 '/label'
2757 );
2758 $this->assertTags($result, $expected);
2759
2760 $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
2761 $expected = array(
2762 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
2763 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
2764 'label' => array('for' => 'ModelField12'),
2765 'half',
2766 '/label'
2767 );
2768 $this->assertTags($result, $expected);
2769
2770 $result = $this->Form->radio('Model.field', array('option A', 'option B'));
2771 $expected = array(
2772 'fieldset' => array(),
2773 'legend' => array(),
2774 'Field',
2775 '/legend',
2776 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
2777 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
2778 array('label' => array('for' => 'ModelField0')),
2779 'option A',
2780 '/label',
2781 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
2782 array('label' => array('for' => 'ModelField1')),
2783 'option B',
2784 '/label',
2785 '/fieldset'
2786 );
2787 $this->assertTags($result, $expected);
2788
2789 $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
2790 $expected = array(
2791 'fieldset' => array(),
2792 'legend' => array(),
2793 'Field',
2794 '/legend',
2795 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
2796 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
2797 array('label' => array('for' => 'ModelField0')),
2798 'option A',
2799 '/label',
2800 'br' => array(),
2801 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
2802 array('label' => array('for' => 'ModelField1')),
2803 'option B',
2804 '/label',
2805 '/fieldset'
2806 );
2807 $this->assertTags($result, $expected);
2808
2809 $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
2810 $expected = array(
2811 'fieldset' => array(),
2812 'legend' => array(),
2813 'Field',
2814 '/legend',
2815 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
2816 array('label' => array('for' => 'ModelField1')),
2817 'Yes',
2818 '/label',
2819 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
2820 array('label' => array('for' => 'ModelField0')),
2821 'No',
2822 '/label',
2823 '/fieldset'
2824 );
2825 $this->assertTags($result, $expected);
2826
2827 $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
2828 $expected = array(
2829 'fieldset' => array(),
2830 'legend' => array(),
2831 'Field',
2832 '/legend',
2833 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
2834 array('label' => array('for' => 'ModelField1')),
2835 'Yes',
2836 '/label',
2837 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
2838 array('label' => array('for' => 'ModelField0')),
2839 'No',
2840 '/label',
2841 '/fieldset'
2842 );
2843 $this->assertTags($result, $expected);
2844
2845 $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
2846 $expected = array(
2847 'fieldset' => array(),
2848 'legend' => array(),
2849 'Field',
2850 '/legend',
2851 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
2852 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
2853 array('label' => array('for' => 'ModelField1')),
2854 'Yes',
2855 '/label',
2856 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
2857 array('label' => array('for' => 'ModelField0')),
2858 'No',
2859 '/label',
2860 '/fieldset'
2861 );
2862 $this->assertTags($result, $expected);
2863
2864 $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
2865 $expected = array(
2866 'fieldset' => array(),
2867 'legend' => array(),
2868 'Field',
2869 '/legend',
2870 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
2871 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
2872 array('label' => array('for' => 'ModelField1')),
2873 'Yes',
2874 '/label',
2875 array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
2876 array('label' => array('for' => 'ModelField0')),
2877 'No',
2878 '/label',
2879 '/fieldset'
2880 );
2881 $this->assertTags($result, $expected);
2882
2883 $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
2884 $expected = array(
2885 'div' => array('class' => 'input radio'),
2886 'fieldset' => array(),
2887 'legend' => array(),
2888 'Legend title',
2889 '/legend',
2890 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
2891 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
2892 array('label' => array('for' => 'NewsletterSubscribe0')),
2893 'Unsubscribe',
2894 '/label',
2895 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
2896 array('label' => array('for' => 'NewsletterSubscribe1')),
2897 'Subscribe',
2898 '/label',
2899 '/fieldset',
2900 '/div'
2901 );
2902 $this->assertTags($result, $expected);
2903
2904 $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
2905 $expected = array(
2906 'div' => array('class' => 'input radio'),
2907 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
2908 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
2909 array('label' => array('for' => 'NewsletterSubscribe0')),
2910 'Unsubscribe',
2911 '/label',
2912 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
2913 array('label' => array('for' => 'NewsletterSubscribe1')),
2914 'Subscribe',
2915 '/label',
2916 '/div'
2917 );
2918 $this->assertTags($result, $expected);
2919
2920 $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
2921 $expected = array(
2922 'div' => array('class' => 'input radio'),
2923 'fieldset' => array(),
2924 'legend' => array(),
2925 'Legend title',
2926 '/legend',
2927 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
2928 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
2929 'Unsubscribe',
2930 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
2931 'Subscribe',
2932 '/fieldset',
2933 '/div'
2934 );
2935 $this->assertTags($result, $expected);
2936
2937 $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
2938 $expected = array(
2939 'div' => array('class' => 'input radio'),
2940 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
2941 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
2942 'Unsubscribe',
2943 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
2944 'Subscribe',
2945 '/div'
2946 );
2947 $this->assertTags($result, $expected);
2948
2949 $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
2950 $expected = array(
2951 'div' => array('class' => 'input radio'),
2952 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
2953 'Unsubscribe',
2954 array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
2955 'Subscribe',
2956 '/div'
2957 );
2958 $this->assertTags($result, $expected);
2959
2960 $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
2961 $expected = array(
2962 'fieldset' => array(),
2963 'legend' => array(),
2964 'Gender',
2965 '/legend',
2966 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
2967 array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
2968 array('label' => array('for' => 'EmployeeGenderMale')),
2969 'Male',
2970 '/label',
2971 array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
2972 array('label' => array('for' => 'EmployeeGenderFemale')),
2973 'Female',
2974 '/label',
2975 '/fieldset',
2976 );
2977 $this->assertTags($result, $expected);
2978
2979 $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
2980 $expected = array(
2981 'fieldset' => array(),
2982 'legend' => array(),
2983 'Gender',
2984 '/legend',
2985 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
2986 array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
2987 array('label' => array('for' => 'OfficerGenderMale')),
2988 'Male',
2989 '/label',
2990 array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
2991 array('label' => array('for' => 'OfficerGenderFemale')),
2992 'Female',
2993 '/label',
2994 '/fieldset',
2995 );
2996 $this->assertTags($result, $expected);
2997
2998 $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
2999 $expected = array(
3000 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
3001 array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
3002 'label' => array('for' => 'Contact1Imrequired0'),
3003 'option A',
3004 '/label'
3005 );
3006 $this->assertTags($result, $expected);
3007
3008 $result = $this->Form->radio('Model.1.field', array('option A'));
3009 $expected = array(
3010 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
3011 array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
3012 'label' => array('for' => 'Model1Field0'),
3013 'option A',
3014 '/label'
3015 );
3016 $this->assertTags($result, $expected);
3017
3018
3019 $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
3020 $expected = array(
3021 'fieldset' => array(),
3022 'legend' => array(),
3023 'Field',
3024 '/legend',
3025 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
3026 array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
3027 array('label' => array('for' => 'ModelField0')),
3028 'option A',
3029 '/label',
3030 array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
3031 array('label' => array('for' => 'ModelField1')),
3032 'option B',
3033 '/label',
3034 '/fieldset'
3035 );
3036 $this->assertTags($result, $expected);
3037 }
3038
3039 /**
3040 * test disabling the hidden input for radio buttons
3041 *
3042 * @return void
3043 */
3044 function testRadioHiddenInputDisabling() {
3045 $result = $this->Form->input('Model.1.field', array(
3046 'type' => 'radio',
3047 'options' => array('option A'),
3048 'hiddenField' => false
3049 )
3050 );
3051 $expected = array(
3052 'div' => array('class' => 'input radio'),
3053 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
3054 'label' => array('for' => 'Model1Field0'),
3055 'option A',
3056 '/label',
3057 '/div'
3058 );
3059 $this->assertTags($result, $expected);
3060
3061 $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
3062 $expected = array(
3063 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
3064 'label' => array('for' => 'Model1Field0'),
3065 'option A',
3066 '/label'
3067 );
3068 $this->assertTags($result, $expected);
3069 }
3070
3071 /**
3072 * testSelect method
3073 *
3074 * Test select element generation.
3075 *
3076 * @access public
3077 * @return void
3078 */
3079 function testSelect() {
3080 $result = $this->Form->select('Model.field', array());
3081 $expected = array(
3082 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3083 array('option' => array('value' => '')),
3084 '/option',
3085 '/select'
3086 );
3087 $this->assertTags($result, $expected);
3088
3089 $this->Form->data = array('Model' => array('field' => 'value'));
3090 $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
3091 $expected = array(
3092 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3093 array('option' => array('value' => '')),
3094 '/option',
3095 array('option' => array('value' => 'value', 'selected' => 'selected')),
3096 'good',
3097 '/option',
3098 array('option' => array('value' => 'other')),
3099 'bad',
3100 '/option',
3101 '/select'
3102 );
3103 $this->assertTags($result, $expected);
3104
3105 $this->Form->data = array();
3106 $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
3107 $expected = array(
3108 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3109 array('option' => array('value' => '')),
3110 '/option',
3111 array('option' => array('value' => 'value')),
3112 'good',
3113 '/option',
3114 array('option' => array('value' => 'other')),
3115 'bad',
3116 '/option',
3117 '/select'
3118 );
3119 $this->assertTags($result, $expected);
3120
3121 $result = $this->Form->select(
3122 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
3123 null, array('empty' => false)
3124 );
3125 $expected = array(
3126 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3127 array('option' => array('value' => 'first')),
3128 'first &quot;html&quot; &lt;chars&gt;',
3129 '/option',
3130 array('option' => array('value' => 'second')),
3131 'value',
3132 '/option',
3133 '/select'
3134 );
3135 $this->assertTags($result, $expected);
3136
3137 $result = $this->Form->select(
3138 'Model.field',
3139 array('first' => 'first "html" <chars>', 'second' => 'value'),
3140 null, array('escape' => false, 'empty' => false)
3141 );
3142 $expected = array(
3143 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3144 array('option' => array('value' => 'first')),
3145 'first "html" <chars>',
3146 '/option',
3147 array('option' => array('value' => 'second')),
3148 'value',
3149 '/option',
3150 '/select'
3151 );
3152 $this->assertTags($result, $expected);
3153
3154 $this->Form->data = array('Model' => array('contact_id' => 228));
3155 $result = $this->Form->select(
3156 'Model.contact_id',
3157 array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
3158 null, array('escape' => false, 'empty' => 'pick something')
3159 );
3160
3161 $expected = array(
3162 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
3163 array('option' => array('value' => '')), 'pick something', '/option',
3164 array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
3165 array('option' => array('value' => '228-1')), '228-1 value', '/option',
3166 array('option' => array('value' => '228-2')), '228-2 value', '/option',
3167 '/select'
3168 );
3169 $this->assertTags($result, $expected);
3170 }
3171
3172 /**
3173 * test that select() with optiongroups listens to the escape param.
3174 *
3175 * @return void
3176 */
3177 function testSelectOptionGroupEscaping() {
3178 $options = array(
3179 '>< Key' => array(
3180 1 => 'One',
3181 2 => 'Two'
3182 )
3183 );
3184 $result = $this->Form->select('Model.field', $options, null, array('empty' => false));
3185 $expected = array(
3186 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3187 'optgroup' => array('label' => '&gt;&lt; Key'),
3188 array('option' => array('value' => '1')), 'One', '/option',
3189 array('option' => array('value' => '2')), 'Two', '/option',
3190 '/optgroup',
3191 '/select'
3192 );
3193 $this->assertTags($result, $expected);
3194
3195 $options = array(
3196 '>< Key' => array(
3197 1 => 'One',
3198 2 => 'Two'
3199 )
3200 );
3201 $result = $this->Form->select('Model.field', $options, null, array('empty' => false, 'escape' => false));
3202 $expected = array(
3203 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3204 'optgroup' => array('label' => '>< Key'),
3205 array('option' => array('value' => '1')), 'One', '/option',
3206 array('option' => array('value' => '2')), 'Two', '/option',
3207 '/optgroup',
3208 '/select'
3209 );
3210 $this->assertTags($result, $expected);
3211 }
3212
3213 /**
3214 * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
3215 *
3216 * @access public
3217 * @return void
3218 */
3219 function testSelectWithNullAttributes() {
3220 $result = $this->Form->select('Model.field', array('first', 'second'), null, array('empty' => false));
3221 $expected = array(
3222 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3223 array('option' => array('value' => '0')),
3224 'first',
3225 '/option',
3226 array('option' => array('value' => '1')),
3227 'second',
3228 '/option',
3229 '/select'
3230 );
3231 $this->assertTags($result, $expected);
3232 }
3233
3234 /**
3235 * testNestedSelect method
3236 *
3237 * test select element generation with optgroups
3238 *
3239 * @access public
3240 * @return void
3241 */
3242 function testNestedSelect() {
3243 $result = $this->Form->select(
3244 'Model.field',
3245 array(1 => 'One', 2 => 'Two', 'Three' => array(
3246 3 => 'Three', 4 => 'Four', 5 => 'Five'
3247 )), null, array('empty' => false)
3248 );
3249 $expected = array(
3250 'select' => array('name' => 'data[Model][field]',
3251 'id' => 'ModelField'),
3252 array('option' => array('value' => 1)),
3253 'One',
3254 '/option',
3255 array('option' => array('value' => 2)),
3256 'Two',
3257 '/option',
3258 array('optgroup' => array('label' => 'Three')),
3259 array('option' => array('value' => 4)),
3260 'Four',
3261 '/option',
3262 array('option' => array('value' => 5)),
3263 'Five',
3264 '/option',
3265 '/optgroup',
3266 '/select'
3267 );
3268 $this->assertTags($result, $expected);
3269
3270 $result = $this->Form->select(
3271 'Model.field',
3272 array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')), null,
3273 array('showParents' => true, 'empty' => false)
3274 );
3275
3276 $expected = array(
3277 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
3278 array('option' => array('value' => 1)),
3279 'One',
3280 '/option',
3281 array('option' => array('value' => 2)),
3282 'Two',
3283 '/option',
3284 array('optgroup' => array('label' => 'Three')),
3285 array('option' => array('value' => 3)),
3286 'Three',
3287 '/option',
3288 array('option' => array('value' => 4)),
3289 'Four',
3290 '/option',
3291 '/optgroup',
3292 '/select'
3293 );
3294 $this->assertTags($result, $expected);
3295 }
3296
3297 /**
3298 * testSelectMultiple method
3299 *
3300 * test generation of multiple select elements
3301 *
3302 * @access public
3303 * @return void
3304 */
3305 function testSelectMultiple() {
3306 $options = array('first', 'second', 'third');
3307 $result = $this->Form->select(
3308 'Model.multi_field', $options, null, array('multiple' => true)
3309 );
3310 $expected = array(
3311 'input' => array(
3312 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
3313 ),
3314 'select' => array(
3315 'name' => 'data[Model][multi_field][]',
3316 'id' => 'ModelMultiField', 'multiple' => 'multiple'
3317 ),
3318 array('option' => array('value' => '0')),
3319 'first',
3320 '/option',
3321 array('option' => array('value' => '1')),
3322 'second',
3323 '/option',
3324 array('option' => array('value' => '2')),
3325 'third',
3326 '/option',
3327 '/select'
3328 );
3329 $this->assertTags($result, $expected);
3330
3331 $result = $this->Form->select(
3332 'Model.multi_field', $options, null, array('multiple' => 'multiple')
3333 );
3334 $expected = array(
3335 'input' => array(
3336 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
3337 ),
3338 'select' => array(
3339 'name' => 'data[Model][multi_field][]',
3340 'id' => 'ModelMultiField', 'multiple' => 'multiple'
3341 ),
3342 array('option' => array('value' => '0')),
3343 'first',
3344 '/option',
3345 array('option' => array('value' => '1')),
3346 'second',
3347 '/option',
3348 array('option' => array('value' => '2')),
3349 'third',
3350 '/option',
3351 '/select'
3352 );
3353 $this->assertTags($result, $expected);
3354
3355 $result = $this->Form->select(
3356 'Model.multi_field', $options, array(0, 1), array('multiple' => true)
3357 );
3358 $expected = array(
3359 'input' => array(
3360 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
3361 ),
3362 'select' => array(
3363 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
3364 'multiple' => 'multiple'
3365 ),
3366 array('option' => array('value' => '0', 'selected' => 'selected')),
3367 'first',
3368 '/option',
3369 array('option' => array('value' => '1', 'selected' => 'selected')),
3370 'second',
3371 '/option',
3372 array('option' => array('value' => '2')),
3373 'third',
3374 '/option',
3375 '/select'
3376 );
3377 $this->assertTags($result, $expected);
3378
3379 $result = $this->Form->select(
3380 'Model.multi_field', $options, array(0, 1), array('multiple' => false)
3381 );
3382 $expected = array(
3383 'select' => array(
3384 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
3385 ),
3386 array('option' => array('value' => '0', 'selected' => 'selected')),
3387 'first',
3388 '/option',
3389 array('option' => array('value' => '1', 'selected' => 'selected')),
3390 'second',
3391 '/option',
3392 array('option' => array('value' => '2')),
3393 'third',
3394 '/option',
3395 '/select'
3396 );
3397 $this->assertTags($result, $expected);
3398 }
3399
3400 /**
3401 * test generation of habtm select boxes.
3402 *
3403 * @return void
3404 */
3405 function testHabtmSelectBox() {
3406 $view =& ClassRegistry::getObject('view');
3407 $view->viewVars['contactTags'] = array(
3408 1 => 'blue',
3409 2 => 'red',
3410 3 => 'green'
3411 );
3412 $this->Form->data = array(
3413 'Contact' => array(),
3414 'ContactTag' => array(
3415 array(
3416 'id' => 1,
3417 'name' => 'blue'
3418 ),
3419 array(
3420 'id' => 3,
3421 'name' => 'green'
3422 )
3423 )
3424 );
3425 $this->Form->create('Contact');
3426 $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
3427 $expected = array(
3428 'input' => array(
3429 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
3430 ),
3431 'select' => array(
3432 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
3433 'multiple' => 'multiple'
3434 ),
3435 array('option' => array('value' => '1', 'selected' => 'selected')),
3436 'blue',
3437 '/option',
3438 array('option' => array('value' => '2')),
3439 'red',
3440 '/option',
3441 array('option' => array('value' => '3', 'selected' => 'selected')),
3442 'green',
3443 '/option',
3444 '/select'
3445 );
3446 $this->assertTags($result, $expected);
3447 }
3448
3449 /**
3450 * test generation of multi select elements in checkbox format
3451 *
3452 * @access public
3453 * @return void
3454 */
3455 function testSelectMultipleCheckboxes() {
3456 $result = $this->Form->select(
3457 'Model.multi_field',
3458 array('first', 'second', 'third'), null,
3459 array('multiple' => 'checkbox')
3460 );
3461
3462 $expected = array(
3463 'input' => array(
3464 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
3465 ),
3466 array('div' => array('class' => 'checkbox')),
3467 array('input' => array(
3468 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
3469 'value' => '0', 'id' => 'ModelMultiField0'
3470 )),
3471 array('label' => array('for' => 'ModelMultiField0')),
3472 'first',
3473 '/label',
3474 '/div',
3475 array('div' => array('class' => 'checkbox')),
3476 array('input' => array(
3477 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
3478 'value' => '1', 'id' => 'ModelMultiField1'
3479 )),
3480 array('label' => array('for' => 'ModelMultiField1')),
3481 'second',
3482 '/label',
3483 '/div',
3484 array('div' => array('class' => 'checkbox')),
3485 array('input' => array(
3486 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
3487 'value' => '2', 'id' => 'ModelMultiField2'
3488 )),
3489 array('label' => array('for' => 'ModelMultiField2')),
3490 'third',
3491 '/label',
3492 '/div'
3493 );
3494 $this->assertTags($result, $expected);
3495
3496 $result = $this->Form->select(
3497 'Model.multi_field',
3498 array('a' => 'first', 'b' => 'second', 'c' => 'third'), null,
3499 array('multiple' => 'checkbox')
3500 );
3501 $expected = array(
3502 'input' => array(
3503 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
3504 ),
3505 array('div' => array('class' => 'checkbox')),
3506 array('input' => array(
3507 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
3508 'value' => 'a', 'id' => 'ModelMultiFieldA'
3509 )),
3510 array('label' => array('for' => 'ModelMultiFieldA')),
3511 'first',
3512 '/label',
3513 '/div',
3514 array('div' => array('class' => 'checkbox')),
3515 array('input' => array(
3516 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
3517 'value' => 'b', 'id' => 'ModelMultiFieldB'
3518 )),
3519 array('label' => array('for' => 'ModelMultiFieldB')),
3520 'second',
3521 '/label',
3522 '/div',
3523 array('div' => array('class' => 'checkbox')),
3524 array('input' => array(
3525 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
3526 'value' => 'c', 'id' => 'ModelMultiFieldC'
3527 )),
3528 array('label' => array('for' => 'ModelMultiFieldC')),
3529 'third',
3530 '/label',
3531 '/div'
3532 );
3533 $this->assertTags($result, $expected);
3534
3535 $result = $this->Form->select(
3536 'Model.multi_field', array('1' => 'first'), null, array('multiple' => 'checkbox')
3537 );
3538 $expected = array(
3539 'input' => array(
3540 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
3541 ),
3542 array('div' => array('class' => 'checkbox')),
3543 array('input' => array(
3544 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
3545 'value' => '1', 'id' => 'ModelMultiField1'
3546 )),
3547 array('label' => array('for' => 'ModelMultiField1')),
3548 'first',
3549 '/label',
3550 '/div'
3551 );
3552 $this->assertTags($result, $expected);
3553
3554 $this->Form->data = array('Model' => array('tags' => array(1)));
3555 $result = $this->Form->select(
3556 'Model.tags', array('1' => 'first', 'Array' => 'Array'), null, array('multiple' => 'checkbox')
3557 );
3558 $expected = array(
3559 'input' => array(
3560 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
3561 ),
3562 array('div' => array('class' => 'checkbox')),
3563 array('input' => array(
3564 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
3565 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
3566 )),
3567 array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
3568 'first',
3569 '/label',
3570 '/div',
3571
3572 array('div' => array('class' => 'checkbox')),
3573 array('input' => array(
3574 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
3575 'value' => 'Array', 'id' => 'ModelTagsArray'
3576 )),
3577 array('label' => array('for' => 'ModelTagsArray')),
3578 'Array',
3579 '/label',
3580 '/div'
3581 );
3582 $this->assertTags($result, $expected);
3583 }
3584
3585 /**
3586 * test multiple checkboxes with div styles.
3587 *
3588 * @return void
3589 */
3590 function testSelectMultipleCheckboxDiv() {
3591 $result = $this->Form->select(
3592 'Model.tags',
3593 array('first', 'second'),
3594 null,
3595 array('multiple' => 'checkbox', 'class' => 'my-class')
3596 );
3597 $expected = array(
3598 'input' => array(
3599 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
3600 ),
3601 array('div' => array('class' => 'my-class')),
3602 array('input' => array(
3603 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
3604 'value' => '0', 'id' => 'ModelTags0'
3605 )),
3606 array('label' => array('for' => 'ModelTags0')), 'first', '/label',
3607 '/div',
3608
3609 array('div' => array('class' => 'my-class')),
3610 array('input' => array(
3611 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
3612 'value' => '1', 'id' => 'ModelTags1'
3613 )),
3614 array('label' => array('for' => 'ModelTags1')), 'second', '/label',
3615 '/div'
3616 );
3617 $this->assertTags($result, $expected);
3618
3619 $result = $this->Form->input('Model.tags', array(
3620 'options' => array('first', 'second'),
3621 'multiple' => 'checkbox',
3622 'class' => 'my-class',
3623 'div' => false,
3624 'label' => false
3625 ));
3626 $this->assertTags($result, $expected);
3627
3628 $this->Form->validationErrors['Model']['tags'] = 'Select atleast one option';
3629 $result = $this->Form->input('Model.tags', array(
3630 'options' => array('one'),
3631 'multiple' => 'checkbox',
3632 'label' => false,
3633 'div' => false
3634 ));
3635 $expected = array(
3636 'input' => array('type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'),
3637 array('div' => array('class' => 'checkbox form-error')),
3638 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][tags][]', 'value' => '0', 'id' => 'ModelTags0')),
3639 array('label' => array('for' => 'ModelTags0')),
3640 'one',
3641 '/label',
3642 '/div'
3643 );
3644 $this->assertTags($result, $expected);
3645
3646 $result = $this->Form->input('Model.tags', array(
3647 'options' => array('one'),
3648 'multiple' => 'checkbox',
3649 'class' => 'mycheckbox',
3650 'label' => false,
3651 'div' => false
3652 ));
3653 $expected = array(
3654 'input' => array('type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'),
3655 array('div' => array('class' => 'mycheckbox form-error')),
3656 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][tags][]', 'value' => '0', 'id' => 'ModelTags0')),
3657 array('label' => array('for' => 'ModelTags0')),
3658 'one',
3659 '/label',
3660 '/div'
3661 );
3662 $this->assertTags($result, $expected);
3663 }
3664
3665 /**
3666 * Checks the security hash array generated for multiple-input checkbox elements
3667 *
3668 * @access public
3669 * @return void
3670 */
3671 function testSelectMultipleCheckboxSecurity() {
3672 $this->Form->params['_Token']['key'] = 'testKey';
3673 $this->assertEqual($this->Form->fields, array());
3674
3675 $result = $this->Form->select(
3676 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
3677 null, array('multiple' => 'checkbox')
3678 );
3679 $this->assertEqual($this->Form->fields, array('Model.multi_field'));
3680
3681 $result = $this->Form->secure($this->Form->fields);
3682 $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
3683 $this->assertPattern('/"' . $key . '"/', $result);
3684 }
3685
3686 /**
3687 * testInputMultipleCheckboxes method
3688 *
3689 * test input() resulting in multi select elements being generated.
3690 *
3691 * @access public
3692 * @return void
3693 */
3694 function testInputMultipleCheckboxes() {
3695 $result = $this->Form->input('Model.multi_field', array(
3696 'options' => array('first', 'second', 'third'),
3697 'multiple' => 'checkbox'
3698 ));
3699 $expected = array(
3700 array('div' => array('class' => 'input select')),
3701 array('label' => array('for' => 'ModelMultiField')),
3702 'Multi Field',
3703 '/label',
3704 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3705 array('div' => array('class' => 'checkbox')),
3706 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
3707 array('label' => array('for' => 'ModelMultiField0')),
3708 'first',
3709 '/label',
3710 '/div',
3711 array('div' => array('class' => 'checkbox')),
3712 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
3713 array('label' => array('for' => 'ModelMultiField1')),
3714 'second',
3715 '/label',
3716 '/div',
3717 array('div' => array('class' => 'checkbox')),
3718 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
3719 array('label' => array('for' => 'ModelMultiField2')),
3720 'third',
3721 '/label',
3722 '/div',
3723 '/div'
3724 );
3725 $this->assertTags($result, $expected);
3726
3727 $result = $this->Form->input('Model.multi_field', array(
3728 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
3729 'multiple' => 'checkbox'
3730 ));
3731 $expected = array(
3732 array('div' => array('class' => 'input select')),
3733 array('label' => array('for' => 'ModelMultiField')),
3734 'Multi Field',
3735 '/label',
3736 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3737 array('div' => array('class' => 'checkbox')),
3738 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
3739 array('label' => array('for' => 'ModelMultiFieldA')),
3740 'first',
3741 '/label',
3742 '/div',
3743 array('div' => array('class' => 'checkbox')),
3744 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
3745 array('label' => array('for' => 'ModelMultiFieldB')),
3746 'second',
3747 '/label',
3748 '/div',
3749 array('div' => array('class' => 'checkbox')),
3750 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
3751 array('label' => array('for' => 'ModelMultiFieldC')),
3752 'third',
3753 '/label',
3754 '/div',
3755 '/div'
3756 );
3757 $this->assertTags($result, $expected);
3758
3759 $result = $this->Form->input('Model.multi_field', array(
3760 'options' => array('1' => 'first'),
3761 'multiple' => 'checkbox',
3762 'label' => false,
3763 'div' => false
3764 ));
3765 $expected = array(
3766 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3767 array('div' => array('class' => 'checkbox')),
3768 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
3769 array('label' => array('for' => 'ModelMultiField1')),
3770 'first',
3771 '/label',
3772 '/div'
3773 );
3774 $this->assertTags($result, $expected);
3775
3776 $result = $this->Form->input('Model.multi_field', array(
3777 'options' => array('2' => 'second'),
3778 'multiple' => 'checkbox',
3779 'label' => false,
3780 'div' => false
3781 ));
3782 $expected = array(
3783 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
3784 array('div' => array('class' => 'checkbox')),
3785 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
3786 array('label' => array('for' => 'ModelMultiField2')),
3787 'second',
3788 '/label',
3789 '/div'
3790 );
3791 $this->assertTags($result, $expected);
3792 }
3793 /**
3794 * testSelectHiddenFieldOmission method
3795 *
3796 * test that select() with 'hiddenField' => false omits the hidden field
3797 *
3798 * @access public
3799 * @return void
3800 */
3801 function testSelectHiddenFieldOmission() {
3802 $result = $this->Form->select('Model.multi_field',
3803 array('first', 'second'),
3804 null,
3805 array('multiple' => 'checkbox', 'hiddenField' => false)
3806 );
3807 $expected = array(
3808 array('div' => array('class' => 'checkbox')),
3809 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
3810 array('label' => array('for' => 'ModelMultiField0')),
3811 'first',
3812 '/label',
3813 '/div',
3814 array('div' => array('class' => 'checkbox')),
3815 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
3816 array('label' => array('for' => 'ModelMultiField1')),
3817 'second',
3818 '/label',
3819 '/div'
3820 );
3821 $this->assertTags($result, $expected);
3822
3823 $result = $this->Form->input('Model.multi_field', array(
3824 'options' => array('first', 'second'),
3825 'multiple' => 'checkbox',
3826 'hiddenField' => false
3827 ));
3828 $expected = array(
3829 array('div' => array('class' => 'input select')),
3830 array('label' => array('for' => 'ModelMultiField')),
3831 'Multi Field',
3832 '/label',
3833 array('div' => array('class' => 'checkbox')),
3834 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
3835 array('label' => array('for' => 'ModelMultiField0')),
3836 'first',
3837 '/label',
3838 '/div',
3839 array('div' => array('class' => 'checkbox')),
3840 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
3841 array('label' => array('for' => 'ModelMultiField1')),
3842 'second',
3843 '/label',
3844 '/div',
3845 '/div'
3846 );
3847 $this->assertTags($result, $expected);
3848 }
3849
3850 /**
3851 * test that select() with multiple = checkbox works with overriding name attribute.
3852 *
3853 * @return void
3854 */
3855 function testSelectCheckboxMultipleOverrideName() {
3856 $result = $this->Form->input('category', array(
3857 'type' => 'select',
3858 'multiple' => 'checkbox',
3859 'name' => 'data[fish]',
3860 'options' => array('1', '2'),
3861 'div' => false,
3862 'label' => false,
3863 ));
3864 $expected = array(
3865 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
3866 array('div' => array('class' => 'checkbox')),
3867 array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
3868 array('label' => array('for' => 'Category0')), '1', '/label',
3869 '/div',
3870 array('div' => array('class' => 'checkbox')),
3871 array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
3872 array('label' => array('for' => 'Category1')), '2', '/label',
3873 '/div'
3874 );
3875 $this->assertTags($result, $expected);
3876 }
3877
3878 /**
3879 * testCheckbox method
3880 *
3881 * Test generation of checkboxes
3882 *
3883 * @access public
3884 * @return void
3885 */
3886 function testCheckbox() {
3887 $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
3888 $expected = array(
3889 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
3890 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
3891 );
3892 $this->assertTags($result, $expected);
3893
3894 $this->Form->validationErrors['Model']['field'] = 1;
3895 $this->Form->data['Model']['field'] = 'myvalue';
3896 $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
3897 $expected = array(
3898 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
3899 array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
3900 );
3901 $this->assertTags($result, $expected);
3902
3903 $result = $this->Form->checkbox('Model.field', array('value' => 'myvalue'));
3904 $expected = array(
3905 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3906 array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ModelField', 'checked' => 'checked', 'class' => 'form-error'))
3907 );
3908 $this->assertTags($result, $expected);
3909
3910 $this->Form->data['Model']['field'] = '';
3911 $result = $this->Form->checkbox('Model.field', array('id' => 'theID'));
3912 $expected = array(
3913 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
3914 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
3915 );
3916 $this->assertTags($result, $expected);
3917
3918 unset($this->Form->validationErrors['Model']['field']);
3919 $result = $this->Form->checkbox('Model.field', array('value' => 'myvalue'));
3920 $expected = array(
3921 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3922 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'ModelField'))
3923 );
3924 $this->assertTags($result, $expected);
3925
3926 $result = $this->Form->checkbox('Contact.name', array('value' => 'myvalue'));
3927 $expected = array(
3928 'input' => array('type' => 'hidden', 'name' => 'data[Contact][name]', 'value' => '0', 'id' => 'ContactName_'),
3929 array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][name]', 'value' => 'myvalue', 'id' => 'ContactName'))
3930 );
3931 $this->assertTags($result, $expected);
3932
3933 $result = $this->Form->checkbox('Model.field');
3934 $expected = array(
3935 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3936 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
3937 );
3938 $this->assertTags($result, $expected);
3939
3940 $result = $this->Form->checkbox('Model.field', array('checked' => false));
3941 $expected = array(
3942 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3943 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
3944 );
3945 $this->assertTags($result, $expected);
3946
3947 $this->Form->validationErrors['Model']['field'] = 1;
3948 $this->Form->data['Contact']['published'] = 1;
3949 $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
3950 $expected = array(
3951 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
3952 array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
3953 );
3954 $this->assertTags($result, $expected);
3955
3956 $this->Form->validationErrors['Model']['field'] = 1;
3957 $this->Form->data['Contact']['published'] = 0;
3958 $result = $this->Form->checkbox('Contact.published', array('id'=>'theID'));
3959 $expected = array(
3960 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
3961 array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
3962 );
3963 $this->assertTags($result, $expected);
3964
3965 $result = $this->Form->checkbox('Model.CustomField.1.value');
3966 $expected = array(
3967 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
3968 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
3969 );
3970 $this->assertTags($result, $expected);
3971
3972 $result = $this->Form->checkbox('CustomField.1.value');
3973 $expected = array(
3974 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
3975 array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
3976 );
3977 $this->assertTags($result, $expected);
3978
3979 $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
3980 $expected = array(
3981 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
3982 array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
3983 );
3984 $this->assertTags($result, $expected);
3985 }
3986
3987 /**
3988 * test the checked option for checkboxes.
3989 *
3990 * @return void
3991 */
3992 function testCheckboxCheckedOption() {
3993 $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
3994 $expected = array(
3995 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
3996 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
3997 );
3998 $this->assertTags($result, $expected);
3999
4000 $result = $this->Form->checkbox('Model.field', array('checked' => 1));
4001 $expected = array(
4002 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
4003 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
4004 );
4005 $this->assertTags($result, $expected);
4006
4007 $result = $this->Form->checkbox('Model.field', array('checked' => true));
4008 $expected = array(
4009 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
4010 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
4011 );
4012 $this->assertTags($result, $expected);
4013
4014 $result = $this->Form->checkbox('Model.field', array('checked' => false));
4015 $expected = array(
4016 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
4017 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
4018 );
4019 $this->assertTags($result, $expected);
4020
4021 $this->Form->data['Model']['field'] = 1;
4022 $result = $this->Form->checkbox('Model.field', array('checked' => false));
4023 $expected = array(
4024 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
4025 array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
4026 );
4027 $this->assertTags($result, $expected);
4028 }
4029
4030 /**
4031 * Test that disabling a checkbox also disables the hidden input so no value is submitted
4032 *
4033 * @return void
4034 */
4035 function testCheckboxDisabling() {
4036 $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
4037 $expected = array(
4038 array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
4039 array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
4040 );
4041 $this->assertTags($result, $expected,true);
4042 }
4043
4044 /**
4045 * Test that specifying false in the 'disabled' option will not disable either the hidden input or the checkbox input
4046 *
4047 * @return void
4048 */
4049 function testCheckboxHiddenDisabling() {
4050 $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
4051 $expected = array(
4052 array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
4053 array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
4054 );
4055 $this->assertTags($result, $expected);
4056 }
4057
4058 /**
4059 * Test that the hidden input for checkboxes can be removed/omitted from the output.
4060 *
4061 * @return void
4062 */
4063 function testCheckboxHiddenFieldOmission() {
4064 $result = $this->Form->input('UserForm.something', array(
4065 'type' => 'checkbox',
4066 'hiddenField' => false
4067 )
4068 );
4069 $expected = array(
4070 'div' => array('class' => 'input checkbox'),
4071 array('input' => array(
4072 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
4073 'value' => '1', 'id' => 'UserFormSomething'
4074 )),
4075 'label' => array('for' => 'UserFormSomething'),
4076 'Something',
4077 '/label',
4078 '/div'
4079 );
4080 $this->assertTags($result, $expected);
4081 }
4082
4083 /**
4084 * testDateTime method
4085 *
4086 * Test generation of date/time select elements
4087 *
4088 * @access public
4089 * @return void
4090 */
4091 function testDateTime() {
4092 extract($this->dateRegex);
4093
4094 $result = $this->Form->dateTime('Contact.date', 'DMY', '12', null, array('empty' => false));
4095 $now = strtotime('now');
4096 $expected = array(
4097 array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
4098 $daysRegex,
4099 array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
4100 date('j', $now),
4101 '/option',
4102 '*/select',
4103 '-',
4104 array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
4105 $monthsRegex,
4106 array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
4107 date('F', $now),
4108 '/option',
4109 '*/select',
4110 '-',
4111 array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
4112 $yearsRegex,
4113 array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
4114 date('Y', $now),
4115 '/option',
4116 '*/select',
4117 array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
4118 $hoursRegex,
4119 array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
4120 date('g', $now),
4121 '/option',
4122 '*/select',
4123 ':',
4124 array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
4125 $minutesRegex,
4126 array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
4127 date('i', $now),
4128 '/option',
4129 '*/select',
4130 ' ',
4131 array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
4132 $meridianRegex,
4133 array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
4134 date('a', $now),
4135 '/option',
4136 '*/select'
4137 );
4138 $this->assertTags($result, $expected);
4139
4140 $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
4141 $expected = array(
4142 array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
4143 $daysRegex,
4144 array('option' => array('value' => '')),
4145 '/option',
4146 '*/select',
4147 '-',
4148 array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
4149 $monthsRegex,
4150 array('option' => array('value' => '')),
4151 '/option',
4152 '*/select',
4153 '-',
4154 array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
4155 $yearsRegex,
4156 array('option' => array('value' => '')),
4157 '/option',
4158 '*/select',
4159 array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
4160 $hoursRegex,
4161 array('option' => array('value' => '')),
4162 '/option',
4163 '*/select',
4164 ':',
4165 array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
4166 $minutesRegex,
4167 array('option' => array('value' => '')),
4168 '/option',
4169 '*/select',
4170 ' ',
4171 array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
4172 $meridianRegex,
4173 array('option' => array('value' => '')),
4174 '/option',
4175 '*/select'
4176 );
4177 $this->assertTags($result, $expected);
4178 $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
4179
4180 $result = $this->Form->dateTime('Contact.date', 'DMY', '12', false);
4181 $expected = array(
4182 array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
4183 $daysRegex,
4184 array('option' => array('value' => '')),
4185 '/option',
4186 '*/select',
4187 '-',
4188 array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
4189 $monthsRegex,
4190 array('option' => array('value' => '')),
4191 '/option',
4192 '*/select',
4193 '-',
4194 array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
4195 $yearsRegex,
4196 array('option' => array('value' => '')),
4197 '/option',
4198 '*/select',
4199 array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
4200 $hoursRegex,
4201 array('option' => array('value' => '')),
4202 '/option',
4203 '*/select',
4204 ':',
4205 array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
4206 $minutesRegex,
4207 array('option' => array('value' => '')),
4208 '/option',
4209 '*/select',
4210 ' ',
4211 array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
4212 $meridianRegex,
4213 array('option' => array('value' => '')),
4214 '/option',
4215 '*/select'
4216 );
4217 $this->assertTags($result, $expected);
4218 $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
4219
4220 $result = $this->Form->dateTime('Contact.date', 'DMY', '12', '');
4221 $expected = array(
4222 array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
4223 $daysRegex,
4224 array('option' => array('value' => '')),
4225 '/option',
4226 '*/select',
4227 '-',
4228 array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
4229 $monthsRegex,
4230 array('option' => array('value' => '')),
4231 '/option',
4232 '*/select',
4233 '-',
4234 array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
4235 $yearsRegex,
4236 array('option' => array('value' => '')),
4237 '/option',
4238 '*/select',
4239 array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
4240 $hoursRegex,
4241 array('option' => array('value' => '')),
4242 '/option',
4243 '*/select',
4244 ':',
4245 array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
4246 $minutesRegex,
4247 array('option' => array('value' => '')),
4248 '/option',
4249 '*/select',
4250 ' ',
4251 array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
4252 $meridianRegex,
4253 array('option' => array('value' => '')),
4254 '/option',
4255 '*/select'
4256 );
4257 $this->assertTags($result, $expected);
4258 $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
4259
4260 $result = $this->Form->dateTime('Contact.date', 'DMY', '12', '', array('interval' => 5));
4261 $expected = array(
4262 array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
4263 $daysRegex,
4264 array('option' => array('value' => '')),
4265 '/option',
4266 '*/select',
4267 '-',
4268 array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
4269 $monthsRegex,
4270 array('option' => array('value' => '')),
4271 '/option',
4272 '*/select',
4273 '-',
4274 array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
4275 $yearsRegex,
4276 array('option' => array('value' => '')),
4277 '/option',
4278 '*/select',
4279 array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
4280 $hoursRegex,
4281 array('option' => array('value' => '')),
4282 '/option',
4283 '*/select',
4284 ':',
4285 array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
4286 $minutesRegex,
4287 array('option' => array('value' => '')),
4288 '/option',
4289 array('option' => array('value' => '00')),
4290 '00',
4291 '/option',
4292 array('option' => array('value' => '05')),
4293 '05',
4294 '/option',
4295 array('option' => array('value' => '10')),
4296 '10',
4297 '/option',
4298 '*/select',
4299 ' ',
4300 array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
4301 $meridianRegex,
4302 array('option' => array('value' => '')),
4303 '/option',
4304 '*/select'
4305 );
4306 $this->assertTags($result, $expected);
4307 $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
4308
4309 $result = $this->Form->dateTime('Contact.date', 'DMY', '12', '', array('minuteInterval' => 5));
4310 $expected = array(
4311 array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
4312 $daysRegex,
4313 array('option' => array('value' => '')),
4314 '/option',
4315 '*/select',
4316 '-',
4317 array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
4318 $monthsRegex,
4319 array('option' => array('value' => '')),
4320 '/option',
4321 '*/select',
4322 '-',
4323 array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
4324 $yearsRegex,
4325 array('option' => array('value' => '')),
4326 '/option',
4327 '*/select',
4328 array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
4329 $hoursRegex,
4330 array('option' => array('value' => '')),
4331 '/option',
4332 '*/select',
4333 ':',
4334 array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
4335 $minutesRegex,
4336 array('option' => array('value' => '')),
4337 '/option',
4338 array('option' => array('value' => '00')),
4339 '00',
4340 '/option',
4341 array('option' => array('value' => '05')),
4342 '05',
4343 '/option',
4344 array('option' => array('value' => '10')),
4345 '10',
4346 '/option',
4347 '*/select',
4348 ' ',
4349 array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
4350 $meridianRegex,
4351 array('option' => array('value' => '')),
4352 '/option',
4353 '*/select'
4354 );
4355 $this->assertTags($result, $expected);
4356 $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
4357
4358 $this->Form->data['Contact']['data'] = null;
4359 $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
4360 $expected = array(
4361 array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
4362 $daysRegex,
4363 array('option' => array('value' => '')),
4364 '/option',
4365 '*/select',
4366 '-',
4367 array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
4368 $monthsRegex,
4369 array('option' => array('value' => '')),
4370 '/option',
4371 '*/select',
4372 '-',
4373 array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
4374 $yearsRegex,
4375 array('option' => array('value' => '')),
4376 '/option',
4377 '*/select',
4378 array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
4379 $hoursRegex,
4380 array('option' => array('value' => '')),
4381 '/option',
4382 '*/select',
4383 ':',
4384 array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
4385 $minutesRegex,
4386 array('option' => array('value' => '')),
4387 '/option',
4388 '*/select',
4389 ' ',
4390 array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
4391 $meridianRegex,
4392 array('option' => array('value' => '')),
4393 '/option',
4394 '*/select'
4395 );
4396 $this->assertTags($result, $expected);
4397 $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
4398
4399 $this->Form->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
4400 $now = strtotime($this->Form->data['Model']['field']);
4401 $result = $this->Form->dateTime('Model.field', 'DMY', '12', null, array('empty' => false));
4402 $expected = array(
4403 array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
4404 $daysRegex,
4405 array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
4406 date('j', $now),
4407 '/option',
4408 '*/select',
4409 '-',
4410 array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
4411 $monthsRegex,
4412 array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
4413 date('F', $now),
4414 '/option',
4415 '*/select',
4416 '-',
4417 array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
4418 $yearsRegex,
4419 array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
4420 date('Y', $now),
4421 '/option',
4422 '*/select',
4423 array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
4424 $hoursRegex,
4425 array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
4426 date('g', $now),
4427 '/option',
4428 '*/select',
4429 ':',
4430 array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
4431 $minutesRegex,
4432 array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
4433 date('i', $now),
4434 '/option',
4435 '*/select',
4436 ' ',
4437 array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
4438 $meridianRegex,
4439 array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
4440 date('a', $now),
4441 '/option',
4442 '*/select'
4443 );
4444 $this->assertTags($result, $expected);
4445
4446 $selected = strtotime('2008-10-26 10:33:00');
4447 $result = $this->Form->dateTime('Model.field', 'DMY', '12', $selected);
4448 $this->assertPattern('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
4449 $this->assertPattern('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>10<\/option>/', $result);
4450 $this->assertPattern('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
4451 $this->assertPattern('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>10<\/option>/', $result);
4452 $this->assertPattern('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
4453
4454 $this->Form->create('Contact');
4455 $result = $this->Form->input('published');
4456 $now = strtotime('now');
4457 $expected = array(
4458 'div' => array('class' => 'input date'),
4459 'label' => array('for' => 'ContactPublishedMonth'),
4460 'Published',
4461 '/label',
4462 array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
4463 $monthsRegex,
4464 array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
4465 date('F', $now),
4466 '/option',
4467 '*/select',
4468 '-',
4469 array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
4470 $daysRegex,
4471 array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
4472 date('j', $now),
4473 '/option',
4474 '*/select',
4475 '-',
4476 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
4477 $yearsRegex,
4478 array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
4479 date('Y', $now),
4480 '/option',
4481 '*/select',
4482 '/div'
4483 );
4484 $this->assertTags($result, $expected);
4485
4486 $result = $this->Form->input('published2', array('type' => 'date'));
4487 $now = strtotime('now');
4488 $expected = array(
4489 'div' => array('class' => 'input date'),
4490 'label' => array('for' => 'ContactPublished2Month'),
4491 'Published2',
4492 '/label',
4493 array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
4494 $monthsRegex,
4495 array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
4496 date('F', $now),
4497 '/option',
4498 '*/select',
4499 '-',
4500 array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
4501 $daysRegex,
4502 array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
4503 date('j', $now),
4504 '/option',
4505 '*/select',
4506 '-',
4507 array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
4508 $yearsRegex,
4509 array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
4510 date('Y', $now),
4511 '/option',
4512 '*/select',
4513 '/div'
4514 );
4515 $this->assertTags($result, $expected);
4516
4517 $result = $this->Form->input('ContactTag');
4518 $expected = array(
4519 'div' => array('class' => 'input select'),
4520 'label' => array('for' => 'ContactTagContactTag'),
4521 'Contact Tag',
4522 '/label',
4523 array('input' => array('type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_')),
4524 array('select' => array('name' => 'data[ContactTag][ContactTag][]', 'multiple' => 'multiple', 'id' => 'ContactTagContactTag')),
4525 '/select',
4526 '/div'
4527 );
4528 $this->assertTags($result, $expected);
4529
4530 $this->Form->create('Contact');
4531 $result = $this->Form->input('published', array('monthNames' => false));
4532 $now = strtotime('now');
4533 $expected = array(
4534 'div' => array('class' => 'input date'),
4535 'label' => array('for' => 'ContactPublishedMonth'),
4536 'Published',
4537 '/label',
4538 array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
4539 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
4540 array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
4541 date('m', $now),
4542 '/option',
4543 '*/select',
4544 '-',
4545 array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
4546 $daysRegex,
4547 array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
4548 date('j', $now),
4549 '/option',
4550 '*/select',
4551 '-',
4552 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
4553 $yearsRegex,
4554 array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
4555 date('Y', $now),
4556 '/option',
4557 '*/select',
4558 '/div'
4559 );
4560 $this->assertTags($result, $expected);
4561
4562 $result = $this->Form->input('published', array('type' => 'time'));
4563 $now = strtotime('now');
4564 $expected = array(
4565 'div' => array('class' => 'input time'),
4566 'label' => array('for' => 'ContactPublishedHour'),
4567 'Published',
4568 '/label',
4569 array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
4570 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
4571 array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
4572 date('g', $now),
4573 '/option',
4574 '*/select',
4575 ':',
4576 );
4577 $this->assertTags($result, $expected);
4578
4579 $result = $this->Form->input('published', array(
4580 'timeFormat' => 24,
4581 'interval' => 5,
4582 'selected' => strtotime('2009-09-03 13:37:00'),
4583 'type' => 'datetime'
4584 ));
4585 $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
4586 $this->assertPattern('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
4587 $this->assertPattern('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
4588 $this->assertPattern('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
4589 $this->assertPattern('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
4590
4591 $this->assertNoErrors();
4592 $this->Form->data['Contact'] = array(
4593 'date' => array(
4594 'day' => '',
4595 'month' => '',
4596 'year' => '',
4597 'hour' => '',
4598 'min' => '',
4599 'meridian' => ''
4600 )
4601 );
4602 $result = $this->Form->dateTime('Contact.date', 'DMY', '12', null, array('empty' => false));
4603 }
4604
4605 /**
4606 * test that datetime() and default values work.
4607 *
4608 * @return void
4609 */
4610 function testDatetimeWithDefault() {
4611 $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', null, array('value' => '2009-06-01 11:15:30'));
4612 $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
4613 $this->assertPattern('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
4614 $this->assertPattern('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
4615
4616 $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', null, array(
4617 'default' => '2009-06-01 11:15:30'
4618 ));
4619 $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
4620 $this->assertPattern('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
4621 $this->assertPattern('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
4622 }
4623
4624 /**
4625 * test that bogus non-date time data doesn't cause errors.
4626 *
4627 * @return void
4628 */
4629 function testDateTimeWithBogusData() {
4630 $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', 'CURRENT_TIMESTAMP');
4631 $this->assertNoPattern('/selected="selected">\d/', $result);
4632 }
4633
4634 /**
4635 * testFormDateTimeMulti method
4636 *
4637 * test multiple datetime element generation
4638 *
4639 * @access public
4640 * @return void
4641 */
4642 function testFormDateTimeMulti() {
4643 extract($this->dateRegex);
4644
4645 $result = $this->Form->dateTime('Contact.1.updated');
4646 $expected = array(
4647 array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
4648 $daysRegex,
4649 array('option' => array('value' => '')),
4650 '/option',
4651 '*/select',
4652 '-',
4653 array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
4654 $monthsRegex,
4655 array('option' => array('value' => '')),
4656 '/option',
4657 '*/select',
4658 '-',
4659 array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
4660 $yearsRegex,
4661 array('option' => array('value' => '')),
4662 '/option',
4663 '*/select',
4664 array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
4665 $hoursRegex,
4666 array('option' => array('value' => '')),
4667 '/option',
4668 '*/select',
4669 ':',
4670 array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
4671 $minutesRegex,
4672 array('option' => array('value' => '')),
4673 '/option',
4674 '*/select',
4675 ' ',
4676 array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
4677 $meridianRegex,
4678 array('option' => array('value' => '')),
4679 '/option',
4680 '*/select'
4681 );
4682 $this->assertTags($result, $expected);
4683
4684 $result = $this->Form->dateTime('Contact.2.updated');
4685 $expected = array(
4686 array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
4687 $daysRegex,
4688 array('option' => array('value' => '')),
4689 '/option',
4690 '*/select',
4691 '-',
4692 array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
4693 $monthsRegex,
4694 array('option' => array('value' => '')),
4695 '/option',
4696 '*/select',
4697 '-',
4698 array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
4699 $yearsRegex,
4700 array('option' => array('value' => '')),
4701 '/option',
4702 '*/select',
4703 array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
4704 $hoursRegex,
4705 array('option' => array('value' => '')),
4706 '/option',
4707 '*/select',
4708 ':',
4709 array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
4710 $minutesRegex,
4711 array('option' => array('value' => '')),
4712 '/option',
4713 '*/select',
4714 ' ',
4715 array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
4716 $meridianRegex,
4717 array('option' => array('value' => '')),
4718 '/option',
4719 '*/select'
4720 );
4721 $this->assertTags($result, $expected);
4722 }
4723
4724 /**
4725 * testMonth method
4726 *
4727 * @access public
4728 * @return void
4729 */
4730 function testMonth() {
4731 $result = $this->Form->month('Model.field');
4732 $expected = array(
4733 array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
4734 array('option' => array('value' => '')),
4735 '/option',
4736 array('option' => array('value' => '01')),
4737 date('F', strtotime('2008-01-01 00:00:00')),
4738 '/option',
4739 array('option' => array('value' => '02')),
4740 date('F', strtotime('2008-02-01 00:00:00')),
4741 '/option',
4742 '*/select',
4743 );
4744 $this->assertTags($result, $expected);
4745
4746 $result = $this->Form->month('Model.field', null, array('empty' => true));
4747 $expected = array(
4748 array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
4749 array('option' => array('value' => '')),
4750 '/option',
4751 array('option' => array('value' => '01')),
4752 date('F', strtotime('2008-01-01 00:00:00')),
4753 '/option',
4754 array('option' => array('value' => '02')),
4755 date('F', strtotime('2008-02-01 00:00:00')),
4756 '/option',
4757 '*/select',
4758 );
4759 $this->assertTags($result, $expected);
4760
4761 $result = $this->Form->month('Model.field', null, array('monthNames' => false));
4762 $expected = array(
4763 array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
4764 array('option' => array('value' => '')),
4765 '/option',
4766 array('option' => array('value' => '01')),
4767 '01',
4768 '/option',
4769 array('option' => array('value' => '02')),
4770 '02',
4771 '/option',
4772 '*/select',
4773 );
4774 $this->assertTags($result, $expected);
4775
4776 $monthNames = array(
4777 '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
4778 '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
4779 $result = $this->Form->month('Model.field', null, array('monthNames' => $monthNames));
4780 $expected = array(
4781 array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
4782 array('option' => array('value' => '')),
4783 '/option',
4784 array('option' => array('value' => '01')),
4785 'Jan',
4786 '/option',
4787 array('option' => array('value' => '02')),
4788 'Feb',
4789 '/option',
4790 '*/select',
4791 );
4792 $this->assertTags($result, $expected);
4793 }
4794
4795 /**
4796 * testDay method
4797 *
4798 * @access public
4799 * @return void
4800 */
4801 function testDay() {
4802 extract($this->dateRegex);
4803
4804 $result = $this->Form->day('Model.field', false);
4805 $expected = array(
4806 array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
4807 array('option' => array('value' => '')),
4808 '/option',
4809 array('option' => array('value' => '01')),
4810 '1',
4811 '/option',
4812 array('option' => array('value' => '02')),
4813 '2',
4814 '/option',
4815 $daysRegex,
4816 '/select',
4817 );
4818 $this->assertTags($result, $expected);
4819
4820 $this->Form->data['Model']['field'] = '2006-10-10 23:12:32';
4821 $result = $this->Form->day('Model.field');
4822 $expected = array(
4823 array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
4824 array('option' => array('value' => '')),
4825 '/option',
4826 array('option' => array('value' => '01')),
4827 '1',
4828 '/option',
4829 array('option' => array('value' => '02')),
4830 '2',
4831 '/option',
4832 $daysRegex,
4833 array('option' => array('value' => '10', 'selected' => 'selected')),
4834 '10',
4835 '/option',
4836 $daysRegex,
4837 '/select',
4838 );
4839 $this->assertTags($result, $expected);
4840
4841 $this->Form->data['Model']['field'] = '';
4842 $result = $this->Form->day('Model.field', '10');
4843 $expected = array(
4844 array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
4845 array('option' => array('value' => '')),
4846 '/option',
4847 array('option' => array('value' => '01')),
4848 '1',
4849 '/option',
4850 array('option' => array('value' => '02')),
4851 '2',
4852 '/option',
4853 $daysRegex,
4854 array('option' => array('value' => '10', 'selected' => 'selected')),
4855 '10',
4856 '/option',
4857 $daysRegex,
4858 '/select',
4859 );
4860 $this->assertTags($result, $expected);
4861
4862 $this->Form->data['Model']['field'] = '2006-10-10 23:12:32';
4863 $result = $this->Form->day('Model.field', true);
4864 $expected = array(
4865 array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
4866 array('option' => array('value' => '')),
4867 '/option',
4868 array('option' => array('value' => '01')),
4869 '1',
4870 '/option',
4871 array('option' => array('value' => '02')),
4872 '2',
4873 '/option',
4874 $daysRegex,
4875 array('option' => array('value' => '10', 'selected' => 'selected')),
4876 '10',
4877 '/option',
4878 $daysRegex,
4879 '/select',
4880 );
4881 $this->assertTags($result, $expected);
4882 }
4883
4884 /**
4885 * testMinute method
4886 *
4887 * @access public
4888 * @return void
4889 */
4890 function testMinute() {
4891 extract($this->dateRegex);
4892
4893 $result = $this->Form->minute('Model.field');
4894 $expected = array(
4895 array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
4896 array('option' => array('value' => '')),
4897 '/option',
4898 array('option' => array('value' => '00')),
4899 '00',
4900 '/option',
4901 array('option' => array('value' => '01')),
4902 '01',
4903 '/option',
4904 array('option' => array('value' => '02')),
4905 '02',
4906 '/option',
4907 $minutesRegex,
4908 '/select',
4909 );
4910 $this->assertTags($result, $expected);
4911
4912 $this->Form->data['Model']['field'] = '2006-10-10 00:12:32';
4913 $result = $this->Form->minute('Model.field');
4914 $expected = array(
4915 array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
4916 array('option' => array('value' => '')),
4917 '/option',
4918 array('option' => array('value' => '00')),
4919 '00',
4920 '/option',
4921 array('option' => array('value' => '01')),
4922 '01',
4923 '/option',
4924 array('option' => array('value' => '02')),
4925 '02',
4926 '/option',
4927 $minutesRegex,
4928 array('option' => array('value' => '12', 'selected' => 'selected')),
4929 '12',
4930 '/option',
4931 $minutesRegex,
4932 '/select',
4933 );
4934 $this->assertTags($result, $expected);
4935
4936 $this->Form->data['Model']['field'] = '';
4937 $result = $this->Form->minute('Model.field', null, array('interval' => 5));
4938 $expected = array(
4939 array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
4940 array('option' => array('value' => '')),
4941 '/option',
4942 array('option' => array('value' => '00')),
4943 '00',
4944 '/option',
4945 array('option' => array('value' => '05')),
4946 '05',
4947 '/option',
4948 array('option' => array('value' => '10')),
4949 '10',
4950 '/option',
4951 $minutesRegex,
4952 '/select',
4953 );
4954 $this->assertTags($result, $expected);
4955
4956 $this->Form->data['Model']['field'] = '2006-10-10 00:10:32';
4957 $result = $this->Form->minute('Model.field', null, array('interval' => 5));
4958 $expected = array(
4959 array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
4960 array('option' => array('value' => '')),
4961 '/option',
4962 array('option' => array('value' => '00')),
4963 '00',
4964 '/option',
4965 array('option' => array('value' => '05')),
4966 '05',
4967 '/option',
4968 array('option' => array('value' => '10', 'selected' => 'selected')),
4969 '10',
4970 '/option',
4971 $minutesRegex,
4972 '/select',
4973 );
4974 $this->assertTags($result, $expected);
4975 }
4976
4977 /**
4978 * testHour method
4979 *
4980 * @access public
4981 * @return void
4982 */
4983 function testHour() {
4984 extract($this->dateRegex);
4985
4986 $result = $this->Form->hour('Model.field', false);
4987 $expected = array(
4988 array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
4989 array('option' => array('value' => '')),
4990 '/option',
4991 array('option' => array('value' => '01')),
4992 '1',
4993 '/option',
4994 array('option' => array('value' => '02')),
4995 '2',
4996 '/option',
4997 $hoursRegex,
4998 '/select',
4999 );
5000 $this->assertTags($result, $expected);
5001
5002 $this->Form->data['Model']['field'] = '2006-10-10 00:12:32';
5003 $result = $this->Form->hour('Model.field', false);
5004 $expected = array(
5005 array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
5006 array('option' => array('value' => '')),
5007 '/option',
5008 array('option' => array('value' => '01')),
5009 '1',
5010 '/option',
5011 array('option' => array('value' => '02')),
5012 '2',
5013 '/option',
5014 $hoursRegex,
5015 array('option' => array('value' => '12', 'selected' => 'selected')),
5016 '12',
5017 '/option',
5018 '/select',
5019 );
5020 $this->assertTags($result, $expected);
5021
5022 $this->Form->data['Model']['field'] = '';
5023 $result = $this->Form->hour('Model.field', true, '23');
5024 $expected = array(
5025 array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
5026 array('option' => array('value' => '')),
5027 '/option',
5028 array('option' => array('value' => '00')),
5029 '0',
5030 '/option',
5031 array('option' => array('value' => '01')),
5032 '1',
5033 '/option',
5034 array('option' => array('value' => '02')),
5035 '2',
5036 '/option',
5037 $hoursRegex,
5038 array('option' => array('value' => '23', 'selected' => 'selected')),
5039 '23',
5040 '/option',
5041 '/select',
5042 );
5043 $this->assertTags($result, $expected);
5044
5045 $this->Form->data['Model']['field'] = '2006-10-10 00:12:32';
5046 $result = $this->Form->hour('Model.field', true);
5047 $expected = array(
5048 array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
5049 array('option' => array('value' => '')),
5050 '/option',
5051 array('option' => array('value' => '00', 'selected' => 'selected')),
5052 '0',
5053 '/option',
5054 array('option' => array('value' => '01')),
5055 '1',
5056 '/option',
5057 array('option' => array('value' => '02')),
5058 '2',
5059 '/option',
5060 $hoursRegex,
5061 '/select',
5062 );
5063 $this->assertTags($result, $expected);
5064
5065 unset($this->Form->data['Model']['field']);
5066 $result = $this->Form->hour('Model.field', true, 'now');
5067 $thisHour = date('H');
5068 $optValue = date('G');
5069 $this->assertPattern('/<option value="' . $thisHour . '" selected="selected">'. $optValue .'<\/option>/', $result);
5070 }
5071
5072 /**
5073 * testYear method
5074 *
5075 * @access public
5076 * @return void
5077 */
5078 function testYear() {
5079 $result = $this->Form->year('Model.field', 2006, 2007);
5080 $expected = array(
5081 array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
5082 array('option' => array('value' => '')),
5083 '/option',
5084 array('option' => array('value' => '2007')),
5085 '2007',
5086 '/option',
5087 array('option' => array('value' => '2006')),
5088 '2006',
5089 '/option',
5090 '/select',
5091 );
5092 $this->assertTags($result, $expected);
5093
5094 $result = $this->Form->year('Model.field', 2006, 2007, null, array('orderYear' => 'asc'));
5095 $expected = array(
5096 array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
5097 array('option' => array('value' => '')),
5098 '/option',
5099 array('option' => array('value' => '2006')),
5100 '2006',
5101 '/option',
5102 array('option' => array('value' => '2007')),
5103 '2007',
5104 '/option',
5105 '/select',
5106 );
5107 $this->assertTags($result, $expected);
5108
5109 $this->data['Contact']['published'] = '';
5110 $result = $this->Form->year('Contact.published', 2006, 2007, null, array('class' => 'year'));
5111 $expected = array(
5112 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
5113 array('option' => array('value' => '')),
5114 '/option',
5115 array('option' => array('value' => '2007')),
5116 '2007',
5117 '/option',
5118 array('option' => array('value' => '2006')),
5119 '2006',
5120 '/option',
5121 '/select',
5122 );
5123 $this->assertTags($result, $expected);
5124
5125 $this->Form->data['Contact']['published'] = '2006-10-10';
5126 $result = $this->Form->year('Contact.published', 2006, 2007, null, array('empty' => false));
5127 $expected = array(
5128 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
5129 array('option' => array('value' => '2007')),
5130 '2007',
5131 '/option',
5132 array('option' => array('value' => '2006', 'selected' => 'selected')),
5133 '2006',
5134 '/option',
5135 '/select',
5136 );
5137 $this->assertTags($result, $expected);
5138
5139 $this->Form->data['Contact']['published'] = '';
5140 $result = $this->Form->year('Contact.published', 2006, 2007, false);
5141 $expected = array(
5142 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
5143 array('option' => array('value' => '')),
5144 '/option',
5145 array('option' => array('value' => '2007')),
5146 '2007',
5147 '/option',
5148 array('option' => array('value' => '2006')),
5149 '2006',
5150 '/option',
5151 '/select',
5152 );
5153 $this->assertTags($result, $expected);
5154
5155 $this->Form->data['Contact']['published'] = '2006-10-10';
5156 $result = $this->Form->year('Contact.published', 2006, 2007, false, array('empty' => false));
5157 $expected = array(
5158 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
5159 array('option' => array('value' => '2007')),
5160 '2007',
5161 '/option',
5162 array('option' => array('value' => '2006', 'selected' => 'selected')),
5163 '2006',
5164 '/option',
5165 '/select',
5166 );
5167 $this->assertTags($result, $expected);
5168
5169 $this->Form->data['Contact']['published'] = '';
5170 $result = $this->Form->year('Contact.published', 2006, 2007, 2007);
5171 $expected = array(
5172 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
5173 array('option' => array('value' => '')),
5174 '/option',
5175 array('option' => array('value' => '2007', 'selected' => 'selected')),
5176 '2007',
5177 '/option',
5178 array('option' => array('value' => '2006')),
5179 '2006',
5180 '/option',
5181 '/select',
5182 );
5183 $this->assertTags($result, $expected);
5184
5185 $this->Form->data['Contact']['published'] = '2006-10-10';
5186 $result = $this->Form->year('Contact.published', 2006, 2007, 2007, array('empty' => false));
5187 $expected = array(
5188 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
5189 array('option' => array('value' => '2007', 'selected' => 'selected')),
5190 '2007',
5191 '/option',
5192 array('option' => array('value' => '2006')),
5193 '2006',
5194 '/option',
5195 '/select',
5196 );
5197 $this->assertTags($result, $expected);
5198
5199 $this->Form->data['Contact']['published'] = '';
5200 $result = $this->Form->year('Contact.published', 2006, 2008, 2007, array('empty' => false));
5201 $expected = array(
5202 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
5203 array('option' => array('value' => '2008')),
5204 '2008',
5205 '/option',
5206 array('option' => array('value' => '2007', 'selected' => 'selected')),
5207 '2007',
5208 '/option',
5209 array('option' => array('value' => '2006')),
5210 '2006',
5211 '/option',
5212 '/select',
5213 );
5214 $this->assertTags($result, $expected);
5215
5216 $this->Form->data['Contact']['published'] = '2006-10-10';
5217 $result = $this->Form->year('Contact.published', 2006, 2008, null, array('empty' => false));
5218 $expected = array(
5219 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
5220 array('option' => array('value' => '2008')),
5221 '2008',
5222 '/option',
5223 array('option' => array('value' => '2007')),
5224 '2007',
5225 '/option',
5226 array('option' => array('value' => '2006', 'selected' => 'selected')),
5227 '2006',
5228 '/option',
5229 '/select',
5230 );
5231 $this->assertTags($result, $expected);
5232
5233 $this->Form->data = array();
5234 $this->Form->create('Contact');
5235 $result = $this->Form->year('published', 2006, 2008, null, array('empty' => false));
5236 $expected = array(
5237 array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
5238 array('option' => array('value' => '2008')),
5239 '2008',
5240 '/option',
5241 array('option' => array('value' => '2007')),
5242 '2007',
5243 '/option',
5244 array('option' => array('value' => '2006')),
5245 '2006',
5246 '/option',
5247 '/select',
5248 );
5249 $this->assertTags($result, $expected);
5250 }
5251
5252 /**
5253 * testTextArea method
5254 *
5255 * @access public
5256 * @return void
5257 */
5258 function testTextArea() {
5259 $this->Form->data = array('Model' => array('field' => 'some test data'));
5260 $result = $this->Form->textarea('Model.field');
5261 $expected = array(
5262 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
5263 'some test data',
5264 '/textarea',
5265 );
5266 $this->assertTags($result, $expected);
5267
5268 $result = $this->Form->textarea('Model.tmp');
5269 $expected = array(
5270 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
5271 '/textarea',
5272 );
5273 $this->assertTags($result, $expected);
5274
5275 $this->Form->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
5276 $result = $this->Form->textarea('Model.field');
5277 $expected = array(
5278 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
5279 htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
5280 '/textarea',
5281 );
5282 $this->assertTags($result, $expected);
5283
5284 $this->Form->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
5285 $result = $this->Form->textarea('Model.field', array('escape' => false));
5286 $expected = array(
5287 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
5288 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
5289 '/textarea',
5290 );
5291 $this->assertTags($result, $expected);
5292
5293 $this->Form->data['Model']['0']['OtherModel']['field'] = null;
5294 $result = $this->Form->textarea('Model.0.OtherModel.field');
5295 $expected = array(
5296 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
5297 '/textarea'
5298 );
5299 $this->assertTags($result, $expected);
5300 }
5301
5302 /**
5303 * testTextAreaWithStupidCharacters method
5304 *
5305 * test text area with non-ascii characters
5306 *
5307 * @access public
5308 * @return void
5309 */
5310 function testTextAreaWithStupidCharacters() {
5311 $result = $this->Form->input('Post.content', array(
5312 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
5313 ));
5314 $expected = array(
5315 'div' => array('class' => 'input text'),
5316 'label' => array('for' => 'PostContent'),
5317 'Current Text',
5318 '/label',
5319 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
5320 'GREAT®',
5321 '/textarea',
5322 '/div'
5323 );
5324 $this->assertTags($result, $expected);
5325 }
5326
5327 /**
5328 * testHiddenField method
5329 *
5330 * @access public
5331 * @return void
5332 */
5333 function testHiddenField() {
5334 $this->Form->validationErrors['Model']['field'] = 1;
5335 $this->Form->data['Model']['field'] = 'test';
5336 $result = $this->Form->hidden('Model.field', array('id' => 'theID'));
5337 $this->assertTags($result, array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'theID', 'value' => 'test')));
5338 }
5339
5340 /**
5341 * testFileUploadField method
5342 *
5343 * @access public
5344 * @return void
5345 */
5346 function testFileUploadField() {
5347 $result = $this->Form->file('Model.upload');
5348 $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
5349
5350 $this->Form->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
5351 $result = $this->Form->input('Model.upload', array('type' => 'file'));
5352 $expected = array(
5353 'div' => array('class' => 'input file'),
5354 'label' => array('for' => 'ModelUpload'),
5355 'Upload',
5356 '/label',
5357 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
5358 '/div'
5359 );
5360 $this->assertTags($result, $expected);
5361 }
5362
5363 /**
5364 * test File upload input on a model not used in create();
5365 *
5366 * @return void
5367 */
5368 function testFileUploadOnOtherModel() {
5369 ClassRegistry::removeObject('view');
5370 $controller =& new Controller();
5371 $controller->name = 'ValidateUsers';
5372 $controller->uses = array('ValidateUser');
5373 $controller->constructClasses();
5374 $view =& new View($controller, true);
5375
5376 $this->Form->create('ValidateUser', array('type' => 'file'));
5377 $result = $this->Form->file('ValidateProfile.city');
5378 $expected = array(
5379 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
5380 );
5381 $this->assertTags($result, $expected);
5382 }
5383
5384 /**
5385 * testButton method
5386 *
5387 * @access public
5388 * @return void
5389 */
5390 function testButton() {
5391 $result = $this->Form->button('Hi');
5392 $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
5393
5394 $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
5395 $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
5396
5397 $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
5398 $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
5399
5400 $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
5401 $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
5402
5403 $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
5404 $this->assertNoPattern('/\&039/', $result);
5405 }
5406
5407 /**
5408 * testSubmitButton method
5409 *
5410 * @access public
5411 * @return void
5412 */
5413 function testSubmitButton() {
5414 $result = $this->Form->submit('');
5415 $expected = array(
5416 'div' => array('class' => 'submit'),
5417 'input' => array('type' => 'submit', 'value' => ''),
5418 '/div'
5419 );
5420 $this->assertTags($result, $expected);
5421
5422 $result = $this->Form->submit('Test Submit');
5423 $expected = array(
5424 'div' => array('class' => 'submit'),
5425 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
5426 '/div'
5427 );
5428 $this->assertTags($result, $expected);
5429
5430 $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
5431 $expected = array(
5432 'span' => array('class' => 'submit'),
5433 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
5434 '/span'
5435 );
5436 $this->assertTags($result, $expected);
5437
5438 $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
5439 $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
5440 $this->assertTags($result, $expected);
5441
5442 $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
5443 $expected = array(
5444 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
5445 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
5446 '/div'
5447 );
5448 $this->assertTags($result, $expected);
5449
5450 $result = $this->Form->submit('Next >');
5451 $expected = array(
5452 'div' => array('class' => 'submit'),
5453 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
5454 '/div'
5455 );
5456 $this->assertTags($result, $expected);
5457
5458 $result = $this->Form->submit('Next >', array('escape' => false));
5459 $expected = array(
5460 'div' => array('class' => 'submit'),
5461 'input' => array('type' => 'submit', 'value' => 'Next >'),
5462 '/div'
5463 );
5464 $this->assertTags($result, $expected);
5465
5466 $result = $this->Form->submit('Reset!', array('type' => 'reset'));
5467 $expected = array(
5468 'div' => array('class' => 'submit'),
5469 'input' => array('type' => 'reset', 'value' => 'Reset!'),
5470 '/div'
5471 );
5472 $this->assertTags($result, $expected);
5473
5474 $before = '--before--';
5475 $after = '--after--';
5476 $result = $this->Form->submit('Test', array('before' => $before));
5477 $expected = array(
5478 'div' => array('class' => 'submit'),
5479 '--before--',
5480 'input' => array('type' => 'submit', 'value' => 'Test'),
5481 '/div'
5482 );
5483 $this->assertTags($result, $expected);
5484
5485 $result = $this->Form->submit('Test', array('after' => $after));
5486 $expected = array(
5487 'div' => array('class' => 'submit'),
5488 'input' => array('type' => 'submit', 'value' => 'Test'),
5489 '--after--',
5490 '/div'
5491 );
5492 $this->assertTags($result, $expected);
5493
5494 $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
5495 $expected = array(
5496 'div' => array('class' => 'submit'),
5497 '--before--',
5498 'input' => array('type' => 'submit', 'value' => 'Test'),
5499 '--after--',
5500 '/div'
5501 );
5502 $this->assertTags($result, $expected);
5503 }
5504
5505 /**
5506 * test image submit types.
5507 *
5508 * @return void
5509 */
5510 function testSubmitImage() {
5511 $result = $this->Form->submit('http://example.com/cake.power.gif');
5512 $expected = array(
5513 'div' => array('class' => 'submit'),
5514 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
5515 '/div'
5516 );
5517 $this->assertTags($result, $expected);
5518
5519 $result = $this->Form->submit('/relative/cake.power.gif');
5520 $expected = array(
5521 'div' => array('class' => 'submit'),
5522 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
5523 '/div'
5524 );
5525 $this->assertTags($result, $expected);
5526
5527 $result = $this->Form->submit('cake.power.gif');
5528 $expected = array(
5529 'div' => array('class' => 'submit'),
5530 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
5531 '/div'
5532 );
5533 $this->assertTags($result, $expected);
5534
5535 $result = $this->Form->submit('Not.an.image');
5536 $expected = array(
5537 'div' => array('class' => 'submit'),
5538 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
5539 '/div'
5540 );
5541 $this->assertTags($result, $expected);
5542
5543 $after = '--after--';
5544 $before = '--before--';
5545 $result = $this->Form->submit('cake.power.gif', array('after' => $after));
5546 $expected = array(
5547 'div' => array('class' => 'submit'),
5548 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
5549 '--after--',
5550 '/div'
5551 );
5552 $this->assertTags($result, $expected);
5553
5554 $result = $this->Form->submit('cake.power.gif', array('before' => $before));
5555 $expected = array(
5556 'div' => array('class' => 'submit'),
5557 '--before--',
5558 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
5559 '/div'
5560 );
5561 $this->assertTags($result, $expected);
5562
5563 $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
5564 $expected = array(
5565 'div' => array('class' => 'submit'),
5566 '--before--',
5567 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
5568 '--after--',
5569 '/div'
5570 );
5571 $this->assertTags($result, $expected);
5572
5573 $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
5574 $expected = array(
5575 'div' => array('class' => 'submit'),
5576 '--before--',
5577 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
5578 '--after--',
5579 '/div'
5580 );
5581 $this->assertTags($result, $expected);
5582 }
5583
5584 /**
5585 * test the create() method
5586 *
5587 * @access public
5588 * @return void
5589 */
5590 function testCreate() {
5591 $result = $this->Form->create('Contact');
5592 $encoding = strtolower(Configure::read('App.encoding'));
5593 $expected = array(
5594 'form' => array(
5595 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
5596 'accept-charset' => $encoding
5597 ),
5598 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
5599 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5600 '/div'
5601 );
5602 $this->assertTags($result, $expected);
5603
5604 $result = $this->Form->create('Contact', array('type' => 'GET'));
5605 $expected = array('form' => array(
5606 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
5607 'accept-charset' => $encoding
5608 ));
5609 $this->assertTags($result, $expected);
5610
5611 $result = $this->Form->create('Contact', array('type' => 'get'));
5612 $expected = array('form' => array(
5613 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
5614 'accept-charset' => $encoding
5615 ));
5616 $this->assertTags($result, $expected);
5617
5618 $result = $this->Form->create('Contact', array('type' => 'put'));
5619 $expected = array(
5620 'form' => array(
5621 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
5622 'accept-charset' => $encoding
5623 ),
5624 'div' => array('style' => 'display:none;'),
5625 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
5626 '/div'
5627 );
5628 $this->assertTags($result, $expected);
5629
5630 $result = $this->Form->create('Contact', array('type' => 'file'));
5631 $expected = array(
5632 'form' => array(
5633 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
5634 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
5635 ),
5636 'div' => array('style' => 'display:none;'),
5637 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5638 '/div'
5639 );
5640 $this->assertTags($result, $expected);
5641
5642 $this->Form->data['Contact']['id'] = 1;
5643 $this->Form->params['action'] = 'edit';
5644 $result = $this->Form->create('Contact');
5645 $expected = array(
5646 'form' => array(
5647 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
5648 'accept-charset' => $encoding
5649 ),
5650 'div' => array('style' => 'display:none;'),
5651 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
5652 '/div'
5653 );
5654 $this->assertTags($result, $expected);
5655
5656 $this->Form->data['Contact']['id'] = 1;
5657 $this->Form->params['action'] = 'edit';
5658 $result = $this->Form->create('Contact', array('type' => 'file'));
5659 $expected = array(
5660 'form' => array(
5661 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
5662 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
5663 ),
5664 'div' => array('style' => 'display:none;'),
5665 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
5666 '/div'
5667 );
5668 $this->assertTags($result, $expected);
5669
5670 $this->Form->data['ContactNonStandardPk']['pk'] = 1;
5671 $result = $this->Form->create('ContactNonStandardPk');
5672 $expected = array(
5673 'form' => array(
5674 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
5675 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
5676 ),
5677 'div' => array('style' => 'display:none;'),
5678 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
5679 '/div'
5680 );
5681 $this->assertTags($result, $expected);
5682
5683 $result = $this->Form->create('Contact', array('id' => 'TestId'));
5684 $expected = array(
5685 'form' => array(
5686 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
5687 'accept-charset' => $encoding
5688 ),
5689 'div' => array('style' => 'display:none;'),
5690 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
5691 '/div'
5692 );
5693 $this->assertTags($result, $expected);
5694
5695 $this->Form->params['action'] = 'add';
5696 $result = $this->Form->create('User', array('url' => array('action' => 'login')));
5697 $expected = array(
5698 'form' => array(
5699 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
5700 'accept-charset' => $encoding
5701 ),
5702 'div' => array('style' => 'display:none;'),
5703 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5704 '/div'
5705 );
5706 $this->assertTags($result, $expected);
5707
5708 $result = $this->Form->create('User', array('action' => 'login'));
5709 $expected = array(
5710 'form' => array(
5711 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
5712 'accept-charset' => $encoding
5713 ),
5714 'div' => array('style' => 'display:none;'),
5715 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5716 '/div'
5717 );
5718 $this->assertTags($result, $expected);
5719
5720 $result = $this->Form->create('User', array('url' => '/users/login'));
5721 $expected = array(
5722 'form' => array('method' => 'post', 'action' => '/users/login','accept-charset' => $encoding),
5723 'div' => array('style' => 'display:none;'),
5724 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5725 '/div'
5726 );
5727 $this->assertTags($result, $expected);
5728
5729 $this->Form->params['controller'] = 'pages';
5730 $this->Form->params['models'] = array('User', 'Post');
5731 $result = $this->Form->create('User', array('action' => 'signup'));
5732 $expected = array(
5733 'form' => array(
5734 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
5735 'accept-charset' => $encoding
5736 ),
5737 'div' => array('style' => 'display:none;'),
5738 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5739 '/div'
5740 );
5741 $this->assertTags($result, $expected);
5742
5743 $this->Form->data = array();
5744 $this->Form->params['controller'] = 'contacts';
5745 $this->Form->params['models'] = array('Contact');
5746 $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
5747 $expected = array(
5748 'form' => array(
5749 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
5750 'accept-charset' => 'utf-8'
5751 ),
5752 'div' => array('style' => 'display:none;'),
5753 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5754 '/div'
5755 );
5756 $this->assertTags($result, $expected);
5757 }
5758
5759 /**
5760 * test that inputDefaults are stored and used.
5761 *
5762 * @return void
5763 */
5764 function testCreateWithInputDefaults() {
5765 $this->Form->create('User', array(
5766 'inputDefaults' => array('div' => false, 'label' => false)
5767 ));
5768 $result = $this->Form->input('username');
5769 $expected = array(
5770 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
5771 );
5772 $this->assertTags($result, $expected);
5773
5774 $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
5775 $expected = array(
5776 'div' => array('class' => 'input text'),
5777 'label' => array('for' => 'UserUsername'), 'username', '/label',
5778 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
5779 '/div'
5780 );
5781 $this->assertTags($result, $expected);
5782 }
5783
5784 /**
5785 * test automatic accept-charset overriding
5786 *
5787 * @return void
5788 */
5789 function testCreateWithAcceptCharset() {
5790 $result = $this->Form->create('UserForm', array(
5791 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
5792 )
5793 );
5794 $expected = array(
5795 'form' => array(
5796 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
5797 'accept-charset' => 'iso-8859-1'
5798 ),
5799 'div' => array('style' => 'display:none;'),
5800 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5801 '/div'
5802 );
5803 $this->assertTags($result, $expected);
5804 }
5805
5806 /**
5807 * Test base form url when url param is passed with multiple parameters (&)
5808 *
5809 */
5810 function testCreateQuerystringParams() {
5811 $encoding = strtolower(Configure::read('App.encoding'));
5812 $result = $this->Form->create('Contact', array(
5813 'type' => 'post',
5814 'escape' => false,
5815 'url' => array(
5816 'controller' => 'controller',
5817 'action' => 'action',
5818 '?' => array('param1' => 'value1', 'param2' => 'value2')
5819 )
5820 ));
5821 $expected = array(
5822 'form' => array(
5823 'id' => 'ContactAddForm',
5824 'method' => 'post',
5825 'action' => '/controller/action?param1=value1&amp;param2=value2',
5826 'accept-charset' => $encoding
5827 ),
5828 'div' => array('style' => 'display:none;'),
5829 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5830 '/div'
5831 );
5832 $this->assertTags($result, $expected, true);
5833 }
5834
5835 /**
5836 * test that create() doesn't cause errors by multiple id's being in the primary key
5837 * as could happen with multiple select or checkboxes.
5838 *
5839 * @return void
5840 */
5841 function testCreateWithMultipleIdInData() {
5842 $encoding = strtolower(Configure::read('App.encoding'));
5843
5844 $this->Form->data['Contact']['id'] = array(1, 2);
5845 $result = $this->Form->create('Contact');
5846 $expected = array(
5847 'form' => array(
5848 'id' => 'ContactAddForm',
5849 'method' => 'post',
5850 'action' => '/contacts/add',
5851 'accept-charset' => $encoding
5852 ),
5853 'div' => array('style' => 'display:none;'),
5854 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5855 '/div'
5856 );
5857 $this->assertTags($result, $expected);
5858 }
5859
5860 /**
5861 * test that create() doesn't add in extra passed params.
5862 *
5863 * @return void
5864 */
5865 function testCreatePassedArgs() {
5866 $encoding = strtolower(Configure::read('App.encoding'));
5867 $this->Form->data['Contact']['id'] = 1;
5868 $result = $this->Form->create('Contact', array(
5869 'type' => 'post',
5870 'escape' => false,
5871 'url' => array(
5872 'action' => 'edit',
5873 'myparam'
5874 )
5875 ));
5876 $expected = array(
5877 'form' => array(
5878 'id' => 'ContactAddForm',
5879 'method' => 'post',
5880 'action' => '/contacts/edit/myparam',
5881 'accept-charset' => $encoding
5882 ),
5883 'div' => array('style' => 'display:none;'),
5884 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
5885 '/div'
5886 );
5887 $this->assertTags($result, $expected, true);
5888 }
5889
5890 /**
5891 * test creating a get form, and get form inputs.
5892 *
5893 * @access public
5894 * @return void
5895 */
5896 function testGetFormCreate() {
5897 $encoding = strtolower(Configure::read('App.encoding'));
5898 $result = $this->Form->create('Contact', array('type' => 'get'));
5899 $this->assertTags($result, array('form' => array(
5900 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
5901 'accept-charset' => $encoding
5902 )));
5903
5904 $result = $this->Form->text('Contact.name');
5905 $this->assertTags($result, array('input' => array(
5906 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
5907 )));
5908
5909 $result = $this->Form->password('password');
5910 $this->assertTags($result, array('input' => array(
5911 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
5912 )));
5913 $this->assertNoPattern('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
5914
5915 $result = $this->Form->text('user_form');
5916 $this->assertTags($result, array('input' => array(
5917 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
5918 )));
5919 }
5920
5921 /**
5922 * test get form, and inputs when the model param is false
5923 *
5924 * @return void
5925 */
5926 function testGetFormWithFalseModel() {
5927 $encoding = strtolower(Configure::read('App.encoding'));
5928 $result = $this->Form->create(false, array('type' => 'get'));
5929
5930 $expected = array('form' => array(
5931 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
5932 'accept-charset' => $encoding
5933 ));
5934 $this->assertTags($result, $expected);
5935
5936 $result = $this->Form->text('reason');
5937 $expected = array(
5938 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
5939 );
5940 $this->assertTags($result, $expected);
5941 }
5942
5943 /**
5944 * test that datetime() works with GET style forms.
5945 *
5946 * @return void
5947 */
5948 function testDateTimeWithGetForms() {
5949 extract($this->dateRegex);
5950 $this->Form->create('Contact', array('type' => 'get'));
5951 $result = $this->Form->datetime('created');
5952
5953 $this->assertPattern('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
5954 $this->assertPattern('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
5955 $this->assertPattern('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
5956 $this->assertPattern('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
5957 $this->assertPattern('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
5958 $this->assertPattern('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
5959 }
5960 /**
5961 * testEditFormWithData method
5962 *
5963 * test auto populating form elements from submitted data.
5964 *
5965 * @access public
5966 * @return void
5967 */
5968 function testEditFormWithData() {
5969 $this->Form->data = array('Person' => array(
5970 'id' => 1,
5971 'first_name' => 'Nate',
5972 'last_name' => 'Abele',
5973 'email' => 'nate@example.com'
5974 ));
5975 $this->Form->params = array('models' => array('Person'), 'controller' => 'people', 'action' => 'add');
5976 $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
5977
5978 $this->Form->create();
5979 $result = $this->Form->select('People.People', $options, null, array('multiple' => true));
5980 $expected = array(
5981 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
5982 'select' => array(
5983 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
5984 ),
5985 array('option' => array('value' => 1)), 'Nate', '/option',
5986 array('option' => array('value' => 2)), 'Garrett', '/option',
5987 array('option' => array('value' => 3)), 'Larry', '/option',
5988 '/select'
5989 );
5990 $this->assertTags($result, $expected);
5991 }
5992
5993 /**
5994 * testFormMagicInput method
5995 *
5996 * @access public
5997 * @return void
5998 */
5999 function testFormMagicInput() {
6000 $encoding = strtolower(Configure::read('App.encoding'));
6001 $result = $this->Form->create('Contact');
6002 $expected = array(
6003 'form' => array(
6004 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
6005 'accept-charset' => $encoding
6006 ),
6007 'div' => array('style' => 'display:none;'),
6008 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
6009 '/div'
6010 );
6011 $this->assertTags($result, $expected);
6012
6013 $result = $this->Form->input('name');
6014 $expected = array(
6015 'div' => array('class' => 'input text'),
6016 'label' => array('for' => 'ContactName'),
6017 'Name',
6018 '/label',
6019 'input' => array(
6020 'type' => 'text', 'name' => 'data[Contact][name]',
6021 'id' => 'ContactName', 'maxlength' => '255'
6022 ),
6023 '/div'
6024 );
6025 $this->assertTags($result, $expected);
6026
6027 $result = $this->Form->input('non_existing_field_in_contact_model');
6028 $expected = array(
6029 'div' => array('class' => 'input text'),
6030 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
6031 'Non Existing Field In Contact Model',
6032 '/label',
6033 'input' => array(
6034 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
6035 'id' => 'ContactNonExistingFieldInContactModel'
6036 ),
6037 '/div'
6038 );
6039 $this->assertTags($result, $expected);
6040
6041 $result = $this->Form->input('Address.street');
6042 $expected = array(
6043 'div' => array('class' => 'input text'),
6044 'label' => array('for' => 'AddressStreet'),
6045 'Street',
6046 '/label',
6047 'input' => array(
6048 'type' => 'text', 'name' => 'data[Address][street]',
6049 'id' => 'AddressStreet'
6050 ),
6051 '/div'
6052 );
6053 $this->assertTags($result, $expected);
6054
6055 $result = $this->Form->input('Address.non_existing_field_in_model');
6056 $expected = array(
6057 'div' => array('class' => 'input text'),
6058 'label' => array('for' => 'AddressNonExistingFieldInModel'),
6059 'Non Existing Field In Model',
6060 '/label',
6061 'input' => array(
6062 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
6063 'id' => 'AddressNonExistingFieldInModel'
6064 ),
6065 '/div'
6066 );
6067 $this->assertTags($result, $expected);
6068
6069 $result = $this->Form->input('name', array('div' => false));
6070 $expected = array(
6071 'label' => array('for' => 'ContactName'),
6072 'Name',
6073 '/label',
6074 'input' => array(
6075 'type' => 'text', 'name' => 'data[Contact][name]',
6076 'id' => 'ContactName', 'maxlength' => '255'
6077 )
6078 );
6079 $this->assertTags($result, $expected);
6080
6081 $result = $this->Form->input('Contact.non_existing');
6082 $expected = array(
6083 'div' => array('class' => 'input text required'),
6084 'label' => array('for' => 'ContactNonExisting'),
6085 'Non Existing',
6086 '/label',
6087 'input' => array(
6088 'type' => 'text', 'name' => 'data[Contact][non_existing]',
6089 'id' => 'ContactNonExisting'
6090 ),
6091 '/div'
6092 );
6093 $this->assertTags($result, $expected);
6094
6095 $result = $this->Form->input('Contact.imrequired');
6096 $expected = array(
6097 'div' => array('class' => 'input text required'),
6098 'label' => array('for' => 'ContactImrequired'),
6099 'Imrequired',
6100 '/label',
6101 'input' => array(
6102 'type' => 'text', 'name' => 'data[Contact][imrequired]',
6103 'id' => 'ContactImrequired'
6104 ),
6105 '/div'
6106 );
6107 $this->assertTags($result, $expected);
6108
6109 $result = $this->Form->input('Contact.imalsorequired');
6110 $expected = array(
6111 'div' => array('class' => 'input text required'),
6112 'label' => array('for' => 'ContactImalsorequired'),
6113 'Imalsorequired',
6114 '/label',
6115 'input' => array(
6116 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
6117 'id' => 'ContactImalsorequired'
6118 ),
6119 '/div'
6120 );
6121 $this->assertTags($result, $expected);
6122
6123 $result = $this->Form->input('Contact.imrequiredtoo');
6124 $expected = array(
6125 'div' => array('class' => 'input text required'),
6126 'label' => array('for' => 'ContactImrequiredtoo'),
6127 'Imrequiredtoo',
6128 '/label',
6129 'input' => array(
6130 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
6131 'id' => 'ContactImrequiredtoo'
6132 ),
6133 '/div'
6134 );
6135 $this->assertTags($result, $expected);
6136
6137 $result = $this->Form->input('Contact.required_one');
6138 $expected = array(
6139 'div' => array('class' => 'input text required'),
6140 'label' => array('for' => 'ContactRequiredOne'),
6141 'Required One',
6142 '/label',
6143 'input' => array(
6144 'type' => 'text', 'name' => 'data[Contact][required_one]',
6145 'id' => 'ContactRequiredOne'
6146 ),
6147 '/div'
6148 );
6149 $this->assertTags($result, $expected);
6150
6151 $result = $this->Form->input('Contact.imnotrequired');
6152 $expected = array(
6153 'div' => array('class' => 'input text'),
6154 'label' => array('for' => 'ContactImnotrequired'),
6155 'Imnotrequired',
6156 '/label',
6157 'input' => array(
6158 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
6159 'id' => 'ContactImnotrequired'
6160 ),
6161 '/div'
6162 );
6163 $this->assertTags($result, $expected);
6164
6165 $result = $this->Form->input('Contact.imalsonotrequired');
6166 $expected = array(
6167 'div' => array('class' => 'input text'),
6168 'label' => array('for' => 'ContactImalsonotrequired'),
6169 'Imalsonotrequired',
6170 '/label',
6171 'input' => array(
6172 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
6173 'id' => 'ContactImalsonotrequired'
6174 ),
6175 '/div'
6176 );
6177 $this->assertTags($result, $expected);
6178
6179 $result = $this->Form->input('Contact.imnotrequiredeither');
6180 $expected = array(
6181 'div' => array('class' => 'input text'),
6182 'label' => array('for' => 'ContactImnotrequiredeither'),
6183 'Imnotrequiredeither',
6184 '/label',
6185 'input' => array(
6186 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
6187 'id' => 'ContactImnotrequiredeither'
6188 ),
6189 '/div'
6190 );
6191 $this->assertTags($result, $expected);
6192
6193 extract($this->dateRegex);
6194 $now = strtotime('now');
6195
6196 $result = $this->Form->input('Contact.published', array('div' => false));
6197 $expected = array(
6198 'label' => array('for' => 'ContactPublishedMonth'),
6199 'Published',
6200 '/label',
6201 array('select' => array(
6202 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
6203 )),
6204 $monthsRegex,
6205 array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6206 date('F', $now),
6207 '/option',
6208 '*/select',
6209 '-',
6210 array('select' => array(
6211 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
6212 )),
6213 $daysRegex,
6214 array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6215 date('j', $now),
6216 '/option',
6217 '*/select',
6218 '-',
6219 array('select' => array(
6220 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
6221 )),
6222 $yearsRegex,
6223 array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6224 date('Y', $now),
6225 '*/select'
6226 );
6227 $this->assertTags($result, $expected);
6228
6229 $result = $this->Form->input('Contact.updated', array('div' => false));
6230 $expected = array(
6231 'label' => array('for' => 'ContactUpdatedMonth'),
6232 'Updated',
6233 '/label',
6234 array('select' => array(
6235 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
6236 )),
6237 $monthsRegex,
6238 array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
6239 date('F', $now),
6240 '/option',
6241 '*/select',
6242 '-',
6243 array('select' => array(
6244 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
6245 )),
6246 $daysRegex,
6247 array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
6248 date('j', $now),
6249 '/option',
6250 '*/select',
6251 '-',
6252 array('select' => array(
6253 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
6254 )),
6255 $yearsRegex,
6256 array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
6257 date('Y', $now),
6258 '*/select'
6259 );
6260 $this->assertTags($result, $expected);
6261
6262 $result = $this->Form->input('User.stuff');
6263 $expected = array(
6264 'div' => array('class' => 'input text'),
6265 'label' => array('for' => 'UserStuff'),
6266 'Stuff',
6267 '/label',
6268 'input' => array(
6269 'type' => 'text', 'name' => 'data[User][stuff]',
6270 'id' => 'UserStuff', 'maxlength' => 10
6271 ),
6272 '/div'
6273 );
6274 $this->assertTags($result, $expected, true);
6275 }
6276
6277 /**
6278 * testForMagicInputNonExistingNorValidated method
6279 *
6280 * @access public
6281 * @return void
6282 */
6283 function testForMagicInputNonExistingNorValidated() {
6284 $encoding = strtolower(Configure::read('App.encoding'));
6285 $result = $this->Form->create('Contact');
6286 $expected = array(
6287 'form' => array(
6288 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
6289 'accept-charset' => $encoding
6290 ),
6291 'div' => array('style' => 'display:none;'),
6292 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
6293 '/div'
6294 );
6295 $this->assertTags($result, $expected);
6296
6297 $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
6298 $expected = array(
6299 'label' => array('for' => 'ContactNonExistingNorValidated'),
6300 'Non Existing Nor Validated',
6301 '/label',
6302 'input' => array(
6303 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
6304 'id' => 'ContactNonExistingNorValidated'
6305 )
6306 );
6307 $this->assertTags($result, $expected);
6308
6309 $result = $this->Form->input('Contact.non_existing_nor_validated', array(
6310 'div' => false, 'value' => 'my value'
6311 ));
6312 $expected = array(
6313 'label' => array('for' => 'ContactNonExistingNorValidated'),
6314 'Non Existing Nor Validated',
6315 '/label',
6316 'input' => array(
6317 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
6318 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
6319 )
6320 );
6321 $this->assertTags($result, $expected);
6322
6323 $this->Form->data = array(
6324 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
6325 ));
6326 $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
6327 $expected = array(
6328 'label' => array('for' => 'ContactNonExistingNorValidated'),
6329 'Non Existing Nor Validated',
6330 '/label',
6331 'input' => array(
6332 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
6333 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
6334 )
6335 );
6336 $this->assertTags($result, $expected);
6337 }
6338
6339 /**
6340 * testFormMagicInputLabel method
6341 *
6342 * @access public
6343 * @return void
6344 */
6345 function testFormMagicInputLabel() {
6346 $encoding = strtolower(Configure::read('App.encoding'));
6347 $result = $this->Form->create('Contact');
6348 $expected = array(
6349 'form' => array(
6350 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
6351 'accept-charset' => $encoding
6352 ),
6353 'div' => array('style' => 'display:none;'),
6354 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
6355 '/div'
6356 );
6357 $this->assertTags($result, $expected);
6358
6359 $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
6360 $this->assertTags($result, array('input' => array(
6361 'name' => 'data[Contact][name]', 'type' => 'text',
6362 'id' => 'ContactName', 'maxlength' => '255')
6363 ));
6364
6365 $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
6366 $expected = array(
6367 'label' => array('for' => 'ContactName'),
6368 'My label',
6369 '/label',
6370 'input' => array(
6371 'type' => 'text', 'name' => 'data[Contact][name]',
6372 'id' => 'ContactName', 'maxlength' => '255'
6373 )
6374 );
6375 $this->assertTags($result, $expected);
6376
6377 $result = $this->Form->input('Contact.name', array(
6378 'div' => false, 'label' => array('class' => 'mandatory')
6379 ));
6380 $expected = array(
6381 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
6382 'Name',
6383 '/label',
6384 'input' => array(
6385 'type' => 'text', 'name' => 'data[Contact][name]',
6386 'id' => 'ContactName', 'maxlength' => '255'
6387 )
6388 );
6389 $this->assertTags($result, $expected);
6390
6391 $result = $this->Form->input('Contact.name', array(
6392 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
6393 ));
6394 $expected = array(
6395 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
6396 'My label',
6397 '/label',
6398 'input' => array(
6399 'type' => 'text', 'name' => 'data[Contact][name]',
6400 'id' => 'ContactName', 'maxlength' => '255'
6401 )
6402 );
6403 $this->assertTags($result, $expected);
6404
6405 $result = $this->Form->input('Contact.name', array(
6406 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
6407 ));
6408 $expected = array(
6409 'label' => array('for' => 'my_id'),
6410 'Name',
6411 '/label',
6412 'input' => array(
6413 'type' => 'text', 'name' => 'data[Contact][name]',
6414 'id' => 'my_id', 'maxlength' => '255'
6415 )
6416 );
6417 $this->assertTags($result, $expected);
6418
6419 $result = $this->Form->input('1.id');
6420 $this->assertTags($result, array('input' => array(
6421 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
6422 'id' => 'Contact1Id'
6423 )));
6424
6425 $result = $this->Form->input("1.name");
6426 $expected = array(
6427 'div' => array('class' => 'input text'),
6428 'label' => array('for' => 'Contact1Name'),
6429 'Name',
6430 '/label',
6431 'input' => array(
6432 'type' => 'text', 'name' => 'data[Contact][1][name]',
6433 'id' => 'Contact1Name', 'maxlength' => '255'
6434 ),
6435 '/div'
6436 );
6437 $this->assertTags($result, $expected);
6438
6439 $result = $this->Form->input('Contact.1.id');
6440 $this->assertTags($result, array(
6441 'input' => array(
6442 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
6443 'id' => 'Contact1Id'
6444 )
6445 ));
6446
6447 $result = $this->Form->input("Model.1.name");
6448 $expected = array(
6449 'div' => array('class' => 'input text'),
6450 'label' => array('for' => 'Model1Name'),
6451 'Name',
6452 '/label',
6453 'input' => array(
6454 'type' => 'text', 'name' => 'data[Model][1][name]',
6455 'id' => 'Model1Name'
6456 ),
6457 '/div'
6458 );
6459 $this->assertTags($result, $expected);
6460 }
6461
6462 /**
6463 * testFormEnd method
6464 *
6465 * @access public
6466 * @return void
6467 */
6468 function testFormEnd() {
6469 $this->assertEqual($this->Form->end(), '</form>');
6470
6471 $result = $this->Form->end('');
6472 $expected = array(
6473 'div' => array('class' => 'submit'),
6474 'input' => array('type' => 'submit', 'value' => ''),
6475 '/div',
6476 '/form'
6477 );
6478 $this->assertTags($result, $expected);
6479
6480 $result = $this->Form->end(array('label' => ''));
6481 $expected = array(
6482 'div' => array('class' => 'submit'),
6483 'input' => array('type' => 'submit', 'value' => ''),
6484 '/div',
6485 '/form'
6486 );
6487 $this->assertTags($result, $expected);
6488
6489 $result = $this->Form->end('save');
6490 $expected = array(
6491 'div' => array('class' => 'submit'),
6492 'input' => array('type' => 'submit', 'value' => 'save'),
6493 '/div',
6494 '/form'
6495 );
6496 $this->assertTags($result, $expected);
6497
6498 $result = $this->Form->end(array('label' => 'save'));
6499 $expected = array(
6500 'div' => array('class' => 'submit'),
6501 'input' => array('type' => 'submit', 'value' => 'save'),
6502 '/div',
6503 '/form'
6504 );
6505 $this->assertTags($result, $expected);
6506
6507 $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
6508 $expected = array(
6509 'div' => array('class' => 'submit'),
6510 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
6511 '/div',
6512 '/form'
6513 );
6514 $this->assertTags($result, $expected);
6515
6516 $result = $this->Form->end(array('name' => 'Whatever'));
6517 $expected = array(
6518 'div' => array('class' => 'submit'),
6519 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
6520 '/div',
6521 '/form'
6522 );
6523 $this->assertTags($result, $expected);
6524
6525 $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
6526 $expected = array(
6527 'div' => array('class' => 'good'),
6528 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
6529 '/div',
6530 '/form'
6531 );
6532 $this->assertTags($result, $expected);
6533
6534 $result = $this->Form->end(array(
6535 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
6536 ));
6537 $expected = array(
6538 'div' => array('class' => 'good'),
6539 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
6540 '/div',
6541 '/form'
6542 );
6543 $this->assertTags($result, $expected);
6544 }
6545
6546 /**
6547 * testMultipleFormWithIdFields method
6548 *
6549 * @access public
6550 * @return void
6551 */
6552 function testMultipleFormWithIdFields() {
6553 $this->Form->create('UserForm');
6554
6555 $result = $this->Form->input('id');
6556 $this->assertTags($result, array('input' => array(
6557 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
6558 )));
6559
6560 $result = $this->Form->input('ValidateItem.id');
6561 $this->assertTags($result, array('input' => array(
6562 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
6563 'id' => 'ValidateItemId'
6564 )));
6565
6566 $result = $this->Form->input('ValidateUser.id');
6567 $this->assertTags($result, array('input' => array(
6568 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
6569 'id' => 'ValidateUserId'
6570 )));
6571 }
6572
6573 /**
6574 * testDbLessModel method
6575 *
6576 * @access public
6577 * @return void
6578 */
6579 function testDbLessModel() {
6580 $this->Form->create('TestMail');
6581
6582 $result = $this->Form->input('name');
6583 $expected = array(
6584 'div' => array('class' => 'input text'),
6585 'label' => array('for' => 'TestMailName'),
6586 'Name',
6587 '/label',
6588 'input' => array(
6589 'name' => 'data[TestMail][name]', 'type' => 'text',
6590 'id' => 'TestMailName'
6591 ),
6592 '/div'
6593 );
6594 $this->assertTags($result, $expected);
6595
6596 ClassRegistry::init('TestMail');
6597 $this->Form->create('TestMail');
6598 $result = $this->Form->input('name');
6599 $expected = array(
6600 'div' => array('class' => 'input text'),
6601 'label' => array('for' => 'TestMailName'),
6602 'Name',
6603 '/label',
6604 'input' => array(
6605 'name' => 'data[TestMail][name]', 'type' => 'text',
6606 'id' => 'TestMailName'
6607 ),
6608 '/div'
6609 );
6610 $this->assertTags($result, $expected);
6611 }
6612
6613 /**
6614 * testBrokenness method
6615 *
6616 * @access public
6617 * @return void
6618 */
6619 function testBrokenness() {
6620 /*
6621 * #4 This test has two parents and four children. By default (as of r7117) both
6622 * parents are show but the first parent is missing a child. This is the inconsistency
6623 * in the default behaviour - one parent has all children, the other does not - dependent
6624 * on the data values.
6625 */
6626 $result = $this->Form->select('Model.field', array(
6627 'Fred' => array(
6628 'freds_son_1' => 'Fred',
6629 'freds_son_2' => 'Freddie'
6630 ),
6631 'Bert' => array(
6632 'berts_son_1' => 'Albert',
6633 'berts_son_2' => 'Bertie')
6634 ),
6635 null,
6636 array('showParents' => true, 'empty' => false)
6637 );
6638
6639 $expected = array(
6640 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
6641 array('optgroup' => array('label' => 'Fred')),
6642 array('option' => array('value' => 'freds_son_1')),
6643 'Fred',
6644 '/option',
6645 array('option' => array('value' => 'freds_son_2')),
6646 'Freddie',
6647 '/option',
6648 '/optgroup',
6649 array('optgroup' => array('label' => 'Bert')),
6650 array('option' => array('value' => 'berts_son_1')),
6651 'Albert',
6652 '/option',
6653 array('option' => array('value' => 'berts_son_2')),
6654 'Bertie',
6655 '/option',
6656 '/optgroup',
6657 '/select'
6658 );
6659 $this->assertTags($result, $expected);
6660
6661 /*
6662 * #2 This is structurally identical to the test above (#1) - only the parent name has
6663 * changed, so we should expect the same select list data, just with a different name
6664 * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
6665 * This is where data corruption can occur, because when a select value is missing from
6666 * a list a form will substitute the first value in the list - without the user knowing.
6667 * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
6668 * affect the availability of 3 => 'Three' as a valid option.
6669 */
6670 $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
6671 3 => 'Three', 4 => 'Four', 5 => 'Five'
6672 ));
6673 $result = $this->Form->select(
6674 'Model.field', $options, null, array('showParents' => true, 'empty' => false)
6675 );
6676
6677 $expected = array(
6678 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
6679 array('option' => array('value' => 1)),
6680 'One',
6681 '/option',
6682 array('option' => array('value' => 2)),
6683 'Two',
6684 '/option',
6685 array('optgroup' => array('label' => 'Three')),
6686 array('option' => array('value' => 3)),
6687 'Three',
6688 '/option',
6689 array('option' => array('value' => 4)),
6690 'Four',
6691 '/option',
6692 array('option' => array('value' => 5)),
6693 'Five',
6694 '/option',
6695 '/optgroup',
6696 '/select'
6697 );
6698 $this->assertTags($result, $expected);
6699 }
6700
6701 /**
6702 * Test the generation of fields for a multi record form.
6703 *
6704 * @return void
6705 */
6706 function testMultiRecordForm() {
6707 $this->Form->create('ValidateProfile');
6708 $this->Form->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
6709 $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
6710 $expected = array(
6711 'div' => array('class' => 'input textarea'),
6712 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
6713 'Name',
6714 '/label',
6715 'textarea' => array(
6716 'id' => 'ValidateProfile1ValidateItem2Name',
6717 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
6718 'cols' => 30,
6719 'rows' => 6
6720 ),
6721 'Value',
6722 '/textarea',
6723 '/div'
6724 );
6725 $this->assertTags($result, $expected);
6726
6727 $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
6728 $expected = array(
6729 'div' => array('class' => 'input date'),
6730 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
6731 'Created',
6732 '/label',
6733 array('select' => array(
6734 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
6735 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
6736 )
6737 ),
6738 array('option' => array('value' => '')), '/option',
6739 $this->dateRegex['monthsRegex'],
6740 '/select', '-',
6741 array('select' => array(
6742 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
6743 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
6744 )
6745 ),
6746 array('option' => array('value' => '')), '/option',
6747 $this->dateRegex['daysRegex'],
6748 '/select', '-',
6749 array('select' => array(
6750 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
6751 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
6752 )
6753 ),
6754 array('option' => array('value' => '')), '/option',
6755 $this->dateRegex['yearsRegex'],
6756 '/select',
6757 '/div'
6758 );
6759 $this->assertTags($result, $expected);
6760
6761 $this->Form->validationErrors['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = 'Error';
6762 $this->Form->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
6763 $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
6764 $expected = array(
6765 'div' => array('class' => 'input select error'),
6766 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
6767 'Profile',
6768 '/label',
6769 'select' => array(
6770 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
6771 'id' => 'ValidateProfile1ValidateItem2ProfileId',
6772 'class' => 'form-error'
6773 ),
6774 '/select',
6775 array('div' => array('class' => 'error-message')),
6776 'Error',
6777 '/div',
6778 '/div'
6779 );
6780 $this->assertTags($result, $expected);
6781 }
6782
6783 /**
6784 * test the correct display of multi-record form validation errors.
6785 *
6786 * @return void
6787 */
6788 function testMultiRecordFormValidationErrors() {
6789 $this->Form->create('ValidateProfile');
6790 $this->Form->validationErrors['ValidateProfile'][2]['ValidateItem'][1]['name'] = 'Error in field name';
6791 $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
6792 $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
6793
6794 $this->Form->validationErrors['ValidateProfile'][2]['city'] = 'Error in field city';
6795 $result = $this->Form->error('ValidateProfile.2.city');
6796 $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
6797
6798 $result = $this->Form->error('2.city');
6799 $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
6800 }
6801
6802 /**
6803 * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
6804 *
6805 * @return void
6806 */
6807 function testInputTemplate() {
6808 $result = $this->Form->input('Contact.email', array(
6809 'type' => 'text', 'format' => array('input')
6810 ));
6811 $expected = array(
6812 'div' => array('class' => 'input text'),
6813 'input' => array(
6814 'type' => 'text', 'name' => 'data[Contact][email]',
6815 'id' => 'ContactEmail'
6816 ),
6817 '/div'
6818 );
6819 $this->assertTags($result, $expected);
6820
6821 $result = $this->Form->input('Contact.email', array(
6822 'type' => 'text', 'format' => array('input', 'label'),
6823 'label' => '<em>Email (required)</em>'
6824 ));
6825 $expected = array(
6826 'div' => array('class' => 'input text'),
6827 array('input' => array(
6828 'type' => 'text', 'name' => 'data[Contact][email]',
6829 'id' => 'ContactEmail'
6830 )),
6831 'label' => array('for' => 'ContactEmail'),
6832 'em' => array(),
6833 'Email (required)',
6834 '/em',
6835 '/label',
6836 '/div'
6837 );
6838 $this->assertTags($result, $expected);
6839
6840 $result = $this->Form->input('Contact.email', array(
6841 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
6842 'between' => '<div>Something in the middle</div>',
6843 'after' => '<span>Some text at the end</span>'
6844 ));
6845 $expected = array(
6846 'div' => array('class' => 'input text'),
6847 array('input' => array(
6848 'type' => 'text', 'name' => 'data[Contact][email]',
6849 'id' => 'ContactEmail'
6850 )),
6851 array('div' => array()),
6852 'Something in the middle',
6853 '/div',
6854 'label' => array('for' => 'ContactEmail'),
6855 'Email',
6856 '/label',
6857 'span' => array(),
6858 'Some text at the end',
6859 '/span',
6860 '/div'
6861 );
6862 $this->assertTags($result, $expected);
6863 }
6864 }