PageRenderTime 131ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

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

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