PageRenderTime 87ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 2ms

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

https://bitbucket.org/npitts/easyuat2.0
PHP | 7740 lines | 6031 code | 609 blank | 1100 comment | 1 complexity | 54c59b3e69b470a709fff3b9e9107d07 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. 'string_required' => 'notEmpty',
  99. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  100. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  101. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  102. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  103. 'imalsonotrequired' => array(
  104. 'alpha' => array('rule' => 'alphaNumeric','allowEmpty' => true),
  105. 'between' => array('rule' => array('between', 5, 30)),
  106. ),
  107. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  108. );
  109. /**
  110. * schema method
  111. *
  112. * @return void
  113. */
  114. public function setSchema($schema) {
  115. $this->_schema = $schema;
  116. }
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  123. /**
  124. * hasAndBelongsToMany property
  125. *
  126. * @var array
  127. */
  128. public $belongsTo = array('User' => array('className' => 'UserForm'));
  129. }
  130. /**
  131. * ContactTagsContact class
  132. *
  133. * @package cake
  134. * @package Cake.Test.Case.View.Helper
  135. */
  136. class ContactTagsContact extends CakeTestModel {
  137. /**
  138. * useTable property
  139. *
  140. * @var bool false
  141. */
  142. public $useTable = false;
  143. /**
  144. * name property
  145. *
  146. * @var string 'Contact'
  147. */
  148. public $name = 'ContactTagsContact';
  149. /**
  150. * Default schema
  151. *
  152. * @var array
  153. */
  154. protected $_schema = array(
  155. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  156. 'contact_tag_id' => array(
  157. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  158. )
  159. );
  160. /**
  161. * schema method
  162. *
  163. * @return void
  164. */
  165. public function setSchema($schema) {
  166. $this->_schema = $schema;
  167. }
  168. }
  169. /**
  170. * ContactNonStandardPk class
  171. *
  172. * @package cake
  173. * @package Cake.Test.Case.View.Helper
  174. */
  175. class ContactNonStandardPk extends Contact {
  176. /**
  177. * primaryKey property
  178. *
  179. * @var string 'pk'
  180. */
  181. public $primaryKey = 'pk';
  182. /**
  183. * name property
  184. *
  185. * @var string 'ContactNonStandardPk'
  186. */
  187. public $name = 'ContactNonStandardPk';
  188. /**
  189. * schema method
  190. *
  191. * @return void
  192. */
  193. public function schema($field = false) {
  194. $this->_schema = parent::schema();
  195. $this->_schema['pk'] = $this->_schema['id'];
  196. unset($this->_schema['id']);
  197. return $this->_schema;
  198. }
  199. }
  200. /**
  201. * ContactTag class
  202. *
  203. * @package cake
  204. * @package Cake.Test.Case.View.Helper
  205. */
  206. class ContactTag extends Model {
  207. /**
  208. * useTable property
  209. *
  210. * @var bool false
  211. */
  212. public $useTable = false;
  213. /**
  214. * schema definition
  215. *
  216. * @var array
  217. */
  218. protected $_schema = array(
  219. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  220. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  221. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  222. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  223. );
  224. }
  225. /**
  226. * UserForm class
  227. *
  228. * @package cake
  229. * @package Cake.Test.Case.View.Helper
  230. */
  231. class UserForm extends CakeTestModel {
  232. /**
  233. * useTable property
  234. *
  235. * @var bool false
  236. */
  237. public $useTable = false;
  238. /**
  239. * primaryKey property
  240. *
  241. * @var string 'id'
  242. */
  243. public $primaryKey = 'id';
  244. /**
  245. * name property
  246. *
  247. * @var string 'UserForm'
  248. */
  249. public $name = 'UserForm';
  250. /**
  251. * hasMany property
  252. *
  253. * @var array
  254. */
  255. public $hasMany = array(
  256. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  257. ));
  258. /**
  259. * schema definition
  260. *
  261. * @var array
  262. */
  263. protected $_schema = array(
  264. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  265. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  266. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  267. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  268. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  269. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  270. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  271. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  272. );
  273. }
  274. /**
  275. * OpenidUrl class
  276. *
  277. * @package cake
  278. * @package Cake.Test.Case.View.Helper
  279. */
  280. class OpenidUrl extends CakeTestModel {
  281. /**
  282. * useTable property
  283. *
  284. * @var bool false
  285. */
  286. public $useTable = false;
  287. /**
  288. * primaryKey property
  289. *
  290. * @var string 'id'
  291. */
  292. public $primaryKey = 'id';
  293. /**
  294. * name property
  295. *
  296. * @var string 'OpenidUrl'
  297. */
  298. public $name = 'OpenidUrl';
  299. /**
  300. * belongsTo property
  301. *
  302. * @var array
  303. */
  304. public $belongsTo = array('UserForm' => array(
  305. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  306. ));
  307. /**
  308. * validate property
  309. *
  310. * @var array
  311. */
  312. public $validate = array('openid_not_registered' => array());
  313. /**
  314. * schema method
  315. *
  316. * @var array
  317. */
  318. protected $_schema = array(
  319. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  320. 'user_form_id' => array(
  321. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  322. ),
  323. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  324. );
  325. /**
  326. * beforeValidate method
  327. *
  328. * @return void
  329. */
  330. public function beforeValidate($options = array()) {
  331. $this->invalidate('openid_not_registered');
  332. return true;
  333. }
  334. }
  335. /**
  336. * ValidateUser class
  337. *
  338. * @package cake
  339. * @package Cake.Test.Case.View.Helper
  340. */
  341. class ValidateUser extends CakeTestModel {
  342. /**
  343. * primaryKey property
  344. *
  345. * @var string 'id'
  346. */
  347. public $primaryKey = 'id';
  348. /**
  349. * useTable property
  350. *
  351. * @var bool false
  352. */
  353. public $useTable = false;
  354. /**
  355. * name property
  356. *
  357. * @var string 'ValidateUser'
  358. */
  359. public $name = 'ValidateUser';
  360. /**
  361. * hasOne property
  362. *
  363. * @var array
  364. */
  365. public $hasOne = array('ValidateProfile' => array(
  366. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  367. ));
  368. /**
  369. * schema method
  370. *
  371. * @var array
  372. */
  373. protected $_schema = array(
  374. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  375. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  376. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  377. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  378. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  379. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  380. );
  381. /**
  382. * beforeValidate method
  383. *
  384. * @return void
  385. */
  386. public function beforeValidate($options = array()) {
  387. $this->invalidate('email');
  388. return false;
  389. }
  390. }
  391. /**
  392. * ValidateProfile class
  393. *
  394. * @package cake
  395. * @package Cake.Test.Case.View.Helper
  396. */
  397. class ValidateProfile extends CakeTestModel {
  398. /**
  399. * primaryKey property
  400. *
  401. * @var string 'id'
  402. */
  403. public $primaryKey = 'id';
  404. /**
  405. * useTable property
  406. *
  407. * @var bool false
  408. */
  409. public $useTable = false;
  410. /**
  411. * schema property
  412. *
  413. * @var array
  414. */
  415. protected $_schema = array(
  416. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  417. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  418. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  419. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  420. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  421. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  422. );
  423. /**
  424. * name property
  425. *
  426. * @var string 'ValidateProfile'
  427. */
  428. public $name = 'ValidateProfile';
  429. /**
  430. * hasOne property
  431. *
  432. * @var array
  433. */
  434. public $hasOne = array('ValidateItem' => array(
  435. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  436. ));
  437. /**
  438. * belongsTo property
  439. *
  440. * @var array
  441. */
  442. public $belongsTo = array('ValidateUser' => array(
  443. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  444. ));
  445. /**
  446. * beforeValidate method
  447. *
  448. * @return void
  449. */
  450. public function beforeValidate($options = array()) {
  451. $this->invalidate('full_name');
  452. $this->invalidate('city');
  453. return false;
  454. }
  455. }
  456. /**
  457. * ValidateItem class
  458. *
  459. * @package cake
  460. * @package Cake.Test.Case.View.Helper
  461. */
  462. class ValidateItem extends CakeTestModel {
  463. /**
  464. * primaryKey property
  465. *
  466. * @var string 'id'
  467. */
  468. public $primaryKey = 'id';
  469. /**
  470. * useTable property
  471. *
  472. * @var bool false
  473. */
  474. public $useTable = false;
  475. /**
  476. * name property
  477. *
  478. * @var string 'ValidateItem'
  479. */
  480. public $name = 'ValidateItem';
  481. /**
  482. * schema property
  483. *
  484. * @var array
  485. */
  486. protected $_schema = array(
  487. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  488. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  489. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  490. 'description' => array(
  491. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  492. ),
  493. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  494. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  495. );
  496. /**
  497. * belongsTo property
  498. *
  499. * @var array
  500. */
  501. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  502. /**
  503. * beforeValidate method
  504. *
  505. * @return void
  506. */
  507. public function beforeValidate($options = array()) {
  508. $this->invalidate('description');
  509. return false;
  510. }
  511. }
  512. /**
  513. * TestMail class
  514. *
  515. * @package cake
  516. * @package Cake.Test.Case.View.Helper
  517. */
  518. class TestMail extends CakeTestModel {
  519. /**
  520. * primaryKey property
  521. *
  522. * @var string 'id'
  523. */
  524. public $primaryKey = 'id';
  525. /**
  526. * useTable property
  527. *
  528. * @var bool false
  529. */
  530. public $useTable = false;
  531. /**
  532. * name property
  533. *
  534. * @var string 'TestMail'
  535. */
  536. public $name = 'TestMail';
  537. }
  538. /**
  539. * FormHelperTest class
  540. *
  541. * @package cake
  542. * @package Cake.Test.Case.View.Helper
  543. */
  544. class FormHelperTest extends CakeTestCase {
  545. /**
  546. * Fixtures to be used
  547. *
  548. * @var array
  549. */
  550. public $fixtures = array('core.post');
  551. /**
  552. * Do not load the fixtures by default
  553. *
  554. * @var boolean
  555. */
  556. public $autoFixtures = false;
  557. /**
  558. * setUp method
  559. *
  560. * @return void
  561. */
  562. public function setUp() {
  563. parent::setUp();
  564. Configure::write('App.base', '');
  565. $this->Controller = new ContactTestController();
  566. $this->View = new View($this->Controller);
  567. $this->Form = new FormHelper($this->View);
  568. $this->Form->Html = new HtmlHelper($this->View);
  569. $this->Form->request = new CakeRequest('contacts/add', false);
  570. $this->Form->request->here = '/contacts/add';
  571. $this->Form->request['action'] = 'add';
  572. $this->Form->request->webroot = '';
  573. $this->Form->request->base = '';
  574. ClassRegistry::addObject('Contact', new Contact());
  575. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  576. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  577. ClassRegistry::addObject('User', new UserForm());
  578. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  579. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  580. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  581. $this->oldSalt = Configure::read('Security.salt');
  582. $this->dateRegex = array(
  583. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  584. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  585. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  586. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  587. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  588. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  589. );
  590. Configure::write('Security.salt', 'foo!');
  591. }
  592. /**
  593. * tearDown method
  594. *
  595. * @return void
  596. */
  597. public function tearDown() {
  598. parent::tearDown();
  599. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  600. Configure::write('Security.salt', $this->oldSalt);
  601. }
  602. /**
  603. * testFormCreateWithSecurity method
  604. *
  605. * Test form->create() with security key.
  606. *
  607. * @return void
  608. */
  609. public function testCreateWithSecurity() {
  610. $this->Form->request['_Token'] = array('key' => 'testKey');
  611. $encoding = strtolower(Configure::read('App.encoding'));
  612. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  613. $expected = array(
  614. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  615. 'div' => array('style' => 'display:none;'),
  616. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  617. array('input' => array(
  618. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  619. )),
  620. '/div'
  621. );
  622. $this->assertTags($result, $expected);
  623. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  624. $expected['form']['id'] = 'MyForm';
  625. $this->assertTags($result, $expected);
  626. }
  627. /**
  628. * test that create() clears the fields property so it starts fresh
  629. *
  630. * @return void
  631. */
  632. public function testCreateClearingFields() {
  633. $this->Form->fields = array('model_id');
  634. $this->Form->create('Contact');
  635. $this->assertEquals(array(), $this->Form->fields);
  636. }
  637. /**
  638. * Tests form hash generation with model-less data
  639. *
  640. * @return void
  641. */
  642. public function testValidateHashNoModel() {
  643. $this->Form->request['_Token'] = array('key' => 'foo');
  644. $result = $this->Form->secure(array('anything'));
  645. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  646. }
  647. /**
  648. * Tests that models with identical field names get resolved properly
  649. *
  650. * @return void
  651. */
  652. public function testDuplicateFieldNameResolution() {
  653. $result = $this->Form->create('ValidateUser');
  654. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  655. $result = $this->Form->input('ValidateItem.name');
  656. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  657. $result = $this->Form->input('ValidateUser.name');
  658. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  659. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  660. $this->assertRegExp('/type="text"/', $result);
  661. $result = $this->Form->input('ValidateItem.name');
  662. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  663. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  664. $this->assertRegExp('/<textarea/', $result);
  665. $result = $this->Form->input('name');
  666. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  667. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  668. $this->assertRegExp('/type="text"/', $result);
  669. }
  670. /**
  671. * Tests that hidden fields generated for checkboxes don't get locked
  672. *
  673. * @return void
  674. */
  675. public function testNoCheckboxLocking() {
  676. $this->Form->request['_Token'] = array('key' => 'foo');
  677. $this->assertSame(array(), $this->Form->fields);
  678. $this->Form->checkbox('check', array('value' => '1'));
  679. $this->assertSame($this->Form->fields, array('check'));
  680. }
  681. /**
  682. * testFormSecurityFields method
  683. *
  684. * Test generation of secure form hash generation.
  685. *
  686. * @return void
  687. */
  688. public function testFormSecurityFields() {
  689. $key = 'testKey';
  690. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  691. $this->Form->request['_Token'] = array('key' => $key);
  692. $result = $this->Form->secure($fields);
  693. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  694. $expected .= ':' . 'Model.valid';
  695. $expected = array(
  696. 'div' => array('style' => 'display:none;'),
  697. array('input' => array(
  698. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  699. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  700. )),
  701. array('input' => array(
  702. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  703. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  704. )),
  705. '/div'
  706. );
  707. $this->assertTags($result, $expected);
  708. }
  709. /**
  710. * Tests correct generation of number fields for double and float fields
  711. *
  712. * @return void
  713. */
  714. public function testTextFieldGenerationForFloats() {
  715. $model = ClassRegistry::getObject('Contact');
  716. $model->setSchema(array('foo' => array(
  717. 'type' => 'float',
  718. 'null' => false,
  719. 'default' => null,
  720. 'length' => null
  721. )));
  722. $this->Form->create('Contact');
  723. $result = $this->Form->input('foo');
  724. $expected = array(
  725. 'div' => array('class' => 'input number'),
  726. 'label' => array('for' => 'ContactFoo'),
  727. 'Foo',
  728. '/label',
  729. array('input' => array(
  730. 'type' => 'number',
  731. 'name' => 'data[Contact][foo]',
  732. 'id' => 'ContactFoo',
  733. 'step' => 'any'
  734. )),
  735. '/div'
  736. );
  737. $this->assertTags($result, $expected);
  738. $result = $this->Form->input('foo', array('step' => 0.5));
  739. $expected = array(
  740. 'div' => array('class' => 'input number'),
  741. 'label' => array('for' => 'ContactFoo'),
  742. 'Foo',
  743. '/label',
  744. array('input' => array(
  745. 'type' => 'number',
  746. 'name' => 'data[Contact][foo]',
  747. 'id' => 'ContactFoo',
  748. 'step' => '0.5'
  749. )),
  750. '/div'
  751. );
  752. $this->assertTags($result, $expected);
  753. }
  754. /**
  755. * Tests correct generation of number fields for integer fields
  756. *
  757. * @access public
  758. * @return void
  759. */
  760. public function testTextFieldTypeNumberGenerationForIntegers() {
  761. $model = ClassRegistry::getObject('Contact');
  762. $model->setSchema(array('foo' => array(
  763. 'type' => 'integer',
  764. 'null' => false,
  765. 'default' => null,
  766. 'length' => null
  767. )));
  768. $this->Form->create('Contact');
  769. $result = $this->Form->input('foo');
  770. $expected = array(
  771. 'div' => array('class' => 'input number'),
  772. 'label' => array('for' => 'ContactFoo'),
  773. 'Foo',
  774. '/label',
  775. array('input' => array(
  776. 'type' => 'number', 'name' => 'data[Contact][foo]',
  777. 'id' => 'ContactFoo'
  778. )),
  779. '/div'
  780. );
  781. $this->assertTags($result, $expected);
  782. }
  783. /**
  784. * testFormSecurityMultipleFields method
  785. *
  786. * Test secure() with multiple row form. Ensure hash is correct.
  787. *
  788. * @return void
  789. */
  790. public function testFormSecurityMultipleFields() {
  791. $key = 'testKey';
  792. $fields = array(
  793. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  794. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  795. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  796. );
  797. $this->Form->request['_Token'] = array('key' => $key);
  798. $result = $this->Form->secure($fields);
  799. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  800. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  801. $expected = array(
  802. 'div' => array('style' => 'display:none;'),
  803. array('input' => array(
  804. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  805. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  806. )),
  807. array('input' => array(
  808. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  809. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  810. )),
  811. '/div'
  812. );
  813. $this->assertTags($result, $expected);
  814. }
  815. /**
  816. * testFormSecurityMultipleSubmitButtons
  817. *
  818. * test form submit generation and ensure that _Token is only created on end()
  819. *
  820. * @return void
  821. */
  822. public function testFormSecurityMultipleSubmitButtons() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('Addresses');
  826. $this->Form->input('Address.title');
  827. $this->Form->input('Address.first_name');
  828. $result = $this->Form->submit('Save', array('name' => 'save'));
  829. $expected = array(
  830. 'div' => array('class' => 'submit'),
  831. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  832. '/div',
  833. );
  834. $this->assertTags($result, $expected);
  835. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  836. $expected = array(
  837. 'div' => array('class' => 'submit'),
  838. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  839. '/div',
  840. );
  841. $this->assertTags($result, $expected);
  842. $result = $this->Form->end(null);
  843. $expected = array(
  844. 'div' => array('style' => 'display:none;'),
  845. array('input' => array(
  846. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  847. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  848. )),
  849. array('input' => array(
  850. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  851. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  852. )),
  853. '/div'
  854. );
  855. $this->assertTags($result, $expected);
  856. }
  857. /**
  858. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  859. *
  860. * @return void
  861. */
  862. public function testSecurityButtonNestedNamed() {
  863. $key = 'testKey';
  864. $this->Form->request['_Token'] = array('key' => $key);
  865. $this->Form->create('Addresses');
  866. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  867. $result = $this->Form->unlockField();
  868. $this->assertEquals(array('Address.button'), $result);
  869. }
  870. /**
  871. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  872. *
  873. * @return void
  874. */
  875. public function testSecuritySubmitNestedNamed() {
  876. $key = 'testKey';
  877. $this->Form->request['_Token'] = array('key' => $key);
  878. $this->Form->create('Addresses');
  879. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  880. $result = $this->Form->unlockField();
  881. $this->assertEquals(array('Address.button'), $result);
  882. }
  883. /**
  884. * Test that the correct fields are unlocked for image submits with no names.
  885. *
  886. * @return void
  887. */
  888. public function testSecuritySubmitImageNoName() {
  889. $key = 'testKey';
  890. $this->Form->request['_Token'] = array('key' => $key);
  891. $this->Form->create('User');
  892. $result = $this->Form->submit('save.png');
  893. $expected = array(
  894. 'div' => array('class' => 'submit'),
  895. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  896. '/div'
  897. );
  898. $this->assertTags($result, $expected);
  899. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  900. }
  901. /**
  902. * Test that the correct fields are unlocked for image submits with names.
  903. *
  904. * @return void
  905. */
  906. public function testSecuritySubmitImageName() {
  907. $key = 'testKey';
  908. $this->Form->request['_Token'] = array('key' => $key);
  909. $this->Form->create('User');
  910. $result = $this->Form->submit('save.png', array('name' => 'test'));
  911. $expected = array(
  912. 'div' => array('class' => 'submit'),
  913. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  914. '/div'
  915. );
  916. $this->assertTags($result, $expected);
  917. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  918. }
  919. /**
  920. * testFormSecurityMultipleInputFields method
  921. *
  922. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  923. *
  924. * @return void
  925. */
  926. public function testFormSecurityMultipleInputFields() {
  927. $key = 'testKey';
  928. $this->Form->request['_Token'] = array('key' => $key);
  929. $this->Form->create('Addresses');
  930. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  931. $this->Form->input('Addresses.0.title');
  932. $this->Form->input('Addresses.0.first_name');
  933. $this->Form->input('Addresses.0.last_name');
  934. $this->Form->input('Addresses.0.address');
  935. $this->Form->input('Addresses.0.city');
  936. $this->Form->input('Addresses.0.phone');
  937. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  938. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  939. $this->Form->input('Addresses.1.title');
  940. $this->Form->input('Addresses.1.first_name');
  941. $this->Form->input('Addresses.1.last_name');
  942. $this->Form->input('Addresses.1.address');
  943. $this->Form->input('Addresses.1.city');
  944. $this->Form->input('Addresses.1.phone');
  945. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  946. $result = $this->Form->secure($this->Form->fields);
  947. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  948. $expected = array(
  949. 'div' => array('style' => 'display:none;'),
  950. array('input' => array(
  951. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  952. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  953. )),
  954. array('input' => array(
  955. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  956. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  957. )),
  958. '/div'
  959. );
  960. $this->assertTags($result, $expected);
  961. }
  962. /**
  963. * Test form security with Model.field.0 style inputs
  964. *
  965. * @return void
  966. */
  967. public function testFormSecurityArrayFields() {
  968. $key = 'testKey';
  969. $this->Form->request->params['_Token']['key'] = $key;
  970. $this->Form->create('Address');
  971. $this->Form->input('Address.primary.1');
  972. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  973. $this->Form->input('Address.secondary.1.0');
  974. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  975. }
  976. /**
  977. * testFormSecurityMultipleInputDisabledFields method
  978. *
  979. * test secure form generation with multiple records and disabled fields.
  980. *
  981. * @return void
  982. */
  983. public function testFormSecurityMultipleInputDisabledFields() {
  984. $key = 'testKey';
  985. $this->Form->request->params['_Token'] = array(
  986. 'key' => $key,
  987. 'unlockedFields' => array('first_name', 'address')
  988. );
  989. $this->Form->create();
  990. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  991. $this->Form->input('Addresses.0.title');
  992. $this->Form->input('Addresses.0.first_name');
  993. $this->Form->input('Addresses.0.last_name');
  994. $this->Form->input('Addresses.0.address');
  995. $this->Form->input('Addresses.0.city');
  996. $this->Form->input('Addresses.0.phone');
  997. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  998. $this->Form->input('Addresses.1.title');
  999. $this->Form->input('Addresses.1.first_name');
  1000. $this->Form->input('Addresses.1.last_name');
  1001. $this->Form->input('Addresses.1.address');
  1002. $this->Form->input('Addresses.1.city');
  1003. $this->Form->input('Addresses.1.phone');
  1004. $result = $this->Form->secure($this->Form->fields);
  1005. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1006. $expected = array(
  1007. 'div' => array('style' => 'display:none;'),
  1008. array('input' => array(
  1009. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1010. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1011. )),
  1012. array('input' => array(
  1013. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1014. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1015. )),
  1016. '/div'
  1017. );
  1018. $this->assertTags($result, $expected);
  1019. }
  1020. /**
  1021. * testFormSecurityInputDisabledFields method
  1022. *
  1023. * Test single record form with disabled fields.
  1024. *
  1025. * @return void
  1026. */
  1027. public function testFormSecurityInputUnlockedFields() {
  1028. $key = 'testKey';
  1029. $this->Form->request['_Token'] = array(
  1030. 'key' => $key,
  1031. 'unlockedFields' => array('first_name', 'address')
  1032. );
  1033. $this->Form->create();
  1034. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1035. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1036. $this->Form->input('Addresses.title');
  1037. $this->Form->input('Addresses.first_name');
  1038. $this->Form->input('Addresses.last_name');
  1039. $this->Form->input('Addresses.address');
  1040. $this->Form->input('Addresses.city');
  1041. $this->Form->input('Addresses.phone');
  1042. $result = $this->Form->fields;
  1043. $expected = array(
  1044. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1045. 'Addresses.city', 'Addresses.phone'
  1046. );
  1047. $this->assertEquals($expected, $result);
  1048. $result = $this->Form->secure($expected);
  1049. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1050. $expected = array(
  1051. 'div' => array('style' => 'display:none;'),
  1052. array('input' => array(
  1053. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1054. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1055. )),
  1056. array('input' => array(
  1057. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1058. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1059. )),
  1060. '/div'
  1061. );
  1062. $this->assertTags($result, $expected);
  1063. }
  1064. /**
  1065. * test securing inputs with custom name attributes.
  1066. *
  1067. * @return void
  1068. */
  1069. public function testFormSecureWithCustomNameAttribute() {
  1070. $this->Form->request->params['_Token']['key'] = 'testKey';
  1071. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1072. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1073. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1074. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1075. }
  1076. /**
  1077. * testFormSecuredInput method
  1078. *
  1079. * Test generation of entire secure form, assertions made on input() output.
  1080. *
  1081. * @return void
  1082. */
  1083. public function testFormSecuredInput() {
  1084. $this->Form->request['_Token'] = array('key' => 'testKey');
  1085. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1086. $encoding = strtolower(Configure::read('App.encoding'));
  1087. $expected = array(
  1088. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1089. 'div' => array('style' => 'display:none;'),
  1090. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1091. array('input' => array(
  1092. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1093. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1094. )),
  1095. '/div'
  1096. );
  1097. $this->assertTags($result, $expected);
  1098. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1099. $expected = array(
  1100. 'div' => array('class' => 'input text'),
  1101. 'label' => array('for' => 'UserFormPublished'),
  1102. 'Published',
  1103. '/label',
  1104. array('input' => array(
  1105. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1106. 'id' => 'UserFormPublished'
  1107. )),
  1108. '/div'
  1109. );
  1110. $this->assertTags($result, $expected);
  1111. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1112. $expected = array(
  1113. 'div' => array('class' => 'input text'),
  1114. 'label' => array('for' => 'UserFormOther'),
  1115. 'Other',
  1116. '/label',
  1117. array('input' => array(
  1118. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1119. 'id' => 'UserFormOther'
  1120. )),
  1121. '/div'
  1122. );
  1123. $this->assertTags($result, $expected);
  1124. $result = $this->Form->hidden('UserForm.stuff');
  1125. $expected = array('input' => array(
  1126. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1127. 'id' => 'UserFormStuff'
  1128. ));
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1131. $expected = array('input' => array(
  1132. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1133. 'value' => '0', 'id' => 'UserFormHidden'
  1134. ));
  1135. $this->assertTags($result, $expected);
  1136. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1137. $expected = array(
  1138. 'div' => array('class' => 'input checkbox'),
  1139. array('input' => array(
  1140. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1141. 'value' => '0', 'id' => 'UserFormSomething_'
  1142. )),
  1143. array('input' => array(
  1144. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1145. 'value' => '1', 'id' => 'UserFormSomething'
  1146. )),
  1147. 'label' => array('for' => 'UserFormSomething'),
  1148. 'Something',
  1149. '/label',
  1150. '/div'
  1151. );
  1152. $this->assertTags($result, $expected);
  1153. $result = $this->Form->fields;
  1154. $expected = array(
  1155. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1156. 'UserForm.hidden' => '0', 'UserForm.something'
  1157. );
  1158. $this->assertEquals($expected, $result);
  1159. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1160. $result = $this->Form->secure($this->Form->fields);
  1161. $expected = array(
  1162. 'div' => array('style' => 'display:none;'),
  1163. array('input' => array(
  1164. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1165. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1166. )),
  1167. array('input' => array(
  1168. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1169. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1170. )),
  1171. '/div'
  1172. );
  1173. $this->assertTags($result, $expected);
  1174. }
  1175. /**
  1176. * Tests that the correct keys are added to the field hash index
  1177. *
  1178. * @return void
  1179. */
  1180. public function testFormSecuredFileInput() {
  1181. $this->Form->request['_Token'] = array('key' => 'testKey');
  1182. $this->assertEquals(array(), $this->Form->fields);
  1183. $result = $this->Form->file('Attachment.file');
  1184. $expected = array(
  1185. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1186. 'Attachment.file.error', 'Attachment.file.size'
  1187. );
  1188. $this->assertEquals($expected, $this->Form->fields);
  1189. }
  1190. /**
  1191. * test that multiple selects keys are added to field hash
  1192. *
  1193. * @return void
  1194. */
  1195. public function testFormSecuredMultipleSelect() {
  1196. $this->Form->request['_Token'] = array('key' => 'testKey');
  1197. $this->assertEquals(array(), $this->Form->fields);
  1198. $options = array('1' => 'one', '2' => 'two');
  1199. $this->Form->select('Model.select', $options);
  1200. $expected = array('Model.select');
  1201. $this->assertEquals($expected, $this->Form->fields);
  1202. $this->Form->fields = array();
  1203. $this->Form->select('Model.select', $options, array('multiple' => true));
  1204. $this->assertEquals($expected, $this->Form->fields);
  1205. }
  1206. /**
  1207. * testFormSecuredRadio method
  1208. *
  1209. * @return void
  1210. */
  1211. public function testFormSecuredRadio() {
  1212. $this->Form->request['_Token'] = array('key' => 'testKey');
  1213. $this->assertEquals(array(), $this->Form->fields);
  1214. $options = array('1' => 'option1', '2' => 'option2');
  1215. $this->Form->radio('Test.test', $options);
  1216. $expected = array('Test.test');
  1217. $this->assertEquals($expected, $this->Form->fields);
  1218. }
  1219. /**
  1220. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1221. * hashing.
  1222. *
  1223. * @return void
  1224. */
  1225. public function testFormSecuredAndDisabled() {
  1226. $this->Form->request['_Token'] = array('key' => 'testKey');
  1227. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1228. $this->Form->text('Model.text', array('disabled' => true));
  1229. $this->Form->password('Model.text', array('disabled' => true));
  1230. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1231. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1232. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1233. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1234. $this->Form->month('Model.month', array('disabled' => true));
  1235. $this->Form->day('Model.day', array('disabled' => true));
  1236. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1237. $this->Form->minute('Model.minute', array('disabled' => true));
  1238. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1239. $expected = array(
  1240. 'Model.radio' => ''
  1241. );
  1242. $this->assertEquals($expected, $this->Form->fields);
  1243. }
  1244. /**
  1245. * testDisableSecurityUsingForm method
  1246. *
  1247. * @return void
  1248. */
  1249. public function testDisableSecurityUsingForm() {
  1250. $this->Form->request['_Token'] = array(
  1251. 'key' => 'testKey',
  1252. 'disabledFields' => array()
  1253. );
  1254. $this->Form->create();
  1255. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1256. $this->Form->input('Addresses.title');
  1257. $this->Form->input('Addresses.first_name', array('secure' => false));
  1258. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1259. $this->Form->input('Addresses.zip', array(
  1260. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1261. ));
  1262. $result = $this->Form->fields;
  1263. $expected = array(
  1264. 'Addresses.id' => '123456', 'Addresses.title',
  1265. );
  1266. $this->assertEquals($expected, $result);
  1267. }
  1268. /**
  1269. * test disableField
  1270. *
  1271. * @return void
  1272. */
  1273. public function testUnlockFieldAddsToList() {
  1274. $this->Form->request['_Token'] = array(
  1275. 'key' => 'testKey',
  1276. 'unlockedFields' => array()
  1277. );
  1278. $this->Form->create('Contact');
  1279. $this->Form->unlockField('Contact.name');
  1280. $this->Form->text('Contact.name');
  1281. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1282. $this->assertEquals(array(), $this->Form->fields);
  1283. }
  1284. /**
  1285. * test unlockField removing from fields array.
  1286. *
  1287. * @return void
  1288. */
  1289. public function testUnlockFieldRemovingFromFields() {
  1290. $this->Form->request['_Token'] = array(
  1291. 'key' => 'testKey',
  1292. 'unlockedFields' => array()
  1293. );
  1294. $this->Form->create('Contact');
  1295. $this->Form->hidden('Contact.id', array('value' => 1));
  1296. $this->Form->text('Contact.name');
  1297. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1298. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1299. $this->Form->unlockField('Contact.name');
  1300. $this->Form->unlockField('Contact.id');
  1301. $this->assertEquals(array(), $this->Form->fields);
  1302. }
  1303. /**
  1304. * testTagIsInvalid method
  1305. *
  1306. * @return void
  1307. */
  1308. public function testTagIsInvalid() {
  1309. $Contact = ClassRegistry::getObject('Contact');
  1310. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1311. $this->Form->setEntity('Contact.0.email');
  1312. $result = $this->Form->tagIsInvalid();
  1313. $expected = array('Please provide an email');
  1314. $this->assertEquals($expected, $result);
  1315. $this->Form->setEntity('Contact.1.email');
  1316. $result = $this->Form->tagIsInvalid();
  1317. $expected = false;
  1318. $this->assertIdentical($expected, $result);
  1319. $this->Form->setEntity('Contact.0.name');
  1320. $result = $this->Form->tagIsInvalid();
  1321. $expected = false;
  1322. $this->assertIdentical($expected, $result);
  1323. }
  1324. /**
  1325. * testPasswordValidation method
  1326. *
  1327. * test validation errors on password input.
  1328. *
  1329. * @return void
  1330. */
  1331. public function testPasswordValidation() {
  1332. $Contact = ClassRegistry::getObject('Contact');
  1333. $Contact->validationErrors['password'] = array('Please provide a password');
  1334. $result = $this->Form->input('Contact.password');
  1335. $expected = array(
  1336. 'div' => array('class' => 'input password error'),
  1337. 'label' => array('for' => 'ContactPassword'),
  1338. 'Password',
  1339. '/label',
  1340. 'input' => array(
  1341. 'type' => 'password', 'name' => 'data[Contact][password]',
  1342. 'id' => 'ContactPassword', 'class' => 'form-error'
  1343. ),
  1344. array('div' => array('class' => 'error-message')),
  1345. 'Please provide a password',
  1346. '/div',
  1347. '/div'
  1348. );
  1349. $this->assertTags($result, $expected);
  1350. }
  1351. /**
  1352. * testEmptyErrorValidation method
  1353. *
  1354. * test validation error div when validation message is an empty string
  1355. *
  1356. * @access public
  1357. * @return void
  1358. */
  1359. public function testEmptyErrorValidation() {
  1360. $this->Form->validationErrors['Contact']['password'] = '';
  1361. $result = $this->Form->input('Contact.password');
  1362. $expected = array(
  1363. 'div' => array('class' => 'input password error'),
  1364. 'label' => array('for' => 'ContactPassword'),
  1365. 'Password',
  1366. '/label',
  1367. 'input' => array(
  1368. 'type' => 'password', 'name' => 'data[Contact][password]',
  1369. 'id' => 'ContactPassword', 'class' => 'form-error'
  1370. ),
  1371. array('div' => array('class' => 'error-message')),
  1372. array(),
  1373. '/div',
  1374. '/div'
  1375. );
  1376. $this->assertTags($result, $expected);
  1377. }
  1378. /**
  1379. * testEmptyInputErrorValidation method
  1380. *
  1381. * test validation error div when validation message is overridden by an empty string when calling input()
  1382. *
  1383. * @access public
  1384. * @return void
  1385. */
  1386. public function testEmptyInputErrorValidation() {
  1387. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1388. $result = $this->Form->input('Contact.password', array('error' => ''));
  1389. $expected = array(
  1390. 'div' => array('class' => 'input password error'),
  1391. 'label' => array('for' => 'ContactPassword'),
  1392. 'Password',
  1393. '/label',
  1394. 'input' => array(
  1395. 'type' => 'password', 'name' => 'data[Contact][password]',
  1396. 'id' => 'ContactPassword', 'class' => 'form-error'
  1397. ),
  1398. array('div' => array('class' => 'error-message')),
  1399. array(),
  1400. '/div',
  1401. '/div'
  1402. );
  1403. $this->assertTags($result, $expected);
  1404. }
  1405. /**
  1406. * testFormValidationAssociated method
  1407. *
  1408. * test display of form errors in conjunction with model::validates.
  1409. *
  1410. * @return void
  1411. */
  1412. public function testFormValidationAssociated() {
  1413. $this->UserForm = ClassRegistry::getObject('UserForm');
  1414. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1415. $data = array(
  1416. 'UserForm' => array('name' => 'user'),
  1417. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1418. );
  1419. $result = $this->UserForm->OpenidUrl->create($data);
  1420. $this->assertFalse(empty($result));
  1421. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1422. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1423. $encoding = strtolower(Configure::read('App.encoding'));
  1424. $expected = array(
  1425. 'form' => array(
  1426. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1427. 'accept-charset' => $encoding
  1428. ),
  1429. 'div' => array('style' => 'display:none;'),
  1430. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1431. '/div'
  1432. );
  1433. $this->assertTags($result, $expected);
  1434. $result = $this->Form->error(
  1435. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1436. );
  1437. $this->assertEquals('Error, not registered', $result);
  1438. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1439. }
  1440. /**
  1441. * testFormValidationAssociatedFirstLevel method
  1442. *
  1443. * test form error display with associated model.
  1444. *
  1445. * @return void
  1446. */
  1447. public function testFormValidationAssociatedFirstLevel() {
  1448. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1449. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1450. $data = array(
  1451. 'ValidateUser' => array('name' => 'mariano'),
  1452. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1453. );
  1454. $result = $this->ValidateUser->create($data);
  1455. $this->assertFalse(empty($result));
  1456. $this->assertFalse($this->ValidateUser->validates());
  1457. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1458. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1459. $encoding = strtolower(Configure::read('App.encoding'));
  1460. $expected = array(
  1461. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1462. 'div' => array('style' => 'display:none;'),
  1463. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1464. '/div'
  1465. );
  1466. $this->assertTags($result, $expected);
  1467. $result = $this->Form->error(
  1468. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1469. );
  1470. $this->assertEquals('Invalid email', $result);
  1471. $result = $this->Form->error(
  1472. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1473. );
  1474. $this->assertEquals('Invalid name', $result);
  1475. $result = $this->Form->error(
  1476. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1477. );
  1478. $this->assertEquals('Invalid city', $result);
  1479. unset($this->ValidateUser->ValidateProfile);
  1480. unset($this->ValidateUser);
  1481. }
  1482. /**
  1483. * testFormValidationAssociatedSecondLevel method
  1484. *
  1485. * test form error display with associated model.
  1486. *
  1487. * @return void
  1488. */
  1489. public function testFormValidationAssociatedSecondLevel() {
  1490. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1491. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1492. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1493. $data = array(
  1494. 'ValidateUser' => array('name' => 'mariano'),
  1495. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1496. 'ValidateItem' => array('name' => 'Item')
  1497. );
  1498. $result = $this->ValidateUser->create($data);
  1499. $this->assertFalse(empty($result));
  1500. $this->assertFalse($this->ValidateUser->validates());
  1501. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1502. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1503. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1504. $encoding = strtolower(Configure::read('App.encoding'));
  1505. $expected = array(
  1506. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1507. 'div' => array('style' => 'display:none;'),
  1508. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1509. '/div'
  1510. );
  1511. $this->assertTags($result, $expected);
  1512. $result = $this->Form->error(
  1513. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1514. );
  1515. $this->assertEquals('Invalid email', $result);
  1516. $result = $this->Form->error(
  1517. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1518. );
  1519. $this->assertEquals('Invalid name', $result);
  1520. $result = $this->Form->error(
  1521. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1522. );
  1523. $result = $this->Form->error(
  1524. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1525. );
  1526. $this->assertEquals('Invalid description', $result);
  1527. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1528. unset($this->ValidateUser->ValidateProfile);
  1529. unset($this->ValidateUser);
  1530. }
  1531. /**
  1532. * testFormValidationMultiRecord method
  1533. *
  1534. * test form error display with multiple records.
  1535. *
  1536. * @return void
  1537. */
  1538. public function testFormValidationMultiRecord() {
  1539. $Contact = ClassRegistry::getObject('Contact');
  1540. $Contact->validationErrors[2] = array(
  1541. 'name' => array('This field cannot be left blank')
  1542. );
  1543. $result = $this->Form->input('Contact.2.name');
  1544. $expected = array(
  1545. 'div' => array('class' => 'input text error'),
  1546. 'label' => array('for' => 'Contact2Name'),
  1547. 'Name',
  1548. '/label',
  1549. 'input' => array(
  1550. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1551. 'class' => 'form-error', 'maxlength' => 255
  1552. ),
  1553. array('div' => array('class' => 'error-message')),
  1554. 'This field cannot be left blank',
  1555. '/div',
  1556. '/div'
  1557. );
  1558. $this->assertTags($result, $expected);
  1559. }
  1560. /**
  1561. * testMultipleInputValidation method
  1562. *
  1563. * test multiple record form validation error display.
  1564. *
  1565. * @return void
  1566. */
  1567. public function testMultipleInputValidation() {
  1568. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1569. $Address->validationErrors[0] = array(
  1570. 'title' => array('This field cannot be empty'),
  1571. 'first_name' => array('This field cannot be empty')
  1572. );
  1573. $Address->validationErrors[1] = array(
  1574. 'last_name' => array('You must have a last name')
  1575. );
  1576. $this->Form->create();
  1577. $result = $this->Form->input('Address.0.title');
  1578. $expected = array(
  1579. 'div' => array('class'),
  1580. 'label' => array('for'),
  1581. 'preg:/[^<]+/',
  1582. '/label',
  1583. 'input' => array(
  1584. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1585. ),
  1586. array('div' => array('class' => 'error-message')),
  1587. 'This field cannot be empty',
  1588. '/div',
  1589. '/div'
  1590. );
  1591. $this->assertTags($result, $expected);
  1592. $result = $this->Form->input('Address.0.first_name');
  1593. $expected = array(
  1594. 'div' => array('class'),
  1595. 'label' => array('for'),
  1596. 'preg:/[^<]+/',
  1597. '/label',
  1598. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1599. array('div' => array('class' => 'error-message')),
  1600. 'This field cannot be empty',
  1601. '/div',
  1602. '/div'
  1603. );
  1604. $this->assertTags($result, $expected);
  1605. $result = $this->Form->input('Address.0.last_name');
  1606. $expected = array(
  1607. 'div' => array('class'),
  1608. 'label' => array('for'),
  1609. 'preg:/[^<]+/',
  1610. '/label',
  1611. 'input' => array('type' => 'text', 'name', 'id'),
  1612. '/div'
  1613. );
  1614. $this->assertTags($result, $expected);
  1615. $result = $this->Form->input('Address.1.last_name');
  1616. $expected = array(
  1617. 'div' => array('class'),
  1618. 'label' => array('for'),
  1619. 'preg:/[^<]+/',
  1620. '/label',
  1621. 'input' => array(
  1622. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1623. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1624. ),
  1625. array('div' => array('class' => 'error-message')),
  1626. 'You must have a last name',
  1627. '/div',
  1628. '/div'
  1629. );
  1630. $this->assertTags($result, $expected);
  1631. }
  1632. /**
  1633. * testInput method
  1634. *
  1635. * Test various incarnations of input().
  1636. *
  1637. * @return void
  1638. */
  1639. public function testInput() {
  1640. $result = $this->Form->input('ValidateUser.balance');
  1641. $expected = array(
  1642. 'div' => array('class'),
  1643. 'label' => array('for'),
  1644. 'Balance',
  1645. '/label',
  1646. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1647. '/div',
  1648. );
  1649. $this->assertTags($result, $expected);
  1650. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1651. $expected = array(
  1652. 'div' => array('class' => 'input text'),
  1653. 'label' => array('for' => 'custom'),
  1654. 'Email',
  1655. '/label',
  1656. array('input' => array(
  1657. 'type' => 'text', 'name' => 'data[Contact][email]',
  1658. 'id' => 'custom', 'maxlength' => 255
  1659. )),
  1660. '/div'
  1661. );
  1662. $this->assertTags($result, $expected);
  1663. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1664. $expected = array(
  1665. '<div',
  1666. 'label' => array('for' => 'ContactEmail'),
  1667. 'Email',
  1668. '/label',
  1669. array('input' => array(
  1670. 'type' => 'text', 'name' => 'data[Contact][email]',
  1671. 'id' => 'ContactEmail', 'maxlength' => 255
  1672. )),
  1673. '/div'
  1674. );
  1675. $this->assertTags($result, $expected);
  1676. $result = $this->Form->hidden('Contact.idontexist');
  1677. $expected = array('input' => array(
  1678. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1679. 'id' => 'ContactIdontexist'
  1680. ));
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1683. $expected = array(
  1684. 'div' => array('class' => 'input text'),
  1685. 'label' => array('for' => 'ContactEmail'),
  1686. 'Email',
  1687. '/label',
  1688. array('input' => array(
  1689. 'type' => 'text', 'name' => 'data[Contact][email]',
  1690. 'id' => 'ContactEmail'
  1691. )),
  1692. '/div'
  1693. );
  1694. $this->assertTags($result, $expected);
  1695. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1696. $expected = array(
  1697. 'div' => array('class' => 'input text'),
  1698. 'label' => array('for' => 'Contact5Email'),
  1699. 'Email',
  1700. '/label',
  1701. array('input' => array(
  1702. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1703. 'id' => 'Contact5Email'
  1704. )),
  1705. '/div'
  1706. );
  1707. $this->assertTags($result, $expected);
  1708. $result = $this->Form->input('Contact.password');
  1709. $expected = array(
  1710. 'div' => array('class' => 'input password'),
  1711. 'label' => array('for' => 'ContactPassword'),
  1712. 'Password',
  1713. '/label',
  1714. array('input' => array(
  1715. 'type' => 'password', 'name' => 'data[Contact][password]',
  1716. 'id' => 'ContactPassword'
  1717. )),
  1718. '/div'
  1719. );
  1720. $this->assertTags($result, $expected);
  1721. $result = $this->Form->input('Contact.email', array(
  1722. 'type' => 'file', 'class' => 'textbox'
  1723. ));
  1724. $expected = array(
  1725. 'div' => array('class' => 'input file'),
  1726. 'label' => array('for' => 'ContactEmail'),
  1727. 'Email',
  1728. '/label',
  1729. array('input' => array(
  1730. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1731. 'id' => 'ContactEmail'
  1732. )),
  1733. '/div'
  1734. );
  1735. $this->assertTags($result, $expected);
  1736. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1737. $result = $this->Form->input('Contact.phone');
  1738. $expected = array(
  1739. 'div' => array('class' => 'input text'),
  1740. 'label' => array('for' => 'ContactPhone'),
  1741. 'Phone',
  1742. '/label',
  1743. array('input' => array(
  1744. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1745. 'value' => 'Hello &amp; World &gt; weird chars',
  1746. 'id' => 'ContactPhone', 'maxlength' => 255
  1747. )),
  1748. '/div'
  1749. );
  1750. $this->assertTags($result, $expected);
  1751. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1752. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1753. $expected = array(
  1754. 'div' => array('class' => 'input text'),
  1755. 'label' => array('for' => 'myId'),
  1756. 'Field',
  1757. '/label',
  1758. 'input' => array(
  1759. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1760. 'value' => 'My value', 'id' => 'myId'
  1761. ),
  1762. '/div'
  1763. );
  1764. $this->assertTags($result, $expected);
  1765. unset($this->Form->request->data);
  1766. $Contact = ClassRegistry::getObject('Contact');
  1767. $Contact->validationErrors['field'] = array('Badness!');
  1768. $result = $this->Form->input('Contact.field');
  1769. $expected = array(
  1770. 'div' => array('class' => 'input text error'),
  1771. 'label' => array('for' => 'ContactField'),
  1772. 'Field',
  1773. '/label',
  1774. 'input' => array(
  1775. 'type' => 'text', 'name' => 'data[Contact][field]',
  1776. 'id' => 'ContactField', 'class' => 'form-error'
  1777. ),
  1778. array('div' => array('class' => 'error-message')),
  1779. 'Badness!',
  1780. '/div',
  1781. '/div'
  1782. );
  1783. $this->assertTags($result, $expected);
  1784. $result = $this->Form->input('Contact.field', array(
  1785. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1786. ));
  1787. $expected = array(
  1788. 'label' => array('for' => 'ContactField'),
  1789. 'Field',
  1790. '/label',
  1791. 'input' => array(
  1792. 'type' => 'text', 'name' => 'data[Contact][field]',
  1793. 'id' => 'ContactField', 'class' => 'form-error'
  1794. ),
  1795. array('span' => array('class' => 'error-message')),
  1796. 'Badness!',
  1797. '/span'
  1798. );
  1799. $this->assertTags($result, $expected);
  1800. $result = $this->Form->input('Contact.field', array(
  1801. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1802. ));
  1803. $expected = array(
  1804. 'div' => array('class' => 'input text error'),
  1805. 'label' => array('for' => 'ContactField'),
  1806. 'Field',
  1807. '/label',
  1808. 'input' => array(
  1809. 'type' => 'text', 'name' => 'data[Contact][field]',
  1810. 'id' => 'ContactField', 'class' => 'form-error'
  1811. ),
  1812. array('div' => array('class' => 'error')),
  1813. 'Badness!',
  1814. '/div'
  1815. );
  1816. $this->assertTags($result, $expected);
  1817. $result = $this->Form->input('Contact.field', array(
  1818. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1819. ));
  1820. $expected = array(
  1821. 'span' => array('class' => 'input text error'),
  1822. 'label' => array('for' => 'ContactField'),
  1823. 'Field',
  1824. '/label',
  1825. 'input' => array(
  1826. 'type' => 'text', 'name' => 'data[Contact][field]',
  1827. 'id' => 'ContactField', 'class' => 'form-error'
  1828. ),
  1829. 'Badness!',
  1830. '/span'
  1831. );
  1832. $this->assertTags($result, $expected);
  1833. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1834. $expected = array(
  1835. 'div' => array('class' => 'input text error'),
  1836. 'label' => array('for' => 'ContactField'),
  1837. 'Field',
  1838. '/label',
  1839. 'input' => array(
  1840. 'type' => 'text', 'name' => 'data[Contact][field]',
  1841. 'id' => 'ContactField', 'class' => 'form-error'
  1842. ),
  1843. 'A message to you, Rudy',
  1844. array('div' => array('class' => 'error-message')),
  1845. 'Badness!',
  1846. '/div',
  1847. '/div'
  1848. );
  1849. $this->assertTags($result, $expected);
  1850. $this->Form->setEntity(null);
  1851. $this->Form->setEntity('Contact.field');
  1852. $result = $this->Form->input('Contact.field', array(
  1853. 'after' => 'A message to you, Rudy', 'error' => false
  1854. ));
  1855. $expected = array(
  1856. 'div' => array('class' => 'input text'),
  1857. 'label' => array('for' => 'ContactField'),
  1858. 'Field',
  1859. '/label',
  1860. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1861. 'A message to you, Rudy',
  1862. '/div'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input text'),
  1868. 'label' => array('for' => 'ObjectField'),
  1869. 'Field',
  1870. '/label',
  1871. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1872. 'A message to you, Rudy',
  1873. '/div'
  1874. );
  1875. $this->assertTags($result, $expected);
  1876. $Contact->validationErrors['field'] = array('minLength');
  1877. $result = $this->Form->input('Contact.field', array(
  1878. 'error' => array(
  1879. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1880. 'maxLength' => 'login too large'
  1881. )
  1882. ));
  1883. $expected = array(
  1884. 'div' => array('class' => 'input text error'),
  1885. 'label' => array('for' => 'ContactField'),
  1886. 'Field',
  1887. '/label',
  1888. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1889. array('div' => array('class' => 'error-message')),
  1890. 'Le login doit contenir au moins 2 caractères',
  1891. '/div',
  1892. '/div'
  1893. );
  1894. $this->assertTags($result, $expected);
  1895. $Contact->validationErrors['field'] = array('maxLength');
  1896. $result = $this->Form->input('Contact.field', array(
  1897. 'error' => array(
  1898. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1899. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1900. 'maxLength' => 'login too large',
  1901. )
  1902. ));
  1903. $expected = array(
  1904. 'div' => array('class' => 'input text error'),
  1905. 'label' => array('for' => 'ContactField'),
  1906. 'Field',
  1907. '/label',
  1908. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1909. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1910. 'login too large',
  1911. '/span',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. }
  1916. /**
  1917. * test input() with checkbox creation
  1918. *
  1919. * @return void
  1920. */
  1921. public function testInputCheckbox() {
  1922. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1923. $expected = array(
  1924. 'div' => array('class' => 'input checkbox'),
  1925. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1926. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1927. '/div'
  1928. );
  1929. $this->assertTags($result, $expected);
  1930. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1931. $expected = array(
  1932. 'div' => array('class' => 'input checkbox'),
  1933. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1934. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1935. '/div'
  1936. );
  1937. $this->assertTags($result, $expected);
  1938. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1939. $expected = array(
  1940. 'div' => array('class' => 'input checkbox'),
  1941. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1942. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1943. '/div'
  1944. );
  1945. $this->assertTags($result, $expected);
  1946. }
  1947. /**
  1948. * test form->input() with time types.
  1949. *
  1950. */
  1951. public function testInputTime() {
  1952. extract($this->dateRegex);
  1953. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1954. $result = explode(':', $result);
  1955. $this->assertRegExp('/option value="23"/', $result[0]);
  1956. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1957. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1958. $result = explode(':', $result);
  1959. $this->assertRegExp('/option value="23"/', $result[0]);
  1960. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1961. $result = $this->Form->input('Model.field', array(
  1962. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1963. ));
  1964. $result = explode(':', $result);
  1965. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1966. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1967. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1968. $result = $this->Form->input('Model.field', array(
  1969. 'type' => 'time', 'timeFormat' => 12, '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('prueba', array(
  1976. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1977. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1978. ));
  1979. $result = explode(':', $result);
  1980. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1981. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1982. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1983. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1984. $result = $this->Form->input('Random.start_time', array(
  1985. 'type' => 'time',
  1986. 'selected' => '18:15'
  1987. ));
  1988. $this->assertRegExp('#<option value="06"[^>]*>6</option>#', $result);
  1989. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result);
  1990. $this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
  1991. }
  1992. /**
  1993. * test form->input() with datetime, date and time types
  1994. *
  1995. * @return void
  1996. */
  1997. public function testInputDatetime() {
  1998. extract($this->dateRegex);
  1999. $result = $this->Form->input('prueba', array(
  2000. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2001. 'maxYear' => date('Y') + 1, 'interval' => 15
  2002. ));
  2003. $result = explode(':', $result);
  2004. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2005. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2006. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2007. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2008. //related to ticket #5013
  2009. $result = $this->Form->input('Contact.date', array(
  2010. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2011. ));
  2012. $this->assertRegExp('/class="customClass"/', $result);
  2013. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2014. $result = $this->Form->input('Contact.date', array(
  2015. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2016. ));
  2017. $this->assertRegExp('/id="customIdDay"/', $result);
  2018. $this->assertRegExp('/id="customIdMonth"/', $result);
  2019. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2020. $result = $this->Form->input('Model.field', array(
  2021. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2022. ));
  2023. $this->assertRegExp('/id="customIDDay"/', $result);
  2024. $this->assertRegExp('/id="customIDHour"/', $result);
  2025. $result = explode('</select><select', $result);
  2026. $result = explode(':', $result[1]);
  2027. $this->assertRegExp('/option value="23"/', $result[0]);
  2028. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2029. $result = $this->Form->input('Model.field', array(
  2030. 'type' => 'datetime', 'timeFormat' => 12
  2031. ));
  2032. $result = explode('</select><select', $result);
  2033. $result = explode(':', $result[1]);
  2034. $this->assertRegExp('/option value="12"/', $result[0]);
  2035. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2036. $this->Form->request->data = array('Contact' => array('created' => null));
  2037. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2038. $expected = array(
  2039. 'div' => array('class' => 'input date'),
  2040. 'label' => array('for' => 'ContactCreatedMonth'),
  2041. 'Created',
  2042. '/label',
  2043. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2044. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2045. $monthsRegex,
  2046. '/select', '-',
  2047. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2048. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2049. $daysRegex,
  2050. '/select', '-',
  2051. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2052. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2053. $yearsRegex,
  2054. '/select',
  2055. '/div'
  2056. );
  2057. $this->assertTags($result, $expected);
  2058. $this->Form->request->data = array('Contact' => array('created' => null));
  2059. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2060. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2061. $this->Form->request->data = array('Contact' => array('created' => null));
  2062. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2063. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2064. $result = $this->Form->input('Contact.created', array(
  2065. 'type' => 'date',
  2066. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2067. 'timeFormat' => 'NONE'
  2068. ));
  2069. $this->assertRegExp('/for\="created-month"/', $result);
  2070. }
  2071. /**
  2072. * Test generating checkboxes in a loop.
  2073. *
  2074. * @return void
  2075. */
  2076. public function testInputCheckboxesInLoop() {
  2077. for ($i = 1; $i < 5; $i++) {
  2078. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2079. $expected = array(
  2080. 'div' => array('class' => 'input checkbox'),
  2081. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2082. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2083. 'label' => array('for' => "Contact{$i}Email"),
  2084. 'Email',
  2085. '/label',
  2086. '/div'
  2087. );
  2088. $this->assertTags($result, $expected);
  2089. }
  2090. }
  2091. /**
  2092. * test input name with leading integer, ensure attributes are generated correctly.
  2093. *
  2094. * @return void
  2095. */
  2096. public function testInputWithLeadingInteger() {
  2097. $result = $this->Form->text('0.Node.title');
  2098. $expected = array(
  2099. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2100. );
  2101. $this->assertTags($result, $expected);
  2102. }
  2103. /**
  2104. * test form->input() with select type inputs.
  2105. *
  2106. * @return void
  2107. */
  2108. public function testInputSelectType() {
  2109. $result = $this->Form->input('email', array(
  2110. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2111. );
  2112. $expected = array(
  2113. 'div' => array('class' => 'input select'),
  2114. 'label' => array('for' => 'email'),
  2115. 'Email',
  2116. '/label',
  2117. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2118. array('option' => array('value' => '')),
  2119. '/option',
  2120. array('option' => array('value' => 'è')),
  2121. 'Firést',
  2122. '/option',
  2123. array('option' => array('value' => 'é')),
  2124. 'Secoènd',
  2125. '/option',
  2126. '/select',
  2127. '/div'
  2128. );
  2129. $this->assertTags($result, $expected);
  2130. $result = $this->Form->input('email', array(
  2131. 'options' => array('First', 'Second'), 'empty' => true)
  2132. );
  2133. $expected = array(
  2134. 'div' => array('class' => 'input select'),
  2135. 'label' => array('for' => 'email'),
  2136. 'Email',
  2137. '/label',
  2138. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2139. array('option' => array('value' => '')),
  2140. '/option',
  2141. array('option' => array('value' => '0')),
  2142. 'First',
  2143. '/option',
  2144. array('option' => array('value' => '1')),
  2145. 'Second',
  2146. '/option',
  2147. '/select',
  2148. '/div'
  2149. );
  2150. $this->assertTags($result, $expected);
  2151. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2152. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2153. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2154. $expected = array(
  2155. 'div' => array('class' => 'input select'),
  2156. 'label' => array('for' => 'ModelUserId'),
  2157. 'User',
  2158. '/label',
  2159. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2160. array('option' => array('value' => '')),
  2161. '/option',
  2162. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2163. 'good',
  2164. '/option',
  2165. array('option' => array('value' => 'other')),
  2166. 'bad',
  2167. '/option',
  2168. '/select',
  2169. '/div'
  2170. );
  2171. $this->assertTags($result, $expected);
  2172. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2173. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2174. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2175. $expected = array(
  2176. 'div' => array('class' => 'input select'),
  2177. 'label' => array('for' => 'ThingUserId'),
  2178. 'User',
  2179. '/label',
  2180. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2181. array('option' => array('value' => '')),
  2182. 'Some Empty',
  2183. '/option',
  2184. array('option' => array('value' => 'value')),
  2185. 'good',
  2186. '/option',
  2187. array('option' => array('value' => 'other')),
  2188. 'bad',
  2189. '/option',
  2190. '/select',
  2191. '/div'
  2192. );
  2193. $this->assertTags($result, $expected);
  2194. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2195. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2196. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2197. $expected = array(
  2198. 'div' => array('class' => 'input select'),
  2199. 'label' => array('for' => 'ThingUserId'),
  2200. 'User',
  2201. '/label',
  2202. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2203. array('option' => array('value' => '')),
  2204. 'Some Empty',
  2205. '/option',
  2206. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2207. 'good',
  2208. '/option',
  2209. array('option' => array('value' => 'other')),
  2210. 'bad',
  2211. '/option',
  2212. '/select',
  2213. '/div'
  2214. );
  2215. $this->assertTags($result, $expected);
  2216. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2217. $this->Form->request->data = array('User' => array('User' => array('value')));
  2218. $result = $this->Form->input('User.User', array('empty' => true));
  2219. $expected = array(
  2220. 'div' => array('class' => 'input select'),
  2221. 'label' => array('for' => 'UserUser'),
  2222. 'User',
  2223. '/label',
  2224. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2225. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2226. array('option' => array('value' => '')),
  2227. '/option',
  2228. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2229. 'good',
  2230. '/option',
  2231. array('option' => array('value' => 'other')),
  2232. 'bad',
  2233. '/option',
  2234. '/select',
  2235. '/div'
  2236. );
  2237. $this->assertTags($result, $expected);
  2238. $this->Form->data = array();
  2239. $result = $this->Form->input('Publisher.id', array(
  2240. 'label' => 'Publisher',
  2241. 'type' => 'select',
  2242. 'multiple' => 'checkbox',
  2243. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2244. ));
  2245. $expected = array(
  2246. array('div' => array('class' => 'input select')),
  2247. array('label' => array('for' => 'PublisherId')),
  2248. 'Publisher',
  2249. '/label',
  2250. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2251. array('div' => array('class' => 'checkbox')),
  2252. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2253. array('label' => array('for' => 'PublisherIdValue1')),
  2254. 'Label 1',
  2255. '/label',
  2256. '/div',
  2257. array('div' => array('class' => 'checkbox')),
  2258. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2259. array('label' => array('for' => 'PublisherIdValue2')),
  2260. 'Label 2',
  2261. '/label',
  2262. '/div',
  2263. '/div'
  2264. );
  2265. $this->assertTags($result, $expected);
  2266. }
  2267. /**
  2268. * test that input() and a non standard primary key makes a hidden input by default.
  2269. *
  2270. * @return void
  2271. */
  2272. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2273. $this->Form->create('User');
  2274. $this->Form->fieldset = array(
  2275. 'User' => array(
  2276. 'fields' => array(
  2277. 'model_id' => array('type' => 'integer')
  2278. ),
  2279. 'validates' => array(),
  2280. 'key' => 'model_id'
  2281. )
  2282. );
  2283. $result = $this->Form->input('model_id');
  2284. $expected = array(
  2285. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2286. );
  2287. $this->assertTags($result, $expected);
  2288. }
  2289. /**
  2290. * test that overriding the magic select type widget is possible
  2291. *
  2292. * @return void
  2293. */
  2294. public function testInputOverridingMagicSelectType() {
  2295. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2296. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2297. $expected = array(
  2298. 'div' => array('class' => 'input text'),
  2299. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2300. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2301. '/div'
  2302. );
  2303. $this->assertTags($result, $expected);
  2304. //Check that magic types still work for plural/singular vars
  2305. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2306. $result = $this->Form->input('Model.type');
  2307. $expected = array(
  2308. 'div' => array('class' => 'input select'),
  2309. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2310. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2311. array('option' => array('value' => 'value')), 'good', '/option',
  2312. array('option' => array('value' => 'other')), 'bad', '/option',
  2313. '/select',
  2314. '/div'
  2315. );
  2316. $this->assertTags($result, $expected);
  2317. }
  2318. /**
  2319. * Test that magic input() selects can easily be converted into radio types without error.
  2320. *
  2321. * @return void
  2322. */
  2323. public function testInputMagicSelectChangeToRadio() {
  2324. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2325. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2326. $this->assertRegExp('/input type="radio"/', $result);
  2327. }
  2328. /**
  2329. * fields with the same name as the model should work.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testInputWithMatchingFieldAndModelName() {
  2334. $this->Form->create('User');
  2335. $this->Form->fieldset = array(
  2336. 'User' => array(
  2337. 'fields' => array(
  2338. 'User' => array('type' => 'text')
  2339. ),
  2340. 'validates' => array(),
  2341. 'key' => 'id'
  2342. )
  2343. );
  2344. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2345. $result = $this->Form->input('User', array('type' => 'text'));
  2346. $expected = array(
  2347. 'div' => array('class' => 'input text'),
  2348. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2349. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2350. '/div'
  2351. );
  2352. $this->assertTags($result, $expected);
  2353. }
  2354. /**
  2355. * testFormInputs method
  2356. *
  2357. * test correct results from form::inputs().
  2358. *
  2359. * @return void
  2360. */
  2361. public function testFormInputs() {
  2362. $this->Form->create('Contact');
  2363. $result = $this->Form->inputs('The Legend');
  2364. $expected = array(
  2365. '<fieldset',
  2366. '<legend',
  2367. 'The Legend',
  2368. '/legend',
  2369. '*/fieldset',
  2370. );
  2371. $this->assertTags($result, $expected);
  2372. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2373. $expected = array(
  2374. 'fieldset' => array('class' => 'classy-stuff'),
  2375. '<legend',
  2376. 'Field of Dreams',
  2377. '/legend',
  2378. '*/fieldset'
  2379. );
  2380. $this->assertTags($result, $expected);
  2381. $this->Form->create('Contact');
  2382. $this->Form->request['prefix'] = 'admin';
  2383. $this->Form->request['action'] = 'admin_edit';
  2384. $result = $this->Form->inputs();
  2385. $expected = array(
  2386. '<fieldset',
  2387. '<legend',
  2388. 'Edit Contact',
  2389. '/legend',
  2390. '*/fieldset',
  2391. );
  2392. $this->assertTags($result, $expected);
  2393. $this->Form->create('Contact');
  2394. $result = $this->Form->inputs(false);
  2395. $expected = array(
  2396. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2397. array('div' => array('class' => 'input text')),
  2398. '*/div',
  2399. array('div' => array('class' => 'input text')),
  2400. '*/div',
  2401. array('div' => array('class' => 'input text')),
  2402. '*/div',
  2403. array('div' => array('class' => 'input password')),
  2404. '*/div',
  2405. array('div' => array('class' => 'input date')),
  2406. '*/div',
  2407. array('div' => array('class' => 'input date')),
  2408. '*/div',
  2409. array('div' => array('class' => 'input datetime')),
  2410. '*/div',
  2411. array('div' => array('class' => 'input number')),
  2412. '*/div',
  2413. array('div' => array('class' => 'input select')),
  2414. '*/div',
  2415. );
  2416. $this->assertTags($result, $expected);
  2417. $this->Form->create('Contact');
  2418. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2419. $expected = array(
  2420. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2421. array('div' => array('class' => 'input text')),
  2422. '*/div',
  2423. array('div' => array('class' => 'input text')),
  2424. '*/div',
  2425. array('div' => array('class' => 'input text')),
  2426. '*/div',
  2427. array('div' => array('class' => 'input password')),
  2428. '*/div',
  2429. array('div' => array('class' => 'input date')),
  2430. '*/div',
  2431. array('div' => array('class' => 'input date')),
  2432. '*/div',
  2433. array('div' => array('class' => 'input datetime')),
  2434. '*/div',
  2435. array('div' => array('class' => 'input number')),
  2436. '*/div',
  2437. array('div' => array('class' => 'input select')),
  2438. '*/div',
  2439. );
  2440. $this->assertTags($result, $expected);
  2441. $this->Form->create('Contact');
  2442. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2443. $expected = array(
  2444. 'fieldset' => array(),
  2445. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2446. array('div' => array('class' => 'input text')),
  2447. '*/div',
  2448. array('div' => array('class' => 'input text')),
  2449. '*/div',
  2450. array('div' => array('class' => 'input text')),
  2451. '*/div',
  2452. array('div' => array('class' => 'input password')),
  2453. '*/div',
  2454. array('div' => array('class' => 'input date')),
  2455. '*/div',
  2456. array('div' => array('class' => 'input date')),
  2457. '*/div',
  2458. array('div' => array('class' => 'input datetime')),
  2459. '*/div',
  2460. array('div' => array('class' => 'input number')),
  2461. '*/div',
  2462. array('div' => array('class' => 'input select')),
  2463. '*/div',
  2464. '/fieldset'
  2465. );
  2466. $this->assertTags($result, $expected);
  2467. $this->Form->create('Contact');
  2468. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2469. $expected = array(
  2470. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2471. array('div' => array('class' => 'input text')),
  2472. '*/div',
  2473. array('div' => array('class' => 'input text')),
  2474. '*/div',
  2475. array('div' => array('class' => 'input text')),
  2476. '*/div',
  2477. array('div' => array('class' => 'input password')),
  2478. '*/div',
  2479. array('div' => array('class' => 'input date')),
  2480. '*/div',
  2481. array('div' => array('class' => 'input date')),
  2482. '*/div',
  2483. array('div' => array('class' => 'input datetime')),
  2484. '*/div',
  2485. array('div' => array('class' => 'input number')),
  2486. '*/div',
  2487. array('div' => array('class' => 'input select')),
  2488. '*/div',
  2489. );
  2490. $this->assertTags($result, $expected);
  2491. $this->Form->create('Contact');
  2492. $result = $this->Form->inputs('Hello');
  2493. $expected = array(
  2494. 'fieldset' => array(),
  2495. 'legend' => array(),
  2496. 'Hello',
  2497. '/legend',
  2498. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2499. array('div' => array('class' => 'input text')),
  2500. '*/div',
  2501. array('div' => array('class' => 'input text')),
  2502. '*/div',
  2503. array('div' => array('class' => 'input text')),
  2504. '*/div',
  2505. array('div' => array('class' => 'input password')),
  2506. '*/div',
  2507. array('div' => array('class' => 'input date')),
  2508. '*/div',
  2509. array('div' => array('class' => 'input date')),
  2510. '*/div',
  2511. array('div' => array('class' => 'input datetime')),
  2512. '*/div',
  2513. array('div' => array('class' => 'input number')),
  2514. '*/div',
  2515. array('div' => array('class' => 'input select')),
  2516. '*/div',
  2517. '/fieldset'
  2518. );
  2519. $this->assertTags($result, $expected);
  2520. $this->Form->create('Contact');
  2521. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2522. $expected = array(
  2523. 'fieldset' => array(),
  2524. 'legend' => array(),
  2525. 'Hello',
  2526. '/legend',
  2527. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2528. array('div' => array('class' => 'input text')),
  2529. '*/div',
  2530. array('div' => array('class' => 'input text')),
  2531. '*/div',
  2532. array('div' => array('class' => 'input text')),
  2533. '*/div',
  2534. array('div' => array('class' => 'input password')),
  2535. '*/div',
  2536. array('div' => array('class' => 'input date')),
  2537. '*/div',
  2538. array('div' => array('class' => 'input date')),
  2539. '*/div',
  2540. array('div' => array('class' => 'input datetime')),
  2541. '*/div',
  2542. array('div' => array('class' => 'input number')),
  2543. '*/div',
  2544. array('div' => array('class' => 'input select')),
  2545. '*/div',
  2546. '/fieldset'
  2547. );
  2548. $this->assertTags($result, $expected);
  2549. }
  2550. /**
  2551. * testSelectAsCheckbox method
  2552. *
  2553. * test multi-select widget with checkbox formatting.
  2554. *
  2555. * @return void
  2556. */
  2557. public function testSelectAsCheckbox() {
  2558. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2559. $expected = array(
  2560. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2561. array('div' => array('class' => 'checkbox')),
  2562. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2563. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2564. 'first',
  2565. '/label',
  2566. '/div',
  2567. array('div' => array('class' => 'checkbox')),
  2568. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2569. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2570. 'second',
  2571. '/label',
  2572. '/div',
  2573. array('div' => array('class' => 'checkbox')),
  2574. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2575. array('label' => array('for' => 'ModelMultiField2')),
  2576. 'third',
  2577. '/label',
  2578. '/div',
  2579. );
  2580. $this->assertTags($result, $expected);
  2581. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2582. $expected = array(
  2583. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2584. array('div' => array('class' => 'checkbox')),
  2585. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2586. array('label' => array('for' => 'ModelMultiField12')),
  2587. 'half',
  2588. '/label',
  2589. '/div',
  2590. );
  2591. $this->assertTags($result, $expected);
  2592. }
  2593. /**
  2594. * testLabel method
  2595. *
  2596. * test label generation.
  2597. *
  2598. * @return void
  2599. */
  2600. public function testLabel() {
  2601. $this->Form->text('Person.name');
  2602. $result = $this->Form->label();
  2603. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2604. $this->Form->text('Person.name');
  2605. $result = $this->Form->label();
  2606. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2607. $result = $this->Form->label('Person.first_name');
  2608. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2609. $result = $this->Form->label('Person.first_name', 'Your first name');
  2610. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2611. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2612. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2613. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2614. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2615. $result = $this->Form->label('Person.first_name', '');
  2616. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2617. $result = $this->Form->label('Person.2.name', '');
  2618. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2619. }
  2620. /**
  2621. * testTextbox method
  2622. *
  2623. * test textbox element generation
  2624. *
  2625. * @return void
  2626. */
  2627. public function testTextbox() {
  2628. $result = $this->Form->text('Model.field');
  2629. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2630. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2631. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2632. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2633. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2634. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2635. $result = $this->Form->text('Model.text');
  2636. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2637. $Contact = ClassRegistry::getObject('Contact');
  2638. $Contact->validationErrors['text'] = array(true);
  2639. $this->Form->request->data['Contact']['text'] = 'test';
  2640. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2641. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2642. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2643. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2644. $expected = array(
  2645. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2646. );
  2647. $this->assertTags($result, $expected);
  2648. }
  2649. /**
  2650. * testDefaultValue method
  2651. *
  2652. * Test default value setting
  2653. *
  2654. * @return void
  2655. */
  2656. public function testDefaultValue() {
  2657. $this->Form->request->data['Model']['field'] = 'test';
  2658. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2659. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2660. unset($this->Form->request->data['Model']['field']);
  2661. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2662. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2663. }
  2664. /**
  2665. * testCheckboxDefaultValue method
  2666. *
  2667. * Test default value setting on checkbox() method
  2668. *
  2669. * @return void
  2670. */
  2671. public function testCheckboxDefaultValue() {
  2672. $this->Form->request->data['Model']['field'] = false;
  2673. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2674. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2675. unset($this->Form->request->data['Model']['field']);
  2676. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2677. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2678. $this->Form->request->data['Model']['field'] = true;
  2679. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2680. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2681. unset($this->Form->request->data['Model']['field']);
  2682. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2683. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2684. }
  2685. /**
  2686. * testError method
  2687. *
  2688. * Test field error generation
  2689. *
  2690. * @return void
  2691. */
  2692. public function testError() {
  2693. $Contact = ClassRegistry::getObject('Contact');
  2694. $Contact->validationErrors['field'] = array(1);
  2695. $result = $this->Form->error('Contact.field');
  2696. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2697. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2698. $this->assertEquals('Error in field Field', $result);
  2699. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2700. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2701. $this->assertEquals('This field contains invalid input', $result);
  2702. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2703. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2704. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2705. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2706. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2707. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2708. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2709. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2710. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2711. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2712. $this->assertEquals('<strong>Badness!</strong>', $result);
  2713. $Contact->validationErrors['field'] = array("email");
  2714. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2715. $expected = array(
  2716. 'div' => array('class' => 'field-error'),
  2717. 'No good!',
  2718. '/div'
  2719. );
  2720. $this->assertTags($result, $expected);
  2721. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2722. $result = $this->Form->error('Contact.field', array(
  2723. 'notEmpty' => 'Cannot be empty',
  2724. 'email' => 'No good!'
  2725. ));
  2726. $expected = array(
  2727. 'div' => array('class' => 'error-message'),
  2728. 'ul' => array(),
  2729. '<li', 'Cannot be empty', '/li',
  2730. '<li', 'No good!', '/li',
  2731. '<li', 'Something else', '/li',
  2732. '/ul',
  2733. '/div'
  2734. );
  2735. $this->assertTags($result, $expected);
  2736. /** Testing error messages list options **/
  2737. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2738. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2739. $expected = array(
  2740. 'div' => array('class' => 'error-message'),
  2741. 'ol' => array(),
  2742. '<li', 'notEmpty', '/li',
  2743. '<li', 'email', '/li',
  2744. '/ol',
  2745. '/div'
  2746. );
  2747. $this->assertTags($result, $expected);
  2748. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2749. $expected = array(
  2750. 'div' => array('class' => 'error-message'),
  2751. 'ol' => array(),
  2752. '<li', 'notEmpty', '/li',
  2753. '<li', 'email', '/li',
  2754. '/ol',
  2755. '/div'
  2756. );
  2757. $this->assertTags($result, $expected);
  2758. $result = $this->Form->error('Contact.field', null, array(
  2759. 'listOptions' => array(
  2760. 'class' => 'ul-class',
  2761. 'itemOptions' => array(
  2762. 'class' => 'li-class'
  2763. )
  2764. )
  2765. ));
  2766. $expected = array(
  2767. 'div' => array('class' => 'error-message'),
  2768. 'ul' => array('class' => 'ul-class'),
  2769. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2770. array('li' => array('class' => 'li-class')), 'email', '/li',
  2771. '/ul',
  2772. '/div'
  2773. );
  2774. $this->assertTags($result, $expected);
  2775. }
  2776. /**
  2777. * test error options when using form->input();
  2778. *
  2779. * @return void
  2780. */
  2781. public function testInputErrorEscape() {
  2782. $this->Form->create('ValidateProfile');
  2783. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2784. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2785. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2786. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2787. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2788. $this->assertRegExp('/required<br>/', $result);
  2789. }
  2790. /**
  2791. * testPassword method
  2792. *
  2793. * Test password element generation
  2794. *
  2795. * @return void
  2796. */
  2797. public function testPassword() {
  2798. $Contact = ClassRegistry::getObject('Contact');
  2799. $result = $this->Form->password('Contact.field');
  2800. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2801. $Contact->validationErrors['passwd'] = 1;
  2802. $this->Form->request->data['Contact']['passwd'] = 'test';
  2803. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2804. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2805. }
  2806. /**
  2807. * testRadio method
  2808. *
  2809. * Test radio element set generation
  2810. *
  2811. * @return void
  2812. */
  2813. public function testRadio() {
  2814. $result = $this->Form->radio('Model.field', array('option A'));
  2815. $expected = array(
  2816. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2817. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2818. 'label' => array('for' => 'ModelField0'),
  2819. 'option A',
  2820. '/label'
  2821. );
  2822. $this->assertTags($result, $expected);
  2823. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2824. $expected = array(
  2825. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2826. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2827. 'label' => array('for' => 'ModelField12'),
  2828. 'half',
  2829. '/label'
  2830. );
  2831. $this->assertTags($result, $expected);
  2832. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2833. $expected = array(
  2834. 'fieldset' => array(),
  2835. 'legend' => array(),
  2836. 'Field',
  2837. '/legend',
  2838. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2839. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2840. array('label' => array('for' => 'ModelField0')),
  2841. 'option A',
  2842. '/label',
  2843. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2844. array('label' => array('for' => 'ModelField1')),
  2845. 'option B',
  2846. '/label',
  2847. '/fieldset'
  2848. );
  2849. $this->assertTags($result, $expected);
  2850. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2851. $expected = array(
  2852. 'fieldset' => array(),
  2853. 'legend' => array(),
  2854. 'Field',
  2855. '/legend',
  2856. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2857. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2858. array('label' => array('for' => 'ModelField0')),
  2859. 'option A',
  2860. '/label',
  2861. 'br' => array(),
  2862. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2863. array('label' => array('for' => 'ModelField1')),
  2864. 'option B',
  2865. '/label',
  2866. '/fieldset'
  2867. );
  2868. $this->assertTags($result, $expected);
  2869. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  2870. $expected = array(
  2871. 'fieldset' => array(),
  2872. 'legend' => array(),
  2873. 'Field',
  2874. '/legend',
  2875. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
  2876. array('label' => array('for' => 'ModelField1')),
  2877. 'Yes',
  2878. '/label',
  2879. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2880. array('label' => array('for' => 'ModelField0')),
  2881. 'No',
  2882. '/label',
  2883. '/fieldset'
  2884. );
  2885. $this->assertTags($result, $expected);
  2886. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  2887. $expected = array(
  2888. 'fieldset' => array(),
  2889. 'legend' => array(),
  2890. 'Field',
  2891. '/legend',
  2892. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2893. array('label' => array('for' => 'ModelField1')),
  2894. 'Yes',
  2895. '/label',
  2896. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  2897. array('label' => array('for' => 'ModelField0')),
  2898. 'No',
  2899. '/label',
  2900. '/fieldset'
  2901. );
  2902. $this->assertTags($result, $expected);
  2903. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  2904. $expected = array(
  2905. 'fieldset' => array(),
  2906. 'legend' => array(),
  2907. 'Field',
  2908. '/legend',
  2909. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2910. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2911. array('label' => array('for' => 'ModelField1')),
  2912. 'Yes',
  2913. '/label',
  2914. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2915. array('label' => array('for' => 'ModelField0')),
  2916. 'No',
  2917. '/label',
  2918. '/fieldset'
  2919. );
  2920. $this->assertTags($result, $expected);
  2921. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  2922. $expected = array(
  2923. 'fieldset' => array(),
  2924. 'legend' => array(),
  2925. 'Field',
  2926. '/legend',
  2927. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2928. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2929. array('label' => array('for' => 'ModelField1')),
  2930. 'Yes',
  2931. '/label',
  2932. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2933. array('label' => array('for' => 'ModelField0')),
  2934. 'No',
  2935. '/label',
  2936. '/fieldset'
  2937. );
  2938. $this->assertTags($result, $expected);
  2939. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2940. $expected = array(
  2941. 'div' => array('class' => 'input radio'),
  2942. 'fieldset' => array(),
  2943. 'legend' => array(),
  2944. 'Legend title',
  2945. '/legend',
  2946. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2947. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2948. array('label' => array('for' => 'NewsletterSubscribe0')),
  2949. 'Unsubscribe',
  2950. '/label',
  2951. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2952. array('label' => array('for' => 'NewsletterSubscribe1')),
  2953. 'Subscribe',
  2954. '/label',
  2955. '/fieldset',
  2956. '/div'
  2957. );
  2958. $this->assertTags($result, $expected);
  2959. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2960. $expected = array(
  2961. 'div' => array('class' => 'input radio'),
  2962. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2963. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2964. array('label' => array('for' => 'NewsletterSubscribe0')),
  2965. 'Unsubscribe',
  2966. '/label',
  2967. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2968. array('label' => array('for' => 'NewsletterSubscribe1')),
  2969. 'Subscribe',
  2970. '/label',
  2971. '/div'
  2972. );
  2973. $this->assertTags($result, $expected);
  2974. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2975. $expected = array(
  2976. 'div' => array('class' => 'input radio'),
  2977. 'fieldset' => array(),
  2978. 'legend' => array(),
  2979. 'Legend title',
  2980. '/legend',
  2981. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2982. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2983. 'Unsubscribe',
  2984. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2985. 'Subscribe',
  2986. '/fieldset',
  2987. '/div'
  2988. );
  2989. $this->assertTags($result, $expected);
  2990. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2991. $expected = array(
  2992. 'div' => array('class' => 'input radio'),
  2993. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2994. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2995. 'Unsubscribe',
  2996. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2997. 'Subscribe',
  2998. '/div'
  2999. );
  3000. $this->assertTags($result, $expected);
  3001. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3002. $expected = array(
  3003. 'div' => array('class' => 'input radio'),
  3004. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3005. 'Unsubscribe',
  3006. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3007. 'Subscribe',
  3008. '/div'
  3009. );
  3010. $this->assertTags($result, $expected);
  3011. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3012. $expected = array(
  3013. 'fieldset' => array(),
  3014. 'legend' => array(),
  3015. 'Gender',
  3016. '/legend',
  3017. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3018. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3019. array('label' => array('for' => 'EmployeeGenderMale')),
  3020. 'Male',
  3021. '/label',
  3022. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3023. array('label' => array('for' => 'EmployeeGenderFemale')),
  3024. 'Female',
  3025. '/label',
  3026. '/fieldset',
  3027. );
  3028. $this->assertTags($result, $expected);
  3029. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3030. $expected = array(
  3031. 'fieldset' => array(),
  3032. 'legend' => array(),
  3033. 'Gender',
  3034. '/legend',
  3035. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3036. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3037. array('label' => array('for' => 'OfficerGenderMale')),
  3038. 'Male',
  3039. '/label',
  3040. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3041. array('label' => array('for' => 'OfficerGenderFemale')),
  3042. 'Female',
  3043. '/label',
  3044. '/fieldset',
  3045. );
  3046. $this->assertTags($result, $expected);
  3047. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3048. $expected = array(
  3049. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3050. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3051. 'label' => array('for' => 'Contact1Imrequired0'),
  3052. 'option A',
  3053. '/label'
  3054. );
  3055. $this->assertTags($result, $expected);
  3056. $result = $this->Form->radio('Model.1.field', array('option A'));
  3057. $expected = array(
  3058. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3059. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3060. 'label' => array('for' => 'Model1Field0'),
  3061. 'option A',
  3062. '/label'
  3063. );
  3064. $this->assertTags($result, $expected);
  3065. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3066. $expected = array(
  3067. 'fieldset' => array(),
  3068. 'legend' => array(),
  3069. 'Field',
  3070. '/legend',
  3071. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3072. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3073. array('label' => array('for' => 'ModelField0')),
  3074. 'option A',
  3075. '/label',
  3076. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3077. array('label' => array('for' => 'ModelField1')),
  3078. 'option B',
  3079. '/label',
  3080. '/fieldset'
  3081. );
  3082. $this->assertTags($result, $expected);
  3083. $result = $this->Form->radio(
  3084. 'Model.field',
  3085. array('option A', 'option B'),
  3086. array('between' => 'I am between')
  3087. );
  3088. $expected = array(
  3089. 'fieldset' => array(),
  3090. 'legend' => array(),
  3091. 'Field',
  3092. '/legend',
  3093. 'I am between',
  3094. 'input' => array(
  3095. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3096. 'value' => '', 'id' => 'ModelField_'
  3097. ),
  3098. array('input' => array(
  3099. 'type' => 'radio', 'name' => 'data[Model][field]',
  3100. 'value' => '0', 'id' => 'ModelField0'
  3101. )),
  3102. array('label' => array('for' => 'ModelField0')),
  3103. 'option A',
  3104. '/label',
  3105. array('input' => array(
  3106. 'type' => 'radio', 'name' => 'data[Model][field]',
  3107. 'value' => '1', 'id' => 'ModelField1'
  3108. )),
  3109. array('label' => array('for' => 'ModelField1')),
  3110. 'option B',
  3111. '/label',
  3112. '/fieldset'
  3113. );
  3114. $this->assertTags($result, $expected);
  3115. }
  3116. /**
  3117. * test disabled radio options
  3118. *
  3119. * @return void
  3120. */
  3121. public function testRadioDisabled() {
  3122. $result = $this->Form->radio(
  3123. 'Model.field',
  3124. array('option A', 'option B'),
  3125. array('disabled' => array('option A'), 'value' => 'option A')
  3126. );
  3127. $expected = array(
  3128. 'fieldset' => array(),
  3129. 'legend' => array(),
  3130. 'Field',
  3131. '/legend',
  3132. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3133. array('label' => array('for' => 'ModelField0')),
  3134. 'option A',
  3135. '/label',
  3136. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3137. array('label' => array('for' => 'ModelField1')),
  3138. 'option B',
  3139. '/label',
  3140. '/fieldset'
  3141. );
  3142. $this->assertTags($result, $expected);
  3143. $result = $this->Form->radio(
  3144. 'Model.field',
  3145. array('option A', 'option B'),
  3146. array('disabled' => true, 'value' => 'option A')
  3147. );
  3148. $expected = array(
  3149. 'fieldset' => array(),
  3150. 'legend' => array(),
  3151. 'Field',
  3152. '/legend',
  3153. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3154. array('label' => array('for' => 'ModelField0')),
  3155. 'option A',
  3156. '/label',
  3157. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3158. array('label' => array('for' => 'ModelField1')),
  3159. 'option B',
  3160. '/label',
  3161. '/fieldset'
  3162. );
  3163. $this->assertTags($result, $expected);
  3164. $result = $this->Form->radio(
  3165. 'Model.field',
  3166. array('option A', 'option B'),
  3167. array('disabled' => 'disabled', 'value' => 'option A')
  3168. );
  3169. $expected = array(
  3170. 'fieldset' => array(),
  3171. 'legend' => array(),
  3172. 'Field',
  3173. '/legend',
  3174. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3175. array('label' => array('for' => 'ModelField0')),
  3176. 'option A',
  3177. '/label',
  3178. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3179. array('label' => array('for' => 'ModelField1')),
  3180. 'option B',
  3181. '/label',
  3182. '/fieldset'
  3183. );
  3184. $this->assertTags($result, $expected);
  3185. }
  3186. /**
  3187. * test disabling the hidden input for radio buttons
  3188. *
  3189. * @return void
  3190. */
  3191. public function testRadioHiddenInputDisabling() {
  3192. $result = $this->Form->input('Model.1.field', array(
  3193. 'type' => 'radio',
  3194. 'options' => array('option A'),
  3195. 'hiddenField' => false
  3196. )
  3197. );
  3198. $expected = array(
  3199. 'div' => array('class' => 'input radio'),
  3200. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3201. 'label' => array('for' => 'Model1Field0'),
  3202. 'option A',
  3203. '/label',
  3204. '/div'
  3205. );
  3206. $this->assertTags($result, $expected);
  3207. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3208. $expected = array(
  3209. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3210. 'label' => array('for' => 'Model1Field0'),
  3211. 'option A',
  3212. '/label'
  3213. );
  3214. $this->assertTags($result, $expected);
  3215. }
  3216. /**
  3217. * testSelect method
  3218. *
  3219. * Test select element generation.
  3220. *
  3221. * @return void
  3222. */
  3223. public function testSelect() {
  3224. $result = $this->Form->select('Model.field', array());
  3225. $expected = array(
  3226. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3227. array('option' => array('value' => '')),
  3228. '/option',
  3229. '/select'
  3230. );
  3231. $this->assertTags($result, $expected);
  3232. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3233. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3234. $expected = array(
  3235. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3236. array('option' => array('value' => '')),
  3237. '/option',
  3238. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3239. 'good',
  3240. '/option',
  3241. array('option' => array('value' => 'other')),
  3242. 'bad',
  3243. '/option',
  3244. '/select'
  3245. );
  3246. $this->assertTags($result, $expected);
  3247. $this->Form->request->data = array();
  3248. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3249. $expected = array(
  3250. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3251. array('option' => array('value' => '')),
  3252. '/option',
  3253. array('option' => array('value' => 'value')),
  3254. 'good',
  3255. '/option',
  3256. array('option' => array('value' => 'other')),
  3257. 'bad',
  3258. '/option',
  3259. '/select'
  3260. );
  3261. $this->assertTags($result, $expected);
  3262. $result = $this->Form->select(
  3263. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3264. array('empty' => false)
  3265. );
  3266. $expected = array(
  3267. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3268. array('option' => array('value' => 'first')),
  3269. 'first &quot;html&quot; &lt;chars&gt;',
  3270. '/option',
  3271. array('option' => array('value' => 'second')),
  3272. 'value',
  3273. '/option',
  3274. '/select'
  3275. );
  3276. $this->assertTags($result, $expected);
  3277. $result = $this->Form->select(
  3278. 'Model.field',
  3279. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3280. array('escape' => false, 'empty' => false)
  3281. );
  3282. $expected = array(
  3283. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3284. array('option' => array('value' => 'first')),
  3285. 'first "html" <chars>',
  3286. '/option',
  3287. array('option' => array('value' => 'second')),
  3288. 'value',
  3289. '/option',
  3290. '/select'
  3291. );
  3292. $this->assertTags($result, $expected);
  3293. $options = array(
  3294. array('value' => 'first', 'name' => 'First'),
  3295. array('value' => 'first', 'name' => 'Another First'),
  3296. );
  3297. $result = $this->Form->select(
  3298. 'Model.field',
  3299. $options,
  3300. array('escape' => false, 'empty' => false)
  3301. );
  3302. $expected = array(
  3303. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3304. array('option' => array('value' => 'first')),
  3305. 'First',
  3306. '/option',
  3307. array('option' => array('value' => 'first')),
  3308. 'Another First',
  3309. '/option',
  3310. '/select'
  3311. );
  3312. $this->assertTags($result, $expected);
  3313. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3314. $result = $this->Form->select(
  3315. 'Model.contact_id',
  3316. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3317. array('escape' => false, 'empty' => 'pick something')
  3318. );
  3319. $expected = array(
  3320. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3321. array('option' => array('value' => '')), 'pick something', '/option',
  3322. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3323. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3324. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3325. '/select'
  3326. );
  3327. $this->assertTags($result, $expected);
  3328. $this->Form->request->data['Model']['field'] = 0;
  3329. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3330. $expected = array(
  3331. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3332. array('option' => array('value' => '')), '/option',
  3333. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3334. array('option' => array('value' => '1')), 'Yes', '/option',
  3335. '/select'
  3336. );
  3337. $this->assertTags($result, $expected);
  3338. }
  3339. /**
  3340. * test that select() with optiongroups listens to the escape param.
  3341. *
  3342. * @return void
  3343. */
  3344. public function testSelectOptionGroupEscaping() {
  3345. $options = array(
  3346. '>< Key' => array(
  3347. 1 => 'One',
  3348. 2 => 'Two'
  3349. )
  3350. );
  3351. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3352. $expected = array(
  3353. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3354. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3355. array('option' => array('value' => '1')), 'One', '/option',
  3356. array('option' => array('value' => '2')), 'Two', '/option',
  3357. '/optgroup',
  3358. '/select'
  3359. );
  3360. $this->assertTags($result, $expected);
  3361. $options = array(
  3362. '>< Key' => array(
  3363. 1 => 'One',
  3364. 2 => 'Two'
  3365. )
  3366. );
  3367. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3368. $expected = array(
  3369. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3370. 'optgroup' => array('label' => '>< Key'),
  3371. array('option' => array('value' => '1')), 'One', '/option',
  3372. array('option' => array('value' => '2')), 'Two', '/option',
  3373. '/optgroup',
  3374. '/select'
  3375. );
  3376. $this->assertTags($result, $expected);
  3377. }
  3378. /**
  3379. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3380. *
  3381. * @return void
  3382. */
  3383. public function testSelectWithNullAttributes() {
  3384. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3385. $expected = array(
  3386. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3387. array('option' => array('value' => '0')),
  3388. 'first',
  3389. '/option',
  3390. array('option' => array('value' => '1')),
  3391. 'second',
  3392. '/option',
  3393. '/select'
  3394. );
  3395. $this->assertTags($result, $expected);
  3396. }
  3397. /**
  3398. * testNestedSelect method
  3399. *
  3400. * test select element generation with optgroups
  3401. *
  3402. * @return void
  3403. */
  3404. public function testNestedSelect() {
  3405. $result = $this->Form->select(
  3406. 'Model.field',
  3407. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3408. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3409. )), array('empty' => false)
  3410. );
  3411. $expected = array(
  3412. 'select' => array('name' => 'data[Model][field]',
  3413. 'id' => 'ModelField'),
  3414. array('option' => array('value' => 1)),
  3415. 'One',
  3416. '/option',
  3417. array('option' => array('value' => 2)),
  3418. 'Two',
  3419. '/option',
  3420. array('optgroup' => array('label' => 'Three')),
  3421. array('option' => array('value' => 4)),
  3422. 'Four',
  3423. '/option',
  3424. array('option' => array('value' => 5)),
  3425. 'Five',
  3426. '/option',
  3427. '/optgroup',
  3428. '/select'
  3429. );
  3430. $this->assertTags($result, $expected);
  3431. $result = $this->Form->select(
  3432. 'Model.field',
  3433. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3434. array('showParents' => true, 'empty' => false)
  3435. );
  3436. $expected = array(
  3437. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3438. array('option' => array('value' => 1)),
  3439. 'One',
  3440. '/option',
  3441. array('option' => array('value' => 2)),
  3442. 'Two',
  3443. '/option',
  3444. array('optgroup' => array('label' => 'Three')),
  3445. array('option' => array('value' => 3)),
  3446. 'Three',
  3447. '/option',
  3448. array('option' => array('value' => 4)),
  3449. 'Four',
  3450. '/option',
  3451. '/optgroup',
  3452. '/select'
  3453. );
  3454. $this->assertTags($result, $expected);
  3455. }
  3456. /**
  3457. * testSelectMultiple method
  3458. *
  3459. * test generation of multiple select elements
  3460. *
  3461. * @return void
  3462. */
  3463. public function testSelectMultiple() {
  3464. $options = array('first', 'second', 'third');
  3465. $result = $this->Form->select(
  3466. 'Model.multi_field', $options, array('multiple' => true)
  3467. );
  3468. $expected = array(
  3469. 'input' => array(
  3470. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3471. ),
  3472. 'select' => array(
  3473. 'name' => 'data[Model][multi_field][]',
  3474. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3475. ),
  3476. array('option' => array('value' => '0')),
  3477. 'first',
  3478. '/option',
  3479. array('option' => array('value' => '1')),
  3480. 'second',
  3481. '/option',
  3482. array('option' => array('value' => '2')),
  3483. 'third',
  3484. '/option',
  3485. '/select'
  3486. );
  3487. $this->assertTags($result, $expected);
  3488. $result = $this->Form->select(
  3489. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3490. );
  3491. $expected = array(
  3492. 'input' => array(
  3493. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3494. ),
  3495. 'select' => array(
  3496. 'name' => 'data[Model][multi_field][]',
  3497. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3498. ),
  3499. array('option' => array('value' => '0')),
  3500. 'first',
  3501. '/option',
  3502. array('option' => array('value' => '1')),
  3503. 'second',
  3504. '/option',
  3505. array('option' => array('value' => '2')),
  3506. 'third',
  3507. '/option',
  3508. '/select'
  3509. );
  3510. $this->assertTags($result, $expected);
  3511. $result = $this->Form->select(
  3512. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3513. );
  3514. $expected = array(
  3515. 'input' => array(
  3516. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3517. ),
  3518. 'select' => array(
  3519. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3520. 'multiple' => 'multiple'
  3521. ),
  3522. array('option' => array('value' => '0', 'selected' => 'selected')),
  3523. 'first',
  3524. '/option',
  3525. array('option' => array('value' => '1', 'selected' => 'selected')),
  3526. 'second',
  3527. '/option',
  3528. array('option' => array('value' => '2')),
  3529. 'third',
  3530. '/option',
  3531. '/select'
  3532. );
  3533. $this->assertTags($result, $expected);
  3534. $result = $this->Form->select(
  3535. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3536. );
  3537. $expected = array(
  3538. 'select' => array(
  3539. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3540. ),
  3541. array('option' => array('value' => '0', 'selected' => 'selected')),
  3542. 'first',
  3543. '/option',
  3544. array('option' => array('value' => '1', 'selected' => 'selected')),
  3545. 'second',
  3546. '/option',
  3547. array('option' => array('value' => '2')),
  3548. 'third',
  3549. '/option',
  3550. '/select'
  3551. );
  3552. $this->assertTags($result, $expected);
  3553. }
  3554. /**
  3555. * test generation of habtm select boxes.
  3556. *
  3557. * @return void
  3558. */
  3559. public function testHabtmSelectBox() {
  3560. $this->View->viewVars['contactTags'] = array(
  3561. 1 => 'blue',
  3562. 2 => 'red',
  3563. 3 => 'green'
  3564. );
  3565. $this->Form->request->data = array(
  3566. 'Contact' => array(),
  3567. 'ContactTag' => array(
  3568. array(
  3569. 'id' => 1,
  3570. 'name' => 'blue'
  3571. ),
  3572. array(
  3573. 'id' => 3,
  3574. 'name' => 'green'
  3575. )
  3576. )
  3577. );
  3578. $this->Form->create('Contact');
  3579. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3580. $expected = array(
  3581. 'input' => array(
  3582. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3583. ),
  3584. 'select' => array(
  3585. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3586. 'multiple' => 'multiple'
  3587. ),
  3588. array('option' => array('value' => '1', 'selected' => 'selected')),
  3589. 'blue',
  3590. '/option',
  3591. array('option' => array('value' => '2')),
  3592. 'red',
  3593. '/option',
  3594. array('option' => array('value' => '3', 'selected' => 'selected')),
  3595. 'green',
  3596. '/option',
  3597. '/select'
  3598. );
  3599. $this->assertTags($result, $expected);
  3600. }
  3601. /**
  3602. * test generation of multi select elements in checkbox format
  3603. *
  3604. * @return void
  3605. */
  3606. public function testSelectMultipleCheckboxes() {
  3607. $result = $this->Form->select(
  3608. 'Model.multi_field',
  3609. array('first', 'second', 'third'),
  3610. array('multiple' => 'checkbox')
  3611. );
  3612. $expected = array(
  3613. 'input' => array(
  3614. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3615. ),
  3616. array('div' => array('class' => 'checkbox')),
  3617. array('input' => array(
  3618. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3619. 'value' => '0', 'id' => 'ModelMultiField0'
  3620. )),
  3621. array('label' => array('for' => 'ModelMultiField0')),
  3622. 'first',
  3623. '/label',
  3624. '/div',
  3625. array('div' => array('class' => 'checkbox')),
  3626. array('input' => array(
  3627. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3628. 'value' => '1', 'id' => 'ModelMultiField1'
  3629. )),
  3630. array('label' => array('for' => 'ModelMultiField1')),
  3631. 'second',
  3632. '/label',
  3633. '/div',
  3634. array('div' => array('class' => 'checkbox')),
  3635. array('input' => array(
  3636. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3637. 'value' => '2', 'id' => 'ModelMultiField2'
  3638. )),
  3639. array('label' => array('for' => 'ModelMultiField2')),
  3640. 'third',
  3641. '/label',
  3642. '/div'
  3643. );
  3644. $this->assertTags($result, $expected);
  3645. $result = $this->Form->select(
  3646. 'Model.multi_field',
  3647. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3648. array('multiple' => 'checkbox')
  3649. );
  3650. $expected = array(
  3651. 'input' => array(
  3652. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3653. ),
  3654. array('div' => array('class' => 'checkbox')),
  3655. array('input' => array(
  3656. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3657. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3658. )),
  3659. array('label' => array('for' => 'ModelMultiFieldA')),
  3660. 'first',
  3661. '/label',
  3662. '/div',
  3663. array('div' => array('class' => 'checkbox')),
  3664. array('input' => array(
  3665. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3666. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3667. )),
  3668. array('label' => array('for' => 'ModelMultiFieldB')),
  3669. 'second',
  3670. '/label',
  3671. '/div',
  3672. array('div' => array('class' => 'checkbox')),
  3673. array('input' => array(
  3674. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3675. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3676. )),
  3677. array('label' => array('for' => 'ModelMultiFieldC')),
  3678. 'third',
  3679. '/label',
  3680. '/div'
  3681. );
  3682. $this->assertTags($result, $expected);
  3683. $result = $this->Form->select(
  3684. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3685. );
  3686. $expected = array(
  3687. 'input' => array(
  3688. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3689. ),
  3690. array('div' => array('class' => 'checkbox')),
  3691. array('input' => array(
  3692. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3693. 'value' => '1', 'id' => 'ModelMultiField1'
  3694. )),
  3695. array('label' => array('for' => 'ModelMultiField1')),
  3696. 'first',
  3697. '/label',
  3698. '/div'
  3699. );
  3700. $this->assertTags($result, $expected);
  3701. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3702. $result = $this->Form->select(
  3703. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3704. );
  3705. $expected = array(
  3706. 'input' => array(
  3707. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3708. ),
  3709. array('div' => array('class' => 'checkbox')),
  3710. array('input' => array(
  3711. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3712. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3713. )),
  3714. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3715. 'first',
  3716. '/label',
  3717. '/div',
  3718. array('div' => array('class' => 'checkbox')),
  3719. array('input' => array(
  3720. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3721. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3722. )),
  3723. array('label' => array('for' => 'ModelTagsArray')),
  3724. 'Array',
  3725. '/label',
  3726. '/div'
  3727. );
  3728. $this->assertTags($result, $expected);
  3729. }
  3730. /**
  3731. * test multiple checkboxes with div styles.
  3732. *
  3733. * @return void
  3734. */
  3735. public function testSelectMultipleCheckboxDiv() {
  3736. $result = $this->Form->select(
  3737. 'Model.tags',
  3738. array('first', 'second'),
  3739. array('multiple' => 'checkbox', 'class' => 'my-class')
  3740. );
  3741. $expected = array(
  3742. 'input' => array(
  3743. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3744. ),
  3745. array('div' => array('class' => 'my-class')),
  3746. array('input' => array(
  3747. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3748. 'value' => '0', 'id' => 'ModelTags0'
  3749. )),
  3750. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3751. '/div',
  3752. array('div' => array('class' => 'my-class')),
  3753. array('input' => array(
  3754. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3755. 'value' => '1', 'id' => 'ModelTags1'
  3756. )),
  3757. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3758. '/div'
  3759. );
  3760. $this->assertTags($result, $expected);
  3761. $result = $this->Form->input('Model.tags', array(
  3762. 'options' => array('first', 'second'),
  3763. 'multiple' => 'checkbox',
  3764. 'class' => 'my-class',
  3765. 'div' => false,
  3766. 'label' => false
  3767. ));
  3768. $this->assertTags($result, $expected);
  3769. $Contact = ClassRegistry::getObject('Contact');
  3770. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3771. $result = $this->Form->input('Contact.tags', array(
  3772. 'options' => array('one'),
  3773. 'multiple' => 'checkbox',
  3774. 'label' => false,
  3775. 'div' => false
  3776. ));
  3777. $expected = array(
  3778. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3779. array('div' => array('class' => 'checkbox form-error')),
  3780. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3781. array('label' => array('for' => 'ContactTags0')),
  3782. 'one',
  3783. '/label',
  3784. '/div'
  3785. );
  3786. $this->assertTags($result, $expected);
  3787. $result = $this->Form->input('Contact.tags', array(
  3788. 'options' => array('one'),
  3789. 'multiple' => 'checkbox',
  3790. 'class' => 'mycheckbox',
  3791. 'label' => false,
  3792. 'div' => false
  3793. ));
  3794. $expected = array(
  3795. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3796. array('div' => array('class' => 'mycheckbox form-error')),
  3797. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3798. array('label' => array('for' => 'ContactTags0')),
  3799. 'one',
  3800. '/label',
  3801. '/div'
  3802. );
  3803. $this->assertTags($result, $expected);
  3804. }
  3805. /**
  3806. * Checks the security hash array generated for multiple-input checkbox elements
  3807. *
  3808. * @return void
  3809. */
  3810. public function testSelectMultipleCheckboxSecurity() {
  3811. $this->Form->request['_Token'] = array('key' => 'testKey');
  3812. $this->assertEquals(array(), $this->Form->fields);
  3813. $result = $this->Form->select(
  3814. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3815. array('multiple' => 'checkbox')
  3816. );
  3817. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3818. $result = $this->Form->secure($this->Form->fields);
  3819. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3820. $this->assertRegExp('/"' . $key . '"/', $result);
  3821. }
  3822. /**
  3823. * testInputMultipleCheckboxes method
  3824. *
  3825. * test input() resulting in multi select elements being generated.
  3826. *
  3827. * @return void
  3828. */
  3829. public function testInputMultipleCheckboxes() {
  3830. $result = $this->Form->input('Model.multi_field', array(
  3831. 'options' => array('first', 'second', 'third'),
  3832. 'multiple' => 'checkbox'
  3833. ));
  3834. $expected = array(
  3835. array('div' => array('class' => 'input select')),
  3836. array('label' => array('for' => 'ModelMultiField')),
  3837. 'Multi Field',
  3838. '/label',
  3839. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3840. array('div' => array('class' => 'checkbox')),
  3841. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3842. array('label' => array('for' => 'ModelMultiField0')),
  3843. 'first',
  3844. '/label',
  3845. '/div',
  3846. array('div' => array('class' => 'checkbox')),
  3847. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3848. array('label' => array('for' => 'ModelMultiField1')),
  3849. 'second',
  3850. '/label',
  3851. '/div',
  3852. array('div' => array('class' => 'checkbox')),
  3853. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3854. array('label' => array('for' => 'ModelMultiField2')),
  3855. 'third',
  3856. '/label',
  3857. '/div',
  3858. '/div'
  3859. );
  3860. $this->assertTags($result, $expected);
  3861. $result = $this->Form->input('Model.multi_field', array(
  3862. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3863. 'multiple' => 'checkbox'
  3864. ));
  3865. $expected = array(
  3866. array('div' => array('class' => 'input select')),
  3867. array('label' => array('for' => 'ModelMultiField')),
  3868. 'Multi Field',
  3869. '/label',
  3870. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3871. array('div' => array('class' => 'checkbox')),
  3872. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3873. array('label' => array('for' => 'ModelMultiFieldA')),
  3874. 'first',
  3875. '/label',
  3876. '/div',
  3877. array('div' => array('class' => 'checkbox')),
  3878. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3879. array('label' => array('for' => 'ModelMultiFieldB')),
  3880. 'second',
  3881. '/label',
  3882. '/div',
  3883. array('div' => array('class' => 'checkbox')),
  3884. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  3885. array('label' => array('for' => 'ModelMultiFieldC')),
  3886. 'third',
  3887. '/label',
  3888. '/div',
  3889. '/div'
  3890. );
  3891. $this->assertTags($result, $expected);
  3892. $result = $this->Form->input('Model.multi_field', array(
  3893. 'options' => array('1' => 'first'),
  3894. 'multiple' => 'checkbox',
  3895. 'label' => false,
  3896. 'div' => false
  3897. ));
  3898. $expected = array(
  3899. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3900. array('div' => array('class' => 'checkbox')),
  3901. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3902. array('label' => array('for' => 'ModelMultiField1')),
  3903. 'first',
  3904. '/label',
  3905. '/div'
  3906. );
  3907. $this->assertTags($result, $expected);
  3908. $result = $this->Form->input('Model.multi_field', array(
  3909. 'options' => array('2' => 'second'),
  3910. 'multiple' => 'checkbox',
  3911. 'label' => false,
  3912. 'div' => false
  3913. ));
  3914. $expected = array(
  3915. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3916. array('div' => array('class' => 'checkbox')),
  3917. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3918. array('label' => array('for' => 'ModelMultiField2')),
  3919. 'second',
  3920. '/label',
  3921. '/div'
  3922. );
  3923. $this->assertTags($result, $expected);
  3924. }
  3925. /**
  3926. * testSelectHiddenFieldOmission method
  3927. *
  3928. * test that select() with 'hiddenField' => false omits the hidden field
  3929. *
  3930. * @return void
  3931. */
  3932. public function testSelectHiddenFieldOmission() {
  3933. $result = $this->Form->select('Model.multi_field',
  3934. array('first', 'second'),
  3935. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3936. );
  3937. $expected = array(
  3938. array('div' => array('class' => 'checkbox')),
  3939. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3940. array('label' => array('for' => 'ModelMultiField0')),
  3941. 'first',
  3942. '/label',
  3943. '/div',
  3944. array('div' => array('class' => 'checkbox')),
  3945. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3946. array('label' => array('for' => 'ModelMultiField1')),
  3947. 'second',
  3948. '/label',
  3949. '/div'
  3950. );
  3951. $this->assertTags($result, $expected);
  3952. $result = $this->Form->input('Model.multi_field', array(
  3953. 'options' => array('first', 'second'),
  3954. 'multiple' => 'checkbox',
  3955. 'hiddenField' => false
  3956. ));
  3957. $expected = array(
  3958. array('div' => array('class' => 'input select')),
  3959. array('label' => array('for' => 'ModelMultiField')),
  3960. 'Multi Field',
  3961. '/label',
  3962. array('div' => array('class' => 'checkbox')),
  3963. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3964. array('label' => array('for' => 'ModelMultiField0')),
  3965. 'first',
  3966. '/label',
  3967. '/div',
  3968. array('div' => array('class' => 'checkbox')),
  3969. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3970. array('label' => array('for' => 'ModelMultiField1')),
  3971. 'second',
  3972. '/label',
  3973. '/div',
  3974. '/div'
  3975. );
  3976. $this->assertTags($result, $expected);
  3977. }
  3978. /**
  3979. * test that select() with multiple = checkbox works with overriding name attribute.
  3980. *
  3981. * @return void
  3982. */
  3983. public function testSelectCheckboxMultipleOverrideName() {
  3984. $result = $this->Form->input('category', array(
  3985. 'type' => 'select',
  3986. 'multiple' => 'checkbox',
  3987. 'name' => 'data[fish]',
  3988. 'options' => array('1', '2'),
  3989. 'div' => false,
  3990. 'label' => false,
  3991. ));
  3992. $expected = array(
  3993. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  3994. array('div' => array('class' => 'checkbox')),
  3995. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  3996. array('label' => array('for' => 'Category0')), '1', '/label',
  3997. '/div',
  3998. array('div' => array('class' => 'checkbox')),
  3999. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4000. array('label' => array('for' => 'Category1')), '2', '/label',
  4001. '/div'
  4002. );
  4003. $this->assertTags($result, $expected);
  4004. }
  4005. /**
  4006. * Test that 'id' overrides all the checkbox id's as well.
  4007. *
  4008. * @return void
  4009. */
  4010. public function testSelectCheckboxMultipleId() {
  4011. $result = $this->Form->select(
  4012. 'Model.multi_field',
  4013. array('first', 'second', 'third'),
  4014. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4015. );
  4016. $expected = array(
  4017. 'input' => array(
  4018. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4019. ),
  4020. array('div' => array('class' => 'checkbox')),
  4021. array('input' => array(
  4022. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4023. 'value' => '0', 'id' => 'CustomId0'
  4024. )),
  4025. array('label' => array('for' => 'CustomId0')),
  4026. 'first',
  4027. '/label',
  4028. '/div',
  4029. array('div' => array('class' => 'checkbox')),
  4030. array('input' => array(
  4031. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4032. 'value' => '1', 'id' => 'CustomId1'
  4033. )),
  4034. array('label' => array('for' => 'CustomId1')),
  4035. 'second',
  4036. '/label',
  4037. '/div',
  4038. array('div' => array('class' => 'checkbox')),
  4039. array('input' => array(
  4040. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4041. 'value' => '2', 'id' => 'CustomId2'
  4042. )),
  4043. array('label' => array('for' => 'CustomId2')),
  4044. 'third',
  4045. '/label',
  4046. '/div'
  4047. );
  4048. $this->assertTags($result, $expected);
  4049. }
  4050. /**
  4051. * testCheckbox method
  4052. *
  4053. * Test generation of checkboxes
  4054. *
  4055. * @return void
  4056. */
  4057. public function testCheckbox() {
  4058. $result = $this->Form->checkbox('Model.field');
  4059. $expected = array(
  4060. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4061. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4062. );
  4063. $this->assertTags($result, $expected);
  4064. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4065. $expected = array(
  4066. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4067. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4068. );
  4069. $this->assertTags($result, $expected);
  4070. $Contact = ClassRegistry::getObject('Contact');
  4071. $Contact->validationErrors['field'] = 1;
  4072. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4073. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4074. $expected = array(
  4075. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4076. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4077. );
  4078. $this->assertTags($result, $expected);
  4079. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4080. $expected = array(
  4081. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4082. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4083. );
  4084. $this->assertTags($result, $expected);
  4085. $this->Form->request->data['Contact']['field'] = '';
  4086. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4087. $expected = array(
  4088. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4089. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4090. );
  4091. $this->assertTags($result, $expected);
  4092. $Contact->validationErrors = array();
  4093. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4094. $expected = array(
  4095. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4096. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4097. );
  4098. $this->assertTags($result, $expected);
  4099. $this->Form->request->data['Contact']['published'] = 1;
  4100. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4101. $expected = array(
  4102. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4103. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4104. );
  4105. $this->assertTags($result, $expected);
  4106. $this->Form->request->data['Contact']['published'] = 0;
  4107. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4108. $expected = array(
  4109. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4110. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4111. );
  4112. $this->assertTags($result, $expected);
  4113. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4114. $expected = array(
  4115. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4116. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4117. );
  4118. $this->assertTags($result, $expected);
  4119. $result = $this->Form->checkbox('CustomField.1.value');
  4120. $expected = array(
  4121. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4122. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4123. );
  4124. $this->assertTags($result, $expected);
  4125. }
  4126. /**
  4127. * test checkbox() with a custom name attribute
  4128. *
  4129. * @return void
  4130. */
  4131. public function testCheckboxCustomNameAttribute() {
  4132. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4133. $expected = array(
  4134. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4135. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4136. );
  4137. $this->assertTags($result, $expected);
  4138. }
  4139. /**
  4140. * test the checked option for checkboxes.
  4141. *
  4142. * @return void
  4143. */
  4144. public function testCheckboxCheckedOption() {
  4145. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4146. $expected = array(
  4147. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4148. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4149. );
  4150. $this->assertTags($result, $expected);
  4151. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4152. $expected = array(
  4153. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4154. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4155. );
  4156. $this->assertTags($result, $expected);
  4157. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4158. $expected = array(
  4159. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4160. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4161. );
  4162. $this->assertTags($result, $expected);
  4163. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4164. $expected = array(
  4165. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4166. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4167. );
  4168. $this->assertTags($result, $expected);
  4169. $this->Form->request->data['Model']['field'] = 1;
  4170. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4171. $expected = array(
  4172. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4173. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4174. );
  4175. $this->assertTags($result, $expected);
  4176. }
  4177. /**
  4178. * Test that disabled attribute works on both the checkbox and hidden input.
  4179. *
  4180. * @return void
  4181. */
  4182. public function testCheckboxDisabling() {
  4183. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4184. $expected = array(
  4185. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4186. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4187. );
  4188. $this->assertTags($result, $expected);
  4189. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4190. $expected = array(
  4191. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4192. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4193. );
  4194. $this->assertTags($result, $expected);
  4195. }
  4196. /**
  4197. * Test that the hidden input for checkboxes can be omitted or set to a
  4198. * specific value.
  4199. *
  4200. * @return void
  4201. */
  4202. public function testCheckboxHiddenField() {
  4203. $result = $this->Form->input('UserForm.something', array(
  4204. 'type' => 'checkbox',
  4205. 'hiddenField' => false
  4206. ));
  4207. $expected = array(
  4208. 'div' => array('class' => 'input checkbox'),
  4209. array('input' => array(
  4210. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4211. 'value' => '1', 'id' => 'UserFormSomething'
  4212. )),
  4213. 'label' => array('for' => 'UserFormSomething'),
  4214. 'Something',
  4215. '/label',
  4216. '/div'
  4217. );
  4218. $this->assertTags($result, $expected);
  4219. $result = $this->Form->input('UserForm.something', array(
  4220. 'type' => 'checkbox',
  4221. 'value' => 'Y',
  4222. 'hiddenField' => 'N',
  4223. ));
  4224. $expected = array(
  4225. 'div' => array('class' => 'input checkbox'),
  4226. array('input' => array(
  4227. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4228. 'value' => 'N', 'id' => 'UserFormSomething_'
  4229. )),
  4230. array('input' => array(
  4231. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4232. 'value' => 'Y', 'id' => 'UserFormSomething'
  4233. )),
  4234. 'label' => array('for' => 'UserFormSomething'),
  4235. 'Something',
  4236. '/label',
  4237. '/div'
  4238. );
  4239. $this->assertTags($result, $expected);
  4240. }
  4241. /**
  4242. * testDateTime method
  4243. *
  4244. * Test generation of date/time select elements
  4245. *
  4246. * @return void
  4247. */
  4248. public function testDateTime() {
  4249. extract($this->dateRegex);
  4250. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4251. $now = strtotime('now');
  4252. $expected = array(
  4253. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4254. $daysRegex,
  4255. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4256. date('j', $now),
  4257. '/option',
  4258. '*/select',
  4259. '-',
  4260. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4261. $monthsRegex,
  4262. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4263. date('F', $now),
  4264. '/option',
  4265. '*/select',
  4266. '-',
  4267. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4268. $yearsRegex,
  4269. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4270. date('Y', $now),
  4271. '/option',
  4272. '*/select',
  4273. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4274. $hoursRegex,
  4275. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4276. date('g', $now),
  4277. '/option',
  4278. '*/select',
  4279. ':',
  4280. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4281. $minutesRegex,
  4282. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4283. date('i', $now),
  4284. '/option',
  4285. '*/select',
  4286. ' ',
  4287. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4288. $meridianRegex,
  4289. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4290. date('a', $now),
  4291. '/option',
  4292. '*/select'
  4293. );
  4294. $this->assertTags($result, $expected);
  4295. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4296. $expected = array(
  4297. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4298. $daysRegex,
  4299. array('option' => array('value' => '')),
  4300. '/option',
  4301. '*/select',
  4302. '-',
  4303. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4304. $monthsRegex,
  4305. array('option' => array('value' => '')),
  4306. '/option',
  4307. '*/select',
  4308. '-',
  4309. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4310. $yearsRegex,
  4311. array('option' => array('value' => '')),
  4312. '/option',
  4313. '*/select',
  4314. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4315. $hoursRegex,
  4316. array('option' => array('value' => '')),
  4317. '/option',
  4318. '*/select',
  4319. ':',
  4320. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4321. $minutesRegex,
  4322. array('option' => array('value' => '')),
  4323. '/option',
  4324. '*/select',
  4325. ' ',
  4326. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4327. $meridianRegex,
  4328. array('option' => array('value' => '')),
  4329. '/option',
  4330. '*/select'
  4331. );
  4332. $this->assertTags($result, $expected);
  4333. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4334. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4335. $this->assertTags($result, $expected);
  4336. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4337. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4338. $this->assertTags($result, $expected);
  4339. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4340. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4341. $expected = array(
  4342. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4343. $daysRegex,
  4344. array('option' => array('value' => '')),
  4345. '/option',
  4346. '*/select',
  4347. '-',
  4348. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4349. $monthsRegex,
  4350. array('option' => array('value' => '')),
  4351. '/option',
  4352. '*/select',
  4353. '-',
  4354. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4355. $yearsRegex,
  4356. array('option' => array('value' => '')),
  4357. '/option',
  4358. '*/select',
  4359. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4360. $hoursRegex,
  4361. array('option' => array('value' => '')),
  4362. '/option',
  4363. '*/select',
  4364. ':',
  4365. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4366. $minutesRegex,
  4367. array('option' => array('value' => '')),
  4368. '/option',
  4369. array('option' => array('value' => '00')),
  4370. '00',
  4371. '/option',
  4372. array('option' => array('value' => '05')),
  4373. '05',
  4374. '/option',
  4375. array('option' => array('value' => '10')),
  4376. '10',
  4377. '/option',
  4378. '*/select',
  4379. ' ',
  4380. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4381. $meridianRegex,
  4382. array('option' => array('value' => '')),
  4383. '/option',
  4384. '*/select'
  4385. );
  4386. $this->assertTags($result, $expected);
  4387. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4388. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4389. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4390. $this->Form->request->data['Contact']['data'] = null;
  4391. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4392. $expected = array(
  4393. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4394. $daysRegex,
  4395. array('option' => array('value' => '')),
  4396. '/option',
  4397. '*/select',
  4398. '-',
  4399. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4400. $monthsRegex,
  4401. array('option' => array('value' => '')),
  4402. '/option',
  4403. '*/select',
  4404. '-',
  4405. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4406. $yearsRegex,
  4407. array('option' => array('value' => '')),
  4408. '/option',
  4409. '*/select',
  4410. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4411. $hoursRegex,
  4412. array('option' => array('value' => '')),
  4413. '/option',
  4414. '*/select',
  4415. ':',
  4416. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4417. $minutesRegex,
  4418. array('option' => array('value' => '')),
  4419. '/option',
  4420. '*/select',
  4421. ' ',
  4422. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4423. $meridianRegex,
  4424. array('option' => array('value' => '')),
  4425. '/option',
  4426. '*/select'
  4427. );
  4428. $this->assertTags($result, $expected);
  4429. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4430. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4431. $now = strtotime($this->Form->data['Model']['field']);
  4432. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4433. $expected = array(
  4434. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4435. $daysRegex,
  4436. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4437. date('j', $now),
  4438. '/option',
  4439. '*/select',
  4440. '-',
  4441. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4442. $monthsRegex,
  4443. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4444. date('F', $now),
  4445. '/option',
  4446. '*/select',
  4447. '-',
  4448. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4449. $yearsRegex,
  4450. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4451. date('Y', $now),
  4452. '/option',
  4453. '*/select',
  4454. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4455. $hoursRegex,
  4456. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4457. date('g', $now),
  4458. '/option',
  4459. '*/select',
  4460. ':',
  4461. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4462. $minutesRegex,
  4463. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4464. date('i', $now),
  4465. '/option',
  4466. '*/select',
  4467. ' ',
  4468. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4469. $meridianRegex,
  4470. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4471. date('a', $now),
  4472. '/option',
  4473. '*/select'
  4474. );
  4475. $this->assertTags($result, $expected);
  4476. $selected = strtotime('2008-10-26 12:33:00');
  4477. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4478. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4479. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4480. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4481. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4482. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4483. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4484. $this->Form->create('Contact');
  4485. $result = $this->Form->input('published');
  4486. $now = strtotime('now');
  4487. $expected = array(
  4488. 'div' => array('class' => 'input date'),
  4489. 'label' => array('for' => 'ContactPublishedMonth'),
  4490. 'Published',
  4491. '/label',
  4492. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4493. $monthsRegex,
  4494. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4495. date('F', $now),
  4496. '/option',
  4497. '*/select',
  4498. '-',
  4499. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4500. $daysRegex,
  4501. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4502. date('j', $now),
  4503. '/option',
  4504. '*/select',
  4505. '-',
  4506. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4507. $yearsRegex,
  4508. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4509. date('Y', $now),
  4510. '/option',
  4511. '*/select',
  4512. '/div'
  4513. );
  4514. $this->assertTags($result, $expected);
  4515. $result = $this->Form->input('published2', array('type' => 'date'));
  4516. $now = strtotime('now');
  4517. $expected = array(
  4518. 'div' => array('class' => 'input date'),
  4519. 'label' => array('for' => 'ContactPublished2Month'),
  4520. 'Published2',
  4521. '/label',
  4522. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4523. $monthsRegex,
  4524. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4525. date('F', $now),
  4526. '/option',
  4527. '*/select',
  4528. '-',
  4529. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4530. $daysRegex,
  4531. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4532. date('j', $now),
  4533. '/option',
  4534. '*/select',
  4535. '-',
  4536. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4537. $yearsRegex,
  4538. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4539. date('Y', $now),
  4540. '/option',
  4541. '*/select',
  4542. '/div'
  4543. );
  4544. $this->assertTags($result, $expected);
  4545. $this->Form->create('Contact');
  4546. $result = $this->Form->input('published', array('monthNames' => false));
  4547. $now = strtotime('now');
  4548. $expected = array(
  4549. 'div' => array('class' => 'input date'),
  4550. 'label' => array('for' => 'ContactPublishedMonth'),
  4551. 'Published',
  4552. '/label',
  4553. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4554. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4555. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4556. date('m', $now),
  4557. '/option',
  4558. '*/select',
  4559. '-',
  4560. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4561. $daysRegex,
  4562. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4563. date('j', $now),
  4564. '/option',
  4565. '*/select',
  4566. '-',
  4567. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4568. $yearsRegex,
  4569. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4570. date('Y', $now),
  4571. '/option',
  4572. '*/select',
  4573. '/div'
  4574. );
  4575. $this->assertTags($result, $expected);
  4576. $result = $this->Form->input('published', array('type' => 'time'));
  4577. $now = strtotime('now');
  4578. $expected = array(
  4579. 'div' => array('class' => 'input time'),
  4580. 'label' => array('for' => 'ContactPublishedHour'),
  4581. 'Published',
  4582. '/label',
  4583. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4584. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4585. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4586. date('g', $now),
  4587. '/option',
  4588. '*/select',
  4589. ':',
  4590. );
  4591. $this->assertTags($result, $expected);
  4592. $result = $this->Form->input('published', array(
  4593. 'timeFormat' => 24,
  4594. 'interval' => 5,
  4595. 'selected' => strtotime('2009-09-03 13:37:00'),
  4596. 'type' => 'datetime'
  4597. ));
  4598. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4599. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4600. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4601. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4602. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4603. $this->assertNoErrors();
  4604. $this->Form->request->data['Contact'] = array(
  4605. 'date' => array(
  4606. 'day' => '',
  4607. 'month' => '',
  4608. 'year' => '',
  4609. 'hour' => '',
  4610. 'min' => '',
  4611. 'meridian' => ''
  4612. )
  4613. );
  4614. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4615. }
  4616. /**
  4617. * test that datetime() and default values work.
  4618. *
  4619. * @return void
  4620. */
  4621. public function testDatetimeWithDefault() {
  4622. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4623. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4624. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4625. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4626. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4627. 'default' => '2009-06-01 11:15:30'
  4628. ));
  4629. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4630. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4631. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4632. }
  4633. /**
  4634. * test that bogus non-date time data doesn't cause errors.
  4635. *
  4636. * @return void
  4637. */
  4638. public function testDateTimeWithBogusData() {
  4639. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4640. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4641. }
  4642. /**
  4643. * testFormDateTimeMulti method
  4644. *
  4645. * test multiple datetime element generation
  4646. *
  4647. * @return void
  4648. */
  4649. public function testFormDateTimeMulti() {
  4650. extract($this->dateRegex);
  4651. $result = $this->Form->dateTime('Contact.1.updated');
  4652. $expected = array(
  4653. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4654. $daysRegex,
  4655. array('option' => array('value' => '')),
  4656. '/option',
  4657. '*/select',
  4658. '-',
  4659. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4660. $monthsRegex,
  4661. array('option' => array('value' => '')),
  4662. '/option',
  4663. '*/select',
  4664. '-',
  4665. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4666. $yearsRegex,
  4667. array('option' => array('value' => '')),
  4668. '/option',
  4669. '*/select',
  4670. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4671. $hoursRegex,
  4672. array('option' => array('value' => '')),
  4673. '/option',
  4674. '*/select',
  4675. ':',
  4676. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4677. $minutesRegex,
  4678. array('option' => array('value' => '')),
  4679. '/option',
  4680. '*/select',
  4681. ' ',
  4682. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4683. $meridianRegex,
  4684. array('option' => array('value' => '')),
  4685. '/option',
  4686. '*/select'
  4687. );
  4688. $this->assertTags($result, $expected);
  4689. $result = $this->Form->dateTime('Contact.2.updated');
  4690. $expected = array(
  4691. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4692. $daysRegex,
  4693. array('option' => array('value' => '')),
  4694. '/option',
  4695. '*/select',
  4696. '-',
  4697. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4698. $monthsRegex,
  4699. array('option' => array('value' => '')),
  4700. '/option',
  4701. '*/select',
  4702. '-',
  4703. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4704. $yearsRegex,
  4705. array('option' => array('value' => '')),
  4706. '/option',
  4707. '*/select',
  4708. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4709. $hoursRegex,
  4710. array('option' => array('value' => '')),
  4711. '/option',
  4712. '*/select',
  4713. ':',
  4714. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4715. $minutesRegex,
  4716. array('option' => array('value' => '')),
  4717. '/option',
  4718. '*/select',
  4719. ' ',
  4720. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4721. $meridianRegex,
  4722. array('option' => array('value' => '')),
  4723. '/option',
  4724. '*/select'
  4725. );
  4726. $this->assertTags($result, $expected);
  4727. }
  4728. /**
  4729. * When changing the date format, the label should always focus the first select box when
  4730. * clicked.
  4731. *
  4732. * @return void
  4733. */
  4734. public function testDateTimeLabelIdMatchesFirstInput() {
  4735. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4736. $this->assertContains('label for="ModelDateMonth"', $result);
  4737. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4738. $this->assertContains('label for="ModelDateDay"', $result);
  4739. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4740. $this->assertContains('label for="ModelDateYear"', $result);
  4741. }
  4742. /**
  4743. * testMonth method
  4744. *
  4745. * @return void
  4746. */
  4747. public function testMonth() {
  4748. $result = $this->Form->month('Model.field');
  4749. $expected = array(
  4750. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4751. array('option' => array('value' => '')),
  4752. '/option',
  4753. array('option' => array('value' => '01')),
  4754. date('F', strtotime('2008-01-01 00:00:00')),
  4755. '/option',
  4756. array('option' => array('value' => '02')),
  4757. date('F', strtotime('2008-02-01 00:00:00')),
  4758. '/option',
  4759. '*/select',
  4760. );
  4761. $this->assertTags($result, $expected);
  4762. $result = $this->Form->month('Model.field', array('empty' => true));
  4763. $expected = array(
  4764. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4765. array('option' => array('value' => '')),
  4766. '/option',
  4767. array('option' => array('value' => '01')),
  4768. date('F', strtotime('2008-01-01 00:00:00')),
  4769. '/option',
  4770. array('option' => array('value' => '02')),
  4771. date('F', strtotime('2008-02-01 00:00:00')),
  4772. '/option',
  4773. '*/select',
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4777. $expected = array(
  4778. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4779. array('option' => array('value' => '')),
  4780. '/option',
  4781. array('option' => array('value' => '01')),
  4782. '01',
  4783. '/option',
  4784. array('option' => array('value' => '02')),
  4785. '02',
  4786. '/option',
  4787. '*/select',
  4788. );
  4789. $this->assertTags($result, $expected);
  4790. $monthNames = array(
  4791. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4792. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4793. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4794. $expected = array(
  4795. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4796. array('option' => array('value' => '')),
  4797. '/option',
  4798. array('option' => array('value' => '01')),
  4799. 'Jan',
  4800. '/option',
  4801. array('option' => array('value' => '02')),
  4802. 'Feb',
  4803. '/option',
  4804. '*/select',
  4805. );
  4806. $this->assertTags($result, $expected);
  4807. }
  4808. /**
  4809. * testDay method
  4810. *
  4811. * @return void
  4812. */
  4813. public function testDay() {
  4814. extract($this->dateRegex);
  4815. $result = $this->Form->day('Model.field', array('value' => false));
  4816. $expected = array(
  4817. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4818. array('option' => array('value' => '')),
  4819. '/option',
  4820. array('option' => array('value' => '01')),
  4821. '1',
  4822. '/option',
  4823. array('option' => array('value' => '02')),
  4824. '2',
  4825. '/option',
  4826. $daysRegex,
  4827. '/select',
  4828. );
  4829. $this->assertTags($result, $expected);
  4830. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4831. $result = $this->Form->day('Model.field');
  4832. $expected = array(
  4833. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4834. array('option' => array('value' => '')),
  4835. '/option',
  4836. array('option' => array('value' => '01')),
  4837. '1',
  4838. '/option',
  4839. array('option' => array('value' => '02')),
  4840. '2',
  4841. '/option',
  4842. $daysRegex,
  4843. array('option' => array('value' => '10', 'selected' => 'selected')),
  4844. '10',
  4845. '/option',
  4846. $daysRegex,
  4847. '/select',
  4848. );
  4849. $this->assertTags($result, $expected);
  4850. $this->Form->request->data['Model']['field'] = '';
  4851. $result = $this->Form->day('Model.field', array('value' => '10'));
  4852. $expected = array(
  4853. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4854. array('option' => array('value' => '')),
  4855. '/option',
  4856. array('option' => array('value' => '01')),
  4857. '1',
  4858. '/option',
  4859. array('option' => array('value' => '02')),
  4860. '2',
  4861. '/option',
  4862. $daysRegex,
  4863. array('option' => array('value' => '10', 'selected' => 'selected')),
  4864. '10',
  4865. '/option',
  4866. $daysRegex,
  4867. '/select',
  4868. );
  4869. $this->assertTags($result, $expected);
  4870. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4871. $result = $this->Form->day('Model.field', array('value' => true));
  4872. $expected = array(
  4873. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4874. array('option' => array('value' => '')),
  4875. '/option',
  4876. array('option' => array('value' => '01')),
  4877. '1',
  4878. '/option',
  4879. array('option' => array('value' => '02')),
  4880. '2',
  4881. '/option',
  4882. $daysRegex,
  4883. array('option' => array('value' => '10', 'selected' => 'selected')),
  4884. '10',
  4885. '/option',
  4886. $daysRegex,
  4887. '/select',
  4888. );
  4889. $this->assertTags($result, $expected);
  4890. }
  4891. /**
  4892. * testMinute method
  4893. *
  4894. * @return void
  4895. */
  4896. public function testMinute() {
  4897. extract($this->dateRegex);
  4898. $result = $this->Form->minute('Model.field');
  4899. $expected = array(
  4900. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4901. array('option' => array('value' => '')),
  4902. '/option',
  4903. array('option' => array('value' => '00')),
  4904. '00',
  4905. '/option',
  4906. array('option' => array('value' => '01')),
  4907. '01',
  4908. '/option',
  4909. array('option' => array('value' => '02')),
  4910. '02',
  4911. '/option',
  4912. $minutesRegex,
  4913. '/select',
  4914. );
  4915. $this->assertTags($result, $expected);
  4916. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4917. $result = $this->Form->minute('Model.field');
  4918. $expected = array(
  4919. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4920. array('option' => array('value' => '')),
  4921. '/option',
  4922. array('option' => array('value' => '00')),
  4923. '00',
  4924. '/option',
  4925. array('option' => array('value' => '01')),
  4926. '01',
  4927. '/option',
  4928. array('option' => array('value' => '02')),
  4929. '02',
  4930. '/option',
  4931. $minutesRegex,
  4932. array('option' => array('value' => '12', 'selected' => 'selected')),
  4933. '12',
  4934. '/option',
  4935. $minutesRegex,
  4936. '/select',
  4937. );
  4938. $this->assertTags($result, $expected);
  4939. $this->Form->request->data['Model']['field'] = '';
  4940. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4941. $expected = array(
  4942. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4943. array('option' => array('value' => '')),
  4944. '/option',
  4945. array('option' => array('value' => '00')),
  4946. '00',
  4947. '/option',
  4948. array('option' => array('value' => '05')),
  4949. '05',
  4950. '/option',
  4951. array('option' => array('value' => '10')),
  4952. '10',
  4953. '/option',
  4954. $minutesRegex,
  4955. '/select',
  4956. );
  4957. $this->assertTags($result, $expected);
  4958. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4959. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4960. $expected = array(
  4961. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4962. array('option' => array('value' => '')),
  4963. '/option',
  4964. array('option' => array('value' => '00')),
  4965. '00',
  4966. '/option',
  4967. array('option' => array('value' => '05')),
  4968. '05',
  4969. '/option',
  4970. array('option' => array('value' => '10', 'selected' => 'selected')),
  4971. '10',
  4972. '/option',
  4973. $minutesRegex,
  4974. '/select',
  4975. );
  4976. $this->assertTags($result, $expected);
  4977. }
  4978. /**
  4979. * testHour method
  4980. *
  4981. * @return void
  4982. */
  4983. public function testHour() {
  4984. extract($this->dateRegex);
  4985. $result = $this->Form->hour('Model.field', false);
  4986. $expected = array(
  4987. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4988. array('option' => array('value' => '')),
  4989. '/option',
  4990. array('option' => array('value' => '01')),
  4991. '1',
  4992. '/option',
  4993. array('option' => array('value' => '02')),
  4994. '2',
  4995. '/option',
  4996. $hoursRegex,
  4997. '/select',
  4998. );
  4999. $this->assertTags($result, $expected);
  5000. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5001. $result = $this->Form->hour('Model.field', false);
  5002. $expected = array(
  5003. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5004. array('option' => array('value' => '')),
  5005. '/option',
  5006. array('option' => array('value' => '01')),
  5007. '1',
  5008. '/option',
  5009. array('option' => array('value' => '02')),
  5010. '2',
  5011. '/option',
  5012. $hoursRegex,
  5013. array('option' => array('value' => '12', 'selected' => 'selected')),
  5014. '12',
  5015. '/option',
  5016. '/select',
  5017. );
  5018. $this->assertTags($result, $expected);
  5019. $this->Form->request->data['Model']['field'] = '';
  5020. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5021. $expected = array(
  5022. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5023. array('option' => array('value' => '')),
  5024. '/option',
  5025. array('option' => array('value' => '00')),
  5026. '0',
  5027. '/option',
  5028. array('option' => array('value' => '01')),
  5029. '1',
  5030. '/option',
  5031. array('option' => array('value' => '02')),
  5032. '2',
  5033. '/option',
  5034. $hoursRegex,
  5035. array('option' => array('value' => '23', 'selected' => 'selected')),
  5036. '23',
  5037. '/option',
  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->hour('Model.field', true);
  5043. $expected = array(
  5044. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5045. array('option' => array('value' => '')),
  5046. '/option',
  5047. array('option' => array('value' => '00', 'selected' => 'selected')),
  5048. '0',
  5049. '/option',
  5050. array('option' => array('value' => '01')),
  5051. '1',
  5052. '/option',
  5053. array('option' => array('value' => '02')),
  5054. '2',
  5055. '/option',
  5056. $hoursRegex,
  5057. '/select',
  5058. );
  5059. $this->assertTags($result, $expected);
  5060. unset($this->Form->request->data['Model']['field']);
  5061. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5062. $thisHour = date('H');
  5063. $optValue = date('G');
  5064. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5065. }
  5066. /**
  5067. * testYear method
  5068. *
  5069. * @return void
  5070. */
  5071. public function testYear() {
  5072. $result = $this->Form->year('Model.field', 2006, 2007);
  5073. $expected = array(
  5074. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5075. array('option' => array('value' => '')),
  5076. '/option',
  5077. array('option' => array('value' => '2007')),
  5078. '2007',
  5079. '/option',
  5080. array('option' => array('value' => '2006')),
  5081. '2006',
  5082. '/option',
  5083. '/select',
  5084. );
  5085. $this->assertTags($result, $expected);
  5086. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5087. $expected = array(
  5088. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5089. array('option' => array('value' => '')),
  5090. '/option',
  5091. array('option' => array('value' => '2006')),
  5092. '2006',
  5093. '/option',
  5094. array('option' => array('value' => '2007')),
  5095. '2007',
  5096. '/option',
  5097. '/select',
  5098. );
  5099. $this->assertTags($result, $expected);
  5100. $this->request->data['Contact']['published'] = '';
  5101. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5102. $expected = array(
  5103. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5104. array('option' => array('value' => '')),
  5105. '/option',
  5106. array('option' => array('value' => '2007')),
  5107. '2007',
  5108. '/option',
  5109. array('option' => array('value' => '2006')),
  5110. '2006',
  5111. '/option',
  5112. '/select',
  5113. );
  5114. $this->assertTags($result, $expected);
  5115. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5116. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5117. $expected = array(
  5118. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5119. array('option' => array('value' => '2007')),
  5120. '2007',
  5121. '/option',
  5122. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5123. '2006',
  5124. '/option',
  5125. '/select',
  5126. );
  5127. $this->assertTags($result, $expected);
  5128. $this->Form->request->data['Contact']['published'] = '';
  5129. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5130. $expected = array(
  5131. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5132. array('option' => array('value' => '')),
  5133. '/option',
  5134. array('option' => array('value' => '2007')),
  5135. '2007',
  5136. '/option',
  5137. array('option' => array('value' => '2006')),
  5138. '2006',
  5139. '/option',
  5140. '/select',
  5141. );
  5142. $this->assertTags($result, $expected);
  5143. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5144. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5145. $expected = array(
  5146. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5147. array('option' => array('value' => '2007')),
  5148. '2007',
  5149. '/option',
  5150. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5151. '2006',
  5152. '/option',
  5153. '/select',
  5154. );
  5155. $this->assertTags($result, $expected);
  5156. $this->Form->request->data['Contact']['published'] = '';
  5157. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5158. $expected = array(
  5159. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5160. array('option' => array('value' => '')),
  5161. '/option',
  5162. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5163. '2007',
  5164. '/option',
  5165. array('option' => array('value' => '2006')),
  5166. '2006',
  5167. '/option',
  5168. '/select',
  5169. );
  5170. $this->assertTags($result, $expected);
  5171. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5172. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5173. $expected = array(
  5174. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5175. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5176. '2007',
  5177. '/option',
  5178. array('option' => array('value' => '2006')),
  5179. '2006',
  5180. '/option',
  5181. '/select',
  5182. );
  5183. $this->assertTags($result, $expected);
  5184. $this->Form->request->data['Contact']['published'] = '';
  5185. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5186. $expected = array(
  5187. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5188. array('option' => array('value' => '2008')),
  5189. '2008',
  5190. '/option',
  5191. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5192. '2007',
  5193. '/option',
  5194. array('option' => array('value' => '2006')),
  5195. '2006',
  5196. '/option',
  5197. '/select',
  5198. );
  5199. $this->assertTags($result, $expected);
  5200. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5201. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5202. $expected = array(
  5203. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5204. array('option' => array('value' => '2008')),
  5205. '2008',
  5206. '/option',
  5207. array('option' => array('value' => '2007')),
  5208. '2007',
  5209. '/option',
  5210. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5211. '2006',
  5212. '/option',
  5213. '/select',
  5214. );
  5215. $this->assertTags($result, $expected);
  5216. $this->Form->request->data = array();
  5217. $this->Form->create('Contact');
  5218. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5219. $expected = array(
  5220. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5221. array('option' => array('value' => '2008')),
  5222. '2008',
  5223. '/option',
  5224. array('option' => array('value' => '2007')),
  5225. '2007',
  5226. '/option',
  5227. array('option' => array('value' => '2006')),
  5228. '2006',
  5229. '/option',
  5230. '/select',
  5231. );
  5232. $this->assertTags($result, $expected);
  5233. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5234. $this->assertContains('data[Contact][published][year]', $result);
  5235. }
  5236. /**
  5237. * testTextArea method
  5238. *
  5239. * @return void
  5240. */
  5241. public function testTextArea() {
  5242. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5243. $result = $this->Form->textarea('Model.field');
  5244. $expected = array(
  5245. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5246. 'some test data',
  5247. '/textarea',
  5248. );
  5249. $this->assertTags($result, $expected);
  5250. $result = $this->Form->textarea('Model.tmp');
  5251. $expected = array(
  5252. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5253. '/textarea',
  5254. );
  5255. $this->assertTags($result, $expected);
  5256. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5257. $result = $this->Form->textarea('Model.field');
  5258. $expected = array(
  5259. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5260. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5261. '/textarea',
  5262. );
  5263. $this->assertTags($result, $expected);
  5264. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5265. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5266. $expected = array(
  5267. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5268. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5269. '/textarea',
  5270. );
  5271. $this->assertTags($result, $expected);
  5272. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5273. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5274. $expected = array(
  5275. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5276. '/textarea'
  5277. );
  5278. $this->assertTags($result, $expected);
  5279. }
  5280. /**
  5281. * testTextAreaWithStupidCharacters method
  5282. *
  5283. * test text area with non-ascii characters
  5284. *
  5285. * @return void
  5286. */
  5287. public function testTextAreaWithStupidCharacters() {
  5288. $this->loadFixtures('Post');
  5289. $result = $this->Form->input('Post.content', array(
  5290. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5291. ));
  5292. $expected = array(
  5293. 'div' => array('class' => 'input text'),
  5294. 'label' => array('for' => 'PostContent'),
  5295. 'Current Text',
  5296. '/label',
  5297. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5298. 'GREAT®',
  5299. '/textarea',
  5300. '/div'
  5301. );
  5302. $this->assertTags($result, $expected);
  5303. }
  5304. /**
  5305. * testHiddenField method
  5306. *
  5307. * @return void
  5308. */
  5309. public function testHiddenField() {
  5310. $Contact = ClassRegistry::getObject('Contact');
  5311. $Contact->validationErrors['field'] = 1;
  5312. $this->Form->request->data['Contact']['field'] = 'test';
  5313. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5314. $this->assertTags($result, array(
  5315. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5316. );
  5317. }
  5318. /**
  5319. * testFileUploadField method
  5320. *
  5321. * @return void
  5322. */
  5323. public function testFileUploadField() {
  5324. $result = $this->Form->file('Model.upload');
  5325. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5326. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5327. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5328. $expected = array(
  5329. 'div' => array('class' => 'input file'),
  5330. 'label' => array('for' => 'ModelUpload'),
  5331. 'Upload',
  5332. '/label',
  5333. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5334. '/div'
  5335. );
  5336. $this->assertTags($result, $expected);
  5337. }
  5338. /**
  5339. * test File upload input on a model not used in create();
  5340. *
  5341. * @return void
  5342. */
  5343. public function testFileUploadOnOtherModel() {
  5344. $this->Form->create('ValidateUser', array('type' => 'file'));
  5345. $result = $this->Form->file('ValidateProfile.city');
  5346. $expected = array(
  5347. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5348. );
  5349. $this->assertTags($result, $expected);
  5350. }
  5351. /**
  5352. * testButton method
  5353. *
  5354. * @return void
  5355. */
  5356. public function testButton() {
  5357. $result = $this->Form->button('Hi');
  5358. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5359. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5360. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5361. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5362. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5363. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5364. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5365. $result = $this->Form->button('No type', array('type' => false));
  5366. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5367. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5368. $this->assertNotRegExp('/\&039/', $result);
  5369. }
  5370. /**
  5371. * Test that button() makes unlocked fields by default.
  5372. *
  5373. * @return void
  5374. */
  5375. public function testButtonUnlockedByDefault() {
  5376. $this->Form->request->params['_Token']['key'] = 'secured';
  5377. $this->Form->button('Save', array('name' => 'save'));
  5378. $this->Form->button('Clear');
  5379. $result = $this->Form->unlockField();
  5380. $this->assertEquals(array('save'), $result);
  5381. }
  5382. /**
  5383. * testPostButton method
  5384. *
  5385. * @return void
  5386. */
  5387. public function testPostButton() {
  5388. $result = $this->Form->postButton('Hi', '/controller/action');
  5389. $this->assertTags($result, array(
  5390. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5391. 'div' => array('style' => 'display:none;'),
  5392. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5393. '/div',
  5394. 'button' => array('type' => 'submit'),
  5395. 'Hi',
  5396. '/button',
  5397. '/form'
  5398. ));
  5399. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5400. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5401. }
  5402. /**
  5403. * Test that postButton adds _Token fields.
  5404. *
  5405. * @return void
  5406. */
  5407. public function testSecurePostButton() {
  5408. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5409. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5410. $expected = array(
  5411. 'form' => array(
  5412. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5413. ),
  5414. array('div' => array('style' => 'display:none;')),
  5415. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5416. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5417. '/div',
  5418. 'button' => array('type' => 'submit'),
  5419. 'Delete',
  5420. '/button',
  5421. array('div' => array('style' => 'display:none;')),
  5422. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5423. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5424. '/div',
  5425. '/form',
  5426. );
  5427. $this->assertTags($result, $expected);
  5428. }
  5429. /**
  5430. * testPostLink method
  5431. *
  5432. * @return void
  5433. */
  5434. public function testPostLink() {
  5435. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5436. $this->assertTags($result, array(
  5437. 'form' => array(
  5438. 'method' => 'post', 'action' => '/posts/delete/1',
  5439. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5440. ),
  5441. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5442. '/form',
  5443. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5444. 'Delete',
  5445. '/a'
  5446. ));
  5447. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5448. $this->assertTags($result, array(
  5449. 'form' => array(
  5450. 'method' => 'post', 'action' => '/posts/delete/1',
  5451. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5452. ),
  5453. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5454. '/form',
  5455. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5456. 'Delete',
  5457. '/a'
  5458. ));
  5459. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5460. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5461. }
  5462. /**
  5463. * Test that postLink adds _Token fields.
  5464. *
  5465. * @return void
  5466. */
  5467. public function testSecurePostLink() {
  5468. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5469. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5470. $expected = array(
  5471. 'form' => array(
  5472. 'method' => 'post', 'action' => '/posts/delete/1',
  5473. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5474. ),
  5475. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5476. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5477. 'div' => array('style' => 'display:none;'),
  5478. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5479. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5480. '/div',
  5481. '/form',
  5482. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5483. 'Delete',
  5484. '/a'
  5485. );
  5486. $this->assertTags($result, $expected);
  5487. }
  5488. /**
  5489. * testSubmitButton method
  5490. *
  5491. * @return void
  5492. */
  5493. public function testSubmitButton() {
  5494. $result = $this->Form->submit('');
  5495. $expected = array(
  5496. 'div' => array('class' => 'submit'),
  5497. 'input' => array('type' => 'submit', 'value' => ''),
  5498. '/div'
  5499. );
  5500. $this->assertTags($result, $expected);
  5501. $result = $this->Form->submit('Test Submit');
  5502. $expected = array(
  5503. 'div' => array('class' => 'submit'),
  5504. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5505. '/div'
  5506. );
  5507. $this->assertTags($result, $expected);
  5508. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5509. $expected = array(
  5510. 'span' => array('class' => 'submit'),
  5511. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5512. '/span'
  5513. );
  5514. $this->assertTags($result, $expected);
  5515. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5516. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5517. $this->assertTags($result, $expected);
  5518. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5519. $expected = array(
  5520. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5521. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5522. '/div'
  5523. );
  5524. $this->assertTags($result, $expected);
  5525. $result = $this->Form->submit('Next >');
  5526. $expected = array(
  5527. 'div' => array('class' => 'submit'),
  5528. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5529. '/div'
  5530. );
  5531. $this->assertTags($result, $expected);
  5532. $result = $this->Form->submit('Next >', array('escape' => false));
  5533. $expected = array(
  5534. 'div' => array('class' => 'submit'),
  5535. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5536. '/div'
  5537. );
  5538. $this->assertTags($result, $expected);
  5539. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5540. $expected = array(
  5541. 'div' => array('class' => 'submit'),
  5542. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5543. '/div'
  5544. );
  5545. $this->assertTags($result, $expected);
  5546. $before = '--before--';
  5547. $after = '--after--';
  5548. $result = $this->Form->submit('Test', array('before' => $before));
  5549. $expected = array(
  5550. 'div' => array('class' => 'submit'),
  5551. '--before--',
  5552. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5553. '/div'
  5554. );
  5555. $this->assertTags($result, $expected);
  5556. $result = $this->Form->submit('Test', array('after' => $after));
  5557. $expected = array(
  5558. 'div' => array('class' => 'submit'),
  5559. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5560. '--after--',
  5561. '/div'
  5562. );
  5563. $this->assertTags($result, $expected);
  5564. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5565. $expected = array(
  5566. 'div' => array('class' => 'submit'),
  5567. '--before--',
  5568. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5569. '--after--',
  5570. '/div'
  5571. );
  5572. $this->assertTags($result, $expected);
  5573. }
  5574. /**
  5575. * test image submit types.
  5576. *
  5577. * @return void
  5578. */
  5579. public function testSubmitImage() {
  5580. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5581. $expected = array(
  5582. 'div' => array('class' => 'submit'),
  5583. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5584. '/div'
  5585. );
  5586. $this->assertTags($result, $expected);
  5587. $result = $this->Form->submit('/relative/cake.power.gif');
  5588. $expected = array(
  5589. 'div' => array('class' => 'submit'),
  5590. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5591. '/div'
  5592. );
  5593. $this->assertTags($result, $expected);
  5594. $result = $this->Form->submit('cake.power.gif');
  5595. $expected = array(
  5596. 'div' => array('class' => 'submit'),
  5597. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5598. '/div'
  5599. );
  5600. $this->assertTags($result, $expected);
  5601. $result = $this->Form->submit('Not.an.image');
  5602. $expected = array(
  5603. 'div' => array('class' => 'submit'),
  5604. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5605. '/div'
  5606. );
  5607. $this->assertTags($result, $expected);
  5608. $after = '--after--';
  5609. $before = '--before--';
  5610. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5611. $expected = array(
  5612. 'div' => array('class' => 'submit'),
  5613. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5614. '--after--',
  5615. '/div'
  5616. );
  5617. $this->assertTags($result, $expected);
  5618. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5619. $expected = array(
  5620. 'div' => array('class' => 'submit'),
  5621. '--before--',
  5622. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5623. '/div'
  5624. );
  5625. $this->assertTags($result, $expected);
  5626. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5627. $expected = array(
  5628. 'div' => array('class' => 'submit'),
  5629. '--before--',
  5630. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5631. '--after--',
  5632. '/div'
  5633. );
  5634. $this->assertTags($result, $expected);
  5635. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5636. $expected = array(
  5637. 'div' => array('class' => 'submit'),
  5638. '--before--',
  5639. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5640. '--after--',
  5641. '/div'
  5642. );
  5643. $this->assertTags($result, $expected);
  5644. }
  5645. /**
  5646. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5647. * be submitted at a time.
  5648. *
  5649. * @return void
  5650. */
  5651. public function testSubmitUnlockedByDefault() {
  5652. $this->Form->request->params['_Token']['key'] = 'secured';
  5653. $this->Form->submit('Go go');
  5654. $this->Form->submit('Save', array('name' => 'save'));
  5655. $result = $this->Form->unlockField();
  5656. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5657. }
  5658. /**
  5659. * Test submit image with timestamps.
  5660. *
  5661. * @return void
  5662. */
  5663. public function testSubmitImageTimestamp() {
  5664. Configure::write('Asset.timestamp', 'force');
  5665. $result = $this->Form->submit('cake.power.gif');
  5666. $expected = array(
  5667. 'div' => array('class' => 'submit'),
  5668. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5669. '/div'
  5670. );
  5671. $this->assertTags($result, $expected);
  5672. }
  5673. /**
  5674. * test the create() method
  5675. *
  5676. * @return void
  5677. */
  5678. public function testCreate() {
  5679. $result = $this->Form->create('Contact');
  5680. $encoding = strtolower(Configure::read('App.encoding'));
  5681. $expected = array(
  5682. 'form' => array(
  5683. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5684. 'accept-charset' => $encoding
  5685. ),
  5686. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5687. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5688. '/div'
  5689. );
  5690. $this->assertTags($result, $expected);
  5691. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5692. $expected = array('form' => array(
  5693. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5694. 'accept-charset' => $encoding
  5695. ));
  5696. $this->assertTags($result, $expected);
  5697. $result = $this->Form->create('Contact', array('type' => 'get'));
  5698. $expected = array('form' => array(
  5699. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5700. 'accept-charset' => $encoding
  5701. ));
  5702. $this->assertTags($result, $expected);
  5703. $result = $this->Form->create('Contact', array('type' => 'put'));
  5704. $expected = array(
  5705. 'form' => array(
  5706. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5707. 'accept-charset' => $encoding
  5708. ),
  5709. 'div' => array('style' => 'display:none;'),
  5710. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5711. '/div'
  5712. );
  5713. $this->assertTags($result, $expected);
  5714. $result = $this->Form->create('Contact', array('type' => 'file'));
  5715. $expected = array(
  5716. 'form' => array(
  5717. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5718. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5719. ),
  5720. 'div' => array('style' => 'display:none;'),
  5721. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5722. '/div'
  5723. );
  5724. $this->assertTags($result, $expected);
  5725. $this->Form->request->data['Contact']['id'] = 1;
  5726. $this->Form->request->here = '/contacts/edit/1';
  5727. $this->Form->request['action'] = 'edit';
  5728. $result = $this->Form->create('Contact');
  5729. $expected = array(
  5730. 'form' => array(
  5731. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5732. 'accept-charset' => $encoding
  5733. ),
  5734. 'div' => array('style' => 'display:none;'),
  5735. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5736. '/div'
  5737. );
  5738. $this->assertTags($result, $expected);
  5739. $this->Form->request->data['Contact']['id'] = 1;
  5740. $this->Form->request->here = '/contacts/edit/1';
  5741. $this->Form->request['action'] = 'edit';
  5742. $result = $this->Form->create('Contact', array('type' => 'file'));
  5743. $expected = array(
  5744. 'form' => array(
  5745. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5746. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5747. ),
  5748. 'div' => array('style' => 'display:none;'),
  5749. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5750. '/div'
  5751. );
  5752. $this->assertTags($result, $expected);
  5753. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5754. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5755. $expected = array(
  5756. 'form' => array(
  5757. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5758. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5759. ),
  5760. 'div' => array('style' => 'display:none;'),
  5761. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5762. '/div'
  5763. );
  5764. $this->assertTags($result, $expected);
  5765. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5766. $expected = array(
  5767. 'form' => array(
  5768. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5769. 'accept-charset' => $encoding
  5770. ),
  5771. 'div' => array('style' => 'display:none;'),
  5772. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5773. '/div'
  5774. );
  5775. $this->assertTags($result, $expected);
  5776. $this->Form->request['action'] = 'add';
  5777. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5778. $expected = array(
  5779. 'form' => array(
  5780. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5781. 'accept-charset' => $encoding
  5782. ),
  5783. 'div' => array('style' => 'display:none;'),
  5784. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5785. '/div'
  5786. );
  5787. $this->assertTags($result, $expected);
  5788. $result = $this->Form->create('User', array('action' => 'login'));
  5789. $expected = array(
  5790. 'form' => array(
  5791. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5792. 'accept-charset' => $encoding
  5793. ),
  5794. 'div' => array('style' => 'display:none;'),
  5795. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5796. '/div'
  5797. );
  5798. $this->assertTags($result, $expected);
  5799. $result = $this->Form->create('User', array('url' => '/users/login'));
  5800. $expected = array(
  5801. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5802. 'div' => array('style' => 'display:none;'),
  5803. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5804. '/div'
  5805. );
  5806. $this->assertTags($result, $expected);
  5807. $this->Form->request['controller'] = 'pages';
  5808. $result = $this->Form->create('User', array('action' => 'signup'));
  5809. $expected = array(
  5810. 'form' => array(
  5811. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5812. 'accept-charset' => $encoding
  5813. ),
  5814. 'div' => array('style' => 'display:none;'),
  5815. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5816. '/div'
  5817. );
  5818. $this->assertTags($result, $expected);
  5819. $this->Form->request->data = array();
  5820. $this->Form->request['controller'] = 'contacts';
  5821. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5822. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5823. $expected = array(
  5824. 'form' => array(
  5825. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5826. 'accept-charset' => 'utf-8'
  5827. ),
  5828. 'div' => array('style' => 'display:none;'),
  5829. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5830. '/div'
  5831. );
  5832. $this->assertTags($result, $expected);
  5833. }
  5834. /**
  5835. * Test the onsubmit option for create()
  5836. *
  5837. * @return void
  5838. */
  5839. public function testCreateOnSubmit() {
  5840. $this->Form->request->data = array();
  5841. $this->Form->request['controller'] = 'contacts';
  5842. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5843. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5844. $expected = array(
  5845. 'form' => array(
  5846. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5847. 'accept-charset' => 'utf-8'
  5848. ),
  5849. 'div' => array('style' => 'display:none;'),
  5850. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5851. '/div'
  5852. );
  5853. $this->assertTags($result, $expected);
  5854. $this->Form->request->data = array();
  5855. $this->Form->request['controller'] = 'contacts';
  5856. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5857. $result = $this->Form->create(array(
  5858. 'url' => array('action' => 'index', 'param'),
  5859. 'default' => false,
  5860. 'onsubmit' => 'someFunction();'
  5861. ));
  5862. $expected = array(
  5863. 'form' => array(
  5864. 'id' => 'ContactAddForm', 'method' => 'post',
  5865. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  5866. 'action' => '/contacts/index/param',
  5867. 'accept-charset' => 'utf-8'
  5868. ),
  5869. 'div' => array('style' => 'display:none;'),
  5870. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5871. '/div'
  5872. );
  5873. $this->assertTags($result, $expected);
  5874. }
  5875. /**
  5876. * test create() with automatic url generation
  5877. *
  5878. * @return void
  5879. */
  5880. public function testCreateAutoUrl() {
  5881. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  5882. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  5883. $this->Form->request->base = '/base_url';
  5884. $result = $this->Form->create('Contact');
  5885. $expected = array(
  5886. 'form' => array(
  5887. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  5888. 'accept-charset' => 'utf-8'
  5889. ),
  5890. 'div' => array('style' => 'display:none;'),
  5891. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5892. '/div'
  5893. );
  5894. $this->assertTags($result, $expected);
  5895. $this->Form->request['action'] = 'delete';
  5896. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  5897. $this->Form->request->base = '/base_url';
  5898. $result = $this->Form->create('Contact');
  5899. $expected = array(
  5900. 'form' => array(
  5901. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  5902. 'accept-charset' => 'utf-8'
  5903. ),
  5904. 'div' => array('style' => 'display:none;'),
  5905. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5906. '/div'
  5907. );
  5908. $this->assertTags($result, $expected);
  5909. }
  5910. /**
  5911. * test create() with a custom route
  5912. *
  5913. * @return void
  5914. */
  5915. public function testCreateCustomRoute() {
  5916. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  5917. $encoding = strtolower(Configure::read('App.encoding'));
  5918. $result = $this->Form->create('User', array('action' => 'login'));
  5919. $expected = array(
  5920. 'form' => array(
  5921. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  5922. 'accept-charset' => $encoding
  5923. ),
  5924. 'div' => array('style' => 'display:none;'),
  5925. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5926. '/div'
  5927. );
  5928. $this->assertTags($result, $expected);
  5929. }
  5930. /**
  5931. * test that inputDefaults are stored and used.
  5932. *
  5933. * @return void
  5934. */
  5935. public function testCreateWithInputDefaults() {
  5936. $this->Form->create('User', array(
  5937. 'inputDefaults' => array(
  5938. 'div' => false,
  5939. 'label' => false,
  5940. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  5941. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  5942. )
  5943. ));
  5944. $result = $this->Form->input('username');
  5945. $expected = array(
  5946. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  5947. );
  5948. $this->assertTags($result, $expected);
  5949. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  5950. $expected = array(
  5951. 'div' => array('class' => 'input text'),
  5952. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  5953. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  5954. '/div'
  5955. );
  5956. $this->assertTags($result, $expected);
  5957. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  5958. $expected = array(
  5959. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  5960. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  5961. );
  5962. $this->assertTags($result, $expected);
  5963. $this->Form->create('User', array(
  5964. 'inputDefaults' => array(
  5965. 'div' => false,
  5966. 'label' => array('class' => 'nice', 'for' => 'changed'),
  5967. )
  5968. ));
  5969. $result = $this->Form->input('username', array('div' => true));
  5970. $expected = array(
  5971. 'div' => array('class' => 'input text'),
  5972. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  5973. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. }
  5978. /**
  5979. * test automatic accept-charset overriding
  5980. *
  5981. * @return void
  5982. */
  5983. public function testCreateWithAcceptCharset() {
  5984. $result = $this->Form->create('UserForm', array(
  5985. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  5986. )
  5987. );
  5988. $expected = array(
  5989. 'form' => array(
  5990. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  5991. 'accept-charset' => 'iso-8859-1'
  5992. ),
  5993. 'div' => array('style' => 'display:none;'),
  5994. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5995. '/div'
  5996. );
  5997. $this->assertTags($result, $expected);
  5998. }
  5999. /**
  6000. * Test base form url when url param is passed with multiple parameters (&)
  6001. *
  6002. */
  6003. public function testCreateQuerystringrequest() {
  6004. $encoding = strtolower(Configure::read('App.encoding'));
  6005. $result = $this->Form->create('Contact', array(
  6006. 'type' => 'post',
  6007. 'escape' => false,
  6008. 'url' => array(
  6009. 'controller' => 'controller',
  6010. 'action' => 'action',
  6011. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6012. )
  6013. ));
  6014. $expected = array(
  6015. 'form' => array(
  6016. 'id' => 'ContactAddForm',
  6017. 'method' => 'post',
  6018. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6019. 'accept-charset' => $encoding
  6020. ),
  6021. 'div' => array('style' => 'display:none;'),
  6022. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6023. '/div'
  6024. );
  6025. $this->assertTags($result, $expected);
  6026. $result = $this->Form->create('Contact', array(
  6027. 'type' => 'post',
  6028. 'url' => array(
  6029. 'controller' => 'controller',
  6030. 'action' => 'action',
  6031. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6032. )
  6033. ));
  6034. $expected = array(
  6035. 'form' => array(
  6036. 'id' => 'ContactAddForm',
  6037. 'method' => 'post',
  6038. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6039. 'accept-charset' => $encoding
  6040. ),
  6041. 'div' => array('style' => 'display:none;'),
  6042. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6043. '/div'
  6044. );
  6045. $this->assertTags($result, $expected);
  6046. }
  6047. /**
  6048. * test that create() doesn't cause errors by multiple id's being in the primary key
  6049. * as could happen with multiple select or checkboxes.
  6050. *
  6051. * @return void
  6052. */
  6053. public function testCreateWithMultipleIdInData() {
  6054. $encoding = strtolower(Configure::read('App.encoding'));
  6055. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6056. $result = $this->Form->create('Contact');
  6057. $expected = array(
  6058. 'form' => array(
  6059. 'id' => 'ContactAddForm',
  6060. 'method' => 'post',
  6061. 'action' => '/contacts/add',
  6062. 'accept-charset' => $encoding
  6063. ),
  6064. 'div' => array('style' => 'display:none;'),
  6065. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6066. '/div'
  6067. );
  6068. $this->assertTags($result, $expected);
  6069. }
  6070. /**
  6071. * test that create() doesn't add in extra passed params.
  6072. *
  6073. * @return void
  6074. */
  6075. public function testCreatePassedArgs() {
  6076. $encoding = strtolower(Configure::read('App.encoding'));
  6077. $this->Form->request->data['Contact']['id'] = 1;
  6078. $result = $this->Form->create('Contact', array(
  6079. 'type' => 'post',
  6080. 'escape' => false,
  6081. 'url' => array(
  6082. 'action' => 'edit',
  6083. 'myparam'
  6084. )
  6085. ));
  6086. $expected = array(
  6087. 'form' => array(
  6088. 'id' => 'ContactAddForm',
  6089. 'method' => 'post',
  6090. 'action' => '/contacts/edit/myparam',
  6091. 'accept-charset' => $encoding
  6092. ),
  6093. 'div' => array('style' => 'display:none;'),
  6094. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6095. '/div'
  6096. );
  6097. $this->assertTags($result, $expected);
  6098. }
  6099. /**
  6100. * test creating a get form, and get form inputs.
  6101. *
  6102. * @return void
  6103. */
  6104. public function testGetFormCreate() {
  6105. $encoding = strtolower(Configure::read('App.encoding'));
  6106. $result = $this->Form->create('Contact', array('type' => 'get'));
  6107. $this->assertTags($result, array('form' => array(
  6108. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6109. 'accept-charset' => $encoding
  6110. )));
  6111. $result = $this->Form->text('Contact.name');
  6112. $this->assertTags($result, array('input' => array(
  6113. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6114. )));
  6115. $result = $this->Form->password('password');
  6116. $this->assertTags($result, array('input' => array(
  6117. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6118. )));
  6119. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6120. $result = $this->Form->text('user_form');
  6121. $this->assertTags($result, array('input' => array(
  6122. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6123. )));
  6124. }
  6125. /**
  6126. * test get form, and inputs when the model param is false
  6127. *
  6128. * @return void
  6129. */
  6130. public function testGetFormWithFalseModel() {
  6131. $encoding = strtolower(Configure::read('App.encoding'));
  6132. $this->Form->request['controller'] = 'contact_test';
  6133. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6134. $expected = array('form' => array(
  6135. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6136. 'accept-charset' => $encoding
  6137. ));
  6138. $this->assertTags($result, $expected);
  6139. $result = $this->Form->text('reason');
  6140. $expected = array(
  6141. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6142. );
  6143. $this->assertTags($result, $expected);
  6144. }
  6145. /**
  6146. * test that datetime() works with GET style forms.
  6147. *
  6148. * @return void
  6149. */
  6150. public function testDateTimeWithGetForms() {
  6151. extract($this->dateRegex);
  6152. $this->Form->create('Contact', array('type' => 'get'));
  6153. $result = $this->Form->datetime('created');
  6154. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6155. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6156. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6157. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6158. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6159. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6160. }
  6161. /**
  6162. * testEditFormWithData method
  6163. *
  6164. * test auto populating form elements from submitted data.
  6165. *
  6166. * @return void
  6167. */
  6168. public function testEditFormWithData() {
  6169. $this->Form->request->data = array('Person' => array(
  6170. 'id' => 1,
  6171. 'first_name' => 'Nate',
  6172. 'last_name' => 'Abele',
  6173. 'email' => 'nate@example.com'
  6174. ));
  6175. $this->Form->request->addParams(array(
  6176. 'models' => array('Person'),
  6177. 'controller' => 'people',
  6178. 'action' => 'add'
  6179. ));
  6180. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6181. $this->Form->create();
  6182. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6183. $expected = array(
  6184. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6185. 'select' => array(
  6186. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6187. ),
  6188. array('option' => array('value' => 1)), 'Nate', '/option',
  6189. array('option' => array('value' => 2)), 'Garrett', '/option',
  6190. array('option' => array('value' => 3)), 'Larry', '/option',
  6191. '/select'
  6192. );
  6193. $this->assertTags($result, $expected);
  6194. }
  6195. /**
  6196. * Test that required fields are created for various types of validation.
  6197. *
  6198. * @return void
  6199. */
  6200. public function testFormInputRequiredDetection() {
  6201. $this->Form->create('Contact');
  6202. $result = $this->Form->input('Contact.non_existing');
  6203. $expected = array(
  6204. 'div' => array('class' => 'input text required'),
  6205. 'label' => array('for' => 'ContactNonExisting'),
  6206. 'Non Existing',
  6207. '/label',
  6208. 'input' => array(
  6209. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6210. 'id' => 'ContactNonExisting'
  6211. ),
  6212. '/div'
  6213. );
  6214. $this->assertTags($result, $expected);
  6215. $result = $this->Form->input('Contact.imrequired');
  6216. $expected = array(
  6217. 'div' => array('class' => 'input text required'),
  6218. 'label' => array('for' => 'ContactImrequired'),
  6219. 'Imrequired',
  6220. '/label',
  6221. 'input' => array(
  6222. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6223. 'id' => 'ContactImrequired'
  6224. ),
  6225. '/div'
  6226. );
  6227. $this->assertTags($result, $expected);
  6228. $result = $this->Form->input('Contact.imalsorequired');
  6229. $expected = array(
  6230. 'div' => array('class' => 'input text required'),
  6231. 'label' => array('for' => 'ContactImalsorequired'),
  6232. 'Imalsorequired',
  6233. '/label',
  6234. 'input' => array(
  6235. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6236. 'id' => 'ContactImalsorequired'
  6237. ),
  6238. '/div'
  6239. );
  6240. $this->assertTags($result, $expected);
  6241. $result = $this->Form->input('Contact.imrequiredtoo');
  6242. $expected = array(
  6243. 'div' => array('class' => 'input text required'),
  6244. 'label' => array('for' => 'ContactImrequiredtoo'),
  6245. 'Imrequiredtoo',
  6246. '/label',
  6247. 'input' => array(
  6248. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6249. 'id' => 'ContactImrequiredtoo'
  6250. ),
  6251. '/div'
  6252. );
  6253. $this->assertTags($result, $expected);
  6254. $result = $this->Form->input('Contact.required_one');
  6255. $expected = array(
  6256. 'div' => array('class' => 'input text required'),
  6257. 'label' => array('for' => 'ContactRequiredOne'),
  6258. 'Required One',
  6259. '/label',
  6260. 'input' => array(
  6261. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6262. 'id' => 'ContactRequiredOne'
  6263. ),
  6264. '/div'
  6265. );
  6266. $this->assertTags($result, $expected);
  6267. $result = $this->Form->input('Contact.string_required');
  6268. $expected = array(
  6269. 'div' => array('class' => 'input text required'),
  6270. 'label' => array('for' => 'ContactStringRequired'),
  6271. 'String Required',
  6272. '/label',
  6273. 'input' => array(
  6274. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6275. 'id' => 'ContactStringRequired'
  6276. ),
  6277. '/div'
  6278. );
  6279. $this->assertTags($result, $expected);
  6280. $result = $this->Form->input('Contact.imnotrequired');
  6281. $expected = array(
  6282. 'div' => array('class' => 'input text'),
  6283. 'label' => array('for' => 'ContactImnotrequired'),
  6284. 'Imnotrequired',
  6285. '/label',
  6286. 'input' => array(
  6287. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6288. 'id' => 'ContactImnotrequired'
  6289. ),
  6290. '/div'
  6291. );
  6292. $this->assertTags($result, $expected);
  6293. $result = $this->Form->input('Contact.imalsonotrequired');
  6294. $expected = array(
  6295. 'div' => array('class' => 'input text'),
  6296. 'label' => array('for' => 'ContactImalsonotrequired'),
  6297. 'Imalsonotrequired',
  6298. '/label',
  6299. 'input' => array(
  6300. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6301. 'id' => 'ContactImalsonotrequired'
  6302. ),
  6303. '/div'
  6304. );
  6305. $this->assertTags($result, $expected);
  6306. $result = $this->Form->input('Contact.imnotrequiredeither');
  6307. $expected = array(
  6308. 'div' => array('class' => 'input text'),
  6309. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6310. 'Imnotrequiredeither',
  6311. '/label',
  6312. 'input' => array(
  6313. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6314. 'id' => 'ContactImnotrequiredeither'
  6315. ),
  6316. '/div'
  6317. );
  6318. $this->assertTags($result, $expected);
  6319. }
  6320. /**
  6321. * testFormMagicInput method
  6322. *
  6323. * @return void
  6324. */
  6325. public function testFormMagicInput() {
  6326. $encoding = strtolower(Configure::read('App.encoding'));
  6327. $result = $this->Form->create('Contact');
  6328. $expected = array(
  6329. 'form' => array(
  6330. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6331. 'accept-charset' => $encoding
  6332. ),
  6333. 'div' => array('style' => 'display:none;'),
  6334. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6335. '/div'
  6336. );
  6337. $this->assertTags($result, $expected);
  6338. $result = $this->Form->input('name');
  6339. $expected = array(
  6340. 'div' => array('class' => 'input text'),
  6341. 'label' => array('for' => 'ContactName'),
  6342. 'Name',
  6343. '/label',
  6344. 'input' => array(
  6345. 'type' => 'text', 'name' => 'data[Contact][name]',
  6346. 'id' => 'ContactName', 'maxlength' => '255'
  6347. ),
  6348. '/div'
  6349. );
  6350. $this->assertTags($result, $expected);
  6351. $result = $this->Form->input('non_existing_field_in_contact_model');
  6352. $expected = array(
  6353. 'div' => array('class' => 'input text'),
  6354. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6355. 'Non Existing Field In Contact Model',
  6356. '/label',
  6357. 'input' => array(
  6358. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6359. 'id' => 'ContactNonExistingFieldInContactModel'
  6360. ),
  6361. '/div'
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. $result = $this->Form->input('Address.street');
  6365. $expected = array(
  6366. 'div' => array('class' => 'input text'),
  6367. 'label' => array('for' => 'AddressStreet'),
  6368. 'Street',
  6369. '/label',
  6370. 'input' => array(
  6371. 'type' => 'text', 'name' => 'data[Address][street]',
  6372. 'id' => 'AddressStreet'
  6373. ),
  6374. '/div'
  6375. );
  6376. $this->assertTags($result, $expected);
  6377. $result = $this->Form->input('Address.non_existing_field_in_model');
  6378. $expected = array(
  6379. 'div' => array('class' => 'input text'),
  6380. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6381. 'Non Existing Field In Model',
  6382. '/label',
  6383. 'input' => array(
  6384. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6385. 'id' => 'AddressNonExistingFieldInModel'
  6386. ),
  6387. '/div'
  6388. );
  6389. $this->assertTags($result, $expected);
  6390. $result = $this->Form->input('name', array('div' => false));
  6391. $expected = array(
  6392. 'label' => array('for' => 'ContactName'),
  6393. 'Name',
  6394. '/label',
  6395. 'input' => array(
  6396. 'type' => 'text', 'name' => 'data[Contact][name]',
  6397. 'id' => 'ContactName', 'maxlength' => '255'
  6398. )
  6399. );
  6400. $this->assertTags($result, $expected);
  6401. extract($this->dateRegex);
  6402. $now = strtotime('now');
  6403. $result = $this->Form->input('Contact.published', array('div' => false));
  6404. $expected = array(
  6405. 'label' => array('for' => 'ContactPublishedMonth'),
  6406. 'Published',
  6407. '/label',
  6408. array('select' => array(
  6409. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6410. )),
  6411. $monthsRegex,
  6412. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6413. date('F', $now),
  6414. '/option',
  6415. '*/select',
  6416. '-',
  6417. array('select' => array(
  6418. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6419. )),
  6420. $daysRegex,
  6421. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6422. date('j', $now),
  6423. '/option',
  6424. '*/select',
  6425. '-',
  6426. array('select' => array(
  6427. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6428. )),
  6429. $yearsRegex,
  6430. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6431. date('Y', $now),
  6432. '*/select'
  6433. );
  6434. $this->assertTags($result, $expected);
  6435. $result = $this->Form->input('Contact.updated', array('div' => false));
  6436. $expected = array(
  6437. 'label' => array('for' => 'ContactUpdatedMonth'),
  6438. 'Updated',
  6439. '/label',
  6440. array('select' => array(
  6441. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6442. )),
  6443. $monthsRegex,
  6444. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6445. date('F', $now),
  6446. '/option',
  6447. '*/select',
  6448. '-',
  6449. array('select' => array(
  6450. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6451. )),
  6452. $daysRegex,
  6453. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6454. date('j', $now),
  6455. '/option',
  6456. '*/select',
  6457. '-',
  6458. array('select' => array(
  6459. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6460. )),
  6461. $yearsRegex,
  6462. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6463. date('Y', $now),
  6464. '*/select'
  6465. );
  6466. $this->assertTags($result, $expected);
  6467. $result = $this->Form->input('UserForm.stuff');
  6468. $expected = array(
  6469. 'div' => array('class' => 'input text'),
  6470. 'label' => array('for' => 'UserFormStuff'),
  6471. 'Stuff',
  6472. '/label',
  6473. 'input' => array(
  6474. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6475. 'id' => 'UserFormStuff', 'maxlength' => 10
  6476. ),
  6477. '/div'
  6478. );
  6479. $this->assertTags($result, $expected);
  6480. }
  6481. /**
  6482. * testForMagicInputNonExistingNorValidated method
  6483. *
  6484. * @return void
  6485. */
  6486. public function testForMagicInputNonExistingNorValidated() {
  6487. $encoding = strtolower(Configure::read('App.encoding'));
  6488. $result = $this->Form->create('Contact');
  6489. $expected = array(
  6490. 'form' => array(
  6491. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6492. 'accept-charset' => $encoding
  6493. ),
  6494. 'div' => array('style' => 'display:none;'),
  6495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6496. '/div'
  6497. );
  6498. $this->assertTags($result, $expected);
  6499. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6500. $expected = array(
  6501. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6502. 'Non Existing Nor Validated',
  6503. '/label',
  6504. 'input' => array(
  6505. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6506. 'id' => 'ContactNonExistingNorValidated'
  6507. )
  6508. );
  6509. $this->assertTags($result, $expected);
  6510. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6511. 'div' => false, 'value' => 'my value'
  6512. ));
  6513. $expected = array(
  6514. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6515. 'Non Existing Nor Validated',
  6516. '/label',
  6517. 'input' => array(
  6518. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6519. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6520. )
  6521. );
  6522. $this->assertTags($result, $expected);
  6523. $this->Form->request->data = array(
  6524. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6525. ));
  6526. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6527. $expected = array(
  6528. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6529. 'Non Existing Nor Validated',
  6530. '/label',
  6531. 'input' => array(
  6532. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6533. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6534. )
  6535. );
  6536. $this->assertTags($result, $expected);
  6537. }
  6538. /**
  6539. * testFormMagicInputLabel method
  6540. *
  6541. * @return void
  6542. */
  6543. public function testFormMagicInputLabel() {
  6544. $encoding = strtolower(Configure::read('App.encoding'));
  6545. $result = $this->Form->create('Contact');
  6546. $expected = array(
  6547. 'form' => array(
  6548. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6549. 'accept-charset' => $encoding
  6550. ),
  6551. 'div' => array('style' => 'display:none;'),
  6552. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6553. '/div'
  6554. );
  6555. $this->assertTags($result, $expected);
  6556. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6557. $this->assertTags($result, array('input' => array(
  6558. 'name' => 'data[Contact][name]', 'type' => 'text',
  6559. 'id' => 'ContactName', 'maxlength' => '255')
  6560. ));
  6561. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6562. $expected = array(
  6563. 'label' => array('for' => 'ContactName'),
  6564. 'My label',
  6565. '/label',
  6566. 'input' => array(
  6567. 'type' => 'text', 'name' => 'data[Contact][name]',
  6568. 'id' => 'ContactName', 'maxlength' => '255'
  6569. )
  6570. );
  6571. $this->assertTags($result, $expected);
  6572. $result = $this->Form->input('Contact.name', array(
  6573. 'div' => false, 'label' => array('class' => 'mandatory')
  6574. ));
  6575. $expected = array(
  6576. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6577. 'Name',
  6578. '/label',
  6579. 'input' => array(
  6580. 'type' => 'text', 'name' => 'data[Contact][name]',
  6581. 'id' => 'ContactName', 'maxlength' => '255'
  6582. )
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. $result = $this->Form->input('Contact.name', array(
  6586. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6587. ));
  6588. $expected = array(
  6589. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6590. 'My label',
  6591. '/label',
  6592. 'input' => array(
  6593. 'type' => 'text', 'name' => 'data[Contact][name]',
  6594. 'id' => 'ContactName', 'maxlength' => '255'
  6595. )
  6596. );
  6597. $this->assertTags($result, $expected);
  6598. $result = $this->Form->input('Contact.name', array(
  6599. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6600. ));
  6601. $expected = array(
  6602. 'label' => array('for' => 'my_id'),
  6603. 'Name',
  6604. '/label',
  6605. 'input' => array(
  6606. 'type' => 'text', 'name' => 'data[Contact][name]',
  6607. 'id' => 'my_id', 'maxlength' => '255'
  6608. )
  6609. );
  6610. $this->assertTags($result, $expected);
  6611. $result = $this->Form->input('1.id');
  6612. $this->assertTags($result, array('input' => array(
  6613. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6614. 'id' => 'Contact1Id'
  6615. )));
  6616. $result = $this->Form->input("1.name");
  6617. $expected = array(
  6618. 'div' => array('class' => 'input text'),
  6619. 'label' => array('for' => 'Contact1Name'),
  6620. 'Name',
  6621. '/label',
  6622. 'input' => array(
  6623. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6624. 'id' => 'Contact1Name', 'maxlength' => '255'
  6625. ),
  6626. '/div'
  6627. );
  6628. $this->assertTags($result, $expected);
  6629. $result = $this->Form->input('Contact.1.id');
  6630. $this->assertTags($result, array(
  6631. 'input' => array(
  6632. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6633. 'id' => 'Contact1Id'
  6634. )
  6635. ));
  6636. $result = $this->Form->input("Model.1.name");
  6637. $expected = array(
  6638. 'div' => array('class' => 'input text'),
  6639. 'label' => array('for' => 'Model1Name'),
  6640. 'Name',
  6641. '/label',
  6642. 'input' => array(
  6643. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6644. 'id' => 'Model1Name'
  6645. ),
  6646. '/div'
  6647. );
  6648. $this->assertTags($result, $expected);
  6649. }
  6650. /**
  6651. * testFormEnd method
  6652. *
  6653. * @return void
  6654. */
  6655. public function testFormEnd() {
  6656. $this->assertEquals('</form>', $this->Form->end());
  6657. $result = $this->Form->end('');
  6658. $expected = array(
  6659. 'div' => array('class' => 'submit'),
  6660. 'input' => array('type' => 'submit', 'value' => ''),
  6661. '/div',
  6662. '/form'
  6663. );
  6664. $this->assertTags($result, $expected);
  6665. $result = $this->Form->end(array('label' => ''));
  6666. $expected = array(
  6667. 'div' => array('class' => 'submit'),
  6668. 'input' => array('type' => 'submit', 'value' => ''),
  6669. '/div',
  6670. '/form'
  6671. );
  6672. $this->assertTags($result, $expected);
  6673. $result = $this->Form->end('save');
  6674. $expected = array(
  6675. 'div' => array('class' => 'submit'),
  6676. 'input' => array('type' => 'submit', 'value' => 'save'),
  6677. '/div',
  6678. '/form'
  6679. );
  6680. $this->assertTags($result, $expected);
  6681. $result = $this->Form->end(array('label' => 'save'));
  6682. $expected = array(
  6683. 'div' => array('class' => 'submit'),
  6684. 'input' => array('type' => 'submit', 'value' => 'save'),
  6685. '/div',
  6686. '/form'
  6687. );
  6688. $this->assertTags($result, $expected);
  6689. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6690. $expected = array(
  6691. 'div' => array('class' => 'submit'),
  6692. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6693. '/div',
  6694. '/form'
  6695. );
  6696. $this->assertTags($result, $expected);
  6697. $result = $this->Form->end(array('name' => 'Whatever'));
  6698. $expected = array(
  6699. 'div' => array('class' => 'submit'),
  6700. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6701. '/div',
  6702. '/form'
  6703. );
  6704. $this->assertTags($result, $expected);
  6705. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6706. $expected = array(
  6707. 'div' => array('class' => 'good'),
  6708. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6709. '/div',
  6710. '/form'
  6711. );
  6712. $this->assertTags($result, $expected);
  6713. $result = $this->Form->end(array(
  6714. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6715. ));
  6716. $expected = array(
  6717. 'div' => array('class' => 'good'),
  6718. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6719. '/div',
  6720. '/form'
  6721. );
  6722. $this->assertTags($result, $expected);
  6723. }
  6724. /**
  6725. * testMultipleFormWithIdFields method
  6726. *
  6727. * @return void
  6728. */
  6729. public function testMultipleFormWithIdFields() {
  6730. $this->Form->create('UserForm');
  6731. $result = $this->Form->input('id');
  6732. $this->assertTags($result, array('input' => array(
  6733. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6734. )));
  6735. $result = $this->Form->input('ValidateItem.id');
  6736. $this->assertTags($result, array('input' => array(
  6737. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6738. 'id' => 'ValidateItemId'
  6739. )));
  6740. $result = $this->Form->input('ValidateUser.id');
  6741. $this->assertTags($result, array('input' => array(
  6742. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6743. 'id' => 'ValidateUserId'
  6744. )));
  6745. }
  6746. /**
  6747. * testDbLessModel method
  6748. *
  6749. * @return void
  6750. */
  6751. public function testDbLessModel() {
  6752. $this->Form->create('TestMail');
  6753. $result = $this->Form->input('name');
  6754. $expected = array(
  6755. 'div' => array('class' => 'input text'),
  6756. 'label' => array('for' => 'TestMailName'),
  6757. 'Name',
  6758. '/label',
  6759. 'input' => array(
  6760. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6761. 'id' => 'TestMailName'
  6762. ),
  6763. '/div'
  6764. );
  6765. $this->assertTags($result, $expected);
  6766. ClassRegistry::init('TestMail');
  6767. $this->Form->create('TestMail');
  6768. $result = $this->Form->input('name');
  6769. $expected = array(
  6770. 'div' => array('class' => 'input text'),
  6771. 'label' => array('for' => 'TestMailName'),
  6772. 'Name',
  6773. '/label',
  6774. 'input' => array(
  6775. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6776. 'id' => 'TestMailName'
  6777. ),
  6778. '/div'
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. }
  6782. /**
  6783. * testBrokenness method
  6784. *
  6785. * @return void
  6786. */
  6787. public function testBrokenness() {
  6788. /*
  6789. * #4 This test has two parents and four children. By default (as of r7117) both
  6790. * parents are show but the first parent is missing a child. This is the inconsistency
  6791. * in the default behaviour - one parent has all children, the other does not - dependent
  6792. * on the data values.
  6793. */
  6794. $result = $this->Form->select('Model.field', array(
  6795. 'Fred' => array(
  6796. 'freds_son_1' => 'Fred',
  6797. 'freds_son_2' => 'Freddie'
  6798. ),
  6799. 'Bert' => array(
  6800. 'berts_son_1' => 'Albert',
  6801. 'berts_son_2' => 'Bertie')
  6802. ),
  6803. array('showParents' => true, 'empty' => false)
  6804. );
  6805. $expected = array(
  6806. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6807. array('optgroup' => array('label' => 'Fred')),
  6808. array('option' => array('value' => 'freds_son_1')),
  6809. 'Fred',
  6810. '/option',
  6811. array('option' => array('value' => 'freds_son_2')),
  6812. 'Freddie',
  6813. '/option',
  6814. '/optgroup',
  6815. array('optgroup' => array('label' => 'Bert')),
  6816. array('option' => array('value' => 'berts_son_1')),
  6817. 'Albert',
  6818. '/option',
  6819. array('option' => array('value' => 'berts_son_2')),
  6820. 'Bertie',
  6821. '/option',
  6822. '/optgroup',
  6823. '/select'
  6824. );
  6825. $this->assertTags($result, $expected);
  6826. /*
  6827. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6828. * changed, so we should expect the same select list data, just with a different name
  6829. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6830. * This is where data corruption can occur, because when a select value is missing from
  6831. * a list a form will substitute the first value in the list - without the user knowing.
  6832. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6833. * affect the availability of 3 => 'Three' as a valid option.
  6834. */
  6835. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6836. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6837. ));
  6838. $result = $this->Form->select(
  6839. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6840. );
  6841. $expected = array(
  6842. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6843. array('option' => array('value' => 1)),
  6844. 'One',
  6845. '/option',
  6846. array('option' => array('value' => 2)),
  6847. 'Two',
  6848. '/option',
  6849. array('optgroup' => array('label' => 'Three')),
  6850. array('option' => array('value' => 3)),
  6851. 'Three',
  6852. '/option',
  6853. array('option' => array('value' => 4)),
  6854. 'Four',
  6855. '/option',
  6856. array('option' => array('value' => 5)),
  6857. 'Five',
  6858. '/option',
  6859. '/optgroup',
  6860. '/select'
  6861. );
  6862. $this->assertTags($result, $expected);
  6863. }
  6864. /**
  6865. * Test the generation of fields for a multi record form.
  6866. *
  6867. * @return void
  6868. */
  6869. public function testMultiRecordForm() {
  6870. $this->Form->create('ValidateProfile');
  6871. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6872. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6873. $expected = array(
  6874. 'div' => array('class' => 'input textarea'),
  6875. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6876. 'Name',
  6877. '/label',
  6878. 'textarea' => array(
  6879. 'id' => 'ValidateProfile1ValidateItem2Name',
  6880. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  6881. 'cols' => 30,
  6882. 'rows' => 6
  6883. ),
  6884. 'Value',
  6885. '/textarea',
  6886. '/div'
  6887. );
  6888. $this->assertTags($result, $expected);
  6889. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  6890. $expected = array(
  6891. 'div' => array('class' => 'input date'),
  6892. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6893. 'Created',
  6894. '/label',
  6895. array('select' => array(
  6896. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  6897. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6898. )
  6899. ),
  6900. array('option' => array('value' => '')), '/option',
  6901. $this->dateRegex['monthsRegex'],
  6902. '/select', '-',
  6903. array('select' => array(
  6904. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  6905. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6906. )
  6907. ),
  6908. array('option' => array('value' => '')), '/option',
  6909. $this->dateRegex['daysRegex'],
  6910. '/select', '-',
  6911. array('select' => array(
  6912. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  6913. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6914. )
  6915. ),
  6916. array('option' => array('value' => '')), '/option',
  6917. $this->dateRegex['yearsRegex'],
  6918. '/select',
  6919. '/div'
  6920. );
  6921. $this->assertTags($result, $expected);
  6922. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  6923. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6924. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6925. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6926. $expected = array(
  6927. 'div' => array('class' => 'input select error'),
  6928. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6929. 'Profile',
  6930. '/label',
  6931. 'select' => array(
  6932. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  6933. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  6934. 'class' => 'form-error'
  6935. ),
  6936. '/select',
  6937. array('div' => array('class' => 'error-message')),
  6938. 'Error',
  6939. '/div',
  6940. '/div'
  6941. );
  6942. $this->assertTags($result, $expected);
  6943. }
  6944. /**
  6945. * test the correct display of multi-record form validation errors.
  6946. *
  6947. * @return void
  6948. */
  6949. public function testMultiRecordFormValidationErrors() {
  6950. $this->Form->create('ValidateProfile');
  6951. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  6952. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6953. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6954. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6955. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6956. $result = $this->Form->error('ValidateProfile.2.city');
  6957. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6958. $result = $this->Form->error('2.city');
  6959. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6960. }
  6961. /**
  6962. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  6963. *
  6964. * @return void
  6965. */
  6966. public function testInputTemplate() {
  6967. $result = $this->Form->input('Contact.email', array(
  6968. 'type' => 'text', 'format' => array('input')
  6969. ));
  6970. $expected = array(
  6971. 'div' => array('class' => 'input text'),
  6972. 'input' => array(
  6973. 'type' => 'text', 'name' => 'data[Contact][email]',
  6974. 'id' => 'ContactEmail'
  6975. ),
  6976. '/div'
  6977. );
  6978. $this->assertTags($result, $expected);
  6979. $result = $this->Form->input('Contact.email', array(
  6980. 'type' => 'text', 'format' => array('input', 'label'),
  6981. 'label' => '<em>Email (required)</em>'
  6982. ));
  6983. $expected = array(
  6984. 'div' => array('class' => 'input text'),
  6985. array('input' => array(
  6986. 'type' => 'text', 'name' => 'data[Contact][email]',
  6987. 'id' => 'ContactEmail'
  6988. )),
  6989. 'label' => array('for' => 'ContactEmail'),
  6990. 'em' => array(),
  6991. 'Email (required)',
  6992. '/em',
  6993. '/label',
  6994. '/div'
  6995. );
  6996. $this->assertTags($result, $expected);
  6997. $result = $this->Form->input('Contact.email', array(
  6998. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  6999. 'between' => '<div>Something in the middle</div>',
  7000. 'after' => '<span>Some text at the end</span>'
  7001. ));
  7002. $expected = array(
  7003. 'div' => array('class' => 'input text'),
  7004. array('input' => array(
  7005. 'type' => 'text', 'name' => 'data[Contact][email]',
  7006. 'id' => 'ContactEmail'
  7007. )),
  7008. array('div' => array()),
  7009. 'Something in the middle',
  7010. '/div',
  7011. 'label' => array('for' => 'ContactEmail'),
  7012. 'Email',
  7013. '/label',
  7014. 'span' => array(),
  7015. 'Some text at the end',
  7016. '/span',
  7017. '/div'
  7018. );
  7019. $this->assertTags($result, $expected);
  7020. $result = $this->Form->input('Contact.method', array(
  7021. 'type' => 'radio',
  7022. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7023. 'between' => 'I am between',
  7024. ));
  7025. $expected = array(
  7026. 'div' => array('class' => 'input radio'),
  7027. 'fieldset' => array(),
  7028. 'legend' => array(),
  7029. 'Method',
  7030. '/legend',
  7031. 'I am between',
  7032. 'input' => array(
  7033. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7034. 'value' => '', 'id' => 'ContactMethod_'
  7035. ),
  7036. array('input' => array(
  7037. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7038. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7039. )),
  7040. array('label' => array('for' => 'ContactMethodEmail')),
  7041. 'Email',
  7042. '/label',
  7043. array('input' => array(
  7044. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7045. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7046. )),
  7047. array('label' => array('for' => 'ContactMethodPigeon')),
  7048. 'Pigeon',
  7049. '/label',
  7050. '/fieldset',
  7051. '/div',
  7052. );
  7053. $this->assertTags($result, $expected);
  7054. }
  7055. /**
  7056. * test that some html5 inputs + FormHelper::__call() work
  7057. *
  7058. * @return void
  7059. */
  7060. public function testHtml5Inputs() {
  7061. $result = $this->Form->email('User.email');
  7062. $expected = array(
  7063. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7064. );
  7065. $this->assertTags($result, $expected);
  7066. $result = $this->Form->search('User.query');
  7067. $expected = array(
  7068. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7069. );
  7070. $this->assertTags($result, $expected);
  7071. $result = $this->Form->search('User.query', array('value' => 'test'));
  7072. $expected = array(
  7073. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7074. );
  7075. $this->assertTags($result, $expected);
  7076. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7077. $expected = array(
  7078. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7079. );
  7080. $this->assertTags($result, $expected);
  7081. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7082. $expected = array(
  7083. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7084. );
  7085. $this->assertTags($result, $expected);
  7086. }
  7087. /**
  7088. *
  7089. * @expectedException CakeException
  7090. * @return void
  7091. */
  7092. public function testHtml5InputException() {
  7093. $this->Form->email();
  7094. }
  7095. /**
  7096. * Tests that a model can be loaded from the model names passed in the request object
  7097. *
  7098. * @return void
  7099. */
  7100. public function testIntrospectModelFromRequest() {
  7101. $this->loadFixtures('Post');
  7102. App::build(array(
  7103. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7104. ));
  7105. CakePlugin::load('TestPlugin');
  7106. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7107. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7108. $this->Form->create('TestPluginPost');
  7109. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7110. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7111. CakePlugin::unload();
  7112. App::build();
  7113. }
  7114. /**
  7115. * Tests that it is possible to set the validation errors directly in the helper for a field
  7116. *
  7117. * @return void
  7118. */
  7119. public function testCustomValidationErrors() {
  7120. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7121. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7122. $this->assertEquals('Badness!', $result);
  7123. }
  7124. }