PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://bitbucket.org/ttalov/fgcu_pci
PHP | 8067 lines | 6301 code | 634 blank | 1132 comment | 1 complexity | 5551855b4ae29c915a5dd3a8f108069b MD5 | raw file
  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. * When a select box has no options it should not be added to the fields list
  3893. * as it always fail post validation.
  3894. *
  3895. * @return void
  3896. */
  3897. public function testSelectNoSecureWithNoOptions() {
  3898. $this->Form->request['_Token'] = array('key' => 'testkey');
  3899. $this->assertEquals(array(), $this->Form->fields);
  3900. $this->Form->select(
  3901. 'Model.select',
  3902. array()
  3903. );
  3904. $this->assertEquals(array(), $this->Form->fields);
  3905. $this->Form->select(
  3906. 'Model.select',
  3907. array(),
  3908. array('empty' => true)
  3909. );
  3910. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3911. }
  3912. /**
  3913. * testInputMultipleCheckboxes method
  3914. *
  3915. * test input() resulting in multi select elements being generated.
  3916. *
  3917. * @return void
  3918. */
  3919. public function testInputMultipleCheckboxes() {
  3920. $result = $this->Form->input('Model.multi_field', array(
  3921. 'options' => array('first', 'second', 'third'),
  3922. 'multiple' => 'checkbox'
  3923. ));
  3924. $expected = array(
  3925. array('div' => array('class' => 'input select')),
  3926. array('label' => array('for' => 'ModelMultiField')),
  3927. 'Multi Field',
  3928. '/label',
  3929. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3930. array('div' => array('class' => 'checkbox')),
  3931. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3932. array('label' => array('for' => 'ModelMultiField0')),
  3933. 'first',
  3934. '/label',
  3935. '/div',
  3936. array('div' => array('class' => 'checkbox')),
  3937. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3938. array('label' => array('for' => 'ModelMultiField1')),
  3939. 'second',
  3940. '/label',
  3941. '/div',
  3942. array('div' => array('class' => 'checkbox')),
  3943. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3944. array('label' => array('for' => 'ModelMultiField2')),
  3945. 'third',
  3946. '/label',
  3947. '/div',
  3948. '/div'
  3949. );
  3950. $this->assertTags($result, $expected);
  3951. $result = $this->Form->input('Model.multi_field', array(
  3952. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3953. 'multiple' => 'checkbox'
  3954. ));
  3955. $expected = array(
  3956. array('div' => array('class' => 'input select')),
  3957. array('label' => array('for' => 'ModelMultiField')),
  3958. 'Multi Field',
  3959. '/label',
  3960. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3961. array('div' => array('class' => 'checkbox')),
  3962. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3963. array('label' => array('for' => 'ModelMultiFieldA')),
  3964. 'first',
  3965. '/label',
  3966. '/div',
  3967. array('div' => array('class' => 'checkbox')),
  3968. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3969. array('label' => array('for' => 'ModelMultiFieldB')),
  3970. 'second',
  3971. '/label',
  3972. '/div',
  3973. array('div' => array('class' => 'checkbox')),
  3974. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  3975. array('label' => array('for' => 'ModelMultiFieldC')),
  3976. 'third',
  3977. '/label',
  3978. '/div',
  3979. '/div'
  3980. );
  3981. $this->assertTags($result, $expected);
  3982. $result = $this->Form->input('Model.multi_field', array(
  3983. 'options' => array('1' => 'first'),
  3984. 'multiple' => 'checkbox',
  3985. 'label' => false,
  3986. 'div' => false
  3987. ));
  3988. $expected = array(
  3989. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3990. array('div' => array('class' => 'checkbox')),
  3991. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3992. array('label' => array('for' => 'ModelMultiField1')),
  3993. 'first',
  3994. '/label',
  3995. '/div'
  3996. );
  3997. $this->assertTags($result, $expected);
  3998. $result = $this->Form->input('Model.multi_field', array(
  3999. 'options' => array('2' => 'second'),
  4000. 'multiple' => 'checkbox',
  4001. 'label' => false,
  4002. 'div' => false
  4003. ));
  4004. $expected = array(
  4005. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4006. array('div' => array('class' => 'checkbox')),
  4007. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4008. array('label' => array('for' => 'ModelMultiField2')),
  4009. 'second',
  4010. '/label',
  4011. '/div'
  4012. );
  4013. $this->assertTags($result, $expected);
  4014. }
  4015. /**
  4016. * testSelectHiddenFieldOmission method
  4017. *
  4018. * test that select() with 'hiddenField' => false omits the hidden field
  4019. *
  4020. * @return void
  4021. */
  4022. public function testSelectHiddenFieldOmission() {
  4023. $result = $this->Form->select('Model.multi_field',
  4024. array('first', 'second'),
  4025. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4026. );
  4027. $expected = array(
  4028. array('div' => array('class' => 'checkbox')),
  4029. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4030. array('label' => array('for' => 'ModelMultiField0')),
  4031. 'first',
  4032. '/label',
  4033. '/div',
  4034. array('div' => array('class' => 'checkbox')),
  4035. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4036. array('label' => array('for' => 'ModelMultiField1')),
  4037. 'second',
  4038. '/label',
  4039. '/div'
  4040. );
  4041. $this->assertTags($result, $expected);
  4042. $result = $this->Form->input('Model.multi_field', array(
  4043. 'options' => array('first', 'second'),
  4044. 'multiple' => 'checkbox',
  4045. 'hiddenField' => false
  4046. ));
  4047. $expected = array(
  4048. array('div' => array('class' => 'input select')),
  4049. array('label' => array('for' => 'ModelMultiField')),
  4050. 'Multi Field',
  4051. '/label',
  4052. array('div' => array('class' => 'checkbox')),
  4053. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4054. array('label' => array('for' => 'ModelMultiField0')),
  4055. 'first',
  4056. '/label',
  4057. '/div',
  4058. array('div' => array('class' => 'checkbox')),
  4059. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4060. array('label' => array('for' => 'ModelMultiField1')),
  4061. 'second',
  4062. '/label',
  4063. '/div',
  4064. '/div'
  4065. );
  4066. $this->assertTags($result, $expected);
  4067. }
  4068. /**
  4069. * test that select() with multiple = checkbox works with overriding name attribute.
  4070. *
  4071. * @return void
  4072. */
  4073. public function testSelectCheckboxMultipleOverrideName() {
  4074. $result = $this->Form->input('category', array(
  4075. 'type' => 'select',
  4076. 'multiple' => 'checkbox',
  4077. 'name' => 'data[fish]',
  4078. 'options' => array('1', '2'),
  4079. 'div' => false,
  4080. 'label' => false,
  4081. ));
  4082. $expected = array(
  4083. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4084. array('div' => array('class' => 'checkbox')),
  4085. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4086. array('label' => array('for' => 'Category0')), '1', '/label',
  4087. '/div',
  4088. array('div' => array('class' => 'checkbox')),
  4089. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4090. array('label' => array('for' => 'Category1')), '2', '/label',
  4091. '/div'
  4092. );
  4093. $this->assertTags($result, $expected);
  4094. }
  4095. /**
  4096. * Test that 'id' overrides all the checkbox id's as well.
  4097. *
  4098. * @return void
  4099. */
  4100. public function testSelectCheckboxMultipleId() {
  4101. $result = $this->Form->select(
  4102. 'Model.multi_field',
  4103. array('first', 'second', 'third'),
  4104. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4105. );
  4106. $expected = array(
  4107. 'input' => array(
  4108. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4109. ),
  4110. array('div' => array('class' => 'checkbox')),
  4111. array('input' => array(
  4112. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4113. 'value' => '0', 'id' => 'CustomId0'
  4114. )),
  4115. array('label' => array('for' => 'CustomId0')),
  4116. 'first',
  4117. '/label',
  4118. '/div',
  4119. array('div' => array('class' => 'checkbox')),
  4120. array('input' => array(
  4121. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4122. 'value' => '1', 'id' => 'CustomId1'
  4123. )),
  4124. array('label' => array('for' => 'CustomId1')),
  4125. 'second',
  4126. '/label',
  4127. '/div',
  4128. array('div' => array('class' => 'checkbox')),
  4129. array('input' => array(
  4130. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4131. 'value' => '2', 'id' => 'CustomId2'
  4132. )),
  4133. array('label' => array('for' => 'CustomId2')),
  4134. 'third',
  4135. '/label',
  4136. '/div'
  4137. );
  4138. $this->assertTags($result, $expected);
  4139. }
  4140. /**
  4141. * testCheckbox method
  4142. *
  4143. * Test generation of checkboxes
  4144. *
  4145. * @return void
  4146. */
  4147. public function testCheckbox() {
  4148. $result = $this->Form->checkbox('Model.field');
  4149. $expected = array(
  4150. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4151. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4155. $expected = array(
  4156. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4157. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4158. );
  4159. $this->assertTags($result, $expected);
  4160. $Contact = ClassRegistry::getObject('Contact');
  4161. $Contact->validationErrors['field'] = 1;
  4162. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4163. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4164. $expected = array(
  4165. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4166. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4167. );
  4168. $this->assertTags($result, $expected);
  4169. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4170. $expected = array(
  4171. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4172. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4173. );
  4174. $this->assertTags($result, $expected);
  4175. $this->Form->request->data['Contact']['field'] = '';
  4176. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4177. $expected = array(
  4178. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4179. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4180. );
  4181. $this->assertTags($result, $expected);
  4182. $Contact->validationErrors = array();
  4183. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4184. $expected = array(
  4185. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4186. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4187. );
  4188. $this->assertTags($result, $expected);
  4189. $this->Form->request->data['Contact']['published'] = 1;
  4190. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4191. $expected = array(
  4192. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4193. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4194. );
  4195. $this->assertTags($result, $expected);
  4196. $this->Form->request->data['Contact']['published'] = 0;
  4197. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4198. $expected = array(
  4199. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4200. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4201. );
  4202. $this->assertTags($result, $expected);
  4203. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4204. $expected = array(
  4205. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4206. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4207. );
  4208. $this->assertTags($result, $expected);
  4209. $result = $this->Form->checkbox('CustomField.1.value');
  4210. $expected = array(
  4211. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4212. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4213. );
  4214. $this->assertTags($result, $expected);
  4215. }
  4216. /**
  4217. * test checkbox() with a custom name attribute
  4218. *
  4219. * @return void
  4220. */
  4221. public function testCheckboxCustomNameAttribute() {
  4222. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4223. $expected = array(
  4224. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4225. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4226. );
  4227. $this->assertTags($result, $expected);
  4228. }
  4229. /**
  4230. * test the checked option for checkboxes.
  4231. *
  4232. * @return void
  4233. */
  4234. public function testCheckboxCheckedOption() {
  4235. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4236. $expected = array(
  4237. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4238. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4239. );
  4240. $this->assertTags($result, $expected);
  4241. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4242. $expected = array(
  4243. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4244. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4245. );
  4246. $this->assertTags($result, $expected);
  4247. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4248. $expected = array(
  4249. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4250. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4251. );
  4252. $this->assertTags($result, $expected);
  4253. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4254. $expected = array(
  4255. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4256. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4257. );
  4258. $this->assertTags($result, $expected);
  4259. $this->Form->request->data['Model']['field'] = 1;
  4260. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4261. $expected = array(
  4262. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4263. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4264. );
  4265. $this->assertTags($result, $expected);
  4266. }
  4267. /**
  4268. * Test that disabled attribute works on both the checkbox and hidden input.
  4269. *
  4270. * @return void
  4271. */
  4272. public function testCheckboxDisabling() {
  4273. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4274. $expected = array(
  4275. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4276. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4277. );
  4278. $this->assertTags($result, $expected);
  4279. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4280. $expected = array(
  4281. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4282. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4283. );
  4284. $this->assertTags($result, $expected);
  4285. }
  4286. /**
  4287. * Test that the hidden input for checkboxes can be omitted or set to a
  4288. * specific value.
  4289. *
  4290. * @return void
  4291. */
  4292. public function testCheckboxHiddenField() {
  4293. $result = $this->Form->input('UserForm.something', array(
  4294. 'type' => 'checkbox',
  4295. 'hiddenField' => false
  4296. ));
  4297. $expected = array(
  4298. 'div' => array('class' => 'input checkbox'),
  4299. array('input' => array(
  4300. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4301. 'value' => '1', 'id' => 'UserFormSomething'
  4302. )),
  4303. 'label' => array('for' => 'UserFormSomething'),
  4304. 'Something',
  4305. '/label',
  4306. '/div'
  4307. );
  4308. $this->assertTags($result, $expected);
  4309. $result = $this->Form->input('UserForm.something', array(
  4310. 'type' => 'checkbox',
  4311. 'value' => 'Y',
  4312. 'hiddenField' => 'N',
  4313. ));
  4314. $expected = array(
  4315. 'div' => array('class' => 'input checkbox'),
  4316. array('input' => array(
  4317. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4318. 'value' => 'N', 'id' => 'UserFormSomething_'
  4319. )),
  4320. array('input' => array(
  4321. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4322. 'value' => 'Y', 'id' => 'UserFormSomething'
  4323. )),
  4324. 'label' => array('for' => 'UserFormSomething'),
  4325. 'Something',
  4326. '/label',
  4327. '/div'
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. }
  4331. /**
  4332. * testDateTime method
  4333. *
  4334. * Test generation of date/time select elements
  4335. *
  4336. * @return void
  4337. */
  4338. public function testDateTime() {
  4339. extract($this->dateRegex);
  4340. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4341. $now = strtotime('now');
  4342. $expected = array(
  4343. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4344. $daysRegex,
  4345. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4346. date('j', $now),
  4347. '/option',
  4348. '*/select',
  4349. '-',
  4350. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4351. $monthsRegex,
  4352. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4353. date('F', $now),
  4354. '/option',
  4355. '*/select',
  4356. '-',
  4357. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4358. $yearsRegex,
  4359. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4360. date('Y', $now),
  4361. '/option',
  4362. '*/select',
  4363. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4364. $hoursRegex,
  4365. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4366. date('g', $now),
  4367. '/option',
  4368. '*/select',
  4369. ':',
  4370. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4371. $minutesRegex,
  4372. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4373. date('i', $now),
  4374. '/option',
  4375. '*/select',
  4376. ' ',
  4377. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4378. $meridianRegex,
  4379. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4380. date('a', $now),
  4381. '/option',
  4382. '*/select'
  4383. );
  4384. $this->assertTags($result, $expected);
  4385. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4386. $expected = array(
  4387. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4388. $daysRegex,
  4389. array('option' => array('value' => '')),
  4390. '/option',
  4391. '*/select',
  4392. '-',
  4393. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4394. $monthsRegex,
  4395. array('option' => array('value' => '')),
  4396. '/option',
  4397. '*/select',
  4398. '-',
  4399. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4400. $yearsRegex,
  4401. array('option' => array('value' => '')),
  4402. '/option',
  4403. '*/select',
  4404. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4405. $hoursRegex,
  4406. array('option' => array('value' => '')),
  4407. '/option',
  4408. '*/select',
  4409. ':',
  4410. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4411. $minutesRegex,
  4412. array('option' => array('value' => '')),
  4413. '/option',
  4414. '*/select',
  4415. ' ',
  4416. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4417. $meridianRegex,
  4418. array('option' => array('value' => '')),
  4419. '/option',
  4420. '*/select'
  4421. );
  4422. $this->assertTags($result, $expected);
  4423. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4424. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4425. $this->assertTags($result, $expected);
  4426. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4427. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4428. $this->assertTags($result, $expected);
  4429. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4430. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4431. $expected = array(
  4432. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4433. $daysRegex,
  4434. array('option' => array('value' => '')),
  4435. '/option',
  4436. '*/select',
  4437. '-',
  4438. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4439. $monthsRegex,
  4440. array('option' => array('value' => '')),
  4441. '/option',
  4442. '*/select',
  4443. '-',
  4444. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4445. $yearsRegex,
  4446. array('option' => array('value' => '')),
  4447. '/option',
  4448. '*/select',
  4449. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4450. $hoursRegex,
  4451. array('option' => array('value' => '')),
  4452. '/option',
  4453. '*/select',
  4454. ':',
  4455. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4456. $minutesRegex,
  4457. array('option' => array('value' => '')),
  4458. '/option',
  4459. array('option' => array('value' => '00')),
  4460. '00',
  4461. '/option',
  4462. array('option' => array('value' => '05')),
  4463. '05',
  4464. '/option',
  4465. array('option' => array('value' => '10')),
  4466. '10',
  4467. '/option',
  4468. '*/select',
  4469. ' ',
  4470. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4471. $meridianRegex,
  4472. array('option' => array('value' => '')),
  4473. '/option',
  4474. '*/select'
  4475. );
  4476. $this->assertTags($result, $expected);
  4477. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4478. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4479. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4480. $this->Form->request->data['Contact']['data'] = null;
  4481. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4482. $expected = array(
  4483. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4484. $daysRegex,
  4485. array('option' => array('value' => '')),
  4486. '/option',
  4487. '*/select',
  4488. '-',
  4489. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4490. $monthsRegex,
  4491. array('option' => array('value' => '')),
  4492. '/option',
  4493. '*/select',
  4494. '-',
  4495. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4496. $yearsRegex,
  4497. array('option' => array('value' => '')),
  4498. '/option',
  4499. '*/select',
  4500. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4501. $hoursRegex,
  4502. array('option' => array('value' => '')),
  4503. '/option',
  4504. '*/select',
  4505. ':',
  4506. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4507. $minutesRegex,
  4508. array('option' => array('value' => '')),
  4509. '/option',
  4510. '*/select',
  4511. ' ',
  4512. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4513. $meridianRegex,
  4514. array('option' => array('value' => '')),
  4515. '/option',
  4516. '*/select'
  4517. );
  4518. $this->assertTags($result, $expected);
  4519. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4520. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4521. $now = strtotime($this->Form->data['Model']['field']);
  4522. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4523. $expected = array(
  4524. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4525. $daysRegex,
  4526. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4527. date('j', $now),
  4528. '/option',
  4529. '*/select',
  4530. '-',
  4531. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4532. $monthsRegex,
  4533. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4534. date('F', $now),
  4535. '/option',
  4536. '*/select',
  4537. '-',
  4538. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4539. $yearsRegex,
  4540. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4541. date('Y', $now),
  4542. '/option',
  4543. '*/select',
  4544. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4545. $hoursRegex,
  4546. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4547. date('g', $now),
  4548. '/option',
  4549. '*/select',
  4550. ':',
  4551. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4552. $minutesRegex,
  4553. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4554. date('i', $now),
  4555. '/option',
  4556. '*/select',
  4557. ' ',
  4558. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4559. $meridianRegex,
  4560. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4561. date('a', $now),
  4562. '/option',
  4563. '*/select'
  4564. );
  4565. $this->assertTags($result, $expected);
  4566. $selected = strtotime('2008-10-26 12:33:00');
  4567. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4568. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4569. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4570. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4571. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4572. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4573. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4574. $this->Form->create('Contact');
  4575. $result = $this->Form->input('published');
  4576. $now = strtotime('now');
  4577. $expected = array(
  4578. 'div' => array('class' => 'input date'),
  4579. 'label' => array('for' => 'ContactPublishedMonth'),
  4580. 'Published',
  4581. '/label',
  4582. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4583. $monthsRegex,
  4584. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4585. date('F', $now),
  4586. '/option',
  4587. '*/select',
  4588. '-',
  4589. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4590. $daysRegex,
  4591. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4592. date('j', $now),
  4593. '/option',
  4594. '*/select',
  4595. '-',
  4596. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4597. $yearsRegex,
  4598. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4599. date('Y', $now),
  4600. '/option',
  4601. '*/select',
  4602. '/div'
  4603. );
  4604. $this->assertTags($result, $expected);
  4605. $result = $this->Form->input('published2', array('type' => 'date'));
  4606. $now = strtotime('now');
  4607. $expected = array(
  4608. 'div' => array('class' => 'input date'),
  4609. 'label' => array('for' => 'ContactPublished2Month'),
  4610. 'Published2',
  4611. '/label',
  4612. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4613. $monthsRegex,
  4614. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4615. date('F', $now),
  4616. '/option',
  4617. '*/select',
  4618. '-',
  4619. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4620. $daysRegex,
  4621. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4622. date('j', $now),
  4623. '/option',
  4624. '*/select',
  4625. '-',
  4626. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4627. $yearsRegex,
  4628. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4629. date('Y', $now),
  4630. '/option',
  4631. '*/select',
  4632. '/div'
  4633. );
  4634. $this->assertTags($result, $expected);
  4635. $this->Form->create('Contact');
  4636. $result = $this->Form->input('published', array('monthNames' => false));
  4637. $now = strtotime('now');
  4638. $expected = array(
  4639. 'div' => array('class' => 'input date'),
  4640. 'label' => array('for' => 'ContactPublishedMonth'),
  4641. 'Published',
  4642. '/label',
  4643. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4644. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4645. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4646. date('m', $now),
  4647. '/option',
  4648. '*/select',
  4649. '-',
  4650. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4651. $daysRegex,
  4652. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4653. date('j', $now),
  4654. '/option',
  4655. '*/select',
  4656. '-',
  4657. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4658. $yearsRegex,
  4659. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4660. date('Y', $now),
  4661. '/option',
  4662. '*/select',
  4663. '/div'
  4664. );
  4665. $this->assertTags($result, $expected);
  4666. $result = $this->Form->input('published', array('type' => 'time'));
  4667. $now = strtotime('now');
  4668. $expected = array(
  4669. 'div' => array('class' => 'input time'),
  4670. 'label' => array('for' => 'ContactPublishedHour'),
  4671. 'Published',
  4672. '/label',
  4673. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4674. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4675. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4676. date('g', $now),
  4677. '/option',
  4678. '*/select',
  4679. ':',
  4680. );
  4681. $this->assertTags($result, $expected);
  4682. $result = $this->Form->input('published', array(
  4683. 'timeFormat' => 24,
  4684. 'interval' => 5,
  4685. 'selected' => strtotime('2009-09-03 13:37:00'),
  4686. 'type' => 'datetime'
  4687. ));
  4688. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4689. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4690. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4691. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4692. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4693. $this->assertNoErrors();
  4694. $this->Form->request->data['Contact'] = array(
  4695. 'date' => array(
  4696. 'day' => '',
  4697. 'month' => '',
  4698. 'year' => '',
  4699. 'hour' => '',
  4700. 'min' => '',
  4701. 'meridian' => ''
  4702. )
  4703. );
  4704. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4705. }
  4706. /**
  4707. * test that datetime() and default values work.
  4708. *
  4709. * @return void
  4710. */
  4711. public function testDatetimeWithDefault() {
  4712. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4713. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4714. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4715. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4716. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4717. 'default' => '2009-06-01 11:15:30'
  4718. ));
  4719. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4720. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4721. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4722. }
  4723. /**
  4724. * test that bogus non-date time data doesn't cause errors.
  4725. *
  4726. * @return void
  4727. */
  4728. public function testDateTimeWithBogusData() {
  4729. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4730. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4731. }
  4732. /**
  4733. * testDateTimeEmptyAsArray
  4734. *
  4735. * @return void
  4736. */
  4737. public function testDateTimeEmptyAsArray() {
  4738. $result = $this->Form->dateTime('Contact.date',
  4739. 'DMY',
  4740. '12',
  4741. array(
  4742. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4743. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4744. )
  4745. )
  4746. );
  4747. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4748. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4749. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4750. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4751. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4752. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4753. $result = $this->Form->dateTime('Contact.date',
  4754. 'DMY',
  4755. '12',
  4756. array(
  4757. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4758. )
  4759. );
  4760. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4761. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4762. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4763. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4764. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4765. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4766. }
  4767. /**
  4768. * testFormDateTimeMulti method
  4769. *
  4770. * test multiple datetime element generation
  4771. *
  4772. * @return void
  4773. */
  4774. public function testFormDateTimeMulti() {
  4775. extract($this->dateRegex);
  4776. $result = $this->Form->dateTime('Contact.1.updated');
  4777. $expected = array(
  4778. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4779. $daysRegex,
  4780. array('option' => array('value' => '')),
  4781. '/option',
  4782. '*/select',
  4783. '-',
  4784. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4785. $monthsRegex,
  4786. array('option' => array('value' => '')),
  4787. '/option',
  4788. '*/select',
  4789. '-',
  4790. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4791. $yearsRegex,
  4792. array('option' => array('value' => '')),
  4793. '/option',
  4794. '*/select',
  4795. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4796. $hoursRegex,
  4797. array('option' => array('value' => '')),
  4798. '/option',
  4799. '*/select',
  4800. ':',
  4801. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4802. $minutesRegex,
  4803. array('option' => array('value' => '')),
  4804. '/option',
  4805. '*/select',
  4806. ' ',
  4807. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4808. $meridianRegex,
  4809. array('option' => array('value' => '')),
  4810. '/option',
  4811. '*/select'
  4812. );
  4813. $this->assertTags($result, $expected);
  4814. $result = $this->Form->dateTime('Contact.2.updated');
  4815. $expected = array(
  4816. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4817. $daysRegex,
  4818. array('option' => array('value' => '')),
  4819. '/option',
  4820. '*/select',
  4821. '-',
  4822. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4823. $monthsRegex,
  4824. array('option' => array('value' => '')),
  4825. '/option',
  4826. '*/select',
  4827. '-',
  4828. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4829. $yearsRegex,
  4830. array('option' => array('value' => '')),
  4831. '/option',
  4832. '*/select',
  4833. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4834. $hoursRegex,
  4835. array('option' => array('value' => '')),
  4836. '/option',
  4837. '*/select',
  4838. ':',
  4839. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4840. $minutesRegex,
  4841. array('option' => array('value' => '')),
  4842. '/option',
  4843. '*/select',
  4844. ' ',
  4845. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4846. $meridianRegex,
  4847. array('option' => array('value' => '')),
  4848. '/option',
  4849. '*/select'
  4850. );
  4851. $this->assertTags($result, $expected);
  4852. }
  4853. /**
  4854. * When changing the date format, the label should always focus the first select box when
  4855. * clicked.
  4856. *
  4857. * @return void
  4858. */
  4859. public function testDateTimeLabelIdMatchesFirstInput() {
  4860. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4861. $this->assertContains('label for="ModelDateMonth"', $result);
  4862. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4863. $this->assertContains('label for="ModelDateDay"', $result);
  4864. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4865. $this->assertContains('label for="ModelDateYear"', $result);
  4866. }
  4867. /**
  4868. * testMonth method
  4869. *
  4870. * @return void
  4871. */
  4872. public function testMonth() {
  4873. $result = $this->Form->month('Model.field');
  4874. $expected = array(
  4875. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4876. array('option' => array('value' => '')),
  4877. '/option',
  4878. array('option' => array('value' => '01')),
  4879. date('F', strtotime('2008-01-01 00:00:00')),
  4880. '/option',
  4881. array('option' => array('value' => '02')),
  4882. date('F', strtotime('2008-02-01 00:00:00')),
  4883. '/option',
  4884. '*/select',
  4885. );
  4886. $this->assertTags($result, $expected);
  4887. $result = $this->Form->month('Model.field', array('empty' => true));
  4888. $expected = array(
  4889. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4890. array('option' => array('value' => '')),
  4891. '/option',
  4892. array('option' => array('value' => '01')),
  4893. date('F', strtotime('2008-01-01 00:00:00')),
  4894. '/option',
  4895. array('option' => array('value' => '02')),
  4896. date('F', strtotime('2008-02-01 00:00:00')),
  4897. '/option',
  4898. '*/select',
  4899. );
  4900. $this->assertTags($result, $expected);
  4901. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4902. $expected = array(
  4903. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4904. array('option' => array('value' => '')),
  4905. '/option',
  4906. array('option' => array('value' => '01')),
  4907. '01',
  4908. '/option',
  4909. array('option' => array('value' => '02')),
  4910. '02',
  4911. '/option',
  4912. '*/select',
  4913. );
  4914. $this->assertTags($result, $expected);
  4915. $monthNames = array(
  4916. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4917. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4918. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4919. $expected = array(
  4920. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4921. array('option' => array('value' => '')),
  4922. '/option',
  4923. array('option' => array('value' => '01')),
  4924. 'Jan',
  4925. '/option',
  4926. array('option' => array('value' => '02')),
  4927. 'Feb',
  4928. '/option',
  4929. '*/select',
  4930. );
  4931. $this->assertTags($result, $expected);
  4932. }
  4933. /**
  4934. * testDay method
  4935. *
  4936. * @return void
  4937. */
  4938. public function testDay() {
  4939. extract($this->dateRegex);
  4940. $result = $this->Form->day('Model.field', array('value' => false));
  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. '/select',
  4953. );
  4954. $this->assertTags($result, $expected);
  4955. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4956. $result = $this->Form->day('Model.field');
  4957. $expected = array(
  4958. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4959. array('option' => array('value' => '')),
  4960. '/option',
  4961. array('option' => array('value' => '01')),
  4962. '1',
  4963. '/option',
  4964. array('option' => array('value' => '02')),
  4965. '2',
  4966. '/option',
  4967. $daysRegex,
  4968. array('option' => array('value' => '10', 'selected' => 'selected')),
  4969. '10',
  4970. '/option',
  4971. $daysRegex,
  4972. '/select',
  4973. );
  4974. $this->assertTags($result, $expected);
  4975. $this->Form->request->data['Model']['field'] = '';
  4976. $result = $this->Form->day('Model.field', array('value' => '10'));
  4977. $expected = array(
  4978. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4979. array('option' => array('value' => '')),
  4980. '/option',
  4981. array('option' => array('value' => '01')),
  4982. '1',
  4983. '/option',
  4984. array('option' => array('value' => '02')),
  4985. '2',
  4986. '/option',
  4987. $daysRegex,
  4988. array('option' => array('value' => '10', 'selected' => 'selected')),
  4989. '10',
  4990. '/option',
  4991. $daysRegex,
  4992. '/select',
  4993. );
  4994. $this->assertTags($result, $expected);
  4995. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4996. $result = $this->Form->day('Model.field', array('value' => true));
  4997. $expected = array(
  4998. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4999. array('option' => array('value' => '')),
  5000. '/option',
  5001. array('option' => array('value' => '01')),
  5002. '1',
  5003. '/option',
  5004. array('option' => array('value' => '02')),
  5005. '2',
  5006. '/option',
  5007. $daysRegex,
  5008. array('option' => array('value' => '10', 'selected' => 'selected')),
  5009. '10',
  5010. '/option',
  5011. $daysRegex,
  5012. '/select',
  5013. );
  5014. $this->assertTags($result, $expected);
  5015. }
  5016. /**
  5017. * testMinute method
  5018. *
  5019. * @return void
  5020. */
  5021. public function testMinute() {
  5022. extract($this->dateRegex);
  5023. $result = $this->Form->minute('Model.field');
  5024. $expected = array(
  5025. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5026. array('option' => array('value' => '')),
  5027. '/option',
  5028. array('option' => array('value' => '00')),
  5029. '00',
  5030. '/option',
  5031. array('option' => array('value' => '01')),
  5032. '01',
  5033. '/option',
  5034. array('option' => array('value' => '02')),
  5035. '02',
  5036. '/option',
  5037. $minutesRegex,
  5038. '/select',
  5039. );
  5040. $this->assertTags($result, $expected);
  5041. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5042. $result = $this->Form->minute('Model.field');
  5043. $expected = array(
  5044. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5045. array('option' => array('value' => '')),
  5046. '/option',
  5047. array('option' => array('value' => '00')),
  5048. '00',
  5049. '/option',
  5050. array('option' => array('value' => '01')),
  5051. '01',
  5052. '/option',
  5053. array('option' => array('value' => '02')),
  5054. '02',
  5055. '/option',
  5056. $minutesRegex,
  5057. array('option' => array('value' => '12', 'selected' => 'selected')),
  5058. '12',
  5059. '/option',
  5060. $minutesRegex,
  5061. '/select',
  5062. );
  5063. $this->assertTags($result, $expected);
  5064. $this->Form->request->data['Model']['field'] = '';
  5065. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5066. $expected = array(
  5067. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5068. array('option' => array('value' => '')),
  5069. '/option',
  5070. array('option' => array('value' => '00')),
  5071. '00',
  5072. '/option',
  5073. array('option' => array('value' => '05')),
  5074. '05',
  5075. '/option',
  5076. array('option' => array('value' => '10')),
  5077. '10',
  5078. '/option',
  5079. $minutesRegex,
  5080. '/select',
  5081. );
  5082. $this->assertTags($result, $expected);
  5083. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5084. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5085. $expected = array(
  5086. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5087. array('option' => array('value' => '')),
  5088. '/option',
  5089. array('option' => array('value' => '00')),
  5090. '00',
  5091. '/option',
  5092. array('option' => array('value' => '05')),
  5093. '05',
  5094. '/option',
  5095. array('option' => array('value' => '10', 'selected' => 'selected')),
  5096. '10',
  5097. '/option',
  5098. $minutesRegex,
  5099. '/select',
  5100. );
  5101. $this->assertTags($result, $expected);
  5102. }
  5103. /**
  5104. * testHour method
  5105. *
  5106. * @return void
  5107. */
  5108. public function testHour() {
  5109. extract($this->dateRegex);
  5110. $result = $this->Form->hour('Model.field', false);
  5111. $expected = array(
  5112. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5113. array('option' => array('value' => '')),
  5114. '/option',
  5115. array('option' => array('value' => '01')),
  5116. '1',
  5117. '/option',
  5118. array('option' => array('value' => '02')),
  5119. '2',
  5120. '/option',
  5121. $hoursRegex,
  5122. '/select',
  5123. );
  5124. $this->assertTags($result, $expected);
  5125. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5126. $result = $this->Form->hour('Model.field', false);
  5127. $expected = array(
  5128. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5129. array('option' => array('value' => '')),
  5130. '/option',
  5131. array('option' => array('value' => '01')),
  5132. '1',
  5133. '/option',
  5134. array('option' => array('value' => '02')),
  5135. '2',
  5136. '/option',
  5137. $hoursRegex,
  5138. array('option' => array('value' => '12', 'selected' => 'selected')),
  5139. '12',
  5140. '/option',
  5141. '/select',
  5142. );
  5143. $this->assertTags($result, $expected);
  5144. $this->Form->request->data['Model']['field'] = '';
  5145. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5146. $expected = array(
  5147. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5148. array('option' => array('value' => '')),
  5149. '/option',
  5150. array('option' => array('value' => '00')),
  5151. '0',
  5152. '/option',
  5153. array('option' => array('value' => '01')),
  5154. '1',
  5155. '/option',
  5156. array('option' => array('value' => '02')),
  5157. '2',
  5158. '/option',
  5159. $hoursRegex,
  5160. array('option' => array('value' => '23', 'selected' => 'selected')),
  5161. '23',
  5162. '/option',
  5163. '/select',
  5164. );
  5165. $this->assertTags($result, $expected);
  5166. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5167. $result = $this->Form->hour('Model.field', true);
  5168. $expected = array(
  5169. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5170. array('option' => array('value' => '')),
  5171. '/option',
  5172. array('option' => array('value' => '00', 'selected' => 'selected')),
  5173. '0',
  5174. '/option',
  5175. array('option' => array('value' => '01')),
  5176. '1',
  5177. '/option',
  5178. array('option' => array('value' => '02')),
  5179. '2',
  5180. '/option',
  5181. $hoursRegex,
  5182. '/select',
  5183. );
  5184. $this->assertTags($result, $expected);
  5185. unset($this->Form->request->data['Model']['field']);
  5186. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5187. $thisHour = date('H');
  5188. $optValue = date('G');
  5189. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5190. }
  5191. /**
  5192. * testYear method
  5193. *
  5194. * @return void
  5195. */
  5196. public function testYear() {
  5197. $result = $this->Form->year('Model.field', 2006, 2007);
  5198. $expected = array(
  5199. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5200. array('option' => array('value' => '')),
  5201. '/option',
  5202. array('option' => array('value' => '2007')),
  5203. '2007',
  5204. '/option',
  5205. array('option' => array('value' => '2006')),
  5206. '2006',
  5207. '/option',
  5208. '/select',
  5209. );
  5210. $this->assertTags($result, $expected);
  5211. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5212. $expected = array(
  5213. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5214. array('option' => array('value' => '')),
  5215. '/option',
  5216. array('option' => array('value' => '2006')),
  5217. '2006',
  5218. '/option',
  5219. array('option' => array('value' => '2007')),
  5220. '2007',
  5221. '/option',
  5222. '/select',
  5223. );
  5224. $this->assertTags($result, $expected);
  5225. $this->request->data['Contact']['published'] = '';
  5226. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5227. $expected = array(
  5228. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5229. array('option' => array('value' => '')),
  5230. '/option',
  5231. array('option' => array('value' => '2007')),
  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));
  5242. $expected = array(
  5243. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5244. array('option' => array('value' => '2007')),
  5245. '2007',
  5246. '/option',
  5247. array('option' => array('value' => '2006', 'selected' => 'selected')),
  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, 2007, array('value' => false));
  5255. $expected = array(
  5256. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5257. array('option' => array('value' => '')),
  5258. '/option',
  5259. array('option' => array('value' => '2007')),
  5260. '2007',
  5261. '/option',
  5262. array('option' => array('value' => '2006')),
  5263. '2006',
  5264. '/option',
  5265. '/select',
  5266. );
  5267. $this->assertTags($result, $expected);
  5268. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5269. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5270. $expected = array(
  5271. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5272. array('option' => array('value' => '2007')),
  5273. '2007',
  5274. '/option',
  5275. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5276. '2006',
  5277. '/option',
  5278. '/select',
  5279. );
  5280. $this->assertTags($result, $expected);
  5281. $this->Form->request->data['Contact']['published'] = '';
  5282. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5283. $expected = array(
  5284. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5285. array('option' => array('value' => '')),
  5286. '/option',
  5287. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5288. '2007',
  5289. '/option',
  5290. array('option' => array('value' => '2006')),
  5291. '2006',
  5292. '/option',
  5293. '/select',
  5294. );
  5295. $this->assertTags($result, $expected);
  5296. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5297. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5298. $expected = array(
  5299. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5300. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5301. '2007',
  5302. '/option',
  5303. array('option' => array('value' => '2006')),
  5304. '2006',
  5305. '/option',
  5306. '/select',
  5307. );
  5308. $this->assertTags($result, $expected);
  5309. $this->Form->request->data['Contact']['published'] = '';
  5310. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5311. $expected = array(
  5312. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5313. array('option' => array('value' => '2008')),
  5314. '2008',
  5315. '/option',
  5316. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5317. '2007',
  5318. '/option',
  5319. array('option' => array('value' => '2006')),
  5320. '2006',
  5321. '/option',
  5322. '/select',
  5323. );
  5324. $this->assertTags($result, $expected);
  5325. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5326. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5327. $expected = array(
  5328. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5329. array('option' => array('value' => '2008')),
  5330. '2008',
  5331. '/option',
  5332. array('option' => array('value' => '2007')),
  5333. '2007',
  5334. '/option',
  5335. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5336. '2006',
  5337. '/option',
  5338. '/select',
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $this->Form->request->data = array();
  5342. $this->Form->create('Contact');
  5343. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5344. $expected = array(
  5345. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5346. array('option' => array('value' => '2008')),
  5347. '2008',
  5348. '/option',
  5349. array('option' => array('value' => '2007')),
  5350. '2007',
  5351. '/option',
  5352. array('option' => array('value' => '2006')),
  5353. '2006',
  5354. '/option',
  5355. '/select',
  5356. );
  5357. $this->assertTags($result, $expected);
  5358. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5359. $this->assertContains('data[Contact][published][year]', $result);
  5360. }
  5361. /**
  5362. * testTextArea method
  5363. *
  5364. * @return void
  5365. */
  5366. public function testTextArea() {
  5367. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5368. $result = $this->Form->textarea('Model.field');
  5369. $expected = array(
  5370. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5371. 'some test data',
  5372. '/textarea',
  5373. );
  5374. $this->assertTags($result, $expected);
  5375. $result = $this->Form->textarea('Model.tmp');
  5376. $expected = array(
  5377. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5378. '/textarea',
  5379. );
  5380. $this->assertTags($result, $expected);
  5381. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5382. $result = $this->Form->textarea('Model.field');
  5383. $expected = array(
  5384. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5385. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5386. '/textarea',
  5387. );
  5388. $this->assertTags($result, $expected);
  5389. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5390. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5391. $expected = array(
  5392. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5393. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5394. '/textarea',
  5395. );
  5396. $this->assertTags($result, $expected);
  5397. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5398. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5399. $expected = array(
  5400. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5401. '/textarea'
  5402. );
  5403. $this->assertTags($result, $expected);
  5404. }
  5405. /**
  5406. * testTextAreaWithStupidCharacters method
  5407. *
  5408. * test text area with non-ascii characters
  5409. *
  5410. * @return void
  5411. */
  5412. public function testTextAreaWithStupidCharacters() {
  5413. $this->loadFixtures('Post');
  5414. $result = $this->Form->input('Post.content', array(
  5415. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5416. ));
  5417. $expected = array(
  5418. 'div' => array('class' => 'input text'),
  5419. 'label' => array('for' => 'PostContent'),
  5420. 'Current Text',
  5421. '/label',
  5422. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5423. 'GREAT®',
  5424. '/textarea',
  5425. '/div'
  5426. );
  5427. $this->assertTags($result, $expected);
  5428. }
  5429. /**
  5430. * testHiddenField method
  5431. *
  5432. * @return void
  5433. */
  5434. public function testHiddenField() {
  5435. $Contact = ClassRegistry::getObject('Contact');
  5436. $Contact->validationErrors['field'] = 1;
  5437. $this->Form->request->data['Contact']['field'] = 'test';
  5438. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5439. $this->assertTags($result, array(
  5440. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5441. );
  5442. }
  5443. /**
  5444. * testFileUploadField method
  5445. *
  5446. * @return void
  5447. */
  5448. public function testFileUploadField() {
  5449. $result = $this->Form->file('Model.upload');
  5450. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5451. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5452. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5453. $expected = array(
  5454. 'div' => array('class' => 'input file'),
  5455. 'label' => array('for' => 'ModelUpload'),
  5456. 'Upload',
  5457. '/label',
  5458. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5459. '/div'
  5460. );
  5461. $this->assertTags($result, $expected);
  5462. }
  5463. /**
  5464. * test File upload input on a model not used in create();
  5465. *
  5466. * @return void
  5467. */
  5468. public function testFileUploadOnOtherModel() {
  5469. $this->Form->create('ValidateUser', array('type' => 'file'));
  5470. $result = $this->Form->file('ValidateProfile.city');
  5471. $expected = array(
  5472. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5473. );
  5474. $this->assertTags($result, $expected);
  5475. }
  5476. /**
  5477. * testButton method
  5478. *
  5479. * @return void
  5480. */
  5481. public function testButton() {
  5482. $result = $this->Form->button('Hi');
  5483. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5484. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5485. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5486. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5487. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5488. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5489. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5490. $result = $this->Form->button('No type', array('type' => false));
  5491. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5492. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5493. $this->assertNotRegExp('/\&039/', $result);
  5494. }
  5495. /**
  5496. * Test that button() makes unlocked fields by default.
  5497. *
  5498. * @return void
  5499. */
  5500. public function testButtonUnlockedByDefault() {
  5501. $this->Form->request->params['_Token']['key'] = 'secured';
  5502. $this->Form->button('Save', array('name' => 'save'));
  5503. $this->Form->button('Clear');
  5504. $result = $this->Form->unlockField();
  5505. $this->assertEquals(array('save'), $result);
  5506. }
  5507. /**
  5508. * testPostButton method
  5509. *
  5510. * @return void
  5511. */
  5512. public function testPostButton() {
  5513. $result = $this->Form->postButton('Hi', '/controller/action');
  5514. $this->assertTags($result, array(
  5515. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5516. 'div' => array('style' => 'display:none;'),
  5517. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5518. '/div',
  5519. 'button' => array('type' => 'submit'),
  5520. 'Hi',
  5521. '/button',
  5522. '/form'
  5523. ));
  5524. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5525. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5526. }
  5527. /**
  5528. * Test that postButton adds _Token fields.
  5529. *
  5530. * @return void
  5531. */
  5532. public function testSecurePostButton() {
  5533. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5534. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5535. $expected = array(
  5536. 'form' => array(
  5537. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5538. ),
  5539. array('div' => array('style' => 'display:none;')),
  5540. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5541. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5542. '/div',
  5543. 'button' => array('type' => 'submit'),
  5544. 'Delete',
  5545. '/button',
  5546. array('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. );
  5552. $this->assertTags($result, $expected);
  5553. }
  5554. /**
  5555. * testPostLink method
  5556. *
  5557. * @return void
  5558. */
  5559. public function testPostLink() {
  5560. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5561. $this->assertTags($result, array(
  5562. 'form' => array(
  5563. 'method' => 'post', 'action' => '/posts/delete/1',
  5564. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5565. ),
  5566. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5567. '/form',
  5568. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5569. 'Delete',
  5570. '/a'
  5571. ));
  5572. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5573. $this->assertTags($result, array(
  5574. 'form' => array(
  5575. 'method' => 'post', 'action' => '/posts/delete/1',
  5576. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5577. ),
  5578. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5579. '/form',
  5580. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5581. 'Delete',
  5582. '/a'
  5583. ));
  5584. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5585. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5586. }
  5587. /**
  5588. * Test that postLink adds _Token fields.
  5589. *
  5590. * @return void
  5591. */
  5592. public function testSecurePostLink() {
  5593. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5594. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5595. $expected = array(
  5596. 'form' => array(
  5597. 'method' => 'post', 'action' => '/posts/delete/1',
  5598. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5599. ),
  5600. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5601. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5602. 'div' => array('style' => 'display:none;'),
  5603. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5604. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5605. '/div',
  5606. '/form',
  5607. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5608. 'Delete',
  5609. '/a'
  5610. );
  5611. $this->assertTags($result, $expected);
  5612. }
  5613. /**
  5614. * testSubmitButton method
  5615. *
  5616. * @return void
  5617. */
  5618. public function testSubmitButton() {
  5619. $result = $this->Form->submit('');
  5620. $expected = array(
  5621. 'div' => array('class' => 'submit'),
  5622. 'input' => array('type' => 'submit', 'value' => ''),
  5623. '/div'
  5624. );
  5625. $this->assertTags($result, $expected);
  5626. $result = $this->Form->submit('Test Submit');
  5627. $expected = array(
  5628. 'div' => array('class' => 'submit'),
  5629. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5630. '/div'
  5631. );
  5632. $this->assertTags($result, $expected);
  5633. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5634. $expected = array(
  5635. 'span' => array('class' => 'submit'),
  5636. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5637. '/span'
  5638. );
  5639. $this->assertTags($result, $expected);
  5640. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5641. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5642. $this->assertTags($result, $expected);
  5643. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5644. $expected = array(
  5645. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5646. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5647. '/div'
  5648. );
  5649. $this->assertTags($result, $expected);
  5650. $result = $this->Form->submit('Next >');
  5651. $expected = array(
  5652. 'div' => array('class' => 'submit'),
  5653. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5654. '/div'
  5655. );
  5656. $this->assertTags($result, $expected);
  5657. $result = $this->Form->submit('Next >', array('escape' => false));
  5658. $expected = array(
  5659. 'div' => array('class' => 'submit'),
  5660. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5661. '/div'
  5662. );
  5663. $this->assertTags($result, $expected);
  5664. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5665. $expected = array(
  5666. 'div' => array('class' => 'submit'),
  5667. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5668. '/div'
  5669. );
  5670. $this->assertTags($result, $expected);
  5671. $before = '--before--';
  5672. $after = '--after--';
  5673. $result = $this->Form->submit('Test', array('before' => $before));
  5674. $expected = array(
  5675. 'div' => array('class' => 'submit'),
  5676. '--before--',
  5677. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5678. '/div'
  5679. );
  5680. $this->assertTags($result, $expected);
  5681. $result = $this->Form->submit('Test', array('after' => $after));
  5682. $expected = array(
  5683. 'div' => array('class' => 'submit'),
  5684. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5685. '--after--',
  5686. '/div'
  5687. );
  5688. $this->assertTags($result, $expected);
  5689. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5690. $expected = array(
  5691. 'div' => array('class' => 'submit'),
  5692. '--before--',
  5693. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5694. '--after--',
  5695. '/div'
  5696. );
  5697. $this->assertTags($result, $expected);
  5698. }
  5699. /**
  5700. * test image submit types.
  5701. *
  5702. * @return void
  5703. */
  5704. public function testSubmitImage() {
  5705. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5706. $expected = array(
  5707. 'div' => array('class' => 'submit'),
  5708. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5709. '/div'
  5710. );
  5711. $this->assertTags($result, $expected);
  5712. $result = $this->Form->submit('/relative/cake.power.gif');
  5713. $expected = array(
  5714. 'div' => array('class' => 'submit'),
  5715. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5716. '/div'
  5717. );
  5718. $this->assertTags($result, $expected);
  5719. $result = $this->Form->submit('cake.power.gif');
  5720. $expected = array(
  5721. 'div' => array('class' => 'submit'),
  5722. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5723. '/div'
  5724. );
  5725. $this->assertTags($result, $expected);
  5726. $result = $this->Form->submit('Not.an.image');
  5727. $expected = array(
  5728. 'div' => array('class' => 'submit'),
  5729. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5730. '/div'
  5731. );
  5732. $this->assertTags($result, $expected);
  5733. $after = '--after--';
  5734. $before = '--before--';
  5735. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5736. $expected = array(
  5737. 'div' => array('class' => 'submit'),
  5738. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5739. '--after--',
  5740. '/div'
  5741. );
  5742. $this->assertTags($result, $expected);
  5743. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5744. $expected = array(
  5745. 'div' => array('class' => 'submit'),
  5746. '--before--',
  5747. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5748. '/div'
  5749. );
  5750. $this->assertTags($result, $expected);
  5751. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5752. $expected = array(
  5753. 'div' => array('class' => 'submit'),
  5754. '--before--',
  5755. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5756. '--after--',
  5757. '/div'
  5758. );
  5759. $this->assertTags($result, $expected);
  5760. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5761. $expected = array(
  5762. 'div' => array('class' => 'submit'),
  5763. '--before--',
  5764. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5765. '--after--',
  5766. '/div'
  5767. );
  5768. $this->assertTags($result, $expected);
  5769. }
  5770. /**
  5771. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5772. * be submitted at a time.
  5773. *
  5774. * @return void
  5775. */
  5776. public function testSubmitUnlockedByDefault() {
  5777. $this->Form->request->params['_Token']['key'] = 'secured';
  5778. $this->Form->submit('Go go');
  5779. $this->Form->submit('Save', array('name' => 'save'));
  5780. $result = $this->Form->unlockField();
  5781. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5782. }
  5783. /**
  5784. * Test submit image with timestamps.
  5785. *
  5786. * @return void
  5787. */
  5788. public function testSubmitImageTimestamp() {
  5789. Configure::write('Asset.timestamp', 'force');
  5790. $result = $this->Form->submit('cake.power.gif');
  5791. $expected = array(
  5792. 'div' => array('class' => 'submit'),
  5793. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5794. '/div'
  5795. );
  5796. $this->assertTags($result, $expected);
  5797. }
  5798. /**
  5799. * test the create() method
  5800. *
  5801. * @return void
  5802. */
  5803. public function testCreate() {
  5804. $result = $this->Form->create('Contact');
  5805. $encoding = strtolower(Configure::read('App.encoding'));
  5806. $expected = array(
  5807. 'form' => array(
  5808. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5809. 'accept-charset' => $encoding
  5810. ),
  5811. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5812. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5813. '/div'
  5814. );
  5815. $this->assertTags($result, $expected);
  5816. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5817. $expected = array('form' => array(
  5818. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5819. 'accept-charset' => $encoding
  5820. ));
  5821. $this->assertTags($result, $expected);
  5822. $result = $this->Form->create('Contact', array('type' => 'get'));
  5823. $expected = array('form' => array(
  5824. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5825. 'accept-charset' => $encoding
  5826. ));
  5827. $this->assertTags($result, $expected);
  5828. $result = $this->Form->create('Contact', array('type' => 'put'));
  5829. $expected = array(
  5830. 'form' => array(
  5831. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5832. 'accept-charset' => $encoding
  5833. ),
  5834. 'div' => array('style' => 'display:none;'),
  5835. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5836. '/div'
  5837. );
  5838. $this->assertTags($result, $expected);
  5839. $result = $this->Form->create('Contact', array('type' => 'file'));
  5840. $expected = array(
  5841. 'form' => array(
  5842. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5843. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5844. ),
  5845. 'div' => array('style' => 'display:none;'),
  5846. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5847. '/div'
  5848. );
  5849. $this->assertTags($result, $expected);
  5850. $this->Form->request->data['Contact']['id'] = 1;
  5851. $this->Form->request->here = '/contacts/edit/1';
  5852. $this->Form->request['action'] = 'edit';
  5853. $result = $this->Form->create('Contact');
  5854. $expected = array(
  5855. 'form' => array(
  5856. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5857. 'accept-charset' => $encoding
  5858. ),
  5859. 'div' => array('style' => 'display:none;'),
  5860. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5861. '/div'
  5862. );
  5863. $this->assertTags($result, $expected);
  5864. $this->Form->request->data['Contact']['id'] = 1;
  5865. $this->Form->request->here = '/contacts/edit/1';
  5866. $this->Form->request['action'] = 'edit';
  5867. $result = $this->Form->create('Contact', array('type' => 'file'));
  5868. $expected = array(
  5869. 'form' => array(
  5870. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5871. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5872. ),
  5873. 'div' => array('style' => 'display:none;'),
  5874. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5875. '/div'
  5876. );
  5877. $this->assertTags($result, $expected);
  5878. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5879. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5880. $expected = array(
  5881. 'form' => array(
  5882. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5883. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5884. ),
  5885. 'div' => array('style' => 'display:none;'),
  5886. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5887. '/div'
  5888. );
  5889. $this->assertTags($result, $expected);
  5890. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5891. $expected = array(
  5892. 'form' => array(
  5893. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5894. 'accept-charset' => $encoding
  5895. ),
  5896. 'div' => array('style' => 'display:none;'),
  5897. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5898. '/div'
  5899. );
  5900. $this->assertTags($result, $expected);
  5901. $this->Form->request['action'] = 'add';
  5902. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5903. $expected = array(
  5904. 'form' => array(
  5905. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5906. 'accept-charset' => $encoding
  5907. ),
  5908. 'div' => array('style' => 'display:none;'),
  5909. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5910. '/div'
  5911. );
  5912. $this->assertTags($result, $expected);
  5913. $result = $this->Form->create('User', array('action' => 'login'));
  5914. $expected = array(
  5915. 'form' => array(
  5916. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5917. 'accept-charset' => $encoding
  5918. ),
  5919. 'div' => array('style' => 'display:none;'),
  5920. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5921. '/div'
  5922. );
  5923. $this->assertTags($result, $expected);
  5924. $result = $this->Form->create('User', array('url' => '/users/login'));
  5925. $expected = array(
  5926. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5927. 'div' => array('style' => 'display:none;'),
  5928. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5929. '/div'
  5930. );
  5931. $this->assertTags($result, $expected);
  5932. $this->Form->request['controller'] = 'pages';
  5933. $result = $this->Form->create('User', array('action' => 'signup'));
  5934. $expected = array(
  5935. 'form' => array(
  5936. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5937. 'accept-charset' => $encoding
  5938. ),
  5939. 'div' => array('style' => 'display:none;'),
  5940. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5941. '/div'
  5942. );
  5943. $this->assertTags($result, $expected);
  5944. $this->Form->request->data = array();
  5945. $this->Form->request['controller'] = 'contacts';
  5946. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5947. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5948. $expected = array(
  5949. 'form' => array(
  5950. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5951. 'accept-charset' => 'utf-8'
  5952. ),
  5953. 'div' => array('style' => 'display:none;'),
  5954. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5955. '/div'
  5956. );
  5957. $this->assertTags($result, $expected);
  5958. }
  5959. /**
  5960. * Test the onsubmit option for create()
  5961. *
  5962. * @return void
  5963. */
  5964. public function testCreateOnSubmit() {
  5965. $this->Form->request->data = array();
  5966. $this->Form->request['controller'] = 'contacts';
  5967. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5968. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5969. $expected = array(
  5970. 'form' => array(
  5971. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5972. 'accept-charset' => 'utf-8'
  5973. ),
  5974. 'div' => array('style' => 'display:none;'),
  5975. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5976. '/div'
  5977. );
  5978. $this->assertTags($result, $expected);
  5979. $this->Form->request->data = array();
  5980. $this->Form->request['controller'] = 'contacts';
  5981. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5982. $result = $this->Form->create(array(
  5983. 'url' => array('action' => 'index', 'param'),
  5984. 'default' => false,
  5985. 'onsubmit' => 'someFunction();'
  5986. ));
  5987. $expected = array(
  5988. 'form' => array(
  5989. 'id' => 'ContactAddForm', 'method' => 'post',
  5990. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  5991. 'action' => '/contacts/index/param',
  5992. 'accept-charset' => 'utf-8'
  5993. ),
  5994. 'div' => array('style' => 'display:none;'),
  5995. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5996. '/div'
  5997. );
  5998. $this->assertTags($result, $expected);
  5999. }
  6000. /**
  6001. * test create() with automatic url generation
  6002. *
  6003. * @return void
  6004. */
  6005. public function testCreateAutoUrl() {
  6006. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6007. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6008. $this->Form->request->base = '/base_url';
  6009. $result = $this->Form->create('Contact');
  6010. $expected = array(
  6011. 'form' => array(
  6012. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6013. 'accept-charset' => 'utf-8'
  6014. ),
  6015. 'div' => array('style' => 'display:none;'),
  6016. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6017. '/div'
  6018. );
  6019. $this->assertTags($result, $expected);
  6020. $this->Form->request['action'] = 'delete';
  6021. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6022. $this->Form->request->base = '/base_url';
  6023. $result = $this->Form->create('Contact');
  6024. $expected = array(
  6025. 'form' => array(
  6026. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6027. 'accept-charset' => 'utf-8'
  6028. ),
  6029. 'div' => array('style' => 'display:none;'),
  6030. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6031. '/div'
  6032. );
  6033. $this->assertTags($result, $expected);
  6034. }
  6035. /**
  6036. * test create() with a custom route
  6037. *
  6038. * @return void
  6039. */
  6040. public function testCreateCustomRoute() {
  6041. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6042. $encoding = strtolower(Configure::read('App.encoding'));
  6043. $result = $this->Form->create('User', array('action' => 'login'));
  6044. $expected = array(
  6045. 'form' => array(
  6046. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6047. 'accept-charset' => $encoding
  6048. ),
  6049. 'div' => array('style' => 'display:none;'),
  6050. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6051. '/div'
  6052. );
  6053. $this->assertTags($result, $expected);
  6054. }
  6055. /**
  6056. * test that inputDefaults are stored and used.
  6057. *
  6058. * @return void
  6059. */
  6060. public function testCreateWithInputDefaults() {
  6061. $this->Form->create('User', array(
  6062. 'inputDefaults' => array(
  6063. 'div' => false,
  6064. 'label' => false,
  6065. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6066. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6067. )
  6068. ));
  6069. $result = $this->Form->input('username');
  6070. $expected = array(
  6071. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6072. );
  6073. $this->assertTags($result, $expected);
  6074. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6075. $expected = array(
  6076. 'div' => array('class' => 'input text'),
  6077. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6078. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6079. '/div'
  6080. );
  6081. $this->assertTags($result, $expected);
  6082. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6083. $expected = array(
  6084. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6085. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6086. );
  6087. $this->assertTags($result, $expected);
  6088. $this->Form->create('User', array(
  6089. 'inputDefaults' => array(
  6090. 'div' => false,
  6091. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6092. )
  6093. ));
  6094. $result = $this->Form->input('username', array('div' => true));
  6095. $expected = array(
  6096. 'div' => array('class' => 'input text'),
  6097. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6098. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6099. '/div'
  6100. );
  6101. $this->assertTags($result, $expected);
  6102. }
  6103. /**
  6104. * test automatic accept-charset overriding
  6105. *
  6106. * @return void
  6107. */
  6108. public function testCreateWithAcceptCharset() {
  6109. $result = $this->Form->create('UserForm', array(
  6110. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6111. )
  6112. );
  6113. $expected = array(
  6114. 'form' => array(
  6115. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6116. 'accept-charset' => 'iso-8859-1'
  6117. ),
  6118. 'div' => array('style' => 'display:none;'),
  6119. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6120. '/div'
  6121. );
  6122. $this->assertTags($result, $expected);
  6123. }
  6124. /**
  6125. * Test base form url when url param is passed with multiple parameters (&)
  6126. *
  6127. */
  6128. public function testCreateQuerystringrequest() {
  6129. $encoding = strtolower(Configure::read('App.encoding'));
  6130. $result = $this->Form->create('Contact', array(
  6131. 'type' => 'post',
  6132. 'escape' => false,
  6133. 'url' => array(
  6134. 'controller' => 'controller',
  6135. 'action' => 'action',
  6136. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6137. )
  6138. ));
  6139. $expected = array(
  6140. 'form' => array(
  6141. 'id' => 'ContactAddForm',
  6142. 'method' => 'post',
  6143. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6144. 'accept-charset' => $encoding
  6145. ),
  6146. 'div' => array('style' => 'display:none;'),
  6147. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6148. '/div'
  6149. );
  6150. $this->assertTags($result, $expected);
  6151. $result = $this->Form->create('Contact', array(
  6152. 'type' => 'post',
  6153. 'url' => array(
  6154. 'controller' => 'controller',
  6155. 'action' => 'action',
  6156. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6157. )
  6158. ));
  6159. $expected = array(
  6160. 'form' => array(
  6161. 'id' => 'ContactAddForm',
  6162. 'method' => 'post',
  6163. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6164. 'accept-charset' => $encoding
  6165. ),
  6166. 'div' => array('style' => 'display:none;'),
  6167. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6168. '/div'
  6169. );
  6170. $this->assertTags($result, $expected);
  6171. }
  6172. /**
  6173. * test that create() doesn't cause errors by multiple id's being in the primary key
  6174. * as could happen with multiple select or checkboxes.
  6175. *
  6176. * @return void
  6177. */
  6178. public function testCreateWithMultipleIdInData() {
  6179. $encoding = strtolower(Configure::read('App.encoding'));
  6180. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6181. $result = $this->Form->create('Contact');
  6182. $expected = array(
  6183. 'form' => array(
  6184. 'id' => 'ContactAddForm',
  6185. 'method' => 'post',
  6186. 'action' => '/contacts/add',
  6187. 'accept-charset' => $encoding
  6188. ),
  6189. 'div' => array('style' => 'display:none;'),
  6190. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6191. '/div'
  6192. );
  6193. $this->assertTags($result, $expected);
  6194. }
  6195. /**
  6196. * test that create() doesn't add in extra passed params.
  6197. *
  6198. * @return void
  6199. */
  6200. public function testCreatePassedArgs() {
  6201. $encoding = strtolower(Configure::read('App.encoding'));
  6202. $this->Form->request->data['Contact']['id'] = 1;
  6203. $result = $this->Form->create('Contact', array(
  6204. 'type' => 'post',
  6205. 'escape' => false,
  6206. 'url' => array(
  6207. 'action' => 'edit',
  6208. 'myparam'
  6209. )
  6210. ));
  6211. $expected = array(
  6212. 'form' => array(
  6213. 'id' => 'ContactAddForm',
  6214. 'method' => 'post',
  6215. 'action' => '/contacts/edit/myparam',
  6216. 'accept-charset' => $encoding
  6217. ),
  6218. 'div' => array('style' => 'display:none;'),
  6219. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6220. '/div'
  6221. );
  6222. $this->assertTags($result, $expected);
  6223. }
  6224. /**
  6225. * test creating a get form, and get form inputs.
  6226. *
  6227. * @return void
  6228. */
  6229. public function testGetFormCreate() {
  6230. $encoding = strtolower(Configure::read('App.encoding'));
  6231. $result = $this->Form->create('Contact', array('type' => 'get'));
  6232. $this->assertTags($result, array('form' => array(
  6233. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6234. 'accept-charset' => $encoding
  6235. )));
  6236. $result = $this->Form->text('Contact.name');
  6237. $this->assertTags($result, array('input' => array(
  6238. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6239. )));
  6240. $result = $this->Form->password('password');
  6241. $this->assertTags($result, array('input' => array(
  6242. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6243. )));
  6244. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6245. $result = $this->Form->text('user_form');
  6246. $this->assertTags($result, array('input' => array(
  6247. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6248. )));
  6249. }
  6250. /**
  6251. * test get form, and inputs when the model param is false
  6252. *
  6253. * @return void
  6254. */
  6255. public function testGetFormWithFalseModel() {
  6256. $encoding = strtolower(Configure::read('App.encoding'));
  6257. $this->Form->request['controller'] = 'contact_test';
  6258. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6259. $expected = array('form' => array(
  6260. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6261. 'accept-charset' => $encoding
  6262. ));
  6263. $this->assertTags($result, $expected);
  6264. $result = $this->Form->text('reason');
  6265. $expected = array(
  6266. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6267. );
  6268. $this->assertTags($result, $expected);
  6269. }
  6270. /**
  6271. * test that datetime() works with GET style forms.
  6272. *
  6273. * @return void
  6274. */
  6275. public function testDateTimeWithGetForms() {
  6276. extract($this->dateRegex);
  6277. $this->Form->create('Contact', array('type' => 'get'));
  6278. $result = $this->Form->datetime('created');
  6279. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6280. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6281. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6282. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6283. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6284. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6285. }
  6286. /**
  6287. * testEditFormWithData method
  6288. *
  6289. * test auto populating form elements from submitted data.
  6290. *
  6291. * @return void
  6292. */
  6293. public function testEditFormWithData() {
  6294. $this->Form->request->data = array('Person' => array(
  6295. 'id' => 1,
  6296. 'first_name' => 'Nate',
  6297. 'last_name' => 'Abele',
  6298. 'email' => 'nate@example.com'
  6299. ));
  6300. $this->Form->request->addParams(array(
  6301. 'models' => array('Person'),
  6302. 'controller' => 'people',
  6303. 'action' => 'add'
  6304. ));
  6305. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6306. $this->Form->create();
  6307. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6308. $expected = array(
  6309. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6310. 'select' => array(
  6311. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6312. ),
  6313. array('option' => array('value' => 1)), 'Nate', '/option',
  6314. array('option' => array('value' => 2)), 'Garrett', '/option',
  6315. array('option' => array('value' => 3)), 'Larry', '/option',
  6316. '/select'
  6317. );
  6318. $this->assertTags($result, $expected);
  6319. }
  6320. /**
  6321. * Test that required fields are created for various types of validation.
  6322. *
  6323. * @return void
  6324. */
  6325. public function testFormInputRequiredDetection() {
  6326. $this->Form->create('Contact');
  6327. $result = $this->Form->input('Contact.non_existing');
  6328. $expected = array(
  6329. 'div' => array('class' => 'input text'),
  6330. 'label' => array('for' => 'ContactNonExisting'),
  6331. 'Non Existing',
  6332. '/label',
  6333. 'input' => array(
  6334. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6335. 'id' => 'ContactNonExisting'
  6336. ),
  6337. '/div'
  6338. );
  6339. $this->assertTags($result, $expected);
  6340. $result = $this->Form->input('Contact.imrequired');
  6341. $expected = array(
  6342. 'div' => array('class' => 'input text required'),
  6343. 'label' => array('for' => 'ContactImrequired'),
  6344. 'Imrequired',
  6345. '/label',
  6346. 'input' => array(
  6347. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6348. 'id' => 'ContactImrequired'
  6349. ),
  6350. '/div'
  6351. );
  6352. $this->assertTags($result, $expected);
  6353. $result = $this->Form->input('Contact.imalsorequired');
  6354. $expected = array(
  6355. 'div' => array('class' => 'input text required'),
  6356. 'label' => array('for' => 'ContactImalsorequired'),
  6357. 'Imalsorequired',
  6358. '/label',
  6359. 'input' => array(
  6360. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6361. 'id' => 'ContactImalsorequired'
  6362. ),
  6363. '/div'
  6364. );
  6365. $this->assertTags($result, $expected);
  6366. $result = $this->Form->input('Contact.imrequiredtoo');
  6367. $expected = array(
  6368. 'div' => array('class' => 'input text required'),
  6369. 'label' => array('for' => 'ContactImrequiredtoo'),
  6370. 'Imrequiredtoo',
  6371. '/label',
  6372. 'input' => array(
  6373. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6374. 'id' => 'ContactImrequiredtoo'
  6375. ),
  6376. '/div'
  6377. );
  6378. $this->assertTags($result, $expected);
  6379. $result = $this->Form->input('Contact.required_one');
  6380. $expected = array(
  6381. 'div' => array('class' => 'input text required'),
  6382. 'label' => array('for' => 'ContactRequiredOne'),
  6383. 'Required One',
  6384. '/label',
  6385. 'input' => array(
  6386. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6387. 'id' => 'ContactRequiredOne'
  6388. ),
  6389. '/div'
  6390. );
  6391. $this->assertTags($result, $expected);
  6392. $result = $this->Form->input('Contact.string_required');
  6393. $expected = array(
  6394. 'div' => array('class' => 'input text required'),
  6395. 'label' => array('for' => 'ContactStringRequired'),
  6396. 'String Required',
  6397. '/label',
  6398. 'input' => array(
  6399. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6400. 'id' => 'ContactStringRequired'
  6401. ),
  6402. '/div'
  6403. );
  6404. $this->assertTags($result, $expected);
  6405. $result = $this->Form->input('Contact.imnotrequired');
  6406. $expected = array(
  6407. 'div' => array('class' => 'input text'),
  6408. 'label' => array('for' => 'ContactImnotrequired'),
  6409. 'Imnotrequired',
  6410. '/label',
  6411. 'input' => array(
  6412. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6413. 'id' => 'ContactImnotrequired'
  6414. ),
  6415. '/div'
  6416. );
  6417. $this->assertTags($result, $expected);
  6418. $result = $this->Form->input('Contact.imalsonotrequired');
  6419. $expected = array(
  6420. 'div' => array('class' => 'input text'),
  6421. 'label' => array('for' => 'ContactImalsonotrequired'),
  6422. 'Imalsonotrequired',
  6423. '/label',
  6424. 'input' => array(
  6425. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6426. 'id' => 'ContactImalsonotrequired'
  6427. ),
  6428. '/div'
  6429. );
  6430. $this->assertTags($result, $expected);
  6431. $result = $this->Form->input('Contact.imnotrequiredeither');
  6432. $expected = array(
  6433. 'div' => array('class' => 'input text'),
  6434. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6435. 'Imnotrequiredeither',
  6436. '/label',
  6437. 'input' => array(
  6438. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6439. 'id' => 'ContactImnotrequiredeither'
  6440. ),
  6441. '/div'
  6442. );
  6443. $this->assertTags($result, $expected);
  6444. }
  6445. /**
  6446. * testFormMagicInput method
  6447. *
  6448. * @return void
  6449. */
  6450. public function testFormMagicInput() {
  6451. $encoding = strtolower(Configure::read('App.encoding'));
  6452. $result = $this->Form->create('Contact');
  6453. $expected = array(
  6454. 'form' => array(
  6455. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6456. 'accept-charset' => $encoding
  6457. ),
  6458. 'div' => array('style' => 'display:none;'),
  6459. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6460. '/div'
  6461. );
  6462. $this->assertTags($result, $expected);
  6463. $result = $this->Form->input('name');
  6464. $expected = array(
  6465. 'div' => array('class' => 'input text'),
  6466. 'label' => array('for' => 'ContactName'),
  6467. 'Name',
  6468. '/label',
  6469. 'input' => array(
  6470. 'type' => 'text', 'name' => 'data[Contact][name]',
  6471. 'id' => 'ContactName', 'maxlength' => '255'
  6472. ),
  6473. '/div'
  6474. );
  6475. $this->assertTags($result, $expected);
  6476. $result = $this->Form->input('non_existing_field_in_contact_model');
  6477. $expected = array(
  6478. 'div' => array('class' => 'input text'),
  6479. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6480. 'Non Existing Field In Contact Model',
  6481. '/label',
  6482. 'input' => array(
  6483. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6484. 'id' => 'ContactNonExistingFieldInContactModel'
  6485. ),
  6486. '/div'
  6487. );
  6488. $this->assertTags($result, $expected);
  6489. $result = $this->Form->input('Address.street');
  6490. $expected = array(
  6491. 'div' => array('class' => 'input text'),
  6492. 'label' => array('for' => 'AddressStreet'),
  6493. 'Street',
  6494. '/label',
  6495. 'input' => array(
  6496. 'type' => 'text', 'name' => 'data[Address][street]',
  6497. 'id' => 'AddressStreet'
  6498. ),
  6499. '/div'
  6500. );
  6501. $this->assertTags($result, $expected);
  6502. $result = $this->Form->input('Address.non_existing_field_in_model');
  6503. $expected = array(
  6504. 'div' => array('class' => 'input text'),
  6505. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6506. 'Non Existing Field In Model',
  6507. '/label',
  6508. 'input' => array(
  6509. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6510. 'id' => 'AddressNonExistingFieldInModel'
  6511. ),
  6512. '/div'
  6513. );
  6514. $this->assertTags($result, $expected);
  6515. $result = $this->Form->input('name', array('div' => false));
  6516. $expected = array(
  6517. 'label' => array('for' => 'ContactName'),
  6518. 'Name',
  6519. '/label',
  6520. 'input' => array(
  6521. 'type' => 'text', 'name' => 'data[Contact][name]',
  6522. 'id' => 'ContactName', 'maxlength' => '255'
  6523. )
  6524. );
  6525. $this->assertTags($result, $expected);
  6526. extract($this->dateRegex);
  6527. $now = strtotime('now');
  6528. $result = $this->Form->input('Contact.published', array('div' => false));
  6529. $expected = array(
  6530. 'label' => array('for' => 'ContactPublishedMonth'),
  6531. 'Published',
  6532. '/label',
  6533. array('select' => array(
  6534. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6535. )),
  6536. $monthsRegex,
  6537. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6538. date('F', $now),
  6539. '/option',
  6540. '*/select',
  6541. '-',
  6542. array('select' => array(
  6543. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6544. )),
  6545. $daysRegex,
  6546. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6547. date('j', $now),
  6548. '/option',
  6549. '*/select',
  6550. '-',
  6551. array('select' => array(
  6552. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6553. )),
  6554. $yearsRegex,
  6555. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6556. date('Y', $now),
  6557. '*/select'
  6558. );
  6559. $this->assertTags($result, $expected);
  6560. $result = $this->Form->input('Contact.updated', array('div' => false));
  6561. $expected = array(
  6562. 'label' => array('for' => 'ContactUpdatedMonth'),
  6563. 'Updated',
  6564. '/label',
  6565. array('select' => array(
  6566. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6567. )),
  6568. $monthsRegex,
  6569. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6570. date('F', $now),
  6571. '/option',
  6572. '*/select',
  6573. '-',
  6574. array('select' => array(
  6575. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6576. )),
  6577. $daysRegex,
  6578. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6579. date('j', $now),
  6580. '/option',
  6581. '*/select',
  6582. '-',
  6583. array('select' => array(
  6584. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6585. )),
  6586. $yearsRegex,
  6587. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6588. date('Y', $now),
  6589. '*/select'
  6590. );
  6591. $this->assertTags($result, $expected);
  6592. $result = $this->Form->input('UserForm.stuff');
  6593. $expected = array(
  6594. 'div' => array('class' => 'input text'),
  6595. 'label' => array('for' => 'UserFormStuff'),
  6596. 'Stuff',
  6597. '/label',
  6598. 'input' => array(
  6599. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6600. 'id' => 'UserFormStuff', 'maxlength' => 10
  6601. ),
  6602. '/div'
  6603. );
  6604. $this->assertTags($result, $expected);
  6605. }
  6606. /**
  6607. * testForMagicInputNonExistingNorValidated method
  6608. *
  6609. * @return void
  6610. */
  6611. public function testForMagicInputNonExistingNorValidated() {
  6612. $encoding = strtolower(Configure::read('App.encoding'));
  6613. $result = $this->Form->create('Contact');
  6614. $expected = array(
  6615. 'form' => array(
  6616. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6617. 'accept-charset' => $encoding
  6618. ),
  6619. 'div' => array('style' => 'display:none;'),
  6620. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6621. '/div'
  6622. );
  6623. $this->assertTags($result, $expected);
  6624. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6625. $expected = array(
  6626. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6627. 'Non Existing Nor Validated',
  6628. '/label',
  6629. 'input' => array(
  6630. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6631. 'id' => 'ContactNonExistingNorValidated'
  6632. )
  6633. );
  6634. $this->assertTags($result, $expected);
  6635. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6636. 'div' => false, 'value' => 'my value'
  6637. ));
  6638. $expected = array(
  6639. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6640. 'Non Existing Nor Validated',
  6641. '/label',
  6642. 'input' => array(
  6643. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6644. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6645. )
  6646. );
  6647. $this->assertTags($result, $expected);
  6648. $this->Form->request->data = array(
  6649. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6650. ));
  6651. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6652. $expected = array(
  6653. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6654. 'Non Existing Nor Validated',
  6655. '/label',
  6656. 'input' => array(
  6657. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6658. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6659. )
  6660. );
  6661. $this->assertTags($result, $expected);
  6662. }
  6663. /**
  6664. * testFormMagicInputLabel method
  6665. *
  6666. * @return void
  6667. */
  6668. public function testFormMagicInputLabel() {
  6669. $encoding = strtolower(Configure::read('App.encoding'));
  6670. $result = $this->Form->create('Contact');
  6671. $expected = array(
  6672. 'form' => array(
  6673. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6674. 'accept-charset' => $encoding
  6675. ),
  6676. 'div' => array('style' => 'display:none;'),
  6677. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6678. '/div'
  6679. );
  6680. $this->assertTags($result, $expected);
  6681. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6682. $this->assertTags($result, array('input' => array(
  6683. 'name' => 'data[Contact][name]', 'type' => 'text',
  6684. 'id' => 'ContactName', 'maxlength' => '255')
  6685. ));
  6686. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6687. $expected = array(
  6688. 'label' => array('for' => 'ContactName'),
  6689. 'My label',
  6690. '/label',
  6691. 'input' => array(
  6692. 'type' => 'text', 'name' => 'data[Contact][name]',
  6693. 'id' => 'ContactName', 'maxlength' => '255'
  6694. )
  6695. );
  6696. $this->assertTags($result, $expected);
  6697. $result = $this->Form->input('Contact.name', array(
  6698. 'div' => false, 'label' => array('class' => 'mandatory')
  6699. ));
  6700. $expected = array(
  6701. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6702. 'Name',
  6703. '/label',
  6704. 'input' => array(
  6705. 'type' => 'text', 'name' => 'data[Contact][name]',
  6706. 'id' => 'ContactName', 'maxlength' => '255'
  6707. )
  6708. );
  6709. $this->assertTags($result, $expected);
  6710. $result = $this->Form->input('Contact.name', array(
  6711. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6712. ));
  6713. $expected = array(
  6714. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6715. 'My label',
  6716. '/label',
  6717. 'input' => array(
  6718. 'type' => 'text', 'name' => 'data[Contact][name]',
  6719. 'id' => 'ContactName', 'maxlength' => '255'
  6720. )
  6721. );
  6722. $this->assertTags($result, $expected);
  6723. $result = $this->Form->input('Contact.name', array(
  6724. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6725. ));
  6726. $expected = array(
  6727. 'label' => array('for' => 'my_id'),
  6728. 'Name',
  6729. '/label',
  6730. 'input' => array(
  6731. 'type' => 'text', 'name' => 'data[Contact][name]',
  6732. 'id' => 'my_id', 'maxlength' => '255'
  6733. )
  6734. );
  6735. $this->assertTags($result, $expected);
  6736. $result = $this->Form->input('1.id');
  6737. $this->assertTags($result, array('input' => array(
  6738. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6739. 'id' => 'Contact1Id'
  6740. )));
  6741. $result = $this->Form->input("1.name");
  6742. $expected = array(
  6743. 'div' => array('class' => 'input text'),
  6744. 'label' => array('for' => 'Contact1Name'),
  6745. 'Name',
  6746. '/label',
  6747. 'input' => array(
  6748. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6749. 'id' => 'Contact1Name', 'maxlength' => '255'
  6750. ),
  6751. '/div'
  6752. );
  6753. $this->assertTags($result, $expected);
  6754. $result = $this->Form->input('Contact.1.id');
  6755. $this->assertTags($result, array(
  6756. 'input' => array(
  6757. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6758. 'id' => 'Contact1Id'
  6759. )
  6760. ));
  6761. $result = $this->Form->input("Model.1.name");
  6762. $expected = array(
  6763. 'div' => array('class' => 'input text'),
  6764. 'label' => array('for' => 'Model1Name'),
  6765. 'Name',
  6766. '/label',
  6767. 'input' => array(
  6768. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6769. 'id' => 'Model1Name'
  6770. ),
  6771. '/div'
  6772. );
  6773. $this->assertTags($result, $expected);
  6774. }
  6775. /**
  6776. * testFormEnd method
  6777. *
  6778. * @return void
  6779. */
  6780. public function testFormEnd() {
  6781. $this->assertEquals('</form>', $this->Form->end());
  6782. $result = $this->Form->end('');
  6783. $expected = array(
  6784. 'div' => array('class' => 'submit'),
  6785. 'input' => array('type' => 'submit', 'value' => ''),
  6786. '/div',
  6787. '/form'
  6788. );
  6789. $this->assertTags($result, $expected);
  6790. $result = $this->Form->end(array('label' => ''));
  6791. $expected = array(
  6792. 'div' => array('class' => 'submit'),
  6793. 'input' => array('type' => 'submit', 'value' => ''),
  6794. '/div',
  6795. '/form'
  6796. );
  6797. $this->assertTags($result, $expected);
  6798. $result = $this->Form->end('save');
  6799. $expected = array(
  6800. 'div' => array('class' => 'submit'),
  6801. 'input' => array('type' => 'submit', 'value' => 'save'),
  6802. '/div',
  6803. '/form'
  6804. );
  6805. $this->assertTags($result, $expected);
  6806. $result = $this->Form->end(array('label' => 'save'));
  6807. $expected = array(
  6808. 'div' => array('class' => 'submit'),
  6809. 'input' => array('type' => 'submit', 'value' => 'save'),
  6810. '/div',
  6811. '/form'
  6812. );
  6813. $this->assertTags($result, $expected);
  6814. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6815. $expected = array(
  6816. 'div' => array('class' => 'submit'),
  6817. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6818. '/div',
  6819. '/form'
  6820. );
  6821. $this->assertTags($result, $expected);
  6822. $result = $this->Form->end(array('name' => 'Whatever'));
  6823. $expected = array(
  6824. 'div' => array('class' => 'submit'),
  6825. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6826. '/div',
  6827. '/form'
  6828. );
  6829. $this->assertTags($result, $expected);
  6830. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6831. $expected = array(
  6832. 'div' => array('class' => 'good'),
  6833. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6834. '/div',
  6835. '/form'
  6836. );
  6837. $this->assertTags($result, $expected);
  6838. $result = $this->Form->end(array(
  6839. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6840. ));
  6841. $expected = array(
  6842. 'div' => array('class' => 'good'),
  6843. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6844. '/div',
  6845. '/form'
  6846. );
  6847. $this->assertTags($result, $expected);
  6848. }
  6849. /**
  6850. * testMultipleFormWithIdFields method
  6851. *
  6852. * @return void
  6853. */
  6854. public function testMultipleFormWithIdFields() {
  6855. $this->Form->create('UserForm');
  6856. $result = $this->Form->input('id');
  6857. $this->assertTags($result, array('input' => array(
  6858. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6859. )));
  6860. $result = $this->Form->input('ValidateItem.id');
  6861. $this->assertTags($result, array('input' => array(
  6862. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6863. 'id' => 'ValidateItemId'
  6864. )));
  6865. $result = $this->Form->input('ValidateUser.id');
  6866. $this->assertTags($result, array('input' => array(
  6867. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6868. 'id' => 'ValidateUserId'
  6869. )));
  6870. }
  6871. /**
  6872. * testDbLessModel method
  6873. *
  6874. * @return void
  6875. */
  6876. public function testDbLessModel() {
  6877. $this->Form->create('TestMail');
  6878. $result = $this->Form->input('name');
  6879. $expected = array(
  6880. 'div' => array('class' => 'input text'),
  6881. 'label' => array('for' => 'TestMailName'),
  6882. 'Name',
  6883. '/label',
  6884. 'input' => array(
  6885. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6886. 'id' => 'TestMailName'
  6887. ),
  6888. '/div'
  6889. );
  6890. $this->assertTags($result, $expected);
  6891. ClassRegistry::init('TestMail');
  6892. $this->Form->create('TestMail');
  6893. $result = $this->Form->input('name');
  6894. $expected = array(
  6895. 'div' => array('class' => 'input text'),
  6896. 'label' => array('for' => 'TestMailName'),
  6897. 'Name',
  6898. '/label',
  6899. 'input' => array(
  6900. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6901. 'id' => 'TestMailName'
  6902. ),
  6903. '/div'
  6904. );
  6905. $this->assertTags($result, $expected);
  6906. }
  6907. /**
  6908. * testBrokenness method
  6909. *
  6910. * @return void
  6911. */
  6912. public function testBrokenness() {
  6913. /*
  6914. * #4 This test has two parents and four children. By default (as of r7117) both
  6915. * parents are show but the first parent is missing a child. This is the inconsistency
  6916. * in the default behaviour - one parent has all children, the other does not - dependent
  6917. * on the data values.
  6918. */
  6919. $result = $this->Form->select('Model.field', array(
  6920. 'Fred' => array(
  6921. 'freds_son_1' => 'Fred',
  6922. 'freds_son_2' => 'Freddie'
  6923. ),
  6924. 'Bert' => array(
  6925. 'berts_son_1' => 'Albert',
  6926. 'berts_son_2' => 'Bertie')
  6927. ),
  6928. array('showParents' => true, 'empty' => false)
  6929. );
  6930. $expected = array(
  6931. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6932. array('optgroup' => array('label' => 'Fred')),
  6933. array('option' => array('value' => 'freds_son_1')),
  6934. 'Fred',
  6935. '/option',
  6936. array('option' => array('value' => 'freds_son_2')),
  6937. 'Freddie',
  6938. '/option',
  6939. '/optgroup',
  6940. array('optgroup' => array('label' => 'Bert')),
  6941. array('option' => array('value' => 'berts_son_1')),
  6942. 'Albert',
  6943. '/option',
  6944. array('option' => array('value' => 'berts_son_2')),
  6945. 'Bertie',
  6946. '/option',
  6947. '/optgroup',
  6948. '/select'
  6949. );
  6950. $this->assertTags($result, $expected);
  6951. /*
  6952. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6953. * changed, so we should expect the same select list data, just with a different name
  6954. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6955. * This is where data corruption can occur, because when a select value is missing from
  6956. * a list a form will substitute the first value in the list - without the user knowing.
  6957. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6958. * affect the availability of 3 => 'Three' as a valid option.
  6959. */
  6960. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6961. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6962. ));
  6963. $result = $this->Form->select(
  6964. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6965. );
  6966. $expected = array(
  6967. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6968. array('option' => array('value' => 1)),
  6969. 'One',
  6970. '/option',
  6971. array('option' => array('value' => 2)),
  6972. 'Two',
  6973. '/option',
  6974. array('optgroup' => array('label' => 'Three')),
  6975. array('option' => array('value' => 3)),
  6976. 'Three',
  6977. '/option',
  6978. array('option' => array('value' => 4)),
  6979. 'Four',
  6980. '/option',
  6981. array('option' => array('value' => 5)),
  6982. 'Five',
  6983. '/option',
  6984. '/optgroup',
  6985. '/select'
  6986. );
  6987. $this->assertTags($result, $expected);
  6988. }
  6989. /**
  6990. * Test the generation of fields for a multi record form.
  6991. *
  6992. * @return void
  6993. */
  6994. public function testMultiRecordForm() {
  6995. $this->Form->create('ValidateProfile');
  6996. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6997. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6998. $expected = array(
  6999. 'div' => array('class' => 'input textarea'),
  7000. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7001. 'Name',
  7002. '/label',
  7003. 'textarea' => array(
  7004. 'id' => 'ValidateProfile1ValidateItem2Name',
  7005. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7006. 'cols' => 30,
  7007. 'rows' => 6
  7008. ),
  7009. 'Value',
  7010. '/textarea',
  7011. '/div'
  7012. );
  7013. $this->assertTags($result, $expected);
  7014. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7015. $expected = array(
  7016. 'div' => array('class' => 'input date'),
  7017. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7018. 'Created',
  7019. '/label',
  7020. array('select' => array(
  7021. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7022. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7023. )
  7024. ),
  7025. array('option' => array('value' => '')), '/option',
  7026. $this->dateRegex['monthsRegex'],
  7027. '/select', '-',
  7028. array('select' => array(
  7029. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7030. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7031. )
  7032. ),
  7033. array('option' => array('value' => '')), '/option',
  7034. $this->dateRegex['daysRegex'],
  7035. '/select', '-',
  7036. array('select' => array(
  7037. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7038. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7039. )
  7040. ),
  7041. array('option' => array('value' => '')), '/option',
  7042. $this->dateRegex['yearsRegex'],
  7043. '/select',
  7044. '/div'
  7045. );
  7046. $this->assertTags($result, $expected);
  7047. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7048. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7049. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7050. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7051. $expected = array(
  7052. 'div' => array('class' => 'input select error'),
  7053. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7054. 'Profile',
  7055. '/label',
  7056. 'select' => array(
  7057. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7058. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7059. 'class' => 'form-error'
  7060. ),
  7061. '/select',
  7062. array('div' => array('class' => 'error-message')),
  7063. 'Error',
  7064. '/div',
  7065. '/div'
  7066. );
  7067. $this->assertTags($result, $expected);
  7068. }
  7069. /**
  7070. * test the correct display of multi-record form validation errors.
  7071. *
  7072. * @return void
  7073. */
  7074. public function testMultiRecordFormValidationErrors() {
  7075. $this->Form->create('ValidateProfile');
  7076. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7077. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7078. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7079. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7080. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7081. $result = $this->Form->error('ValidateProfile.2.city');
  7082. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7083. $result = $this->Form->error('2.city');
  7084. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7085. }
  7086. /**
  7087. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7088. *
  7089. * @return void
  7090. */
  7091. public function testInputTemplate() {
  7092. $result = $this->Form->input('Contact.email', array(
  7093. 'type' => 'text', 'format' => array('input')
  7094. ));
  7095. $expected = array(
  7096. 'div' => array('class' => 'input text'),
  7097. 'input' => array(
  7098. 'type' => 'text', 'name' => 'data[Contact][email]',
  7099. 'id' => 'ContactEmail'
  7100. ),
  7101. '/div'
  7102. );
  7103. $this->assertTags($result, $expected);
  7104. $result = $this->Form->input('Contact.email', array(
  7105. 'type' => 'text', 'format' => array('input', 'label'),
  7106. 'label' => '<em>Email (required)</em>'
  7107. ));
  7108. $expected = array(
  7109. 'div' => array('class' => 'input text'),
  7110. array('input' => array(
  7111. 'type' => 'text', 'name' => 'data[Contact][email]',
  7112. 'id' => 'ContactEmail'
  7113. )),
  7114. 'label' => array('for' => 'ContactEmail'),
  7115. 'em' => array(),
  7116. 'Email (required)',
  7117. '/em',
  7118. '/label',
  7119. '/div'
  7120. );
  7121. $this->assertTags($result, $expected);
  7122. $result = $this->Form->input('Contact.email', array(
  7123. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7124. 'between' => '<div>Something in the middle</div>',
  7125. 'after' => '<span>Some text at the end</span>'
  7126. ));
  7127. $expected = array(
  7128. 'div' => array('class' => 'input text'),
  7129. array('input' => array(
  7130. 'type' => 'text', 'name' => 'data[Contact][email]',
  7131. 'id' => 'ContactEmail'
  7132. )),
  7133. array('div' => array()),
  7134. 'Something in the middle',
  7135. '/div',
  7136. 'label' => array('for' => 'ContactEmail'),
  7137. 'Email',
  7138. '/label',
  7139. 'span' => array(),
  7140. 'Some text at the end',
  7141. '/span',
  7142. '/div'
  7143. );
  7144. $this->assertTags($result, $expected);
  7145. $result = $this->Form->input('Contact.method', array(
  7146. 'type' => 'radio',
  7147. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7148. 'between' => 'I am between',
  7149. ));
  7150. $expected = array(
  7151. 'div' => array('class' => 'input radio'),
  7152. 'fieldset' => array(),
  7153. 'legend' => array(),
  7154. 'Method',
  7155. '/legend',
  7156. 'I am between',
  7157. 'input' => array(
  7158. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7159. 'value' => '', 'id' => 'ContactMethod_'
  7160. ),
  7161. array('input' => array(
  7162. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7163. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7164. )),
  7165. array('label' => array('for' => 'ContactMethodEmail')),
  7166. 'Email',
  7167. '/label',
  7168. array('input' => array(
  7169. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7170. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7171. )),
  7172. array('label' => array('for' => 'ContactMethodPigeon')),
  7173. 'Pigeon',
  7174. '/label',
  7175. '/fieldset',
  7176. '/div',
  7177. );
  7178. $this->assertTags($result, $expected);
  7179. }
  7180. /**
  7181. * test that some html5 inputs + FormHelper::__call() work
  7182. *
  7183. * @return void
  7184. */
  7185. public function testHtml5Inputs() {
  7186. $result = $this->Form->email('User.email');
  7187. $expected = array(
  7188. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7189. );
  7190. $this->assertTags($result, $expected);
  7191. $result = $this->Form->search('User.query');
  7192. $expected = array(
  7193. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7194. );
  7195. $this->assertTags($result, $expected);
  7196. $result = $this->Form->search('User.query', array('value' => 'test'));
  7197. $expected = array(
  7198. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7199. );
  7200. $this->assertTags($result, $expected);
  7201. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7202. $expected = array(
  7203. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7204. );
  7205. $this->assertTags($result, $expected);
  7206. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7207. $expected = array(
  7208. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7209. );
  7210. $this->assertTags($result, $expected);
  7211. }
  7212. /**
  7213. *
  7214. * @expectedException CakeException
  7215. * @return void
  7216. */
  7217. public function testHtml5InputException() {
  7218. $this->Form->email();
  7219. }
  7220. /**
  7221. * Tests that a model can be loaded from the model names passed in the request object
  7222. *
  7223. * @return void
  7224. */
  7225. public function testIntrospectModelFromRequest() {
  7226. $this->loadFixtures('Post');
  7227. App::build(array(
  7228. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7229. ));
  7230. CakePlugin::load('TestPlugin');
  7231. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7232. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7233. $this->Form->create('TestPluginPost');
  7234. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7235. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7236. CakePlugin::unload();
  7237. App::build();
  7238. }
  7239. /**
  7240. * Tests that it is possible to set the validation errors directly in the helper for a field
  7241. *
  7242. * @return void
  7243. */
  7244. public function testCustomValidationErrors() {
  7245. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7246. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7247. $this->assertEquals('Badness!', $result);
  7248. }
  7249. /**
  7250. * Tests that the 'on' key validates as expected on create
  7251. *
  7252. * @return void
  7253. */
  7254. public function testRequiredOnCreate() {
  7255. $this->Form->create('Contact');
  7256. $result = $this->Form->input('Contact.imrequiredonupdate');
  7257. $expected = array(
  7258. 'div' => array('class' => 'input text'),
  7259. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7260. 'Imrequiredonupdate',
  7261. '/label',
  7262. 'input' => array(
  7263. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7264. 'id' => 'ContactImrequiredonupdate'
  7265. ),
  7266. '/div'
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. $result = $this->Form->input('Contact.imrequiredoncreate');
  7270. $expected = array(
  7271. 'div' => array('class' => 'input text required'),
  7272. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7273. 'Imrequiredoncreate',
  7274. '/label',
  7275. 'input' => array(
  7276. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7277. 'id' => 'ContactImrequiredoncreate'
  7278. ),
  7279. '/div'
  7280. );
  7281. $this->assertTags($result, $expected);
  7282. $result = $this->Form->input('Contact.imrequiredonboth');
  7283. $expected = array(
  7284. 'div' => array('class' => 'input text required'),
  7285. 'label' => array('for' => 'ContactImrequiredonboth'),
  7286. 'Imrequiredonboth',
  7287. '/label',
  7288. 'input' => array(
  7289. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7290. 'id' => 'ContactImrequiredonboth'
  7291. ),
  7292. '/div'
  7293. );
  7294. $this->assertTags($result, $expected);
  7295. $result = $this->Form->input('Contact.imrequired');
  7296. $expected = array(
  7297. 'div' => array('class' => 'input text required'),
  7298. 'label' => array('for' => 'ContactImrequired'),
  7299. 'Imrequired',
  7300. '/label',
  7301. 'input' => array(
  7302. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7303. 'id' => 'ContactImrequired'
  7304. ),
  7305. '/div'
  7306. );
  7307. $this->assertTags($result, $expected);
  7308. }
  7309. /**
  7310. * Tests that the 'on' key validates as expected on update
  7311. *
  7312. * @return void
  7313. */
  7314. public function testRequiredOnUpdate() {
  7315. $this->Form->request->data['Contact']['id'] = 1;
  7316. $this->Form->create('Contact');
  7317. $result = $this->Form->input('Contact.imrequiredonupdate');
  7318. $expected = array(
  7319. 'div' => array('class' => 'input text required'),
  7320. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7321. 'Imrequiredonupdate',
  7322. '/label',
  7323. 'input' => array(
  7324. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7325. 'id' => 'ContactImrequiredonupdate'
  7326. ),
  7327. '/div'
  7328. );
  7329. $this->assertTags($result, $expected);
  7330. $result = $this->Form->input('Contact.imrequiredoncreate');
  7331. $expected = array(
  7332. 'div' => array('class' => 'input text'),
  7333. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7334. 'Imrequiredoncreate',
  7335. '/label',
  7336. 'input' => array(
  7337. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7338. 'id' => 'ContactImrequiredoncreate'
  7339. ),
  7340. '/div'
  7341. );
  7342. $this->assertTags($result, $expected);
  7343. $result = $this->Form->input('Contact.imrequiredonboth');
  7344. $expected = array(
  7345. 'div' => array('class' => 'input text required'),
  7346. 'label' => array('for' => 'ContactImrequiredonboth'),
  7347. 'Imrequiredonboth',
  7348. '/label',
  7349. 'input' => array(
  7350. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7351. 'id' => 'ContactImrequiredonboth'
  7352. ),
  7353. '/div'
  7354. );
  7355. $this->assertTags($result, $expected);
  7356. $result = $this->Form->input('Contact.imrequired');
  7357. $expected = array(
  7358. 'div' => array('class' => 'input text required'),
  7359. 'label' => array('for' => 'ContactImrequired'),
  7360. 'Imrequired',
  7361. '/label',
  7362. 'input' => array(
  7363. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7364. 'id' => 'ContactImrequired'
  7365. ),
  7366. '/div'
  7367. );
  7368. $this->assertTags($result, $expected);
  7369. }
  7370. /**
  7371. * Test inputDefaults setter and getter
  7372. *
  7373. * @return void
  7374. */
  7375. public function testInputDefaults() {
  7376. $this->Form->create('Contact');
  7377. $this->Form->inputDefaults(array(
  7378. 'label' => false,
  7379. 'div' => array(
  7380. 'style' => 'color: #000;'
  7381. )
  7382. ));
  7383. $result = $this->Form->input('Contact.field1');
  7384. $expected = array(
  7385. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7386. 'input' => array(
  7387. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7388. 'id' => 'ContactField1'
  7389. ),
  7390. '/div'
  7391. );
  7392. $this->assertTags($result, $expected);
  7393. $this->Form->inputDefaults(array(
  7394. 'div' => false,
  7395. 'label' => 'Label',
  7396. ));
  7397. $result = $this->Form->input('Contact.field1');
  7398. $expected = array(
  7399. 'label' => array('for' => 'ContactField1'),
  7400. 'Label',
  7401. '/label',
  7402. 'input' => array(
  7403. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7404. 'id' => 'ContactField1'
  7405. ),
  7406. );
  7407. $this->assertTags($result, $expected);
  7408. $this->Form->inputDefaults(array(
  7409. 'label' => false,
  7410. ), true);
  7411. $result = $this->Form->input('Contact.field1');
  7412. $expected = array(
  7413. 'input' => array(
  7414. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7415. 'id' => 'ContactField1'
  7416. ),
  7417. );
  7418. $this->assertTags($result, $expected);
  7419. $result = $this->Form->inputDefaults();
  7420. $expected = array(
  7421. 'div' => false,
  7422. 'label' => false,
  7423. );
  7424. $this->assertEqual($result, $expected);
  7425. }
  7426. }