PageRenderTime 73ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 2ms

/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Test/Case/View/Helper/FormHelperTest.php

https://bitbucket.org/daveschwan/ronin-group
PHP | 9627 lines | 7686 code | 752 blank | 1189 comment | 1 complexity | 948e0af78c32a78cea5e576038a4fcea MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View.Helper
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ClassRegistry', 'Utility');
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('Model', 'Model');
  22. App::uses('Security', 'Utility');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. App::uses('Router', 'Routing');
  27. /**
  28. * ContactTestController class
  29. *
  30. * @package Cake.Test.Case.View.Helper
  31. */
  32. class ContactTestController extends Controller {
  33. /**
  34. * uses property
  35. *
  36. * @var mixed null
  37. */
  38. public $uses = null;
  39. }
  40. /**
  41. * Contact class
  42. *
  43. * @package Cake.Test.Case.View.Helper
  44. */
  45. class Contact extends CakeTestModel {
  46. /**
  47. * useTable property
  48. *
  49. * @var boolean
  50. */
  51. public $useTable = false;
  52. /**
  53. * Default schema
  54. *
  55. * @var array
  56. */
  57. protected $_schema = array(
  58. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  59. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  60. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  64. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  65. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  66. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  67. );
  68. /**
  69. * validate property
  70. *
  71. * @var array
  72. */
  73. public $validate = array(
  74. 'non_existing' => array(),
  75. 'idontexist' => array(),
  76. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  77. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  78. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  79. 'imrequiredonboth' => array(
  80. 'required' => array('rule' => 'alphaNumeric'),
  81. ),
  82. 'string_required' => 'notEmpty',
  83. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  84. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  85. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  86. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  87. 'imalsonotrequired' => array(
  88. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imalsonotrequired2' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  94. ),
  95. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. 'iamrequiredalways' => array(
  97. 'email' => array('rule' => 'email'),
  98. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  99. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  100. ),
  101. 'boolean_field' => array('rule' => 'boolean')
  102. );
  103. /**
  104. * schema method
  105. *
  106. * @return void
  107. */
  108. public function setSchema($schema) {
  109. $this->_schema = $schema;
  110. }
  111. /**
  112. * hasAndBelongsToMany property
  113. *
  114. * @var array
  115. */
  116. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $belongsTo = array('User' => array('className' => 'UserForm'));
  123. }
  124. /**
  125. * ContactTagsContact class
  126. *
  127. * @package Cake.Test.Case.View.Helper
  128. */
  129. class ContactTagsContact extends CakeTestModel {
  130. /**
  131. * useTable property
  132. *
  133. * @var boolean
  134. */
  135. public $useTable = false;
  136. /**
  137. * Default schema
  138. *
  139. * @var array
  140. */
  141. protected $_schema = array(
  142. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  143. 'contact_tag_id' => array(
  144. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  145. )
  146. );
  147. /**
  148. * schema method
  149. *
  150. * @return void
  151. */
  152. public function setSchema($schema) {
  153. $this->_schema = $schema;
  154. }
  155. }
  156. /**
  157. * ContactNonStandardPk class
  158. *
  159. * @package Cake.Test.Case.View.Helper
  160. */
  161. class ContactNonStandardPk extends Contact {
  162. /**
  163. * primaryKey property
  164. *
  165. * @var string
  166. */
  167. public $primaryKey = 'pk';
  168. /**
  169. * schema method
  170. *
  171. * @return void
  172. */
  173. public function schema($field = false) {
  174. $this->_schema = parent::schema();
  175. $this->_schema['pk'] = $this->_schema['id'];
  176. unset($this->_schema['id']);
  177. return $this->_schema;
  178. }
  179. }
  180. /**
  181. * ContactTag class
  182. *
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactTag extends Model {
  186. /**
  187. * useTable property
  188. *
  189. * @var boolean
  190. */
  191. public $useTable = false;
  192. /**
  193. * schema definition
  194. *
  195. * @var array
  196. */
  197. protected $_schema = array(
  198. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  199. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  200. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  201. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  202. );
  203. }
  204. /**
  205. * UserForm class
  206. *
  207. * @package Cake.Test.Case.View.Helper
  208. */
  209. class UserForm extends CakeTestModel {
  210. /**
  211. * useTable property
  212. *
  213. * @var boolean
  214. */
  215. public $useTable = false;
  216. /**
  217. * hasMany property
  218. *
  219. * @var array
  220. */
  221. public $hasMany = array(
  222. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  223. ));
  224. /**
  225. * schema definition
  226. *
  227. * @var array
  228. */
  229. protected $_schema = array(
  230. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  231. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  232. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  233. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  234. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  235. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  236. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  237. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  238. );
  239. }
  240. /**
  241. * OpenidUrl class
  242. *
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class OpenidUrl extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var boolean
  250. */
  251. public $useTable = false;
  252. /**
  253. * belongsTo property
  254. *
  255. * @var array
  256. */
  257. public $belongsTo = array('UserForm' => array(
  258. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  259. ));
  260. /**
  261. * validate property
  262. *
  263. * @var array
  264. */
  265. public $validate = array('openid_not_registered' => array());
  266. /**
  267. * schema method
  268. *
  269. * @var array
  270. */
  271. protected $_schema = array(
  272. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  273. 'user_form_id' => array(
  274. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  275. ),
  276. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  277. );
  278. /**
  279. * beforeValidate method
  280. *
  281. * @return void
  282. */
  283. public function beforeValidate($options = array()) {
  284. $this->invalidate('openid_not_registered');
  285. return true;
  286. }
  287. }
  288. /**
  289. * ValidateUser class
  290. *
  291. * @package Cake.Test.Case.View.Helper
  292. */
  293. class ValidateUser extends CakeTestModel {
  294. /**
  295. * useTable property
  296. *
  297. * @var boolean
  298. */
  299. public $useTable = false;
  300. /**
  301. * hasOne property
  302. *
  303. * @var array
  304. */
  305. public $hasOne = array('ValidateProfile' => array(
  306. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  307. ));
  308. /**
  309. * schema method
  310. *
  311. * @var array
  312. */
  313. protected $_schema = array(
  314. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  315. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  316. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  317. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  318. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  319. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  320. );
  321. /**
  322. * beforeValidate method
  323. *
  324. * @return void
  325. */
  326. public function beforeValidate($options = array()) {
  327. $this->invalidate('email');
  328. return false;
  329. }
  330. }
  331. /**
  332. * ValidateProfile class
  333. *
  334. * @package Cake.Test.Case.View.Helper
  335. */
  336. class ValidateProfile extends CakeTestModel {
  337. /**
  338. * useTable property
  339. *
  340. * @var boolean
  341. */
  342. public $useTable = false;
  343. /**
  344. * schema property
  345. *
  346. * @var array
  347. */
  348. protected $_schema = array(
  349. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  350. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  351. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  352. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  353. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  354. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  355. );
  356. /**
  357. * hasOne property
  358. *
  359. * @var array
  360. */
  361. public $hasOne = array('ValidateItem' => array(
  362. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  363. ));
  364. /**
  365. * belongsTo property
  366. *
  367. * @var array
  368. */
  369. public $belongsTo = array('ValidateUser' => array(
  370. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  371. ));
  372. /**
  373. * beforeValidate method
  374. *
  375. * @return void
  376. */
  377. public function beforeValidate($options = array()) {
  378. $this->invalidate('full_name');
  379. $this->invalidate('city');
  380. return false;
  381. }
  382. }
  383. /**
  384. * ValidateItem class
  385. *
  386. * @package Cake.Test.Case.View.Helper
  387. */
  388. class ValidateItem extends CakeTestModel {
  389. /**
  390. * useTable property
  391. *
  392. * @var boolean
  393. */
  394. public $useTable = false;
  395. /**
  396. * schema property
  397. *
  398. * @var array
  399. */
  400. protected $_schema = array(
  401. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  402. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  403. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  404. 'description' => array(
  405. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  406. ),
  407. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  408. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  409. );
  410. /**
  411. * belongsTo property
  412. *
  413. * @var array
  414. */
  415. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  416. /**
  417. * beforeValidate method
  418. *
  419. * @return void
  420. */
  421. public function beforeValidate($options = array()) {
  422. $this->invalidate('description');
  423. return false;
  424. }
  425. }
  426. /**
  427. * TestMail class
  428. *
  429. * @package Cake.Test.Case.View.Helper
  430. */
  431. class TestMail extends CakeTestModel {
  432. /**
  433. * useTable property
  434. *
  435. * @var boolean
  436. */
  437. public $useTable = false;
  438. }
  439. /**
  440. * FormHelperTest class
  441. *
  442. * @package Cake.Test.Case.View.Helper
  443. * @property FormHelper $Form
  444. */
  445. class FormHelperTest extends CakeTestCase {
  446. /**
  447. * Fixtures to be used
  448. *
  449. * @var array
  450. */
  451. public $fixtures = array('core.post');
  452. /**
  453. * Do not load the fixtures by default
  454. *
  455. * @var boolean
  456. */
  457. public $autoFixtures = false;
  458. /**
  459. * setUp method
  460. *
  461. * @return void
  462. */
  463. public function setUp() {
  464. parent::setUp();
  465. Configure::write('Config.language', 'eng');
  466. Configure::write('App.base', '');
  467. Configure::delete('Asset');
  468. $this->Controller = new ContactTestController();
  469. $this->View = new View($this->Controller);
  470. $this->Form = new FormHelper($this->View);
  471. $this->Form->Html = new HtmlHelper($this->View);
  472. $this->Form->request = new CakeRequest('contacts/add', false);
  473. $this->Form->request->here = '/contacts/add';
  474. $this->Form->request['action'] = 'add';
  475. $this->Form->request->webroot = '';
  476. $this->Form->request->base = '';
  477. ClassRegistry::addObject('Contact', new Contact());
  478. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  479. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  480. ClassRegistry::addObject('User', new UserForm());
  481. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  482. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  483. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  484. $this->oldSalt = Configure::read('Security.salt');
  485. $this->dateRegex = array(
  486. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  487. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  488. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  491. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  492. );
  493. Configure::write('Security.salt', 'foo!');
  494. }
  495. /**
  496. * tearDown method
  497. *
  498. * @return void
  499. */
  500. public function tearDown() {
  501. parent::tearDown();
  502. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  503. Configure::write('Security.salt', $this->oldSalt);
  504. }
  505. /**
  506. * testFormCreateWithSecurity method
  507. *
  508. * Test form->create() with security key.
  509. *
  510. * @return void
  511. */
  512. public function testCreateWithSecurity() {
  513. $this->Form->request['_Token'] = array('key' => 'testKey');
  514. $encoding = strtolower(Configure::read('App.encoding'));
  515. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  516. $expected = array(
  517. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  518. 'div' => array('style' => 'display:none;'),
  519. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  520. array('input' => array(
  521. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  522. )),
  523. '/div'
  524. );
  525. $this->assertTags($result, $expected);
  526. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  527. $expected['form']['id'] = 'MyForm';
  528. $this->assertTags($result, $expected);
  529. }
  530. /**
  531. * testFormCreateGetNoSecurity method
  532. *
  533. * Test form->create() with no security key as its a get form
  534. *
  535. * @return void
  536. */
  537. public function testCreateEndGetNoSecurity() {
  538. $this->Form->request['_Token'] = array('key' => 'testKey');
  539. $encoding = strtolower(Configure::read('App.encoding'));
  540. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  541. $this->assertNotContains('Token', $result);
  542. $result = $this->Form->end('Save');
  543. $this->assertNotContains('Token', $result);
  544. }
  545. /**
  546. * test that create() clears the fields property so it starts fresh
  547. *
  548. * @return void
  549. */
  550. public function testCreateClearingFields() {
  551. $this->Form->fields = array('model_id');
  552. $this->Form->create('Contact');
  553. $this->assertEquals(array(), $this->Form->fields);
  554. }
  555. /**
  556. * Tests form hash generation with model-less data
  557. *
  558. * @return void
  559. */
  560. public function testValidateHashNoModel() {
  561. $this->Form->request['_Token'] = array('key' => 'foo');
  562. $result = $this->Form->secure(array('anything'));
  563. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  564. }
  565. /**
  566. * Tests that models with identical field names get resolved properly
  567. *
  568. * @return void
  569. */
  570. public function testDuplicateFieldNameResolution() {
  571. $result = $this->Form->create('ValidateUser');
  572. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  573. $result = $this->Form->input('ValidateItem.name');
  574. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  575. $result = $this->Form->input('ValidateUser.name');
  576. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  577. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  578. $this->assertRegExp('/type="text"/', $result);
  579. $result = $this->Form->input('ValidateItem.name');
  580. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  581. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  582. $this->assertRegExp('/<textarea/', $result);
  583. $result = $this->Form->input('name');
  584. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  585. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  586. $this->assertRegExp('/type="text"/', $result);
  587. }
  588. /**
  589. * Tests that hidden fields generated for checkboxes don't get locked
  590. *
  591. * @return void
  592. */
  593. public function testNoCheckboxLocking() {
  594. $this->Form->request['_Token'] = array('key' => 'foo');
  595. $this->assertSame(array(), $this->Form->fields);
  596. $this->Form->checkbox('check', array('value' => '1'));
  597. $this->assertSame($this->Form->fields, array('check'));
  598. }
  599. /**
  600. * testFormSecurityFields method
  601. *
  602. * Test generation of secure form hash generation.
  603. *
  604. * @return void
  605. */
  606. public function testFormSecurityFields() {
  607. $key = 'testKey';
  608. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  609. $this->Form->request['_Token'] = array('key' => $key);
  610. $result = $this->Form->secure($fields);
  611. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  612. $hash .= ':' . 'Model.valid';
  613. $hash = urlencode($hash);
  614. $expected = array(
  615. 'div' => array('style' => 'display:none;'),
  616. array('input' => array(
  617. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  618. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  619. )),
  620. array('input' => array(
  621. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  622. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  623. )),
  624. '/div'
  625. );
  626. $this->assertTags($result, $expected);
  627. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  628. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  629. $result = $this->Form->secure($fields);
  630. $expected = array(
  631. 'div' => array('class' => 'hidden'),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  634. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  635. )),
  636. array('input' => array(
  637. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  638. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  639. )),
  640. '/div'
  641. );
  642. $this->assertTags($result, $expected);
  643. }
  644. /**
  645. * Tests correct generation of number fields for double and float fields
  646. *
  647. * @return void
  648. */
  649. public function testTextFieldGenerationForFloats() {
  650. $model = ClassRegistry::getObject('Contact');
  651. $model->setSchema(array('foo' => array(
  652. 'type' => 'float',
  653. 'null' => false,
  654. 'default' => null,
  655. 'length' => 10
  656. )));
  657. $this->Form->create('Contact');
  658. $result = $this->Form->input('foo');
  659. $expected = array(
  660. 'div' => array('class' => 'input number'),
  661. 'label' => array('for' => 'ContactFoo'),
  662. 'Foo',
  663. '/label',
  664. array('input' => array(
  665. 'type' => 'number',
  666. 'name' => 'data[Contact][foo]',
  667. 'id' => 'ContactFoo',
  668. 'step' => 'any'
  669. )),
  670. '/div'
  671. );
  672. $this->assertTags($result, $expected);
  673. $result = $this->Form->input('foo', array('step' => 0.5));
  674. $expected = array(
  675. 'div' => array('class' => 'input number'),
  676. 'label' => array('for' => 'ContactFoo'),
  677. 'Foo',
  678. '/label',
  679. array('input' => array(
  680. 'type' => 'number',
  681. 'name' => 'data[Contact][foo]',
  682. 'id' => 'ContactFoo',
  683. 'step' => '0.5'
  684. )),
  685. '/div'
  686. );
  687. $this->assertTags($result, $expected);
  688. }
  689. /**
  690. * Tests correct generation of number fields for integer fields
  691. *
  692. * @return void
  693. */
  694. public function testTextFieldTypeNumberGenerationForIntegers() {
  695. $model = ClassRegistry::getObject('Contact');
  696. $model->setSchema(array('foo' => array(
  697. 'type' => 'integer',
  698. 'null' => false,
  699. 'default' => null,
  700. 'length' => null
  701. )));
  702. $this->Form->create('Contact');
  703. $result = $this->Form->input('foo');
  704. $expected = array(
  705. 'div' => array('class' => 'input number'),
  706. 'label' => array('for' => 'ContactFoo'),
  707. 'Foo',
  708. '/label',
  709. array('input' => array(
  710. 'type' => 'number', 'name' => 'data[Contact][foo]',
  711. 'id' => 'ContactFoo'
  712. )),
  713. '/div'
  714. );
  715. $this->assertTags($result, $expected);
  716. }
  717. /**
  718. * testFormSecurityMultipleFields method
  719. *
  720. * Test secure() with multiple row form. Ensure hash is correct.
  721. *
  722. * @return void
  723. */
  724. public function testFormSecurityMultipleFields() {
  725. $key = 'testKey';
  726. $fields = array(
  727. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  728. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  729. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  730. );
  731. $this->Form->request['_Token'] = array('key' => $key);
  732. $result = $this->Form->secure($fields);
  733. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  734. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  735. $expected = array(
  736. 'div' => array('style' => 'display:none;'),
  737. array('input' => array(
  738. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  739. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  740. )),
  741. array('input' => array(
  742. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  743. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  744. )),
  745. '/div'
  746. );
  747. $this->assertTags($result, $expected);
  748. }
  749. /**
  750. * testFormSecurityMultipleSubmitButtons
  751. *
  752. * test form submit generation and ensure that _Token is only created on end()
  753. *
  754. * @return void
  755. */
  756. public function testFormSecurityMultipleSubmitButtons() {
  757. $key = 'testKey';
  758. $this->Form->request['_Token'] = array('key' => $key);
  759. $this->Form->create('Addresses');
  760. $this->Form->input('Address.title');
  761. $this->Form->input('Address.first_name');
  762. $result = $this->Form->submit('Save', array('name' => 'save'));
  763. $expected = array(
  764. 'div' => array('class' => 'submit'),
  765. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  766. '/div',
  767. );
  768. $this->assertTags($result, $expected);
  769. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  770. $expected = array(
  771. 'div' => array('class' => 'submit'),
  772. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  773. '/div',
  774. );
  775. $this->assertTags($result, $expected);
  776. $result = $this->Form->end(null);
  777. $expected = array(
  778. 'div' => array('style' => 'display:none;'),
  779. array('input' => array(
  780. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  781. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  782. )),
  783. array('input' => array(
  784. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  785. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  786. )),
  787. '/div'
  788. );
  789. $this->assertTags($result, $expected);
  790. }
  791. /**
  792. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  793. *
  794. * @return void
  795. */
  796. public function testSecurityButtonNestedNamed() {
  797. $key = 'testKey';
  798. $this->Form->request['_Token'] = array('key' => $key);
  799. $this->Form->create('Addresses');
  800. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  801. $result = $this->Form->unlockField();
  802. $this->assertEquals(array('Address.button'), $result);
  803. }
  804. /**
  805. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  806. *
  807. * @return void
  808. */
  809. public function testSecuritySubmitNestedNamed() {
  810. $key = 'testKey';
  811. $this->Form->request['_Token'] = array('key' => $key);
  812. $this->Form->create('Addresses');
  813. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  814. $result = $this->Form->unlockField();
  815. $this->assertEquals(array('Address.button'), $result);
  816. }
  817. /**
  818. * Test that the correct fields are unlocked for image submits with no names.
  819. *
  820. * @return void
  821. */
  822. public function testSecuritySubmitImageNoName() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('User');
  826. $result = $this->Form->submit('save.png');
  827. $expected = array(
  828. 'div' => array('class' => 'submit'),
  829. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  830. '/div'
  831. );
  832. $this->assertTags($result, $expected);
  833. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  834. }
  835. /**
  836. * Test that the correct fields are unlocked for image submits with names.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitImageName() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('User');
  844. $result = $this->Form->submit('save.png', array('name' => 'test'));
  845. $expected = array(
  846. 'div' => array('class' => 'submit'),
  847. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  848. '/div'
  849. );
  850. $this->assertTags($result, $expected);
  851. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  852. }
  853. /**
  854. * testFormSecurityMultipleInputFields method
  855. *
  856. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  857. *
  858. * @return void
  859. */
  860. public function testFormSecurityMultipleInputFields() {
  861. $key = 'testKey';
  862. $this->Form->request['_Token'] = array('key' => $key);
  863. $this->Form->create('Addresses');
  864. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  865. $this->Form->input('Addresses.0.title');
  866. $this->Form->input('Addresses.0.first_name');
  867. $this->Form->input('Addresses.0.last_name');
  868. $this->Form->input('Addresses.0.address');
  869. $this->Form->input('Addresses.0.city');
  870. $this->Form->input('Addresses.0.phone');
  871. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  872. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  873. $this->Form->input('Addresses.1.title');
  874. $this->Form->input('Addresses.1.first_name');
  875. $this->Form->input('Addresses.1.last_name');
  876. $this->Form->input('Addresses.1.address');
  877. $this->Form->input('Addresses.1.city');
  878. $this->Form->input('Addresses.1.phone');
  879. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  880. $result = $this->Form->secure($this->Form->fields);
  881. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  882. $expected = array(
  883. 'div' => array('style' => 'display:none;'),
  884. array('input' => array(
  885. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  886. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  887. )),
  888. array('input' => array(
  889. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  890. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  891. )),
  892. '/div'
  893. );
  894. $this->assertTags($result, $expected);
  895. }
  896. /**
  897. * Test form security with Model.field.0 style inputs
  898. *
  899. * @return void
  900. */
  901. public function testFormSecurityArrayFields() {
  902. $key = 'testKey';
  903. $this->Form->request->params['_Token']['key'] = $key;
  904. $this->Form->create('Address');
  905. $this->Form->input('Address.primary.1');
  906. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  907. $this->Form->input('Address.secondary.1.0');
  908. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  909. }
  910. /**
  911. * testFormSecurityMultipleInputDisabledFields method
  912. *
  913. * test secure form generation with multiple records and disabled fields.
  914. *
  915. * @return void
  916. */
  917. public function testFormSecurityMultipleInputDisabledFields() {
  918. $key = 'testKey';
  919. $this->Form->request->params['_Token'] = array(
  920. 'key' => $key,
  921. 'unlockedFields' => array('first_name', 'address')
  922. );
  923. $this->Form->create();
  924. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  925. $this->Form->input('Addresses.0.title');
  926. $this->Form->input('Addresses.0.first_name');
  927. $this->Form->input('Addresses.0.last_name');
  928. $this->Form->input('Addresses.0.address');
  929. $this->Form->input('Addresses.0.city');
  930. $this->Form->input('Addresses.0.phone');
  931. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  932. $this->Form->input('Addresses.1.title');
  933. $this->Form->input('Addresses.1.first_name');
  934. $this->Form->input('Addresses.1.last_name');
  935. $this->Form->input('Addresses.1.address');
  936. $this->Form->input('Addresses.1.city');
  937. $this->Form->input('Addresses.1.phone');
  938. $result = $this->Form->secure($this->Form->fields);
  939. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  940. $expected = array(
  941. 'div' => array('style' => 'display:none;'),
  942. array('input' => array(
  943. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  944. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  945. )),
  946. array('input' => array(
  947. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  948. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  949. )),
  950. '/div'
  951. );
  952. $this->assertTags($result, $expected);
  953. }
  954. /**
  955. * testFormSecurityInputDisabledFields method
  956. *
  957. * Test single record form with disabled fields.
  958. *
  959. * @return void
  960. */
  961. public function testFormSecurityInputUnlockedFields() {
  962. $key = 'testKey';
  963. $this->Form->request['_Token'] = array(
  964. 'key' => $key,
  965. 'unlockedFields' => array('first_name', 'address')
  966. );
  967. $this->Form->create();
  968. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  969. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  970. $this->Form->input('Addresses.title');
  971. $this->Form->input('Addresses.first_name');
  972. $this->Form->input('Addresses.last_name');
  973. $this->Form->input('Addresses.address');
  974. $this->Form->input('Addresses.city');
  975. $this->Form->input('Addresses.phone');
  976. $result = $this->Form->fields;
  977. $expected = array(
  978. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  979. 'Addresses.city', 'Addresses.phone'
  980. );
  981. $this->assertEquals($expected, $result);
  982. $result = $this->Form->secure($expected);
  983. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  984. $expected = array(
  985. 'div' => array('style' => 'display:none;'),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  988. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  989. )),
  990. array('input' => array(
  991. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  992. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  993. )),
  994. '/div'
  995. );
  996. $this->assertTags($result, $expected);
  997. }
  998. /**
  999. * test securing inputs with custom name attributes.
  1000. *
  1001. * @return void
  1002. */
  1003. public function testFormSecureWithCustomNameAttribute() {
  1004. $this->Form->request->params['_Token']['key'] = 'testKey';
  1005. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1006. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1007. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1008. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1009. }
  1010. /**
  1011. * testFormSecuredInput method
  1012. *
  1013. * Test generation of entire secure form, assertions made on input() output.
  1014. *
  1015. * @return void
  1016. */
  1017. public function testFormSecuredInput() {
  1018. $this->Form->request['_Token'] = array('key' => 'testKey');
  1019. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1020. $encoding = strtolower(Configure::read('App.encoding'));
  1021. $expected = array(
  1022. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1023. 'div' => array('style' => 'display:none;'),
  1024. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1025. array('input' => array(
  1026. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1027. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1028. )),
  1029. '/div'
  1030. );
  1031. $this->assertTags($result, $expected);
  1032. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1033. $expected = array(
  1034. 'div' => array('class' => 'input text'),
  1035. 'label' => array('for' => 'UserFormPublished'),
  1036. 'Published',
  1037. '/label',
  1038. array('input' => array(
  1039. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1040. 'id' => 'UserFormPublished'
  1041. )),
  1042. '/div'
  1043. );
  1044. $this->assertTags($result, $expected);
  1045. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1046. $expected = array(
  1047. 'div' => array('class' => 'input text'),
  1048. 'label' => array('for' => 'UserFormOther'),
  1049. 'Other',
  1050. '/label',
  1051. array('input' => array(
  1052. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1053. 'id' => 'UserFormOther'
  1054. )),
  1055. '/div'
  1056. );
  1057. $this->assertTags($result, $expected);
  1058. $result = $this->Form->hidden('UserForm.stuff');
  1059. $expected = array(
  1060. 'input' => array(
  1061. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1062. 'id' => 'UserFormStuff'
  1063. ));
  1064. $this->assertTags($result, $expected);
  1065. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1066. $expected = array('input' => array(
  1067. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1068. 'value' => '0', 'id' => 'UserFormHidden'
  1069. ));
  1070. $this->assertTags($result, $expected);
  1071. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1072. $expected = array(
  1073. 'div' => array('class' => 'input checkbox'),
  1074. array('input' => array(
  1075. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1076. 'value' => '0', 'id' => 'UserFormSomething_'
  1077. )),
  1078. array('input' => array(
  1079. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1080. 'value' => '1', 'id' => 'UserFormSomething'
  1081. )),
  1082. 'label' => array('for' => 'UserFormSomething'),
  1083. 'Something',
  1084. '/label',
  1085. '/div'
  1086. );
  1087. $this->assertTags($result, $expected);
  1088. $result = $this->Form->fields;
  1089. $expected = array(
  1090. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1091. 'UserForm.hidden' => '0', 'UserForm.something'
  1092. );
  1093. $this->assertEquals($expected, $result);
  1094. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1095. $result = $this->Form->secure($this->Form->fields);
  1096. $expected = array(
  1097. 'div' => array('style' => 'display:none;'),
  1098. array('input' => array(
  1099. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1100. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1101. )),
  1102. array('input' => array(
  1103. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1104. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1105. )),
  1106. '/div'
  1107. );
  1108. $this->assertTags($result, $expected);
  1109. }
  1110. /**
  1111. * Test secured inputs with custom names.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testSecuredInputCustomName() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $this->assertEquals(array(), $this->Form->fields);
  1118. $this->Form->input('text_input', array(
  1119. 'name' => 'data[Option][General.default_role]',
  1120. ));
  1121. $expected = array('Option.General.default_role');
  1122. $this->assertEquals($expected, $this->Form->fields);
  1123. $this->Form->input('select_box', array(
  1124. 'name' => 'data[Option][General.select_role]',
  1125. 'type' => 'select',
  1126. 'options' => array(1, 2),
  1127. ));
  1128. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1129. $this->assertEquals($expected, $this->Form->fields);
  1130. }
  1131. /**
  1132. * Tests that the correct keys are added to the field hash index
  1133. *
  1134. * @return void
  1135. */
  1136. public function testFormSecuredFileInput() {
  1137. $this->Form->request['_Token'] = array('key' => 'testKey');
  1138. $this->assertEquals(array(), $this->Form->fields);
  1139. $this->Form->file('Attachment.file');
  1140. $expected = array(
  1141. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1142. 'Attachment.file.error', 'Attachment.file.size'
  1143. );
  1144. $this->assertEquals($expected, $this->Form->fields);
  1145. }
  1146. /**
  1147. * test that multiple selects keys are added to field hash
  1148. *
  1149. * @return void
  1150. */
  1151. public function testFormSecuredMultipleSelect() {
  1152. $this->Form->request['_Token'] = array('key' => 'testKey');
  1153. $this->assertEquals(array(), $this->Form->fields);
  1154. $options = array('1' => 'one', '2' => 'two');
  1155. $this->Form->select('Model.select', $options);
  1156. $expected = array('Model.select');
  1157. $this->assertEquals($expected, $this->Form->fields);
  1158. $this->Form->fields = array();
  1159. $this->Form->select('Model.select', $options, array('multiple' => true));
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. }
  1162. /**
  1163. * testFormSecuredRadio method
  1164. *
  1165. * @return void
  1166. */
  1167. public function testFormSecuredRadio() {
  1168. $this->Form->request['_Token'] = array('key' => 'testKey');
  1169. $this->assertEquals(array(), $this->Form->fields);
  1170. $options = array('1' => 'option1', '2' => 'option2');
  1171. $this->Form->radio('Test.test', $options);
  1172. $expected = array('Test.test');
  1173. $this->assertEquals($expected, $this->Form->fields);
  1174. }
  1175. /**
  1176. * Test that when disabled is in a list based attribute array it works.
  1177. *
  1178. * @return void
  1179. */
  1180. public function testFormSecuredAndDisabledNotAssoc() {
  1181. $this->Form->request['_Token'] = array('key' => 'testKey');
  1182. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1183. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1184. $this->Form->text('Model.text', array('disabled'));
  1185. $this->Form->textarea('Model.textarea', array('disabled'));
  1186. $this->Form->password('Model.password', array('disabled'));
  1187. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1188. $expected = array(
  1189. 'Model.radio' => ''
  1190. );
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1195. * hashing.
  1196. *
  1197. * @return void
  1198. */
  1199. public function testFormSecuredAndDisabled() {
  1200. $this->Form->request['_Token'] = array('key' => 'testKey');
  1201. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1202. $this->Form->text('Model.text', array('disabled' => true));
  1203. $this->Form->password('Model.text', array('disabled' => true));
  1204. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1205. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1206. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1207. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1208. $this->Form->month('Model.month', array('disabled' => true));
  1209. $this->Form->day('Model.day', array('disabled' => true));
  1210. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1211. $this->Form->minute('Model.minute', array('disabled' => true));
  1212. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1213. $expected = array(
  1214. 'Model.radio' => ''
  1215. );
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. }
  1218. /**
  1219. * testDisableSecurityUsingForm method
  1220. *
  1221. * @return void
  1222. */
  1223. public function testDisableSecurityUsingForm() {
  1224. $this->Form->request['_Token'] = array(
  1225. 'key' => 'testKey',
  1226. 'disabledFields' => array()
  1227. );
  1228. $this->Form->create();
  1229. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1230. $this->Form->input('Addresses.title');
  1231. $this->Form->input('Addresses.first_name', array('secure' => false));
  1232. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1233. $this->Form->input('Addresses.zip', array(
  1234. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1235. ));
  1236. $result = $this->Form->fields;
  1237. $expected = array(
  1238. 'Addresses.id' => '123456', 'Addresses.title',
  1239. );
  1240. $this->assertEquals($expected, $result);
  1241. }
  1242. /**
  1243. * test disableField
  1244. *
  1245. * @return void
  1246. */
  1247. public function testUnlockFieldAddsToList() {
  1248. $this->Form->request['_Token'] = array(
  1249. 'key' => 'testKey',
  1250. 'unlockedFields' => array()
  1251. );
  1252. $this->Form->create('Contact');
  1253. $this->Form->unlockField('Contact.name');
  1254. $this->Form->text('Contact.name');
  1255. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1256. $this->assertEquals(array(), $this->Form->fields);
  1257. }
  1258. /**
  1259. * test unlockField removing from fields array.
  1260. *
  1261. * @return void
  1262. */
  1263. public function testUnlockFieldRemovingFromFields() {
  1264. $this->Form->request['_Token'] = array(
  1265. 'key' => 'testKey',
  1266. 'unlockedFields' => array()
  1267. );
  1268. $this->Form->create('Contact');
  1269. $this->Form->hidden('Contact.id', array('value' => 1));
  1270. $this->Form->text('Contact.name');
  1271. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1272. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1273. $this->Form->unlockField('Contact.name');
  1274. $this->Form->unlockField('Contact.id');
  1275. $this->assertEquals(array(), $this->Form->fields);
  1276. }
  1277. /**
  1278. * testTagIsInvalid method
  1279. *
  1280. * @return void
  1281. */
  1282. public function testTagIsInvalid() {
  1283. $Contact = ClassRegistry::getObject('Contact');
  1284. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1285. $this->Form->setEntity('Contact.0.email');
  1286. $result = $this->Form->tagIsInvalid();
  1287. $this->assertEquals($expected, $result);
  1288. $this->Form->setEntity('Contact.1.email');
  1289. $result = $this->Form->tagIsInvalid();
  1290. $this->assertFalse($result);
  1291. $this->Form->setEntity('Contact.0.name');
  1292. $result = $this->Form->tagIsInvalid();
  1293. $this->assertFalse($result);
  1294. }
  1295. /**
  1296. * Test tagIsInvalid with validation errors from a saveMany
  1297. *
  1298. * @return void
  1299. */
  1300. public function testTagIsInvalidSaveMany() {
  1301. $Contact = ClassRegistry::getObject('Contact');
  1302. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1303. $this->Form->create('Contact');
  1304. $this->Form->setEntity('0.email');
  1305. $result = $this->Form->tagIsInvalid();
  1306. $this->assertEquals($expected, $result);
  1307. $this->Form->setEntity('0.Contact.email');
  1308. $result = $this->Form->tagIsInvalid();
  1309. $this->assertEquals($expected, $result);
  1310. }
  1311. /**
  1312. * Test validation errors.
  1313. *
  1314. * @return void
  1315. */
  1316. public function testPasswordValidation() {
  1317. $Contact = ClassRegistry::getObject('Contact');
  1318. $Contact->validationErrors['password'] = array('Please provide a password');
  1319. $result = $this->Form->input('Contact.password');
  1320. $expected = array(
  1321. 'div' => array('class' => 'input password error'),
  1322. 'label' => array('for' => 'ContactPassword'),
  1323. 'Password',
  1324. '/label',
  1325. 'input' => array(
  1326. 'type' => 'password', 'name' => 'data[Contact][password]',
  1327. 'id' => 'ContactPassword', 'class' => 'form-error'
  1328. ),
  1329. array('div' => array('class' => 'error-message')),
  1330. 'Please provide a password',
  1331. '/div',
  1332. '/div'
  1333. );
  1334. $this->assertTags($result, $expected);
  1335. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1336. $expected = array(
  1337. 'div' => array('class' => 'input password error'),
  1338. 'label' => array('for' => 'ContactPassword'),
  1339. 'Password',
  1340. '/label',
  1341. 'input' => array(
  1342. 'type' => 'password', 'name' => 'data[Contact][password]',
  1343. 'id' => 'ContactPassword', 'class' => 'form-error'
  1344. ),
  1345. '/div'
  1346. );
  1347. $this->assertTags($result, $expected);
  1348. }
  1349. /**
  1350. * Test validation errors, when validation message is an empty string.
  1351. *
  1352. * @return void
  1353. */
  1354. public function testEmptyErrorValidation() {
  1355. $this->Form->validationErrors['Contact']['password'] = '';
  1356. $result = $this->Form->input('Contact.password');
  1357. $expected = array(
  1358. 'div' => array('class' => 'input password error'),
  1359. 'label' => array('for' => 'ContactPassword'),
  1360. 'Password',
  1361. '/label',
  1362. 'input' => array(
  1363. 'type' => 'password', 'name' => 'data[Contact][password]',
  1364. 'id' => 'ContactPassword', 'class' => 'form-error'
  1365. ),
  1366. array('div' => array('class' => 'error-message')),
  1367. array(),
  1368. '/div',
  1369. '/div'
  1370. );
  1371. $this->assertTags($result, $expected);
  1372. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1373. $expected = array(
  1374. 'div' => array('class' => 'input password error'),
  1375. 'label' => array('for' => 'ContactPassword'),
  1376. 'Password',
  1377. '/label',
  1378. 'input' => array(
  1379. 'type' => 'password', 'name' => 'data[Contact][password]',
  1380. 'id' => 'ContactPassword', 'class' => 'form-error'
  1381. ),
  1382. '/div'
  1383. );
  1384. $this->assertTags($result, $expected);
  1385. }
  1386. /**
  1387. * Test validation errors, when calling input() overriding validation message by an empty string.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testEmptyInputErrorValidation() {
  1392. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1393. $result = $this->Form->input('Contact.password', array('error' => ''));
  1394. $expected = array(
  1395. 'div' => array('class' => 'input password error'),
  1396. 'label' => array('for' => 'ContactPassword'),
  1397. 'Password',
  1398. '/label',
  1399. 'input' => array(
  1400. 'type' => 'password', 'name' => 'data[Contact][password]',
  1401. 'id' => 'ContactPassword', 'class' => 'form-error'
  1402. ),
  1403. array('div' => array('class' => 'error-message')),
  1404. array(),
  1405. '/div',
  1406. '/div'
  1407. );
  1408. $this->assertTags($result, $expected);
  1409. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1410. $expected = array(
  1411. 'div' => array('class' => 'input password error'),
  1412. 'label' => array('for' => 'ContactPassword'),
  1413. 'Password',
  1414. '/label',
  1415. 'input' => array(
  1416. 'type' => 'password', 'name' => 'data[Contact][password]',
  1417. 'id' => 'ContactPassword', 'class' => 'form-error'
  1418. ),
  1419. '/div'
  1420. );
  1421. $this->assertTags($result, $expected);
  1422. }
  1423. /**
  1424. * testFormValidationAssociated method
  1425. *
  1426. * test display of form errors in conjunction with model::validates.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testFormValidationAssociated() {
  1431. $this->UserForm = ClassRegistry::getObject('UserForm');
  1432. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1433. $data = array(
  1434. 'UserForm' => array('name' => 'user'),
  1435. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1436. );
  1437. $result = $this->UserForm->OpenidUrl->create($data);
  1438. $this->assertFalse(empty($result));
  1439. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1440. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1441. $encoding = strtolower(Configure::read('App.encoding'));
  1442. $expected = array(
  1443. 'form' => array(
  1444. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1445. 'accept-charset' => $encoding
  1446. ),
  1447. 'div' => array('style' => 'display:none;'),
  1448. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1449. '/div'
  1450. );
  1451. $this->assertTags($result, $expected);
  1452. $result = $this->Form->error(
  1453. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1454. );
  1455. $this->assertEquals('Error, not registered', $result);
  1456. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1457. }
  1458. /**
  1459. * testFormValidationAssociatedFirstLevel method
  1460. *
  1461. * test form error display with associated model.
  1462. *
  1463. * @return void
  1464. */
  1465. public function testFormValidationAssociatedFirstLevel() {
  1466. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1467. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1468. $data = array(
  1469. 'ValidateUser' => array('name' => 'mariano'),
  1470. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1471. );
  1472. $result = $this->ValidateUser->create($data);
  1473. $this->assertFalse(empty($result));
  1474. $this->assertFalse($this->ValidateUser->validates());
  1475. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1476. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1477. $encoding = strtolower(Configure::read('App.encoding'));
  1478. $expected = array(
  1479. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1480. 'div' => array('style' => 'display:none;'),
  1481. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1482. '/div'
  1483. );
  1484. $this->assertTags($result, $expected);
  1485. $result = $this->Form->error(
  1486. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1487. );
  1488. $this->assertEquals('Invalid email', $result);
  1489. $result = $this->Form->error(
  1490. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1491. );
  1492. $this->assertEquals('Invalid name', $result);
  1493. $result = $this->Form->error(
  1494. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1495. );
  1496. $this->assertEquals('Invalid city', $result);
  1497. unset($this->ValidateUser->ValidateProfile);
  1498. unset($this->ValidateUser);
  1499. }
  1500. /**
  1501. * testFormValidationAssociatedSecondLevel method
  1502. *
  1503. * test form error display with associated model.
  1504. *
  1505. * @return void
  1506. */
  1507. public function testFormValidationAssociatedSecondLevel() {
  1508. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1509. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1510. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1511. $data = array(
  1512. 'ValidateUser' => array('name' => 'mariano'),
  1513. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1514. 'ValidateItem' => array('name' => 'Item')
  1515. );
  1516. $result = $this->ValidateUser->create($data);
  1517. $this->assertFalse(empty($result));
  1518. $this->assertFalse($this->ValidateUser->validates());
  1519. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1520. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1521. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1522. $encoding = strtolower(Configure::read('App.encoding'));
  1523. $expected = array(
  1524. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1525. 'div' => array('style' => 'display:none;'),
  1526. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1527. '/div'
  1528. );
  1529. $this->assertTags($result, $expected);
  1530. $result = $this->Form->error(
  1531. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1532. );
  1533. $this->assertEquals('Invalid email', $result);
  1534. $result = $this->Form->error(
  1535. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1536. );
  1537. $this->assertEquals('Invalid name', $result);
  1538. $result = $this->Form->error(
  1539. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1540. );
  1541. $result = $this->Form->error(
  1542. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1543. );
  1544. $this->assertEquals('Invalid description', $result);
  1545. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1546. unset($this->ValidateUser->ValidateProfile);
  1547. unset($this->ValidateUser);
  1548. }
  1549. /**
  1550. * testFormValidationMultiRecord method
  1551. *
  1552. * test form error display with multiple records.
  1553. *
  1554. * @return void
  1555. */
  1556. public function testFormValidationMultiRecord() {
  1557. $Contact = ClassRegistry::getObject('Contact');
  1558. $Contact->validationErrors[2] = array(
  1559. 'name' => array('This field cannot be left blank')
  1560. );
  1561. $result = $this->Form->input('Contact.2.name');
  1562. $expected = array(
  1563. 'div' => array('class' => 'input text error'),
  1564. 'label' => array('for' => 'Contact2Name'),
  1565. 'Name',
  1566. '/label',
  1567. 'input' => array(
  1568. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1569. 'class' => 'form-error', 'maxlength' => 255
  1570. ),
  1571. array('div' => array('class' => 'error-message')),
  1572. 'This field cannot be left blank',
  1573. '/div',
  1574. '/div'
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. }
  1578. /**
  1579. * testMultipleInputValidation method
  1580. *
  1581. * test multiple record form validation error display.
  1582. *
  1583. * @return void
  1584. */
  1585. public function testMultipleInputValidation() {
  1586. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1587. $Address->validationErrors[0] = array(
  1588. 'title' => array('This field cannot be empty'),
  1589. 'first_name' => array('This field cannot be empty')
  1590. );
  1591. $Address->validationErrors[1] = array(
  1592. 'last_name' => array('You must have a last name')
  1593. );
  1594. $this->Form->create();
  1595. $result = $this->Form->input('Address.0.title');
  1596. $expected = array(
  1597. 'div' => array('class'),
  1598. 'label' => array('for'),
  1599. 'preg:/[^<]+/',
  1600. '/label',
  1601. 'input' => array(
  1602. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1603. ),
  1604. array('div' => array('class' => 'error-message')),
  1605. 'This field cannot be empty',
  1606. '/div',
  1607. '/div'
  1608. );
  1609. $this->assertTags($result, $expected);
  1610. $result = $this->Form->input('Address.0.first_name');
  1611. $expected = array(
  1612. 'div' => array('class'),
  1613. 'label' => array('for'),
  1614. 'preg:/[^<]+/',
  1615. '/label',
  1616. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1617. array('div' => array('class' => 'error-message')),
  1618. 'This field cannot be empty',
  1619. '/div',
  1620. '/div'
  1621. );
  1622. $this->assertTags($result, $expected);
  1623. $result = $this->Form->input('Address.0.last_name');
  1624. $expected = array(
  1625. 'div' => array('class'),
  1626. 'label' => array('for'),
  1627. 'preg:/[^<]+/',
  1628. '/label',
  1629. 'input' => array('type' => 'text', 'name', 'id'),
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->input('Address.1.last_name');
  1634. $expected = array(
  1635. 'div' => array('class'),
  1636. 'label' => array('for'),
  1637. 'preg:/[^<]+/',
  1638. '/label',
  1639. 'input' => array(
  1640. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1641. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1642. ),
  1643. array('div' => array('class' => 'error-message')),
  1644. 'You must have a last name',
  1645. '/div',
  1646. '/div'
  1647. );
  1648. $this->assertTags($result, $expected);
  1649. }
  1650. /**
  1651. * testInput method
  1652. *
  1653. * Test various incarnations of input().
  1654. *
  1655. * @return void
  1656. */
  1657. public function testInput() {
  1658. $result = $this->Form->input('ValidateUser.balance');
  1659. $expected = array(
  1660. 'div' => array('class'),
  1661. 'label' => array('for'),
  1662. 'Balance',
  1663. '/label',
  1664. 'input' => array('name', 'type' => 'number', 'id'),
  1665. '/div',
  1666. );
  1667. $this->assertTags($result, $expected);
  1668. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1669. $expected = array(
  1670. 'div' => array('class' => 'input email'),
  1671. 'label' => array('for' => 'custom'),
  1672. 'Email',
  1673. '/label',
  1674. array('input' => array(
  1675. 'type' => 'email', 'name' => 'data[Contact][email]',
  1676. 'id' => 'custom', 'maxlength' => 255
  1677. )),
  1678. '/div'
  1679. );
  1680. $this->assertTags($result, $expected);
  1681. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1682. $expected = array(
  1683. '<div',
  1684. 'label' => array('for' => 'ContactEmail'),
  1685. 'Email',
  1686. '/label',
  1687. array('input' => array(
  1688. 'type' => 'email', 'name' => 'data[Contact][email]',
  1689. 'id' => 'ContactEmail', 'maxlength' => 255
  1690. )),
  1691. '/div'
  1692. );
  1693. $this->assertTags($result, $expected);
  1694. $result = $this->Form->hidden('Contact.idontexist');
  1695. $expected = array('input' => array(
  1696. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1697. 'id' => 'ContactIdontexist'
  1698. ));
  1699. $this->assertTags($result, $expected);
  1700. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1701. $expected = array(
  1702. 'div' => array('class' => 'input text'),
  1703. 'label' => array('for' => 'ContactEmail'),
  1704. 'Email',
  1705. '/label',
  1706. array('input' => array(
  1707. 'type' => 'text', 'name' => 'data[Contact][email]',
  1708. 'id' => 'ContactEmail'
  1709. )),
  1710. '/div'
  1711. );
  1712. $this->assertTags($result, $expected);
  1713. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1714. $expected = array(
  1715. 'div' => array('class' => 'input text'),
  1716. 'label' => array('for' => 'Contact5Email'),
  1717. 'Email',
  1718. '/label',
  1719. array('input' => array(
  1720. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1721. 'id' => 'Contact5Email'
  1722. )),
  1723. '/div'
  1724. );
  1725. $this->assertTags($result, $expected);
  1726. $result = $this->Form->input('Contact.password');
  1727. $expected = array(
  1728. 'div' => array('class' => 'input password'),
  1729. 'label' => array('for' => 'ContactPassword'),
  1730. 'Password',
  1731. '/label',
  1732. array('input' => array(
  1733. 'type' => 'password', 'name' => 'data[Contact][password]',
  1734. 'id' => 'ContactPassword'
  1735. )),
  1736. '/div'
  1737. );
  1738. $this->assertTags($result, $expected);
  1739. $result = $this->Form->input('Contact.email', array(
  1740. 'type' => 'file', 'class' => 'textbox'
  1741. ));
  1742. $expected = array(
  1743. 'div' => array('class' => 'input file'),
  1744. 'label' => array('for' => 'ContactEmail'),
  1745. 'Email',
  1746. '/label',
  1747. array('input' => array(
  1748. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1749. 'id' => 'ContactEmail'
  1750. )),
  1751. '/div'
  1752. );
  1753. $this->assertTags($result, $expected);
  1754. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1755. $result = $this->Form->input('Contact.phone');
  1756. $expected = array(
  1757. 'div' => array('class' => 'input tel'),
  1758. 'label' => array('for' => 'ContactPhone'),
  1759. 'Phone',
  1760. '/label',
  1761. array('input' => array(
  1762. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1763. 'value' => 'Hello &amp; World &gt; weird chars',
  1764. 'id' => 'ContactPhone', 'maxlength' => 255
  1765. )),
  1766. '/div'
  1767. );
  1768. $this->assertTags($result, $expected);
  1769. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1770. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1771. $expected = array(
  1772. 'div' => array('class' => 'input text'),
  1773. 'label' => array('for' => 'myId'),
  1774. 'Field',
  1775. '/label',
  1776. 'input' => array(
  1777. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1778. 'value' => 'My value', 'id' => 'myId'
  1779. ),
  1780. '/div'
  1781. );
  1782. $this->assertTags($result, $expected);
  1783. unset($this->Form->request->data);
  1784. $Contact = ClassRegistry::getObject('Contact');
  1785. $Contact->validationErrors['field'] = array('Badness!');
  1786. $result = $this->Form->input('Contact.field');
  1787. $expected = array(
  1788. 'div' => array('class' => 'input text error'),
  1789. 'label' => array('for' => 'ContactField'),
  1790. 'Field',
  1791. '/label',
  1792. 'input' => array(
  1793. 'type' => 'text', 'name' => 'data[Contact][field]',
  1794. 'id' => 'ContactField', 'class' => 'form-error'
  1795. ),
  1796. array('div' => array('class' => 'error-message')),
  1797. 'Badness!',
  1798. '/div',
  1799. '/div'
  1800. );
  1801. $this->assertTags($result, $expected);
  1802. $result = $this->Form->input('Contact.field', array(
  1803. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1804. ));
  1805. $expected = array(
  1806. 'label' => array('for' => 'ContactField'),
  1807. 'Field',
  1808. '/label',
  1809. 'input' => array(
  1810. 'type' => 'text', 'name' => 'data[Contact][field]',
  1811. 'id' => 'ContactField', 'class' => 'form-error'
  1812. ),
  1813. array('span' => array('class' => 'error-message')),
  1814. 'Badness!',
  1815. '/span'
  1816. );
  1817. $this->assertTags($result, $expected);
  1818. $result = $this->Form->input('Contact.field', array(
  1819. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1820. ));
  1821. $expected = array(
  1822. 'div' => array('class' => 'input text error'),
  1823. 'label' => array('for' => 'ContactField'),
  1824. 'Field',
  1825. '/label',
  1826. 'input' => array(
  1827. 'type' => 'text', 'name' => 'data[Contact][field]',
  1828. 'id' => 'ContactField', 'class' => 'form-error'
  1829. ),
  1830. array('div' => array('class' => 'error')),
  1831. 'Badness!',
  1832. '/div'
  1833. );
  1834. $this->assertTags($result, $expected);
  1835. $result = $this->Form->input('Contact.field', array(
  1836. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1837. ));
  1838. $expected = array(
  1839. 'span' => array('class' => 'input text error'),
  1840. 'label' => array('for' => 'ContactField'),
  1841. 'Field',
  1842. '/label',
  1843. 'input' => array(
  1844. 'type' => 'text', 'name' => 'data[Contact][field]',
  1845. 'id' => 'ContactField', 'class' => 'form-error'
  1846. ),
  1847. 'Badness!',
  1848. '/span'
  1849. );
  1850. $this->assertTags($result, $expected);
  1851. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1852. $expected = array(
  1853. 'div' => array('class' => 'input text error'),
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'data[Contact][field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. 'A message to you, Rudy',
  1862. array('div' => array('class' => 'error-message')),
  1863. 'Badness!',
  1864. '/div',
  1865. '/div'
  1866. );
  1867. $this->assertTags($result, $expected);
  1868. $this->Form->setEntity(null);
  1869. $this->Form->setEntity('Contact.field');
  1870. $result = $this->Form->input('Contact.field', array(
  1871. 'after' => 'A message to you, Rudy', 'error' => false
  1872. ));
  1873. $expected = array(
  1874. 'div' => array('class' => 'input text'),
  1875. 'label' => array('for' => 'ContactField'),
  1876. 'Field',
  1877. '/label',
  1878. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1879. 'A message to you, Rudy',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1884. $expected = array(
  1885. 'div' => array('class' => 'input text'),
  1886. 'label' => array('for' => 'ObjectField'),
  1887. 'Field',
  1888. '/label',
  1889. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1890. 'A message to you, Rudy',
  1891. '/div'
  1892. );
  1893. $this->assertTags($result, $expected);
  1894. $Contact->validationErrors['field'] = array('minLength');
  1895. $result = $this->Form->input('Contact.field', array(
  1896. 'error' => array(
  1897. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1898. 'maxLength' => 'login too large'
  1899. )
  1900. ));
  1901. $expected = array(
  1902. 'div' => array('class' => 'input text error'),
  1903. 'label' => array('for' => 'ContactField'),
  1904. 'Field',
  1905. '/label',
  1906. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1907. array('div' => array('class' => 'error-message')),
  1908. 'Le login doit contenir au moins 2 caractères',
  1909. '/div',
  1910. '/div'
  1911. );
  1912. $this->assertTags($result, $expected);
  1913. $Contact->validationErrors['field'] = array('maxLength');
  1914. $result = $this->Form->input('Contact.field', array(
  1915. 'error' => array(
  1916. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1917. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1918. 'maxLength' => 'login too large',
  1919. )
  1920. ));
  1921. $expected = array(
  1922. 'div' => array('class' => 'input text error'),
  1923. 'label' => array('for' => 'ContactField'),
  1924. 'Field',
  1925. '/label',
  1926. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1927. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1928. 'login too large',
  1929. '/span',
  1930. '/div'
  1931. );
  1932. $this->assertTags($result, $expected);
  1933. }
  1934. /**
  1935. * Test that inputs with 0 can be created.
  1936. *
  1937. * @return void
  1938. */
  1939. public function testInputZero() {
  1940. $this->Form->create('User');
  1941. $result = $this->Form->input('0');
  1942. $expected = array(
  1943. 'div' => array('class' => 'input text'),
  1944. 'label' => array('for' => 'User0'), '/label',
  1945. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. }
  1950. /**
  1951. * test input() with checkbox creation
  1952. *
  1953. * @return void
  1954. */
  1955. public function testInputCheckbox() {
  1956. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1957. $expected = array(
  1958. 'div' => array('class' => 'input checkbox'),
  1959. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1960. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1961. '/div'
  1962. );
  1963. $this->assertTags($result, $expected);
  1964. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1965. $expected = array(
  1966. 'div' => array('class' => 'input checkbox'),
  1967. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1968. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1969. '/div'
  1970. );
  1971. $this->assertTags($result, $expected);
  1972. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1973. $expected = array(
  1974. 'div' => array('class' => 'input checkbox'),
  1975. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1976. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. $result = $this->Form->input('User.disabled', array(
  1981. 'label' => 'Disabled',
  1982. 'type' => 'checkbox',
  1983. 'data-foo' => 'disabled'
  1984. ));
  1985. $expected = array(
  1986. 'div' => array('class' => 'input checkbox'),
  1987. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1988. array('input' => array(
  1989. 'type' => 'checkbox',
  1990. 'name' => 'data[User][disabled]',
  1991. 'value' => '1',
  1992. 'id' => 'UserDisabled',
  1993. 'data-foo' => 'disabled'
  1994. )),
  1995. 'label' => array('for' => 'UserDisabled'),
  1996. 'Disabled',
  1997. '/label',
  1998. '/div'
  1999. );
  2000. $this->assertTags($result, $expected);
  2001. }
  2002. /**
  2003. * test form->input() with time types.
  2004. *
  2005. */
  2006. public function testInputTime() {
  2007. extract($this->dateRegex);
  2008. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2009. $result = explode(':', $result);
  2010. $this->assertRegExp('/option value="23"/', $result[0]);
  2011. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2012. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2013. $result = explode(':', $result);
  2014. $this->assertRegExp('/option value="23"/', $result[0]);
  2015. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2016. $result = $this->Form->input('Model.field', array(
  2017. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2018. ));
  2019. $result = explode(':', $result);
  2020. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2021. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2022. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2023. $result = $this->Form->input('Model.field', array(
  2024. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2025. ));
  2026. $result = explode(':', $result);
  2027. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2028. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2029. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2030. $result = $this->Form->input('prueba', array(
  2031. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2032. 'maxYear' => date('Y') + 1, 'interval' => 15
  2033. ));
  2034. $result = explode(':', $result);
  2035. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2036. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2037. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2038. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2039. $result = $this->Form->input('Random.start_time', array(
  2040. 'type' => 'time',
  2041. 'selected' => '18:15'
  2042. ));
  2043. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2044. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2045. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2046. $result = $this->Form->input('published', array('type' => 'time'));
  2047. $now = strtotime('now');
  2048. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2049. $now = strtotime('2013-03-09 00:42:21');
  2050. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2051. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2052. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2053. }
  2054. /**
  2055. * Test interval + selected near the hour roll over.
  2056. *
  2057. * @return void
  2058. */
  2059. public function testTimeSelectedWithInterval() {
  2060. $result = $this->Form->input('Model.start_time', array(
  2061. 'type' => 'time',
  2062. 'interval' => 15,
  2063. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2064. ));
  2065. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2066. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2067. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2068. $result = $this->Form->input('Model.start_time', array(
  2069. 'type' => 'time',
  2070. 'interval' => 15,
  2071. 'selected' => '2012-10-23 15:57:00'
  2072. ));
  2073. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2074. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2075. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2076. $result = $this->Form->input('Model.start_time', array(
  2077. 'timeFormat' => 24,
  2078. 'type' => 'time',
  2079. 'interval' => 15,
  2080. 'selected' => '15:57'
  2081. ));
  2082. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2083. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2084. $result = $this->Form->input('Model.start_time', array(
  2085. 'timeFormat' => 24,
  2086. 'type' => 'time',
  2087. 'interval' => 15,
  2088. 'selected' => '23:57'
  2089. ));
  2090. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2091. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2092. $result = $this->Form->input('Model.created', array(
  2093. 'timeFormat' => 24,
  2094. 'type' => 'datetime',
  2095. 'interval' => 15,
  2096. 'selected' => '2012-09-30 23:56'
  2097. ));
  2098. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2099. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2100. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2101. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2102. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2103. }
  2104. /**
  2105. * Test time with selected values around 12:xx:xx
  2106. *
  2107. * @return void
  2108. */
  2109. public function testTimeSelectedWithIntervalTwelve() {
  2110. $result = $this->Form->input('Model.start_time', array(
  2111. 'type' => 'time',
  2112. 'timeFormat' => 12,
  2113. 'interval' => 15,
  2114. 'selected' => '00:00:00'
  2115. ));
  2116. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2117. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2118. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2119. $result = $this->Form->input('Model.start_time', array(
  2120. 'type' => 'time',
  2121. 'timeFormat' => 12,
  2122. 'interval' => 15,
  2123. 'selected' => '12:00:00'
  2124. ));
  2125. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2126. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2127. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2128. $result = $this->Form->input('Model.start_time', array(
  2129. 'type' => 'time',
  2130. 'timeFormat' => 12,
  2131. 'interval' => 15,
  2132. 'selected' => '12:15:00'
  2133. ));
  2134. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2135. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2136. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2137. }
  2138. /**
  2139. * Test interval & timeFormat = 12
  2140. *
  2141. * @return void
  2142. */
  2143. public function testInputTimeWithIntervalAnd12HourFormat() {
  2144. $result = $this->Form->input('Model.start_time', array(
  2145. 'type' => 'time',
  2146. 'timeFormat' => 12,
  2147. 'interval' => 5,
  2148. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2149. ));
  2150. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2151. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2152. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2153. $result = $this->Form->input('Model.start_time', array(
  2154. 'type' => 'time',
  2155. 'timeFormat' => '12',
  2156. 'interval' => 5,
  2157. 'selected' => '2013-04-19 16:30:00'
  2158. ));
  2159. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2160. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2161. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2162. $result = $this->Form->input('Model.start_time', array(
  2163. 'type' => 'time',
  2164. 'timeFormat' => '12',
  2165. 'interval' => 10,
  2166. 'selected' => '2013-05-19 00:33:00'
  2167. ));
  2168. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2169. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2170. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2171. $result = $this->Form->input('Model.start_time', array(
  2172. 'type' => 'time',
  2173. 'timeFormat' => '12',
  2174. 'interval' => 10,
  2175. 'selected' => '2013-05-19 13:33:00'
  2176. ));
  2177. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2178. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2179. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2180. $result = $this->Form->input('Model.start_time', array(
  2181. 'type' => 'time',
  2182. 'timeFormat' => '12',
  2183. 'interval' => 10,
  2184. 'selected' => '2013-05-19 01:33:00'
  2185. ));
  2186. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2187. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2188. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2189. }
  2190. /**
  2191. * test form->input() with datetime, date and time types
  2192. *
  2193. * @return void
  2194. */
  2195. public function testInputDatetime() {
  2196. extract($this->dateRegex);
  2197. $result = $this->Form->input('prueba', array(
  2198. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2199. 'maxYear' => date('Y') + 1, 'interval' => 15
  2200. ));
  2201. $result = explode(':', $result);
  2202. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2203. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2204. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2205. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2206. //related to ticket #5013
  2207. $result = $this->Form->input('Contact.date', array(
  2208. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2209. ));
  2210. $this->assertRegExp('/class="customClass"/', $result);
  2211. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2212. $result = $this->Form->input('Contact.date', array(
  2213. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2214. ));
  2215. $this->assertRegExp('/id="customIdDay"/', $result);
  2216. $this->assertRegExp('/id="customIdMonth"/', $result);
  2217. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2218. $result = $this->Form->input('Model.field', array(
  2219. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2220. ));
  2221. $this->assertRegExp('/id="customIDDay"/', $result);
  2222. $this->assertRegExp('/id="customIDHour"/', $result);
  2223. $result = explode('</select><select', $result);
  2224. $result = explode(':', $result[1]);
  2225. $this->assertRegExp('/option value="23"/', $result[0]);
  2226. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2227. $result = $this->Form->input('Model.field', array(
  2228. 'type' => 'datetime', 'timeFormat' => 12
  2229. ));
  2230. $result = explode('</select><select', $result);
  2231. $result = explode(':', $result[1]);
  2232. $this->assertRegExp('/option value="12"/', $result[0]);
  2233. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2234. $this->Form->request->data = array('Contact' => array('created' => null));
  2235. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2236. $expected = array(
  2237. 'div' => array('class' => 'input date'),
  2238. 'label' => array('for' => 'ContactCreatedMonth'),
  2239. 'Created',
  2240. '/label',
  2241. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2242. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2243. $monthsRegex,
  2244. '/select', '-',
  2245. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2246. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2247. $daysRegex,
  2248. '/select', '-',
  2249. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2250. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2251. $yearsRegex,
  2252. '/select',
  2253. '/div'
  2254. );
  2255. $this->assertTags($result, $expected);
  2256. $this->Form->request->data = array('Contact' => array('created' => null));
  2257. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2258. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2259. $this->Form->request->data = array('Contact' => array('created' => null));
  2260. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2261. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2262. $result = $this->Form->input('Contact.created', array(
  2263. 'type' => 'date',
  2264. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2265. 'timeFormat' => 'NONE'
  2266. ));
  2267. $this->assertRegExp('/for\="created-month"/', $result);
  2268. }
  2269. /**
  2270. * Test generating checkboxes in a loop.
  2271. *
  2272. * @return void
  2273. */
  2274. public function testInputCheckboxesInLoop() {
  2275. for ($i = 1; $i < 5; $i++) {
  2276. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2277. $expected = array(
  2278. 'div' => array('class' => 'input checkbox'),
  2279. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2280. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2281. 'label' => array('for' => "Contact{$i}Email"),
  2282. 'Email',
  2283. '/label',
  2284. '/div'
  2285. );
  2286. $this->assertTags($result, $expected);
  2287. }
  2288. }
  2289. /**
  2290. * Test generating checkboxes with disabled elements.
  2291. *
  2292. * @return void
  2293. */
  2294. public function testInputCheckboxWithDisabledElements() {
  2295. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2296. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2297. $expected = array(
  2298. array('div' => array('class' => 'input select')),
  2299. array('label' => array('for' => "ContactMultiple")),
  2300. 'Multiple',
  2301. '/label',
  2302. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2303. array('div' => array('class' => 'checkbox')),
  2304. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2305. array('label' => array('for' => "ContactMultiple1")),
  2306. 'One',
  2307. '/label',
  2308. '/div',
  2309. array('div' => array('class' => 'checkbox')),
  2310. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2311. array('label' => array('for' => "ContactMultiple2")),
  2312. 'Two',
  2313. '/label',
  2314. '/div',
  2315. array('div' => array('class' => 'checkbox')),
  2316. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2317. array('label' => array('for' => "ContactMultiple3")),
  2318. 'Three',
  2319. '/label',
  2320. '/div',
  2321. '/div'
  2322. );
  2323. $this->assertTags($result, $expected);
  2324. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2325. $this->assertTags($result, $expected);
  2326. $disabled = array('2', 3);
  2327. $expected = array(
  2328. array('div' => array('class' => 'input select')),
  2329. array('label' => array('for' => "ContactMultiple")),
  2330. 'Multiple',
  2331. '/label',
  2332. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2333. array('div' => array('class' => 'checkbox')),
  2334. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2335. array('label' => array('for' => "ContactMultiple1")),
  2336. 'One',
  2337. '/label',
  2338. '/div',
  2339. array('div' => array('class' => 'checkbox')),
  2340. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2341. array('label' => array('for' => "ContactMultiple2")),
  2342. 'Two',
  2343. '/label',
  2344. '/div',
  2345. array('div' => array('class' => 'checkbox')),
  2346. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2347. array('label' => array('for' => "ContactMultiple3")),
  2348. 'Three',
  2349. '/label',
  2350. '/div',
  2351. '/div'
  2352. );
  2353. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2354. $this->assertTags($result, $expected);
  2355. // make sure 50 does only disable 50, and not 50f5c0cf
  2356. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2357. $disabled = array(50);
  2358. $expected = array(
  2359. array('div' => array('class' => 'input select')),
  2360. array('label' => array('for' => "ContactMultiple")),
  2361. 'Multiple',
  2362. '/label',
  2363. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2364. array('div' => array('class' => 'checkbox')),
  2365. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2366. array('label' => array('for' => "ContactMultiple50")),
  2367. 'Fifty',
  2368. '/label',
  2369. '/div',
  2370. array('div' => array('class' => 'checkbox')),
  2371. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2372. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2373. 'Stringy',
  2374. '/label',
  2375. '/div',
  2376. '/div'
  2377. );
  2378. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2379. $this->assertTags($result, $expected);
  2380. }
  2381. /**
  2382. * test input name with leading integer, ensure attributes are generated correctly.
  2383. *
  2384. * @return void
  2385. */
  2386. public function testInputWithLeadingInteger() {
  2387. $result = $this->Form->text('0.Node.title');
  2388. $expected = array(
  2389. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2390. );
  2391. $this->assertTags($result, $expected);
  2392. }
  2393. /**
  2394. * test form->input() with select type inputs.
  2395. *
  2396. * @return void
  2397. */
  2398. public function testInputSelectType() {
  2399. $result = $this->Form->input('email', array(
  2400. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2401. );
  2402. $expected = array(
  2403. 'div' => array('class' => 'input select'),
  2404. 'label' => array('for' => 'email'),
  2405. 'Email',
  2406. '/label',
  2407. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2408. array('option' => array('value' => '')),
  2409. '/option',
  2410. array('option' => array('value' => 'è')),
  2411. 'Firést',
  2412. '/option',
  2413. array('option' => array('value' => 'é')),
  2414. 'Secoènd',
  2415. '/option',
  2416. '/select',
  2417. '/div'
  2418. );
  2419. $this->assertTags($result, $expected);
  2420. $result = $this->Form->input('email', array(
  2421. 'options' => array('First', 'Second'), 'empty' => true)
  2422. );
  2423. $expected = array(
  2424. 'div' => array('class' => 'input select'),
  2425. 'label' => array('for' => 'email'),
  2426. 'Email',
  2427. '/label',
  2428. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2429. array('option' => array('value' => '')),
  2430. '/option',
  2431. array('option' => array('value' => '0')),
  2432. 'First',
  2433. '/option',
  2434. array('option' => array('value' => '1')),
  2435. 'Second',
  2436. '/option',
  2437. '/select',
  2438. '/div'
  2439. );
  2440. $this->assertTags($result, $expected);
  2441. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2442. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2443. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2444. $expected = array(
  2445. 'div' => array('class' => 'input select'),
  2446. 'label' => array('for' => 'ModelUserId'),
  2447. 'User',
  2448. '/label',
  2449. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2450. array('option' => array('value' => '')),
  2451. '/option',
  2452. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2453. 'good',
  2454. '/option',
  2455. array('option' => array('value' => 'other')),
  2456. 'bad',
  2457. '/option',
  2458. '/select',
  2459. '/div'
  2460. );
  2461. $this->assertTags($result, $expected);
  2462. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2463. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2464. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2465. $expected = array(
  2466. 'div' => array('class' => 'input select'),
  2467. 'label' => array('for' => 'ThingUserId'),
  2468. 'User',
  2469. '/label',
  2470. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2471. array('option' => array('value' => '')),
  2472. 'Some Empty',
  2473. '/option',
  2474. array('option' => array('value' => 'value')),
  2475. 'good',
  2476. '/option',
  2477. array('option' => array('value' => 'other')),
  2478. 'bad',
  2479. '/option',
  2480. '/select',
  2481. '/div'
  2482. );
  2483. $this->assertTags($result, $expected);
  2484. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2485. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2486. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2487. $expected = array(
  2488. 'div' => array('class' => 'input select'),
  2489. 'label' => array('for' => 'ThingUserId'),
  2490. 'User',
  2491. '/label',
  2492. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2493. array('option' => array('value' => '')),
  2494. 'Some Empty',
  2495. '/option',
  2496. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2497. 'good',
  2498. '/option',
  2499. array('option' => array('value' => 'other')),
  2500. 'bad',
  2501. '/option',
  2502. '/select',
  2503. '/div'
  2504. );
  2505. $this->assertTags($result, $expected);
  2506. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2507. $this->Form->request->data = array('User' => array('User' => array('value')));
  2508. $result = $this->Form->input('User.User', array('empty' => true));
  2509. $expected = array(
  2510. 'div' => array('class' => 'input select'),
  2511. 'label' => array('for' => 'UserUser'),
  2512. 'User',
  2513. '/label',
  2514. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2515. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2516. array('option' => array('value' => '')),
  2517. '/option',
  2518. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2519. 'good',
  2520. '/option',
  2521. array('option' => array('value' => 'other')),
  2522. 'bad',
  2523. '/option',
  2524. '/select',
  2525. '/div'
  2526. );
  2527. $this->assertTags($result, $expected);
  2528. $this->Form->data = array();
  2529. $result = $this->Form->input('Publisher.id', array(
  2530. 'label' => 'Publisher',
  2531. 'type' => 'select',
  2532. 'multiple' => 'checkbox',
  2533. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2534. ));
  2535. $expected = array(
  2536. array('div' => array('class' => 'input select')),
  2537. array('label' => array('for' => 'PublisherId')),
  2538. 'Publisher',
  2539. '/label',
  2540. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2541. array('div' => array('class' => 'checkbox')),
  2542. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2543. array('label' => array('for' => 'PublisherIdValue1')),
  2544. 'Label 1',
  2545. '/label',
  2546. '/div',
  2547. array('div' => array('class' => 'checkbox')),
  2548. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2549. array('label' => array('for' => 'PublisherIdValue2')),
  2550. 'Label 2',
  2551. '/label',
  2552. '/div',
  2553. '/div'
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. }
  2557. /**
  2558. * test that input() and a non standard primary key makes a hidden input by default.
  2559. *
  2560. * @return void
  2561. */
  2562. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2563. $this->Form->create('User');
  2564. $this->Form->fieldset = array(
  2565. 'User' => array(
  2566. 'fields' => array(
  2567. 'model_id' => array('type' => 'integer')
  2568. ),
  2569. 'validates' => array(),
  2570. 'key' => 'model_id'
  2571. )
  2572. );
  2573. $result = $this->Form->input('model_id');
  2574. $expected = array(
  2575. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2576. );
  2577. $this->assertTags($result, $expected);
  2578. }
  2579. /**
  2580. * test that overriding the magic select type widget is possible
  2581. *
  2582. * @return void
  2583. */
  2584. public function testInputOverridingMagicSelectType() {
  2585. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2586. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2587. $expected = array(
  2588. 'div' => array('class' => 'input text'),
  2589. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2590. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2591. '/div'
  2592. );
  2593. $this->assertTags($result, $expected);
  2594. //Check that magic types still work for plural/singular vars
  2595. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2596. $result = $this->Form->input('Model.type');
  2597. $expected = array(
  2598. 'div' => array('class' => 'input select'),
  2599. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2600. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2601. array('option' => array('value' => 'value')), 'good', '/option',
  2602. array('option' => array('value' => 'other')), 'bad', '/option',
  2603. '/select',
  2604. '/div'
  2605. );
  2606. $this->assertTags($result, $expected);
  2607. }
  2608. /**
  2609. * Test that inferred types do not override developer input
  2610. *
  2611. * @return void
  2612. */
  2613. public function testInputMagicTypeDoesNotOverride() {
  2614. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2615. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2616. $expected = array(
  2617. 'div' => array('class' => 'input checkbox'),
  2618. array('input' => array(
  2619. 'type' => 'hidden',
  2620. 'name' => 'data[Model][user]',
  2621. 'id' => 'ModelUser_',
  2622. 'value' => 0,
  2623. )),
  2624. array('input' => array(
  2625. 'name' => 'data[Model][user]',
  2626. 'type' => 'checkbox',
  2627. 'id' => 'ModelUser',
  2628. 'value' => 1
  2629. )),
  2630. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2631. '/div'
  2632. );
  2633. $this->assertTags($result, $expected);
  2634. }
  2635. /**
  2636. * Test that magic input() selects are created for type=number
  2637. *
  2638. * @return void
  2639. */
  2640. public function testInputMagicSelectForTypeNumber() {
  2641. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2642. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2643. $result = $this->Form->input('ValidateUser.balance');
  2644. $expected = array(
  2645. 'div' => array('class' => 'input select'),
  2646. 'label' => array('for' => 'ValidateUserBalance'),
  2647. 'Balance',
  2648. '/label',
  2649. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2650. array('option' => array('value' => '0')),
  2651. 'nothing',
  2652. '/option',
  2653. array('option' => array('value' => '1', 'selected' => 'selected')),
  2654. 'some',
  2655. '/option',
  2656. array('option' => array('value' => '100')),
  2657. 'a lot',
  2658. '/option',
  2659. '/select',
  2660. '/div'
  2661. );
  2662. $this->assertTags($result, $expected);
  2663. }
  2664. /**
  2665. * Test that magic input() selects can easily be converted into radio types without error.
  2666. *
  2667. * @return void
  2668. */
  2669. public function testInputMagicSelectChangeToRadio() {
  2670. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2671. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2672. $this->assertRegExp('/input type="radio"/', $result);
  2673. }
  2674. /**
  2675. * fields with the same name as the model should work.
  2676. *
  2677. * @return void
  2678. */
  2679. public function testInputWithMatchingFieldAndModelName() {
  2680. $this->Form->create('User');
  2681. $this->Form->fieldset = array(
  2682. 'User' => array(
  2683. 'fields' => array(
  2684. 'User' => array('type' => 'text')
  2685. ),
  2686. 'validates' => array(),
  2687. 'key' => 'id'
  2688. )
  2689. );
  2690. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2691. $result = $this->Form->input('User', array('type' => 'text'));
  2692. $expected = array(
  2693. 'div' => array('class' => 'input text'),
  2694. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2695. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2696. '/div'
  2697. );
  2698. $this->assertTags($result, $expected);
  2699. }
  2700. /**
  2701. * testFormInputs method
  2702. *
  2703. * test correct results from form::inputs().
  2704. *
  2705. * @return void
  2706. */
  2707. public function testFormInputs() {
  2708. $this->Form->create('Contact');
  2709. $result = $this->Form->inputs('The Legend');
  2710. $expected = array(
  2711. '<fieldset',
  2712. '<legend',
  2713. 'The Legend',
  2714. '/legend',
  2715. '*/fieldset',
  2716. );
  2717. $this->assertTags($result, $expected);
  2718. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2719. $expected = array(
  2720. 'fieldset' => array('class' => 'classy-stuff'),
  2721. '<legend',
  2722. 'Field of Dreams',
  2723. '/legend',
  2724. '*/fieldset'
  2725. );
  2726. $this->assertTags($result, $expected);
  2727. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2728. $this->assertTags($result, $expected);
  2729. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2730. $this->assertTags($result, $expected);
  2731. $this->Form->create('Contact');
  2732. $this->Form->request['prefix'] = 'admin';
  2733. $this->Form->request['action'] = 'admin_edit';
  2734. $result = $this->Form->inputs();
  2735. $expected = array(
  2736. '<fieldset',
  2737. '<legend',
  2738. 'Edit Contact',
  2739. '/legend',
  2740. '*/fieldset',
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. $this->Form->create('Contact');
  2744. $result = $this->Form->inputs(false);
  2745. $expected = array(
  2746. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2747. array('div' => array('class' => 'input text')),
  2748. '*/div',
  2749. array('div' => array('class' => 'input email')),
  2750. '*/div',
  2751. array('div' => array('class' => 'input tel')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input password')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input date')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input date')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input datetime')),
  2760. '*/div',
  2761. array('div' => array('class' => 'input number')),
  2762. '*/div',
  2763. array('div' => array('class' => 'input select')),
  2764. '*/div',
  2765. );
  2766. $this->assertTags($result, $expected);
  2767. $this->Form->create('Contact');
  2768. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2769. $expected = array(
  2770. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2771. array('div' => array('class' => 'input text')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input email')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input tel')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input password')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input date')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input date')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input datetime')),
  2784. '*/div',
  2785. array('div' => array('class' => 'input number')),
  2786. '*/div',
  2787. array('div' => array('class' => 'input select')),
  2788. '*/div',
  2789. );
  2790. $this->assertTags($result, $expected);
  2791. $this->Form->create('Contact');
  2792. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2793. $this->assertTags($result, $expected);
  2794. $this->Form->create('Contact');
  2795. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2796. $expected = array(
  2797. 'fieldset' => array(),
  2798. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2799. array('div' => array('class' => 'input text')),
  2800. '*/div',
  2801. array('div' => array('class' => 'input email')),
  2802. '*/div',
  2803. array('div' => array('class' => 'input tel')),
  2804. '*/div',
  2805. array('div' => array('class' => 'input password')),
  2806. '*/div',
  2807. array('div' => array('class' => 'input date')),
  2808. '*/div',
  2809. array('div' => array('class' => 'input date')),
  2810. '*/div',
  2811. array('div' => array('class' => 'input datetime')),
  2812. '*/div',
  2813. array('div' => array('class' => 'input number')),
  2814. '*/div',
  2815. array('div' => array('class' => 'input select')),
  2816. '*/div',
  2817. '/fieldset'
  2818. );
  2819. $this->assertTags($result, $expected);
  2820. $this->Form->create('Contact');
  2821. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2822. $expected = array(
  2823. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2824. array('div' => array('class' => 'input text')),
  2825. '*/div',
  2826. array('div' => array('class' => 'input email')),
  2827. '*/div',
  2828. array('div' => array('class' => 'input tel')),
  2829. '*/div',
  2830. array('div' => array('class' => 'input password')),
  2831. '*/div',
  2832. array('div' => array('class' => 'input date')),
  2833. '*/div',
  2834. array('div' => array('class' => 'input date')),
  2835. '*/div',
  2836. array('div' => array('class' => 'input datetime')),
  2837. '*/div',
  2838. array('div' => array('class' => 'input number')),
  2839. '*/div',
  2840. array('div' => array('class' => 'input select')),
  2841. '*/div',
  2842. );
  2843. $this->assertTags($result, $expected);
  2844. $this->Form->create('Contact');
  2845. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2846. $this->assertTags($result, $expected);
  2847. $this->Form->create('Contact');
  2848. $result = $this->Form->inputs('Hello');
  2849. $expected = array(
  2850. 'fieldset' => array(),
  2851. 'legend' => array(),
  2852. 'Hello',
  2853. '/legend',
  2854. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2855. array('div' => array('class' => 'input text')),
  2856. '*/div',
  2857. array('div' => array('class' => 'input email')),
  2858. '*/div',
  2859. array('div' => array('class' => 'input tel')),
  2860. '*/div',
  2861. array('div' => array('class' => 'input password')),
  2862. '*/div',
  2863. array('div' => array('class' => 'input date')),
  2864. '*/div',
  2865. array('div' => array('class' => 'input date')),
  2866. '*/div',
  2867. array('div' => array('class' => 'input datetime')),
  2868. '*/div',
  2869. array('div' => array('class' => 'input number')),
  2870. '*/div',
  2871. array('div' => array('class' => 'input select')),
  2872. '*/div',
  2873. '/fieldset'
  2874. );
  2875. $this->assertTags($result, $expected);
  2876. $this->Form->create('Contact');
  2877. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2878. $expected = array(
  2879. 'fieldset' => array(),
  2880. 'legend' => array(),
  2881. 'Hello',
  2882. '/legend',
  2883. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2884. array('div' => array('class' => 'input text')),
  2885. '*/div',
  2886. array('div' => array('class' => 'input email')),
  2887. '*/div',
  2888. array('div' => array('class' => 'input tel')),
  2889. '*/div',
  2890. array('div' => array('class' => 'input password')),
  2891. '*/div',
  2892. array('div' => array('class' => 'input date')),
  2893. '*/div',
  2894. array('div' => array('class' => 'input date')),
  2895. '*/div',
  2896. array('div' => array('class' => 'input datetime')),
  2897. '*/div',
  2898. array('div' => array('class' => 'input number')),
  2899. '*/div',
  2900. array('div' => array('class' => 'input select')),
  2901. '*/div',
  2902. '/fieldset'
  2903. );
  2904. $this->assertTags($result, $expected);
  2905. $this->Form->create('Contact');
  2906. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2907. $this->assertTags($result, $expected);
  2908. $this->Form->end();
  2909. $this->Form->create(false);
  2910. $expected = array(
  2911. 'fieldset' => array(),
  2912. array('div' => array('class' => 'input text')),
  2913. 'label' => array('for' => 'foo'),
  2914. 'Foo',
  2915. '/label',
  2916. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2917. '*/div',
  2918. '/fieldset'
  2919. );
  2920. $result = $this->Form->inputs(
  2921. array('foo' => array('type' => 'text')),
  2922. array(),
  2923. array('legend' => false)
  2924. );
  2925. $this->assertTags($result, $expected);
  2926. }
  2927. /**
  2928. * Tests inputs() works with plugin models
  2929. *
  2930. * @return void
  2931. */
  2932. public function testInputsPluginModel() {
  2933. $this->loadFixtures('Post');
  2934. App::build(array(
  2935. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2936. ));
  2937. CakePlugin::load('TestPlugin');
  2938. $this->Form->request['models'] = array(
  2939. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2940. );
  2941. $this->Form->create('TestPlugin.TestPluginPost');
  2942. $result = $this->Form->inputs();
  2943. $this->assertContains('data[TestPluginPost][id]', $result);
  2944. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2945. $this->assertContains('data[TestPluginPost][title]', $result);
  2946. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2947. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2948. $this->assertEquals('TestPluginPost', $this->Form->model());
  2949. }
  2950. /**
  2951. * testSelectAsCheckbox method
  2952. *
  2953. * test multi-select widget with checkbox formatting.
  2954. *
  2955. * @return void
  2956. */
  2957. public function testSelectAsCheckbox() {
  2958. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2959. $expected = array(
  2960. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2961. array('div' => array('class' => 'checkbox')),
  2962. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2963. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2964. 'first',
  2965. '/label',
  2966. '/div',
  2967. array('div' => array('class' => 'checkbox')),
  2968. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2969. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2970. 'second',
  2971. '/label',
  2972. '/div',
  2973. array('div' => array('class' => 'checkbox')),
  2974. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2975. array('label' => array('for' => 'ModelMultiField2')),
  2976. 'third',
  2977. '/label',
  2978. '/div',
  2979. );
  2980. $this->assertTags($result, $expected);
  2981. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2982. $expected = array(
  2983. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2984. array('div' => array('class' => 'checkbox')),
  2985. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/2')),
  2986. array('label' => array('for' => 'ModelMultiField1/2')),
  2987. 'half',
  2988. '/label',
  2989. '/div',
  2990. );
  2991. $this->assertTags($result, $expected);
  2992. }
  2993. /**
  2994. * testLabel method
  2995. *
  2996. * test label generation.
  2997. *
  2998. * @return void
  2999. */
  3000. public function testLabel() {
  3001. $this->Form->text('Person.name');
  3002. $result = $this->Form->label();
  3003. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3004. $this->Form->text('Person.name');
  3005. $result = $this->Form->label();
  3006. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3007. $result = $this->Form->label('Person.first_name');
  3008. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3009. $result = $this->Form->label('Person.first_name', 'Your first name');
  3010. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3011. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3012. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3013. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3014. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3015. $result = $this->Form->label('Person.first_name', '');
  3016. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3017. $result = $this->Form->label('Person.2.name', '');
  3018. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3019. }
  3020. /**
  3021. * testTextbox method
  3022. *
  3023. * test textbox element generation
  3024. *
  3025. * @return void
  3026. */
  3027. public function testTextbox() {
  3028. $result = $this->Form->text('Model.field');
  3029. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3030. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3031. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3032. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3033. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3034. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3035. $result = $this->Form->text('Model.text');
  3036. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3037. $Contact = ClassRegistry::getObject('Contact');
  3038. $Contact->validationErrors['text'] = array(true);
  3039. $this->Form->request->data['Contact']['text'] = 'test';
  3040. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3041. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3042. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3043. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3044. $expected = array(
  3045. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3046. );
  3047. $this->assertTags($result, $expected);
  3048. }
  3049. /**
  3050. * testDefaultValue method
  3051. *
  3052. * Test default value setting
  3053. *
  3054. * @return void
  3055. */
  3056. public function testDefaultValue() {
  3057. $this->Form->request->data['Model']['field'] = 'test';
  3058. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3059. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3060. unset($this->Form->request->data['Model']['field']);
  3061. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3062. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3063. }
  3064. /**
  3065. * testCheckboxDefaultValue method
  3066. *
  3067. * Test default value setting on checkbox() method
  3068. *
  3069. * @return void
  3070. */
  3071. public function testCheckboxDefaultValue() {
  3072. $this->Form->request->data['Model']['field'] = false;
  3073. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3074. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3075. unset($this->Form->request->data['Model']['field']);
  3076. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3077. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3078. $this->Form->request->data['Model']['field'] = true;
  3079. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3080. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3081. unset($this->Form->request->data['Model']['field']);
  3082. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3083. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3084. }
  3085. /**
  3086. * testError method
  3087. *
  3088. * Test field error generation
  3089. *
  3090. * @return void
  3091. */
  3092. public function testError() {
  3093. $Contact = ClassRegistry::getObject('Contact');
  3094. $Contact->validationErrors['field'] = array(1);
  3095. $result = $this->Form->error('Contact.field');
  3096. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3097. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3098. $this->assertEquals('Error in field Field', $result);
  3099. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3100. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3101. $this->assertEquals('This field contains invalid input', $result);
  3102. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3103. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3104. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3105. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3106. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3107. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3108. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3109. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3110. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3111. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3112. $this->assertEquals('<strong>Badness!</strong>', $result);
  3113. $Contact->validationErrors['field'] = array("email");
  3114. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3115. $expected = array(
  3116. 'div' => array('class' => 'field-error'),
  3117. 'No good!',
  3118. '/div'
  3119. );
  3120. $this->assertTags($result, $expected);
  3121. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3122. $result = $this->Form->error('Contact.field', array(
  3123. 'notEmpty' => 'Cannot be empty',
  3124. 'email' => 'No good!'
  3125. ));
  3126. $expected = array(
  3127. 'div' => array('class' => 'error-message'),
  3128. 'ul' => array(),
  3129. '<li', 'Cannot be empty', '/li',
  3130. '<li', 'No good!', '/li',
  3131. '<li', 'Something else', '/li',
  3132. '/ul',
  3133. '/div'
  3134. );
  3135. $this->assertTags($result, $expected);
  3136. // Testing error messages list options
  3137. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3138. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3139. $expected = array(
  3140. 'div' => array('class' => 'error-message'),
  3141. 'ol' => array(),
  3142. '<li', 'notEmpty', '/li',
  3143. '<li', 'email', '/li',
  3144. '/ol',
  3145. '/div'
  3146. );
  3147. $this->assertTags($result, $expected);
  3148. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3149. $expected = array(
  3150. 'div' => array('class' => 'error-message'),
  3151. 'ol' => array(),
  3152. '<li', 'notEmpty', '/li',
  3153. '<li', 'email', '/li',
  3154. '/ol',
  3155. '/div'
  3156. );
  3157. $this->assertTags($result, $expected);
  3158. $result = $this->Form->error('Contact.field', null, array(
  3159. 'listOptions' => array(
  3160. 'class' => 'ul-class',
  3161. 'itemOptions' => array(
  3162. 'class' => 'li-class'
  3163. )
  3164. )
  3165. ));
  3166. $expected = array(
  3167. 'div' => array('class' => 'error-message'),
  3168. 'ul' => array('class' => 'ul-class'),
  3169. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3170. array('li' => array('class' => 'li-class')), 'email', '/li',
  3171. '/ul',
  3172. '/div'
  3173. );
  3174. $this->assertTags($result, $expected);
  3175. }
  3176. /**
  3177. * test error options when using form->input();
  3178. *
  3179. * @return void
  3180. */
  3181. public function testInputErrorEscape() {
  3182. $this->Form->create('ValidateProfile');
  3183. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3184. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3185. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3186. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3187. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3188. $this->assertRegExp('/required<br>/', $result);
  3189. }
  3190. /**
  3191. * testPassword method
  3192. *
  3193. * Test password element generation
  3194. *
  3195. * @return void
  3196. */
  3197. public function testPassword() {
  3198. $Contact = ClassRegistry::getObject('Contact');
  3199. $result = $this->Form->password('Contact.field');
  3200. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3201. $Contact->validationErrors['passwd'] = 1;
  3202. $this->Form->request->data['Contact']['passwd'] = 'test';
  3203. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3204. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3205. }
  3206. /**
  3207. * testRadio method
  3208. *
  3209. * Test radio element set generation
  3210. *
  3211. * @return void
  3212. */
  3213. public function testRadio() {
  3214. $result = $this->Form->radio('Model.field', array('option A'));
  3215. $expected = array(
  3216. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3218. 'label' => array('for' => 'ModelField0'),
  3219. 'option A',
  3220. '/label'
  3221. );
  3222. $this->assertTags($result, $expected);
  3223. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3224. $expected = array(
  3225. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3226. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField1/2')),
  3227. 'label' => array('for' => 'ModelField1/2'),
  3228. 'half',
  3229. '/label'
  3230. );
  3231. $this->assertTags($result, $expected);
  3232. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3233. $expected = array(
  3234. 'fieldset' => array(),
  3235. 'legend' => array(),
  3236. 'Field',
  3237. '/legend',
  3238. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3240. array('label' => array('for' => 'ModelField0')),
  3241. 'option A',
  3242. '/label',
  3243. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3244. array('label' => array('for' => 'ModelField1')),
  3245. 'option B',
  3246. '/label',
  3247. '/fieldset'
  3248. );
  3249. $this->assertTags($result, $expected);
  3250. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3251. $expected = array(
  3252. 'fieldset' => array(),
  3253. 'legend' => array(),
  3254. 'Field',
  3255. '/legend',
  3256. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3257. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3258. array('label' => array('for' => 'ModelField0')),
  3259. 'option A',
  3260. '/label',
  3261. 'br' => array(),
  3262. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3263. array('label' => array('for' => 'ModelField1')),
  3264. 'option B',
  3265. '/label',
  3266. '/fieldset'
  3267. );
  3268. $this->assertTags($result, $expected);
  3269. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3270. $expected = array(
  3271. 'fieldset' => array(),
  3272. 'legend' => array(),
  3273. 'Gender',
  3274. '/legend',
  3275. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3276. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3277. array('label' => array('for' => 'EmployeeGenderMale')),
  3278. 'Male',
  3279. '/label',
  3280. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3281. array('label' => array('for' => 'EmployeeGenderFemale')),
  3282. 'Female',
  3283. '/label',
  3284. '/fieldset',
  3285. );
  3286. $this->assertTags($result, $expected);
  3287. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3288. $expected = array(
  3289. 'fieldset' => array(),
  3290. 'legend' => array(),
  3291. 'Gender',
  3292. '/legend',
  3293. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3294. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3295. array('label' => array('for' => 'OfficerGenderMale')),
  3296. 'Male',
  3297. '/label',
  3298. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3299. array('label' => array('for' => 'OfficerGenderFemale')),
  3300. 'Female',
  3301. '/label',
  3302. '/fieldset',
  3303. );
  3304. $this->assertTags($result, $expected);
  3305. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3306. $expected = array(
  3307. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3308. array('input' => array(
  3309. 'type' => 'radio',
  3310. 'name' => 'data[Contact][1][imrequired]',
  3311. 'value' => '0',
  3312. 'id' => 'Contact1Imrequired0',
  3313. 'required' => 'required'
  3314. )),
  3315. 'label' => array('for' => 'Contact1Imrequired0'),
  3316. 'option A',
  3317. '/label'
  3318. );
  3319. $this->assertTags($result, $expected);
  3320. $result = $this->Form->radio('Model.1.field', array('option A'));
  3321. $expected = array(
  3322. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3323. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3324. 'label' => array('for' => 'Model1Field0'),
  3325. 'option A',
  3326. '/label'
  3327. );
  3328. $this->assertTags($result, $expected);
  3329. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3330. $expected = array(
  3331. 'fieldset' => array(),
  3332. 'legend' => array(),
  3333. 'Field',
  3334. '/legend',
  3335. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3336. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3337. array('label' => array('for' => 'ModelField0')),
  3338. 'option A',
  3339. '/label',
  3340. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3341. array('label' => array('for' => 'ModelField1')),
  3342. 'option B',
  3343. '/label',
  3344. '/fieldset'
  3345. );
  3346. $this->assertTags($result, $expected);
  3347. $result = $this->Form->radio(
  3348. 'Model.field',
  3349. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3350. );
  3351. $expected = array(
  3352. 'fieldset' => array(),
  3353. 'legend' => array(),
  3354. 'Field',
  3355. '/legend',
  3356. 'input' => array(
  3357. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3358. 'id' => 'ModelField_', 'value' => '',
  3359. ),
  3360. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3361. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3362. array('label' => array('for' => 'ModelFieldAB')),
  3363. 'first',
  3364. '/label',
  3365. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3366. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3367. array('label' => array('for' => 'ModelFieldAB1')),
  3368. 'second',
  3369. '/label',
  3370. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3371. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3372. array('label' => array('for' => 'ModelFieldAB2')),
  3373. 'third',
  3374. '/label',
  3375. '/fieldset'
  3376. );
  3377. $this->assertTags($result, $expected);
  3378. }
  3379. /**
  3380. * Test radio inputs with between as string or array. Also ensure
  3381. * that an array with less between elements works.
  3382. *
  3383. * @return void
  3384. */
  3385. public function testRadioBetween() {
  3386. $result = $this->Form->radio(
  3387. 'Model.field',
  3388. array('option A', 'option B'),
  3389. array('between' => 'I am between')
  3390. );
  3391. $expected = array(
  3392. 'fieldset' => array(),
  3393. 'legend' => array(),
  3394. 'Field',
  3395. '/legend',
  3396. 'I am between',
  3397. 'input' => array(
  3398. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3399. 'value' => '', 'id' => 'ModelField_'
  3400. ),
  3401. array('input' => array(
  3402. 'type' => 'radio', 'name' => 'data[Model][field]',
  3403. 'value' => '0', 'id' => 'ModelField0'
  3404. )),
  3405. array('label' => array('for' => 'ModelField0')),
  3406. 'option A',
  3407. '/label',
  3408. array('input' => array(
  3409. 'type' => 'radio', 'name' => 'data[Model][field]',
  3410. 'value' => '1', 'id' => 'ModelField1'
  3411. )),
  3412. array('label' => array('for' => 'ModelField1')),
  3413. 'option B',
  3414. '/label',
  3415. '/fieldset'
  3416. );
  3417. $this->assertTags($result, $expected);
  3418. $result = $this->Form->radio(
  3419. 'Model.field',
  3420. array('option A', 'option B', 'option C'),
  3421. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3422. );
  3423. $expected = array(
  3424. 'fieldset' => array(),
  3425. 'legend' => array(),
  3426. 'Field',
  3427. '/legend',
  3428. 'input' => array(
  3429. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3430. 'value' => '', 'id' => 'ModelField_'
  3431. ),
  3432. array('input' => array(
  3433. 'type' => 'radio', 'name' => 'data[Model][field]',
  3434. 'value' => '0', 'id' => 'ModelField0'
  3435. )),
  3436. array('label' => array('for' => 'ModelField0')),
  3437. 'option A',
  3438. '/label',
  3439. 'between A',
  3440. '--separator--',
  3441. array('input' => array(
  3442. 'type' => 'radio', 'name' => 'data[Model][field]',
  3443. 'value' => '1', 'id' => 'ModelField1'
  3444. )),
  3445. array('label' => array('for' => 'ModelField1')),
  3446. 'option B',
  3447. '/label',
  3448. 'between B',
  3449. '--separator--',
  3450. array('input' => array(
  3451. 'type' => 'radio', 'name' => 'data[Model][field]',
  3452. 'value' => '2', 'id' => 'ModelField2'
  3453. )),
  3454. array('label' => array('for' => 'ModelField2')),
  3455. 'option C',
  3456. '/label',
  3457. 'between C',
  3458. '/fieldset'
  3459. );
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->input('Model.field', array(
  3462. 'options' => array('1' => 'first', '2' => 'second'),
  3463. 'type' => 'radio',
  3464. 'before' => '--before--',
  3465. 'after' => '--after--',
  3466. 'separator' => '--separator--',
  3467. 'between' => array('--between first--', '--between second--')
  3468. ));
  3469. $expected = array(
  3470. 'div' => array('class' => 'input radio'),
  3471. '--before--',
  3472. 'fieldset' => array(),
  3473. 'legend' => array(),
  3474. 'Field',
  3475. '/legend',
  3476. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3477. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3478. array('label' => array('for' => 'ModelField1')),
  3479. 'first',
  3480. '/label',
  3481. '--between first--',
  3482. '--separator--',
  3483. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3484. array('label' => array('for' => 'ModelField2')),
  3485. 'second',
  3486. '/label',
  3487. '--between second--',
  3488. '/fieldset',
  3489. '--after--',
  3490. '/div'
  3491. );
  3492. $this->assertTags($result, $expected);
  3493. $result = $this->Form->input('Model.field', array(
  3494. 'options' => array('1' => 'first', '2' => 'second'),
  3495. 'type' => 'radio',
  3496. 'before' => '--before--',
  3497. 'after' => '--after--',
  3498. 'separator' => '--separator--',
  3499. 'between' => array('--between first--')
  3500. ));
  3501. $expected = array(
  3502. 'div' => array('class' => 'input radio'),
  3503. '--before--',
  3504. 'fieldset' => array(),
  3505. 'legend' => array(),
  3506. 'Field',
  3507. '/legend',
  3508. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3509. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3510. array('label' => array('for' => 'ModelField1')),
  3511. 'first',
  3512. '/label',
  3513. '--between first--',
  3514. '--separator--',
  3515. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3516. array('label' => array('for' => 'ModelField2')),
  3517. 'second',
  3518. '/label',
  3519. '/fieldset',
  3520. '--after--',
  3521. '/div'
  3522. );
  3523. $this->assertTags($result, $expected);
  3524. }
  3525. /**
  3526. * Test that radios with a 0 value are selected under the correct conditions.
  3527. * Also ensure that values that are booleanish are handled correctly.
  3528. *
  3529. * @return void
  3530. */
  3531. public function testRadioOptionWithBooleanishValues() {
  3532. $expected = array(
  3533. 'fieldset' => array(),
  3534. 'legend' => array(),
  3535. 'Field',
  3536. '/legend',
  3537. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3538. array('label' => array('for' => 'ModelField1')),
  3539. 'Yes',
  3540. '/label',
  3541. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3542. array('label' => array('for' => 'ModelField0')),
  3543. 'No',
  3544. '/label',
  3545. '/fieldset'
  3546. );
  3547. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3548. $this->assertTags($result, $expected);
  3549. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3550. $this->assertTags($result, $expected);
  3551. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3552. $this->assertTags($result, $expected);
  3553. $expected = array(
  3554. 'fieldset' => array(),
  3555. 'legend' => array(),
  3556. 'Field',
  3557. '/legend',
  3558. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3559. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3560. array('label' => array('for' => 'ModelField1')),
  3561. 'Yes',
  3562. '/label',
  3563. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3564. array('label' => array('for' => 'ModelField0')),
  3565. 'No',
  3566. '/label',
  3567. '/fieldset'
  3568. );
  3569. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3570. $this->assertTags($result, $expected);
  3571. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3572. $this->assertTags($result, $expected);
  3573. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3574. $this->assertTags($result, $expected);
  3575. $expected = array(
  3576. 'fieldset' => array(),
  3577. 'legend' => array(),
  3578. 'Field',
  3579. '/legend',
  3580. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3581. array('label' => array('for' => 'ModelField1')),
  3582. 'Yes',
  3583. '/label',
  3584. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3585. array('label' => array('for' => 'ModelField0')),
  3586. 'No',
  3587. '/label',
  3588. '/fieldset'
  3589. );
  3590. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3591. $this->assertTags($result, $expected);
  3592. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3593. $this->assertTags($result, $expected);
  3594. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3595. $this->assertTags($result, $expected);
  3596. }
  3597. /**
  3598. * test disabled radio options
  3599. *
  3600. * @return void
  3601. */
  3602. public function testRadioDisabled() {
  3603. $result = $this->Form->radio(
  3604. 'Model.field',
  3605. array('option A', 'option B'),
  3606. array('disabled' => array(0), 'value' => '0')
  3607. );
  3608. $expected = array(
  3609. 'fieldset' => array(),
  3610. 'legend' => array(),
  3611. 'Field',
  3612. '/legend',
  3613. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3614. array('label' => array('for' => 'ModelField0')),
  3615. 'option A',
  3616. '/label',
  3617. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3618. array('label' => array('for' => 'ModelField1')),
  3619. 'option B',
  3620. '/label',
  3621. '/fieldset'
  3622. );
  3623. $this->assertTags($result, $expected);
  3624. $result = $this->Form->radio(
  3625. 'Model.field',
  3626. array('option A', 'option B'),
  3627. array('disabled' => true, 'value' => '0')
  3628. );
  3629. $expected = array(
  3630. 'fieldset' => array(),
  3631. 'legend' => array(),
  3632. 'Field',
  3633. '/legend',
  3634. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3635. array('label' => array('for' => 'ModelField0')),
  3636. 'option A',
  3637. '/label',
  3638. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3639. array('label' => array('for' => 'ModelField1')),
  3640. 'option B',
  3641. '/label',
  3642. '/fieldset'
  3643. );
  3644. $this->assertTags($result, $expected);
  3645. $result = $this->Form->radio(
  3646. 'Model.field',
  3647. array('option A', 'option B'),
  3648. array('disabled' => 'disabled', 'value' => '0')
  3649. );
  3650. $expected = array(
  3651. 'fieldset' => array(),
  3652. 'legend' => array(),
  3653. 'Field',
  3654. '/legend',
  3655. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3656. array('label' => array('for' => 'ModelField0')),
  3657. 'option A',
  3658. '/label',
  3659. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3660. array('label' => array('for' => 'ModelField1')),
  3661. 'option B',
  3662. '/label',
  3663. '/fieldset'
  3664. );
  3665. $this->assertTags($result, $expected);
  3666. $result = $this->Form->input('Model.field', array(
  3667. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3668. 'type' => 'radio',
  3669. 'disabled' => array(2, '3x'),
  3670. ));
  3671. $expected = array(
  3672. 'div' => array('class' => 'input radio'),
  3673. 'fieldset' => array(),
  3674. 'legend' => array(),
  3675. 'Field',
  3676. '/legend',
  3677. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3678. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3679. array('label' => array('for' => 'ModelField1')),
  3680. 'first',
  3681. '/label',
  3682. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3683. array('label' => array('for' => 'ModelField2')),
  3684. 'second',
  3685. '/label',
  3686. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3687. array('label' => array('for' => 'ModelField2x')),
  3688. '2x',
  3689. '/label',
  3690. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3691. array('label' => array('for' => 'ModelField3')),
  3692. 'third',
  3693. '/label',
  3694. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3695. array('label' => array('for' => 'ModelField3x')),
  3696. '3x',
  3697. '/label',
  3698. '/fieldset',
  3699. '/div'
  3700. );
  3701. $this->assertTags($result, $expected);
  3702. $result = $this->Form->input('Model.field', array(
  3703. 'type' => 'radio',
  3704. 'options' => array(
  3705. 1 => 'A',
  3706. 2 => 'B',
  3707. 3 => 'C'
  3708. ),
  3709. 'disabled' => array(1)
  3710. ));
  3711. $expected = array(
  3712. 'div' => array('class' => 'input radio'),
  3713. 'fieldset' => array(),
  3714. 'legend' => array(),
  3715. 'Field',
  3716. '/legend',
  3717. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3718. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3719. array('label' => array('for' => 'ModelField1')),
  3720. 'A',
  3721. '/label',
  3722. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3723. array('label' => array('for' => 'ModelField2')),
  3724. 'B',
  3725. '/label',
  3726. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3727. array('label' => array('for' => 'ModelField3')),
  3728. 'C',
  3729. '/label',
  3730. '/fieldset',
  3731. '/div'
  3732. );
  3733. $this->assertTags($result, $expected);
  3734. }
  3735. /**
  3736. * test disabling the hidden input for radio buttons
  3737. *
  3738. * @return void
  3739. */
  3740. public function testRadioHiddenInputDisabling() {
  3741. $result = $this->Form->input('Model.1.field', array(
  3742. 'type' => 'radio',
  3743. 'options' => array('option A'),
  3744. 'hiddenField' => false
  3745. )
  3746. );
  3747. $expected = array(
  3748. 'div' => array('class' => 'input radio'),
  3749. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3750. 'label' => array('for' => 'Model1Field0'),
  3751. 'option A',
  3752. '/label',
  3753. '/div'
  3754. );
  3755. $this->assertTags($result, $expected);
  3756. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3757. $expected = array(
  3758. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3759. 'label' => array('for' => 'Model1Field0'),
  3760. 'option A',
  3761. '/label'
  3762. );
  3763. $this->assertTags($result, $expected);
  3764. }
  3765. /**
  3766. * test adding an empty option for radio buttons
  3767. *
  3768. * @return void
  3769. */
  3770. public function testRadioAddEmptyOption() {
  3771. $result = $this->Form->input('Model.1.field', array(
  3772. 'type' => 'radio',
  3773. 'options' => array('option A'),
  3774. 'empty' => true,
  3775. 'hiddenField' => false
  3776. ));
  3777. $expected = array(
  3778. 'div' => array('class' => 'input radio'),
  3779. 'fieldset' => array(),
  3780. 'legend' => array(),
  3781. 'Field',
  3782. '/legend',
  3783. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3784. array('label' => array('for' => 'Model1Field')),
  3785. __('empty'),
  3786. '/label',
  3787. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3788. array('label' => array('for' => 'Model1Field0')),
  3789. 'option A',
  3790. '/label',
  3791. '/fieldset',
  3792. '/div'
  3793. );
  3794. $this->assertTags($result, $expected);
  3795. $result = $this->Form->input('Model.1.field', array(
  3796. 'type' => 'radio',
  3797. 'options' => array('option A'),
  3798. 'empty' => 'CustomEmptyLabel',
  3799. 'hiddenField' => false
  3800. ));
  3801. $expected = array(
  3802. 'div' => array('class' => 'input radio'),
  3803. 'fieldset' => array(),
  3804. 'legend' => array(),
  3805. 'Field',
  3806. '/legend',
  3807. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3808. array('label' => array('for' => 'Model1Field')),
  3809. 'CustomEmptyLabel',
  3810. '/label',
  3811. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3812. array('label' => array('for' => 'Model1Field0')),
  3813. 'option A',
  3814. '/label',
  3815. '/fieldset',
  3816. '/div'
  3817. );
  3818. $this->assertTags($result, $expected);
  3819. $result = $this->Form->input('Model.1.field', array(
  3820. 'type' => 'radio',
  3821. 'options' => array('option A'),
  3822. 'empty' => false,
  3823. 'hiddenField' => false
  3824. ));
  3825. $this->assertTextNotContains('"Model1Field"', $result);
  3826. }
  3827. /**
  3828. * Test that radio() accepts an array for label
  3829. *
  3830. * @return void
  3831. */
  3832. public function testRadioLabelArray() {
  3833. $result = $this->Form->input('Model.field', array(
  3834. 'type' => 'radio',
  3835. 'legend' => false,
  3836. 'label' => array(
  3837. 'class' => 'checkbox float-left',
  3838. ),
  3839. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3840. ));
  3841. $this->assertTextContains(
  3842. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3843. $result
  3844. );
  3845. }
  3846. /**
  3847. * Test that label id's match the input element id's when radio is called after create().
  3848. *
  3849. * @return void
  3850. */
  3851. public function testRadioWithCreate() {
  3852. $this->Form->create('Model');
  3853. $result = $this->Form->radio('recipient',
  3854. array('1' => '1', '2' => '2', '3' => '3'),
  3855. array('legend' => false, 'value' => '1')
  3856. );
  3857. $this->assertTextNotContains(
  3858. '<label for="ModelModelRecipient1">1</label>',
  3859. $result
  3860. );
  3861. $this->assertTextContains(
  3862. '<label for="ModelRecipient1">1</label>',
  3863. $result
  3864. );
  3865. }
  3866. /**
  3867. * testDomIdSuffix method
  3868. *
  3869. * @return void
  3870. */
  3871. public function testDomIdSuffix() {
  3872. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3873. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3874. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3875. $this->assertEquals('AbcX=FooY=Bar', $result);
  3876. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3877. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3878. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3879. $this->assertEquals('AbcXFooYBar', $result);
  3880. }
  3881. /**
  3882. * testDomIdSuffixCollisionResolvement()
  3883. *
  3884. * @return void
  3885. */
  3886. public function testDomIdSuffixCollisionResolvement() {
  3887. $result = $this->Form->domIdSuffix('a>b');
  3888. $this->assertEquals('AB', $result);
  3889. $result = $this->Form->domIdSuffix('a<b');
  3890. $this->assertEquals('AB1', $result);
  3891. $result = $this->Form->domIdSuffix('a\'b');
  3892. $this->assertEquals('AB2', $result);
  3893. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3894. $this->assertEquals('1StringWith1-dollar', $result);
  3895. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3896. $this->assertEquals('1StringWith1-dollar1', $result);
  3897. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3898. $this->assertEquals('1StringWith1-dollar2', $result);
  3899. }
  3900. /**
  3901. * testSelect method
  3902. *
  3903. * Test select element generation.
  3904. *
  3905. * @return void
  3906. */
  3907. public function testSelect() {
  3908. $result = $this->Form->select('Model.field', array());
  3909. $expected = array(
  3910. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3911. array('option' => array('value' => '')),
  3912. '/option',
  3913. '/select'
  3914. );
  3915. $this->assertTags($result, $expected);
  3916. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3917. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3918. $expected = array(
  3919. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3920. array('option' => array('value' => '')),
  3921. '/option',
  3922. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3923. 'good',
  3924. '/option',
  3925. array('option' => array('value' => 'other')),
  3926. 'bad',
  3927. '/option',
  3928. '/select'
  3929. );
  3930. $this->assertTags($result, $expected);
  3931. $this->Form->request->data = array();
  3932. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3933. $expected = array(
  3934. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3935. array('option' => array('value' => '')),
  3936. '/option',
  3937. array('option' => array('value' => 'value')),
  3938. 'good',
  3939. '/option',
  3940. array('option' => array('value' => 'other')),
  3941. 'bad',
  3942. '/option',
  3943. '/select'
  3944. );
  3945. $this->assertTags($result, $expected);
  3946. $result = $this->Form->select(
  3947. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3948. array('empty' => false)
  3949. );
  3950. $expected = array(
  3951. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3952. array('option' => array('value' => 'first')),
  3953. 'first &quot;html&quot; &lt;chars&gt;',
  3954. '/option',
  3955. array('option' => array('value' => 'second')),
  3956. 'value',
  3957. '/option',
  3958. '/select'
  3959. );
  3960. $this->assertTags($result, $expected);
  3961. $result = $this->Form->select(
  3962. 'Model.field',
  3963. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3964. array('escape' => false, 'empty' => false)
  3965. );
  3966. $expected = array(
  3967. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3968. array('option' => array('value' => 'first')),
  3969. 'first "html" <chars>',
  3970. '/option',
  3971. array('option' => array('value' => 'second')),
  3972. 'value',
  3973. '/option',
  3974. '/select'
  3975. );
  3976. $this->assertTags($result, $expected);
  3977. $options = array(
  3978. array('value' => 'first', 'name' => 'First'),
  3979. array('value' => 'first', 'name' => 'Another First'),
  3980. );
  3981. $result = $this->Form->select(
  3982. 'Model.field',
  3983. $options,
  3984. array('escape' => false, 'empty' => false)
  3985. );
  3986. $expected = array(
  3987. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3988. array('option' => array('value' => 'first')),
  3989. 'First',
  3990. '/option',
  3991. array('option' => array('value' => 'first')),
  3992. 'Another First',
  3993. '/option',
  3994. '/select'
  3995. );
  3996. $this->assertTags($result, $expected);
  3997. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3998. $result = $this->Form->select(
  3999. 'Model.contact_id',
  4000. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4001. array('escape' => false, 'empty' => 'pick something')
  4002. );
  4003. $expected = array(
  4004. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4005. array('option' => array('value' => '')), 'pick something', '/option',
  4006. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4007. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4008. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4009. '/select'
  4010. );
  4011. $this->assertTags($result, $expected);
  4012. $this->Form->request->data['Model']['field'] = 0;
  4013. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4014. $expected = array(
  4015. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4016. array('option' => array('value' => '')), '/option',
  4017. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4018. array('option' => array('value' => '1')), 'Yes', '/option',
  4019. '/select'
  4020. );
  4021. $this->assertTags($result, $expected);
  4022. $this->Form->request->data['Model']['field'] = 50;
  4023. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4024. $expected = array(
  4025. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4026. array('option' => array('value' => '')), '/option',
  4027. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4028. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4029. '/select'
  4030. );
  4031. $this->assertTags($result, $expected);
  4032. $result = $this->Form->select('Contact.required_one', array('option A'));
  4033. $expected = array(
  4034. 'select' => array(
  4035. 'name' => 'data[Contact][required_one]',
  4036. 'id' => 'ContactRequiredOne',
  4037. 'required' => 'required'
  4038. ),
  4039. array('option' => array('value' => '')), '/option',
  4040. array('option' => array('value' => '0')), 'option A', '/option',
  4041. '/select'
  4042. );
  4043. $this->assertTags($result, $expected);
  4044. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4045. $expected = array(
  4046. 'select' => array(
  4047. 'name' => 'data[Contact][required_one]',
  4048. 'id' => 'ContactRequiredOne',
  4049. 'disabled' => 'disabled'
  4050. ),
  4051. array('option' => array('value' => '')), '/option',
  4052. array('option' => array('value' => '0')), 'option A', '/option',
  4053. '/select'
  4054. );
  4055. $this->assertTags($result, $expected);
  4056. }
  4057. /**
  4058. * test that select() with optiongroups listens to the escape param.
  4059. *
  4060. * @return void
  4061. */
  4062. public function testSelectOptionGroupEscaping() {
  4063. $options = array(
  4064. '>< Key' => array(
  4065. 1 => 'One',
  4066. 2 => 'Two'
  4067. )
  4068. );
  4069. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4070. $expected = array(
  4071. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4072. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4073. array('option' => array('value' => '1')), 'One', '/option',
  4074. array('option' => array('value' => '2')), 'Two', '/option',
  4075. '/optgroup',
  4076. '/select'
  4077. );
  4078. $this->assertTags($result, $expected);
  4079. $options = array(
  4080. '>< Key' => array(
  4081. 1 => 'One',
  4082. 2 => 'Two'
  4083. )
  4084. );
  4085. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4086. $expected = array(
  4087. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4088. 'optgroup' => array('label' => '>< Key'),
  4089. array('option' => array('value' => '1')), 'One', '/option',
  4090. array('option' => array('value' => '2')), 'Two', '/option',
  4091. '/optgroup',
  4092. '/select'
  4093. );
  4094. $this->assertTags($result, $expected);
  4095. }
  4096. /**
  4097. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4098. *
  4099. * @return void
  4100. */
  4101. public function testSelectWithNullAttributes() {
  4102. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4103. $expected = array(
  4104. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4105. array('option' => array('value' => '0')),
  4106. 'first',
  4107. '/option',
  4108. array('option' => array('value' => '1')),
  4109. 'second',
  4110. '/option',
  4111. '/select'
  4112. );
  4113. $this->assertTags($result, $expected);
  4114. }
  4115. /**
  4116. * testNestedSelect method
  4117. *
  4118. * test select element generation with optgroups
  4119. *
  4120. * @return void
  4121. */
  4122. public function testNestedSelect() {
  4123. $result = $this->Form->select(
  4124. 'Model.field',
  4125. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4126. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4127. )), array('empty' => false)
  4128. );
  4129. $expected = array(
  4130. 'select' => array('name' => 'data[Model][field]',
  4131. 'id' => 'ModelField'),
  4132. array('option' => array('value' => 1)),
  4133. 'One',
  4134. '/option',
  4135. array('option' => array('value' => 2)),
  4136. 'Two',
  4137. '/option',
  4138. array('optgroup' => array('label' => 'Three')),
  4139. array('option' => array('value' => 4)),
  4140. 'Four',
  4141. '/option',
  4142. array('option' => array('value' => 5)),
  4143. 'Five',
  4144. '/option',
  4145. '/optgroup',
  4146. '/select'
  4147. );
  4148. $this->assertTags($result, $expected);
  4149. $result = $this->Form->select(
  4150. 'Model.field',
  4151. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4152. array('showParents' => true, 'empty' => false)
  4153. );
  4154. $expected = array(
  4155. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4156. array('option' => array('value' => 1)),
  4157. 'One',
  4158. '/option',
  4159. array('option' => array('value' => 2)),
  4160. 'Two',
  4161. '/option',
  4162. array('optgroup' => array('label' => 'Three')),
  4163. array('option' => array('value' => 3)),
  4164. 'Three',
  4165. '/option',
  4166. array('option' => array('value' => 4)),
  4167. 'Four',
  4168. '/option',
  4169. '/optgroup',
  4170. '/select'
  4171. );
  4172. $this->assertTags($result, $expected);
  4173. }
  4174. /**
  4175. * testSelectMultiple method
  4176. *
  4177. * test generation of multiple select elements
  4178. *
  4179. * @return void
  4180. */
  4181. public function testSelectMultiple() {
  4182. $options = array('first', 'second', 'third');
  4183. $result = $this->Form->select(
  4184. 'Model.multi_field', $options, array('multiple' => true)
  4185. );
  4186. $expected = array(
  4187. 'input' => array(
  4188. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4189. ),
  4190. 'select' => array(
  4191. 'name' => 'data[Model][multi_field][]',
  4192. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4193. ),
  4194. array('option' => array('value' => '0')),
  4195. 'first',
  4196. '/option',
  4197. array('option' => array('value' => '1')),
  4198. 'second',
  4199. '/option',
  4200. array('option' => array('value' => '2')),
  4201. 'third',
  4202. '/option',
  4203. '/select'
  4204. );
  4205. $this->assertTags($result, $expected);
  4206. $result = $this->Form->select(
  4207. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4208. );
  4209. $expected = array(
  4210. 'input' => array(
  4211. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4212. ),
  4213. 'select' => array(
  4214. 'name' => 'data[Model][multi_field][]',
  4215. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4216. ),
  4217. array('option' => array('value' => '0')),
  4218. 'first',
  4219. '/option',
  4220. array('option' => array('value' => '1')),
  4221. 'second',
  4222. '/option',
  4223. array('option' => array('value' => '2')),
  4224. 'third',
  4225. '/option',
  4226. '/select'
  4227. );
  4228. $this->assertTags($result, $expected);
  4229. $result = $this->Form->select(
  4230. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4231. );
  4232. $expected = array(
  4233. 'input' => array(
  4234. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4235. ),
  4236. 'select' => array(
  4237. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4238. 'multiple' => 'multiple'
  4239. ),
  4240. array('option' => array('value' => '0', 'selected' => 'selected')),
  4241. 'first',
  4242. '/option',
  4243. array('option' => array('value' => '1', 'selected' => 'selected')),
  4244. 'second',
  4245. '/option',
  4246. array('option' => array('value' => '2')),
  4247. 'third',
  4248. '/option',
  4249. '/select'
  4250. );
  4251. $this->assertTags($result, $expected);
  4252. $result = $this->Form->select(
  4253. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4254. );
  4255. $expected = array(
  4256. 'select' => array(
  4257. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4258. ),
  4259. array('option' => array('value' => '0', 'selected' => 'selected')),
  4260. 'first',
  4261. '/option',
  4262. array('option' => array('value' => '1', 'selected' => 'selected')),
  4263. 'second',
  4264. '/option',
  4265. array('option' => array('value' => '2')),
  4266. 'third',
  4267. '/option',
  4268. '/select'
  4269. );
  4270. $this->assertTags($result, $expected);
  4271. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4272. $selected = array('2', '3x');
  4273. $result = $this->Form->select(
  4274. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4275. );
  4276. $expected = array(
  4277. 'input' => array(
  4278. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4279. ),
  4280. 'select' => array(
  4281. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4282. ),
  4283. array('option' => array('value' => '1')),
  4284. 'One',
  4285. '/option',
  4286. array('option' => array('value' => '2', 'selected' => 'selected')),
  4287. 'Two',
  4288. '/option',
  4289. array('option' => array('value' => '3')),
  4290. 'Three',
  4291. '/option',
  4292. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4293. 'Stringy',
  4294. '/option',
  4295. '/select'
  4296. );
  4297. $this->assertTags($result, $expected);
  4298. $result = $this->Form->select('Contact.required_one', array(
  4299. '1' => 'option A',
  4300. '2' => 'option B'
  4301. ), array('multiple' => true));
  4302. $expected = array(
  4303. 'input' => array(
  4304. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4305. ),
  4306. 'select' => array(
  4307. 'name' => 'data[Contact][required_one][]',
  4308. 'id' => 'ContactRequiredOne',
  4309. 'required' => 'required',
  4310. 'multiple' => 'multiple'
  4311. ),
  4312. array('option' => array('value' => '1')), 'option A', '/option',
  4313. array('option' => array('value' => '2')), 'option B', '/option',
  4314. '/select'
  4315. );
  4316. $this->assertTags($result, $expected);
  4317. $result = $this->Form->select(
  4318. 'Model.multi_field',
  4319. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4320. array('multiple' => true)
  4321. );
  4322. $expected = array(
  4323. 'input' => array(
  4324. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4325. 'id' => 'ModelMultiField_'
  4326. ),
  4327. array('select' => array('name' => 'data[Model][multi_field][]',
  4328. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4329. )),
  4330. array('option' => array('value' => 'a&gt;b')),
  4331. 'first',
  4332. '/option',
  4333. array('option' => array('value' => 'a&lt;b')),
  4334. 'second',
  4335. '/option',
  4336. array('option' => array(
  4337. 'value' => 'a&quot;b'
  4338. )),
  4339. 'third',
  4340. '/option',
  4341. '/select'
  4342. );
  4343. $this->assertTags($result, $expected);
  4344. }
  4345. /**
  4346. * Test generating multiple select with disabled elements.
  4347. *
  4348. * @return void
  4349. */
  4350. public function testSelectMultipleWithDisabledElements() {
  4351. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4352. $disabled = array(2, 3);
  4353. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4354. $expected = array(
  4355. 'input' => array(
  4356. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4357. ),
  4358. 'select' => array(
  4359. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4360. ),
  4361. array('option' => array('value' => '1')),
  4362. 'One',
  4363. '/option',
  4364. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4365. 'Two',
  4366. '/option',
  4367. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4368. 'Three',
  4369. '/option',
  4370. array('option' => array('value' => '3x')),
  4371. 'Stringy',
  4372. '/option',
  4373. '/select'
  4374. );
  4375. $this->assertTags($result, $expected);
  4376. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4377. $disabled = array('2', '3x');
  4378. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4379. $expected = array(
  4380. array('div' => array('class' => 'input select')),
  4381. array('label' => array('for' => 'ContactMultiple')),
  4382. 'Multiple',
  4383. '/label',
  4384. 'input' => array(
  4385. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4386. ),
  4387. 'select' => array(
  4388. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4389. ),
  4390. array('option' => array('value' => '1')),
  4391. 'One',
  4392. '/option',
  4393. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4394. 'Two',
  4395. '/option',
  4396. array('option' => array('value' => '3')),
  4397. 'Three',
  4398. '/option',
  4399. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4400. 'Stringy',
  4401. '/option',
  4402. '/select',
  4403. '/div'
  4404. );
  4405. $this->assertTags($result, $expected);
  4406. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4407. $disabled = true;
  4408. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4409. $expected = array(
  4410. array('div' => array('class' => 'input select')),
  4411. array('label' => array('for' => 'ContactMultiple')),
  4412. 'Multiple',
  4413. '/label',
  4414. 'input' => array(
  4415. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4416. ),
  4417. 'select' => array(
  4418. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4419. ),
  4420. array('option' => array('value' => '1')),
  4421. 'One',
  4422. '/option',
  4423. array('option' => array('value' => '2')),
  4424. 'Two',
  4425. '/option',
  4426. array('option' => array('value' => '3')),
  4427. 'Three',
  4428. '/option',
  4429. array('option' => array('value' => '3x')),
  4430. 'Stringy',
  4431. '/option',
  4432. '/select',
  4433. '/div'
  4434. );
  4435. $this->assertTags($result, $expected);
  4436. }
  4437. /**
  4438. * Test generating select with disabled elements.
  4439. *
  4440. * @return void
  4441. */
  4442. public function testSelectWithDisabledElements() {
  4443. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4444. $disabled = array(2, 3);
  4445. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4446. $expected = array(
  4447. 'select' => array(
  4448. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4449. ),
  4450. array('option' => array('value' => '')),
  4451. '/option',
  4452. array('option' => array('value' => '1')),
  4453. 'One',
  4454. '/option',
  4455. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4456. 'Two',
  4457. '/option',
  4458. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4459. 'Three',
  4460. '/option',
  4461. array('option' => array('value' => '3x')),
  4462. 'Stringy',
  4463. '/option',
  4464. '/select'
  4465. );
  4466. $this->assertTags($result, $expected);
  4467. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4468. $disabled = array('2', '3x');
  4469. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4470. $expected = array(
  4471. array('div' => array('class' => 'input select')),
  4472. array('label' => array('for' => 'ModelField')),
  4473. 'Field',
  4474. '/label',
  4475. 'select' => array(
  4476. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4477. ),
  4478. array('option' => array('value' => '1')),
  4479. 'One',
  4480. '/option',
  4481. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4482. 'Two',
  4483. '/option',
  4484. array('option' => array('value' => '3')),
  4485. 'Three',
  4486. '/option',
  4487. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4488. 'Stringy',
  4489. '/option',
  4490. '/select',
  4491. '/div'
  4492. );
  4493. $this->assertTags($result, $expected);
  4494. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4495. $disabled = true;
  4496. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4497. $expected = array(
  4498. array('div' => array('class' => 'input select')),
  4499. array('label' => array('for' => 'ModelField')),
  4500. 'Field',
  4501. '/label',
  4502. 'select' => array(
  4503. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4504. ),
  4505. array('option' => array('value' => '1')),
  4506. 'One',
  4507. '/option',
  4508. array('option' => array('value' => '2')),
  4509. 'Two',
  4510. '/option',
  4511. array('option' => array('value' => '3')),
  4512. 'Three',
  4513. '/option',
  4514. array('option' => array('value' => '3x')),
  4515. 'Stringy',
  4516. '/option',
  4517. '/select',
  4518. '/div'
  4519. );
  4520. $this->assertTags($result, $expected);
  4521. }
  4522. /**
  4523. * test generation of habtm select boxes.
  4524. *
  4525. * @return void
  4526. */
  4527. public function testHabtmSelectBox() {
  4528. $this->View->viewVars['contactTags'] = array(
  4529. 1 => 'blue',
  4530. 2 => 'red',
  4531. 3 => 'green'
  4532. );
  4533. $this->Form->request->data = array(
  4534. 'Contact' => array(),
  4535. 'ContactTag' => array(
  4536. array(
  4537. 'id' => '1',
  4538. 'name' => 'blue'
  4539. ),
  4540. array(
  4541. 'id' => 3,
  4542. 'name' => 'green'
  4543. )
  4544. )
  4545. );
  4546. $this->Form->create('Contact');
  4547. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4548. $expected = array(
  4549. 'input' => array(
  4550. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4551. ),
  4552. 'select' => array(
  4553. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4554. 'multiple' => 'multiple'
  4555. ),
  4556. array('option' => array('value' => '1', 'selected' => 'selected')),
  4557. 'blue',
  4558. '/option',
  4559. array('option' => array('value' => '2')),
  4560. 'red',
  4561. '/option',
  4562. array('option' => array('value' => '3', 'selected' => 'selected')),
  4563. 'green',
  4564. '/option',
  4565. '/select'
  4566. );
  4567. $this->assertTags($result, $expected);
  4568. // make sure only 50 is selected, and not 50f5c0cf
  4569. $this->View->viewVars['contactTags'] = array(
  4570. '1' => 'blue',
  4571. '50f5c0cf' => 'red',
  4572. '50' => 'green'
  4573. );
  4574. $this->Form->request->data = array(
  4575. 'Contact' => array(),
  4576. 'ContactTag' => array(
  4577. array(
  4578. 'id' => 1,
  4579. 'name' => 'blue'
  4580. ),
  4581. array(
  4582. 'id' => 50,
  4583. 'name' => 'green'
  4584. )
  4585. )
  4586. );
  4587. $this->Form->create('Contact');
  4588. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4589. $expected = array(
  4590. 'input' => array(
  4591. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4592. ),
  4593. 'select' => array(
  4594. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4595. 'multiple' => 'multiple'
  4596. ),
  4597. array('option' => array('value' => '1', 'selected' => 'selected')),
  4598. 'blue',
  4599. '/option',
  4600. array('option' => array('value' => '50f5c0cf')),
  4601. 'red',
  4602. '/option',
  4603. array('option' => array('value' => '50', 'selected' => 'selected')),
  4604. 'green',
  4605. '/option',
  4606. '/select'
  4607. );
  4608. $this->assertTags($result, $expected);
  4609. }
  4610. /**
  4611. * test generation of multi select elements in checkbox format
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelectMultipleCheckboxes() {
  4616. $result = $this->Form->select(
  4617. 'Model.multi_field',
  4618. array('first', 'second', 'third'),
  4619. array('multiple' => 'checkbox')
  4620. );
  4621. $expected = array(
  4622. 'input' => array(
  4623. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4624. ),
  4625. array('div' => array('class' => 'checkbox')),
  4626. array('input' => array(
  4627. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4628. 'value' => '0', 'id' => 'ModelMultiField0'
  4629. )),
  4630. array('label' => array('for' => 'ModelMultiField0')),
  4631. 'first',
  4632. '/label',
  4633. '/div',
  4634. array('div' => array('class' => 'checkbox')),
  4635. array('input' => array(
  4636. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4637. 'value' => '1', 'id' => 'ModelMultiField1'
  4638. )),
  4639. array('label' => array('for' => 'ModelMultiField1')),
  4640. 'second',
  4641. '/label',
  4642. '/div',
  4643. array('div' => array('class' => 'checkbox')),
  4644. array('input' => array(
  4645. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4646. 'value' => '2', 'id' => 'ModelMultiField2'
  4647. )),
  4648. array('label' => array('for' => 'ModelMultiField2')),
  4649. 'third',
  4650. '/label',
  4651. '/div'
  4652. );
  4653. $this->assertTags($result, $expected);
  4654. $result = $this->Form->select(
  4655. 'Model.multi_field',
  4656. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4657. array('multiple' => 'checkbox')
  4658. );
  4659. $expected = array(
  4660. 'input' => array(
  4661. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4662. ),
  4663. array('div' => array('class' => 'checkbox')),
  4664. array('input' => array(
  4665. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4666. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4667. )),
  4668. array('label' => array('for' => 'ModelMultiFieldA')),
  4669. 'first',
  4670. '/label',
  4671. '/div',
  4672. array('div' => array('class' => 'checkbox')),
  4673. array('input' => array(
  4674. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4675. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4676. )),
  4677. array('label' => array('for' => 'ModelMultiFieldB')),
  4678. 'second',
  4679. '/label',
  4680. '/div',
  4681. array('div' => array('class' => 'checkbox')),
  4682. array('input' => array(
  4683. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4684. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4685. )),
  4686. array('label' => array('for' => 'ModelMultiFieldC')),
  4687. 'third',
  4688. '/label',
  4689. '/div'
  4690. );
  4691. $this->assertTags($result, $expected);
  4692. $result = $this->Form->select(
  4693. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4694. );
  4695. $expected = array(
  4696. 'input' => array(
  4697. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4698. ),
  4699. array('div' => array('class' => 'checkbox')),
  4700. array('input' => array(
  4701. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4702. 'value' => '1', 'id' => 'ModelMultiField1'
  4703. )),
  4704. array('label' => array('for' => 'ModelMultiField1')),
  4705. 'first',
  4706. '/label',
  4707. '/div'
  4708. );
  4709. $this->assertTags($result, $expected);
  4710. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4711. $result = $this->Form->select(
  4712. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4713. );
  4714. $expected = array(
  4715. 'input' => array(
  4716. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4717. ),
  4718. array('div' => array('class' => 'checkbox')),
  4719. array('input' => array(
  4720. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4721. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4722. )),
  4723. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4724. 'first',
  4725. '/label',
  4726. '/div',
  4727. array('div' => array('class' => 'checkbox')),
  4728. array('input' => array(
  4729. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4730. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4731. )),
  4732. array('label' => array('for' => 'ModelTagsArray')),
  4733. 'Array',
  4734. '/label',
  4735. '/div'
  4736. );
  4737. $this->assertTags($result, $expected);
  4738. $result = $this->Form->select(
  4739. 'Model.multi_field',
  4740. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4741. array('multiple' => 'checkbox')
  4742. );
  4743. $expected = array(
  4744. 'input' => array(
  4745. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4746. ),
  4747. array('div' => array('class' => 'checkbox')),
  4748. array('input' => array(
  4749. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4750. 'value' => 'a+', 'id' => 'ModelMultiFieldA+'
  4751. )),
  4752. array('label' => array('for' => 'ModelMultiFieldA+')),
  4753. 'first',
  4754. '/label',
  4755. '/div',
  4756. array('div' => array('class' => 'checkbox')),
  4757. array('input' => array(
  4758. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4759. 'value' => 'a++', 'id' => 'ModelMultiFieldA++'
  4760. )),
  4761. array('label' => array('for' => 'ModelMultiFieldA++')),
  4762. 'second',
  4763. '/label',
  4764. '/div',
  4765. array('div' => array('class' => 'checkbox')),
  4766. array('input' => array(
  4767. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4768. 'value' => 'a+++', 'id' => 'ModelMultiFieldA+++'
  4769. )),
  4770. array('label' => array('for' => 'ModelMultiFieldA+++')),
  4771. 'third',
  4772. '/label',
  4773. '/div'
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. $result = $this->Form->select(
  4777. 'Model.multi_field',
  4778. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4779. array('multiple' => 'checkbox')
  4780. );
  4781. $expected = array(
  4782. 'input' => array(
  4783. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4784. ),
  4785. array('div' => array('class' => 'checkbox')),
  4786. array('input' => array(
  4787. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4788. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4789. )),
  4790. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4791. 'first',
  4792. '/label',
  4793. '/div',
  4794. array('div' => array('class' => 'checkbox')),
  4795. array('input' => array(
  4796. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4797. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4798. )),
  4799. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4800. 'second',
  4801. '/label',
  4802. '/div',
  4803. array('div' => array('class' => 'checkbox')),
  4804. array('input' => array(
  4805. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4806. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4807. )),
  4808. array('label' => array('for' => 'ModelMultiFieldAB')),
  4809. 'third',
  4810. '/label',
  4811. '/div'
  4812. );
  4813. $this->assertTags($result, $expected);
  4814. }
  4815. /**
  4816. * test multiple checkboxes with div styles.
  4817. *
  4818. * @return void
  4819. */
  4820. public function testSelectMultipleCheckboxDiv() {
  4821. $result = $this->Form->select(
  4822. 'Model.tags',
  4823. array('first', 'second'),
  4824. array('multiple' => 'checkbox', 'class' => 'my-class')
  4825. );
  4826. $expected = array(
  4827. 'input' => array(
  4828. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4829. ),
  4830. array('div' => array('class' => 'my-class')),
  4831. array('input' => array(
  4832. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4833. 'value' => '0', 'id' => 'ModelTags0'
  4834. )),
  4835. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4836. '/div',
  4837. array('div' => array('class' => 'my-class')),
  4838. array('input' => array(
  4839. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4840. 'value' => '1', 'id' => 'ModelTags1'
  4841. )),
  4842. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4843. '/div'
  4844. );
  4845. $this->assertTags($result, $expected);
  4846. $result = $this->Form->input('Model.tags', array(
  4847. 'options' => array('first', 'second'),
  4848. 'multiple' => 'checkbox',
  4849. 'class' => 'my-class',
  4850. 'div' => false,
  4851. 'label' => false
  4852. ));
  4853. $this->assertTags($result, $expected);
  4854. $Contact = ClassRegistry::getObject('Contact');
  4855. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4856. $result = $this->Form->input('Contact.tags', array(
  4857. 'options' => array('one'),
  4858. 'multiple' => 'checkbox',
  4859. 'label' => false,
  4860. 'div' => false
  4861. ));
  4862. $expected = array(
  4863. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4864. array('div' => array('class' => 'checkbox form-error')),
  4865. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4866. array('label' => array('for' => 'ContactTags0')),
  4867. 'one',
  4868. '/label',
  4869. '/div'
  4870. );
  4871. $this->assertTags($result, $expected);
  4872. $result = $this->Form->input('Contact.tags', array(
  4873. 'options' => array('one'),
  4874. 'multiple' => 'checkbox',
  4875. 'class' => 'mycheckbox',
  4876. 'label' => false,
  4877. 'div' => false
  4878. ));
  4879. $expected = array(
  4880. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4881. array('div' => array('class' => 'mycheckbox form-error')),
  4882. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4883. array('label' => array('for' => 'ContactTags0')),
  4884. 'one',
  4885. '/label',
  4886. '/div'
  4887. );
  4888. $this->assertTags($result, $expected);
  4889. }
  4890. /**
  4891. * Checks the security hash array generated for multiple-input checkbox elements
  4892. *
  4893. * @return void
  4894. */
  4895. public function testSelectMultipleCheckboxSecurity() {
  4896. $this->Form->request['_Token'] = array('key' => 'testKey');
  4897. $this->assertEquals(array(), $this->Form->fields);
  4898. $result = $this->Form->select(
  4899. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4900. array('multiple' => 'checkbox')
  4901. );
  4902. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4903. $result = $this->Form->secure($this->Form->fields);
  4904. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4905. $this->assertRegExp('/"' . $key . '"/', $result);
  4906. }
  4907. /**
  4908. * Multiple select elements should always be secured as they always participate
  4909. * in the POST data.
  4910. *
  4911. * @return void
  4912. */
  4913. public function testSelectMultipleSecureWithNoOptions() {
  4914. $this->Form->request['_Token'] = array('key' => 'testkey');
  4915. $this->assertEquals(array(), $this->Form->fields);
  4916. $this->Form->select(
  4917. 'Model.select',
  4918. array(),
  4919. array('multiple' => true)
  4920. );
  4921. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4922. }
  4923. /**
  4924. * When a select box has no options it should not be added to the fields list
  4925. * as it always fail post validation.
  4926. *
  4927. * @return void
  4928. */
  4929. public function testSelectNoSecureWithNoOptions() {
  4930. $this->Form->request['_Token'] = array('key' => 'testkey');
  4931. $this->assertEquals(array(), $this->Form->fields);
  4932. $this->Form->select(
  4933. 'Model.select',
  4934. array()
  4935. );
  4936. $this->assertEquals(array(), $this->Form->fields);
  4937. $this->Form->select(
  4938. 'Model.select',
  4939. array(),
  4940. array('empty' => true)
  4941. );
  4942. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4943. }
  4944. /**
  4945. * testInputMultipleCheckboxes method
  4946. *
  4947. * test input() resulting in multi select elements being generated.
  4948. *
  4949. * @return void
  4950. */
  4951. public function testInputMultipleCheckboxes() {
  4952. $result = $this->Form->input('Model.multi_field', array(
  4953. 'options' => array('first', 'second', 'third'),
  4954. 'multiple' => 'checkbox'
  4955. ));
  4956. $expected = array(
  4957. array('div' => array('class' => 'input select')),
  4958. array('label' => array('for' => 'ModelMultiField')),
  4959. 'Multi Field',
  4960. '/label',
  4961. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4962. array('div' => array('class' => 'checkbox')),
  4963. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4964. array('label' => array('for' => 'ModelMultiField0')),
  4965. 'first',
  4966. '/label',
  4967. '/div',
  4968. array('div' => array('class' => 'checkbox')),
  4969. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4970. array('label' => array('for' => 'ModelMultiField1')),
  4971. 'second',
  4972. '/label',
  4973. '/div',
  4974. array('div' => array('class' => 'checkbox')),
  4975. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4976. array('label' => array('for' => 'ModelMultiField2')),
  4977. 'third',
  4978. '/label',
  4979. '/div',
  4980. '/div'
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. $result = $this->Form->input('Model.multi_field', array(
  4984. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4985. 'multiple' => 'checkbox'
  4986. ));
  4987. $expected = array(
  4988. array('div' => array('class' => 'input select')),
  4989. array('label' => array('for' => 'ModelMultiField')),
  4990. 'Multi Field',
  4991. '/label',
  4992. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4993. array('div' => array('class' => 'checkbox')),
  4994. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4995. array('label' => array('for' => 'ModelMultiFieldA')),
  4996. 'first',
  4997. '/label',
  4998. '/div',
  4999. array('div' => array('class' => 'checkbox')),
  5000. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5001. array('label' => array('for' => 'ModelMultiFieldB')),
  5002. 'second',
  5003. '/label',
  5004. '/div',
  5005. array('div' => array('class' => 'checkbox')),
  5006. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5007. array('label' => array('for' => 'ModelMultiFieldC')),
  5008. 'third',
  5009. '/label',
  5010. '/div',
  5011. '/div'
  5012. );
  5013. $this->assertTags($result, $expected);
  5014. $result = $this->Form->input('Model.multi_field', array(
  5015. 'options' => array('1' => 'first'),
  5016. 'multiple' => 'checkbox',
  5017. 'label' => false,
  5018. 'div' => false
  5019. ));
  5020. $expected = array(
  5021. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5022. array('div' => array('class' => 'checkbox')),
  5023. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5024. array('label' => array('for' => 'ModelMultiField1')),
  5025. 'first',
  5026. '/label',
  5027. '/div'
  5028. );
  5029. $this->assertTags($result, $expected);
  5030. $result = $this->Form->input('Model.multi_field', array(
  5031. 'options' => array('2' => 'second'),
  5032. 'multiple' => 'checkbox',
  5033. 'label' => false,
  5034. 'div' => false
  5035. ));
  5036. $expected = array(
  5037. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5038. array('div' => array('class' => 'checkbox')),
  5039. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5040. array('label' => array('for' => 'ModelMultiField2')),
  5041. 'second',
  5042. '/label',
  5043. '/div'
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. }
  5047. /**
  5048. * testSelectHiddenFieldOmission method
  5049. *
  5050. * test that select() with 'hiddenField' => false omits the hidden field
  5051. *
  5052. * @return void
  5053. */
  5054. public function testSelectHiddenFieldOmission() {
  5055. $result = $this->Form->select('Model.multi_field',
  5056. array('first', 'second'),
  5057. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5058. );
  5059. $expected = array(
  5060. array('div' => array('class' => 'checkbox')),
  5061. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5062. array('label' => array('for' => 'ModelMultiField0')),
  5063. 'first',
  5064. '/label',
  5065. '/div',
  5066. array('div' => array('class' => 'checkbox')),
  5067. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5068. array('label' => array('for' => 'ModelMultiField1')),
  5069. 'second',
  5070. '/label',
  5071. '/div'
  5072. );
  5073. $this->assertTags($result, $expected);
  5074. $result = $this->Form->input('Model.multi_field', array(
  5075. 'options' => array('first', 'second'),
  5076. 'multiple' => 'checkbox',
  5077. 'hiddenField' => false
  5078. ));
  5079. $expected = array(
  5080. array('div' => array('class' => 'input select')),
  5081. array('label' => array('for' => 'ModelMultiField')),
  5082. 'Multi Field',
  5083. '/label',
  5084. array('div' => array('class' => 'checkbox')),
  5085. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5086. array('label' => array('for' => 'ModelMultiField0')),
  5087. 'first',
  5088. '/label',
  5089. '/div',
  5090. array('div' => array('class' => 'checkbox')),
  5091. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5092. array('label' => array('for' => 'ModelMultiField1')),
  5093. 'second',
  5094. '/label',
  5095. '/div',
  5096. '/div'
  5097. );
  5098. $this->assertTags($result, $expected);
  5099. }
  5100. /**
  5101. * test that select() with multiple = checkbox works with overriding name attribute.
  5102. *
  5103. * @return void
  5104. */
  5105. public function testSelectCheckboxMultipleOverrideName() {
  5106. $result = $this->Form->input('category', array(
  5107. 'type' => 'select',
  5108. 'multiple' => 'checkbox',
  5109. 'name' => 'data[fish]',
  5110. 'options' => array('1', '2'),
  5111. 'div' => false,
  5112. 'label' => false,
  5113. ));
  5114. $expected = array(
  5115. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5116. array('div' => array('class' => 'checkbox')),
  5117. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5118. array('label' => array('for' => 'Category0')), '1', '/label',
  5119. '/div',
  5120. array('div' => array('class' => 'checkbox')),
  5121. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5122. array('label' => array('for' => 'Category1')), '2', '/label',
  5123. '/div'
  5124. );
  5125. $this->assertTags($result, $expected);
  5126. }
  5127. /**
  5128. * Test that 'id' overrides all the checkbox id's as well.
  5129. *
  5130. * @return void
  5131. */
  5132. public function testSelectCheckboxMultipleId() {
  5133. $result = $this->Form->select(
  5134. 'Model.multi_field',
  5135. array('first', 'second', 'third'),
  5136. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5137. );
  5138. $expected = array(
  5139. 'input' => array(
  5140. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5141. ),
  5142. array('div' => array('class' => 'checkbox')),
  5143. array('input' => array(
  5144. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5145. 'value' => '0', 'id' => 'CustomId0'
  5146. )),
  5147. array('label' => array('for' => 'CustomId0')),
  5148. 'first',
  5149. '/label',
  5150. '/div',
  5151. array('div' => array('class' => 'checkbox')),
  5152. array('input' => array(
  5153. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5154. 'value' => '1', 'id' => 'CustomId1'
  5155. )),
  5156. array('label' => array('for' => 'CustomId1')),
  5157. 'second',
  5158. '/label',
  5159. '/div',
  5160. array('div' => array('class' => 'checkbox')),
  5161. array('input' => array(
  5162. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5163. 'value' => '2', 'id' => 'CustomId2'
  5164. )),
  5165. array('label' => array('for' => 'CustomId2')),
  5166. 'third',
  5167. '/label',
  5168. '/div'
  5169. );
  5170. $this->assertTags($result, $expected);
  5171. }
  5172. /**
  5173. * testCheckbox method
  5174. *
  5175. * Test generation of checkboxes
  5176. *
  5177. * @return void
  5178. */
  5179. public function testCheckbox() {
  5180. $result = $this->Form->checkbox('Model.field');
  5181. $expected = array(
  5182. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5183. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5184. );
  5185. $this->assertTags($result, $expected);
  5186. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5187. $expected = array(
  5188. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5189. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5190. );
  5191. $this->assertTags($result, $expected);
  5192. $Contact = ClassRegistry::getObject('Contact');
  5193. $Contact->validationErrors['field'] = 1;
  5194. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5195. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5196. $expected = array(
  5197. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5198. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5199. );
  5200. $this->assertTags($result, $expected);
  5201. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5202. $expected = array(
  5203. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5204. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5205. );
  5206. $this->assertTags($result, $expected);
  5207. $this->Form->request->data['Contact']['field'] = '';
  5208. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5209. $expected = array(
  5210. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5211. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5212. );
  5213. $this->assertTags($result, $expected);
  5214. $Contact->validationErrors = array();
  5215. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5216. $expected = array(
  5217. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5218. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5219. );
  5220. $this->assertTags($result, $expected);
  5221. $this->Form->request->data['Contact']['published'] = 1;
  5222. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5223. $expected = array(
  5224. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5225. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5226. );
  5227. $this->assertTags($result, $expected);
  5228. $this->Form->request->data['Contact']['published'] = 0;
  5229. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5230. $expected = array(
  5231. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5232. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5233. );
  5234. $this->assertTags($result, $expected);
  5235. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5236. $expected = array(
  5237. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5238. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5239. );
  5240. $this->assertTags($result, $expected);
  5241. $result = $this->Form->checkbox('CustomField.1.value');
  5242. $expected = array(
  5243. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5244. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5245. );
  5246. $this->assertTags($result, $expected);
  5247. }
  5248. /**
  5249. * test checkbox() with a custom name attribute
  5250. *
  5251. * @return void
  5252. */
  5253. public function testCheckboxCustomNameAttribute() {
  5254. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5255. $expected = array(
  5256. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5257. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5258. );
  5259. $this->assertTags($result, $expected);
  5260. }
  5261. /**
  5262. * test the checked option for checkboxes.
  5263. *
  5264. * @return void
  5265. */
  5266. public function testCheckboxCheckedOption() {
  5267. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5268. $expected = array(
  5269. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5270. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5274. $expected = array(
  5275. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5276. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5277. );
  5278. $this->assertTags($result, $expected);
  5279. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5280. $expected = array(
  5281. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5282. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5283. );
  5284. $this->assertTags($result, $expected);
  5285. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5286. $expected = array(
  5287. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5288. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5289. );
  5290. $this->assertTags($result, $expected);
  5291. $this->Form->request->data['Model']['field'] = 1;
  5292. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5293. $expected = array(
  5294. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5295. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5296. );
  5297. $this->assertTags($result, $expected);
  5298. }
  5299. /**
  5300. * Test that disabled attribute works on both the checkbox and hidden input.
  5301. *
  5302. * @return void
  5303. */
  5304. public function testCheckboxDisabling() {
  5305. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5306. $expected = array(
  5307. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5308. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5309. );
  5310. $this->assertTags($result, $expected);
  5311. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5312. $expected = array(
  5313. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5314. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5315. );
  5316. $this->assertTags($result, $expected);
  5317. }
  5318. /**
  5319. * Test that the hidden input for checkboxes can be omitted or set to a
  5320. * specific value.
  5321. *
  5322. * @return void
  5323. */
  5324. public function testCheckboxHiddenField() {
  5325. $result = $this->Form->input('UserForm.something', array(
  5326. 'type' => 'checkbox',
  5327. 'hiddenField' => false
  5328. ));
  5329. $expected = array(
  5330. 'div' => array('class' => 'input checkbox'),
  5331. array('input' => array(
  5332. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5333. 'value' => '1', 'id' => 'UserFormSomething'
  5334. )),
  5335. 'label' => array('for' => 'UserFormSomething'),
  5336. 'Something',
  5337. '/label',
  5338. '/div'
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $result = $this->Form->input('UserForm.something', array(
  5342. 'type' => 'checkbox',
  5343. 'value' => 'Y',
  5344. 'hiddenField' => 'N',
  5345. ));
  5346. $expected = array(
  5347. 'div' => array('class' => 'input checkbox'),
  5348. array('input' => array(
  5349. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5350. 'value' => 'N', 'id' => 'UserFormSomething_'
  5351. )),
  5352. array('input' => array(
  5353. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5354. 'value' => 'Y', 'id' => 'UserFormSomething'
  5355. )),
  5356. 'label' => array('for' => 'UserFormSomething'),
  5357. 'Something',
  5358. '/label',
  5359. '/div'
  5360. );
  5361. $this->assertTags($result, $expected);
  5362. }
  5363. /**
  5364. * testDateTime method
  5365. *
  5366. * Test generation of date/time select elements
  5367. *
  5368. * @return void
  5369. */
  5370. public function testDateTime() {
  5371. extract($this->dateRegex);
  5372. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5373. $now = strtotime('now');
  5374. $expected = array(
  5375. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5376. $daysRegex,
  5377. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5378. date('j', $now),
  5379. '/option',
  5380. '*/select',
  5381. '-',
  5382. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5383. $monthsRegex,
  5384. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5385. date('F', $now),
  5386. '/option',
  5387. '*/select',
  5388. '-',
  5389. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5390. $yearsRegex,
  5391. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5392. date('Y', $now),
  5393. '/option',
  5394. '*/select',
  5395. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5396. $hoursRegex,
  5397. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5398. date('g', $now),
  5399. '/option',
  5400. '*/select',
  5401. ':',
  5402. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5403. $minutesRegex,
  5404. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5405. date('i', $now),
  5406. '/option',
  5407. '*/select',
  5408. ' ',
  5409. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5410. $meridianRegex,
  5411. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5412. date('a', $now),
  5413. '/option',
  5414. '*/select'
  5415. );
  5416. $this->assertTags($result, $expected);
  5417. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5418. $expected = array(
  5419. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5420. $daysRegex,
  5421. array('option' => array('value' => '')),
  5422. '/option',
  5423. '*/select',
  5424. '-',
  5425. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5426. $monthsRegex,
  5427. array('option' => array('value' => '')),
  5428. '/option',
  5429. '*/select',
  5430. '-',
  5431. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5432. $yearsRegex,
  5433. array('option' => array('value' => '')),
  5434. '/option',
  5435. '*/select',
  5436. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5437. $hoursRegex,
  5438. array('option' => array('value' => '')),
  5439. '/option',
  5440. '*/select',
  5441. ':',
  5442. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5443. $minutesRegex,
  5444. array('option' => array('value' => '')),
  5445. '/option',
  5446. '*/select',
  5447. ' ',
  5448. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5449. $meridianRegex,
  5450. array('option' => array('value' => '')),
  5451. '/option',
  5452. '*/select'
  5453. );
  5454. $this->assertTags($result, $expected);
  5455. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5456. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5457. $this->assertTags($result, $expected);
  5458. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5459. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5460. $this->assertTags($result, $expected);
  5461. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5462. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5463. $expected = array(
  5464. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5465. $daysRegex,
  5466. array('option' => array('value' => '')),
  5467. '/option',
  5468. '*/select',
  5469. '-',
  5470. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5471. $monthsRegex,
  5472. array('option' => array('value' => '')),
  5473. '/option',
  5474. '*/select',
  5475. '-',
  5476. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5477. $yearsRegex,
  5478. array('option' => array('value' => '')),
  5479. '/option',
  5480. '*/select',
  5481. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5482. $hoursRegex,
  5483. array('option' => array('value' => '')),
  5484. '/option',
  5485. '*/select',
  5486. ':',
  5487. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5488. $minutesRegex,
  5489. array('option' => array('value' => '')),
  5490. '/option',
  5491. array('option' => array('value' => '00')),
  5492. '00',
  5493. '/option',
  5494. array('option' => array('value' => '05')),
  5495. '05',
  5496. '/option',
  5497. array('option' => array('value' => '10')),
  5498. '10',
  5499. '/option',
  5500. '*/select',
  5501. ' ',
  5502. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5503. $meridianRegex,
  5504. array('option' => array('value' => '')),
  5505. '/option',
  5506. '*/select'
  5507. );
  5508. $this->assertTags($result, $expected);
  5509. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5510. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5511. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5512. $this->Form->request->data['Contact']['data'] = null;
  5513. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5514. $expected = array(
  5515. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5516. $daysRegex,
  5517. array('option' => array('value' => '')),
  5518. '/option',
  5519. '*/select',
  5520. '-',
  5521. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5522. $monthsRegex,
  5523. array('option' => array('value' => '')),
  5524. '/option',
  5525. '*/select',
  5526. '-',
  5527. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5528. $yearsRegex,
  5529. array('option' => array('value' => '')),
  5530. '/option',
  5531. '*/select',
  5532. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5533. $hoursRegex,
  5534. array('option' => array('value' => '')),
  5535. '/option',
  5536. '*/select',
  5537. ':',
  5538. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5539. $minutesRegex,
  5540. array('option' => array('value' => '')),
  5541. '/option',
  5542. '*/select',
  5543. ' ',
  5544. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5545. $meridianRegex,
  5546. array('option' => array('value' => '')),
  5547. '/option',
  5548. '*/select'
  5549. );
  5550. $this->assertTags($result, $expected);
  5551. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5552. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5553. $now = strtotime($this->Form->data['Model']['field']);
  5554. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5555. $expected = array(
  5556. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5557. $daysRegex,
  5558. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5559. date('j', $now),
  5560. '/option',
  5561. '*/select',
  5562. '-',
  5563. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5564. $monthsRegex,
  5565. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5566. date('F', $now),
  5567. '/option',
  5568. '*/select',
  5569. '-',
  5570. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5571. $yearsRegex,
  5572. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5573. date('Y', $now),
  5574. '/option',
  5575. '*/select',
  5576. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5577. $hoursRegex,
  5578. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5579. date('g', $now),
  5580. '/option',
  5581. '*/select',
  5582. ':',
  5583. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5584. $minutesRegex,
  5585. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5586. date('i', $now),
  5587. '/option',
  5588. '*/select',
  5589. ' ',
  5590. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5591. $meridianRegex,
  5592. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5593. date('a', $now),
  5594. '/option',
  5595. '*/select'
  5596. );
  5597. $this->assertTags($result, $expected);
  5598. $selected = strtotime('2008-10-26 12:33:00');
  5599. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5600. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5601. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5602. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5603. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5604. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5605. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5606. $this->Form->create('Contact');
  5607. $result = $this->Form->input('published');
  5608. $now = strtotime('now');
  5609. $expected = array(
  5610. 'div' => array('class' => 'input date'),
  5611. 'label' => array('for' => 'ContactPublishedMonth'),
  5612. 'Published',
  5613. '/label',
  5614. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5615. $monthsRegex,
  5616. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5617. date('F', $now),
  5618. '/option',
  5619. '*/select',
  5620. '-',
  5621. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5622. $daysRegex,
  5623. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5624. date('j', $now),
  5625. '/option',
  5626. '*/select',
  5627. '-',
  5628. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5629. $yearsRegex,
  5630. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5631. date('Y', $now),
  5632. '/option',
  5633. '*/select',
  5634. '/div'
  5635. );
  5636. $this->assertTags($result, $expected);
  5637. $result = $this->Form->input('published2', array('type' => 'date'));
  5638. $now = strtotime('now');
  5639. $expected = array(
  5640. 'div' => array('class' => 'input date'),
  5641. 'label' => array('for' => 'ContactPublished2Month'),
  5642. 'Published2',
  5643. '/label',
  5644. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5645. $monthsRegex,
  5646. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5647. date('F', $now),
  5648. '/option',
  5649. '*/select',
  5650. '-',
  5651. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5652. $daysRegex,
  5653. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5654. date('j', $now),
  5655. '/option',
  5656. '*/select',
  5657. '-',
  5658. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5659. $yearsRegex,
  5660. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5661. date('Y', $now),
  5662. '/option',
  5663. '*/select',
  5664. '/div'
  5665. );
  5666. $this->assertTags($result, $expected);
  5667. $this->Form->create('Contact');
  5668. $result = $this->Form->input('published', array('monthNames' => false));
  5669. $now = strtotime('now');
  5670. $expected = array(
  5671. 'div' => array('class' => 'input date'),
  5672. 'label' => array('for' => 'ContactPublishedMonth'),
  5673. 'Published',
  5674. '/label',
  5675. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5676. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5677. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5678. date('m', $now),
  5679. '/option',
  5680. '*/select',
  5681. '-',
  5682. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5683. $daysRegex,
  5684. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5685. date('j', $now),
  5686. '/option',
  5687. '*/select',
  5688. '-',
  5689. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5690. $yearsRegex,
  5691. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5692. date('Y', $now),
  5693. '/option',
  5694. '*/select',
  5695. '/div'
  5696. );
  5697. $this->assertTags($result, $expected);
  5698. $result = $this->Form->input('published', array(
  5699. 'timeFormat' => 24,
  5700. 'interval' => 5,
  5701. 'selected' => strtotime('2009-09-03 13:37:00'),
  5702. 'type' => 'datetime'
  5703. ));
  5704. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5705. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5706. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5707. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5708. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5709. }
  5710. /**
  5711. * Test dateTime with rounding
  5712. *
  5713. * @return void
  5714. */
  5715. public function testDateTimeRounding() {
  5716. $this->Form->request->data['Contact'] = array(
  5717. 'date' => array(
  5718. 'day' => '13',
  5719. 'month' => '12',
  5720. 'year' => '2010',
  5721. 'hour' => '04',
  5722. 'min' => '19',
  5723. 'meridian' => 'AM'
  5724. )
  5725. );
  5726. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5727. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5728. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5729. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5730. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5731. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5732. }
  5733. /**
  5734. * Test that empty values don't trigger errors.
  5735. *
  5736. * @return void
  5737. */
  5738. public function testDateTimeNoErrorsOnEmptyData() {
  5739. $this->Form->request->data['Contact'] = array(
  5740. 'date' => array(
  5741. 'day' => '',
  5742. 'month' => '',
  5743. 'year' => '',
  5744. 'hour' => '',
  5745. 'min' => '',
  5746. 'meridian' => ''
  5747. )
  5748. );
  5749. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5750. $this->assertNotEmpty($result);
  5751. }
  5752. /**
  5753. * test that datetime() and default values work.
  5754. *
  5755. * @return void
  5756. */
  5757. public function testDatetimeWithDefault() {
  5758. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5759. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5760. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5761. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5762. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5763. 'default' => '2009-06-01 11:15:30'
  5764. ));
  5765. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5766. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5767. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5768. }
  5769. /**
  5770. * test that bogus non-date time data doesn't cause errors.
  5771. *
  5772. * @return void
  5773. */
  5774. public function testDateTimeWithBogusData() {
  5775. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5776. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5777. }
  5778. /**
  5779. * testDateTime all zeros
  5780. *
  5781. * @return void
  5782. */
  5783. public function testDateTimeAllZeros() {
  5784. $result = $this->Form->dateTime('Contact.date',
  5785. 'DMY',
  5786. false,
  5787. array(
  5788. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5789. 'value' => '0000-00-00'
  5790. )
  5791. );
  5792. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5793. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5794. }
  5795. /**
  5796. * testDateTimeEmptyAsArray
  5797. *
  5798. * @return void
  5799. */
  5800. public function testDateTimeEmptyAsArray() {
  5801. $result = $this->Form->dateTime('Contact.date',
  5802. 'DMY',
  5803. '12',
  5804. array(
  5805. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5806. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5807. )
  5808. )
  5809. );
  5810. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5811. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5812. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5813. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5814. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5815. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5816. $result = $this->Form->dateTime('Contact.date',
  5817. 'DMY',
  5818. '12',
  5819. array(
  5820. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5821. )
  5822. );
  5823. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5824. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5825. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5826. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5827. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5828. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5829. }
  5830. /**
  5831. * testFormDateTimeMulti method
  5832. *
  5833. * test multiple datetime element generation
  5834. *
  5835. * @return void
  5836. */
  5837. public function testFormDateTimeMulti() {
  5838. extract($this->dateRegex);
  5839. $result = $this->Form->dateTime('Contact.1.updated');
  5840. $expected = array(
  5841. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5842. $daysRegex,
  5843. array('option' => array('value' => '')),
  5844. '/option',
  5845. '*/select',
  5846. '-',
  5847. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5848. $monthsRegex,
  5849. array('option' => array('value' => '')),
  5850. '/option',
  5851. '*/select',
  5852. '-',
  5853. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5854. $yearsRegex,
  5855. array('option' => array('value' => '')),
  5856. '/option',
  5857. '*/select',
  5858. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5859. $hoursRegex,
  5860. array('option' => array('value' => '')),
  5861. '/option',
  5862. '*/select',
  5863. ':',
  5864. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5865. $minutesRegex,
  5866. array('option' => array('value' => '')),
  5867. '/option',
  5868. '*/select',
  5869. ' ',
  5870. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5871. $meridianRegex,
  5872. array('option' => array('value' => '')),
  5873. '/option',
  5874. '*/select'
  5875. );
  5876. $this->assertTags($result, $expected);
  5877. $result = $this->Form->dateTime('Contact.2.updated');
  5878. $expected = array(
  5879. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5880. $daysRegex,
  5881. array('option' => array('value' => '')),
  5882. '/option',
  5883. '*/select',
  5884. '-',
  5885. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5886. $monthsRegex,
  5887. array('option' => array('value' => '')),
  5888. '/option',
  5889. '*/select',
  5890. '-',
  5891. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5892. $yearsRegex,
  5893. array('option' => array('value' => '')),
  5894. '/option',
  5895. '*/select',
  5896. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5897. $hoursRegex,
  5898. array('option' => array('value' => '')),
  5899. '/option',
  5900. '*/select',
  5901. ':',
  5902. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5903. $minutesRegex,
  5904. array('option' => array('value' => '')),
  5905. '/option',
  5906. '*/select',
  5907. ' ',
  5908. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5909. $meridianRegex,
  5910. array('option' => array('value' => '')),
  5911. '/option',
  5912. '*/select'
  5913. );
  5914. $this->assertTags($result, $expected);
  5915. }
  5916. /**
  5917. * When changing the date format, the label should always focus the first select box when
  5918. * clicked.
  5919. *
  5920. * @return void
  5921. */
  5922. public function testDateTimeLabelIdMatchesFirstInput() {
  5923. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5924. $this->assertContains('label for="ModelDateMonth"', $result);
  5925. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5926. $this->assertContains('label for="ModelDateDay"', $result);
  5927. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5928. $this->assertContains('label for="ModelDateYear"', $result);
  5929. }
  5930. /**
  5931. * testMonth method
  5932. *
  5933. * @return void
  5934. */
  5935. public function testMonth() {
  5936. $result = $this->Form->month('Model.field');
  5937. $expected = array(
  5938. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5939. array('option' => array('value' => '')),
  5940. '/option',
  5941. array('option' => array('value' => '01')),
  5942. date('F', strtotime('2008-01-01 00:00:00')),
  5943. '/option',
  5944. array('option' => array('value' => '02')),
  5945. date('F', strtotime('2008-02-01 00:00:00')),
  5946. '/option',
  5947. '*/select',
  5948. );
  5949. $this->assertTags($result, $expected);
  5950. $result = $this->Form->month('Model.field', array('empty' => true));
  5951. $expected = array(
  5952. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5953. array('option' => array('value' => '')),
  5954. '/option',
  5955. array('option' => array('value' => '01')),
  5956. date('F', strtotime('2008-01-01 00:00:00')),
  5957. '/option',
  5958. array('option' => array('value' => '02')),
  5959. date('F', strtotime('2008-02-01 00:00:00')),
  5960. '/option',
  5961. '*/select',
  5962. );
  5963. $this->assertTags($result, $expected);
  5964. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5965. $expected = array(
  5966. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5967. array('option' => array('value' => '')),
  5968. '/option',
  5969. array('option' => array('value' => '01')),
  5970. '01',
  5971. '/option',
  5972. array('option' => array('value' => '02')),
  5973. '02',
  5974. '/option',
  5975. '*/select',
  5976. );
  5977. $this->assertTags($result, $expected);
  5978. $monthNames = array(
  5979. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5980. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5981. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5982. $expected = array(
  5983. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5984. array('option' => array('value' => '')),
  5985. '/option',
  5986. array('option' => array('value' => '01')),
  5987. 'Jan',
  5988. '/option',
  5989. array('option' => array('value' => '02')),
  5990. 'Feb',
  5991. '/option',
  5992. '*/select',
  5993. );
  5994. $this->assertTags($result, $expected);
  5995. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5996. $result = $this->Form->month('Project.release');
  5997. $expected = array(
  5998. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5999. array('option' => array('value' => '')),
  6000. '/option',
  6001. array('option' => array('value' => '01')),
  6002. 'January',
  6003. '/option',
  6004. array('option' => array('value' => '02', 'selected' => 'selected')),
  6005. 'February',
  6006. '/option',
  6007. '*/select',
  6008. );
  6009. $this->assertTags($result, $expected);
  6010. }
  6011. /**
  6012. * testDay method
  6013. *
  6014. * @return void
  6015. */
  6016. public function testDay() {
  6017. extract($this->dateRegex);
  6018. $result = $this->Form->day('Model.field', array('value' => false));
  6019. $expected = array(
  6020. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6021. array('option' => array('value' => '')),
  6022. '/option',
  6023. array('option' => array('value' => '01')),
  6024. '1',
  6025. '/option',
  6026. array('option' => array('value' => '02')),
  6027. '2',
  6028. '/option',
  6029. $daysRegex,
  6030. '/select',
  6031. );
  6032. $this->assertTags($result, $expected);
  6033. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6034. $result = $this->Form->day('Model.field');
  6035. $expected = array(
  6036. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6037. array('option' => array('value' => '')),
  6038. '/option',
  6039. array('option' => array('value' => '01')),
  6040. '1',
  6041. '/option',
  6042. array('option' => array('value' => '02')),
  6043. '2',
  6044. '/option',
  6045. $daysRegex,
  6046. array('option' => array('value' => '10', 'selected' => 'selected')),
  6047. '10',
  6048. '/option',
  6049. $daysRegex,
  6050. '/select',
  6051. );
  6052. $this->assertTags($result, $expected);
  6053. $this->Form->request->data['Model']['field'] = '';
  6054. $result = $this->Form->day('Model.field', array('value' => '10'));
  6055. $expected = array(
  6056. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6057. array('option' => array('value' => '')),
  6058. '/option',
  6059. array('option' => array('value' => '01')),
  6060. '1',
  6061. '/option',
  6062. array('option' => array('value' => '02')),
  6063. '2',
  6064. '/option',
  6065. $daysRegex,
  6066. array('option' => array('value' => '10', 'selected' => 'selected')),
  6067. '10',
  6068. '/option',
  6069. $daysRegex,
  6070. '/select',
  6071. );
  6072. $this->assertTags($result, $expected);
  6073. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6074. $result = $this->Form->day('Model.field', array('value' => true));
  6075. $expected = array(
  6076. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6077. array('option' => array('value' => '')),
  6078. '/option',
  6079. array('option' => array('value' => '01')),
  6080. '1',
  6081. '/option',
  6082. array('option' => array('value' => '02')),
  6083. '2',
  6084. '/option',
  6085. $daysRegex,
  6086. array('option' => array('value' => '10', 'selected' => 'selected')),
  6087. '10',
  6088. '/option',
  6089. $daysRegex,
  6090. '/select',
  6091. );
  6092. $this->assertTags($result, $expected);
  6093. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6094. $result = $this->Form->day('Project.release');
  6095. $expected = array(
  6096. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6097. array('option' => array('value' => '')),
  6098. '/option',
  6099. array('option' => array('value' => '01')),
  6100. '1',
  6101. '/option',
  6102. array('option' => array('value' => '02')),
  6103. '2',
  6104. '/option',
  6105. $daysRegex,
  6106. array('option' => array('value' => '10', 'selected' => 'selected')),
  6107. '10',
  6108. '/option',
  6109. $daysRegex,
  6110. '/select',
  6111. );
  6112. $this->assertTags($result, $expected);
  6113. }
  6114. /**
  6115. * testMinute method
  6116. *
  6117. * @return void
  6118. */
  6119. public function testMinute() {
  6120. extract($this->dateRegex);
  6121. $result = $this->Form->minute('Model.field');
  6122. $expected = array(
  6123. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6124. array('option' => array('value' => '')),
  6125. '/option',
  6126. array('option' => array('value' => '00')),
  6127. '00',
  6128. '/option',
  6129. array('option' => array('value' => '01')),
  6130. '01',
  6131. '/option',
  6132. array('option' => array('value' => '02')),
  6133. '02',
  6134. '/option',
  6135. $minutesRegex,
  6136. '/select',
  6137. );
  6138. $this->assertTags($result, $expected);
  6139. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6140. $result = $this->Form->minute('Model.field');
  6141. $expected = array(
  6142. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6143. array('option' => array('value' => '')),
  6144. '/option',
  6145. array('option' => array('value' => '00')),
  6146. '00',
  6147. '/option',
  6148. array('option' => array('value' => '01')),
  6149. '01',
  6150. '/option',
  6151. array('option' => array('value' => '02')),
  6152. '02',
  6153. '/option',
  6154. $minutesRegex,
  6155. array('option' => array('value' => '12', 'selected' => 'selected')),
  6156. '12',
  6157. '/option',
  6158. $minutesRegex,
  6159. '/select',
  6160. );
  6161. $this->assertTags($result, $expected);
  6162. $this->Form->request->data['Model']['field'] = '';
  6163. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6164. $expected = array(
  6165. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6166. array('option' => array('value' => '')),
  6167. '/option',
  6168. array('option' => array('value' => '00')),
  6169. '00',
  6170. '/option',
  6171. array('option' => array('value' => '05')),
  6172. '05',
  6173. '/option',
  6174. array('option' => array('value' => '10')),
  6175. '10',
  6176. '/option',
  6177. $minutesRegex,
  6178. '/select',
  6179. );
  6180. $this->assertTags($result, $expected);
  6181. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6182. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6183. $expected = array(
  6184. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6185. array('option' => array('value' => '')),
  6186. '/option',
  6187. array('option' => array('value' => '00')),
  6188. '00',
  6189. '/option',
  6190. array('option' => array('value' => '05')),
  6191. '05',
  6192. '/option',
  6193. array('option' => array('value' => '10', 'selected' => 'selected')),
  6194. '10',
  6195. '/option',
  6196. $minutesRegex,
  6197. '/select',
  6198. );
  6199. $this->assertTags($result, $expected);
  6200. }
  6201. /**
  6202. * testHour method
  6203. *
  6204. * @return void
  6205. */
  6206. public function testHour() {
  6207. extract($this->dateRegex);
  6208. $result = $this->Form->hour('Model.field', false);
  6209. $expected = array(
  6210. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6211. array('option' => array('value' => '')),
  6212. '/option',
  6213. array('option' => array('value' => '01')),
  6214. '1',
  6215. '/option',
  6216. array('option' => array('value' => '02')),
  6217. '2',
  6218. '/option',
  6219. $hoursRegex,
  6220. '/select',
  6221. );
  6222. $this->assertTags($result, $expected);
  6223. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6224. $result = $this->Form->hour('Model.field', false);
  6225. $expected = array(
  6226. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6227. array('option' => array('value' => '')),
  6228. '/option',
  6229. array('option' => array('value' => '01')),
  6230. '1',
  6231. '/option',
  6232. array('option' => array('value' => '02')),
  6233. '2',
  6234. '/option',
  6235. $hoursRegex,
  6236. array('option' => array('value' => '12', 'selected' => 'selected')),
  6237. '12',
  6238. '/option',
  6239. '/select',
  6240. );
  6241. $this->assertTags($result, $expected);
  6242. $this->Form->request->data['Model']['field'] = '';
  6243. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6244. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6245. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6246. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6247. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6248. $result = $this->Form->hour('Model.field', true);
  6249. $expected = array(
  6250. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6251. array('option' => array('value' => '')),
  6252. '/option',
  6253. array('option' => array('value' => '00', 'selected' => 'selected')),
  6254. '0',
  6255. '/option',
  6256. array('option' => array('value' => '01')),
  6257. '1',
  6258. '/option',
  6259. array('option' => array('value' => '02')),
  6260. '2',
  6261. '/option',
  6262. $hoursRegex,
  6263. '/select',
  6264. );
  6265. $this->assertTags($result, $expected);
  6266. unset($this->Form->request->data['Model']['field']);
  6267. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6268. $thisHour = date('H');
  6269. $optValue = date('G');
  6270. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6271. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6272. $result = $this->Form->hour('Model.field', true);
  6273. $expected = array(
  6274. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6275. array('option' => array('value' => '')),
  6276. '/option',
  6277. array('option' => array('value' => '00')),
  6278. '0',
  6279. '/option',
  6280. array('option' => array('value' => '01', 'selected' => 'selected')),
  6281. '1',
  6282. '/option',
  6283. array('option' => array('value' => '02')),
  6284. '2',
  6285. '/option',
  6286. $hoursRegex,
  6287. '/select',
  6288. );
  6289. $this->assertTags($result, $expected);
  6290. }
  6291. /**
  6292. * testYear method
  6293. *
  6294. * @return void
  6295. */
  6296. public function testYear() {
  6297. $result = $this->Form->year('Model.field', 2006, 2007);
  6298. $expected = array(
  6299. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6300. array('option' => array('value' => '')),
  6301. '/option',
  6302. array('option' => array('value' => '2007')),
  6303. '2007',
  6304. '/option',
  6305. array('option' => array('value' => '2006')),
  6306. '2006',
  6307. '/option',
  6308. '/select',
  6309. );
  6310. $this->assertTags($result, $expected);
  6311. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6312. $expected = array(
  6313. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6314. array('option' => array('value' => '')),
  6315. '/option',
  6316. array('option' => array('value' => '2006')),
  6317. '2006',
  6318. '/option',
  6319. array('option' => array('value' => '2007')),
  6320. '2007',
  6321. '/option',
  6322. '/select',
  6323. );
  6324. $this->assertTags($result, $expected);
  6325. $this->request->data['Contact']['published'] = '';
  6326. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6327. $expected = array(
  6328. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6329. array('option' => array('value' => '')),
  6330. '/option',
  6331. array('option' => array('value' => '2007')),
  6332. '2007',
  6333. '/option',
  6334. array('option' => array('value' => '2006')),
  6335. '2006',
  6336. '/option',
  6337. '/select',
  6338. );
  6339. $this->assertTags($result, $expected);
  6340. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6341. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6342. $expected = array(
  6343. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6344. array('option' => array('value' => '2007')),
  6345. '2007',
  6346. '/option',
  6347. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6348. '2006',
  6349. '/option',
  6350. '/select',
  6351. );
  6352. $this->assertTags($result, $expected);
  6353. $this->Form->request->data['Contact']['published'] = '';
  6354. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6355. $expected = array(
  6356. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6357. array('option' => array('value' => '')),
  6358. '/option',
  6359. array('option' => array('value' => '2007')),
  6360. '2007',
  6361. '/option',
  6362. array('option' => array('value' => '2006')),
  6363. '2006',
  6364. '/option',
  6365. '/select',
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6369. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6370. $expected = array(
  6371. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6372. array('option' => array('value' => '2007')),
  6373. '2007',
  6374. '/option',
  6375. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6376. '2006',
  6377. '/option',
  6378. '/select',
  6379. );
  6380. $this->assertTags($result, $expected);
  6381. $this->Form->request->data['Contact']['published'] = '';
  6382. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6383. $expected = array(
  6384. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6385. array('option' => array('value' => '')),
  6386. '/option',
  6387. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6388. '2007',
  6389. '/option',
  6390. array('option' => array('value' => '2006')),
  6391. '2006',
  6392. '/option',
  6393. '/select',
  6394. );
  6395. $this->assertTags($result, $expected);
  6396. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6397. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6398. $expected = array(
  6399. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6400. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6401. '2007',
  6402. '/option',
  6403. array('option' => array('value' => '2006')),
  6404. '2006',
  6405. '/option',
  6406. '/select',
  6407. );
  6408. $this->assertTags($result, $expected);
  6409. $this->Form->request->data['Contact']['published'] = '';
  6410. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6411. $expected = array(
  6412. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6413. array('option' => array('value' => '2008')),
  6414. '2008',
  6415. '/option',
  6416. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6417. '2007',
  6418. '/option',
  6419. array('option' => array('value' => '2006')),
  6420. '2006',
  6421. '/option',
  6422. '/select',
  6423. );
  6424. $this->assertTags($result, $expected);
  6425. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6426. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6427. $expected = array(
  6428. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6429. array('option' => array('value' => '2008')),
  6430. '2008',
  6431. '/option',
  6432. array('option' => array('value' => '2007')),
  6433. '2007',
  6434. '/option',
  6435. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6436. '2006',
  6437. '/option',
  6438. '/select',
  6439. );
  6440. $this->assertTags($result, $expected);
  6441. $this->Form->request->data = array();
  6442. $this->Form->create('Contact');
  6443. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6444. $expected = array(
  6445. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6446. array('option' => array('value' => '2008')),
  6447. '2008',
  6448. '/option',
  6449. array('option' => array('value' => '2007')),
  6450. '2007',
  6451. '/option',
  6452. array('option' => array('value' => '2006')),
  6453. '2006',
  6454. '/option',
  6455. '/select',
  6456. );
  6457. $this->assertTags($result, $expected);
  6458. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6459. $this->assertContains('data[Contact][published][year]', $result);
  6460. }
  6461. /**
  6462. * testYearAutoExpandRange method
  6463. *
  6464. * @return void
  6465. */
  6466. public function testYearAutoExpandRange() {
  6467. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6468. $result = $this->Form->year('User.birthday');
  6469. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6470. $result = $matches[1];
  6471. $expected = range(date('Y') + 20, 1930);
  6472. $this->assertEquals($expected, $result);
  6473. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6474. $result = $this->Form->year('Project.release');
  6475. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6476. $result = $matches[1];
  6477. $expected = range(2050, date('Y') - 20);
  6478. $this->assertEquals($expected, $result);
  6479. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6480. $result = $this->Form->year('Project.release', 1890, 1900);
  6481. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6482. $result = $matches[1];
  6483. $expected = range(1900, 1881);
  6484. $this->assertEquals($expected, $result);
  6485. }
  6486. /**
  6487. * testInputDate method
  6488. *
  6489. * Test various inputs with type date and different dateFormat values.
  6490. * Failing to provide a dateFormat key should not error.
  6491. * It should simply not pre-select any value then.
  6492. *
  6493. * @return void
  6494. */
  6495. public function testInputDate() {
  6496. $this->Form->request->data = array(
  6497. 'User' => array(
  6498. 'month_year' => array('month' => date('m')),
  6499. 'just_year' => array('month' => date('m')),
  6500. 'just_month' => array('year' => date('Y')),
  6501. 'just_day' => array('month' => date('m')),
  6502. )
  6503. );
  6504. $this->Form->create('User');
  6505. $result = $this->Form->input('month_year',
  6506. array(
  6507. 'label' => false,
  6508. 'div' => false,
  6509. 'type' => 'date',
  6510. 'dateFormat' => 'MY',
  6511. 'minYear' => 2006,
  6512. 'maxYear' => 2008
  6513. )
  6514. );
  6515. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6516. $this->assertNotContains('value="2008" selected="selected"', $result);
  6517. $result = $this->Form->input('just_year',
  6518. array(
  6519. 'type' => 'date',
  6520. 'label' => false,
  6521. 'dateFormat' => 'Y',
  6522. 'minYear' => date('Y'),
  6523. 'maxYear' => date('Y', strtotime('+20 years'))
  6524. )
  6525. );
  6526. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6527. $result = $this->Form->input('just_month',
  6528. array(
  6529. 'type' => 'date',
  6530. 'label' => false,
  6531. 'dateFormat' => 'M',
  6532. 'empty' => false,
  6533. )
  6534. );
  6535. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6536. $result = $this->Form->input('just_day',
  6537. array(
  6538. 'type' => 'date',
  6539. 'label' => false,
  6540. 'dateFormat' => 'D',
  6541. 'empty' => false,
  6542. )
  6543. );
  6544. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6545. }
  6546. /**
  6547. * testInputDateMaxYear method
  6548. *
  6549. * Let's say we want to only allow users born from 2006 to 2008 to register
  6550. * This being the first singup page, we still don't have any data
  6551. *
  6552. * @return void
  6553. */
  6554. public function testInputDateMaxYear() {
  6555. $this->Form->request->data = array();
  6556. $this->Form->create('User');
  6557. $result = $this->Form->input('birthday',
  6558. array(
  6559. 'label' => false,
  6560. 'div' => false,
  6561. 'type' => 'date',
  6562. 'dateFormat' => 'DMY',
  6563. 'minYear' => 2006,
  6564. 'maxYear' => 2008
  6565. )
  6566. );
  6567. $this->assertContains('value="2008" selected="selected"', $result);
  6568. }
  6569. /**
  6570. * testTextArea method
  6571. *
  6572. * @return void
  6573. */
  6574. public function testTextArea() {
  6575. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6576. $result = $this->Form->textarea('Model.field');
  6577. $expected = array(
  6578. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6579. 'some test data',
  6580. '/textarea',
  6581. );
  6582. $this->assertTags($result, $expected);
  6583. $result = $this->Form->textarea('Model.tmp');
  6584. $expected = array(
  6585. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6586. '/textarea',
  6587. );
  6588. $this->assertTags($result, $expected);
  6589. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6590. $result = $this->Form->textarea('Model.field');
  6591. $expected = array(
  6592. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6593. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6594. '/textarea',
  6595. );
  6596. $this->assertTags($result, $expected);
  6597. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6598. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6599. $expected = array(
  6600. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6601. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6602. '/textarea',
  6603. );
  6604. $this->assertTags($result, $expected);
  6605. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6606. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6607. $expected = array(
  6608. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6609. '/textarea'
  6610. );
  6611. $this->assertTags($result, $expected);
  6612. }
  6613. /**
  6614. * testTextAreaWithStupidCharacters method
  6615. *
  6616. * test text area with non-ascii characters
  6617. *
  6618. * @return void
  6619. */
  6620. public function testTextAreaWithStupidCharacters() {
  6621. $this->loadFixtures('Post');
  6622. $result = $this->Form->input('Post.content', array(
  6623. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6624. ));
  6625. $expected = array(
  6626. 'div' => array('class' => 'input textarea'),
  6627. 'label' => array('for' => 'PostContent'),
  6628. 'Current Text',
  6629. '/label',
  6630. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6631. 'GREAT®',
  6632. '/textarea',
  6633. '/div'
  6634. );
  6635. $this->assertTags($result, $expected);
  6636. }
  6637. /**
  6638. * testHiddenField method
  6639. *
  6640. * @return void
  6641. */
  6642. public function testHiddenField() {
  6643. $Contact = ClassRegistry::getObject('Contact');
  6644. $Contact->validationErrors['field'] = 1;
  6645. $this->Form->request->data['Contact']['field'] = 'test';
  6646. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6647. $this->assertTags($result, array(
  6648. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6649. );
  6650. }
  6651. /**
  6652. * testFileUploadField method
  6653. *
  6654. * @return void
  6655. */
  6656. public function testFileUploadField() {
  6657. $result = $this->Form->file('Model.upload');
  6658. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6659. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6660. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6661. $expected = array(
  6662. 'div' => array('class' => 'input file'),
  6663. 'label' => array('for' => 'ModelUpload'),
  6664. 'Upload',
  6665. '/label',
  6666. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6667. '/div'
  6668. );
  6669. $this->assertTags($result, $expected);
  6670. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6671. $result = $this->Form->file('Model.upload');
  6672. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6673. }
  6674. /**
  6675. * test File upload input on a model not used in create();
  6676. *
  6677. * @return void
  6678. */
  6679. public function testFileUploadOnOtherModel() {
  6680. $this->Form->create('ValidateUser', array('type' => 'file'));
  6681. $result = $this->Form->file('ValidateProfile.city');
  6682. $expected = array(
  6683. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6684. );
  6685. $this->assertTags($result, $expected);
  6686. }
  6687. /**
  6688. * testButton method
  6689. *
  6690. * @return void
  6691. */
  6692. public function testButton() {
  6693. $result = $this->Form->button('Hi');
  6694. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6695. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6696. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6697. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6698. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6699. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6700. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6701. $result = $this->Form->button('No type', array('type' => false));
  6702. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6703. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6704. $this->assertNotRegExp('/\&039/', $result);
  6705. }
  6706. /**
  6707. * Test that button() makes unlocked fields by default.
  6708. *
  6709. * @return void
  6710. */
  6711. public function testButtonUnlockedByDefault() {
  6712. $this->Form->request->params['_Token']['key'] = 'secured';
  6713. $this->Form->button('Save', array('name' => 'save'));
  6714. $this->Form->button('Clear');
  6715. $result = $this->Form->unlockField();
  6716. $this->assertEquals(array('save'), $result);
  6717. }
  6718. /**
  6719. * testPostButton method
  6720. *
  6721. * @return void
  6722. */
  6723. public function testPostButton() {
  6724. $result = $this->Form->postButton('Hi', '/controller/action');
  6725. $this->assertTags($result, array(
  6726. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6727. 'div' => array('style' => 'display:none;'),
  6728. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6729. '/div',
  6730. 'button' => array('type' => 'submit'),
  6731. 'Hi',
  6732. '/button',
  6733. '/form'
  6734. ));
  6735. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6736. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6737. }
  6738. /**
  6739. * Test that postButton adds _Token fields.
  6740. *
  6741. * @return void
  6742. */
  6743. public function testSecurePostButton() {
  6744. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6745. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6746. $expected = array(
  6747. 'form' => array(
  6748. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6749. ),
  6750. array('div' => array('style' => 'display:none;')),
  6751. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6752. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6753. '/div',
  6754. 'button' => array('type' => 'submit'),
  6755. 'Delete',
  6756. '/button',
  6757. array('div' => array('style' => 'display:none;')),
  6758. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6759. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6760. '/div',
  6761. '/form',
  6762. );
  6763. $this->assertTags($result, $expected);
  6764. }
  6765. /**
  6766. * testPostLink method
  6767. *
  6768. * @return void
  6769. */
  6770. public function testPostLink() {
  6771. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6772. $this->assertTags($result, array(
  6773. 'form' => array(
  6774. 'method' => 'post', 'action' => '/posts/delete/1',
  6775. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6776. ),
  6777. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6778. '/form',
  6779. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6780. 'Delete',
  6781. '/a'
  6782. ));
  6783. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6784. $this->assertTags($result, array(
  6785. 'form' => array(
  6786. 'method' => 'post', 'action' => '/posts/delete/1',
  6787. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6788. ),
  6789. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6790. '/form',
  6791. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6792. 'Delete',
  6793. '/a'
  6794. ));
  6795. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6796. $this->assertTags($result, array(
  6797. 'form' => array(
  6798. 'method' => 'post', 'action' => '/posts/delete/1',
  6799. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6800. ),
  6801. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6802. '/form',
  6803. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6804. 'Delete',
  6805. '/a'
  6806. ));
  6807. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6808. $this->assertTags($result, array(
  6809. 'form' => array(
  6810. 'method' => 'post', 'action' => '/posts/delete/1',
  6811. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6812. ),
  6813. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6814. '/form',
  6815. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6816. 'Delete',
  6817. '/a'
  6818. ));
  6819. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6820. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6821. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6822. $this->assertTags($result, array(
  6823. 'form' => array(
  6824. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6825. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6826. ),
  6827. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6828. '/form',
  6829. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6830. 'Delete',
  6831. '/a'
  6832. ));
  6833. $result = $this->Form->postLink(
  6834. '',
  6835. array('controller' => 'items', 'action' => 'delete', 10),
  6836. array('class' => 'btn btn-danger', 'escape' => false),
  6837. 'Confirm thing'
  6838. );
  6839. $this->assertTags($result, array(
  6840. 'form' => array(
  6841. 'method' => 'post', 'action' => '/items/delete/10',
  6842. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6843. ),
  6844. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6845. '/form',
  6846. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6847. '/a'
  6848. ));
  6849. }
  6850. /**
  6851. * test creating postLinks after a GET form.
  6852. *
  6853. * @return void
  6854. */
  6855. public function testPostLinkAfterGetForm() {
  6856. $this->Form->request->params['_Token']['key'] = 'testkey';
  6857. $this->Form->create('User', array('type' => 'get'));
  6858. $this->Form->end();
  6859. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6860. $this->assertTags($result, array(
  6861. 'form' => array(
  6862. 'method' => 'post', 'action' => '/posts/delete/1',
  6863. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6864. ),
  6865. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6866. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6867. 'div' => array('style' => 'display:none;'),
  6868. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6869. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6870. '/div',
  6871. '/form',
  6872. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6873. 'Delete',
  6874. '/a'
  6875. ));
  6876. }
  6877. /**
  6878. * Test that postLink adds _Token fields.
  6879. *
  6880. * @return void
  6881. */
  6882. public function testSecurePostLink() {
  6883. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6884. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6885. $expected = array(
  6886. 'form' => array(
  6887. 'method' => 'post', 'action' => '/posts/delete/1',
  6888. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6889. ),
  6890. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6891. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6892. 'div' => array('style' => 'display:none;'),
  6893. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6894. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6895. '/div',
  6896. '/form',
  6897. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6898. 'Delete',
  6899. '/a'
  6900. );
  6901. $this->assertTags($result, $expected);
  6902. }
  6903. /**
  6904. * testSubmitButton method
  6905. *
  6906. * @return void
  6907. */
  6908. public function testSubmitButton() {
  6909. $result = $this->Form->submit('');
  6910. $expected = array(
  6911. 'div' => array('class' => 'submit'),
  6912. 'input' => array('type' => 'submit', 'value' => ''),
  6913. '/div'
  6914. );
  6915. $this->assertTags($result, $expected);
  6916. $result = $this->Form->submit('Test Submit');
  6917. $expected = array(
  6918. 'div' => array('class' => 'submit'),
  6919. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6920. '/div'
  6921. );
  6922. $this->assertTags($result, $expected);
  6923. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6924. $expected = array(
  6925. 'span' => array('class' => 'submit'),
  6926. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6927. '/span'
  6928. );
  6929. $this->assertTags($result, $expected);
  6930. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6931. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6932. $this->assertTags($result, $expected);
  6933. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6934. $expected = array(
  6935. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6936. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6937. '/div'
  6938. );
  6939. $this->assertTags($result, $expected);
  6940. $result = $this->Form->submit('Next >');
  6941. $expected = array(
  6942. 'div' => array('class' => 'submit'),
  6943. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6944. '/div'
  6945. );
  6946. $this->assertTags($result, $expected);
  6947. $result = $this->Form->submit('Next >', array('escape' => false));
  6948. $expected = array(
  6949. 'div' => array('class' => 'submit'),
  6950. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6951. '/div'
  6952. );
  6953. $this->assertTags($result, $expected);
  6954. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6955. $expected = array(
  6956. 'div' => array('class' => 'submit'),
  6957. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6958. '/div'
  6959. );
  6960. $this->assertTags($result, $expected);
  6961. $before = '--before--';
  6962. $after = '--after--';
  6963. $result = $this->Form->submit('Test', array('before' => $before));
  6964. $expected = array(
  6965. 'div' => array('class' => 'submit'),
  6966. '--before--',
  6967. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6968. '/div'
  6969. );
  6970. $this->assertTags($result, $expected);
  6971. $result = $this->Form->submit('Test', array('after' => $after));
  6972. $expected = array(
  6973. 'div' => array('class' => 'submit'),
  6974. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6975. '--after--',
  6976. '/div'
  6977. );
  6978. $this->assertTags($result, $expected);
  6979. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6980. $expected = array(
  6981. 'div' => array('class' => 'submit'),
  6982. '--before--',
  6983. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6984. '--after--',
  6985. '/div'
  6986. );
  6987. $this->assertTags($result, $expected);
  6988. }
  6989. /**
  6990. * test image submit types.
  6991. *
  6992. * @return void
  6993. */
  6994. public function testSubmitImage() {
  6995. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6996. $expected = array(
  6997. 'div' => array('class' => 'submit'),
  6998. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6999. '/div'
  7000. );
  7001. $this->assertTags($result, $expected);
  7002. $result = $this->Form->submit('/relative/cake.power.gif');
  7003. $expected = array(
  7004. 'div' => array('class' => 'submit'),
  7005. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7006. '/div'
  7007. );
  7008. $this->assertTags($result, $expected);
  7009. $result = $this->Form->submit('cake.power.gif');
  7010. $expected = array(
  7011. 'div' => array('class' => 'submit'),
  7012. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7013. '/div'
  7014. );
  7015. $this->assertTags($result, $expected);
  7016. $result = $this->Form->submit('Not.an.image');
  7017. $expected = array(
  7018. 'div' => array('class' => 'submit'),
  7019. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7020. '/div'
  7021. );
  7022. $this->assertTags($result, $expected);
  7023. $after = '--after--';
  7024. $before = '--before--';
  7025. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7026. $expected = array(
  7027. 'div' => array('class' => 'submit'),
  7028. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7029. '--after--',
  7030. '/div'
  7031. );
  7032. $this->assertTags($result, $expected);
  7033. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7034. $expected = array(
  7035. 'div' => array('class' => 'submit'),
  7036. '--before--',
  7037. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7038. '/div'
  7039. );
  7040. $this->assertTags($result, $expected);
  7041. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7042. $expected = array(
  7043. 'div' => array('class' => 'submit'),
  7044. '--before--',
  7045. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7046. '--after--',
  7047. '/div'
  7048. );
  7049. $this->assertTags($result, $expected);
  7050. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7051. $expected = array(
  7052. 'div' => array('class' => 'submit'),
  7053. '--before--',
  7054. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7055. '--after--',
  7056. '/div'
  7057. );
  7058. $this->assertTags($result, $expected);
  7059. }
  7060. /**
  7061. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7062. * be submitted at a time.
  7063. *
  7064. * @return void
  7065. */
  7066. public function testSubmitUnlockedByDefault() {
  7067. $this->Form->request->params['_Token']['key'] = 'secured';
  7068. $this->Form->submit('Go go');
  7069. $this->Form->submit('Save', array('name' => 'save'));
  7070. $result = $this->Form->unlockField();
  7071. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7072. }
  7073. /**
  7074. * Test submit image with timestamps.
  7075. *
  7076. * @return void
  7077. */
  7078. public function testSubmitImageTimestamp() {
  7079. Configure::write('Asset.timestamp', 'force');
  7080. $result = $this->Form->submit('cake.power.gif');
  7081. $expected = array(
  7082. 'div' => array('class' => 'submit'),
  7083. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7084. '/div'
  7085. );
  7086. $this->assertTags($result, $expected);
  7087. }
  7088. /**
  7089. * test the create() method
  7090. *
  7091. * @return void
  7092. */
  7093. public function testCreate() {
  7094. $result = $this->Form->create('Contact');
  7095. $encoding = strtolower(Configure::read('App.encoding'));
  7096. $expected = array(
  7097. 'form' => array(
  7098. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7099. 'accept-charset' => $encoding
  7100. ),
  7101. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7102. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7103. '/div'
  7104. );
  7105. $this->assertTags($result, $expected);
  7106. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7107. $expected = array('form' => array(
  7108. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7109. 'accept-charset' => $encoding
  7110. ));
  7111. $this->assertTags($result, $expected);
  7112. $result = $this->Form->create('Contact', array('type' => 'get'));
  7113. $expected = array('form' => array(
  7114. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7115. 'accept-charset' => $encoding
  7116. ));
  7117. $this->assertTags($result, $expected);
  7118. $result = $this->Form->create('Contact', array('type' => 'put'));
  7119. $expected = array(
  7120. 'form' => array(
  7121. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7122. 'accept-charset' => $encoding
  7123. ),
  7124. 'div' => array('style' => 'display:none;'),
  7125. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7126. '/div'
  7127. );
  7128. $this->assertTags($result, $expected);
  7129. $result = $this->Form->create('Contact', array('type' => 'file'));
  7130. $expected = array(
  7131. 'form' => array(
  7132. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7133. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7134. ),
  7135. 'div' => array('style' => 'display:none;'),
  7136. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7137. '/div'
  7138. );
  7139. $this->assertTags($result, $expected);
  7140. $this->Form->request->data['Contact']['id'] = 1;
  7141. $this->Form->request->here = '/contacts/edit/1';
  7142. $this->Form->request['action'] = 'edit';
  7143. $result = $this->Form->create('Contact');
  7144. $expected = array(
  7145. 'form' => array(
  7146. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7147. 'accept-charset' => $encoding
  7148. ),
  7149. 'div' => array('style' => 'display:none;'),
  7150. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7151. '/div'
  7152. );
  7153. $this->assertTags($result, $expected);
  7154. $this->Form->request->data['Contact']['id'] = 1;
  7155. $this->Form->request->here = '/contacts/edit/1';
  7156. $this->Form->request['action'] = 'edit';
  7157. $result = $this->Form->create('Contact', array('type' => 'file'));
  7158. $expected = array(
  7159. 'form' => array(
  7160. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7161. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7162. ),
  7163. 'div' => array('style' => 'display:none;'),
  7164. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7165. '/div'
  7166. );
  7167. $this->assertTags($result, $expected);
  7168. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7169. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7170. $expected = array(
  7171. 'form' => array(
  7172. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7173. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7174. ),
  7175. 'div' => array('style' => 'display:none;'),
  7176. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7177. '/div'
  7178. );
  7179. $this->assertTags($result, $expected);
  7180. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7181. $expected = array(
  7182. 'form' => array(
  7183. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7184. 'accept-charset' => $encoding
  7185. ),
  7186. 'div' => array('style' => 'display:none;'),
  7187. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7188. '/div'
  7189. );
  7190. $this->assertTags($result, $expected);
  7191. $this->Form->request['action'] = 'add';
  7192. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7193. $expected = array(
  7194. 'form' => array(
  7195. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7196. 'accept-charset' => $encoding
  7197. ),
  7198. 'div' => array('style' => 'display:none;'),
  7199. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7200. '/div'
  7201. );
  7202. $this->assertTags($result, $expected);
  7203. $result = $this->Form->create('User', array('action' => 'login'));
  7204. $expected = array(
  7205. 'form' => array(
  7206. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7207. 'accept-charset' => $encoding
  7208. ),
  7209. 'div' => array('style' => 'display:none;'),
  7210. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7211. '/div'
  7212. );
  7213. $this->assertTags($result, $expected);
  7214. $result = $this->Form->create('User', array('url' => '/users/login'));
  7215. $expected = array(
  7216. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7217. 'div' => array('style' => 'display:none;'),
  7218. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7219. '/div'
  7220. );
  7221. $this->assertTags($result, $expected);
  7222. $this->Form->request['controller'] = 'pages';
  7223. $result = $this->Form->create('User', array('action' => 'signup'));
  7224. $expected = array(
  7225. 'form' => array(
  7226. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7227. 'accept-charset' => $encoding
  7228. ),
  7229. 'div' => array('style' => 'display:none;'),
  7230. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7231. '/div'
  7232. );
  7233. $this->assertTags($result, $expected);
  7234. $this->Form->request->data = array();
  7235. $this->Form->request['controller'] = 'contacts';
  7236. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7237. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7238. $expected = array(
  7239. 'form' => array(
  7240. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7241. 'accept-charset' => 'utf-8'
  7242. ),
  7243. 'div' => array('style' => 'display:none;'),
  7244. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7245. '/div'
  7246. );
  7247. $this->assertTags($result, $expected);
  7248. }
  7249. /**
  7250. * Test the onsubmit option for create()
  7251. *
  7252. * @return void
  7253. */
  7254. public function testCreateOnSubmit() {
  7255. $this->Form->request->data = array();
  7256. $this->Form->request['controller'] = 'contacts';
  7257. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7258. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7259. $expected = array(
  7260. 'form' => array(
  7261. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7262. 'accept-charset' => 'utf-8'
  7263. ),
  7264. 'div' => array('style' => 'display:none;'),
  7265. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7266. '/div'
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. $this->Form->request->data = array();
  7270. $this->Form->request['controller'] = 'contacts';
  7271. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7272. $result = $this->Form->create(array(
  7273. 'url' => array('action' => 'index', 'param'),
  7274. 'default' => false,
  7275. 'onsubmit' => 'someFunction();'
  7276. ));
  7277. $expected = array(
  7278. 'form' => array(
  7279. 'id' => 'ContactAddForm', 'method' => 'post',
  7280. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7281. 'action' => '/contacts/index/param',
  7282. 'accept-charset' => 'utf-8'
  7283. ),
  7284. 'div' => array('style' => 'display:none;'),
  7285. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7286. '/div'
  7287. );
  7288. $this->assertTags($result, $expected);
  7289. }
  7290. /**
  7291. * test create() with automatic url generation
  7292. *
  7293. * @return void
  7294. */
  7295. public function testCreateAutoUrl() {
  7296. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7297. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7298. $this->Form->request->base = '/base_url';
  7299. $result = $this->Form->create('Contact');
  7300. $expected = array(
  7301. 'form' => array(
  7302. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7303. 'accept-charset' => 'utf-8'
  7304. ),
  7305. 'div' => array('style' => 'display:none;'),
  7306. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7307. '/div'
  7308. );
  7309. $this->assertTags($result, $expected);
  7310. $this->Form->request['action'] = 'delete';
  7311. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7312. $this->Form->request->base = '/base_url';
  7313. $result = $this->Form->create('Contact');
  7314. $expected = array(
  7315. 'form' => array(
  7316. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7317. 'accept-charset' => 'utf-8'
  7318. ),
  7319. 'div' => array('style' => 'display:none;'),
  7320. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7321. '/div'
  7322. );
  7323. $this->assertTags($result, $expected);
  7324. }
  7325. /**
  7326. * test create() with a custom route
  7327. *
  7328. * @return void
  7329. */
  7330. public function testCreateCustomRoute() {
  7331. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7332. $encoding = strtolower(Configure::read('App.encoding'));
  7333. $result = $this->Form->create('User', array('action' => 'login'));
  7334. $expected = array(
  7335. 'form' => array(
  7336. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7337. 'accept-charset' => $encoding
  7338. ),
  7339. 'div' => array('style' => 'display:none;'),
  7340. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7341. '/div'
  7342. );
  7343. $this->assertTags($result, $expected);
  7344. }
  7345. /**
  7346. * test that inputDefaults are stored and used.
  7347. *
  7348. * @return void
  7349. */
  7350. public function testCreateWithInputDefaults() {
  7351. $this->Form->create('User', array(
  7352. 'inputDefaults' => array(
  7353. 'div' => false,
  7354. 'label' => false,
  7355. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7356. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7357. )
  7358. ));
  7359. $result = $this->Form->input('username');
  7360. $expected = array(
  7361. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7362. );
  7363. $this->assertTags($result, $expected);
  7364. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7365. $expected = array(
  7366. 'div' => array('class' => 'input text'),
  7367. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7368. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7369. '/div'
  7370. );
  7371. $this->assertTags($result, $expected);
  7372. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7373. $expected = array(
  7374. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7375. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7376. );
  7377. $this->assertTags($result, $expected);
  7378. $this->Form->create('User', array(
  7379. 'inputDefaults' => array(
  7380. 'div' => false,
  7381. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7382. )
  7383. ));
  7384. $result = $this->Form->input('username', array('div' => true));
  7385. $expected = array(
  7386. 'div' => array('class' => 'input text'),
  7387. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7388. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7389. '/div'
  7390. );
  7391. $this->assertTags($result, $expected);
  7392. }
  7393. /**
  7394. * test automatic accept-charset overriding
  7395. *
  7396. * @return void
  7397. */
  7398. public function testCreateWithAcceptCharset() {
  7399. $result = $this->Form->create('UserForm', array(
  7400. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7401. )
  7402. );
  7403. $expected = array(
  7404. 'form' => array(
  7405. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7406. 'accept-charset' => 'iso-8859-1'
  7407. ),
  7408. 'div' => array('style' => 'display:none;'),
  7409. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7410. '/div'
  7411. );
  7412. $this->assertTags($result, $expected);
  7413. }
  7414. /**
  7415. * Test base form URL when url param is passed with multiple parameters (&)
  7416. *
  7417. */
  7418. public function testCreateQuerystringrequest() {
  7419. $encoding = strtolower(Configure::read('App.encoding'));
  7420. $result = $this->Form->create('Contact', array(
  7421. 'type' => 'post',
  7422. 'escape' => false,
  7423. 'url' => array(
  7424. 'controller' => 'controller',
  7425. 'action' => 'action',
  7426. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7427. )
  7428. ));
  7429. $expected = array(
  7430. 'form' => array(
  7431. 'id' => 'ContactAddForm',
  7432. 'method' => 'post',
  7433. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7434. 'accept-charset' => $encoding
  7435. ),
  7436. 'div' => array('style' => 'display:none;'),
  7437. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7438. '/div'
  7439. );
  7440. $this->assertTags($result, $expected);
  7441. $result = $this->Form->create('Contact', array(
  7442. 'type' => 'post',
  7443. 'url' => array(
  7444. 'controller' => 'controller',
  7445. 'action' => 'action',
  7446. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7447. )
  7448. ));
  7449. $expected = array(
  7450. 'form' => array(
  7451. 'id' => 'ContactAddForm',
  7452. 'method' => 'post',
  7453. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7454. 'accept-charset' => $encoding
  7455. ),
  7456. 'div' => array('style' => 'display:none;'),
  7457. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7458. '/div'
  7459. );
  7460. $this->assertTags($result, $expected);
  7461. }
  7462. /**
  7463. * test that create() doesn't cause errors by multiple id's being in the primary key
  7464. * as could happen with multiple select or checkboxes.
  7465. *
  7466. * @return void
  7467. */
  7468. public function testCreateWithMultipleIdInData() {
  7469. $encoding = strtolower(Configure::read('App.encoding'));
  7470. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7471. $result = $this->Form->create('Contact');
  7472. $expected = array(
  7473. 'form' => array(
  7474. 'id' => 'ContactAddForm',
  7475. 'method' => 'post',
  7476. 'action' => '/contacts/add',
  7477. 'accept-charset' => $encoding
  7478. ),
  7479. 'div' => array('style' => 'display:none;'),
  7480. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7481. '/div'
  7482. );
  7483. $this->assertTags($result, $expected);
  7484. }
  7485. /**
  7486. * test that create() doesn't add in extra passed params.
  7487. *
  7488. * @return void
  7489. */
  7490. public function testCreatePassedArgs() {
  7491. $encoding = strtolower(Configure::read('App.encoding'));
  7492. $this->Form->request->data['Contact']['id'] = 1;
  7493. $result = $this->Form->create('Contact', array(
  7494. 'type' => 'post',
  7495. 'escape' => false,
  7496. 'url' => array(
  7497. 'action' => 'edit',
  7498. 'myparam'
  7499. )
  7500. ));
  7501. $expected = array(
  7502. 'form' => array(
  7503. 'id' => 'ContactAddForm',
  7504. 'method' => 'post',
  7505. 'action' => '/contacts/edit/myparam',
  7506. 'accept-charset' => $encoding
  7507. ),
  7508. 'div' => array('style' => 'display:none;'),
  7509. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7510. '/div'
  7511. );
  7512. $this->assertTags($result, $expected);
  7513. }
  7514. /**
  7515. * test creating a get form, and get form inputs.
  7516. *
  7517. * @return void
  7518. */
  7519. public function testGetFormCreate() {
  7520. $encoding = strtolower(Configure::read('App.encoding'));
  7521. $result = $this->Form->create('Contact', array('type' => 'get'));
  7522. $this->assertTags($result, array('form' => array(
  7523. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7524. 'accept-charset' => $encoding
  7525. )));
  7526. $result = $this->Form->text('Contact.name');
  7527. $this->assertTags($result, array('input' => array(
  7528. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7529. )));
  7530. $result = $this->Form->password('password');
  7531. $this->assertTags($result, array('input' => array(
  7532. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7533. )));
  7534. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7535. $result = $this->Form->text('user_form');
  7536. $this->assertTags($result, array('input' => array(
  7537. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7538. )));
  7539. }
  7540. /**
  7541. * test get form, and inputs when the model param is false
  7542. *
  7543. * @return void
  7544. */
  7545. public function testGetFormWithFalseModel() {
  7546. $encoding = strtolower(Configure::read('App.encoding'));
  7547. $this->Form->request['controller'] = 'contact_test';
  7548. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7549. $expected = array('form' => array(
  7550. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7551. 'accept-charset' => $encoding
  7552. ));
  7553. $this->assertTags($result, $expected);
  7554. $result = $this->Form->text('reason');
  7555. $expected = array(
  7556. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7557. );
  7558. $this->assertTags($result, $expected);
  7559. }
  7560. /**
  7561. * test that datetime() works with GET style forms.
  7562. *
  7563. * @return void
  7564. */
  7565. public function testDateTimeWithGetForms() {
  7566. extract($this->dateRegex);
  7567. $this->Form->create('Contact', array('type' => 'get'));
  7568. $result = $this->Form->datetime('created');
  7569. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7570. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7571. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7572. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7573. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7574. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7575. }
  7576. /**
  7577. * testEditFormWithData method
  7578. *
  7579. * test auto populating form elements from submitted data.
  7580. *
  7581. * @return void
  7582. */
  7583. public function testEditFormWithData() {
  7584. $this->Form->request->data = array('Person' => array(
  7585. 'id' => 1,
  7586. 'first_name' => 'Nate',
  7587. 'last_name' => 'Abele',
  7588. 'email' => 'nate@example.com'
  7589. ));
  7590. $this->Form->request->addParams(array(
  7591. 'models' => array('Person'),
  7592. 'controller' => 'people',
  7593. 'action' => 'add'
  7594. ));
  7595. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7596. $this->Form->create();
  7597. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7598. $expected = array(
  7599. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7600. 'select' => array(
  7601. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7602. ),
  7603. array('option' => array('value' => 1)), 'Nate', '/option',
  7604. array('option' => array('value' => 2)), 'Garrett', '/option',
  7605. array('option' => array('value' => 3)), 'Larry', '/option',
  7606. '/select'
  7607. );
  7608. $this->assertTags($result, $expected);
  7609. }
  7610. /**
  7611. * Test that required fields are created for various types of validation.
  7612. *
  7613. * @return void
  7614. */
  7615. public function testFormInputRequiredDetection() {
  7616. $this->Form->create('Contact');
  7617. $result = $this->Form->input('Contact.non_existing');
  7618. $expected = array(
  7619. 'div' => array('class' => 'input text'),
  7620. 'label' => array('for' => 'ContactNonExisting'),
  7621. 'Non Existing',
  7622. '/label',
  7623. 'input' => array(
  7624. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7625. 'id' => 'ContactNonExisting'
  7626. ),
  7627. '/div'
  7628. );
  7629. $this->assertTags($result, $expected);
  7630. $result = $this->Form->input('Contact.imrequired');
  7631. $expected = array(
  7632. 'div' => array('class' => 'input text required'),
  7633. 'label' => array('for' => 'ContactImrequired'),
  7634. 'Imrequired',
  7635. '/label',
  7636. 'input' => array(
  7637. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7638. 'id' => 'ContactImrequired',
  7639. 'required' => 'required'
  7640. ),
  7641. '/div'
  7642. );
  7643. $this->assertTags($result, $expected);
  7644. $result = $this->Form->input('Contact.imalsorequired');
  7645. $expected = array(
  7646. 'div' => array('class' => 'input text required'),
  7647. 'label' => array('for' => 'ContactImalsorequired'),
  7648. 'Imalsorequired',
  7649. '/label',
  7650. 'input' => array(
  7651. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7652. 'id' => 'ContactImalsorequired',
  7653. 'required' => 'required'
  7654. ),
  7655. '/div'
  7656. );
  7657. $this->assertTags($result, $expected);
  7658. $result = $this->Form->input('Contact.imrequiredtoo');
  7659. $expected = array(
  7660. 'div' => array('class' => 'input text required'),
  7661. 'label' => array('for' => 'ContactImrequiredtoo'),
  7662. 'Imrequiredtoo',
  7663. '/label',
  7664. 'input' => array(
  7665. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7666. 'id' => 'ContactImrequiredtoo',
  7667. 'required' => 'required'
  7668. ),
  7669. '/div'
  7670. );
  7671. $this->assertTags($result, $expected);
  7672. $result = $this->Form->input('Contact.required_one');
  7673. $expected = array(
  7674. 'div' => array('class' => 'input text required'),
  7675. 'label' => array('for' => 'ContactRequiredOne'),
  7676. 'Required One',
  7677. '/label',
  7678. 'input' => array(
  7679. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7680. 'id' => 'ContactRequiredOne',
  7681. 'required' => 'required'
  7682. ),
  7683. '/div'
  7684. );
  7685. $this->assertTags($result, $expected);
  7686. $result = $this->Form->input('Contact.string_required');
  7687. $expected = array(
  7688. 'div' => array('class' => 'input text required'),
  7689. 'label' => array('for' => 'ContactStringRequired'),
  7690. 'String Required',
  7691. '/label',
  7692. 'input' => array(
  7693. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7694. 'id' => 'ContactStringRequired',
  7695. 'required' => 'required'
  7696. ),
  7697. '/div'
  7698. );
  7699. $this->assertTags($result, $expected);
  7700. $result = $this->Form->input('Contact.imnotrequired');
  7701. $expected = array(
  7702. 'div' => array('class' => 'input text'),
  7703. 'label' => array('for' => 'ContactImnotrequired'),
  7704. 'Imnotrequired',
  7705. '/label',
  7706. 'input' => array(
  7707. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7708. 'id' => 'ContactImnotrequired'
  7709. ),
  7710. '/div'
  7711. );
  7712. $this->assertTags($result, $expected);
  7713. $result = $this->Form->input('Contact.imalsonotrequired');
  7714. $expected = array(
  7715. 'div' => array('class' => 'input text'),
  7716. 'label' => array('for' => 'ContactImalsonotrequired'),
  7717. 'Imalsonotrequired',
  7718. '/label',
  7719. 'input' => array(
  7720. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7721. 'id' => 'ContactImalsonotrequired'
  7722. ),
  7723. '/div'
  7724. );
  7725. $this->assertTags($result, $expected);
  7726. $result = $this->Form->input('Contact.imalsonotrequired2');
  7727. $expected = array(
  7728. 'div' => array('class' => 'input text'),
  7729. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7730. 'Imalsonotrequired2',
  7731. '/label',
  7732. 'input' => array(
  7733. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7734. 'id' => 'ContactImalsonotrequired2'
  7735. ),
  7736. '/div'
  7737. );
  7738. $this->assertTags($result, $expected);
  7739. $result = $this->Form->input('Contact.imnotrequiredeither');
  7740. $expected = array(
  7741. 'div' => array('class' => 'input text'),
  7742. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7743. 'Imnotrequiredeither',
  7744. '/label',
  7745. 'input' => array(
  7746. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7747. 'id' => 'ContactImnotrequiredeither'
  7748. ),
  7749. '/div'
  7750. );
  7751. $this->assertTags($result, $expected);
  7752. $result = $this->Form->input('Contact.iamrequiredalways');
  7753. $expected = array(
  7754. 'div' => array('class' => 'input text required'),
  7755. 'label' => array('for' => 'ContactIamrequiredalways'),
  7756. 'Iamrequiredalways',
  7757. '/label',
  7758. 'input' => array(
  7759. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7760. 'id' => 'ContactIamrequiredalways',
  7761. 'required' => 'required'
  7762. ),
  7763. '/div'
  7764. );
  7765. $this->assertTags($result, $expected);
  7766. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7767. $expected = array(
  7768. 'div' => array('class' => 'input checkbox required'),
  7769. array('input' => array(
  7770. 'type' => 'hidden',
  7771. 'name' => 'data[Contact][boolean_field]',
  7772. 'id' => 'ContactBooleanField_',
  7773. 'value' => '0'
  7774. )),
  7775. array('input' => array(
  7776. 'type' => 'checkbox',
  7777. 'name' => 'data[Contact][boolean_field]',
  7778. 'value' => '1',
  7779. 'id' => 'ContactBooleanField'
  7780. )),
  7781. 'label' => array('for' => 'ContactBooleanField'),
  7782. 'Boolean Field',
  7783. '/label',
  7784. '/div'
  7785. );
  7786. $this->assertTags($result, $expected);
  7787. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7788. $expected = array(
  7789. 'div' => array('class' => 'input checkbox required'),
  7790. array('input' => array(
  7791. 'type' => 'hidden',
  7792. 'name' => 'data[Contact][boolean_field]',
  7793. 'id' => 'ContactBooleanField_',
  7794. 'value' => '0'
  7795. )),
  7796. array('input' => array(
  7797. 'type' => 'checkbox',
  7798. 'name' => 'data[Contact][boolean_field]',
  7799. 'value' => '1',
  7800. 'id' => 'ContactBooleanField',
  7801. 'required' => 'required'
  7802. )),
  7803. 'label' => array('for' => 'ContactBooleanField'),
  7804. 'Boolean Field',
  7805. '/label',
  7806. '/div'
  7807. );
  7808. $this->assertTags($result, $expected);
  7809. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7810. $expected = array(
  7811. 'div' => array('class' => 'input file required'),
  7812. 'label' => array('for' => 'ContactIamrequiredalways'),
  7813. 'Iamrequiredalways',
  7814. '/label',
  7815. 'input' => array(
  7816. 'type' => 'file',
  7817. 'name' => 'data[Contact][iamrequiredalways]',
  7818. 'id' => 'ContactIamrequiredalways',
  7819. 'required' => 'required'
  7820. ),
  7821. '/div'
  7822. );
  7823. $this->assertTags($result, $expected);
  7824. }
  7825. /**
  7826. * Test that required fields are created when only using ModelValidator::add().
  7827. *
  7828. * @return void
  7829. */
  7830. public function testFormInputRequiredDetectionModelValidator() {
  7831. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7832. $this->Form->create('ContactTag');
  7833. $result = $this->Form->input('ContactTag.iwillberequired');
  7834. $expected = array(
  7835. 'div' => array('class' => 'input text required'),
  7836. 'label' => array('for' => 'ContactTagIwillberequired'),
  7837. 'Iwillberequired',
  7838. '/label',
  7839. 'input' => array(
  7840. 'name' => 'data[ContactTag][iwillberequired]',
  7841. 'type' => 'text',
  7842. 'id' => 'ContactTagIwillberequired',
  7843. 'required' => 'required'
  7844. ),
  7845. '/div'
  7846. );
  7847. $this->assertTags($result, $expected);
  7848. }
  7849. /**
  7850. * testFormMagicInput method
  7851. *
  7852. * @return void
  7853. */
  7854. public function testFormMagicInput() {
  7855. $encoding = strtolower(Configure::read('App.encoding'));
  7856. $result = $this->Form->create('Contact');
  7857. $expected = array(
  7858. 'form' => array(
  7859. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7860. 'accept-charset' => $encoding
  7861. ),
  7862. 'div' => array('style' => 'display:none;'),
  7863. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7864. '/div'
  7865. );
  7866. $this->assertTags($result, $expected);
  7867. $result = $this->Form->input('name');
  7868. $expected = array(
  7869. 'div' => array('class' => 'input text'),
  7870. 'label' => array('for' => 'ContactName'),
  7871. 'Name',
  7872. '/label',
  7873. 'input' => array(
  7874. 'type' => 'text', 'name' => 'data[Contact][name]',
  7875. 'id' => 'ContactName', 'maxlength' => '255'
  7876. ),
  7877. '/div'
  7878. );
  7879. $this->assertTags($result, $expected);
  7880. $result = $this->Form->input('non_existing_field_in_contact_model');
  7881. $expected = array(
  7882. 'div' => array('class' => 'input text'),
  7883. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7884. 'Non Existing Field In Contact Model',
  7885. '/label',
  7886. 'input' => array(
  7887. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7888. 'id' => 'ContactNonExistingFieldInContactModel'
  7889. ),
  7890. '/div'
  7891. );
  7892. $this->assertTags($result, $expected);
  7893. $result = $this->Form->input('Address.street');
  7894. $expected = array(
  7895. 'div' => array('class' => 'input text'),
  7896. 'label' => array('for' => 'AddressStreet'),
  7897. 'Street',
  7898. '/label',
  7899. 'input' => array(
  7900. 'type' => 'text', 'name' => 'data[Address][street]',
  7901. 'id' => 'AddressStreet'
  7902. ),
  7903. '/div'
  7904. );
  7905. $this->assertTags($result, $expected);
  7906. $result = $this->Form->input('Address.non_existing_field_in_model');
  7907. $expected = array(
  7908. 'div' => array('class' => 'input text'),
  7909. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7910. 'Non Existing Field In Model',
  7911. '/label',
  7912. 'input' => array(
  7913. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7914. 'id' => 'AddressNonExistingFieldInModel'
  7915. ),
  7916. '/div'
  7917. );
  7918. $this->assertTags($result, $expected);
  7919. $result = $this->Form->input('name', array('div' => false));
  7920. $expected = array(
  7921. 'label' => array('for' => 'ContactName'),
  7922. 'Name',
  7923. '/label',
  7924. 'input' => array(
  7925. 'type' => 'text', 'name' => 'data[Contact][name]',
  7926. 'id' => 'ContactName', 'maxlength' => '255'
  7927. )
  7928. );
  7929. $this->assertTags($result, $expected);
  7930. extract($this->dateRegex);
  7931. $now = strtotime('now');
  7932. $result = $this->Form->input('Contact.published', array('div' => false));
  7933. $expected = array(
  7934. 'label' => array('for' => 'ContactPublishedMonth'),
  7935. 'Published',
  7936. '/label',
  7937. array('select' => array(
  7938. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7939. )),
  7940. $monthsRegex,
  7941. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7942. date('F', $now),
  7943. '/option',
  7944. '*/select',
  7945. '-',
  7946. array('select' => array(
  7947. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7948. )),
  7949. $daysRegex,
  7950. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7951. date('j', $now),
  7952. '/option',
  7953. '*/select',
  7954. '-',
  7955. array('select' => array(
  7956. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7957. )),
  7958. $yearsRegex,
  7959. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7960. date('Y', $now),
  7961. '*/select'
  7962. );
  7963. $this->assertTags($result, $expected);
  7964. $result = $this->Form->input('Contact.updated', array('div' => false));
  7965. $expected = array(
  7966. 'label' => array('for' => 'ContactUpdatedMonth'),
  7967. 'Updated',
  7968. '/label',
  7969. array('select' => array(
  7970. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7971. )),
  7972. $monthsRegex,
  7973. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7974. date('F', $now),
  7975. '/option',
  7976. '*/select',
  7977. '-',
  7978. array('select' => array(
  7979. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7980. )),
  7981. $daysRegex,
  7982. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7983. date('j', $now),
  7984. '/option',
  7985. '*/select',
  7986. '-',
  7987. array('select' => array(
  7988. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7989. )),
  7990. $yearsRegex,
  7991. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7992. date('Y', $now),
  7993. '*/select'
  7994. );
  7995. $this->assertTags($result, $expected);
  7996. $result = $this->Form->input('UserForm.stuff');
  7997. $expected = array(
  7998. 'div' => array('class' => 'input text'),
  7999. 'label' => array('for' => 'UserFormStuff'),
  8000. 'Stuff',
  8001. '/label',
  8002. 'input' => array(
  8003. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  8004. 'id' => 'UserFormStuff', 'maxlength' => 10
  8005. ),
  8006. '/div'
  8007. );
  8008. $this->assertTags($result, $expected);
  8009. }
  8010. /**
  8011. * testForMagicInputNonExistingNorValidated method
  8012. *
  8013. * @return void
  8014. */
  8015. public function testForMagicInputNonExistingNorValidated() {
  8016. $encoding = strtolower(Configure::read('App.encoding'));
  8017. $result = $this->Form->create('Contact');
  8018. $expected = array(
  8019. 'form' => array(
  8020. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8021. 'accept-charset' => $encoding
  8022. ),
  8023. 'div' => array('style' => 'display:none;'),
  8024. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8025. '/div'
  8026. );
  8027. $this->assertTags($result, $expected);
  8028. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8029. $expected = array(
  8030. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8031. 'Non Existing Nor Validated',
  8032. '/label',
  8033. 'input' => array(
  8034. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8035. 'id' => 'ContactNonExistingNorValidated'
  8036. )
  8037. );
  8038. $this->assertTags($result, $expected);
  8039. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8040. 'div' => false, 'value' => 'my value'
  8041. ));
  8042. $expected = array(
  8043. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8044. 'Non Existing Nor Validated',
  8045. '/label',
  8046. 'input' => array(
  8047. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8048. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8049. )
  8050. );
  8051. $this->assertTags($result, $expected);
  8052. $this->Form->request->data = array(
  8053. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8054. ));
  8055. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8056. $expected = array(
  8057. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8058. 'Non Existing Nor Validated',
  8059. '/label',
  8060. 'input' => array(
  8061. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8062. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8063. )
  8064. );
  8065. $this->assertTags($result, $expected);
  8066. }
  8067. /**
  8068. * testFormMagicInputLabel method
  8069. *
  8070. * @return void
  8071. */
  8072. public function testFormMagicInputLabel() {
  8073. $encoding = strtolower(Configure::read('App.encoding'));
  8074. $result = $this->Form->create('Contact');
  8075. $expected = array(
  8076. 'form' => array(
  8077. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8078. 'accept-charset' => $encoding
  8079. ),
  8080. 'div' => array('style' => 'display:none;'),
  8081. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8082. '/div'
  8083. );
  8084. $this->assertTags($result, $expected);
  8085. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8086. $this->assertTags($result, array('input' => array(
  8087. 'name' => 'data[Contact][name]', 'type' => 'text',
  8088. 'id' => 'ContactName', 'maxlength' => '255')
  8089. ));
  8090. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8091. $expected = array(
  8092. 'label' => array('for' => 'ContactName'),
  8093. 'My label',
  8094. '/label',
  8095. 'input' => array(
  8096. 'type' => 'text', 'name' => 'data[Contact][name]',
  8097. 'id' => 'ContactName', 'maxlength' => '255'
  8098. )
  8099. );
  8100. $this->assertTags($result, $expected);
  8101. $result = $this->Form->input('Contact.name', array(
  8102. 'div' => false, 'label' => array('class' => 'mandatory')
  8103. ));
  8104. $expected = array(
  8105. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8106. 'Name',
  8107. '/label',
  8108. 'input' => array(
  8109. 'type' => 'text', 'name' => 'data[Contact][name]',
  8110. 'id' => 'ContactName', 'maxlength' => '255'
  8111. )
  8112. );
  8113. $this->assertTags($result, $expected);
  8114. $result = $this->Form->input('Contact.name', array(
  8115. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8116. ));
  8117. $expected = array(
  8118. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8119. 'My label',
  8120. '/label',
  8121. 'input' => array(
  8122. 'type' => 'text', 'name' => 'data[Contact][name]',
  8123. 'id' => 'ContactName', 'maxlength' => '255'
  8124. )
  8125. );
  8126. $this->assertTags($result, $expected);
  8127. $result = $this->Form->input('Contact.name', array(
  8128. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8129. ));
  8130. $expected = array(
  8131. 'label' => array('for' => 'my_id'),
  8132. 'Name',
  8133. '/label',
  8134. 'input' => array(
  8135. 'type' => 'text', 'name' => 'data[Contact][name]',
  8136. 'id' => 'my_id', 'maxlength' => '255'
  8137. )
  8138. );
  8139. $this->assertTags($result, $expected);
  8140. $result = $this->Form->input('1.id');
  8141. $this->assertTags($result, array('input' => array(
  8142. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8143. 'id' => 'Contact1Id'
  8144. )));
  8145. $result = $this->Form->input("1.name");
  8146. $expected = array(
  8147. 'div' => array('class' => 'input text'),
  8148. 'label' => array('for' => 'Contact1Name'),
  8149. 'Name',
  8150. '/label',
  8151. 'input' => array(
  8152. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8153. 'id' => 'Contact1Name', 'maxlength' => '255'
  8154. ),
  8155. '/div'
  8156. );
  8157. $this->assertTags($result, $expected);
  8158. $result = $this->Form->input('Contact.1.id');
  8159. $this->assertTags($result, array(
  8160. 'input' => array(
  8161. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8162. 'id' => 'Contact1Id'
  8163. )
  8164. ));
  8165. $result = $this->Form->input("Model.1.name");
  8166. $expected = array(
  8167. 'div' => array('class' => 'input text'),
  8168. 'label' => array('for' => 'Model1Name'),
  8169. 'Name',
  8170. '/label',
  8171. 'input' => array(
  8172. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8173. 'id' => 'Model1Name'
  8174. ),
  8175. '/div'
  8176. );
  8177. $this->assertTags($result, $expected);
  8178. }
  8179. /**
  8180. * testFormEnd method
  8181. *
  8182. * @return void
  8183. */
  8184. public function testFormEnd() {
  8185. $this->assertEquals('</form>', $this->Form->end());
  8186. $result = $this->Form->end('');
  8187. $expected = array(
  8188. 'div' => array('class' => 'submit'),
  8189. 'input' => array('type' => 'submit', 'value' => ''),
  8190. '/div',
  8191. '/form'
  8192. );
  8193. $this->assertTags($result, $expected);
  8194. $result = $this->Form->end(array('label' => ''));
  8195. $expected = array(
  8196. 'div' => array('class' => 'submit'),
  8197. 'input' => array('type' => 'submit', 'value' => ''),
  8198. '/div',
  8199. '/form'
  8200. );
  8201. $this->assertTags($result, $expected);
  8202. $result = $this->Form->end('save');
  8203. $expected = array(
  8204. 'div' => array('class' => 'submit'),
  8205. 'input' => array('type' => 'submit', 'value' => 'save'),
  8206. '/div',
  8207. '/form'
  8208. );
  8209. $this->assertTags($result, $expected);
  8210. $result = $this->Form->end(array('label' => 'save'));
  8211. $expected = array(
  8212. 'div' => array('class' => 'submit'),
  8213. 'input' => array('type' => 'submit', 'value' => 'save'),
  8214. '/div',
  8215. '/form'
  8216. );
  8217. $this->assertTags($result, $expected);
  8218. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8219. $expected = array(
  8220. 'div' => array('class' => 'submit'),
  8221. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8222. '/div',
  8223. '/form'
  8224. );
  8225. $this->assertTags($result, $expected);
  8226. $result = $this->Form->end(array('name' => 'Whatever'));
  8227. $expected = array(
  8228. 'div' => array('class' => 'submit'),
  8229. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8230. '/div',
  8231. '/form'
  8232. );
  8233. $this->assertTags($result, $expected);
  8234. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8235. $expected = array(
  8236. 'div' => array('class' => 'good'),
  8237. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8238. '/div',
  8239. '/form'
  8240. );
  8241. $this->assertTags($result, $expected);
  8242. $result = $this->Form->end(array(
  8243. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8244. ));
  8245. $expected = array(
  8246. 'div' => array('class' => 'good'),
  8247. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8248. '/div',
  8249. '/form'
  8250. );
  8251. $this->assertTags($result, $expected);
  8252. }
  8253. /**
  8254. * testMultipleFormWithIdFields method
  8255. *
  8256. * @return void
  8257. */
  8258. public function testMultipleFormWithIdFields() {
  8259. $this->Form->create('UserForm');
  8260. $result = $this->Form->input('id');
  8261. $this->assertTags($result, array('input' => array(
  8262. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8263. )));
  8264. $result = $this->Form->input('ValidateItem.id');
  8265. $this->assertTags($result, array('input' => array(
  8266. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8267. 'id' => 'ValidateItemId'
  8268. )));
  8269. $result = $this->Form->input('ValidateUser.id');
  8270. $this->assertTags($result, array('input' => array(
  8271. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8272. 'id' => 'ValidateUserId'
  8273. )));
  8274. }
  8275. /**
  8276. * testDbLessModel method
  8277. *
  8278. * @return void
  8279. */
  8280. public function testDbLessModel() {
  8281. $this->Form->create('TestMail');
  8282. $result = $this->Form->input('name');
  8283. $expected = array(
  8284. 'div' => array('class' => 'input text'),
  8285. 'label' => array('for' => 'TestMailName'),
  8286. 'Name',
  8287. '/label',
  8288. 'input' => array(
  8289. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8290. 'id' => 'TestMailName'
  8291. ),
  8292. '/div'
  8293. );
  8294. $this->assertTags($result, $expected);
  8295. ClassRegistry::init('TestMail');
  8296. $this->Form->create('TestMail');
  8297. $result = $this->Form->input('name');
  8298. $expected = array(
  8299. 'div' => array('class' => 'input text'),
  8300. 'label' => array('for' => 'TestMailName'),
  8301. 'Name',
  8302. '/label',
  8303. 'input' => array(
  8304. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8305. 'id' => 'TestMailName'
  8306. ),
  8307. '/div'
  8308. );
  8309. $this->assertTags($result, $expected);
  8310. }
  8311. /**
  8312. * testBrokenness method
  8313. *
  8314. * @return void
  8315. */
  8316. public function testBrokenness() {
  8317. /*
  8318. * #4 This test has two parents and four children. By default (as of r7117) both
  8319. * parents are show but the first parent is missing a child. This is the inconsistency
  8320. * in the default behaviour - one parent has all children, the other does not - dependent
  8321. * on the data values.
  8322. */
  8323. $result = $this->Form->select('Model.field', array(
  8324. 'Fred' => array(
  8325. 'freds_son_1' => 'Fred',
  8326. 'freds_son_2' => 'Freddie'
  8327. ),
  8328. 'Bert' => array(
  8329. 'berts_son_1' => 'Albert',
  8330. 'berts_son_2' => 'Bertie')
  8331. ),
  8332. array('showParents' => true, 'empty' => false)
  8333. );
  8334. $expected = array(
  8335. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8336. array('optgroup' => array('label' => 'Fred')),
  8337. array('option' => array('value' => 'freds_son_1')),
  8338. 'Fred',
  8339. '/option',
  8340. array('option' => array('value' => 'freds_son_2')),
  8341. 'Freddie',
  8342. '/option',
  8343. '/optgroup',
  8344. array('optgroup' => array('label' => 'Bert')),
  8345. array('option' => array('value' => 'berts_son_1')),
  8346. 'Albert',
  8347. '/option',
  8348. array('option' => array('value' => 'berts_son_2')),
  8349. 'Bertie',
  8350. '/option',
  8351. '/optgroup',
  8352. '/select'
  8353. );
  8354. $this->assertTags($result, $expected);
  8355. /*
  8356. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8357. * changed, so we should expect the same select list data, just with a different name
  8358. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8359. * This is where data corruption can occur, because when a select value is missing from
  8360. * a list a form will substitute the first value in the list - without the user knowing.
  8361. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8362. * affect the availability of 3 => 'Three' as a valid option.
  8363. */
  8364. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8365. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8366. ));
  8367. $result = $this->Form->select(
  8368. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8369. );
  8370. $expected = array(
  8371. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8372. array('option' => array('value' => 1)),
  8373. 'One',
  8374. '/option',
  8375. array('option' => array('value' => 2)),
  8376. 'Two',
  8377. '/option',
  8378. array('optgroup' => array('label' => 'Three')),
  8379. array('option' => array('value' => 3)),
  8380. 'Three',
  8381. '/option',
  8382. array('option' => array('value' => 4)),
  8383. 'Four',
  8384. '/option',
  8385. array('option' => array('value' => 5)),
  8386. 'Five',
  8387. '/option',
  8388. '/optgroup',
  8389. '/select'
  8390. );
  8391. $this->assertTags($result, $expected);
  8392. }
  8393. /**
  8394. * Test the generation of fields for a multi record form.
  8395. *
  8396. * @return void
  8397. */
  8398. public function testMultiRecordForm() {
  8399. $this->Form->create('ValidateProfile');
  8400. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8401. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8402. $expected = array(
  8403. 'div' => array('class' => 'input textarea'),
  8404. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8405. 'Name',
  8406. '/label',
  8407. 'textarea' => array(
  8408. 'id' => 'ValidateProfile1ValidateItem2Name',
  8409. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8410. 'cols' => 30,
  8411. 'rows' => 6
  8412. ),
  8413. 'Value',
  8414. '/textarea',
  8415. '/div'
  8416. );
  8417. $this->assertTags($result, $expected);
  8418. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8419. $expected = array(
  8420. 'div' => array('class' => 'input date'),
  8421. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8422. 'Created',
  8423. '/label',
  8424. array('select' => array(
  8425. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8426. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8427. )
  8428. ),
  8429. array('option' => array('value' => '')), '/option',
  8430. $this->dateRegex['monthsRegex'],
  8431. '/select', '-',
  8432. array('select' => array(
  8433. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8434. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8435. )
  8436. ),
  8437. array('option' => array('value' => '')), '/option',
  8438. $this->dateRegex['daysRegex'],
  8439. '/select', '-',
  8440. array('select' => array(
  8441. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8442. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8443. )
  8444. ),
  8445. array('option' => array('value' => '')), '/option',
  8446. $this->dateRegex['yearsRegex'],
  8447. '/select',
  8448. '/div'
  8449. );
  8450. $this->assertTags($result, $expected);
  8451. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8452. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8453. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8454. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8455. $expected = array(
  8456. 'div' => array('class' => 'input select error'),
  8457. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8458. 'Profile',
  8459. '/label',
  8460. 'select' => array(
  8461. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8462. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8463. 'class' => 'form-error'
  8464. ),
  8465. '/select',
  8466. array('div' => array('class' => 'error-message')),
  8467. 'Error',
  8468. '/div',
  8469. '/div'
  8470. );
  8471. $this->assertTags($result, $expected);
  8472. }
  8473. /**
  8474. * test the correct display of multi-record form validation errors.
  8475. *
  8476. * @return void
  8477. */
  8478. public function testMultiRecordFormValidationErrors() {
  8479. $this->Form->create('ValidateProfile');
  8480. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8481. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8482. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8483. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8484. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8485. $result = $this->Form->error('ValidateProfile.2.city');
  8486. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8487. $result = $this->Form->error('2.city');
  8488. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8489. }
  8490. /**
  8491. * test the correct display of multi-record form validation errors.
  8492. *
  8493. * @return void
  8494. */
  8495. public function testSaveManyRecordFormValidationErrors() {
  8496. $this->Form->create('ValidateUser');
  8497. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8498. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8499. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8500. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8501. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8502. $result = $this->Form->error('ValidateUser.0.city');
  8503. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8504. }
  8505. /**
  8506. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8507. *
  8508. * @return void
  8509. */
  8510. public function testInputTemplate() {
  8511. $result = $this->Form->input('Contact.email', array(
  8512. 'type' => 'text', 'format' => array('input')
  8513. ));
  8514. $expected = array(
  8515. 'div' => array('class' => 'input text'),
  8516. 'input' => array(
  8517. 'type' => 'text', 'name' => 'data[Contact][email]',
  8518. 'id' => 'ContactEmail'
  8519. ),
  8520. '/div'
  8521. );
  8522. $this->assertTags($result, $expected);
  8523. $result = $this->Form->input('Contact.email', array(
  8524. 'type' => 'text', 'format' => array('input', 'label'),
  8525. 'label' => '<em>Email (required)</em>'
  8526. ));
  8527. $expected = array(
  8528. 'div' => array('class' => 'input text'),
  8529. array('input' => array(
  8530. 'type' => 'text', 'name' => 'data[Contact][email]',
  8531. 'id' => 'ContactEmail'
  8532. )),
  8533. 'label' => array('for' => 'ContactEmail'),
  8534. 'em' => array(),
  8535. 'Email (required)',
  8536. '/em',
  8537. '/label',
  8538. '/div'
  8539. );
  8540. $this->assertTags($result, $expected);
  8541. $result = $this->Form->input('Contact.email', array(
  8542. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8543. 'between' => '<div>Something in the middle</div>',
  8544. 'after' => '<span>Some text at the end</span>'
  8545. ));
  8546. $expected = array(
  8547. 'div' => array('class' => 'input text'),
  8548. array('input' => array(
  8549. 'type' => 'text', 'name' => 'data[Contact][email]',
  8550. 'id' => 'ContactEmail'
  8551. )),
  8552. array('div' => array()),
  8553. 'Something in the middle',
  8554. '/div',
  8555. 'label' => array('for' => 'ContactEmail'),
  8556. 'Email',
  8557. '/label',
  8558. 'span' => array(),
  8559. 'Some text at the end',
  8560. '/span',
  8561. '/div'
  8562. );
  8563. $this->assertTags($result, $expected);
  8564. $result = $this->Form->input('Contact.method', array(
  8565. 'type' => 'radio',
  8566. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8567. 'between' => 'I am between',
  8568. ));
  8569. $expected = array(
  8570. 'div' => array('class' => 'input radio'),
  8571. 'fieldset' => array(),
  8572. 'legend' => array(),
  8573. 'Method',
  8574. '/legend',
  8575. 'I am between',
  8576. 'input' => array(
  8577. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8578. 'value' => '', 'id' => 'ContactMethod_'
  8579. ),
  8580. array('input' => array(
  8581. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8582. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8583. )),
  8584. array('label' => array('for' => 'ContactMethodEmail')),
  8585. 'Email',
  8586. '/label',
  8587. array('input' => array(
  8588. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8589. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8590. )),
  8591. array('label' => array('for' => 'ContactMethodPigeon')),
  8592. 'Pigeon',
  8593. '/label',
  8594. '/fieldset',
  8595. '/div',
  8596. );
  8597. $this->assertTags($result, $expected);
  8598. }
  8599. /**
  8600. * test that some html5 inputs + FormHelper::__call() work
  8601. *
  8602. * @return void
  8603. */
  8604. public function testHtml5Inputs() {
  8605. $result = $this->Form->email('User.email');
  8606. $expected = array(
  8607. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8608. );
  8609. $this->assertTags($result, $expected);
  8610. $result = $this->Form->search('User.query');
  8611. $expected = array(
  8612. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8613. );
  8614. $this->assertTags($result, $expected);
  8615. $result = $this->Form->search('User.query', array('value' => 'test'));
  8616. $expected = array(
  8617. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8618. );
  8619. $this->assertTags($result, $expected);
  8620. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8621. $expected = array(
  8622. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8623. );
  8624. $this->assertTags($result, $expected);
  8625. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8626. $expected = array(
  8627. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8628. );
  8629. $this->assertTags($result, $expected);
  8630. }
  8631. /**
  8632. *
  8633. * @expectedException CakeException
  8634. * @return void
  8635. */
  8636. public function testHtml5InputException() {
  8637. $this->Form->email();
  8638. }
  8639. /**
  8640. * Tests that a model can be loaded from the model names passed in the request object
  8641. *
  8642. * @return void
  8643. */
  8644. public function testIntrospectModelFromRequest() {
  8645. $this->loadFixtures('Post');
  8646. App::build(array(
  8647. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8648. ));
  8649. CakePlugin::load('TestPlugin');
  8650. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8651. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8652. $this->Form->create('TestPluginPost');
  8653. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8654. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8655. CakePlugin::unload();
  8656. App::build();
  8657. }
  8658. /**
  8659. * Tests that it is possible to set the validation errors directly in the helper for a field
  8660. *
  8661. * @return void
  8662. */
  8663. public function testCustomValidationErrors() {
  8664. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8665. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8666. $this->assertEquals('Badness!', $result);
  8667. }
  8668. /**
  8669. * Tests that the 'on' key validates as expected on create
  8670. *
  8671. * @return void
  8672. */
  8673. public function testRequiredOnCreate() {
  8674. $this->Form->create('Contact');
  8675. $result = $this->Form->input('Contact.imrequiredonupdate');
  8676. $expected = array(
  8677. 'div' => array('class' => 'input text'),
  8678. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8679. 'Imrequiredonupdate',
  8680. '/label',
  8681. 'input' => array(
  8682. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8683. 'id' => 'ContactImrequiredonupdate'
  8684. ),
  8685. '/div'
  8686. );
  8687. $this->assertTags($result, $expected);
  8688. $result = $this->Form->input('Contact.imrequiredoncreate');
  8689. $expected = array(
  8690. 'div' => array('class' => 'input text required'),
  8691. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8692. 'Imrequiredoncreate',
  8693. '/label',
  8694. 'input' => array(
  8695. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8696. 'id' => 'ContactImrequiredoncreate',
  8697. 'required' => 'required'
  8698. ),
  8699. '/div'
  8700. );
  8701. $this->assertTags($result, $expected);
  8702. $result = $this->Form->input('Contact.imrequiredonboth');
  8703. $expected = array(
  8704. 'div' => array('class' => 'input text required'),
  8705. 'label' => array('for' => 'ContactImrequiredonboth'),
  8706. 'Imrequiredonboth',
  8707. '/label',
  8708. 'input' => array(
  8709. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8710. 'id' => 'ContactImrequiredonboth',
  8711. 'required' => 'required'
  8712. ),
  8713. '/div'
  8714. );
  8715. $this->assertTags($result, $expected);
  8716. $this->Form->inputDefaults(array('required' => false));
  8717. $result = $this->Form->input('Contact.imrequired');
  8718. $expected = array(
  8719. 'div' => array('class' => 'input text'),
  8720. 'label' => array('for' => 'ContactImrequired'),
  8721. 'Imrequired',
  8722. '/label',
  8723. 'input' => array(
  8724. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8725. 'id' => 'ContactImrequired'
  8726. ),
  8727. '/div'
  8728. );
  8729. $this->assertTags($result, $expected);
  8730. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8731. $this->assertTags($result, $expected);
  8732. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8733. $expected = array(
  8734. 'div' => array('class' => 'input text required'),
  8735. 'label' => array('for' => 'ContactImrequired'),
  8736. 'Imrequired',
  8737. '/label',
  8738. 'input' => array(
  8739. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8740. 'id' => 'ContactImrequired'
  8741. ),
  8742. '/div'
  8743. );
  8744. $this->assertTags($result, $expected);
  8745. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8746. $this->assertTags($result, $expected);
  8747. }
  8748. /**
  8749. * Tests that the 'on' key validates as expected on update
  8750. *
  8751. * @return void
  8752. */
  8753. public function testRequiredOnUpdate() {
  8754. $this->Form->request->data['Contact']['id'] = 1;
  8755. $this->Form->create('Contact');
  8756. $result = $this->Form->input('Contact.imrequiredonupdate');
  8757. $expected = array(
  8758. 'div' => array('class' => 'input text required'),
  8759. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8760. 'Imrequiredonupdate',
  8761. '/label',
  8762. 'input' => array(
  8763. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8764. 'id' => 'ContactImrequiredonupdate',
  8765. 'required' => 'required'
  8766. ),
  8767. '/div'
  8768. );
  8769. $this->assertTags($result, $expected);
  8770. $result = $this->Form->input('Contact.imrequiredoncreate');
  8771. $expected = array(
  8772. 'div' => array('class' => 'input text'),
  8773. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8774. 'Imrequiredoncreate',
  8775. '/label',
  8776. 'input' => array(
  8777. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8778. 'id' => 'ContactImrequiredoncreate'
  8779. ),
  8780. '/div'
  8781. );
  8782. $this->assertTags($result, $expected);
  8783. $result = $this->Form->input('Contact.imrequiredonboth');
  8784. $expected = array(
  8785. 'div' => array('class' => 'input text required'),
  8786. 'label' => array('for' => 'ContactImrequiredonboth'),
  8787. 'Imrequiredonboth',
  8788. '/label',
  8789. 'input' => array(
  8790. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8791. 'id' => 'ContactImrequiredonboth',
  8792. 'required' => 'required'
  8793. ),
  8794. '/div'
  8795. );
  8796. $this->assertTags($result, $expected);
  8797. $result = $this->Form->input('Contact.imrequired');
  8798. $expected = array(
  8799. 'div' => array('class' => 'input text required'),
  8800. 'label' => array('for' => 'ContactImrequired'),
  8801. 'Imrequired',
  8802. '/label',
  8803. 'input' => array(
  8804. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8805. 'id' => 'ContactImrequired',
  8806. 'required' => 'required'
  8807. ),
  8808. '/div'
  8809. );
  8810. $this->assertTags($result, $expected);
  8811. }
  8812. /**
  8813. * Test inputDefaults setter and getter
  8814. *
  8815. * @return void
  8816. */
  8817. public function testInputDefaults() {
  8818. $this->Form->create('Contact');
  8819. $this->Form->inputDefaults(array(
  8820. 'label' => false,
  8821. 'div' => array(
  8822. 'style' => 'color: #000;'
  8823. )
  8824. ));
  8825. $result = $this->Form->input('Contact.field1');
  8826. $expected = array(
  8827. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8828. 'input' => array(
  8829. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8830. 'id' => 'ContactField1'
  8831. ),
  8832. '/div'
  8833. );
  8834. $this->assertTags($result, $expected);
  8835. $this->Form->inputDefaults(array(
  8836. 'div' => false,
  8837. 'label' => 'Label',
  8838. ));
  8839. $result = $this->Form->input('Contact.field1');
  8840. $expected = array(
  8841. 'label' => array('for' => 'ContactField1'),
  8842. 'Label',
  8843. '/label',
  8844. 'input' => array(
  8845. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8846. 'id' => 'ContactField1'
  8847. ),
  8848. );
  8849. $this->assertTags($result, $expected);
  8850. $this->Form->inputDefaults(array(
  8851. 'label' => false,
  8852. ), true);
  8853. $result = $this->Form->input('Contact.field1');
  8854. $expected = array(
  8855. 'input' => array(
  8856. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8857. 'id' => 'ContactField1'
  8858. ),
  8859. );
  8860. $this->assertTags($result, $expected);
  8861. $result = $this->Form->inputDefaults();
  8862. $expected = array(
  8863. 'div' => false,
  8864. 'label' => false,
  8865. );
  8866. $this->assertEquals($expected, $result);
  8867. }
  8868. }