PageRenderTime 98ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/View/Helper/FormHelperTest.php

https://bitbucket.org/pyroka/hms
PHP | 8004 lines | 6256 code | 627 blank | 1121 comment | 1 complexity | 0e5e86c56a99161709d74a244d105691 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  102. 'check' => array('rule' => 'alphaNumeric')
  103. ),
  104. 'string_required' => 'notEmpty',
  105. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  106. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  107. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  108. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  109. 'imalsonotrequired' => array(
  110. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  111. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  112. ),
  113. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  114. );
  115. /**
  116. * schema method
  117. *
  118. * @return void
  119. */
  120. public function setSchema($schema) {
  121. $this->_schema = $schema;
  122. }
  123. /**
  124. * hasAndBelongsToMany property
  125. *
  126. * @var array
  127. */
  128. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  129. /**
  130. * hasAndBelongsToMany property
  131. *
  132. * @var array
  133. */
  134. public $belongsTo = array('User' => array('className' => 'UserForm'));
  135. }
  136. /**
  137. * ContactTagsContact class
  138. *
  139. * @package cake
  140. * @package Cake.Test.Case.View.Helper
  141. */
  142. class ContactTagsContact extends CakeTestModel {
  143. /**
  144. * useTable property
  145. *
  146. * @var bool false
  147. */
  148. public $useTable = false;
  149. /**
  150. * name property
  151. *
  152. * @var string 'Contact'
  153. */
  154. public $name = 'ContactTagsContact';
  155. /**
  156. * Default schema
  157. *
  158. * @var array
  159. */
  160. protected $_schema = array(
  161. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  162. 'contact_tag_id' => array(
  163. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  164. )
  165. );
  166. /**
  167. * schema method
  168. *
  169. * @return void
  170. */
  171. public function setSchema($schema) {
  172. $this->_schema = $schema;
  173. }
  174. }
  175. /**
  176. * ContactNonStandardPk class
  177. *
  178. * @package cake
  179. * @package Cake.Test.Case.View.Helper
  180. */
  181. class ContactNonStandardPk extends Contact {
  182. /**
  183. * primaryKey property
  184. *
  185. * @var string 'pk'
  186. */
  187. public $primaryKey = 'pk';
  188. /**
  189. * name property
  190. *
  191. * @var string 'ContactNonStandardPk'
  192. */
  193. public $name = 'ContactNonStandardPk';
  194. /**
  195. * schema method
  196. *
  197. * @return void
  198. */
  199. public function schema($field = false) {
  200. $this->_schema = parent::schema();
  201. $this->_schema['pk'] = $this->_schema['id'];
  202. unset($this->_schema['id']);
  203. return $this->_schema;
  204. }
  205. }
  206. /**
  207. * ContactTag class
  208. *
  209. * @package cake
  210. * @package Cake.Test.Case.View.Helper
  211. */
  212. class ContactTag extends Model {
  213. /**
  214. * useTable property
  215. *
  216. * @var bool false
  217. */
  218. public $useTable = false;
  219. /**
  220. * schema definition
  221. *
  222. * @var array
  223. */
  224. protected $_schema = array(
  225. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  226. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  227. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  228. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  229. );
  230. }
  231. /**
  232. * UserForm class
  233. *
  234. * @package cake
  235. * @package Cake.Test.Case.View.Helper
  236. */
  237. class UserForm extends CakeTestModel {
  238. /**
  239. * useTable property
  240. *
  241. * @var bool false
  242. */
  243. public $useTable = false;
  244. /**
  245. * primaryKey property
  246. *
  247. * @var string 'id'
  248. */
  249. public $primaryKey = 'id';
  250. /**
  251. * name property
  252. *
  253. * @var string 'UserForm'
  254. */
  255. public $name = 'UserForm';
  256. /**
  257. * hasMany property
  258. *
  259. * @var array
  260. */
  261. public $hasMany = array(
  262. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  263. ));
  264. /**
  265. * schema definition
  266. *
  267. * @var array
  268. */
  269. protected $_schema = array(
  270. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  271. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  272. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  273. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  274. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  275. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  276. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  277. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  278. );
  279. }
  280. /**
  281. * OpenidUrl class
  282. *
  283. * @package cake
  284. * @package Cake.Test.Case.View.Helper
  285. */
  286. class OpenidUrl extends CakeTestModel {
  287. /**
  288. * useTable property
  289. *
  290. * @var bool false
  291. */
  292. public $useTable = false;
  293. /**
  294. * primaryKey property
  295. *
  296. * @var string 'id'
  297. */
  298. public $primaryKey = 'id';
  299. /**
  300. * name property
  301. *
  302. * @var string 'OpenidUrl'
  303. */
  304. public $name = 'OpenidUrl';
  305. /**
  306. * belongsTo property
  307. *
  308. * @var array
  309. */
  310. public $belongsTo = array('UserForm' => array(
  311. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  312. ));
  313. /**
  314. * validate property
  315. *
  316. * @var array
  317. */
  318. public $validate = array('openid_not_registered' => array());
  319. /**
  320. * schema method
  321. *
  322. * @var array
  323. */
  324. protected $_schema = array(
  325. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  326. 'user_form_id' => array(
  327. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  328. ),
  329. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('openid_not_registered');
  338. return true;
  339. }
  340. }
  341. /**
  342. * ValidateUser class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateUser extends CakeTestModel {
  348. /**
  349. * primaryKey property
  350. *
  351. * @var string 'id'
  352. */
  353. public $primaryKey = 'id';
  354. /**
  355. * useTable property
  356. *
  357. * @var bool false
  358. */
  359. public $useTable = false;
  360. /**
  361. * name property
  362. *
  363. * @var string 'ValidateUser'
  364. */
  365. public $name = 'ValidateUser';
  366. /**
  367. * hasOne property
  368. *
  369. * @var array
  370. */
  371. public $hasOne = array('ValidateProfile' => array(
  372. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * schema method
  376. *
  377. * @var array
  378. */
  379. protected $_schema = array(
  380. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  381. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  382. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  383. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  384. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  385. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  386. );
  387. /**
  388. * beforeValidate method
  389. *
  390. * @return void
  391. */
  392. public function beforeValidate($options = array()) {
  393. $this->invalidate('email');
  394. return false;
  395. }
  396. }
  397. /**
  398. * ValidateProfile class
  399. *
  400. * @package cake
  401. * @package Cake.Test.Case.View.Helper
  402. */
  403. class ValidateProfile extends CakeTestModel {
  404. /**
  405. * primaryKey property
  406. *
  407. * @var string 'id'
  408. */
  409. public $primaryKey = 'id';
  410. /**
  411. * useTable property
  412. *
  413. * @var bool false
  414. */
  415. public $useTable = false;
  416. /**
  417. * schema property
  418. *
  419. * @var array
  420. */
  421. protected $_schema = array(
  422. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  423. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  424. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  425. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  426. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  427. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  428. );
  429. /**
  430. * name property
  431. *
  432. * @var string 'ValidateProfile'
  433. */
  434. public $name = 'ValidateProfile';
  435. /**
  436. * hasOne property
  437. *
  438. * @var array
  439. */
  440. public $hasOne = array('ValidateItem' => array(
  441. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  442. ));
  443. /**
  444. * belongsTo property
  445. *
  446. * @var array
  447. */
  448. public $belongsTo = array('ValidateUser' => array(
  449. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  450. ));
  451. /**
  452. * beforeValidate method
  453. *
  454. * @return void
  455. */
  456. public function beforeValidate($options = array()) {
  457. $this->invalidate('full_name');
  458. $this->invalidate('city');
  459. return false;
  460. }
  461. }
  462. /**
  463. * ValidateItem class
  464. *
  465. * @package cake
  466. * @package Cake.Test.Case.View.Helper
  467. */
  468. class ValidateItem extends CakeTestModel {
  469. /**
  470. * primaryKey property
  471. *
  472. * @var string 'id'
  473. */
  474. public $primaryKey = 'id';
  475. /**
  476. * useTable property
  477. *
  478. * @var bool false
  479. */
  480. public $useTable = false;
  481. /**
  482. * name property
  483. *
  484. * @var string 'ValidateItem'
  485. */
  486. public $name = 'ValidateItem';
  487. /**
  488. * schema property
  489. *
  490. * @var array
  491. */
  492. protected $_schema = array(
  493. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  495. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  496. 'description' => array(
  497. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  498. ),
  499. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  500. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  501. );
  502. /**
  503. * belongsTo property
  504. *
  505. * @var array
  506. */
  507. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  508. /**
  509. * beforeValidate method
  510. *
  511. * @return void
  512. */
  513. public function beforeValidate($options = array()) {
  514. $this->invalidate('description');
  515. return false;
  516. }
  517. }
  518. /**
  519. * TestMail class
  520. *
  521. * @package cake
  522. * @package Cake.Test.Case.View.Helper
  523. */
  524. class TestMail extends CakeTestModel {
  525. /**
  526. * primaryKey property
  527. *
  528. * @var string 'id'
  529. */
  530. public $primaryKey = 'id';
  531. /**
  532. * useTable property
  533. *
  534. * @var bool false
  535. */
  536. public $useTable = false;
  537. /**
  538. * name property
  539. *
  540. * @var string 'TestMail'
  541. */
  542. public $name = 'TestMail';
  543. }
  544. /**
  545. * FormHelperTest class
  546. *
  547. * @package cake
  548. * @subpackage Cake.Test.Case.View.Helper
  549. * @property FormHelper $Form
  550. */
  551. class FormHelperTest extends CakeTestCase {
  552. /**
  553. * Fixtures to be used
  554. *
  555. * @var array
  556. */
  557. public $fixtures = array('core.post');
  558. /**
  559. * Do not load the fixtures by default
  560. *
  561. * @var boolean
  562. */
  563. public $autoFixtures = false;
  564. /**
  565. * setUp method
  566. *
  567. * @return void
  568. */
  569. public function setUp() {
  570. parent::setUp();
  571. Configure::write('App.base', '');
  572. $this->Controller = new ContactTestController();
  573. $this->View = new View($this->Controller);
  574. $this->Form = new FormHelper($this->View);
  575. $this->Form->Html = new HtmlHelper($this->View);
  576. $this->Form->request = new CakeRequest('contacts/add', false);
  577. $this->Form->request->here = '/contacts/add';
  578. $this->Form->request['action'] = 'add';
  579. $this->Form->request->webroot = '';
  580. $this->Form->request->base = '';
  581. ClassRegistry::addObject('Contact', new Contact());
  582. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  583. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  584. ClassRegistry::addObject('User', new UserForm());
  585. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  586. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  587. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  588. $this->oldSalt = Configure::read('Security.salt');
  589. $this->dateRegex = array(
  590. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  591. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  592. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  593. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  594. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  595. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  596. );
  597. Configure::write('Security.salt', 'foo!');
  598. }
  599. /**
  600. * tearDown method
  601. *
  602. * @return void
  603. */
  604. public function tearDown() {
  605. parent::tearDown();
  606. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  607. Configure::write('Security.salt', $this->oldSalt);
  608. }
  609. /**
  610. * testFormCreateWithSecurity method
  611. *
  612. * Test form->create() with security key.
  613. *
  614. * @return void
  615. */
  616. public function testCreateWithSecurity() {
  617. $this->Form->request['_Token'] = array('key' => 'testKey');
  618. $encoding = strtolower(Configure::read('App.encoding'));
  619. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  620. $expected = array(
  621. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  622. 'div' => array('style' => 'display:none;'),
  623. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  624. array('input' => array(
  625. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  631. $expected['form']['id'] = 'MyForm';
  632. $this->assertTags($result, $expected);
  633. }
  634. /**
  635. * test that create() clears the fields property so it starts fresh
  636. *
  637. * @return void
  638. */
  639. public function testCreateClearingFields() {
  640. $this->Form->fields = array('model_id');
  641. $this->Form->create('Contact');
  642. $this->assertEquals(array(), $this->Form->fields);
  643. }
  644. /**
  645. * Tests form hash generation with model-less data
  646. *
  647. * @return void
  648. */
  649. public function testValidateHashNoModel() {
  650. $this->Form->request['_Token'] = array('key' => 'foo');
  651. $result = $this->Form->secure(array('anything'));
  652. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  653. }
  654. /**
  655. * Tests that models with identical field names get resolved properly
  656. *
  657. * @return void
  658. */
  659. public function testDuplicateFieldNameResolution() {
  660. $result = $this->Form->create('ValidateUser');
  661. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  662. $result = $this->Form->input('ValidateItem.name');
  663. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  664. $result = $this->Form->input('ValidateUser.name');
  665. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  666. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  667. $this->assertRegExp('/type="text"/', $result);
  668. $result = $this->Form->input('ValidateItem.name');
  669. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  670. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  671. $this->assertRegExp('/<textarea/', $result);
  672. $result = $this->Form->input('name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. }
  677. /**
  678. * Tests that hidden fields generated for checkboxes don't get locked
  679. *
  680. * @return void
  681. */
  682. public function testNoCheckboxLocking() {
  683. $this->Form->request['_Token'] = array('key' => 'foo');
  684. $this->assertSame(array(), $this->Form->fields);
  685. $this->Form->checkbox('check', array('value' => '1'));
  686. $this->assertSame($this->Form->fields, array('check'));
  687. }
  688. /**
  689. * testFormSecurityFields method
  690. *
  691. * Test generation of secure form hash generation.
  692. *
  693. * @return void
  694. */
  695. public function testFormSecurityFields() {
  696. $key = 'testKey';
  697. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  698. $this->Form->request['_Token'] = array('key' => $key);
  699. $result = $this->Form->secure($fields);
  700. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  701. $expected .= ':' . 'Model.valid';
  702. $expected = array(
  703. 'div' => array('style' => 'display:none;'),
  704. array('input' => array(
  705. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  706. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  707. )),
  708. array('input' => array(
  709. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  710. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  711. )),
  712. '/div'
  713. );
  714. $this->assertTags($result, $expected);
  715. }
  716. /**
  717. * Tests correct generation of number fields for double and float fields
  718. *
  719. * @return void
  720. */
  721. public function testTextFieldGenerationForFloats() {
  722. $model = ClassRegistry::getObject('Contact');
  723. $model->setSchema(array('foo' => array(
  724. 'type' => 'float',
  725. 'null' => false,
  726. 'default' => null,
  727. 'length' => null
  728. )));
  729. $this->Form->create('Contact');
  730. $result = $this->Form->input('foo');
  731. $expected = array(
  732. 'div' => array('class' => 'input number'),
  733. 'label' => array('for' => 'ContactFoo'),
  734. 'Foo',
  735. '/label',
  736. array('input' => array(
  737. 'type' => 'number',
  738. 'name' => 'data[Contact][foo]',
  739. 'id' => 'ContactFoo',
  740. 'step' => 'any'
  741. )),
  742. '/div'
  743. );
  744. $this->assertTags($result, $expected);
  745. $result = $this->Form->input('foo', array('step' => 0.5));
  746. $expected = array(
  747. 'div' => array('class' => 'input number'),
  748. 'label' => array('for' => 'ContactFoo'),
  749. 'Foo',
  750. '/label',
  751. array('input' => array(
  752. 'type' => 'number',
  753. 'name' => 'data[Contact][foo]',
  754. 'id' => 'ContactFoo',
  755. 'step' => '0.5'
  756. )),
  757. '/div'
  758. );
  759. $this->assertTags($result, $expected);
  760. }
  761. /**
  762. * Tests correct generation of number fields for integer fields
  763. *
  764. * @access public
  765. * @return void
  766. */
  767. public function testTextFieldTypeNumberGenerationForIntegers() {
  768. $model = ClassRegistry::getObject('Contact');
  769. $model->setSchema(array('foo' => array(
  770. 'type' => 'integer',
  771. 'null' => false,
  772. 'default' => null,
  773. 'length' => null
  774. )));
  775. $this->Form->create('Contact');
  776. $result = $this->Form->input('foo');
  777. $expected = array(
  778. 'div' => array('class' => 'input number'),
  779. 'label' => array('for' => 'ContactFoo'),
  780. 'Foo',
  781. '/label',
  782. array('input' => array(
  783. 'type' => 'number', 'name' => 'data[Contact][foo]',
  784. 'id' => 'ContactFoo'
  785. )),
  786. '/div'
  787. );
  788. $this->assertTags($result, $expected);
  789. }
  790. /**
  791. * testFormSecurityMultipleFields method
  792. *
  793. * Test secure() with multiple row form. Ensure hash is correct.
  794. *
  795. * @return void
  796. */
  797. public function testFormSecurityMultipleFields() {
  798. $key = 'testKey';
  799. $fields = array(
  800. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  801. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  802. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  803. );
  804. $this->Form->request['_Token'] = array('key' => $key);
  805. $result = $this->Form->secure($fields);
  806. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  807. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  808. $expected = array(
  809. 'div' => array('style' => 'display:none;'),
  810. array('input' => array(
  811. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  812. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  813. )),
  814. array('input' => array(
  815. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  816. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  817. )),
  818. '/div'
  819. );
  820. $this->assertTags($result, $expected);
  821. }
  822. /**
  823. * testFormSecurityMultipleSubmitButtons
  824. *
  825. * test form submit generation and ensure that _Token is only created on end()
  826. *
  827. * @return void
  828. */
  829. public function testFormSecurityMultipleSubmitButtons() {
  830. $key = 'testKey';
  831. $this->Form->request['_Token'] = array('key' => $key);
  832. $this->Form->create('Addresses');
  833. $this->Form->input('Address.title');
  834. $this->Form->input('Address.first_name');
  835. $result = $this->Form->submit('Save', array('name' => 'save'));
  836. $expected = array(
  837. 'div' => array('class' => 'submit'),
  838. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  839. '/div',
  840. );
  841. $this->assertTags($result, $expected);
  842. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  843. $expected = array(
  844. 'div' => array('class' => 'submit'),
  845. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  846. '/div',
  847. );
  848. $this->assertTags($result, $expected);
  849. $result = $this->Form->end(null);
  850. $expected = array(
  851. 'div' => array('style' => 'display:none;'),
  852. array('input' => array(
  853. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  854. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  855. )),
  856. array('input' => array(
  857. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  858. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  859. )),
  860. '/div'
  861. );
  862. $this->assertTags($result, $expected);
  863. }
  864. /**
  865. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  866. *
  867. * @return void
  868. */
  869. public function testSecurityButtonNestedNamed() {
  870. $key = 'testKey';
  871. $this->Form->request['_Token'] = array('key' => $key);
  872. $this->Form->create('Addresses');
  873. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  874. $result = $this->Form->unlockField();
  875. $this->assertEquals(array('Address.button'), $result);
  876. }
  877. /**
  878. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  879. *
  880. * @return void
  881. */
  882. public function testSecuritySubmitNestedNamed() {
  883. $key = 'testKey';
  884. $this->Form->request['_Token'] = array('key' => $key);
  885. $this->Form->create('Addresses');
  886. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  887. $result = $this->Form->unlockField();
  888. $this->assertEquals(array('Address.button'), $result);
  889. }
  890. /**
  891. * Test that the correct fields are unlocked for image submits with no names.
  892. *
  893. * @return void
  894. */
  895. public function testSecuritySubmitImageNoName() {
  896. $key = 'testKey';
  897. $this->Form->request['_Token'] = array('key' => $key);
  898. $this->Form->create('User');
  899. $result = $this->Form->submit('save.png');
  900. $expected = array(
  901. 'div' => array('class' => 'submit'),
  902. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  903. '/div'
  904. );
  905. $this->assertTags($result, $expected);
  906. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  907. }
  908. /**
  909. * Test that the correct fields are unlocked for image submits with names.
  910. *
  911. * @return void
  912. */
  913. public function testSecuritySubmitImageName() {
  914. $key = 'testKey';
  915. $this->Form->request['_Token'] = array('key' => $key);
  916. $this->Form->create('User');
  917. $result = $this->Form->submit('save.png', array('name' => 'test'));
  918. $expected = array(
  919. 'div' => array('class' => 'submit'),
  920. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  921. '/div'
  922. );
  923. $this->assertTags($result, $expected);
  924. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  925. }
  926. /**
  927. * testFormSecurityMultipleInputFields method
  928. *
  929. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  930. *
  931. * @return void
  932. */
  933. public function testFormSecurityMultipleInputFields() {
  934. $key = 'testKey';
  935. $this->Form->request['_Token'] = array('key' => $key);
  936. $this->Form->create('Addresses');
  937. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  938. $this->Form->input('Addresses.0.title');
  939. $this->Form->input('Addresses.0.first_name');
  940. $this->Form->input('Addresses.0.last_name');
  941. $this->Form->input('Addresses.0.address');
  942. $this->Form->input('Addresses.0.city');
  943. $this->Form->input('Addresses.0.phone');
  944. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  945. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  946. $this->Form->input('Addresses.1.title');
  947. $this->Form->input('Addresses.1.first_name');
  948. $this->Form->input('Addresses.1.last_name');
  949. $this->Form->input('Addresses.1.address');
  950. $this->Form->input('Addresses.1.city');
  951. $this->Form->input('Addresses.1.phone');
  952. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  953. $result = $this->Form->secure($this->Form->fields);
  954. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  955. $expected = array(
  956. 'div' => array('style' => 'display:none;'),
  957. array('input' => array(
  958. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  959. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  960. )),
  961. array('input' => array(
  962. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  963. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  964. )),
  965. '/div'
  966. );
  967. $this->assertTags($result, $expected);
  968. }
  969. /**
  970. * Test form security with Model.field.0 style inputs
  971. *
  972. * @return void
  973. */
  974. public function testFormSecurityArrayFields() {
  975. $key = 'testKey';
  976. $this->Form->request->params['_Token']['key'] = $key;
  977. $this->Form->create('Address');
  978. $this->Form->input('Address.primary.1');
  979. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  980. $this->Form->input('Address.secondary.1.0');
  981. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  982. }
  983. /**
  984. * testFormSecurityMultipleInputDisabledFields method
  985. *
  986. * test secure form generation with multiple records and disabled fields.
  987. *
  988. * @return void
  989. */
  990. public function testFormSecurityMultipleInputDisabledFields() {
  991. $key = 'testKey';
  992. $this->Form->request->params['_Token'] = array(
  993. 'key' => $key,
  994. 'unlockedFields' => array('first_name', 'address')
  995. );
  996. $this->Form->create();
  997. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  998. $this->Form->input('Addresses.0.title');
  999. $this->Form->input('Addresses.0.first_name');
  1000. $this->Form->input('Addresses.0.last_name');
  1001. $this->Form->input('Addresses.0.address');
  1002. $this->Form->input('Addresses.0.city');
  1003. $this->Form->input('Addresses.0.phone');
  1004. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1005. $this->Form->input('Addresses.1.title');
  1006. $this->Form->input('Addresses.1.first_name');
  1007. $this->Form->input('Addresses.1.last_name');
  1008. $this->Form->input('Addresses.1.address');
  1009. $this->Form->input('Addresses.1.city');
  1010. $this->Form->input('Addresses.1.phone');
  1011. $result = $this->Form->secure($this->Form->fields);
  1012. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1013. $expected = array(
  1014. 'div' => array('style' => 'display:none;'),
  1015. array('input' => array(
  1016. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1017. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1018. )),
  1019. array('input' => array(
  1020. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1021. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1022. )),
  1023. '/div'
  1024. );
  1025. $this->assertTags($result, $expected);
  1026. }
  1027. /**
  1028. * testFormSecurityInputDisabledFields method
  1029. *
  1030. * Test single record form with disabled fields.
  1031. *
  1032. * @return void
  1033. */
  1034. public function testFormSecurityInputUnlockedFields() {
  1035. $key = 'testKey';
  1036. $this->Form->request['_Token'] = array(
  1037. 'key' => $key,
  1038. 'unlockedFields' => array('first_name', 'address')
  1039. );
  1040. $this->Form->create();
  1041. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1042. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1043. $this->Form->input('Addresses.title');
  1044. $this->Form->input('Addresses.first_name');
  1045. $this->Form->input('Addresses.last_name');
  1046. $this->Form->input('Addresses.address');
  1047. $this->Form->input('Addresses.city');
  1048. $this->Form->input('Addresses.phone');
  1049. $result = $this->Form->fields;
  1050. $expected = array(
  1051. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1052. 'Addresses.city', 'Addresses.phone'
  1053. );
  1054. $this->assertEquals($expected, $result);
  1055. $result = $this->Form->secure($expected);
  1056. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1057. $expected = array(
  1058. 'div' => array('style' => 'display:none;'),
  1059. array('input' => array(
  1060. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1061. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1062. )),
  1063. array('input' => array(
  1064. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1065. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1066. )),
  1067. '/div'
  1068. );
  1069. $this->assertTags($result, $expected);
  1070. }
  1071. /**
  1072. * test securing inputs with custom name attributes.
  1073. *
  1074. * @return void
  1075. */
  1076. public function testFormSecureWithCustomNameAttribute() {
  1077. $this->Form->request->params['_Token']['key'] = 'testKey';
  1078. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1079. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1080. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1081. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1082. }
  1083. /**
  1084. * testFormSecuredInput method
  1085. *
  1086. * Test generation of entire secure form, assertions made on input() output.
  1087. *
  1088. * @return void
  1089. */
  1090. public function testFormSecuredInput() {
  1091. $this->Form->request['_Token'] = array('key' => 'testKey');
  1092. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1093. $encoding = strtolower(Configure::read('App.encoding'));
  1094. $expected = array(
  1095. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1096. 'div' => array('style' => 'display:none;'),
  1097. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1098. array('input' => array(
  1099. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1100. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1101. )),
  1102. '/div'
  1103. );
  1104. $this->assertTags($result, $expected);
  1105. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1106. $expected = array(
  1107. 'div' => array('class' => 'input text'),
  1108. 'label' => array('for' => 'UserFormPublished'),
  1109. 'Published',
  1110. '/label',
  1111. array('input' => array(
  1112. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1113. 'id' => 'UserFormPublished'
  1114. )),
  1115. '/div'
  1116. );
  1117. $this->assertTags($result, $expected);
  1118. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1119. $expected = array(
  1120. 'div' => array('class' => 'input text'),
  1121. 'label' => array('for' => 'UserFormOther'),
  1122. 'Other',
  1123. '/label',
  1124. array('input' => array(
  1125. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1126. 'id' => 'UserFormOther'
  1127. )),
  1128. '/div'
  1129. );
  1130. $this->assertTags($result, $expected);
  1131. $result = $this->Form->hidden('UserForm.stuff');
  1132. $expected = array('input' => array(
  1133. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1134. 'id' => 'UserFormStuff'
  1135. ));
  1136. $this->assertTags($result, $expected);
  1137. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1138. $expected = array('input' => array(
  1139. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1140. 'value' => '0', 'id' => 'UserFormHidden'
  1141. ));
  1142. $this->assertTags($result, $expected);
  1143. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1144. $expected = array(
  1145. 'div' => array('class' => 'input checkbox'),
  1146. array('input' => array(
  1147. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1148. 'value' => '0', 'id' => 'UserFormSomething_'
  1149. )),
  1150. array('input' => array(
  1151. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1152. 'value' => '1', 'id' => 'UserFormSomething'
  1153. )),
  1154. 'label' => array('for' => 'UserFormSomething'),
  1155. 'Something',
  1156. '/label',
  1157. '/div'
  1158. );
  1159. $this->assertTags($result, $expected);
  1160. $result = $this->Form->fields;
  1161. $expected = array(
  1162. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1163. 'UserForm.hidden' => '0', 'UserForm.something'
  1164. );
  1165. $this->assertEquals($expected, $result);
  1166. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1167. $result = $this->Form->secure($this->Form->fields);
  1168. $expected = array(
  1169. 'div' => array('style' => 'display:none;'),
  1170. array('input' => array(
  1171. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1172. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1173. )),
  1174. array('input' => array(
  1175. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1176. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1177. )),
  1178. '/div'
  1179. );
  1180. $this->assertTags($result, $expected);
  1181. }
  1182. /**
  1183. * Tests that the correct keys are added to the field hash index
  1184. *
  1185. * @return void
  1186. */
  1187. public function testFormSecuredFileInput() {
  1188. $this->Form->request['_Token'] = array('key' => 'testKey');
  1189. $this->assertEquals(array(), $this->Form->fields);
  1190. $result = $this->Form->file('Attachment.file');
  1191. $expected = array(
  1192. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1193. 'Attachment.file.error', 'Attachment.file.size'
  1194. );
  1195. $this->assertEquals($expected, $this->Form->fields);
  1196. }
  1197. /**
  1198. * test that multiple selects keys are added to field hash
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFormSecuredMultipleSelect() {
  1203. $this->Form->request['_Token'] = array('key' => 'testKey');
  1204. $this->assertEquals(array(), $this->Form->fields);
  1205. $options = array('1' => 'one', '2' => 'two');
  1206. $this->Form->select('Model.select', $options);
  1207. $expected = array('Model.select');
  1208. $this->assertEquals($expected, $this->Form->fields);
  1209. $this->Form->fields = array();
  1210. $this->Form->select('Model.select', $options, array('multiple' => true));
  1211. $this->assertEquals($expected, $this->Form->fields);
  1212. }
  1213. /**
  1214. * testFormSecuredRadio method
  1215. *
  1216. * @return void
  1217. */
  1218. public function testFormSecuredRadio() {
  1219. $this->Form->request['_Token'] = array('key' => 'testKey');
  1220. $this->assertEquals(array(), $this->Form->fields);
  1221. $options = array('1' => 'option1', '2' => 'option2');
  1222. $this->Form->radio('Test.test', $options);
  1223. $expected = array('Test.test');
  1224. $this->assertEquals($expected, $this->Form->fields);
  1225. }
  1226. /**
  1227. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1228. * hashing.
  1229. *
  1230. * @return void
  1231. */
  1232. public function testFormSecuredAndDisabled() {
  1233. $this->Form->request['_Token'] = array('key' => 'testKey');
  1234. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1235. $this->Form->text('Model.text', array('disabled' => true));
  1236. $this->Form->password('Model.text', array('disabled' => true));
  1237. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1238. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1239. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1240. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1241. $this->Form->month('Model.month', array('disabled' => true));
  1242. $this->Form->day('Model.day', array('disabled' => true));
  1243. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1244. $this->Form->minute('Model.minute', array('disabled' => true));
  1245. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1246. $expected = array(
  1247. 'Model.radio' => ''
  1248. );
  1249. $this->assertEquals($expected, $this->Form->fields);
  1250. }
  1251. /**
  1252. * testDisableSecurityUsingForm method
  1253. *
  1254. * @return void
  1255. */
  1256. public function testDisableSecurityUsingForm() {
  1257. $this->Form->request['_Token'] = array(
  1258. 'key' => 'testKey',
  1259. 'disabledFields' => array()
  1260. );
  1261. $this->Form->create();
  1262. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1263. $this->Form->input('Addresses.title');
  1264. $this->Form->input('Addresses.first_name', array('secure' => false));
  1265. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1266. $this->Form->input('Addresses.zip', array(
  1267. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1268. ));
  1269. $result = $this->Form->fields;
  1270. $expected = array(
  1271. 'Addresses.id' => '123456', 'Addresses.title',
  1272. );
  1273. $this->assertEquals($expected, $result);
  1274. }
  1275. /**
  1276. * test disableField
  1277. *
  1278. * @return void
  1279. */
  1280. public function testUnlockFieldAddsToList() {
  1281. $this->Form->request['_Token'] = array(
  1282. 'key' => 'testKey',
  1283. 'unlockedFields' => array()
  1284. );
  1285. $this->Form->create('Contact');
  1286. $this->Form->unlockField('Contact.name');
  1287. $this->Form->text('Contact.name');
  1288. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1289. $this->assertEquals(array(), $this->Form->fields);
  1290. }
  1291. /**
  1292. * test unlockField removing from fields array.
  1293. *
  1294. * @return void
  1295. */
  1296. public function testUnlockFieldRemovingFromFields() {
  1297. $this->Form->request['_Token'] = array(
  1298. 'key' => 'testKey',
  1299. 'unlockedFields' => array()
  1300. );
  1301. $this->Form->create('Contact');
  1302. $this->Form->hidden('Contact.id', array('value' => 1));
  1303. $this->Form->text('Contact.name');
  1304. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1305. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1306. $this->Form->unlockField('Contact.name');
  1307. $this->Form->unlockField('Contact.id');
  1308. $this->assertEquals(array(), $this->Form->fields);
  1309. }
  1310. /**
  1311. * testTagIsInvalid method
  1312. *
  1313. * @return void
  1314. */
  1315. public function testTagIsInvalid() {
  1316. $Contact = ClassRegistry::getObject('Contact');
  1317. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1318. $this->Form->setEntity('Contact.0.email');
  1319. $result = $this->Form->tagIsInvalid();
  1320. $expected = array('Please provide an email');
  1321. $this->assertEquals($expected, $result);
  1322. $this->Form->setEntity('Contact.1.email');
  1323. $result = $this->Form->tagIsInvalid();
  1324. $expected = false;
  1325. $this->assertSame($expected, $result);
  1326. $this->Form->setEntity('Contact.0.name');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = false;
  1329. $this->assertSame($expected, $result);
  1330. }
  1331. /**
  1332. * testPasswordValidation method
  1333. *
  1334. * test validation errors on password input.
  1335. *
  1336. * @return void
  1337. */
  1338. public function testPasswordValidation() {
  1339. $Contact = ClassRegistry::getObject('Contact');
  1340. $Contact->validationErrors['password'] = array('Please provide a password');
  1341. $result = $this->Form->input('Contact.password');
  1342. $expected = array(
  1343. 'div' => array('class' => 'input password error'),
  1344. 'label' => array('for' => 'ContactPassword'),
  1345. 'Password',
  1346. '/label',
  1347. 'input' => array(
  1348. 'type' => 'password', 'name' => 'data[Contact][password]',
  1349. 'id' => 'ContactPassword', 'class' => 'form-error'
  1350. ),
  1351. array('div' => array('class' => 'error-message')),
  1352. 'Please provide a password',
  1353. '/div',
  1354. '/div'
  1355. );
  1356. $this->assertTags($result, $expected);
  1357. }
  1358. /**
  1359. * testEmptyErrorValidation method
  1360. *
  1361. * test validation error div when validation message is an empty string
  1362. *
  1363. * @access public
  1364. * @return void
  1365. */
  1366. public function testEmptyErrorValidation() {
  1367. $this->Form->validationErrors['Contact']['password'] = '';
  1368. $result = $this->Form->input('Contact.password');
  1369. $expected = array(
  1370. 'div' => array('class' => 'input password error'),
  1371. 'label' => array('for' => 'ContactPassword'),
  1372. 'Password',
  1373. '/label',
  1374. 'input' => array(
  1375. 'type' => 'password', 'name' => 'data[Contact][password]',
  1376. 'id' => 'ContactPassword', 'class' => 'form-error'
  1377. ),
  1378. array('div' => array('class' => 'error-message')),
  1379. array(),
  1380. '/div',
  1381. '/div'
  1382. );
  1383. $this->assertTags($result, $expected);
  1384. }
  1385. /**
  1386. * testEmptyInputErrorValidation method
  1387. *
  1388. * test validation error div when validation message is overridden by an empty string when calling input()
  1389. *
  1390. * @access public
  1391. * @return void
  1392. */
  1393. public function testEmptyInputErrorValidation() {
  1394. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1395. $result = $this->Form->input('Contact.password', array('error' => ''));
  1396. $expected = array(
  1397. 'div' => array('class' => 'input password error'),
  1398. 'label' => array('for' => 'ContactPassword'),
  1399. 'Password',
  1400. '/label',
  1401. 'input' => array(
  1402. 'type' => 'password', 'name' => 'data[Contact][password]',
  1403. 'id' => 'ContactPassword', 'class' => 'form-error'
  1404. ),
  1405. array('div' => array('class' => 'error-message')),
  1406. array(),
  1407. '/div',
  1408. '/div'
  1409. );
  1410. $this->assertTags($result, $expected);
  1411. }
  1412. /**
  1413. * testFormValidationAssociated method
  1414. *
  1415. * test display of form errors in conjunction with model::validates.
  1416. *
  1417. * @return void
  1418. */
  1419. public function testFormValidationAssociated() {
  1420. $this->UserForm = ClassRegistry::getObject('UserForm');
  1421. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1422. $data = array(
  1423. 'UserForm' => array('name' => 'user'),
  1424. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1425. );
  1426. $result = $this->UserForm->OpenidUrl->create($data);
  1427. $this->assertFalse(empty($result));
  1428. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1429. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1430. $encoding = strtolower(Configure::read('App.encoding'));
  1431. $expected = array(
  1432. 'form' => array(
  1433. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1434. 'accept-charset' => $encoding
  1435. ),
  1436. 'div' => array('style' => 'display:none;'),
  1437. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1438. '/div'
  1439. );
  1440. $this->assertTags($result, $expected);
  1441. $result = $this->Form->error(
  1442. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1443. );
  1444. $this->assertEquals('Error, not registered', $result);
  1445. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1446. }
  1447. /**
  1448. * testFormValidationAssociatedFirstLevel method
  1449. *
  1450. * test form error display with associated model.
  1451. *
  1452. * @return void
  1453. */
  1454. public function testFormValidationAssociatedFirstLevel() {
  1455. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1456. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1457. $data = array(
  1458. 'ValidateUser' => array('name' => 'mariano'),
  1459. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1460. );
  1461. $result = $this->ValidateUser->create($data);
  1462. $this->assertFalse(empty($result));
  1463. $this->assertFalse($this->ValidateUser->validates());
  1464. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1465. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1466. $encoding = strtolower(Configure::read('App.encoding'));
  1467. $expected = array(
  1468. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1469. 'div' => array('style' => 'display:none;'),
  1470. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1471. '/div'
  1472. );
  1473. $this->assertTags($result, $expected);
  1474. $result = $this->Form->error(
  1475. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1476. );
  1477. $this->assertEquals('Invalid email', $result);
  1478. $result = $this->Form->error(
  1479. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1480. );
  1481. $this->assertEquals('Invalid name', $result);
  1482. $result = $this->Form->error(
  1483. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1484. );
  1485. $this->assertEquals('Invalid city', $result);
  1486. unset($this->ValidateUser->ValidateProfile);
  1487. unset($this->ValidateUser);
  1488. }
  1489. /**
  1490. * testFormValidationAssociatedSecondLevel method
  1491. *
  1492. * test form error display with associated model.
  1493. *
  1494. * @return void
  1495. */
  1496. public function testFormValidationAssociatedSecondLevel() {
  1497. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1498. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1499. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1500. $data = array(
  1501. 'ValidateUser' => array('name' => 'mariano'),
  1502. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1503. 'ValidateItem' => array('name' => 'Item')
  1504. );
  1505. $result = $this->ValidateUser->create($data);
  1506. $this->assertFalse(empty($result));
  1507. $this->assertFalse($this->ValidateUser->validates());
  1508. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1509. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1510. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1511. $encoding = strtolower(Configure::read('App.encoding'));
  1512. $expected = array(
  1513. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1514. 'div' => array('style' => 'display:none;'),
  1515. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1516. '/div'
  1517. );
  1518. $this->assertTags($result, $expected);
  1519. $result = $this->Form->error(
  1520. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1521. );
  1522. $this->assertEquals('Invalid email', $result);
  1523. $result = $this->Form->error(
  1524. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1525. );
  1526. $this->assertEquals('Invalid name', $result);
  1527. $result = $this->Form->error(
  1528. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1529. );
  1530. $result = $this->Form->error(
  1531. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1532. );
  1533. $this->assertEquals('Invalid description', $result);
  1534. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1535. unset($this->ValidateUser->ValidateProfile);
  1536. unset($this->ValidateUser);
  1537. }
  1538. /**
  1539. * testFormValidationMultiRecord method
  1540. *
  1541. * test form error display with multiple records.
  1542. *
  1543. * @return void
  1544. */
  1545. public function testFormValidationMultiRecord() {
  1546. $Contact = ClassRegistry::getObject('Contact');
  1547. $Contact->validationErrors[2] = array(
  1548. 'name' => array('This field cannot be left blank')
  1549. );
  1550. $result = $this->Form->input('Contact.2.name');
  1551. $expected = array(
  1552. 'div' => array('class' => 'input text error'),
  1553. 'label' => array('for' => 'Contact2Name'),
  1554. 'Name',
  1555. '/label',
  1556. 'input' => array(
  1557. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1558. 'class' => 'form-error', 'maxlength' => 255
  1559. ),
  1560. array('div' => array('class' => 'error-message')),
  1561. 'This field cannot be left blank',
  1562. '/div',
  1563. '/div'
  1564. );
  1565. $this->assertTags($result, $expected);
  1566. }
  1567. /**
  1568. * testMultipleInputValidation method
  1569. *
  1570. * test multiple record form validation error display.
  1571. *
  1572. * @return void
  1573. */
  1574. public function testMultipleInputValidation() {
  1575. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1576. $Address->validationErrors[0] = array(
  1577. 'title' => array('This field cannot be empty'),
  1578. 'first_name' => array('This field cannot be empty')
  1579. );
  1580. $Address->validationErrors[1] = array(
  1581. 'last_name' => array('You must have a last name')
  1582. );
  1583. $this->Form->create();
  1584. $result = $this->Form->input('Address.0.title');
  1585. $expected = array(
  1586. 'div' => array('class'),
  1587. 'label' => array('for'),
  1588. 'preg:/[^<]+/',
  1589. '/label',
  1590. 'input' => array(
  1591. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1592. ),
  1593. array('div' => array('class' => 'error-message')),
  1594. 'This field cannot be empty',
  1595. '/div',
  1596. '/div'
  1597. );
  1598. $this->assertTags($result, $expected);
  1599. $result = $this->Form->input('Address.0.first_name');
  1600. $expected = array(
  1601. 'div' => array('class'),
  1602. 'label' => array('for'),
  1603. 'preg:/[^<]+/',
  1604. '/label',
  1605. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1606. array('div' => array('class' => 'error-message')),
  1607. 'This field cannot be empty',
  1608. '/div',
  1609. '/div'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. $result = $this->Form->input('Address.0.last_name');
  1613. $expected = array(
  1614. 'div' => array('class'),
  1615. 'label' => array('for'),
  1616. 'preg:/[^<]+/',
  1617. '/label',
  1618. 'input' => array('type' => 'text', 'name', 'id'),
  1619. '/div'
  1620. );
  1621. $this->assertTags($result, $expected);
  1622. $result = $this->Form->input('Address.1.last_name');
  1623. $expected = array(
  1624. 'div' => array('class'),
  1625. 'label' => array('for'),
  1626. 'preg:/[^<]+/',
  1627. '/label',
  1628. 'input' => array(
  1629. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1630. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1631. ),
  1632. array('div' => array('class' => 'error-message')),
  1633. 'You must have a last name',
  1634. '/div',
  1635. '/div'
  1636. );
  1637. $this->assertTags($result, $expected);
  1638. }
  1639. /**
  1640. * testInput method
  1641. *
  1642. * Test various incarnations of input().
  1643. *
  1644. * @return void
  1645. */
  1646. public function testInput() {
  1647. $result = $this->Form->input('ValidateUser.balance');
  1648. $expected = array(
  1649. 'div' => array('class'),
  1650. 'label' => array('for'),
  1651. 'Balance',
  1652. '/label',
  1653. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1654. '/div',
  1655. );
  1656. $this->assertTags($result, $expected);
  1657. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1658. $expected = array(
  1659. 'div' => array('class' => 'input text'),
  1660. 'label' => array('for' => 'custom'),
  1661. 'Email',
  1662. '/label',
  1663. array('input' => array(
  1664. 'type' => 'text', 'name' => 'data[Contact][email]',
  1665. 'id' => 'custom', 'maxlength' => 255
  1666. )),
  1667. '/div'
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1671. $expected = array(
  1672. '<div',
  1673. 'label' => array('for' => 'ContactEmail'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'text', 'name' => 'data[Contact][email]',
  1678. 'id' => 'ContactEmail', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->hidden('Contact.idontexist');
  1684. $expected = array('input' => array(
  1685. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1686. 'id' => 'ContactIdontexist'
  1687. ));
  1688. $this->assertTags($result, $expected);
  1689. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1690. $expected = array(
  1691. 'div' => array('class' => 'input text'),
  1692. 'label' => array('for' => 'ContactEmail'),
  1693. 'Email',
  1694. '/label',
  1695. array('input' => array(
  1696. 'type' => 'text', 'name' => 'data[Contact][email]',
  1697. 'id' => 'ContactEmail'
  1698. )),
  1699. '/div'
  1700. );
  1701. $this->assertTags($result, $expected);
  1702. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input text'),
  1705. 'label' => array('for' => 'Contact5Email'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1710. 'id' => 'Contact5Email'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.password');
  1716. $expected = array(
  1717. 'div' => array('class' => 'input password'),
  1718. 'label' => array('for' => 'ContactPassword'),
  1719. 'Password',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'password', 'name' => 'data[Contact][password]',
  1723. 'id' => 'ContactPassword'
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.email', array(
  1729. 'type' => 'file', 'class' => 'textbox'
  1730. ));
  1731. $expected = array(
  1732. 'div' => array('class' => 'input file'),
  1733. 'label' => array('for' => 'ContactEmail'),
  1734. 'Email',
  1735. '/label',
  1736. array('input' => array(
  1737. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1738. 'id' => 'ContactEmail'
  1739. )),
  1740. '/div'
  1741. );
  1742. $this->assertTags($result, $expected);
  1743. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1744. $result = $this->Form->input('Contact.phone');
  1745. $expected = array(
  1746. 'div' => array('class' => 'input text'),
  1747. 'label' => array('for' => 'ContactPhone'),
  1748. 'Phone',
  1749. '/label',
  1750. array('input' => array(
  1751. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1752. 'value' => 'Hello &amp; World &gt; weird chars',
  1753. 'id' => 'ContactPhone', 'maxlength' => 255
  1754. )),
  1755. '/div'
  1756. );
  1757. $this->assertTags($result, $expected);
  1758. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1759. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1760. $expected = array(
  1761. 'div' => array('class' => 'input text'),
  1762. 'label' => array('for' => 'myId'),
  1763. 'Field',
  1764. '/label',
  1765. 'input' => array(
  1766. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1767. 'value' => 'My value', 'id' => 'myId'
  1768. ),
  1769. '/div'
  1770. );
  1771. $this->assertTags($result, $expected);
  1772. unset($this->Form->request->data);
  1773. $Contact = ClassRegistry::getObject('Contact');
  1774. $Contact->validationErrors['field'] = array('Badness!');
  1775. $result = $this->Form->input('Contact.field');
  1776. $expected = array(
  1777. 'div' => array('class' => 'input text error'),
  1778. 'label' => array('for' => 'ContactField'),
  1779. 'Field',
  1780. '/label',
  1781. 'input' => array(
  1782. 'type' => 'text', 'name' => 'data[Contact][field]',
  1783. 'id' => 'ContactField', 'class' => 'form-error'
  1784. ),
  1785. array('div' => array('class' => 'error-message')),
  1786. 'Badness!',
  1787. '/div',
  1788. '/div'
  1789. );
  1790. $this->assertTags($result, $expected);
  1791. $result = $this->Form->input('Contact.field', array(
  1792. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1793. ));
  1794. $expected = array(
  1795. 'label' => array('for' => 'ContactField'),
  1796. 'Field',
  1797. '/label',
  1798. 'input' => array(
  1799. 'type' => 'text', 'name' => 'data[Contact][field]',
  1800. 'id' => 'ContactField', 'class' => 'form-error'
  1801. ),
  1802. array('span' => array('class' => 'error-message')),
  1803. 'Badness!',
  1804. '/span'
  1805. );
  1806. $this->assertTags($result, $expected);
  1807. $result = $this->Form->input('Contact.field', array(
  1808. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1809. ));
  1810. $expected = array(
  1811. 'div' => array('class' => 'input text error'),
  1812. 'label' => array('for' => 'ContactField'),
  1813. 'Field',
  1814. '/label',
  1815. 'input' => array(
  1816. 'type' => 'text', 'name' => 'data[Contact][field]',
  1817. 'id' => 'ContactField', 'class' => 'form-error'
  1818. ),
  1819. array('div' => array('class' => 'error')),
  1820. 'Badness!',
  1821. '/div'
  1822. );
  1823. $this->assertTags($result, $expected);
  1824. $result = $this->Form->input('Contact.field', array(
  1825. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1826. ));
  1827. $expected = array(
  1828. 'span' => array('class' => 'input text error'),
  1829. 'label' => array('for' => 'ContactField'),
  1830. 'Field',
  1831. '/label',
  1832. 'input' => array(
  1833. 'type' => 'text', 'name' => 'data[Contact][field]',
  1834. 'id' => 'ContactField', 'class' => 'form-error'
  1835. ),
  1836. 'Badness!',
  1837. '/span'
  1838. );
  1839. $this->assertTags($result, $expected);
  1840. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1841. $expected = array(
  1842. 'div' => array('class' => 'input text error'),
  1843. 'label' => array('for' => 'ContactField'),
  1844. 'Field',
  1845. '/label',
  1846. 'input' => array(
  1847. 'type' => 'text', 'name' => 'data[Contact][field]',
  1848. 'id' => 'ContactField', 'class' => 'form-error'
  1849. ),
  1850. 'A message to you, Rudy',
  1851. array('div' => array('class' => 'error-message')),
  1852. 'Badness!',
  1853. '/div',
  1854. '/div'
  1855. );
  1856. $this->assertTags($result, $expected);
  1857. $this->Form->setEntity(null);
  1858. $this->Form->setEntity('Contact.field');
  1859. $result = $this->Form->input('Contact.field', array(
  1860. 'after' => 'A message to you, Rudy', 'error' => false
  1861. ));
  1862. $expected = array(
  1863. 'div' => array('class' => 'input text'),
  1864. 'label' => array('for' => 'ContactField'),
  1865. 'Field',
  1866. '/label',
  1867. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1868. 'A message to you, Rudy',
  1869. '/div'
  1870. );
  1871. $this->assertTags($result, $expected);
  1872. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1873. $expected = array(
  1874. 'div' => array('class' => 'input text'),
  1875. 'label' => array('for' => 'ObjectField'),
  1876. 'Field',
  1877. '/label',
  1878. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1879. 'A message to you, Rudy',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $Contact->validationErrors['field'] = array('minLength');
  1884. $result = $this->Form->input('Contact.field', array(
  1885. 'error' => array(
  1886. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1887. 'maxLength' => 'login too large'
  1888. )
  1889. ));
  1890. $expected = array(
  1891. 'div' => array('class' => 'input text error'),
  1892. 'label' => array('for' => 'ContactField'),
  1893. 'Field',
  1894. '/label',
  1895. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1896. array('div' => array('class' => 'error-message')),
  1897. 'Le login doit contenir au moins 2 caractères',
  1898. '/div',
  1899. '/div'
  1900. );
  1901. $this->assertTags($result, $expected);
  1902. $Contact->validationErrors['field'] = array('maxLength');
  1903. $result = $this->Form->input('Contact.field', array(
  1904. 'error' => array(
  1905. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1906. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1907. 'maxLength' => 'login too large',
  1908. )
  1909. ));
  1910. $expected = array(
  1911. 'div' => array('class' => 'input text error'),
  1912. 'label' => array('for' => 'ContactField'),
  1913. 'Field',
  1914. '/label',
  1915. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1916. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1917. 'login too large',
  1918. '/span',
  1919. '/div'
  1920. );
  1921. $this->assertTags($result, $expected);
  1922. }
  1923. /**
  1924. * test input() with checkbox creation
  1925. *
  1926. * @return void
  1927. */
  1928. public function testInputCheckbox() {
  1929. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1930. $expected = array(
  1931. 'div' => array('class' => 'input checkbox'),
  1932. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1933. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1934. '/div'
  1935. );
  1936. $this->assertTags($result, $expected);
  1937. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1938. $expected = array(
  1939. 'div' => array('class' => 'input checkbox'),
  1940. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1941. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1942. '/div'
  1943. );
  1944. $this->assertTags($result, $expected);
  1945. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1946. $expected = array(
  1947. 'div' => array('class' => 'input checkbox'),
  1948. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1949. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1950. '/div'
  1951. );
  1952. $this->assertTags($result, $expected);
  1953. }
  1954. /**
  1955. * test form->input() with time types.
  1956. *
  1957. */
  1958. public function testInputTime() {
  1959. extract($this->dateRegex);
  1960. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1961. $result = explode(':', $result);
  1962. $this->assertRegExp('/option value="23"/', $result[0]);
  1963. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1964. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1965. $result = explode(':', $result);
  1966. $this->assertRegExp('/option value="23"/', $result[0]);
  1967. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1968. $result = $this->Form->input('Model.field', array(
  1969. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1970. ));
  1971. $result = explode(':', $result);
  1972. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1973. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1974. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1975. $result = $this->Form->input('Model.field', array(
  1976. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1977. ));
  1978. $result = explode(':', $result);
  1979. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1980. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1981. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1982. $result = $this->Form->input('prueba', array(
  1983. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1984. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1985. ));
  1986. $result = explode(':', $result);
  1987. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1988. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1989. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1990. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1991. $result = $this->Form->input('Random.start_time', array(
  1992. 'type' => 'time',
  1993. 'selected' => '18:15'
  1994. ));
  1995. $this->assertRegExp('#<option value="06"[^>]*>6</option>#', $result);
  1996. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result);
  1997. $this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
  1998. }
  1999. /**
  2000. * test form->input() with datetime, date and time types
  2001. *
  2002. * @return void
  2003. */
  2004. public function testInputDatetime() {
  2005. extract($this->dateRegex);
  2006. $result = $this->Form->input('prueba', array(
  2007. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2008. 'maxYear' => date('Y') + 1, 'interval' => 15
  2009. ));
  2010. $result = explode(':', $result);
  2011. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2012. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2013. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2014. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2015. //related to ticket #5013
  2016. $result = $this->Form->input('Contact.date', array(
  2017. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2018. ));
  2019. $this->assertRegExp('/class="customClass"/', $result);
  2020. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2021. $result = $this->Form->input('Contact.date', array(
  2022. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2023. ));
  2024. $this->assertRegExp('/id="customIdDay"/', $result);
  2025. $this->assertRegExp('/id="customIdMonth"/', $result);
  2026. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2027. $result = $this->Form->input('Model.field', array(
  2028. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2029. ));
  2030. $this->assertRegExp('/id="customIDDay"/', $result);
  2031. $this->assertRegExp('/id="customIDHour"/', $result);
  2032. $result = explode('</select><select', $result);
  2033. $result = explode(':', $result[1]);
  2034. $this->assertRegExp('/option value="23"/', $result[0]);
  2035. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2036. $result = $this->Form->input('Model.field', array(
  2037. 'type' => 'datetime', 'timeFormat' => 12
  2038. ));
  2039. $result = explode('</select><select', $result);
  2040. $result = explode(':', $result[1]);
  2041. $this->assertRegExp('/option value="12"/', $result[0]);
  2042. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2043. $this->Form->request->data = array('Contact' => array('created' => null));
  2044. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2045. $expected = array(
  2046. 'div' => array('class' => 'input date'),
  2047. 'label' => array('for' => 'ContactCreatedMonth'),
  2048. 'Created',
  2049. '/label',
  2050. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2051. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2052. $monthsRegex,
  2053. '/select', '-',
  2054. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2055. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2056. $daysRegex,
  2057. '/select', '-',
  2058. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2059. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2060. $yearsRegex,
  2061. '/select',
  2062. '/div'
  2063. );
  2064. $this->assertTags($result, $expected);
  2065. $this->Form->request->data = array('Contact' => array('created' => null));
  2066. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2067. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2068. $this->Form->request->data = array('Contact' => array('created' => null));
  2069. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2070. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2071. $result = $this->Form->input('Contact.created', array(
  2072. 'type' => 'date',
  2073. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2074. 'timeFormat' => 'NONE'
  2075. ));
  2076. $this->assertRegExp('/for\="created-month"/', $result);
  2077. }
  2078. /**
  2079. * Test generating checkboxes in a loop.
  2080. *
  2081. * @return void
  2082. */
  2083. public function testInputCheckboxesInLoop() {
  2084. for ($i = 1; $i < 5; $i++) {
  2085. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2086. $expected = array(
  2087. 'div' => array('class' => 'input checkbox'),
  2088. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2089. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2090. 'label' => array('for' => "Contact{$i}Email"),
  2091. 'Email',
  2092. '/label',
  2093. '/div'
  2094. );
  2095. $this->assertTags($result, $expected);
  2096. }
  2097. }
  2098. /**
  2099. * test input name with leading integer, ensure attributes are generated correctly.
  2100. *
  2101. * @return void
  2102. */
  2103. public function testInputWithLeadingInteger() {
  2104. $result = $this->Form->text('0.Node.title');
  2105. $expected = array(
  2106. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2107. );
  2108. $this->assertTags($result, $expected);
  2109. }
  2110. /**
  2111. * test form->input() with select type inputs.
  2112. *
  2113. * @return void
  2114. */
  2115. public function testInputSelectType() {
  2116. $result = $this->Form->input('email', array(
  2117. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2118. );
  2119. $expected = array(
  2120. 'div' => array('class' => 'input select'),
  2121. 'label' => array('for' => 'email'),
  2122. 'Email',
  2123. '/label',
  2124. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2125. array('option' => array('value' => '')),
  2126. '/option',
  2127. array('option' => array('value' => 'è')),
  2128. 'Firést',
  2129. '/option',
  2130. array('option' => array('value' => 'é')),
  2131. 'Secoènd',
  2132. '/option',
  2133. '/select',
  2134. '/div'
  2135. );
  2136. $this->assertTags($result, $expected);
  2137. $result = $this->Form->input('email', array(
  2138. 'options' => array('First', 'Second'), 'empty' => true)
  2139. );
  2140. $expected = array(
  2141. 'div' => array('class' => 'input select'),
  2142. 'label' => array('for' => 'email'),
  2143. 'Email',
  2144. '/label',
  2145. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2146. array('option' => array('value' => '')),
  2147. '/option',
  2148. array('option' => array('value' => '0')),
  2149. 'First',
  2150. '/option',
  2151. array('option' => array('value' => '1')),
  2152. 'Second',
  2153. '/option',
  2154. '/select',
  2155. '/div'
  2156. );
  2157. $this->assertTags($result, $expected);
  2158. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2159. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2160. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2161. $expected = array(
  2162. 'div' => array('class' => 'input select'),
  2163. 'label' => array('for' => 'ModelUserId'),
  2164. 'User',
  2165. '/label',
  2166. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2167. array('option' => array('value' => '')),
  2168. '/option',
  2169. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2170. 'good',
  2171. '/option',
  2172. array('option' => array('value' => 'other')),
  2173. 'bad',
  2174. '/option',
  2175. '/select',
  2176. '/div'
  2177. );
  2178. $this->assertTags($result, $expected);
  2179. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2180. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2181. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2182. $expected = array(
  2183. 'div' => array('class' => 'input select'),
  2184. 'label' => array('for' => 'ThingUserId'),
  2185. 'User',
  2186. '/label',
  2187. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2188. array('option' => array('value' => '')),
  2189. 'Some Empty',
  2190. '/option',
  2191. array('option' => array('value' => 'value')),
  2192. 'good',
  2193. '/option',
  2194. array('option' => array('value' => 'other')),
  2195. 'bad',
  2196. '/option',
  2197. '/select',
  2198. '/div'
  2199. );
  2200. $this->assertTags($result, $expected);
  2201. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2202. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2203. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input select'),
  2206. 'label' => array('for' => 'ThingUserId'),
  2207. 'User',
  2208. '/label',
  2209. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2210. array('option' => array('value' => '')),
  2211. 'Some Empty',
  2212. '/option',
  2213. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2214. 'good',
  2215. '/option',
  2216. array('option' => array('value' => 'other')),
  2217. 'bad',
  2218. '/option',
  2219. '/select',
  2220. '/div'
  2221. );
  2222. $this->assertTags($result, $expected);
  2223. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2224. $this->Form->request->data = array('User' => array('User' => array('value')));
  2225. $result = $this->Form->input('User.User', array('empty' => true));
  2226. $expected = array(
  2227. 'div' => array('class' => 'input select'),
  2228. 'label' => array('for' => 'UserUser'),
  2229. 'User',
  2230. '/label',
  2231. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2232. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2233. array('option' => array('value' => '')),
  2234. '/option',
  2235. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2236. 'good',
  2237. '/option',
  2238. array('option' => array('value' => 'other')),
  2239. 'bad',
  2240. '/option',
  2241. '/select',
  2242. '/div'
  2243. );
  2244. $this->assertTags($result, $expected);
  2245. $this->Form->data = array();
  2246. $result = $this->Form->input('Publisher.id', array(
  2247. 'label' => 'Publisher',
  2248. 'type' => 'select',
  2249. 'multiple' => 'checkbox',
  2250. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2251. ));
  2252. $expected = array(
  2253. array('div' => array('class' => 'input select')),
  2254. array('label' => array('for' => 'PublisherId')),
  2255. 'Publisher',
  2256. '/label',
  2257. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2258. array('div' => array('class' => 'checkbox')),
  2259. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2260. array('label' => array('for' => 'PublisherIdValue1')),
  2261. 'Label 1',
  2262. '/label',
  2263. '/div',
  2264. array('div' => array('class' => 'checkbox')),
  2265. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2266. array('label' => array('for' => 'PublisherIdValue2')),
  2267. 'Label 2',
  2268. '/label',
  2269. '/div',
  2270. '/div'
  2271. );
  2272. $this->assertTags($result, $expected);
  2273. }
  2274. /**
  2275. * test that input() and a non standard primary key makes a hidden input by default.
  2276. *
  2277. * @return void
  2278. */
  2279. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2280. $this->Form->create('User');
  2281. $this->Form->fieldset = array(
  2282. 'User' => array(
  2283. 'fields' => array(
  2284. 'model_id' => array('type' => 'integer')
  2285. ),
  2286. 'validates' => array(),
  2287. 'key' => 'model_id'
  2288. )
  2289. );
  2290. $result = $this->Form->input('model_id');
  2291. $expected = array(
  2292. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2293. );
  2294. $this->assertTags($result, $expected);
  2295. }
  2296. /**
  2297. * test that overriding the magic select type widget is possible
  2298. *
  2299. * @return void
  2300. */
  2301. public function testInputOverridingMagicSelectType() {
  2302. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2303. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2304. $expected = array(
  2305. 'div' => array('class' => 'input text'),
  2306. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2307. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2308. '/div'
  2309. );
  2310. $this->assertTags($result, $expected);
  2311. //Check that magic types still work for plural/singular vars
  2312. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2313. $result = $this->Form->input('Model.type');
  2314. $expected = array(
  2315. 'div' => array('class' => 'input select'),
  2316. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2317. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2318. array('option' => array('value' => 'value')), 'good', '/option',
  2319. array('option' => array('value' => 'other')), 'bad', '/option',
  2320. '/select',
  2321. '/div'
  2322. );
  2323. $this->assertTags($result, $expected);
  2324. }
  2325. /**
  2326. * Test that magic input() selects can easily be converted into radio types without error.
  2327. *
  2328. * @return void
  2329. */
  2330. public function testInputMagicSelectChangeToRadio() {
  2331. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2332. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2333. $this->assertRegExp('/input type="radio"/', $result);
  2334. }
  2335. /**
  2336. * fields with the same name as the model should work.
  2337. *
  2338. * @return void
  2339. */
  2340. public function testInputWithMatchingFieldAndModelName() {
  2341. $this->Form->create('User');
  2342. $this->Form->fieldset = array(
  2343. 'User' => array(
  2344. 'fields' => array(
  2345. 'User' => array('type' => 'text')
  2346. ),
  2347. 'validates' => array(),
  2348. 'key' => 'id'
  2349. )
  2350. );
  2351. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2352. $result = $this->Form->input('User', array('type' => 'text'));
  2353. $expected = array(
  2354. 'div' => array('class' => 'input text'),
  2355. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2356. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2357. '/div'
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * testFormInputs method
  2363. *
  2364. * test correct results from form::inputs().
  2365. *
  2366. * @return void
  2367. */
  2368. public function testFormInputs() {
  2369. $this->Form->create('Contact');
  2370. $result = $this->Form->inputs('The Legend');
  2371. $expected = array(
  2372. '<fieldset',
  2373. '<legend',
  2374. 'The Legend',
  2375. '/legend',
  2376. '*/fieldset',
  2377. );
  2378. $this->assertTags($result, $expected);
  2379. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2380. $expected = array(
  2381. 'fieldset' => array('class' => 'classy-stuff'),
  2382. '<legend',
  2383. 'Field of Dreams',
  2384. '/legend',
  2385. '*/fieldset'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $this->Form->create('Contact');
  2389. $this->Form->request['prefix'] = 'admin';
  2390. $this->Form->request['action'] = 'admin_edit';
  2391. $result = $this->Form->inputs();
  2392. $expected = array(
  2393. '<fieldset',
  2394. '<legend',
  2395. 'Edit Contact',
  2396. '/legend',
  2397. '*/fieldset',
  2398. );
  2399. $this->assertTags($result, $expected);
  2400. $this->Form->create('Contact');
  2401. $result = $this->Form->inputs(false);
  2402. $expected = array(
  2403. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2404. array('div' => array('class' => 'input text')),
  2405. '*/div',
  2406. array('div' => array('class' => 'input text')),
  2407. '*/div',
  2408. array('div' => array('class' => 'input text')),
  2409. '*/div',
  2410. array('div' => array('class' => 'input password')),
  2411. '*/div',
  2412. array('div' => array('class' => 'input date')),
  2413. '*/div',
  2414. array('div' => array('class' => 'input date')),
  2415. '*/div',
  2416. array('div' => array('class' => 'input datetime')),
  2417. '*/div',
  2418. array('div' => array('class' => 'input number')),
  2419. '*/div',
  2420. array('div' => array('class' => 'input select')),
  2421. '*/div',
  2422. );
  2423. $this->assertTags($result, $expected);
  2424. $this->Form->create('Contact');
  2425. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2426. $expected = array(
  2427. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2428. array('div' => array('class' => 'input text')),
  2429. '*/div',
  2430. array('div' => array('class' => 'input text')),
  2431. '*/div',
  2432. array('div' => array('class' => 'input text')),
  2433. '*/div',
  2434. array('div' => array('class' => 'input password')),
  2435. '*/div',
  2436. array('div' => array('class' => 'input date')),
  2437. '*/div',
  2438. array('div' => array('class' => 'input date')),
  2439. '*/div',
  2440. array('div' => array('class' => 'input datetime')),
  2441. '*/div',
  2442. array('div' => array('class' => 'input number')),
  2443. '*/div',
  2444. array('div' => array('class' => 'input select')),
  2445. '*/div',
  2446. );
  2447. $this->assertTags($result, $expected);
  2448. $this->Form->create('Contact');
  2449. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2450. $expected = array(
  2451. 'fieldset' => array(),
  2452. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2453. array('div' => array('class' => 'input text')),
  2454. '*/div',
  2455. array('div' => array('class' => 'input text')),
  2456. '*/div',
  2457. array('div' => array('class' => 'input text')),
  2458. '*/div',
  2459. array('div' => array('class' => 'input password')),
  2460. '*/div',
  2461. array('div' => array('class' => 'input date')),
  2462. '*/div',
  2463. array('div' => array('class' => 'input date')),
  2464. '*/div',
  2465. array('div' => array('class' => 'input datetime')),
  2466. '*/div',
  2467. array('div' => array('class' => 'input number')),
  2468. '*/div',
  2469. array('div' => array('class' => 'input select')),
  2470. '*/div',
  2471. '/fieldset'
  2472. );
  2473. $this->assertTags($result, $expected);
  2474. $this->Form->create('Contact');
  2475. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2476. $expected = array(
  2477. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2478. array('div' => array('class' => 'input text')),
  2479. '*/div',
  2480. array('div' => array('class' => 'input text')),
  2481. '*/div',
  2482. array('div' => array('class' => 'input text')),
  2483. '*/div',
  2484. array('div' => array('class' => 'input password')),
  2485. '*/div',
  2486. array('div' => array('class' => 'input date')),
  2487. '*/div',
  2488. array('div' => array('class' => 'input date')),
  2489. '*/div',
  2490. array('div' => array('class' => 'input datetime')),
  2491. '*/div',
  2492. array('div' => array('class' => 'input number')),
  2493. '*/div',
  2494. array('div' => array('class' => 'input select')),
  2495. '*/div',
  2496. );
  2497. $this->assertTags($result, $expected);
  2498. $this->Form->create('Contact');
  2499. $result = $this->Form->inputs('Hello');
  2500. $expected = array(
  2501. 'fieldset' => array(),
  2502. 'legend' => array(),
  2503. 'Hello',
  2504. '/legend',
  2505. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2506. array('div' => array('class' => 'input text')),
  2507. '*/div',
  2508. array('div' => array('class' => 'input text')),
  2509. '*/div',
  2510. array('div' => array('class' => 'input text')),
  2511. '*/div',
  2512. array('div' => array('class' => 'input password')),
  2513. '*/div',
  2514. array('div' => array('class' => 'input date')),
  2515. '*/div',
  2516. array('div' => array('class' => 'input date')),
  2517. '*/div',
  2518. array('div' => array('class' => 'input datetime')),
  2519. '*/div',
  2520. array('div' => array('class' => 'input number')),
  2521. '*/div',
  2522. array('div' => array('class' => 'input select')),
  2523. '*/div',
  2524. '/fieldset'
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. $this->Form->create('Contact');
  2528. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2529. $expected = array(
  2530. 'fieldset' => array(),
  2531. 'legend' => array(),
  2532. 'Hello',
  2533. '/legend',
  2534. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2535. array('div' => array('class' => 'input text')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input text')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input text')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input password')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input date')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input date')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input datetime')),
  2548. '*/div',
  2549. array('div' => array('class' => 'input number')),
  2550. '*/div',
  2551. array('div' => array('class' => 'input select')),
  2552. '*/div',
  2553. '/fieldset'
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. }
  2557. /**
  2558. * testSelectAsCheckbox method
  2559. *
  2560. * test multi-select widget with checkbox formatting.
  2561. *
  2562. * @return void
  2563. */
  2564. public function testSelectAsCheckbox() {
  2565. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2566. $expected = array(
  2567. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2568. array('div' => array('class' => 'checkbox')),
  2569. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2570. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2571. 'first',
  2572. '/label',
  2573. '/div',
  2574. array('div' => array('class' => 'checkbox')),
  2575. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2576. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2577. 'second',
  2578. '/label',
  2579. '/div',
  2580. array('div' => array('class' => 'checkbox')),
  2581. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2582. array('label' => array('for' => 'ModelMultiField2')),
  2583. 'third',
  2584. '/label',
  2585. '/div',
  2586. );
  2587. $this->assertTags($result, $expected);
  2588. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2589. $expected = array(
  2590. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2591. array('div' => array('class' => 'checkbox')),
  2592. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2593. array('label' => array('for' => 'ModelMultiField12')),
  2594. 'half',
  2595. '/label',
  2596. '/div',
  2597. );
  2598. $this->assertTags($result, $expected);
  2599. }
  2600. /**
  2601. * testLabel method
  2602. *
  2603. * test label generation.
  2604. *
  2605. * @return void
  2606. */
  2607. public function testLabel() {
  2608. $this->Form->text('Person.name');
  2609. $result = $this->Form->label();
  2610. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2611. $this->Form->text('Person.name');
  2612. $result = $this->Form->label();
  2613. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2614. $result = $this->Form->label('Person.first_name');
  2615. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2616. $result = $this->Form->label('Person.first_name', 'Your first name');
  2617. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2618. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2619. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2620. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2621. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2622. $result = $this->Form->label('Person.first_name', '');
  2623. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2624. $result = $this->Form->label('Person.2.name', '');
  2625. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2626. }
  2627. /**
  2628. * testTextbox method
  2629. *
  2630. * test textbox element generation
  2631. *
  2632. * @return void
  2633. */
  2634. public function testTextbox() {
  2635. $result = $this->Form->text('Model.field');
  2636. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2637. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2638. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2639. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2640. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2641. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2642. $result = $this->Form->text('Model.text');
  2643. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2644. $Contact = ClassRegistry::getObject('Contact');
  2645. $Contact->validationErrors['text'] = array(true);
  2646. $this->Form->request->data['Contact']['text'] = 'test';
  2647. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2648. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2649. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2650. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2651. $expected = array(
  2652. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2653. );
  2654. $this->assertTags($result, $expected);
  2655. }
  2656. /**
  2657. * testDefaultValue method
  2658. *
  2659. * Test default value setting
  2660. *
  2661. * @return void
  2662. */
  2663. public function testDefaultValue() {
  2664. $this->Form->request->data['Model']['field'] = 'test';
  2665. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2666. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2667. unset($this->Form->request->data['Model']['field']);
  2668. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2669. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2670. }
  2671. /**
  2672. * testCheckboxDefaultValue method
  2673. *
  2674. * Test default value setting on checkbox() method
  2675. *
  2676. * @return void
  2677. */
  2678. public function testCheckboxDefaultValue() {
  2679. $this->Form->request->data['Model']['field'] = false;
  2680. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2681. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2682. unset($this->Form->request->data['Model']['field']);
  2683. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2684. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2685. $this->Form->request->data['Model']['field'] = true;
  2686. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2687. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2688. unset($this->Form->request->data['Model']['field']);
  2689. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2690. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2691. }
  2692. /**
  2693. * testError method
  2694. *
  2695. * Test field error generation
  2696. *
  2697. * @return void
  2698. */
  2699. public function testError() {
  2700. $Contact = ClassRegistry::getObject('Contact');
  2701. $Contact->validationErrors['field'] = array(1);
  2702. $result = $this->Form->error('Contact.field');
  2703. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2704. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2705. $this->assertEquals('Error in field Field', $result);
  2706. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2707. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2708. $this->assertEquals('This field contains invalid input', $result);
  2709. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2710. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2711. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2712. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2713. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2714. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2715. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2716. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2717. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2718. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2719. $this->assertEquals('<strong>Badness!</strong>', $result);
  2720. $Contact->validationErrors['field'] = array("email");
  2721. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2722. $expected = array(
  2723. 'div' => array('class' => 'field-error'),
  2724. 'No good!',
  2725. '/div'
  2726. );
  2727. $this->assertTags($result, $expected);
  2728. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2729. $result = $this->Form->error('Contact.field', array(
  2730. 'notEmpty' => 'Cannot be empty',
  2731. 'email' => 'No good!'
  2732. ));
  2733. $expected = array(
  2734. 'div' => array('class' => 'error-message'),
  2735. 'ul' => array(),
  2736. '<li', 'Cannot be empty', '/li',
  2737. '<li', 'No good!', '/li',
  2738. '<li', 'Something else', '/li',
  2739. '/ul',
  2740. '/div'
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. /** Testing error messages list options **/
  2744. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2745. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2746. $expected = array(
  2747. 'div' => array('class' => 'error-message'),
  2748. 'ol' => array(),
  2749. '<li', 'notEmpty', '/li',
  2750. '<li', 'email', '/li',
  2751. '/ol',
  2752. '/div'
  2753. );
  2754. $this->assertTags($result, $expected);
  2755. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2756. $expected = array(
  2757. 'div' => array('class' => 'error-message'),
  2758. 'ol' => array(),
  2759. '<li', 'notEmpty', '/li',
  2760. '<li', 'email', '/li',
  2761. '/ol',
  2762. '/div'
  2763. );
  2764. $this->assertTags($result, $expected);
  2765. $result = $this->Form->error('Contact.field', null, array(
  2766. 'listOptions' => array(
  2767. 'class' => 'ul-class',
  2768. 'itemOptions' => array(
  2769. 'class' => 'li-class'
  2770. )
  2771. )
  2772. ));
  2773. $expected = array(
  2774. 'div' => array('class' => 'error-message'),
  2775. 'ul' => array('class' => 'ul-class'),
  2776. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2777. array('li' => array('class' => 'li-class')), 'email', '/li',
  2778. '/ul',
  2779. '/div'
  2780. );
  2781. $this->assertTags($result, $expected);
  2782. }
  2783. /**
  2784. * test error options when using form->input();
  2785. *
  2786. * @return void
  2787. */
  2788. public function testInputErrorEscape() {
  2789. $this->Form->create('ValidateProfile');
  2790. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2791. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2792. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2793. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2794. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2795. $this->assertRegExp('/required<br>/', $result);
  2796. }
  2797. /**
  2798. * testPassword method
  2799. *
  2800. * Test password element generation
  2801. *
  2802. * @return void
  2803. */
  2804. public function testPassword() {
  2805. $Contact = ClassRegistry::getObject('Contact');
  2806. $result = $this->Form->password('Contact.field');
  2807. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2808. $Contact->validationErrors['passwd'] = 1;
  2809. $this->Form->request->data['Contact']['passwd'] = 'test';
  2810. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2811. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2812. }
  2813. /**
  2814. * testRadio method
  2815. *
  2816. * Test radio element set generation
  2817. *
  2818. * @return void
  2819. */
  2820. public function testRadio() {
  2821. $result = $this->Form->radio('Model.field', array('option A'));
  2822. $expected = array(
  2823. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2824. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2825. 'label' => array('for' => 'ModelField0'),
  2826. 'option A',
  2827. '/label'
  2828. );
  2829. $this->assertTags($result, $expected);
  2830. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2831. $expected = array(
  2832. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2833. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2834. 'label' => array('for' => 'ModelField12'),
  2835. 'half',
  2836. '/label'
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2840. $expected = array(
  2841. 'fieldset' => array(),
  2842. 'legend' => array(),
  2843. 'Field',
  2844. '/legend',
  2845. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2846. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2847. array('label' => array('for' => 'ModelField0')),
  2848. 'option A',
  2849. '/label',
  2850. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2851. array('label' => array('for' => 'ModelField1')),
  2852. 'option B',
  2853. '/label',
  2854. '/fieldset'
  2855. );
  2856. $this->assertTags($result, $expected);
  2857. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2858. $expected = array(
  2859. 'fieldset' => array(),
  2860. 'legend' => array(),
  2861. 'Field',
  2862. '/legend',
  2863. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2864. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2865. array('label' => array('for' => 'ModelField0')),
  2866. 'option A',
  2867. '/label',
  2868. 'br' => array(),
  2869. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2870. array('label' => array('for' => 'ModelField1')),
  2871. 'option B',
  2872. '/label',
  2873. '/fieldset'
  2874. );
  2875. $this->assertTags($result, $expected);
  2876. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  2877. $expected = array(
  2878. 'fieldset' => array(),
  2879. 'legend' => array(),
  2880. 'Field',
  2881. '/legend',
  2882. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
  2883. array('label' => array('for' => 'ModelField1')),
  2884. 'Yes',
  2885. '/label',
  2886. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2887. array('label' => array('for' => 'ModelField0')),
  2888. 'No',
  2889. '/label',
  2890. '/fieldset'
  2891. );
  2892. $this->assertTags($result, $expected);
  2893. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  2894. $expected = array(
  2895. 'fieldset' => array(),
  2896. 'legend' => array(),
  2897. 'Field',
  2898. '/legend',
  2899. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2900. array('label' => array('for' => 'ModelField1')),
  2901. 'Yes',
  2902. '/label',
  2903. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  2904. array('label' => array('for' => 'ModelField0')),
  2905. 'No',
  2906. '/label',
  2907. '/fieldset'
  2908. );
  2909. $this->assertTags($result, $expected);
  2910. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  2911. $expected = array(
  2912. 'fieldset' => array(),
  2913. 'legend' => array(),
  2914. 'Field',
  2915. '/legend',
  2916. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2917. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2918. array('label' => array('for' => 'ModelField1')),
  2919. 'Yes',
  2920. '/label',
  2921. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2922. array('label' => array('for' => 'ModelField0')),
  2923. 'No',
  2924. '/label',
  2925. '/fieldset'
  2926. );
  2927. $this->assertTags($result, $expected);
  2928. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  2929. $expected = array(
  2930. 'fieldset' => array(),
  2931. 'legend' => array(),
  2932. 'Field',
  2933. '/legend',
  2934. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2935. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2936. array('label' => array('for' => 'ModelField1')),
  2937. 'Yes',
  2938. '/label',
  2939. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2940. array('label' => array('for' => 'ModelField0')),
  2941. 'No',
  2942. '/label',
  2943. '/fieldset'
  2944. );
  2945. $this->assertTags($result, $expected);
  2946. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2947. $expected = array(
  2948. 'div' => array('class' => 'input radio'),
  2949. 'fieldset' => array(),
  2950. 'legend' => array(),
  2951. 'Legend title',
  2952. '/legend',
  2953. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2954. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2955. array('label' => array('for' => 'NewsletterSubscribe0')),
  2956. 'Unsubscribe',
  2957. '/label',
  2958. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2959. array('label' => array('for' => 'NewsletterSubscribe1')),
  2960. 'Subscribe',
  2961. '/label',
  2962. '/fieldset',
  2963. '/div'
  2964. );
  2965. $this->assertTags($result, $expected);
  2966. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2967. $expected = array(
  2968. 'div' => array('class' => 'input radio'),
  2969. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2970. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2971. array('label' => array('for' => 'NewsletterSubscribe0')),
  2972. 'Unsubscribe',
  2973. '/label',
  2974. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2975. array('label' => array('for' => 'NewsletterSubscribe1')),
  2976. 'Subscribe',
  2977. '/label',
  2978. '/div'
  2979. );
  2980. $this->assertTags($result, $expected);
  2981. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2982. $expected = array(
  2983. 'div' => array('class' => 'input radio'),
  2984. 'fieldset' => array(),
  2985. 'legend' => array(),
  2986. 'Legend title',
  2987. '/legend',
  2988. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2989. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2990. 'Unsubscribe',
  2991. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2992. 'Subscribe',
  2993. '/fieldset',
  2994. '/div'
  2995. );
  2996. $this->assertTags($result, $expected);
  2997. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2998. $expected = array(
  2999. 'div' => array('class' => 'input radio'),
  3000. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3001. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3002. 'Unsubscribe',
  3003. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3004. 'Subscribe',
  3005. '/div'
  3006. );
  3007. $this->assertTags($result, $expected);
  3008. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3009. $expected = array(
  3010. 'div' => array('class' => 'input radio'),
  3011. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3012. 'Unsubscribe',
  3013. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3014. 'Subscribe',
  3015. '/div'
  3016. );
  3017. $this->assertTags($result, $expected);
  3018. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3019. $expected = array(
  3020. 'fieldset' => array(),
  3021. 'legend' => array(),
  3022. 'Gender',
  3023. '/legend',
  3024. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3025. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3026. array('label' => array('for' => 'EmployeeGenderMale')),
  3027. 'Male',
  3028. '/label',
  3029. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3030. array('label' => array('for' => 'EmployeeGenderFemale')),
  3031. 'Female',
  3032. '/label',
  3033. '/fieldset',
  3034. );
  3035. $this->assertTags($result, $expected);
  3036. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3037. $expected = array(
  3038. 'fieldset' => array(),
  3039. 'legend' => array(),
  3040. 'Gender',
  3041. '/legend',
  3042. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3043. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3044. array('label' => array('for' => 'OfficerGenderMale')),
  3045. 'Male',
  3046. '/label',
  3047. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3048. array('label' => array('for' => 'OfficerGenderFemale')),
  3049. 'Female',
  3050. '/label',
  3051. '/fieldset',
  3052. );
  3053. $this->assertTags($result, $expected);
  3054. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3055. $expected = array(
  3056. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3057. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3058. 'label' => array('for' => 'Contact1Imrequired0'),
  3059. 'option A',
  3060. '/label'
  3061. );
  3062. $this->assertTags($result, $expected);
  3063. $result = $this->Form->radio('Model.1.field', array('option A'));
  3064. $expected = array(
  3065. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3066. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3067. 'label' => array('for' => 'Model1Field0'),
  3068. 'option A',
  3069. '/label'
  3070. );
  3071. $this->assertTags($result, $expected);
  3072. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3073. $expected = array(
  3074. 'fieldset' => array(),
  3075. 'legend' => array(),
  3076. 'Field',
  3077. '/legend',
  3078. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3079. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3080. array('label' => array('for' => 'ModelField0')),
  3081. 'option A',
  3082. '/label',
  3083. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3084. array('label' => array('for' => 'ModelField1')),
  3085. 'option B',
  3086. '/label',
  3087. '/fieldset'
  3088. );
  3089. $this->assertTags($result, $expected);
  3090. $result = $this->Form->radio(
  3091. 'Model.field',
  3092. array('option A', 'option B'),
  3093. array('between' => 'I am between')
  3094. );
  3095. $expected = array(
  3096. 'fieldset' => array(),
  3097. 'legend' => array(),
  3098. 'Field',
  3099. '/legend',
  3100. 'I am between',
  3101. 'input' => array(
  3102. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3103. 'value' => '', 'id' => 'ModelField_'
  3104. ),
  3105. array('input' => array(
  3106. 'type' => 'radio', 'name' => 'data[Model][field]',
  3107. 'value' => '0', 'id' => 'ModelField0'
  3108. )),
  3109. array('label' => array('for' => 'ModelField0')),
  3110. 'option A',
  3111. '/label',
  3112. array('input' => array(
  3113. 'type' => 'radio', 'name' => 'data[Model][field]',
  3114. 'value' => '1', 'id' => 'ModelField1'
  3115. )),
  3116. array('label' => array('for' => 'ModelField1')),
  3117. 'option B',
  3118. '/label',
  3119. '/fieldset'
  3120. );
  3121. $this->assertTags($result, $expected);
  3122. }
  3123. /**
  3124. * test disabled radio options
  3125. *
  3126. * @return void
  3127. */
  3128. public function testRadioDisabled() {
  3129. $result = $this->Form->radio(
  3130. 'Model.field',
  3131. array('option A', 'option B'),
  3132. array('disabled' => array('option A'), 'value' => 'option A')
  3133. );
  3134. $expected = array(
  3135. 'fieldset' => array(),
  3136. 'legend' => array(),
  3137. 'Field',
  3138. '/legend',
  3139. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3140. array('label' => array('for' => 'ModelField0')),
  3141. 'option A',
  3142. '/label',
  3143. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3144. array('label' => array('for' => 'ModelField1')),
  3145. 'option B',
  3146. '/label',
  3147. '/fieldset'
  3148. );
  3149. $this->assertTags($result, $expected);
  3150. $result = $this->Form->radio(
  3151. 'Model.field',
  3152. array('option A', 'option B'),
  3153. array('disabled' => true, 'value' => 'option A')
  3154. );
  3155. $expected = array(
  3156. 'fieldset' => array(),
  3157. 'legend' => array(),
  3158. 'Field',
  3159. '/legend',
  3160. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3161. array('label' => array('for' => 'ModelField0')),
  3162. 'option A',
  3163. '/label',
  3164. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3165. array('label' => array('for' => 'ModelField1')),
  3166. 'option B',
  3167. '/label',
  3168. '/fieldset'
  3169. );
  3170. $this->assertTags($result, $expected);
  3171. $result = $this->Form->radio(
  3172. 'Model.field',
  3173. array('option A', 'option B'),
  3174. array('disabled' => 'disabled', 'value' => 'option A')
  3175. );
  3176. $expected = array(
  3177. 'fieldset' => array(),
  3178. 'legend' => array(),
  3179. 'Field',
  3180. '/legend',
  3181. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3182. array('label' => array('for' => 'ModelField0')),
  3183. 'option A',
  3184. '/label',
  3185. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3186. array('label' => array('for' => 'ModelField1')),
  3187. 'option B',
  3188. '/label',
  3189. '/fieldset'
  3190. );
  3191. $this->assertTags($result, $expected);
  3192. }
  3193. /**
  3194. * test disabling the hidden input for radio buttons
  3195. *
  3196. * @return void
  3197. */
  3198. public function testRadioHiddenInputDisabling() {
  3199. $result = $this->Form->input('Model.1.field', array(
  3200. 'type' => 'radio',
  3201. 'options' => array('option A'),
  3202. 'hiddenField' => false
  3203. )
  3204. );
  3205. $expected = array(
  3206. 'div' => array('class' => 'input radio'),
  3207. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3208. 'label' => array('for' => 'Model1Field0'),
  3209. 'option A',
  3210. '/label',
  3211. '/div'
  3212. );
  3213. $this->assertTags($result, $expected);
  3214. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3215. $expected = array(
  3216. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3217. 'label' => array('for' => 'Model1Field0'),
  3218. 'option A',
  3219. '/label'
  3220. );
  3221. $this->assertTags($result, $expected);
  3222. }
  3223. /**
  3224. * test adding an empty option for radio buttons
  3225. *
  3226. * @return void
  3227. */
  3228. public function testRadioAddEmptyOption() {
  3229. $result = $this->Form->input('Model.1.field', array(
  3230. 'type' => 'radio',
  3231. 'options' => array('option A'),
  3232. 'empty' => true,
  3233. 'hiddenField' => false
  3234. ));
  3235. $expected = array(
  3236. 'div' => array('class' => 'input radio'),
  3237. 'fieldset' => array(),
  3238. 'legend' => array(),
  3239. 'Field',
  3240. '/legend',
  3241. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3242. array('label' => array('for' => 'Model1Field')),
  3243. __('empty'),
  3244. '/label',
  3245. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3246. array('label' => array('for' => 'Model1Field0')),
  3247. 'option A',
  3248. '/label',
  3249. '/fieldset',
  3250. '/div'
  3251. );
  3252. $this->assertTags($result, $expected);
  3253. $result = $this->Form->input('Model.1.field', array(
  3254. 'type' => 'radio',
  3255. 'options' => array('option A'),
  3256. 'empty' => 'CustomEmptyLabel',
  3257. 'hiddenField' => false
  3258. ));
  3259. $expected = array(
  3260. 'div' => array('class' => 'input radio'),
  3261. 'fieldset' => array(),
  3262. 'legend' => array(),
  3263. 'Field',
  3264. '/legend',
  3265. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3266. array('label' => array('for' => 'Model1Field')),
  3267. 'CustomEmptyLabel',
  3268. '/label',
  3269. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3270. array('label' => array('for' => 'Model1Field0')),
  3271. 'option A',
  3272. '/label',
  3273. '/fieldset',
  3274. '/div'
  3275. );
  3276. $this->assertTags($result, $expected);
  3277. $result = $this->Form->input('Model.1.field', array(
  3278. 'type' => 'radio',
  3279. 'options' => array('option A'),
  3280. 'empty' => false,
  3281. 'hiddenField' => false
  3282. ));
  3283. $this->assertTextNotContains('"Model1Field"', $result);
  3284. }
  3285. /**
  3286. * testSelect method
  3287. *
  3288. * Test select element generation.
  3289. *
  3290. * @return void
  3291. */
  3292. public function testSelect() {
  3293. $result = $this->Form->select('Model.field', array());
  3294. $expected = array(
  3295. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3296. array('option' => array('value' => '')),
  3297. '/option',
  3298. '/select'
  3299. );
  3300. $this->assertTags($result, $expected);
  3301. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3302. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3303. $expected = array(
  3304. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3305. array('option' => array('value' => '')),
  3306. '/option',
  3307. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3308. 'good',
  3309. '/option',
  3310. array('option' => array('value' => 'other')),
  3311. 'bad',
  3312. '/option',
  3313. '/select'
  3314. );
  3315. $this->assertTags($result, $expected);
  3316. $this->Form->request->data = array();
  3317. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3318. $expected = array(
  3319. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3320. array('option' => array('value' => '')),
  3321. '/option',
  3322. array('option' => array('value' => 'value')),
  3323. 'good',
  3324. '/option',
  3325. array('option' => array('value' => 'other')),
  3326. 'bad',
  3327. '/option',
  3328. '/select'
  3329. );
  3330. $this->assertTags($result, $expected);
  3331. $result = $this->Form->select(
  3332. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3333. array('empty' => false)
  3334. );
  3335. $expected = array(
  3336. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3337. array('option' => array('value' => 'first')),
  3338. 'first &quot;html&quot; &lt;chars&gt;',
  3339. '/option',
  3340. array('option' => array('value' => 'second')),
  3341. 'value',
  3342. '/option',
  3343. '/select'
  3344. );
  3345. $this->assertTags($result, $expected);
  3346. $result = $this->Form->select(
  3347. 'Model.field',
  3348. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3349. array('escape' => false, 'empty' => false)
  3350. );
  3351. $expected = array(
  3352. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3353. array('option' => array('value' => 'first')),
  3354. 'first "html" <chars>',
  3355. '/option',
  3356. array('option' => array('value' => 'second')),
  3357. 'value',
  3358. '/option',
  3359. '/select'
  3360. );
  3361. $this->assertTags($result, $expected);
  3362. $options = array(
  3363. array('value' => 'first', 'name' => 'First'),
  3364. array('value' => 'first', 'name' => 'Another First'),
  3365. );
  3366. $result = $this->Form->select(
  3367. 'Model.field',
  3368. $options,
  3369. array('escape' => false, 'empty' => false)
  3370. );
  3371. $expected = array(
  3372. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3373. array('option' => array('value' => 'first')),
  3374. 'First',
  3375. '/option',
  3376. array('option' => array('value' => 'first')),
  3377. 'Another First',
  3378. '/option',
  3379. '/select'
  3380. );
  3381. $this->assertTags($result, $expected);
  3382. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3383. $result = $this->Form->select(
  3384. 'Model.contact_id',
  3385. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3386. array('escape' => false, 'empty' => 'pick something')
  3387. );
  3388. $expected = array(
  3389. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3390. array('option' => array('value' => '')), 'pick something', '/option',
  3391. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3392. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3393. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3394. '/select'
  3395. );
  3396. $this->assertTags($result, $expected);
  3397. $this->Form->request->data['Model']['field'] = 0;
  3398. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3399. $expected = array(
  3400. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3401. array('option' => array('value' => '')), '/option',
  3402. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3403. array('option' => array('value' => '1')), 'Yes', '/option',
  3404. '/select'
  3405. );
  3406. $this->assertTags($result, $expected);
  3407. }
  3408. /**
  3409. * test that select() with optiongroups listens to the escape param.
  3410. *
  3411. * @return void
  3412. */
  3413. public function testSelectOptionGroupEscaping() {
  3414. $options = array(
  3415. '>< Key' => array(
  3416. 1 => 'One',
  3417. 2 => 'Two'
  3418. )
  3419. );
  3420. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3421. $expected = array(
  3422. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3423. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3424. array('option' => array('value' => '1')), 'One', '/option',
  3425. array('option' => array('value' => '2')), 'Two', '/option',
  3426. '/optgroup',
  3427. '/select'
  3428. );
  3429. $this->assertTags($result, $expected);
  3430. $options = array(
  3431. '>< Key' => array(
  3432. 1 => 'One',
  3433. 2 => 'Two'
  3434. )
  3435. );
  3436. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3437. $expected = array(
  3438. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3439. 'optgroup' => array('label' => '>< Key'),
  3440. array('option' => array('value' => '1')), 'One', '/option',
  3441. array('option' => array('value' => '2')), 'Two', '/option',
  3442. '/optgroup',
  3443. '/select'
  3444. );
  3445. $this->assertTags($result, $expected);
  3446. }
  3447. /**
  3448. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3449. *
  3450. * @return void
  3451. */
  3452. public function testSelectWithNullAttributes() {
  3453. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3454. $expected = array(
  3455. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3456. array('option' => array('value' => '0')),
  3457. 'first',
  3458. '/option',
  3459. array('option' => array('value' => '1')),
  3460. 'second',
  3461. '/option',
  3462. '/select'
  3463. );
  3464. $this->assertTags($result, $expected);
  3465. }
  3466. /**
  3467. * testNestedSelect method
  3468. *
  3469. * test select element generation with optgroups
  3470. *
  3471. * @return void
  3472. */
  3473. public function testNestedSelect() {
  3474. $result = $this->Form->select(
  3475. 'Model.field',
  3476. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3477. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3478. )), array('empty' => false)
  3479. );
  3480. $expected = array(
  3481. 'select' => array('name' => 'data[Model][field]',
  3482. 'id' => 'ModelField'),
  3483. array('option' => array('value' => 1)),
  3484. 'One',
  3485. '/option',
  3486. array('option' => array('value' => 2)),
  3487. 'Two',
  3488. '/option',
  3489. array('optgroup' => array('label' => 'Three')),
  3490. array('option' => array('value' => 4)),
  3491. 'Four',
  3492. '/option',
  3493. array('option' => array('value' => 5)),
  3494. 'Five',
  3495. '/option',
  3496. '/optgroup',
  3497. '/select'
  3498. );
  3499. $this->assertTags($result, $expected);
  3500. $result = $this->Form->select(
  3501. 'Model.field',
  3502. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3503. array('showParents' => true, 'empty' => false)
  3504. );
  3505. $expected = array(
  3506. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3507. array('option' => array('value' => 1)),
  3508. 'One',
  3509. '/option',
  3510. array('option' => array('value' => 2)),
  3511. 'Two',
  3512. '/option',
  3513. array('optgroup' => array('label' => 'Three')),
  3514. array('option' => array('value' => 3)),
  3515. 'Three',
  3516. '/option',
  3517. array('option' => array('value' => 4)),
  3518. 'Four',
  3519. '/option',
  3520. '/optgroup',
  3521. '/select'
  3522. );
  3523. $this->assertTags($result, $expected);
  3524. }
  3525. /**
  3526. * testSelectMultiple method
  3527. *
  3528. * test generation of multiple select elements
  3529. *
  3530. * @return void
  3531. */
  3532. public function testSelectMultiple() {
  3533. $options = array('first', 'second', 'third');
  3534. $result = $this->Form->select(
  3535. 'Model.multi_field', $options, array('multiple' => true)
  3536. );
  3537. $expected = array(
  3538. 'input' => array(
  3539. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3540. ),
  3541. 'select' => array(
  3542. 'name' => 'data[Model][multi_field][]',
  3543. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3544. ),
  3545. array('option' => array('value' => '0')),
  3546. 'first',
  3547. '/option',
  3548. array('option' => array('value' => '1')),
  3549. 'second',
  3550. '/option',
  3551. array('option' => array('value' => '2')),
  3552. 'third',
  3553. '/option',
  3554. '/select'
  3555. );
  3556. $this->assertTags($result, $expected);
  3557. $result = $this->Form->select(
  3558. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3559. );
  3560. $expected = array(
  3561. 'input' => array(
  3562. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3563. ),
  3564. 'select' => array(
  3565. 'name' => 'data[Model][multi_field][]',
  3566. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3567. ),
  3568. array('option' => array('value' => '0')),
  3569. 'first',
  3570. '/option',
  3571. array('option' => array('value' => '1')),
  3572. 'second',
  3573. '/option',
  3574. array('option' => array('value' => '2')),
  3575. 'third',
  3576. '/option',
  3577. '/select'
  3578. );
  3579. $this->assertTags($result, $expected);
  3580. $result = $this->Form->select(
  3581. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3582. );
  3583. $expected = array(
  3584. 'input' => array(
  3585. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3586. ),
  3587. 'select' => array(
  3588. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3589. 'multiple' => 'multiple'
  3590. ),
  3591. array('option' => array('value' => '0', 'selected' => 'selected')),
  3592. 'first',
  3593. '/option',
  3594. array('option' => array('value' => '1', 'selected' => 'selected')),
  3595. 'second',
  3596. '/option',
  3597. array('option' => array('value' => '2')),
  3598. 'third',
  3599. '/option',
  3600. '/select'
  3601. );
  3602. $this->assertTags($result, $expected);
  3603. $result = $this->Form->select(
  3604. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3605. );
  3606. $expected = array(
  3607. 'select' => array(
  3608. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3609. ),
  3610. array('option' => array('value' => '0', 'selected' => 'selected')),
  3611. 'first',
  3612. '/option',
  3613. array('option' => array('value' => '1', 'selected' => 'selected')),
  3614. 'second',
  3615. '/option',
  3616. array('option' => array('value' => '2')),
  3617. 'third',
  3618. '/option',
  3619. '/select'
  3620. );
  3621. $this->assertTags($result, $expected);
  3622. }
  3623. /**
  3624. * test generation of habtm select boxes.
  3625. *
  3626. * @return void
  3627. */
  3628. public function testHabtmSelectBox() {
  3629. $this->View->viewVars['contactTags'] = array(
  3630. 1 => 'blue',
  3631. 2 => 'red',
  3632. 3 => 'green'
  3633. );
  3634. $this->Form->request->data = array(
  3635. 'Contact' => array(),
  3636. 'ContactTag' => array(
  3637. array(
  3638. 'id' => 1,
  3639. 'name' => 'blue'
  3640. ),
  3641. array(
  3642. 'id' => 3,
  3643. 'name' => 'green'
  3644. )
  3645. )
  3646. );
  3647. $this->Form->create('Contact');
  3648. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3649. $expected = array(
  3650. 'input' => array(
  3651. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3652. ),
  3653. 'select' => array(
  3654. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3655. 'multiple' => 'multiple'
  3656. ),
  3657. array('option' => array('value' => '1', 'selected' => 'selected')),
  3658. 'blue',
  3659. '/option',
  3660. array('option' => array('value' => '2')),
  3661. 'red',
  3662. '/option',
  3663. array('option' => array('value' => '3', 'selected' => 'selected')),
  3664. 'green',
  3665. '/option',
  3666. '/select'
  3667. );
  3668. $this->assertTags($result, $expected);
  3669. }
  3670. /**
  3671. * test generation of multi select elements in checkbox format
  3672. *
  3673. * @return void
  3674. */
  3675. public function testSelectMultipleCheckboxes() {
  3676. $result = $this->Form->select(
  3677. 'Model.multi_field',
  3678. array('first', 'second', 'third'),
  3679. array('multiple' => 'checkbox')
  3680. );
  3681. $expected = array(
  3682. 'input' => array(
  3683. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3684. ),
  3685. array('div' => array('class' => 'checkbox')),
  3686. array('input' => array(
  3687. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3688. 'value' => '0', 'id' => 'ModelMultiField0'
  3689. )),
  3690. array('label' => array('for' => 'ModelMultiField0')),
  3691. 'first',
  3692. '/label',
  3693. '/div',
  3694. array('div' => array('class' => 'checkbox')),
  3695. array('input' => array(
  3696. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3697. 'value' => '1', 'id' => 'ModelMultiField1'
  3698. )),
  3699. array('label' => array('for' => 'ModelMultiField1')),
  3700. 'second',
  3701. '/label',
  3702. '/div',
  3703. array('div' => array('class' => 'checkbox')),
  3704. array('input' => array(
  3705. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3706. 'value' => '2', 'id' => 'ModelMultiField2'
  3707. )),
  3708. array('label' => array('for' => 'ModelMultiField2')),
  3709. 'third',
  3710. '/label',
  3711. '/div'
  3712. );
  3713. $this->assertTags($result, $expected);
  3714. $result = $this->Form->select(
  3715. 'Model.multi_field',
  3716. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3717. array('multiple' => 'checkbox')
  3718. );
  3719. $expected = array(
  3720. 'input' => array(
  3721. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3722. ),
  3723. array('div' => array('class' => 'checkbox')),
  3724. array('input' => array(
  3725. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3726. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3727. )),
  3728. array('label' => array('for' => 'ModelMultiFieldA')),
  3729. 'first',
  3730. '/label',
  3731. '/div',
  3732. array('div' => array('class' => 'checkbox')),
  3733. array('input' => array(
  3734. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3735. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3736. )),
  3737. array('label' => array('for' => 'ModelMultiFieldB')),
  3738. 'second',
  3739. '/label',
  3740. '/div',
  3741. array('div' => array('class' => 'checkbox')),
  3742. array('input' => array(
  3743. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3744. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3745. )),
  3746. array('label' => array('for' => 'ModelMultiFieldC')),
  3747. 'third',
  3748. '/label',
  3749. '/div'
  3750. );
  3751. $this->assertTags($result, $expected);
  3752. $result = $this->Form->select(
  3753. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3754. );
  3755. $expected = array(
  3756. 'input' => array(
  3757. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3758. ),
  3759. array('div' => array('class' => 'checkbox')),
  3760. array('input' => array(
  3761. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3762. 'value' => '1', 'id' => 'ModelMultiField1'
  3763. )),
  3764. array('label' => array('for' => 'ModelMultiField1')),
  3765. 'first',
  3766. '/label',
  3767. '/div'
  3768. );
  3769. $this->assertTags($result, $expected);
  3770. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3771. $result = $this->Form->select(
  3772. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3773. );
  3774. $expected = array(
  3775. 'input' => array(
  3776. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3777. ),
  3778. array('div' => array('class' => 'checkbox')),
  3779. array('input' => array(
  3780. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3781. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3782. )),
  3783. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3784. 'first',
  3785. '/label',
  3786. '/div',
  3787. array('div' => array('class' => 'checkbox')),
  3788. array('input' => array(
  3789. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3790. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3791. )),
  3792. array('label' => array('for' => 'ModelTagsArray')),
  3793. 'Array',
  3794. '/label',
  3795. '/div'
  3796. );
  3797. $this->assertTags($result, $expected);
  3798. }
  3799. /**
  3800. * test multiple checkboxes with div styles.
  3801. *
  3802. * @return void
  3803. */
  3804. public function testSelectMultipleCheckboxDiv() {
  3805. $result = $this->Form->select(
  3806. 'Model.tags',
  3807. array('first', 'second'),
  3808. array('multiple' => 'checkbox', 'class' => 'my-class')
  3809. );
  3810. $expected = array(
  3811. 'input' => array(
  3812. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3813. ),
  3814. array('div' => array('class' => 'my-class')),
  3815. array('input' => array(
  3816. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3817. 'value' => '0', 'id' => 'ModelTags0'
  3818. )),
  3819. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3820. '/div',
  3821. array('div' => array('class' => 'my-class')),
  3822. array('input' => array(
  3823. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3824. 'value' => '1', 'id' => 'ModelTags1'
  3825. )),
  3826. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3827. '/div'
  3828. );
  3829. $this->assertTags($result, $expected);
  3830. $result = $this->Form->input('Model.tags', array(
  3831. 'options' => array('first', 'second'),
  3832. 'multiple' => 'checkbox',
  3833. 'class' => 'my-class',
  3834. 'div' => false,
  3835. 'label' => false
  3836. ));
  3837. $this->assertTags($result, $expected);
  3838. $Contact = ClassRegistry::getObject('Contact');
  3839. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3840. $result = $this->Form->input('Contact.tags', array(
  3841. 'options' => array('one'),
  3842. 'multiple' => 'checkbox',
  3843. 'label' => false,
  3844. 'div' => false
  3845. ));
  3846. $expected = array(
  3847. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3848. array('div' => array('class' => 'checkbox form-error')),
  3849. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3850. array('label' => array('for' => 'ContactTags0')),
  3851. 'one',
  3852. '/label',
  3853. '/div'
  3854. );
  3855. $this->assertTags($result, $expected);
  3856. $result = $this->Form->input('Contact.tags', array(
  3857. 'options' => array('one'),
  3858. 'multiple' => 'checkbox',
  3859. 'class' => 'mycheckbox',
  3860. 'label' => false,
  3861. 'div' => false
  3862. ));
  3863. $expected = array(
  3864. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3865. array('div' => array('class' => 'mycheckbox form-error')),
  3866. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3867. array('label' => array('for' => 'ContactTags0')),
  3868. 'one',
  3869. '/label',
  3870. '/div'
  3871. );
  3872. $this->assertTags($result, $expected);
  3873. }
  3874. /**
  3875. * Checks the security hash array generated for multiple-input checkbox elements
  3876. *
  3877. * @return void
  3878. */
  3879. public function testSelectMultipleCheckboxSecurity() {
  3880. $this->Form->request['_Token'] = array('key' => 'testKey');
  3881. $this->assertEquals(array(), $this->Form->fields);
  3882. $result = $this->Form->select(
  3883. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3884. array('multiple' => 'checkbox')
  3885. );
  3886. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3887. $result = $this->Form->secure($this->Form->fields);
  3888. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3889. $this->assertRegExp('/"' . $key . '"/', $result);
  3890. }
  3891. /**
  3892. * testInputMultipleCheckboxes method
  3893. *
  3894. * test input() resulting in multi select elements being generated.
  3895. *
  3896. * @return void
  3897. */
  3898. public function testInputMultipleCheckboxes() {
  3899. $result = $this->Form->input('Model.multi_field', array(
  3900. 'options' => array('first', 'second', 'third'),
  3901. 'multiple' => 'checkbox'
  3902. ));
  3903. $expected = array(
  3904. array('div' => array('class' => 'input select')),
  3905. array('label' => array('for' => 'ModelMultiField')),
  3906. 'Multi Field',
  3907. '/label',
  3908. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3909. array('div' => array('class' => 'checkbox')),
  3910. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3911. array('label' => array('for' => 'ModelMultiField0')),
  3912. 'first',
  3913. '/label',
  3914. '/div',
  3915. array('div' => array('class' => 'checkbox')),
  3916. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3917. array('label' => array('for' => 'ModelMultiField1')),
  3918. 'second',
  3919. '/label',
  3920. '/div',
  3921. array('div' => array('class' => 'checkbox')),
  3922. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3923. array('label' => array('for' => 'ModelMultiField2')),
  3924. 'third',
  3925. '/label',
  3926. '/div',
  3927. '/div'
  3928. );
  3929. $this->assertTags($result, $expected);
  3930. $result = $this->Form->input('Model.multi_field', array(
  3931. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3932. 'multiple' => 'checkbox'
  3933. ));
  3934. $expected = array(
  3935. array('div' => array('class' => 'input select')),
  3936. array('label' => array('for' => 'ModelMultiField')),
  3937. 'Multi Field',
  3938. '/label',
  3939. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3940. array('div' => array('class' => 'checkbox')),
  3941. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3942. array('label' => array('for' => 'ModelMultiFieldA')),
  3943. 'first',
  3944. '/label',
  3945. '/div',
  3946. array('div' => array('class' => 'checkbox')),
  3947. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3948. array('label' => array('for' => 'ModelMultiFieldB')),
  3949. 'second',
  3950. '/label',
  3951. '/div',
  3952. array('div' => array('class' => 'checkbox')),
  3953. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  3954. array('label' => array('for' => 'ModelMultiFieldC')),
  3955. 'third',
  3956. '/label',
  3957. '/div',
  3958. '/div'
  3959. );
  3960. $this->assertTags($result, $expected);
  3961. $result = $this->Form->input('Model.multi_field', array(
  3962. 'options' => array('1' => 'first'),
  3963. 'multiple' => 'checkbox',
  3964. 'label' => false,
  3965. 'div' => false
  3966. ));
  3967. $expected = array(
  3968. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3969. array('div' => array('class' => 'checkbox')),
  3970. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3971. array('label' => array('for' => 'ModelMultiField1')),
  3972. 'first',
  3973. '/label',
  3974. '/div'
  3975. );
  3976. $this->assertTags($result, $expected);
  3977. $result = $this->Form->input('Model.multi_field', array(
  3978. 'options' => array('2' => 'second'),
  3979. 'multiple' => 'checkbox',
  3980. 'label' => false,
  3981. 'div' => false
  3982. ));
  3983. $expected = array(
  3984. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3985. array('div' => array('class' => 'checkbox')),
  3986. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3987. array('label' => array('for' => 'ModelMultiField2')),
  3988. 'second',
  3989. '/label',
  3990. '/div'
  3991. );
  3992. $this->assertTags($result, $expected);
  3993. }
  3994. /**
  3995. * testSelectHiddenFieldOmission method
  3996. *
  3997. * test that select() with 'hiddenField' => false omits the hidden field
  3998. *
  3999. * @return void
  4000. */
  4001. public function testSelectHiddenFieldOmission() {
  4002. $result = $this->Form->select('Model.multi_field',
  4003. array('first', 'second'),
  4004. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4005. );
  4006. $expected = array(
  4007. array('div' => array('class' => 'checkbox')),
  4008. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4009. array('label' => array('for' => 'ModelMultiField0')),
  4010. 'first',
  4011. '/label',
  4012. '/div',
  4013. array('div' => array('class' => 'checkbox')),
  4014. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4015. array('label' => array('for' => 'ModelMultiField1')),
  4016. 'second',
  4017. '/label',
  4018. '/div'
  4019. );
  4020. $this->assertTags($result, $expected);
  4021. $result = $this->Form->input('Model.multi_field', array(
  4022. 'options' => array('first', 'second'),
  4023. 'multiple' => 'checkbox',
  4024. 'hiddenField' => false
  4025. ));
  4026. $expected = array(
  4027. array('div' => array('class' => 'input select')),
  4028. array('label' => array('for' => 'ModelMultiField')),
  4029. 'Multi Field',
  4030. '/label',
  4031. array('div' => array('class' => 'checkbox')),
  4032. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4033. array('label' => array('for' => 'ModelMultiField0')),
  4034. 'first',
  4035. '/label',
  4036. '/div',
  4037. array('div' => array('class' => 'checkbox')),
  4038. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4039. array('label' => array('for' => 'ModelMultiField1')),
  4040. 'second',
  4041. '/label',
  4042. '/div',
  4043. '/div'
  4044. );
  4045. $this->assertTags($result, $expected);
  4046. }
  4047. /**
  4048. * test that select() with multiple = checkbox works with overriding name attribute.
  4049. *
  4050. * @return void
  4051. */
  4052. public function testSelectCheckboxMultipleOverrideName() {
  4053. $result = $this->Form->input('category', array(
  4054. 'type' => 'select',
  4055. 'multiple' => 'checkbox',
  4056. 'name' => 'data[fish]',
  4057. 'options' => array('1', '2'),
  4058. 'div' => false,
  4059. 'label' => false,
  4060. ));
  4061. $expected = array(
  4062. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4063. array('div' => array('class' => 'checkbox')),
  4064. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4065. array('label' => array('for' => 'Category0')), '1', '/label',
  4066. '/div',
  4067. array('div' => array('class' => 'checkbox')),
  4068. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4069. array('label' => array('for' => 'Category1')), '2', '/label',
  4070. '/div'
  4071. );
  4072. $this->assertTags($result, $expected);
  4073. }
  4074. /**
  4075. * Test that 'id' overrides all the checkbox id's as well.
  4076. *
  4077. * @return void
  4078. */
  4079. public function testSelectCheckboxMultipleId() {
  4080. $result = $this->Form->select(
  4081. 'Model.multi_field',
  4082. array('first', 'second', 'third'),
  4083. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4084. );
  4085. $expected = array(
  4086. 'input' => array(
  4087. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4088. ),
  4089. array('div' => array('class' => 'checkbox')),
  4090. array('input' => array(
  4091. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4092. 'value' => '0', 'id' => 'CustomId0'
  4093. )),
  4094. array('label' => array('for' => 'CustomId0')),
  4095. 'first',
  4096. '/label',
  4097. '/div',
  4098. array('div' => array('class' => 'checkbox')),
  4099. array('input' => array(
  4100. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4101. 'value' => '1', 'id' => 'CustomId1'
  4102. )),
  4103. array('label' => array('for' => 'CustomId1')),
  4104. 'second',
  4105. '/label',
  4106. '/div',
  4107. array('div' => array('class' => 'checkbox')),
  4108. array('input' => array(
  4109. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4110. 'value' => '2', 'id' => 'CustomId2'
  4111. )),
  4112. array('label' => array('for' => 'CustomId2')),
  4113. 'third',
  4114. '/label',
  4115. '/div'
  4116. );
  4117. $this->assertTags($result, $expected);
  4118. }
  4119. /**
  4120. * testCheckbox method
  4121. *
  4122. * Test generation of checkboxes
  4123. *
  4124. * @return void
  4125. */
  4126. public function testCheckbox() {
  4127. $result = $this->Form->checkbox('Model.field');
  4128. $expected = array(
  4129. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4130. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4131. );
  4132. $this->assertTags($result, $expected);
  4133. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4134. $expected = array(
  4135. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4136. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4137. );
  4138. $this->assertTags($result, $expected);
  4139. $Contact = ClassRegistry::getObject('Contact');
  4140. $Contact->validationErrors['field'] = 1;
  4141. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4142. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4143. $expected = array(
  4144. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4145. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4146. );
  4147. $this->assertTags($result, $expected);
  4148. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4149. $expected = array(
  4150. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4151. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $this->Form->request->data['Contact']['field'] = '';
  4155. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4156. $expected = array(
  4157. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4158. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4159. );
  4160. $this->assertTags($result, $expected);
  4161. $Contact->validationErrors = array();
  4162. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4163. $expected = array(
  4164. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4165. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4166. );
  4167. $this->assertTags($result, $expected);
  4168. $this->Form->request->data['Contact']['published'] = 1;
  4169. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4170. $expected = array(
  4171. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4172. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4173. );
  4174. $this->assertTags($result, $expected);
  4175. $this->Form->request->data['Contact']['published'] = 0;
  4176. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4177. $expected = array(
  4178. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4179. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4180. );
  4181. $this->assertTags($result, $expected);
  4182. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4183. $expected = array(
  4184. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4185. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4186. );
  4187. $this->assertTags($result, $expected);
  4188. $result = $this->Form->checkbox('CustomField.1.value');
  4189. $expected = array(
  4190. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4191. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4192. );
  4193. $this->assertTags($result, $expected);
  4194. }
  4195. /**
  4196. * test checkbox() with a custom name attribute
  4197. *
  4198. * @return void
  4199. */
  4200. public function testCheckboxCustomNameAttribute() {
  4201. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4202. $expected = array(
  4203. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4204. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4205. );
  4206. $this->assertTags($result, $expected);
  4207. }
  4208. /**
  4209. * test the checked option for checkboxes.
  4210. *
  4211. * @return void
  4212. */
  4213. public function testCheckboxCheckedOption() {
  4214. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4215. $expected = array(
  4216. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4217. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4218. );
  4219. $this->assertTags($result, $expected);
  4220. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4221. $expected = array(
  4222. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4223. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4224. );
  4225. $this->assertTags($result, $expected);
  4226. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4227. $expected = array(
  4228. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4229. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4230. );
  4231. $this->assertTags($result, $expected);
  4232. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4233. $expected = array(
  4234. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4235. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4236. );
  4237. $this->assertTags($result, $expected);
  4238. $this->Form->request->data['Model']['field'] = 1;
  4239. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4240. $expected = array(
  4241. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4242. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. }
  4246. /**
  4247. * Test that disabled attribute works on both the checkbox and hidden input.
  4248. *
  4249. * @return void
  4250. */
  4251. public function testCheckboxDisabling() {
  4252. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4253. $expected = array(
  4254. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4255. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4256. );
  4257. $this->assertTags($result, $expected);
  4258. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4259. $expected = array(
  4260. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4261. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4262. );
  4263. $this->assertTags($result, $expected);
  4264. }
  4265. /**
  4266. * Test that the hidden input for checkboxes can be omitted or set to a
  4267. * specific value.
  4268. *
  4269. * @return void
  4270. */
  4271. public function testCheckboxHiddenField() {
  4272. $result = $this->Form->input('UserForm.something', array(
  4273. 'type' => 'checkbox',
  4274. 'hiddenField' => false
  4275. ));
  4276. $expected = array(
  4277. 'div' => array('class' => 'input checkbox'),
  4278. array('input' => array(
  4279. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4280. 'value' => '1', 'id' => 'UserFormSomething'
  4281. )),
  4282. 'label' => array('for' => 'UserFormSomething'),
  4283. 'Something',
  4284. '/label',
  4285. '/div'
  4286. );
  4287. $this->assertTags($result, $expected);
  4288. $result = $this->Form->input('UserForm.something', array(
  4289. 'type' => 'checkbox',
  4290. 'value' => 'Y',
  4291. 'hiddenField' => 'N',
  4292. ));
  4293. $expected = array(
  4294. 'div' => array('class' => 'input checkbox'),
  4295. array('input' => array(
  4296. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4297. 'value' => 'N', 'id' => 'UserFormSomething_'
  4298. )),
  4299. array('input' => array(
  4300. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4301. 'value' => 'Y', 'id' => 'UserFormSomething'
  4302. )),
  4303. 'label' => array('for' => 'UserFormSomething'),
  4304. 'Something',
  4305. '/label',
  4306. '/div'
  4307. );
  4308. $this->assertTags($result, $expected);
  4309. }
  4310. /**
  4311. * testDateTime method
  4312. *
  4313. * Test generation of date/time select elements
  4314. *
  4315. * @return void
  4316. */
  4317. public function testDateTime() {
  4318. extract($this->dateRegex);
  4319. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4320. $now = strtotime('now');
  4321. $expected = array(
  4322. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4323. $daysRegex,
  4324. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4325. date('j', $now),
  4326. '/option',
  4327. '*/select',
  4328. '-',
  4329. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4330. $monthsRegex,
  4331. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4332. date('F', $now),
  4333. '/option',
  4334. '*/select',
  4335. '-',
  4336. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4337. $yearsRegex,
  4338. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4339. date('Y', $now),
  4340. '/option',
  4341. '*/select',
  4342. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4343. $hoursRegex,
  4344. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4345. date('g', $now),
  4346. '/option',
  4347. '*/select',
  4348. ':',
  4349. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4350. $minutesRegex,
  4351. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4352. date('i', $now),
  4353. '/option',
  4354. '*/select',
  4355. ' ',
  4356. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4357. $meridianRegex,
  4358. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4359. date('a', $now),
  4360. '/option',
  4361. '*/select'
  4362. );
  4363. $this->assertTags($result, $expected);
  4364. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4365. $expected = array(
  4366. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4367. $daysRegex,
  4368. array('option' => array('value' => '')),
  4369. '/option',
  4370. '*/select',
  4371. '-',
  4372. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4373. $monthsRegex,
  4374. array('option' => array('value' => '')),
  4375. '/option',
  4376. '*/select',
  4377. '-',
  4378. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4379. $yearsRegex,
  4380. array('option' => array('value' => '')),
  4381. '/option',
  4382. '*/select',
  4383. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4384. $hoursRegex,
  4385. array('option' => array('value' => '')),
  4386. '/option',
  4387. '*/select',
  4388. ':',
  4389. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4390. $minutesRegex,
  4391. array('option' => array('value' => '')),
  4392. '/option',
  4393. '*/select',
  4394. ' ',
  4395. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4396. $meridianRegex,
  4397. array('option' => array('value' => '')),
  4398. '/option',
  4399. '*/select'
  4400. );
  4401. $this->assertTags($result, $expected);
  4402. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4403. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4404. $this->assertTags($result, $expected);
  4405. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4406. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4407. $this->assertTags($result, $expected);
  4408. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4409. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4410. $expected = array(
  4411. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4412. $daysRegex,
  4413. array('option' => array('value' => '')),
  4414. '/option',
  4415. '*/select',
  4416. '-',
  4417. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4418. $monthsRegex,
  4419. array('option' => array('value' => '')),
  4420. '/option',
  4421. '*/select',
  4422. '-',
  4423. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4424. $yearsRegex,
  4425. array('option' => array('value' => '')),
  4426. '/option',
  4427. '*/select',
  4428. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4429. $hoursRegex,
  4430. array('option' => array('value' => '')),
  4431. '/option',
  4432. '*/select',
  4433. ':',
  4434. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4435. $minutesRegex,
  4436. array('option' => array('value' => '')),
  4437. '/option',
  4438. array('option' => array('value' => '00')),
  4439. '00',
  4440. '/option',
  4441. array('option' => array('value' => '05')),
  4442. '05',
  4443. '/option',
  4444. array('option' => array('value' => '10')),
  4445. '10',
  4446. '/option',
  4447. '*/select',
  4448. ' ',
  4449. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4450. $meridianRegex,
  4451. array('option' => array('value' => '')),
  4452. '/option',
  4453. '*/select'
  4454. );
  4455. $this->assertTags($result, $expected);
  4456. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4457. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4458. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4459. $this->Form->request->data['Contact']['data'] = null;
  4460. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4461. $expected = array(
  4462. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4463. $daysRegex,
  4464. array('option' => array('value' => '')),
  4465. '/option',
  4466. '*/select',
  4467. '-',
  4468. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4469. $monthsRegex,
  4470. array('option' => array('value' => '')),
  4471. '/option',
  4472. '*/select',
  4473. '-',
  4474. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4475. $yearsRegex,
  4476. array('option' => array('value' => '')),
  4477. '/option',
  4478. '*/select',
  4479. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4480. $hoursRegex,
  4481. array('option' => array('value' => '')),
  4482. '/option',
  4483. '*/select',
  4484. ':',
  4485. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4486. $minutesRegex,
  4487. array('option' => array('value' => '')),
  4488. '/option',
  4489. '*/select',
  4490. ' ',
  4491. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4492. $meridianRegex,
  4493. array('option' => array('value' => '')),
  4494. '/option',
  4495. '*/select'
  4496. );
  4497. $this->assertTags($result, $expected);
  4498. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4499. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4500. $now = strtotime($this->Form->data['Model']['field']);
  4501. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4502. $expected = array(
  4503. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4504. $daysRegex,
  4505. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4506. date('j', $now),
  4507. '/option',
  4508. '*/select',
  4509. '-',
  4510. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4511. $monthsRegex,
  4512. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4513. date('F', $now),
  4514. '/option',
  4515. '*/select',
  4516. '-',
  4517. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4518. $yearsRegex,
  4519. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4520. date('Y', $now),
  4521. '/option',
  4522. '*/select',
  4523. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4524. $hoursRegex,
  4525. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4526. date('g', $now),
  4527. '/option',
  4528. '*/select',
  4529. ':',
  4530. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4531. $minutesRegex,
  4532. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4533. date('i', $now),
  4534. '/option',
  4535. '*/select',
  4536. ' ',
  4537. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4538. $meridianRegex,
  4539. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4540. date('a', $now),
  4541. '/option',
  4542. '*/select'
  4543. );
  4544. $this->assertTags($result, $expected);
  4545. $selected = strtotime('2008-10-26 12:33:00');
  4546. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4547. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4548. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4549. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4550. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4551. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4552. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4553. $this->Form->create('Contact');
  4554. $result = $this->Form->input('published');
  4555. $now = strtotime('now');
  4556. $expected = array(
  4557. 'div' => array('class' => 'input date'),
  4558. 'label' => array('for' => 'ContactPublishedMonth'),
  4559. 'Published',
  4560. '/label',
  4561. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4562. $monthsRegex,
  4563. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4564. date('F', $now),
  4565. '/option',
  4566. '*/select',
  4567. '-',
  4568. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4569. $daysRegex,
  4570. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4571. date('j', $now),
  4572. '/option',
  4573. '*/select',
  4574. '-',
  4575. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4576. $yearsRegex,
  4577. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4578. date('Y', $now),
  4579. '/option',
  4580. '*/select',
  4581. '/div'
  4582. );
  4583. $this->assertTags($result, $expected);
  4584. $result = $this->Form->input('published2', array('type' => 'date'));
  4585. $now = strtotime('now');
  4586. $expected = array(
  4587. 'div' => array('class' => 'input date'),
  4588. 'label' => array('for' => 'ContactPublished2Month'),
  4589. 'Published2',
  4590. '/label',
  4591. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4592. $monthsRegex,
  4593. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4594. date('F', $now),
  4595. '/option',
  4596. '*/select',
  4597. '-',
  4598. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4599. $daysRegex,
  4600. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4601. date('j', $now),
  4602. '/option',
  4603. '*/select',
  4604. '-',
  4605. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4606. $yearsRegex,
  4607. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4608. date('Y', $now),
  4609. '/option',
  4610. '*/select',
  4611. '/div'
  4612. );
  4613. $this->assertTags($result, $expected);
  4614. $this->Form->create('Contact');
  4615. $result = $this->Form->input('published', array('monthNames' => false));
  4616. $now = strtotime('now');
  4617. $expected = array(
  4618. 'div' => array('class' => 'input date'),
  4619. 'label' => array('for' => 'ContactPublishedMonth'),
  4620. 'Published',
  4621. '/label',
  4622. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4623. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4624. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4625. date('m', $now),
  4626. '/option',
  4627. '*/select',
  4628. '-',
  4629. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4630. $daysRegex,
  4631. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4632. date('j', $now),
  4633. '/option',
  4634. '*/select',
  4635. '-',
  4636. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4637. $yearsRegex,
  4638. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4639. date('Y', $now),
  4640. '/option',
  4641. '*/select',
  4642. '/div'
  4643. );
  4644. $this->assertTags($result, $expected);
  4645. $result = $this->Form->input('published', array('type' => 'time'));
  4646. $now = strtotime('now');
  4647. $expected = array(
  4648. 'div' => array('class' => 'input time'),
  4649. 'label' => array('for' => 'ContactPublishedHour'),
  4650. 'Published',
  4651. '/label',
  4652. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4653. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4654. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4655. date('g', $now),
  4656. '/option',
  4657. '*/select',
  4658. ':',
  4659. );
  4660. $this->assertTags($result, $expected);
  4661. $result = $this->Form->input('published', array(
  4662. 'timeFormat' => 24,
  4663. 'interval' => 5,
  4664. 'selected' => strtotime('2009-09-03 13:37:00'),
  4665. 'type' => 'datetime'
  4666. ));
  4667. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4668. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4669. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4670. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4671. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4672. $this->assertNoErrors();
  4673. $this->Form->request->data['Contact'] = array(
  4674. 'date' => array(
  4675. 'day' => '',
  4676. 'month' => '',
  4677. 'year' => '',
  4678. 'hour' => '',
  4679. 'min' => '',
  4680. 'meridian' => ''
  4681. )
  4682. );
  4683. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4684. }
  4685. /**
  4686. * test that datetime() and default values work.
  4687. *
  4688. * @return void
  4689. */
  4690. public function testDatetimeWithDefault() {
  4691. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4692. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4693. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4694. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4695. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4696. 'default' => '2009-06-01 11:15:30'
  4697. ));
  4698. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4699. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4700. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4701. }
  4702. /**
  4703. * test that bogus non-date time data doesn't cause errors.
  4704. *
  4705. * @return void
  4706. */
  4707. public function testDateTimeWithBogusData() {
  4708. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4709. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4710. }
  4711. /**
  4712. * testFormDateTimeMulti method
  4713. *
  4714. * test multiple datetime element generation
  4715. *
  4716. * @return void
  4717. */
  4718. public function testFormDateTimeMulti() {
  4719. extract($this->dateRegex);
  4720. $result = $this->Form->dateTime('Contact.1.updated');
  4721. $expected = array(
  4722. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4723. $daysRegex,
  4724. array('option' => array('value' => '')),
  4725. '/option',
  4726. '*/select',
  4727. '-',
  4728. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4729. $monthsRegex,
  4730. array('option' => array('value' => '')),
  4731. '/option',
  4732. '*/select',
  4733. '-',
  4734. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4735. $yearsRegex,
  4736. array('option' => array('value' => '')),
  4737. '/option',
  4738. '*/select',
  4739. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4740. $hoursRegex,
  4741. array('option' => array('value' => '')),
  4742. '/option',
  4743. '*/select',
  4744. ':',
  4745. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4746. $minutesRegex,
  4747. array('option' => array('value' => '')),
  4748. '/option',
  4749. '*/select',
  4750. ' ',
  4751. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4752. $meridianRegex,
  4753. array('option' => array('value' => '')),
  4754. '/option',
  4755. '*/select'
  4756. );
  4757. $this->assertTags($result, $expected);
  4758. $result = $this->Form->dateTime('Contact.2.updated');
  4759. $expected = array(
  4760. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4761. $daysRegex,
  4762. array('option' => array('value' => '')),
  4763. '/option',
  4764. '*/select',
  4765. '-',
  4766. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4767. $monthsRegex,
  4768. array('option' => array('value' => '')),
  4769. '/option',
  4770. '*/select',
  4771. '-',
  4772. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4773. $yearsRegex,
  4774. array('option' => array('value' => '')),
  4775. '/option',
  4776. '*/select',
  4777. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4778. $hoursRegex,
  4779. array('option' => array('value' => '')),
  4780. '/option',
  4781. '*/select',
  4782. ':',
  4783. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4784. $minutesRegex,
  4785. array('option' => array('value' => '')),
  4786. '/option',
  4787. '*/select',
  4788. ' ',
  4789. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4790. $meridianRegex,
  4791. array('option' => array('value' => '')),
  4792. '/option',
  4793. '*/select'
  4794. );
  4795. $this->assertTags($result, $expected);
  4796. }
  4797. /**
  4798. * When changing the date format, the label should always focus the first select box when
  4799. * clicked.
  4800. *
  4801. * @return void
  4802. */
  4803. public function testDateTimeLabelIdMatchesFirstInput() {
  4804. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4805. $this->assertContains('label for="ModelDateMonth"', $result);
  4806. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4807. $this->assertContains('label for="ModelDateDay"', $result);
  4808. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4809. $this->assertContains('label for="ModelDateYear"', $result);
  4810. }
  4811. /**
  4812. * testMonth method
  4813. *
  4814. * @return void
  4815. */
  4816. public function testMonth() {
  4817. $result = $this->Form->month('Model.field');
  4818. $expected = array(
  4819. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4820. array('option' => array('value' => '')),
  4821. '/option',
  4822. array('option' => array('value' => '01')),
  4823. date('F', strtotime('2008-01-01 00:00:00')),
  4824. '/option',
  4825. array('option' => array('value' => '02')),
  4826. date('F', strtotime('2008-02-01 00:00:00')),
  4827. '/option',
  4828. '*/select',
  4829. );
  4830. $this->assertTags($result, $expected);
  4831. $result = $this->Form->month('Model.field', array('empty' => true));
  4832. $expected = array(
  4833. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4834. array('option' => array('value' => '')),
  4835. '/option',
  4836. array('option' => array('value' => '01')),
  4837. date('F', strtotime('2008-01-01 00:00:00')),
  4838. '/option',
  4839. array('option' => array('value' => '02')),
  4840. date('F', strtotime('2008-02-01 00:00:00')),
  4841. '/option',
  4842. '*/select',
  4843. );
  4844. $this->assertTags($result, $expected);
  4845. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4846. $expected = array(
  4847. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4848. array('option' => array('value' => '')),
  4849. '/option',
  4850. array('option' => array('value' => '01')),
  4851. '01',
  4852. '/option',
  4853. array('option' => array('value' => '02')),
  4854. '02',
  4855. '/option',
  4856. '*/select',
  4857. );
  4858. $this->assertTags($result, $expected);
  4859. $monthNames = array(
  4860. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4861. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4862. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4863. $expected = array(
  4864. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4865. array('option' => array('value' => '')),
  4866. '/option',
  4867. array('option' => array('value' => '01')),
  4868. 'Jan',
  4869. '/option',
  4870. array('option' => array('value' => '02')),
  4871. 'Feb',
  4872. '/option',
  4873. '*/select',
  4874. );
  4875. $this->assertTags($result, $expected);
  4876. }
  4877. /**
  4878. * testDay method
  4879. *
  4880. * @return void
  4881. */
  4882. public function testDay() {
  4883. extract($this->dateRegex);
  4884. $result = $this->Form->day('Model.field', array('value' => false));
  4885. $expected = array(
  4886. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4887. array('option' => array('value' => '')),
  4888. '/option',
  4889. array('option' => array('value' => '01')),
  4890. '1',
  4891. '/option',
  4892. array('option' => array('value' => '02')),
  4893. '2',
  4894. '/option',
  4895. $daysRegex,
  4896. '/select',
  4897. );
  4898. $this->assertTags($result, $expected);
  4899. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4900. $result = $this->Form->day('Model.field');
  4901. $expected = array(
  4902. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4903. array('option' => array('value' => '')),
  4904. '/option',
  4905. array('option' => array('value' => '01')),
  4906. '1',
  4907. '/option',
  4908. array('option' => array('value' => '02')),
  4909. '2',
  4910. '/option',
  4911. $daysRegex,
  4912. array('option' => array('value' => '10', 'selected' => 'selected')),
  4913. '10',
  4914. '/option',
  4915. $daysRegex,
  4916. '/select',
  4917. );
  4918. $this->assertTags($result, $expected);
  4919. $this->Form->request->data['Model']['field'] = '';
  4920. $result = $this->Form->day('Model.field', array('value' => '10'));
  4921. $expected = array(
  4922. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4923. array('option' => array('value' => '')),
  4924. '/option',
  4925. array('option' => array('value' => '01')),
  4926. '1',
  4927. '/option',
  4928. array('option' => array('value' => '02')),
  4929. '2',
  4930. '/option',
  4931. $daysRegex,
  4932. array('option' => array('value' => '10', 'selected' => 'selected')),
  4933. '10',
  4934. '/option',
  4935. $daysRegex,
  4936. '/select',
  4937. );
  4938. $this->assertTags($result, $expected);
  4939. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4940. $result = $this->Form->day('Model.field', array('value' => true));
  4941. $expected = array(
  4942. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4943. array('option' => array('value' => '')),
  4944. '/option',
  4945. array('option' => array('value' => '01')),
  4946. '1',
  4947. '/option',
  4948. array('option' => array('value' => '02')),
  4949. '2',
  4950. '/option',
  4951. $daysRegex,
  4952. array('option' => array('value' => '10', 'selected' => 'selected')),
  4953. '10',
  4954. '/option',
  4955. $daysRegex,
  4956. '/select',
  4957. );
  4958. $this->assertTags($result, $expected);
  4959. }
  4960. /**
  4961. * testMinute method
  4962. *
  4963. * @return void
  4964. */
  4965. public function testMinute() {
  4966. extract($this->dateRegex);
  4967. $result = $this->Form->minute('Model.field');
  4968. $expected = array(
  4969. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4970. array('option' => array('value' => '')),
  4971. '/option',
  4972. array('option' => array('value' => '00')),
  4973. '00',
  4974. '/option',
  4975. array('option' => array('value' => '01')),
  4976. '01',
  4977. '/option',
  4978. array('option' => array('value' => '02')),
  4979. '02',
  4980. '/option',
  4981. $minutesRegex,
  4982. '/select',
  4983. );
  4984. $this->assertTags($result, $expected);
  4985. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4986. $result = $this->Form->minute('Model.field');
  4987. $expected = array(
  4988. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4989. array('option' => array('value' => '')),
  4990. '/option',
  4991. array('option' => array('value' => '00')),
  4992. '00',
  4993. '/option',
  4994. array('option' => array('value' => '01')),
  4995. '01',
  4996. '/option',
  4997. array('option' => array('value' => '02')),
  4998. '02',
  4999. '/option',
  5000. $minutesRegex,
  5001. array('option' => array('value' => '12', 'selected' => 'selected')),
  5002. '12',
  5003. '/option',
  5004. $minutesRegex,
  5005. '/select',
  5006. );
  5007. $this->assertTags($result, $expected);
  5008. $this->Form->request->data['Model']['field'] = '';
  5009. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5010. $expected = array(
  5011. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5012. array('option' => array('value' => '')),
  5013. '/option',
  5014. array('option' => array('value' => '00')),
  5015. '00',
  5016. '/option',
  5017. array('option' => array('value' => '05')),
  5018. '05',
  5019. '/option',
  5020. array('option' => array('value' => '10')),
  5021. '10',
  5022. '/option',
  5023. $minutesRegex,
  5024. '/select',
  5025. );
  5026. $this->assertTags($result, $expected);
  5027. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5028. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5029. $expected = array(
  5030. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5031. array('option' => array('value' => '')),
  5032. '/option',
  5033. array('option' => array('value' => '00')),
  5034. '00',
  5035. '/option',
  5036. array('option' => array('value' => '05')),
  5037. '05',
  5038. '/option',
  5039. array('option' => array('value' => '10', 'selected' => 'selected')),
  5040. '10',
  5041. '/option',
  5042. $minutesRegex,
  5043. '/select',
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. }
  5047. /**
  5048. * testHour method
  5049. *
  5050. * @return void
  5051. */
  5052. public function testHour() {
  5053. extract($this->dateRegex);
  5054. $result = $this->Form->hour('Model.field', false);
  5055. $expected = array(
  5056. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5057. array('option' => array('value' => '')),
  5058. '/option',
  5059. array('option' => array('value' => '01')),
  5060. '1',
  5061. '/option',
  5062. array('option' => array('value' => '02')),
  5063. '2',
  5064. '/option',
  5065. $hoursRegex,
  5066. '/select',
  5067. );
  5068. $this->assertTags($result, $expected);
  5069. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5070. $result = $this->Form->hour('Model.field', false);
  5071. $expected = array(
  5072. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5073. array('option' => array('value' => '')),
  5074. '/option',
  5075. array('option' => array('value' => '01')),
  5076. '1',
  5077. '/option',
  5078. array('option' => array('value' => '02')),
  5079. '2',
  5080. '/option',
  5081. $hoursRegex,
  5082. array('option' => array('value' => '12', 'selected' => 'selected')),
  5083. '12',
  5084. '/option',
  5085. '/select',
  5086. );
  5087. $this->assertTags($result, $expected);
  5088. $this->Form->request->data['Model']['field'] = '';
  5089. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5090. $expected = array(
  5091. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5092. array('option' => array('value' => '')),
  5093. '/option',
  5094. array('option' => array('value' => '00')),
  5095. '0',
  5096. '/option',
  5097. array('option' => array('value' => '01')),
  5098. '1',
  5099. '/option',
  5100. array('option' => array('value' => '02')),
  5101. '2',
  5102. '/option',
  5103. $hoursRegex,
  5104. array('option' => array('value' => '23', 'selected' => 'selected')),
  5105. '23',
  5106. '/option',
  5107. '/select',
  5108. );
  5109. $this->assertTags($result, $expected);
  5110. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5111. $result = $this->Form->hour('Model.field', true);
  5112. $expected = array(
  5113. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5114. array('option' => array('value' => '')),
  5115. '/option',
  5116. array('option' => array('value' => '00', 'selected' => 'selected')),
  5117. '0',
  5118. '/option',
  5119. array('option' => array('value' => '01')),
  5120. '1',
  5121. '/option',
  5122. array('option' => array('value' => '02')),
  5123. '2',
  5124. '/option',
  5125. $hoursRegex,
  5126. '/select',
  5127. );
  5128. $this->assertTags($result, $expected);
  5129. unset($this->Form->request->data['Model']['field']);
  5130. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5131. $thisHour = date('H');
  5132. $optValue = date('G');
  5133. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5134. }
  5135. /**
  5136. * testYear method
  5137. *
  5138. * @return void
  5139. */
  5140. public function testYear() {
  5141. $result = $this->Form->year('Model.field', 2006, 2007);
  5142. $expected = array(
  5143. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5144. array('option' => array('value' => '')),
  5145. '/option',
  5146. array('option' => array('value' => '2007')),
  5147. '2007',
  5148. '/option',
  5149. array('option' => array('value' => '2006')),
  5150. '2006',
  5151. '/option',
  5152. '/select',
  5153. );
  5154. $this->assertTags($result, $expected);
  5155. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5156. $expected = array(
  5157. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5158. array('option' => array('value' => '')),
  5159. '/option',
  5160. array('option' => array('value' => '2006')),
  5161. '2006',
  5162. '/option',
  5163. array('option' => array('value' => '2007')),
  5164. '2007',
  5165. '/option',
  5166. '/select',
  5167. );
  5168. $this->assertTags($result, $expected);
  5169. $this->request->data['Contact']['published'] = '';
  5170. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5171. $expected = array(
  5172. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5173. array('option' => array('value' => '')),
  5174. '/option',
  5175. array('option' => array('value' => '2007')),
  5176. '2007',
  5177. '/option',
  5178. array('option' => array('value' => '2006')),
  5179. '2006',
  5180. '/option',
  5181. '/select',
  5182. );
  5183. $this->assertTags($result, $expected);
  5184. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5185. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5186. $expected = array(
  5187. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5188. array('option' => array('value' => '2007')),
  5189. '2007',
  5190. '/option',
  5191. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5192. '2006',
  5193. '/option',
  5194. '/select',
  5195. );
  5196. $this->assertTags($result, $expected);
  5197. $this->Form->request->data['Contact']['published'] = '';
  5198. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5199. $expected = array(
  5200. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5201. array('option' => array('value' => '')),
  5202. '/option',
  5203. array('option' => array('value' => '2007')),
  5204. '2007',
  5205. '/option',
  5206. array('option' => array('value' => '2006')),
  5207. '2006',
  5208. '/option',
  5209. '/select',
  5210. );
  5211. $this->assertTags($result, $expected);
  5212. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5213. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5214. $expected = array(
  5215. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5216. array('option' => array('value' => '2007')),
  5217. '2007',
  5218. '/option',
  5219. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5220. '2006',
  5221. '/option',
  5222. '/select',
  5223. );
  5224. $this->assertTags($result, $expected);
  5225. $this->Form->request->data['Contact']['published'] = '';
  5226. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5227. $expected = array(
  5228. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5229. array('option' => array('value' => '')),
  5230. '/option',
  5231. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5232. '2007',
  5233. '/option',
  5234. array('option' => array('value' => '2006')),
  5235. '2006',
  5236. '/option',
  5237. '/select',
  5238. );
  5239. $this->assertTags($result, $expected);
  5240. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5241. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5242. $expected = array(
  5243. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5244. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5245. '2007',
  5246. '/option',
  5247. array('option' => array('value' => '2006')),
  5248. '2006',
  5249. '/option',
  5250. '/select',
  5251. );
  5252. $this->assertTags($result, $expected);
  5253. $this->Form->request->data['Contact']['published'] = '';
  5254. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5255. $expected = array(
  5256. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5257. array('option' => array('value' => '2008')),
  5258. '2008',
  5259. '/option',
  5260. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5261. '2007',
  5262. '/option',
  5263. array('option' => array('value' => '2006')),
  5264. '2006',
  5265. '/option',
  5266. '/select',
  5267. );
  5268. $this->assertTags($result, $expected);
  5269. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5270. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5271. $expected = array(
  5272. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5273. array('option' => array('value' => '2008')),
  5274. '2008',
  5275. '/option',
  5276. array('option' => array('value' => '2007')),
  5277. '2007',
  5278. '/option',
  5279. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5280. '2006',
  5281. '/option',
  5282. '/select',
  5283. );
  5284. $this->assertTags($result, $expected);
  5285. $this->Form->request->data = array();
  5286. $this->Form->create('Contact');
  5287. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5288. $expected = array(
  5289. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5290. array('option' => array('value' => '2008')),
  5291. '2008',
  5292. '/option',
  5293. array('option' => array('value' => '2007')),
  5294. '2007',
  5295. '/option',
  5296. array('option' => array('value' => '2006')),
  5297. '2006',
  5298. '/option',
  5299. '/select',
  5300. );
  5301. $this->assertTags($result, $expected);
  5302. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5303. $this->assertContains('data[Contact][published][year]', $result);
  5304. }
  5305. /**
  5306. * testTextArea method
  5307. *
  5308. * @return void
  5309. */
  5310. public function testTextArea() {
  5311. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5312. $result = $this->Form->textarea('Model.field');
  5313. $expected = array(
  5314. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5315. 'some test data',
  5316. '/textarea',
  5317. );
  5318. $this->assertTags($result, $expected);
  5319. $result = $this->Form->textarea('Model.tmp');
  5320. $expected = array(
  5321. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5322. '/textarea',
  5323. );
  5324. $this->assertTags($result, $expected);
  5325. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5326. $result = $this->Form->textarea('Model.field');
  5327. $expected = array(
  5328. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5329. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5330. '/textarea',
  5331. );
  5332. $this->assertTags($result, $expected);
  5333. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5334. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5335. $expected = array(
  5336. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5337. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5338. '/textarea',
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5342. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5343. $expected = array(
  5344. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5345. '/textarea'
  5346. );
  5347. $this->assertTags($result, $expected);
  5348. }
  5349. /**
  5350. * testTextAreaWithStupidCharacters method
  5351. *
  5352. * test text area with non-ascii characters
  5353. *
  5354. * @return void
  5355. */
  5356. public function testTextAreaWithStupidCharacters() {
  5357. $this->loadFixtures('Post');
  5358. $result = $this->Form->input('Post.content', array(
  5359. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5360. ));
  5361. $expected = array(
  5362. 'div' => array('class' => 'input text'),
  5363. 'label' => array('for' => 'PostContent'),
  5364. 'Current Text',
  5365. '/label',
  5366. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5367. 'GREAT®',
  5368. '/textarea',
  5369. '/div'
  5370. );
  5371. $this->assertTags($result, $expected);
  5372. }
  5373. /**
  5374. * testHiddenField method
  5375. *
  5376. * @return void
  5377. */
  5378. public function testHiddenField() {
  5379. $Contact = ClassRegistry::getObject('Contact');
  5380. $Contact->validationErrors['field'] = 1;
  5381. $this->Form->request->data['Contact']['field'] = 'test';
  5382. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5383. $this->assertTags($result, array(
  5384. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5385. );
  5386. }
  5387. /**
  5388. * testFileUploadField method
  5389. *
  5390. * @return void
  5391. */
  5392. public function testFileUploadField() {
  5393. $result = $this->Form->file('Model.upload');
  5394. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5395. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5396. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5397. $expected = array(
  5398. 'div' => array('class' => 'input file'),
  5399. 'label' => array('for' => 'ModelUpload'),
  5400. 'Upload',
  5401. '/label',
  5402. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5403. '/div'
  5404. );
  5405. $this->assertTags($result, $expected);
  5406. }
  5407. /**
  5408. * test File upload input on a model not used in create();
  5409. *
  5410. * @return void
  5411. */
  5412. public function testFileUploadOnOtherModel() {
  5413. $this->Form->create('ValidateUser', array('type' => 'file'));
  5414. $result = $this->Form->file('ValidateProfile.city');
  5415. $expected = array(
  5416. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5417. );
  5418. $this->assertTags($result, $expected);
  5419. }
  5420. /**
  5421. * testButton method
  5422. *
  5423. * @return void
  5424. */
  5425. public function testButton() {
  5426. $result = $this->Form->button('Hi');
  5427. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5428. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5429. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5430. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5431. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5432. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5433. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5434. $result = $this->Form->button('No type', array('type' => false));
  5435. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5436. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5437. $this->assertNotRegExp('/\&039/', $result);
  5438. }
  5439. /**
  5440. * Test that button() makes unlocked fields by default.
  5441. *
  5442. * @return void
  5443. */
  5444. public function testButtonUnlockedByDefault() {
  5445. $this->Form->request->params['_Token']['key'] = 'secured';
  5446. $this->Form->button('Save', array('name' => 'save'));
  5447. $this->Form->button('Clear');
  5448. $result = $this->Form->unlockField();
  5449. $this->assertEquals(array('save'), $result);
  5450. }
  5451. /**
  5452. * testPostButton method
  5453. *
  5454. * @return void
  5455. */
  5456. public function testPostButton() {
  5457. $result = $this->Form->postButton('Hi', '/controller/action');
  5458. $this->assertTags($result, array(
  5459. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5460. 'div' => array('style' => 'display:none;'),
  5461. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5462. '/div',
  5463. 'button' => array('type' => 'submit'),
  5464. 'Hi',
  5465. '/button',
  5466. '/form'
  5467. ));
  5468. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5469. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5470. }
  5471. /**
  5472. * Test that postButton adds _Token fields.
  5473. *
  5474. * @return void
  5475. */
  5476. public function testSecurePostButton() {
  5477. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5478. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5479. $expected = array(
  5480. 'form' => array(
  5481. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5482. ),
  5483. array('div' => array('style' => 'display:none;')),
  5484. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5485. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5486. '/div',
  5487. 'button' => array('type' => 'submit'),
  5488. 'Delete',
  5489. '/button',
  5490. array('div' => array('style' => 'display:none;')),
  5491. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5492. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5493. '/div',
  5494. '/form',
  5495. );
  5496. $this->assertTags($result, $expected);
  5497. }
  5498. /**
  5499. * testPostLink method
  5500. *
  5501. * @return void
  5502. */
  5503. public function testPostLink() {
  5504. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5505. $this->assertTags($result, array(
  5506. 'form' => array(
  5507. 'method' => 'post', 'action' => '/posts/delete/1',
  5508. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5509. ),
  5510. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5511. '/form',
  5512. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5513. 'Delete',
  5514. '/a'
  5515. ));
  5516. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5517. $this->assertTags($result, array(
  5518. 'form' => array(
  5519. 'method' => 'post', 'action' => '/posts/delete/1',
  5520. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5521. ),
  5522. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5523. '/form',
  5524. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5525. 'Delete',
  5526. '/a'
  5527. ));
  5528. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5529. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5530. }
  5531. /**
  5532. * Test that postLink adds _Token fields.
  5533. *
  5534. * @return void
  5535. */
  5536. public function testSecurePostLink() {
  5537. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5538. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5539. $expected = array(
  5540. 'form' => array(
  5541. 'method' => 'post', 'action' => '/posts/delete/1',
  5542. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5543. ),
  5544. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5545. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5546. 'div' => array('style' => 'display:none;'),
  5547. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5548. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5549. '/div',
  5550. '/form',
  5551. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5552. 'Delete',
  5553. '/a'
  5554. );
  5555. $this->assertTags($result, $expected);
  5556. }
  5557. /**
  5558. * testSubmitButton method
  5559. *
  5560. * @return void
  5561. */
  5562. public function testSubmitButton() {
  5563. $result = $this->Form->submit('');
  5564. $expected = array(
  5565. 'div' => array('class' => 'submit'),
  5566. 'input' => array('type' => 'submit', 'value' => ''),
  5567. '/div'
  5568. );
  5569. $this->assertTags($result, $expected);
  5570. $result = $this->Form->submit('Test Submit');
  5571. $expected = array(
  5572. 'div' => array('class' => 'submit'),
  5573. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5574. '/div'
  5575. );
  5576. $this->assertTags($result, $expected);
  5577. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5578. $expected = array(
  5579. 'span' => array('class' => 'submit'),
  5580. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5581. '/span'
  5582. );
  5583. $this->assertTags($result, $expected);
  5584. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5585. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5586. $this->assertTags($result, $expected);
  5587. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5588. $expected = array(
  5589. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5590. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5591. '/div'
  5592. );
  5593. $this->assertTags($result, $expected);
  5594. $result = $this->Form->submit('Next >');
  5595. $expected = array(
  5596. 'div' => array('class' => 'submit'),
  5597. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5598. '/div'
  5599. );
  5600. $this->assertTags($result, $expected);
  5601. $result = $this->Form->submit('Next >', array('escape' => false));
  5602. $expected = array(
  5603. 'div' => array('class' => 'submit'),
  5604. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5605. '/div'
  5606. );
  5607. $this->assertTags($result, $expected);
  5608. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5609. $expected = array(
  5610. 'div' => array('class' => 'submit'),
  5611. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5612. '/div'
  5613. );
  5614. $this->assertTags($result, $expected);
  5615. $before = '--before--';
  5616. $after = '--after--';
  5617. $result = $this->Form->submit('Test', array('before' => $before));
  5618. $expected = array(
  5619. 'div' => array('class' => 'submit'),
  5620. '--before--',
  5621. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5622. '/div'
  5623. );
  5624. $this->assertTags($result, $expected);
  5625. $result = $this->Form->submit('Test', array('after' => $after));
  5626. $expected = array(
  5627. 'div' => array('class' => 'submit'),
  5628. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5629. '--after--',
  5630. '/div'
  5631. );
  5632. $this->assertTags($result, $expected);
  5633. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5634. $expected = array(
  5635. 'div' => array('class' => 'submit'),
  5636. '--before--',
  5637. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5638. '--after--',
  5639. '/div'
  5640. );
  5641. $this->assertTags($result, $expected);
  5642. }
  5643. /**
  5644. * test image submit types.
  5645. *
  5646. * @return void
  5647. */
  5648. public function testSubmitImage() {
  5649. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5650. $expected = array(
  5651. 'div' => array('class' => 'submit'),
  5652. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5653. '/div'
  5654. );
  5655. $this->assertTags($result, $expected);
  5656. $result = $this->Form->submit('/relative/cake.power.gif');
  5657. $expected = array(
  5658. 'div' => array('class' => 'submit'),
  5659. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5660. '/div'
  5661. );
  5662. $this->assertTags($result, $expected);
  5663. $result = $this->Form->submit('cake.power.gif');
  5664. $expected = array(
  5665. 'div' => array('class' => 'submit'),
  5666. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5667. '/div'
  5668. );
  5669. $this->assertTags($result, $expected);
  5670. $result = $this->Form->submit('Not.an.image');
  5671. $expected = array(
  5672. 'div' => array('class' => 'submit'),
  5673. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5674. '/div'
  5675. );
  5676. $this->assertTags($result, $expected);
  5677. $after = '--after--';
  5678. $before = '--before--';
  5679. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5680. $expected = array(
  5681. 'div' => array('class' => 'submit'),
  5682. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5683. '--after--',
  5684. '/div'
  5685. );
  5686. $this->assertTags($result, $expected);
  5687. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5688. $expected = array(
  5689. 'div' => array('class' => 'submit'),
  5690. '--before--',
  5691. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5692. '/div'
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5696. $expected = array(
  5697. 'div' => array('class' => 'submit'),
  5698. '--before--',
  5699. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5700. '--after--',
  5701. '/div'
  5702. );
  5703. $this->assertTags($result, $expected);
  5704. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5705. $expected = array(
  5706. 'div' => array('class' => 'submit'),
  5707. '--before--',
  5708. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5709. '--after--',
  5710. '/div'
  5711. );
  5712. $this->assertTags($result, $expected);
  5713. }
  5714. /**
  5715. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5716. * be submitted at a time.
  5717. *
  5718. * @return void
  5719. */
  5720. public function testSubmitUnlockedByDefault() {
  5721. $this->Form->request->params['_Token']['key'] = 'secured';
  5722. $this->Form->submit('Go go');
  5723. $this->Form->submit('Save', array('name' => 'save'));
  5724. $result = $this->Form->unlockField();
  5725. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5726. }
  5727. /**
  5728. * Test submit image with timestamps.
  5729. *
  5730. * @return void
  5731. */
  5732. public function testSubmitImageTimestamp() {
  5733. Configure::write('Asset.timestamp', 'force');
  5734. $result = $this->Form->submit('cake.power.gif');
  5735. $expected = array(
  5736. 'div' => array('class' => 'submit'),
  5737. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5738. '/div'
  5739. );
  5740. $this->assertTags($result, $expected);
  5741. }
  5742. /**
  5743. * test the create() method
  5744. *
  5745. * @return void
  5746. */
  5747. public function testCreate() {
  5748. $result = $this->Form->create('Contact');
  5749. $encoding = strtolower(Configure::read('App.encoding'));
  5750. $expected = array(
  5751. 'form' => array(
  5752. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5753. 'accept-charset' => $encoding
  5754. ),
  5755. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5756. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5757. '/div'
  5758. );
  5759. $this->assertTags($result, $expected);
  5760. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5761. $expected = array('form' => array(
  5762. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5763. 'accept-charset' => $encoding
  5764. ));
  5765. $this->assertTags($result, $expected);
  5766. $result = $this->Form->create('Contact', array('type' => 'get'));
  5767. $expected = array('form' => array(
  5768. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5769. 'accept-charset' => $encoding
  5770. ));
  5771. $this->assertTags($result, $expected);
  5772. $result = $this->Form->create('Contact', array('type' => 'put'));
  5773. $expected = array(
  5774. 'form' => array(
  5775. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5776. 'accept-charset' => $encoding
  5777. ),
  5778. 'div' => array('style' => 'display:none;'),
  5779. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5780. '/div'
  5781. );
  5782. $this->assertTags($result, $expected);
  5783. $result = $this->Form->create('Contact', array('type' => 'file'));
  5784. $expected = array(
  5785. 'form' => array(
  5786. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5787. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5788. ),
  5789. 'div' => array('style' => 'display:none;'),
  5790. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5791. '/div'
  5792. );
  5793. $this->assertTags($result, $expected);
  5794. $this->Form->request->data['Contact']['id'] = 1;
  5795. $this->Form->request->here = '/contacts/edit/1';
  5796. $this->Form->request['action'] = 'edit';
  5797. $result = $this->Form->create('Contact');
  5798. $expected = array(
  5799. 'form' => array(
  5800. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5801. 'accept-charset' => $encoding
  5802. ),
  5803. 'div' => array('style' => 'display:none;'),
  5804. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5805. '/div'
  5806. );
  5807. $this->assertTags($result, $expected);
  5808. $this->Form->request->data['Contact']['id'] = 1;
  5809. $this->Form->request->here = '/contacts/edit/1';
  5810. $this->Form->request['action'] = 'edit';
  5811. $result = $this->Form->create('Contact', array('type' => 'file'));
  5812. $expected = array(
  5813. 'form' => array(
  5814. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5815. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5816. ),
  5817. 'div' => array('style' => 'display:none;'),
  5818. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5819. '/div'
  5820. );
  5821. $this->assertTags($result, $expected);
  5822. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5823. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5824. $expected = array(
  5825. 'form' => array(
  5826. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5827. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5828. ),
  5829. 'div' => array('style' => 'display:none;'),
  5830. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5831. '/div'
  5832. );
  5833. $this->assertTags($result, $expected);
  5834. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5835. $expected = array(
  5836. 'form' => array(
  5837. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5838. 'accept-charset' => $encoding
  5839. ),
  5840. 'div' => array('style' => 'display:none;'),
  5841. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5842. '/div'
  5843. );
  5844. $this->assertTags($result, $expected);
  5845. $this->Form->request['action'] = 'add';
  5846. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5847. $expected = array(
  5848. 'form' => array(
  5849. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5850. 'accept-charset' => $encoding
  5851. ),
  5852. 'div' => array('style' => 'display:none;'),
  5853. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5854. '/div'
  5855. );
  5856. $this->assertTags($result, $expected);
  5857. $result = $this->Form->create('User', array('action' => 'login'));
  5858. $expected = array(
  5859. 'form' => array(
  5860. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5861. 'accept-charset' => $encoding
  5862. ),
  5863. 'div' => array('style' => 'display:none;'),
  5864. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5865. '/div'
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $result = $this->Form->create('User', array('url' => '/users/login'));
  5869. $expected = array(
  5870. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5871. 'div' => array('style' => 'display:none;'),
  5872. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5873. '/div'
  5874. );
  5875. $this->assertTags($result, $expected);
  5876. $this->Form->request['controller'] = 'pages';
  5877. $result = $this->Form->create('User', array('action' => 'signup'));
  5878. $expected = array(
  5879. 'form' => array(
  5880. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5881. 'accept-charset' => $encoding
  5882. ),
  5883. 'div' => array('style' => 'display:none;'),
  5884. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5885. '/div'
  5886. );
  5887. $this->assertTags($result, $expected);
  5888. $this->Form->request->data = array();
  5889. $this->Form->request['controller'] = 'contacts';
  5890. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5891. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5892. $expected = array(
  5893. 'form' => array(
  5894. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5895. 'accept-charset' => 'utf-8'
  5896. ),
  5897. 'div' => array('style' => 'display:none;'),
  5898. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5899. '/div'
  5900. );
  5901. $this->assertTags($result, $expected);
  5902. }
  5903. /**
  5904. * Test the onsubmit option for create()
  5905. *
  5906. * @return void
  5907. */
  5908. public function testCreateOnSubmit() {
  5909. $this->Form->request->data = array();
  5910. $this->Form->request['controller'] = 'contacts';
  5911. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5912. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5913. $expected = array(
  5914. 'form' => array(
  5915. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5916. 'accept-charset' => 'utf-8'
  5917. ),
  5918. 'div' => array('style' => 'display:none;'),
  5919. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5920. '/div'
  5921. );
  5922. $this->assertTags($result, $expected);
  5923. $this->Form->request->data = array();
  5924. $this->Form->request['controller'] = 'contacts';
  5925. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5926. $result = $this->Form->create(array(
  5927. 'url' => array('action' => 'index', 'param'),
  5928. 'default' => false,
  5929. 'onsubmit' => 'someFunction();'
  5930. ));
  5931. $expected = array(
  5932. 'form' => array(
  5933. 'id' => 'ContactAddForm', 'method' => 'post',
  5934. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  5935. 'action' => '/contacts/index/param',
  5936. 'accept-charset' => 'utf-8'
  5937. ),
  5938. 'div' => array('style' => 'display:none;'),
  5939. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5940. '/div'
  5941. );
  5942. $this->assertTags($result, $expected);
  5943. }
  5944. /**
  5945. * test create() with automatic url generation
  5946. *
  5947. * @return void
  5948. */
  5949. public function testCreateAutoUrl() {
  5950. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  5951. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  5952. $this->Form->request->base = '/base_url';
  5953. $result = $this->Form->create('Contact');
  5954. $expected = array(
  5955. 'form' => array(
  5956. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  5957. 'accept-charset' => 'utf-8'
  5958. ),
  5959. 'div' => array('style' => 'display:none;'),
  5960. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5961. '/div'
  5962. );
  5963. $this->assertTags($result, $expected);
  5964. $this->Form->request['action'] = 'delete';
  5965. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  5966. $this->Form->request->base = '/base_url';
  5967. $result = $this->Form->create('Contact');
  5968. $expected = array(
  5969. 'form' => array(
  5970. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  5971. 'accept-charset' => 'utf-8'
  5972. ),
  5973. 'div' => array('style' => 'display:none;'),
  5974. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5975. '/div'
  5976. );
  5977. $this->assertTags($result, $expected);
  5978. }
  5979. /**
  5980. * test create() with a custom route
  5981. *
  5982. * @return void
  5983. */
  5984. public function testCreateCustomRoute() {
  5985. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  5986. $encoding = strtolower(Configure::read('App.encoding'));
  5987. $result = $this->Form->create('User', array('action' => 'login'));
  5988. $expected = array(
  5989. 'form' => array(
  5990. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  5991. 'accept-charset' => $encoding
  5992. ),
  5993. 'div' => array('style' => 'display:none;'),
  5994. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5995. '/div'
  5996. );
  5997. $this->assertTags($result, $expected);
  5998. }
  5999. /**
  6000. * test that inputDefaults are stored and used.
  6001. *
  6002. * @return void
  6003. */
  6004. public function testCreateWithInputDefaults() {
  6005. $this->Form->create('User', array(
  6006. 'inputDefaults' => array(
  6007. 'div' => false,
  6008. 'label' => false,
  6009. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6010. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6011. )
  6012. ));
  6013. $result = $this->Form->input('username');
  6014. $expected = array(
  6015. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6019. $expected = array(
  6020. 'div' => array('class' => 'input text'),
  6021. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6022. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6023. '/div'
  6024. );
  6025. $this->assertTags($result, $expected);
  6026. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6027. $expected = array(
  6028. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6029. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6030. );
  6031. $this->assertTags($result, $expected);
  6032. $this->Form->create('User', array(
  6033. 'inputDefaults' => array(
  6034. 'div' => false,
  6035. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6036. )
  6037. ));
  6038. $result = $this->Form->input('username', array('div' => true));
  6039. $expected = array(
  6040. 'div' => array('class' => 'input text'),
  6041. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6042. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6043. '/div'
  6044. );
  6045. $this->assertTags($result, $expected);
  6046. }
  6047. /**
  6048. * test automatic accept-charset overriding
  6049. *
  6050. * @return void
  6051. */
  6052. public function testCreateWithAcceptCharset() {
  6053. $result = $this->Form->create('UserForm', array(
  6054. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6055. )
  6056. );
  6057. $expected = array(
  6058. 'form' => array(
  6059. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6060. 'accept-charset' => 'iso-8859-1'
  6061. ),
  6062. 'div' => array('style' => 'display:none;'),
  6063. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6064. '/div'
  6065. );
  6066. $this->assertTags($result, $expected);
  6067. }
  6068. /**
  6069. * Test base form url when url param is passed with multiple parameters (&)
  6070. *
  6071. */
  6072. public function testCreateQuerystringrequest() {
  6073. $encoding = strtolower(Configure::read('App.encoding'));
  6074. $result = $this->Form->create('Contact', array(
  6075. 'type' => 'post',
  6076. 'escape' => false,
  6077. 'url' => array(
  6078. 'controller' => 'controller',
  6079. 'action' => 'action',
  6080. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6081. )
  6082. ));
  6083. $expected = array(
  6084. 'form' => array(
  6085. 'id' => 'ContactAddForm',
  6086. 'method' => 'post',
  6087. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6088. 'accept-charset' => $encoding
  6089. ),
  6090. 'div' => array('style' => 'display:none;'),
  6091. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6092. '/div'
  6093. );
  6094. $this->assertTags($result, $expected);
  6095. $result = $this->Form->create('Contact', array(
  6096. 'type' => 'post',
  6097. 'url' => array(
  6098. 'controller' => 'controller',
  6099. 'action' => 'action',
  6100. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6101. )
  6102. ));
  6103. $expected = array(
  6104. 'form' => array(
  6105. 'id' => 'ContactAddForm',
  6106. 'method' => 'post',
  6107. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6108. 'accept-charset' => $encoding
  6109. ),
  6110. 'div' => array('style' => 'display:none;'),
  6111. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6112. '/div'
  6113. );
  6114. $this->assertTags($result, $expected);
  6115. }
  6116. /**
  6117. * test that create() doesn't cause errors by multiple id's being in the primary key
  6118. * as could happen with multiple select or checkboxes.
  6119. *
  6120. * @return void
  6121. */
  6122. public function testCreateWithMultipleIdInData() {
  6123. $encoding = strtolower(Configure::read('App.encoding'));
  6124. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6125. $result = $this->Form->create('Contact');
  6126. $expected = array(
  6127. 'form' => array(
  6128. 'id' => 'ContactAddForm',
  6129. 'method' => 'post',
  6130. 'action' => '/contacts/add',
  6131. 'accept-charset' => $encoding
  6132. ),
  6133. 'div' => array('style' => 'display:none;'),
  6134. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6135. '/div'
  6136. );
  6137. $this->assertTags($result, $expected);
  6138. }
  6139. /**
  6140. * test that create() doesn't add in extra passed params.
  6141. *
  6142. * @return void
  6143. */
  6144. public function testCreatePassedArgs() {
  6145. $encoding = strtolower(Configure::read('App.encoding'));
  6146. $this->Form->request->data['Contact']['id'] = 1;
  6147. $result = $this->Form->create('Contact', array(
  6148. 'type' => 'post',
  6149. 'escape' => false,
  6150. 'url' => array(
  6151. 'action' => 'edit',
  6152. 'myparam'
  6153. )
  6154. ));
  6155. $expected = array(
  6156. 'form' => array(
  6157. 'id' => 'ContactAddForm',
  6158. 'method' => 'post',
  6159. 'action' => '/contacts/edit/myparam',
  6160. 'accept-charset' => $encoding
  6161. ),
  6162. 'div' => array('style' => 'display:none;'),
  6163. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6164. '/div'
  6165. );
  6166. $this->assertTags($result, $expected);
  6167. }
  6168. /**
  6169. * test creating a get form, and get form inputs.
  6170. *
  6171. * @return void
  6172. */
  6173. public function testGetFormCreate() {
  6174. $encoding = strtolower(Configure::read('App.encoding'));
  6175. $result = $this->Form->create('Contact', array('type' => 'get'));
  6176. $this->assertTags($result, array('form' => array(
  6177. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6178. 'accept-charset' => $encoding
  6179. )));
  6180. $result = $this->Form->text('Contact.name');
  6181. $this->assertTags($result, array('input' => array(
  6182. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6183. )));
  6184. $result = $this->Form->password('password');
  6185. $this->assertTags($result, array('input' => array(
  6186. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6187. )));
  6188. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6189. $result = $this->Form->text('user_form');
  6190. $this->assertTags($result, array('input' => array(
  6191. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6192. )));
  6193. }
  6194. /**
  6195. * test get form, and inputs when the model param is false
  6196. *
  6197. * @return void
  6198. */
  6199. public function testGetFormWithFalseModel() {
  6200. $encoding = strtolower(Configure::read('App.encoding'));
  6201. $this->Form->request['controller'] = 'contact_test';
  6202. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6203. $expected = array('form' => array(
  6204. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6205. 'accept-charset' => $encoding
  6206. ));
  6207. $this->assertTags($result, $expected);
  6208. $result = $this->Form->text('reason');
  6209. $expected = array(
  6210. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6211. );
  6212. $this->assertTags($result, $expected);
  6213. }
  6214. /**
  6215. * test that datetime() works with GET style forms.
  6216. *
  6217. * @return void
  6218. */
  6219. public function testDateTimeWithGetForms() {
  6220. extract($this->dateRegex);
  6221. $this->Form->create('Contact', array('type' => 'get'));
  6222. $result = $this->Form->datetime('created');
  6223. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6224. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6225. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6226. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6227. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6228. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6229. }
  6230. /**
  6231. * testEditFormWithData method
  6232. *
  6233. * test auto populating form elements from submitted data.
  6234. *
  6235. * @return void
  6236. */
  6237. public function testEditFormWithData() {
  6238. $this->Form->request->data = array('Person' => array(
  6239. 'id' => 1,
  6240. 'first_name' => 'Nate',
  6241. 'last_name' => 'Abele',
  6242. 'email' => 'nate@example.com'
  6243. ));
  6244. $this->Form->request->addParams(array(
  6245. 'models' => array('Person'),
  6246. 'controller' => 'people',
  6247. 'action' => 'add'
  6248. ));
  6249. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6250. $this->Form->create();
  6251. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6252. $expected = array(
  6253. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6254. 'select' => array(
  6255. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6256. ),
  6257. array('option' => array('value' => 1)), 'Nate', '/option',
  6258. array('option' => array('value' => 2)), 'Garrett', '/option',
  6259. array('option' => array('value' => 3)), 'Larry', '/option',
  6260. '/select'
  6261. );
  6262. $this->assertTags($result, $expected);
  6263. }
  6264. /**
  6265. * Test that required fields are created for various types of validation.
  6266. *
  6267. * @return void
  6268. */
  6269. public function testFormInputRequiredDetection() {
  6270. $this->Form->create('Contact');
  6271. $result = $this->Form->input('Contact.non_existing');
  6272. $expected = array(
  6273. 'div' => array('class' => 'input text'),
  6274. 'label' => array('for' => 'ContactNonExisting'),
  6275. 'Non Existing',
  6276. '/label',
  6277. 'input' => array(
  6278. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6279. 'id' => 'ContactNonExisting'
  6280. ),
  6281. '/div'
  6282. );
  6283. $this->assertTags($result, $expected);
  6284. $result = $this->Form->input('Contact.imrequired');
  6285. $expected = array(
  6286. 'div' => array('class' => 'input text required'),
  6287. 'label' => array('for' => 'ContactImrequired'),
  6288. 'Imrequired',
  6289. '/label',
  6290. 'input' => array(
  6291. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6292. 'id' => 'ContactImrequired'
  6293. ),
  6294. '/div'
  6295. );
  6296. $this->assertTags($result, $expected);
  6297. $result = $this->Form->input('Contact.imalsorequired');
  6298. $expected = array(
  6299. 'div' => array('class' => 'input text required'),
  6300. 'label' => array('for' => 'ContactImalsorequired'),
  6301. 'Imalsorequired',
  6302. '/label',
  6303. 'input' => array(
  6304. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6305. 'id' => 'ContactImalsorequired'
  6306. ),
  6307. '/div'
  6308. );
  6309. $this->assertTags($result, $expected);
  6310. $result = $this->Form->input('Contact.imrequiredtoo');
  6311. $expected = array(
  6312. 'div' => array('class' => 'input text required'),
  6313. 'label' => array('for' => 'ContactImrequiredtoo'),
  6314. 'Imrequiredtoo',
  6315. '/label',
  6316. 'input' => array(
  6317. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6318. 'id' => 'ContactImrequiredtoo'
  6319. ),
  6320. '/div'
  6321. );
  6322. $this->assertTags($result, $expected);
  6323. $result = $this->Form->input('Contact.required_one');
  6324. $expected = array(
  6325. 'div' => array('class' => 'input text required'),
  6326. 'label' => array('for' => 'ContactRequiredOne'),
  6327. 'Required One',
  6328. '/label',
  6329. 'input' => array(
  6330. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6331. 'id' => 'ContactRequiredOne'
  6332. ),
  6333. '/div'
  6334. );
  6335. $this->assertTags($result, $expected);
  6336. $result = $this->Form->input('Contact.string_required');
  6337. $expected = array(
  6338. 'div' => array('class' => 'input text required'),
  6339. 'label' => array('for' => 'ContactStringRequired'),
  6340. 'String Required',
  6341. '/label',
  6342. 'input' => array(
  6343. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6344. 'id' => 'ContactStringRequired'
  6345. ),
  6346. '/div'
  6347. );
  6348. $this->assertTags($result, $expected);
  6349. $result = $this->Form->input('Contact.imnotrequired');
  6350. $expected = array(
  6351. 'div' => array('class' => 'input text'),
  6352. 'label' => array('for' => 'ContactImnotrequired'),
  6353. 'Imnotrequired',
  6354. '/label',
  6355. 'input' => array(
  6356. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6357. 'id' => 'ContactImnotrequired'
  6358. ),
  6359. '/div'
  6360. );
  6361. $this->assertTags($result, $expected);
  6362. $result = $this->Form->input('Contact.imalsonotrequired');
  6363. $expected = array(
  6364. 'div' => array('class' => 'input text'),
  6365. 'label' => array('for' => 'ContactImalsonotrequired'),
  6366. 'Imalsonotrequired',
  6367. '/label',
  6368. 'input' => array(
  6369. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6370. 'id' => 'ContactImalsonotrequired'
  6371. ),
  6372. '/div'
  6373. );
  6374. $this->assertTags($result, $expected);
  6375. $result = $this->Form->input('Contact.imnotrequiredeither');
  6376. $expected = array(
  6377. 'div' => array('class' => 'input text'),
  6378. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6379. 'Imnotrequiredeither',
  6380. '/label',
  6381. 'input' => array(
  6382. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6383. 'id' => 'ContactImnotrequiredeither'
  6384. ),
  6385. '/div'
  6386. );
  6387. $this->assertTags($result, $expected);
  6388. }
  6389. /**
  6390. * testFormMagicInput method
  6391. *
  6392. * @return void
  6393. */
  6394. public function testFormMagicInput() {
  6395. $encoding = strtolower(Configure::read('App.encoding'));
  6396. $result = $this->Form->create('Contact');
  6397. $expected = array(
  6398. 'form' => array(
  6399. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6400. 'accept-charset' => $encoding
  6401. ),
  6402. 'div' => array('style' => 'display:none;'),
  6403. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6404. '/div'
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. $result = $this->Form->input('name');
  6408. $expected = array(
  6409. 'div' => array('class' => 'input text'),
  6410. 'label' => array('for' => 'ContactName'),
  6411. 'Name',
  6412. '/label',
  6413. 'input' => array(
  6414. 'type' => 'text', 'name' => 'data[Contact][name]',
  6415. 'id' => 'ContactName', 'maxlength' => '255'
  6416. ),
  6417. '/div'
  6418. );
  6419. $this->assertTags($result, $expected);
  6420. $result = $this->Form->input('non_existing_field_in_contact_model');
  6421. $expected = array(
  6422. 'div' => array('class' => 'input text'),
  6423. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6424. 'Non Existing Field In Contact Model',
  6425. '/label',
  6426. 'input' => array(
  6427. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6428. 'id' => 'ContactNonExistingFieldInContactModel'
  6429. ),
  6430. '/div'
  6431. );
  6432. $this->assertTags($result, $expected);
  6433. $result = $this->Form->input('Address.street');
  6434. $expected = array(
  6435. 'div' => array('class' => 'input text'),
  6436. 'label' => array('for' => 'AddressStreet'),
  6437. 'Street',
  6438. '/label',
  6439. 'input' => array(
  6440. 'type' => 'text', 'name' => 'data[Address][street]',
  6441. 'id' => 'AddressStreet'
  6442. ),
  6443. '/div'
  6444. );
  6445. $this->assertTags($result, $expected);
  6446. $result = $this->Form->input('Address.non_existing_field_in_model');
  6447. $expected = array(
  6448. 'div' => array('class' => 'input text'),
  6449. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6450. 'Non Existing Field In Model',
  6451. '/label',
  6452. 'input' => array(
  6453. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6454. 'id' => 'AddressNonExistingFieldInModel'
  6455. ),
  6456. '/div'
  6457. );
  6458. $this->assertTags($result, $expected);
  6459. $result = $this->Form->input('name', array('div' => false));
  6460. $expected = array(
  6461. 'label' => array('for' => 'ContactName'),
  6462. 'Name',
  6463. '/label',
  6464. 'input' => array(
  6465. 'type' => 'text', 'name' => 'data[Contact][name]',
  6466. 'id' => 'ContactName', 'maxlength' => '255'
  6467. )
  6468. );
  6469. $this->assertTags($result, $expected);
  6470. extract($this->dateRegex);
  6471. $now = strtotime('now');
  6472. $result = $this->Form->input('Contact.published', array('div' => false));
  6473. $expected = array(
  6474. 'label' => array('for' => 'ContactPublishedMonth'),
  6475. 'Published',
  6476. '/label',
  6477. array('select' => array(
  6478. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6479. )),
  6480. $monthsRegex,
  6481. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6482. date('F', $now),
  6483. '/option',
  6484. '*/select',
  6485. '-',
  6486. array('select' => array(
  6487. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6488. )),
  6489. $daysRegex,
  6490. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6491. date('j', $now),
  6492. '/option',
  6493. '*/select',
  6494. '-',
  6495. array('select' => array(
  6496. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6497. )),
  6498. $yearsRegex,
  6499. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6500. date('Y', $now),
  6501. '*/select'
  6502. );
  6503. $this->assertTags($result, $expected);
  6504. $result = $this->Form->input('Contact.updated', array('div' => false));
  6505. $expected = array(
  6506. 'label' => array('for' => 'ContactUpdatedMonth'),
  6507. 'Updated',
  6508. '/label',
  6509. array('select' => array(
  6510. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6511. )),
  6512. $monthsRegex,
  6513. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6514. date('F', $now),
  6515. '/option',
  6516. '*/select',
  6517. '-',
  6518. array('select' => array(
  6519. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6520. )),
  6521. $daysRegex,
  6522. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6523. date('j', $now),
  6524. '/option',
  6525. '*/select',
  6526. '-',
  6527. array('select' => array(
  6528. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6529. )),
  6530. $yearsRegex,
  6531. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6532. date('Y', $now),
  6533. '*/select'
  6534. );
  6535. $this->assertTags($result, $expected);
  6536. $result = $this->Form->input('UserForm.stuff');
  6537. $expected = array(
  6538. 'div' => array('class' => 'input text'),
  6539. 'label' => array('for' => 'UserFormStuff'),
  6540. 'Stuff',
  6541. '/label',
  6542. 'input' => array(
  6543. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6544. 'id' => 'UserFormStuff', 'maxlength' => 10
  6545. ),
  6546. '/div'
  6547. );
  6548. $this->assertTags($result, $expected);
  6549. }
  6550. /**
  6551. * testForMagicInputNonExistingNorValidated method
  6552. *
  6553. * @return void
  6554. */
  6555. public function testForMagicInputNonExistingNorValidated() {
  6556. $encoding = strtolower(Configure::read('App.encoding'));
  6557. $result = $this->Form->create('Contact');
  6558. $expected = array(
  6559. 'form' => array(
  6560. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6561. 'accept-charset' => $encoding
  6562. ),
  6563. 'div' => array('style' => 'display:none;'),
  6564. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6565. '/div'
  6566. );
  6567. $this->assertTags($result, $expected);
  6568. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6569. $expected = array(
  6570. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6571. 'Non Existing Nor Validated',
  6572. '/label',
  6573. 'input' => array(
  6574. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6575. 'id' => 'ContactNonExistingNorValidated'
  6576. )
  6577. );
  6578. $this->assertTags($result, $expected);
  6579. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6580. 'div' => false, 'value' => 'my value'
  6581. ));
  6582. $expected = array(
  6583. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6584. 'Non Existing Nor Validated',
  6585. '/label',
  6586. 'input' => array(
  6587. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6588. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6589. )
  6590. );
  6591. $this->assertTags($result, $expected);
  6592. $this->Form->request->data = array(
  6593. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6594. ));
  6595. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6596. $expected = array(
  6597. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6598. 'Non Existing Nor Validated',
  6599. '/label',
  6600. 'input' => array(
  6601. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6602. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6603. )
  6604. );
  6605. $this->assertTags($result, $expected);
  6606. }
  6607. /**
  6608. * testFormMagicInputLabel method
  6609. *
  6610. * @return void
  6611. */
  6612. public function testFormMagicInputLabel() {
  6613. $encoding = strtolower(Configure::read('App.encoding'));
  6614. $result = $this->Form->create('Contact');
  6615. $expected = array(
  6616. 'form' => array(
  6617. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6618. 'accept-charset' => $encoding
  6619. ),
  6620. 'div' => array('style' => 'display:none;'),
  6621. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6622. '/div'
  6623. );
  6624. $this->assertTags($result, $expected);
  6625. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6626. $this->assertTags($result, array('input' => array(
  6627. 'name' => 'data[Contact][name]', 'type' => 'text',
  6628. 'id' => 'ContactName', 'maxlength' => '255')
  6629. ));
  6630. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6631. $expected = array(
  6632. 'label' => array('for' => 'ContactName'),
  6633. 'My label',
  6634. '/label',
  6635. 'input' => array(
  6636. 'type' => 'text', 'name' => 'data[Contact][name]',
  6637. 'id' => 'ContactName', 'maxlength' => '255'
  6638. )
  6639. );
  6640. $this->assertTags($result, $expected);
  6641. $result = $this->Form->input('Contact.name', array(
  6642. 'div' => false, 'label' => array('class' => 'mandatory')
  6643. ));
  6644. $expected = array(
  6645. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6646. 'Name',
  6647. '/label',
  6648. 'input' => array(
  6649. 'type' => 'text', 'name' => 'data[Contact][name]',
  6650. 'id' => 'ContactName', 'maxlength' => '255'
  6651. )
  6652. );
  6653. $this->assertTags($result, $expected);
  6654. $result = $this->Form->input('Contact.name', array(
  6655. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6656. ));
  6657. $expected = array(
  6658. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6659. 'My label',
  6660. '/label',
  6661. 'input' => array(
  6662. 'type' => 'text', 'name' => 'data[Contact][name]',
  6663. 'id' => 'ContactName', 'maxlength' => '255'
  6664. )
  6665. );
  6666. $this->assertTags($result, $expected);
  6667. $result = $this->Form->input('Contact.name', array(
  6668. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6669. ));
  6670. $expected = array(
  6671. 'label' => array('for' => 'my_id'),
  6672. 'Name',
  6673. '/label',
  6674. 'input' => array(
  6675. 'type' => 'text', 'name' => 'data[Contact][name]',
  6676. 'id' => 'my_id', 'maxlength' => '255'
  6677. )
  6678. );
  6679. $this->assertTags($result, $expected);
  6680. $result = $this->Form->input('1.id');
  6681. $this->assertTags($result, array('input' => array(
  6682. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6683. 'id' => 'Contact1Id'
  6684. )));
  6685. $result = $this->Form->input("1.name");
  6686. $expected = array(
  6687. 'div' => array('class' => 'input text'),
  6688. 'label' => array('for' => 'Contact1Name'),
  6689. 'Name',
  6690. '/label',
  6691. 'input' => array(
  6692. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6693. 'id' => 'Contact1Name', 'maxlength' => '255'
  6694. ),
  6695. '/div'
  6696. );
  6697. $this->assertTags($result, $expected);
  6698. $result = $this->Form->input('Contact.1.id');
  6699. $this->assertTags($result, array(
  6700. 'input' => array(
  6701. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6702. 'id' => 'Contact1Id'
  6703. )
  6704. ));
  6705. $result = $this->Form->input("Model.1.name");
  6706. $expected = array(
  6707. 'div' => array('class' => 'input text'),
  6708. 'label' => array('for' => 'Model1Name'),
  6709. 'Name',
  6710. '/label',
  6711. 'input' => array(
  6712. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6713. 'id' => 'Model1Name'
  6714. ),
  6715. '/div'
  6716. );
  6717. $this->assertTags($result, $expected);
  6718. }
  6719. /**
  6720. * testFormEnd method
  6721. *
  6722. * @return void
  6723. */
  6724. public function testFormEnd() {
  6725. $this->assertEquals('</form>', $this->Form->end());
  6726. $result = $this->Form->end('');
  6727. $expected = array(
  6728. 'div' => array('class' => 'submit'),
  6729. 'input' => array('type' => 'submit', 'value' => ''),
  6730. '/div',
  6731. '/form'
  6732. );
  6733. $this->assertTags($result, $expected);
  6734. $result = $this->Form->end(array('label' => ''));
  6735. $expected = array(
  6736. 'div' => array('class' => 'submit'),
  6737. 'input' => array('type' => 'submit', 'value' => ''),
  6738. '/div',
  6739. '/form'
  6740. );
  6741. $this->assertTags($result, $expected);
  6742. $result = $this->Form->end('save');
  6743. $expected = array(
  6744. 'div' => array('class' => 'submit'),
  6745. 'input' => array('type' => 'submit', 'value' => 'save'),
  6746. '/div',
  6747. '/form'
  6748. );
  6749. $this->assertTags($result, $expected);
  6750. $result = $this->Form->end(array('label' => 'save'));
  6751. $expected = array(
  6752. 'div' => array('class' => 'submit'),
  6753. 'input' => array('type' => 'submit', 'value' => 'save'),
  6754. '/div',
  6755. '/form'
  6756. );
  6757. $this->assertTags($result, $expected);
  6758. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6759. $expected = array(
  6760. 'div' => array('class' => 'submit'),
  6761. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6762. '/div',
  6763. '/form'
  6764. );
  6765. $this->assertTags($result, $expected);
  6766. $result = $this->Form->end(array('name' => 'Whatever'));
  6767. $expected = array(
  6768. 'div' => array('class' => 'submit'),
  6769. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6770. '/div',
  6771. '/form'
  6772. );
  6773. $this->assertTags($result, $expected);
  6774. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6775. $expected = array(
  6776. 'div' => array('class' => 'good'),
  6777. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6778. '/div',
  6779. '/form'
  6780. );
  6781. $this->assertTags($result, $expected);
  6782. $result = $this->Form->end(array(
  6783. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6784. ));
  6785. $expected = array(
  6786. 'div' => array('class' => 'good'),
  6787. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6788. '/div',
  6789. '/form'
  6790. );
  6791. $this->assertTags($result, $expected);
  6792. }
  6793. /**
  6794. * testMultipleFormWithIdFields method
  6795. *
  6796. * @return void
  6797. */
  6798. public function testMultipleFormWithIdFields() {
  6799. $this->Form->create('UserForm');
  6800. $result = $this->Form->input('id');
  6801. $this->assertTags($result, array('input' => array(
  6802. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6803. )));
  6804. $result = $this->Form->input('ValidateItem.id');
  6805. $this->assertTags($result, array('input' => array(
  6806. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6807. 'id' => 'ValidateItemId'
  6808. )));
  6809. $result = $this->Form->input('ValidateUser.id');
  6810. $this->assertTags($result, array('input' => array(
  6811. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6812. 'id' => 'ValidateUserId'
  6813. )));
  6814. }
  6815. /**
  6816. * testDbLessModel method
  6817. *
  6818. * @return void
  6819. */
  6820. public function testDbLessModel() {
  6821. $this->Form->create('TestMail');
  6822. $result = $this->Form->input('name');
  6823. $expected = array(
  6824. 'div' => array('class' => 'input text'),
  6825. 'label' => array('for' => 'TestMailName'),
  6826. 'Name',
  6827. '/label',
  6828. 'input' => array(
  6829. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6830. 'id' => 'TestMailName'
  6831. ),
  6832. '/div'
  6833. );
  6834. $this->assertTags($result, $expected);
  6835. ClassRegistry::init('TestMail');
  6836. $this->Form->create('TestMail');
  6837. $result = $this->Form->input('name');
  6838. $expected = array(
  6839. 'div' => array('class' => 'input text'),
  6840. 'label' => array('for' => 'TestMailName'),
  6841. 'Name',
  6842. '/label',
  6843. 'input' => array(
  6844. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6845. 'id' => 'TestMailName'
  6846. ),
  6847. '/div'
  6848. );
  6849. $this->assertTags($result, $expected);
  6850. }
  6851. /**
  6852. * testBrokenness method
  6853. *
  6854. * @return void
  6855. */
  6856. public function testBrokenness() {
  6857. /*
  6858. * #4 This test has two parents and four children. By default (as of r7117) both
  6859. * parents are show but the first parent is missing a child. This is the inconsistency
  6860. * in the default behaviour - one parent has all children, the other does not - dependent
  6861. * on the data values.
  6862. */
  6863. $result = $this->Form->select('Model.field', array(
  6864. 'Fred' => array(
  6865. 'freds_son_1' => 'Fred',
  6866. 'freds_son_2' => 'Freddie'
  6867. ),
  6868. 'Bert' => array(
  6869. 'berts_son_1' => 'Albert',
  6870. 'berts_son_2' => 'Bertie')
  6871. ),
  6872. array('showParents' => true, 'empty' => false)
  6873. );
  6874. $expected = array(
  6875. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6876. array('optgroup' => array('label' => 'Fred')),
  6877. array('option' => array('value' => 'freds_son_1')),
  6878. 'Fred',
  6879. '/option',
  6880. array('option' => array('value' => 'freds_son_2')),
  6881. 'Freddie',
  6882. '/option',
  6883. '/optgroup',
  6884. array('optgroup' => array('label' => 'Bert')),
  6885. array('option' => array('value' => 'berts_son_1')),
  6886. 'Albert',
  6887. '/option',
  6888. array('option' => array('value' => 'berts_son_2')),
  6889. 'Bertie',
  6890. '/option',
  6891. '/optgroup',
  6892. '/select'
  6893. );
  6894. $this->assertTags($result, $expected);
  6895. /*
  6896. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6897. * changed, so we should expect the same select list data, just with a different name
  6898. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6899. * This is where data corruption can occur, because when a select value is missing from
  6900. * a list a form will substitute the first value in the list - without the user knowing.
  6901. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6902. * affect the availability of 3 => 'Three' as a valid option.
  6903. */
  6904. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6905. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6906. ));
  6907. $result = $this->Form->select(
  6908. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6909. );
  6910. $expected = array(
  6911. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6912. array('option' => array('value' => 1)),
  6913. 'One',
  6914. '/option',
  6915. array('option' => array('value' => 2)),
  6916. 'Two',
  6917. '/option',
  6918. array('optgroup' => array('label' => 'Three')),
  6919. array('option' => array('value' => 3)),
  6920. 'Three',
  6921. '/option',
  6922. array('option' => array('value' => 4)),
  6923. 'Four',
  6924. '/option',
  6925. array('option' => array('value' => 5)),
  6926. 'Five',
  6927. '/option',
  6928. '/optgroup',
  6929. '/select'
  6930. );
  6931. $this->assertTags($result, $expected);
  6932. }
  6933. /**
  6934. * Test the generation of fields for a multi record form.
  6935. *
  6936. * @return void
  6937. */
  6938. public function testMultiRecordForm() {
  6939. $this->Form->create('ValidateProfile');
  6940. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6941. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6942. $expected = array(
  6943. 'div' => array('class' => 'input textarea'),
  6944. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6945. 'Name',
  6946. '/label',
  6947. 'textarea' => array(
  6948. 'id' => 'ValidateProfile1ValidateItem2Name',
  6949. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  6950. 'cols' => 30,
  6951. 'rows' => 6
  6952. ),
  6953. 'Value',
  6954. '/textarea',
  6955. '/div'
  6956. );
  6957. $this->assertTags($result, $expected);
  6958. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  6959. $expected = array(
  6960. 'div' => array('class' => 'input date'),
  6961. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6962. 'Created',
  6963. '/label',
  6964. array('select' => array(
  6965. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  6966. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6967. )
  6968. ),
  6969. array('option' => array('value' => '')), '/option',
  6970. $this->dateRegex['monthsRegex'],
  6971. '/select', '-',
  6972. array('select' => array(
  6973. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  6974. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6975. )
  6976. ),
  6977. array('option' => array('value' => '')), '/option',
  6978. $this->dateRegex['daysRegex'],
  6979. '/select', '-',
  6980. array('select' => array(
  6981. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  6982. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6983. )
  6984. ),
  6985. array('option' => array('value' => '')), '/option',
  6986. $this->dateRegex['yearsRegex'],
  6987. '/select',
  6988. '/div'
  6989. );
  6990. $this->assertTags($result, $expected);
  6991. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  6992. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6993. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6994. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6995. $expected = array(
  6996. 'div' => array('class' => 'input select error'),
  6997. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6998. 'Profile',
  6999. '/label',
  7000. 'select' => array(
  7001. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7002. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7003. 'class' => 'form-error'
  7004. ),
  7005. '/select',
  7006. array('div' => array('class' => 'error-message')),
  7007. 'Error',
  7008. '/div',
  7009. '/div'
  7010. );
  7011. $this->assertTags($result, $expected);
  7012. }
  7013. /**
  7014. * test the correct display of multi-record form validation errors.
  7015. *
  7016. * @return void
  7017. */
  7018. public function testMultiRecordFormValidationErrors() {
  7019. $this->Form->create('ValidateProfile');
  7020. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7021. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7022. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7023. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7024. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7025. $result = $this->Form->error('ValidateProfile.2.city');
  7026. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7027. $result = $this->Form->error('2.city');
  7028. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7029. }
  7030. /**
  7031. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7032. *
  7033. * @return void
  7034. */
  7035. public function testInputTemplate() {
  7036. $result = $this->Form->input('Contact.email', array(
  7037. 'type' => 'text', 'format' => array('input')
  7038. ));
  7039. $expected = array(
  7040. 'div' => array('class' => 'input text'),
  7041. 'input' => array(
  7042. 'type' => 'text', 'name' => 'data[Contact][email]',
  7043. 'id' => 'ContactEmail'
  7044. ),
  7045. '/div'
  7046. );
  7047. $this->assertTags($result, $expected);
  7048. $result = $this->Form->input('Contact.email', array(
  7049. 'type' => 'text', 'format' => array('input', 'label'),
  7050. 'label' => '<em>Email (required)</em>'
  7051. ));
  7052. $expected = array(
  7053. 'div' => array('class' => 'input text'),
  7054. array('input' => array(
  7055. 'type' => 'text', 'name' => 'data[Contact][email]',
  7056. 'id' => 'ContactEmail'
  7057. )),
  7058. 'label' => array('for' => 'ContactEmail'),
  7059. 'em' => array(),
  7060. 'Email (required)',
  7061. '/em',
  7062. '/label',
  7063. '/div'
  7064. );
  7065. $this->assertTags($result, $expected);
  7066. $result = $this->Form->input('Contact.email', array(
  7067. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7068. 'between' => '<div>Something in the middle</div>',
  7069. 'after' => '<span>Some text at the end</span>'
  7070. ));
  7071. $expected = array(
  7072. 'div' => array('class' => 'input text'),
  7073. array('input' => array(
  7074. 'type' => 'text', 'name' => 'data[Contact][email]',
  7075. 'id' => 'ContactEmail'
  7076. )),
  7077. array('div' => array()),
  7078. 'Something in the middle',
  7079. '/div',
  7080. 'label' => array('for' => 'ContactEmail'),
  7081. 'Email',
  7082. '/label',
  7083. 'span' => array(),
  7084. 'Some text at the end',
  7085. '/span',
  7086. '/div'
  7087. );
  7088. $this->assertTags($result, $expected);
  7089. $result = $this->Form->input('Contact.method', array(
  7090. 'type' => 'radio',
  7091. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7092. 'between' => 'I am between',
  7093. ));
  7094. $expected = array(
  7095. 'div' => array('class' => 'input radio'),
  7096. 'fieldset' => array(),
  7097. 'legend' => array(),
  7098. 'Method',
  7099. '/legend',
  7100. 'I am between',
  7101. 'input' => array(
  7102. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7103. 'value' => '', 'id' => 'ContactMethod_'
  7104. ),
  7105. array('input' => array(
  7106. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7107. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7108. )),
  7109. array('label' => array('for' => 'ContactMethodEmail')),
  7110. 'Email',
  7111. '/label',
  7112. array('input' => array(
  7113. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7114. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7115. )),
  7116. array('label' => array('for' => 'ContactMethodPigeon')),
  7117. 'Pigeon',
  7118. '/label',
  7119. '/fieldset',
  7120. '/div',
  7121. );
  7122. $this->assertTags($result, $expected);
  7123. }
  7124. /**
  7125. * test that some html5 inputs + FormHelper::__call() work
  7126. *
  7127. * @return void
  7128. */
  7129. public function testHtml5Inputs() {
  7130. $result = $this->Form->email('User.email');
  7131. $expected = array(
  7132. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7133. );
  7134. $this->assertTags($result, $expected);
  7135. $result = $this->Form->search('User.query');
  7136. $expected = array(
  7137. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7138. );
  7139. $this->assertTags($result, $expected);
  7140. $result = $this->Form->search('User.query', array('value' => 'test'));
  7141. $expected = array(
  7142. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7143. );
  7144. $this->assertTags($result, $expected);
  7145. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7146. $expected = array(
  7147. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7148. );
  7149. $this->assertTags($result, $expected);
  7150. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7151. $expected = array(
  7152. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7153. );
  7154. $this->assertTags($result, $expected);
  7155. }
  7156. /**
  7157. *
  7158. * @expectedException CakeException
  7159. * @return void
  7160. */
  7161. public function testHtml5InputException() {
  7162. $this->Form->email();
  7163. }
  7164. /**
  7165. * Tests that a model can be loaded from the model names passed in the request object
  7166. *
  7167. * @return void
  7168. */
  7169. public function testIntrospectModelFromRequest() {
  7170. $this->loadFixtures('Post');
  7171. App::build(array(
  7172. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7173. ));
  7174. CakePlugin::load('TestPlugin');
  7175. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7176. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7177. $this->Form->create('TestPluginPost');
  7178. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7179. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7180. CakePlugin::unload();
  7181. App::build();
  7182. }
  7183. /**
  7184. * Tests that it is possible to set the validation errors directly in the helper for a field
  7185. *
  7186. * @return void
  7187. */
  7188. public function testCustomValidationErrors() {
  7189. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7190. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7191. $this->assertEquals('Badness!', $result);
  7192. }
  7193. /**
  7194. * Tests that the 'on' key validates as expected on create
  7195. *
  7196. * @return void
  7197. */
  7198. public function testRequiredOnCreate() {
  7199. $this->Form->create('Contact');
  7200. $result = $this->Form->input('Contact.imrequiredonupdate');
  7201. $expected = array(
  7202. 'div' => array('class' => 'input text'),
  7203. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7204. 'Imrequiredonupdate',
  7205. '/label',
  7206. 'input' => array(
  7207. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7208. 'id' => 'ContactImrequiredonupdate'
  7209. ),
  7210. '/div'
  7211. );
  7212. $this->assertTags($result, $expected);
  7213. $result = $this->Form->input('Contact.imrequiredoncreate');
  7214. $expected = array(
  7215. 'div' => array('class' => 'input text required'),
  7216. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7217. 'Imrequiredoncreate',
  7218. '/label',
  7219. 'input' => array(
  7220. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7221. 'id' => 'ContactImrequiredoncreate'
  7222. ),
  7223. '/div'
  7224. );
  7225. $this->assertTags($result, $expected);
  7226. $result = $this->Form->input('Contact.imrequiredonboth');
  7227. $expected = array(
  7228. 'div' => array('class' => 'input text required'),
  7229. 'label' => array('for' => 'ContactImrequiredonboth'),
  7230. 'Imrequiredonboth',
  7231. '/label',
  7232. 'input' => array(
  7233. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7234. 'id' => 'ContactImrequiredonboth'
  7235. ),
  7236. '/div'
  7237. );
  7238. $this->assertTags($result, $expected);
  7239. $result = $this->Form->input('Contact.imrequired');
  7240. $expected = array(
  7241. 'div' => array('class' => 'input text required'),
  7242. 'label' => array('for' => 'ContactImrequired'),
  7243. 'Imrequired',
  7244. '/label',
  7245. 'input' => array(
  7246. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7247. 'id' => 'ContactImrequired'
  7248. ),
  7249. '/div'
  7250. );
  7251. $this->assertTags($result, $expected);
  7252. }
  7253. /**
  7254. * Tests that the 'on' key validates as expected on update
  7255. *
  7256. * @return void
  7257. */
  7258. public function testRequiredOnUpdate() {
  7259. $this->Form->request->data['Contact']['id'] = 1;
  7260. $this->Form->create('Contact');
  7261. $result = $this->Form->input('Contact.imrequiredonupdate');
  7262. $expected = array(
  7263. 'div' => array('class' => 'input text required'),
  7264. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7265. 'Imrequiredonupdate',
  7266. '/label',
  7267. 'input' => array(
  7268. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7269. 'id' => 'ContactImrequiredonupdate'
  7270. ),
  7271. '/div'
  7272. );
  7273. $this->assertTags($result, $expected);
  7274. $result = $this->Form->input('Contact.imrequiredoncreate');
  7275. $expected = array(
  7276. 'div' => array('class' => 'input text'),
  7277. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7278. 'Imrequiredoncreate',
  7279. '/label',
  7280. 'input' => array(
  7281. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7282. 'id' => 'ContactImrequiredoncreate'
  7283. ),
  7284. '/div'
  7285. );
  7286. $this->assertTags($result, $expected);
  7287. $result = $this->Form->input('Contact.imrequiredonboth');
  7288. $expected = array(
  7289. 'div' => array('class' => 'input text required'),
  7290. 'label' => array('for' => 'ContactImrequiredonboth'),
  7291. 'Imrequiredonboth',
  7292. '/label',
  7293. 'input' => array(
  7294. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7295. 'id' => 'ContactImrequiredonboth'
  7296. ),
  7297. '/div'
  7298. );
  7299. $this->assertTags($result, $expected);
  7300. $result = $this->Form->input('Contact.imrequired');
  7301. $expected = array(
  7302. 'div' => array('class' => 'input text required'),
  7303. 'label' => array('for' => 'ContactImrequired'),
  7304. 'Imrequired',
  7305. '/label',
  7306. 'input' => array(
  7307. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7308. 'id' => 'ContactImrequired'
  7309. ),
  7310. '/div'
  7311. );
  7312. $this->assertTags($result, $expected);
  7313. }
  7314. /**
  7315. * Test inputDefaults setter and getter
  7316. *
  7317. * @return void
  7318. */
  7319. public function testInputDefaults() {
  7320. $this->Form->create('Contact');
  7321. $this->Form->inputDefaults(array(
  7322. 'label' => false,
  7323. 'div' => array(
  7324. 'style' => 'color: #000;'
  7325. )
  7326. ));
  7327. $result = $this->Form->input('Contact.field1');
  7328. $expected = array(
  7329. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7330. 'input' => array(
  7331. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7332. 'id' => 'ContactField1'
  7333. ),
  7334. '/div'
  7335. );
  7336. $this->assertTags($result, $expected);
  7337. $this->Form->inputDefaults(array(
  7338. 'div' => false,
  7339. 'label' => 'Label',
  7340. ));
  7341. $result = $this->Form->input('Contact.field1');
  7342. $expected = array(
  7343. 'label' => array('for' => 'ContactField1'),
  7344. 'Label',
  7345. '/label',
  7346. 'input' => array(
  7347. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7348. 'id' => 'ContactField1'
  7349. ),
  7350. );
  7351. $this->assertTags($result, $expected);
  7352. $this->Form->inputDefaults(array(
  7353. 'label' => false,
  7354. ), true);
  7355. $result = $this->Form->input('Contact.field1');
  7356. $expected = array(
  7357. 'input' => array(
  7358. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7359. 'id' => 'ContactField1'
  7360. ),
  7361. );
  7362. $this->assertTags($result, $expected);
  7363. $result = $this->Form->inputDefaults();
  7364. $expected = array(
  7365. 'div' => false,
  7366. 'label' => false,
  7367. );
  7368. $this->assertEqual($result, $expected);
  7369. }
  7370. }