PageRenderTime 106ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 2ms

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

https://bitbucket.org/Aapje/quoted-for-the-win
PHP | 8151 lines | 6353 code | 645 blank | 1153 comment | 1 complexity | 13a44c5be121a2426464f6e0d7cbf7d6 MD5 | raw file
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  111. ),
  112. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  113. 'iamrequiredalways' => array(
  114. 'email' => array('rule' => 'email'),
  115. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  116. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  117. ),
  118. );
  119. /**
  120. * schema method
  121. *
  122. * @return void
  123. */
  124. public function setSchema($schema) {
  125. $this->_schema = $schema;
  126. }
  127. /**
  128. * hasAndBelongsToMany property
  129. *
  130. * @var array
  131. */
  132. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  133. /**
  134. * hasAndBelongsToMany property
  135. *
  136. * @var array
  137. */
  138. public $belongsTo = array('User' => array('className' => 'UserForm'));
  139. }
  140. /**
  141. * ContactTagsContact class
  142. *
  143. * @package cake
  144. * @package Cake.Test.Case.View.Helper
  145. */
  146. class ContactTagsContact extends CakeTestModel {
  147. /**
  148. * useTable property
  149. *
  150. * @var bool false
  151. */
  152. public $useTable = false;
  153. /**
  154. * name property
  155. *
  156. * @var string 'Contact'
  157. */
  158. public $name = 'ContactTagsContact';
  159. /**
  160. * Default schema
  161. *
  162. * @var array
  163. */
  164. protected $_schema = array(
  165. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  166. 'contact_tag_id' => array(
  167. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  168. )
  169. );
  170. /**
  171. * schema method
  172. *
  173. * @return void
  174. */
  175. public function setSchema($schema) {
  176. $this->_schema = $schema;
  177. }
  178. }
  179. /**
  180. * ContactNonStandardPk class
  181. *
  182. * @package cake
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactNonStandardPk extends Contact {
  186. /**
  187. * primaryKey property
  188. *
  189. * @var string 'pk'
  190. */
  191. public $primaryKey = 'pk';
  192. /**
  193. * name property
  194. *
  195. * @var string 'ContactNonStandardPk'
  196. */
  197. public $name = 'ContactNonStandardPk';
  198. /**
  199. * schema method
  200. *
  201. * @return void
  202. */
  203. public function schema($field = false) {
  204. $this->_schema = parent::schema();
  205. $this->_schema['pk'] = $this->_schema['id'];
  206. unset($this->_schema['id']);
  207. return $this->_schema;
  208. }
  209. }
  210. /**
  211. * ContactTag class
  212. *
  213. * @package cake
  214. * @package Cake.Test.Case.View.Helper
  215. */
  216. class ContactTag extends Model {
  217. /**
  218. * useTable property
  219. *
  220. * @var bool false
  221. */
  222. public $useTable = false;
  223. /**
  224. * schema definition
  225. *
  226. * @var array
  227. */
  228. protected $_schema = array(
  229. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  230. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  231. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  232. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  233. );
  234. }
  235. /**
  236. * UserForm class
  237. *
  238. * @package cake
  239. * @package Cake.Test.Case.View.Helper
  240. */
  241. class UserForm extends CakeTestModel {
  242. /**
  243. * useTable property
  244. *
  245. * @var bool false
  246. */
  247. public $useTable = false;
  248. /**
  249. * primaryKey property
  250. *
  251. * @var string 'id'
  252. */
  253. public $primaryKey = 'id';
  254. /**
  255. * name property
  256. *
  257. * @var string 'UserForm'
  258. */
  259. public $name = 'UserForm';
  260. /**
  261. * hasMany property
  262. *
  263. * @var array
  264. */
  265. public $hasMany = array(
  266. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  267. ));
  268. /**
  269. * schema definition
  270. *
  271. * @var array
  272. */
  273. protected $_schema = array(
  274. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  275. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  276. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  277. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  278. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  279. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  280. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  281. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  282. );
  283. }
  284. /**
  285. * OpenidUrl class
  286. *
  287. * @package cake
  288. * @package Cake.Test.Case.View.Helper
  289. */
  290. class OpenidUrl extends CakeTestModel {
  291. /**
  292. * useTable property
  293. *
  294. * @var bool false
  295. */
  296. public $useTable = false;
  297. /**
  298. * primaryKey property
  299. *
  300. * @var string 'id'
  301. */
  302. public $primaryKey = 'id';
  303. /**
  304. * name property
  305. *
  306. * @var string 'OpenidUrl'
  307. */
  308. public $name = 'OpenidUrl';
  309. /**
  310. * belongsTo property
  311. *
  312. * @var array
  313. */
  314. public $belongsTo = array('UserForm' => array(
  315. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  316. ));
  317. /**
  318. * validate property
  319. *
  320. * @var array
  321. */
  322. public $validate = array('openid_not_registered' => array());
  323. /**
  324. * schema method
  325. *
  326. * @var array
  327. */
  328. protected $_schema = array(
  329. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  330. 'user_form_id' => array(
  331. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  332. ),
  333. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  334. );
  335. /**
  336. * beforeValidate method
  337. *
  338. * @return void
  339. */
  340. public function beforeValidate($options = array()) {
  341. $this->invalidate('openid_not_registered');
  342. return true;
  343. }
  344. }
  345. /**
  346. * ValidateUser class
  347. *
  348. * @package cake
  349. * @package Cake.Test.Case.View.Helper
  350. */
  351. class ValidateUser extends CakeTestModel {
  352. /**
  353. * primaryKey property
  354. *
  355. * @var string 'id'
  356. */
  357. public $primaryKey = 'id';
  358. /**
  359. * useTable property
  360. *
  361. * @var bool false
  362. */
  363. public $useTable = false;
  364. /**
  365. * name property
  366. *
  367. * @var string 'ValidateUser'
  368. */
  369. public $name = 'ValidateUser';
  370. /**
  371. * hasOne property
  372. *
  373. * @var array
  374. */
  375. public $hasOne = array('ValidateProfile' => array(
  376. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  377. ));
  378. /**
  379. * schema method
  380. *
  381. * @var array
  382. */
  383. protected $_schema = array(
  384. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  385. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  386. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  387. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  388. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  389. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  390. );
  391. /**
  392. * beforeValidate method
  393. *
  394. * @return void
  395. */
  396. public function beforeValidate($options = array()) {
  397. $this->invalidate('email');
  398. return false;
  399. }
  400. }
  401. /**
  402. * ValidateProfile class
  403. *
  404. * @package cake
  405. * @package Cake.Test.Case.View.Helper
  406. */
  407. class ValidateProfile extends CakeTestModel {
  408. /**
  409. * primaryKey property
  410. *
  411. * @var string 'id'
  412. */
  413. public $primaryKey = 'id';
  414. /**
  415. * useTable property
  416. *
  417. * @var bool false
  418. */
  419. public $useTable = false;
  420. /**
  421. * schema property
  422. *
  423. * @var array
  424. */
  425. protected $_schema = array(
  426. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  427. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  428. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  429. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  430. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  431. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  432. );
  433. /**
  434. * name property
  435. *
  436. * @var string 'ValidateProfile'
  437. */
  438. public $name = 'ValidateProfile';
  439. /**
  440. * hasOne property
  441. *
  442. * @var array
  443. */
  444. public $hasOne = array('ValidateItem' => array(
  445. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  446. ));
  447. /**
  448. * belongsTo property
  449. *
  450. * @var array
  451. */
  452. public $belongsTo = array('ValidateUser' => array(
  453. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  454. ));
  455. /**
  456. * beforeValidate method
  457. *
  458. * @return void
  459. */
  460. public function beforeValidate($options = array()) {
  461. $this->invalidate('full_name');
  462. $this->invalidate('city');
  463. return false;
  464. }
  465. }
  466. /**
  467. * ValidateItem class
  468. *
  469. * @package cake
  470. * @package Cake.Test.Case.View.Helper
  471. */
  472. class ValidateItem extends CakeTestModel {
  473. /**
  474. * primaryKey property
  475. *
  476. * @var string 'id'
  477. */
  478. public $primaryKey = 'id';
  479. /**
  480. * useTable property
  481. *
  482. * @var bool false
  483. */
  484. public $useTable = false;
  485. /**
  486. * name property
  487. *
  488. * @var string 'ValidateItem'
  489. */
  490. public $name = 'ValidateItem';
  491. /**
  492. * schema property
  493. *
  494. * @var array
  495. */
  496. protected $_schema = array(
  497. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  498. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  499. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  500. 'description' => array(
  501. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  502. ),
  503. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  504. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  505. );
  506. /**
  507. * belongsTo property
  508. *
  509. * @var array
  510. */
  511. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  512. /**
  513. * beforeValidate method
  514. *
  515. * @return void
  516. */
  517. public function beforeValidate($options = array()) {
  518. $this->invalidate('description');
  519. return false;
  520. }
  521. }
  522. /**
  523. * TestMail class
  524. *
  525. * @package cake
  526. * @package Cake.Test.Case.View.Helper
  527. */
  528. class TestMail extends CakeTestModel {
  529. /**
  530. * primaryKey property
  531. *
  532. * @var string 'id'
  533. */
  534. public $primaryKey = 'id';
  535. /**
  536. * useTable property
  537. *
  538. * @var bool false
  539. */
  540. public $useTable = false;
  541. /**
  542. * name property
  543. *
  544. * @var string 'TestMail'
  545. */
  546. public $name = 'TestMail';
  547. }
  548. /**
  549. * FormHelperTest class
  550. *
  551. * @package cake
  552. * @subpackage Cake.Test.Case.View.Helper
  553. * @property FormHelper $Form
  554. */
  555. class FormHelperTest extends CakeTestCase {
  556. /**
  557. * Fixtures to be used
  558. *
  559. * @var array
  560. */
  561. public $fixtures = array('core.post');
  562. /**
  563. * Do not load the fixtures by default
  564. *
  565. * @var boolean
  566. */
  567. public $autoFixtures = false;
  568. /**
  569. * setUp method
  570. *
  571. * @return void
  572. */
  573. public function setUp() {
  574. parent::setUp();
  575. Configure::write('App.base', '');
  576. $this->Controller = new ContactTestController();
  577. $this->View = new View($this->Controller);
  578. $this->Form = new FormHelper($this->View);
  579. $this->Form->Html = new HtmlHelper($this->View);
  580. $this->Form->request = new CakeRequest('contacts/add', false);
  581. $this->Form->request->here = '/contacts/add';
  582. $this->Form->request['action'] = 'add';
  583. $this->Form->request->webroot = '';
  584. $this->Form->request->base = '';
  585. ClassRegistry::addObject('Contact', new Contact());
  586. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  587. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  588. ClassRegistry::addObject('User', new UserForm());
  589. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  590. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  591. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  592. $this->oldSalt = Configure::read('Security.salt');
  593. $this->dateRegex = array(
  594. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  595. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  596. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  597. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  598. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  599. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  600. );
  601. Configure::write('Security.salt', 'foo!');
  602. }
  603. /**
  604. * tearDown method
  605. *
  606. * @return void
  607. */
  608. public function tearDown() {
  609. parent::tearDown();
  610. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  611. Configure::write('Security.salt', $this->oldSalt);
  612. }
  613. /**
  614. * testFormCreateWithSecurity method
  615. *
  616. * Test form->create() with security key.
  617. *
  618. * @return void
  619. */
  620. public function testCreateWithSecurity() {
  621. $this->Form->request['_Token'] = array('key' => 'testKey');
  622. $encoding = strtolower(Configure::read('App.encoding'));
  623. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  624. $expected = array(
  625. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  626. 'div' => array('style' => 'display:none;'),
  627. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  628. array('input' => array(
  629. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  630. )),
  631. '/div'
  632. );
  633. $this->assertTags($result, $expected);
  634. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  635. $expected['form']['id'] = 'MyForm';
  636. $this->assertTags($result, $expected);
  637. }
  638. /**
  639. * test that create() clears the fields property so it starts fresh
  640. *
  641. * @return void
  642. */
  643. public function testCreateClearingFields() {
  644. $this->Form->fields = array('model_id');
  645. $this->Form->create('Contact');
  646. $this->assertEquals(array(), $this->Form->fields);
  647. }
  648. /**
  649. * Tests form hash generation with model-less data
  650. *
  651. * @return void
  652. */
  653. public function testValidateHashNoModel() {
  654. $this->Form->request['_Token'] = array('key' => 'foo');
  655. $result = $this->Form->secure(array('anything'));
  656. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  657. }
  658. /**
  659. * Tests that models with identical field names get resolved properly
  660. *
  661. * @return void
  662. */
  663. public function testDuplicateFieldNameResolution() {
  664. $result = $this->Form->create('ValidateUser');
  665. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  666. $result = $this->Form->input('ValidateItem.name');
  667. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  668. $result = $this->Form->input('ValidateUser.name');
  669. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  670. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  671. $this->assertRegExp('/type="text"/', $result);
  672. $result = $this->Form->input('ValidateItem.name');
  673. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  675. $this->assertRegExp('/<textarea/', $result);
  676. $result = $this->Form->input('name');
  677. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  679. $this->assertRegExp('/type="text"/', $result);
  680. }
  681. /**
  682. * Tests that hidden fields generated for checkboxes don't get locked
  683. *
  684. * @return void
  685. */
  686. public function testNoCheckboxLocking() {
  687. $this->Form->request['_Token'] = array('key' => 'foo');
  688. $this->assertSame(array(), $this->Form->fields);
  689. $this->Form->checkbox('check', array('value' => '1'));
  690. $this->assertSame($this->Form->fields, array('check'));
  691. }
  692. /**
  693. * testFormSecurityFields method
  694. *
  695. * Test generation of secure form hash generation.
  696. *
  697. * @return void
  698. */
  699. public function testFormSecurityFields() {
  700. $key = 'testKey';
  701. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  702. $this->Form->request['_Token'] = array('key' => $key);
  703. $result = $this->Form->secure($fields);
  704. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  705. $expected .= ':' . 'Model.valid';
  706. $expected = array(
  707. 'div' => array('style' => 'display:none;'),
  708. array('input' => array(
  709. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  710. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  711. )),
  712. array('input' => array(
  713. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  714. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  715. )),
  716. '/div'
  717. );
  718. $this->assertTags($result, $expected);
  719. }
  720. /**
  721. * Tests correct generation of number fields for double and float fields
  722. *
  723. * @return void
  724. */
  725. public function testTextFieldGenerationForFloats() {
  726. $model = ClassRegistry::getObject('Contact');
  727. $model->setSchema(array('foo' => array(
  728. 'type' => 'float',
  729. 'null' => false,
  730. 'default' => null,
  731. 'length' => null
  732. )));
  733. $this->Form->create('Contact');
  734. $result = $this->Form->input('foo');
  735. $expected = array(
  736. 'div' => array('class' => 'input number'),
  737. 'label' => array('for' => 'ContactFoo'),
  738. 'Foo',
  739. '/label',
  740. array('input' => array(
  741. 'type' => 'number',
  742. 'name' => 'data[Contact][foo]',
  743. 'id' => 'ContactFoo',
  744. 'step' => 'any'
  745. )),
  746. '/div'
  747. );
  748. $this->assertTags($result, $expected);
  749. $result = $this->Form->input('foo', array('step' => 0.5));
  750. $expected = array(
  751. 'div' => array('class' => 'input number'),
  752. 'label' => array('for' => 'ContactFoo'),
  753. 'Foo',
  754. '/label',
  755. array('input' => array(
  756. 'type' => 'number',
  757. 'name' => 'data[Contact][foo]',
  758. 'id' => 'ContactFoo',
  759. 'step' => '0.5'
  760. )),
  761. '/div'
  762. );
  763. $this->assertTags($result, $expected);
  764. }
  765. /**
  766. * Tests correct generation of number fields for integer fields
  767. *
  768. * @access public
  769. * @return void
  770. */
  771. public function testTextFieldTypeNumberGenerationForIntegers() {
  772. $model = ClassRegistry::getObject('Contact');
  773. $model->setSchema(array('foo' => array(
  774. 'type' => 'integer',
  775. 'null' => false,
  776. 'default' => null,
  777. 'length' => null
  778. )));
  779. $this->Form->create('Contact');
  780. $result = $this->Form->input('foo');
  781. $expected = array(
  782. 'div' => array('class' => 'input number'),
  783. 'label' => array('for' => 'ContactFoo'),
  784. 'Foo',
  785. '/label',
  786. array('input' => array(
  787. 'type' => 'number', 'name' => 'data[Contact][foo]',
  788. 'id' => 'ContactFoo'
  789. )),
  790. '/div'
  791. );
  792. $this->assertTags($result, $expected);
  793. }
  794. /**
  795. * testFormSecurityMultipleFields method
  796. *
  797. * Test secure() with multiple row form. Ensure hash is correct.
  798. *
  799. * @return void
  800. */
  801. public function testFormSecurityMultipleFields() {
  802. $key = 'testKey';
  803. $fields = array(
  804. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  805. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  806. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  807. );
  808. $this->Form->request['_Token'] = array('key' => $key);
  809. $result = $this->Form->secure($fields);
  810. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  811. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  812. $expected = array(
  813. 'div' => array('style' => 'display:none;'),
  814. array('input' => array(
  815. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  816. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  817. )),
  818. array('input' => array(
  819. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  820. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  821. )),
  822. '/div'
  823. );
  824. $this->assertTags($result, $expected);
  825. }
  826. /**
  827. * testFormSecurityMultipleSubmitButtons
  828. *
  829. * test form submit generation and ensure that _Token is only created on end()
  830. *
  831. * @return void
  832. */
  833. public function testFormSecurityMultipleSubmitButtons() {
  834. $key = 'testKey';
  835. $this->Form->request['_Token'] = array('key' => $key);
  836. $this->Form->create('Addresses');
  837. $this->Form->input('Address.title');
  838. $this->Form->input('Address.first_name');
  839. $result = $this->Form->submit('Save', array('name' => 'save'));
  840. $expected = array(
  841. 'div' => array('class' => 'submit'),
  842. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  843. '/div',
  844. );
  845. $this->assertTags($result, $expected);
  846. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  847. $expected = array(
  848. 'div' => array('class' => 'submit'),
  849. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  850. '/div',
  851. );
  852. $this->assertTags($result, $expected);
  853. $result = $this->Form->end(null);
  854. $expected = array(
  855. 'div' => array('style' => 'display:none;'),
  856. array('input' => array(
  857. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  858. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  859. )),
  860. array('input' => array(
  861. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  862. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  863. )),
  864. '/div'
  865. );
  866. $this->assertTags($result, $expected);
  867. }
  868. /**
  869. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  870. *
  871. * @return void
  872. */
  873. public function testSecurityButtonNestedNamed() {
  874. $key = 'testKey';
  875. $this->Form->request['_Token'] = array('key' => $key);
  876. $this->Form->create('Addresses');
  877. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  878. $result = $this->Form->unlockField();
  879. $this->assertEquals(array('Address.button'), $result);
  880. }
  881. /**
  882. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  883. *
  884. * @return void
  885. */
  886. public function testSecuritySubmitNestedNamed() {
  887. $key = 'testKey';
  888. $this->Form->request['_Token'] = array('key' => $key);
  889. $this->Form->create('Addresses');
  890. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  891. $result = $this->Form->unlockField();
  892. $this->assertEquals(array('Address.button'), $result);
  893. }
  894. /**
  895. * Test that the correct fields are unlocked for image submits with no names.
  896. *
  897. * @return void
  898. */
  899. public function testSecuritySubmitImageNoName() {
  900. $key = 'testKey';
  901. $this->Form->request['_Token'] = array('key' => $key);
  902. $this->Form->create('User');
  903. $result = $this->Form->submit('save.png');
  904. $expected = array(
  905. 'div' => array('class' => 'submit'),
  906. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  907. '/div'
  908. );
  909. $this->assertTags($result, $expected);
  910. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  911. }
  912. /**
  913. * Test that the correct fields are unlocked for image submits with names.
  914. *
  915. * @return void
  916. */
  917. public function testSecuritySubmitImageName() {
  918. $key = 'testKey';
  919. $this->Form->request['_Token'] = array('key' => $key);
  920. $this->Form->create('User');
  921. $result = $this->Form->submit('save.png', array('name' => 'test'));
  922. $expected = array(
  923. 'div' => array('class' => 'submit'),
  924. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  925. '/div'
  926. );
  927. $this->assertTags($result, $expected);
  928. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  929. }
  930. /**
  931. * testFormSecurityMultipleInputFields method
  932. *
  933. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  934. *
  935. * @return void
  936. */
  937. public function testFormSecurityMultipleInputFields() {
  938. $key = 'testKey';
  939. $this->Form->request['_Token'] = array('key' => $key);
  940. $this->Form->create('Addresses');
  941. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  942. $this->Form->input('Addresses.0.title');
  943. $this->Form->input('Addresses.0.first_name');
  944. $this->Form->input('Addresses.0.last_name');
  945. $this->Form->input('Addresses.0.address');
  946. $this->Form->input('Addresses.0.city');
  947. $this->Form->input('Addresses.0.phone');
  948. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  949. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  950. $this->Form->input('Addresses.1.title');
  951. $this->Form->input('Addresses.1.first_name');
  952. $this->Form->input('Addresses.1.last_name');
  953. $this->Form->input('Addresses.1.address');
  954. $this->Form->input('Addresses.1.city');
  955. $this->Form->input('Addresses.1.phone');
  956. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  957. $result = $this->Form->secure($this->Form->fields);
  958. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  959. $expected = array(
  960. 'div' => array('style' => 'display:none;'),
  961. array('input' => array(
  962. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  963. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  964. )),
  965. array('input' => array(
  966. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  967. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  968. )),
  969. '/div'
  970. );
  971. $this->assertTags($result, $expected);
  972. }
  973. /**
  974. * Test form security with Model.field.0 style inputs
  975. *
  976. * @return void
  977. */
  978. public function testFormSecurityArrayFields() {
  979. $key = 'testKey';
  980. $this->Form->request->params['_Token']['key'] = $key;
  981. $this->Form->create('Address');
  982. $this->Form->input('Address.primary.1');
  983. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  984. $this->Form->input('Address.secondary.1.0');
  985. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  986. }
  987. /**
  988. * testFormSecurityMultipleInputDisabledFields method
  989. *
  990. * test secure form generation with multiple records and disabled fields.
  991. *
  992. * @return void
  993. */
  994. public function testFormSecurityMultipleInputDisabledFields() {
  995. $key = 'testKey';
  996. $this->Form->request->params['_Token'] = array(
  997. 'key' => $key,
  998. 'unlockedFields' => array('first_name', 'address')
  999. );
  1000. $this->Form->create();
  1001. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1002. $this->Form->input('Addresses.0.title');
  1003. $this->Form->input('Addresses.0.first_name');
  1004. $this->Form->input('Addresses.0.last_name');
  1005. $this->Form->input('Addresses.0.address');
  1006. $this->Form->input('Addresses.0.city');
  1007. $this->Form->input('Addresses.0.phone');
  1008. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1009. $this->Form->input('Addresses.1.title');
  1010. $this->Form->input('Addresses.1.first_name');
  1011. $this->Form->input('Addresses.1.last_name');
  1012. $this->Form->input('Addresses.1.address');
  1013. $this->Form->input('Addresses.1.city');
  1014. $this->Form->input('Addresses.1.phone');
  1015. $result = $this->Form->secure($this->Form->fields);
  1016. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1017. $expected = array(
  1018. 'div' => array('style' => 'display:none;'),
  1019. array('input' => array(
  1020. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1021. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1022. )),
  1023. array('input' => array(
  1024. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1025. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1026. )),
  1027. '/div'
  1028. );
  1029. $this->assertTags($result, $expected);
  1030. }
  1031. /**
  1032. * testFormSecurityInputDisabledFields method
  1033. *
  1034. * Test single record form with disabled fields.
  1035. *
  1036. * @return void
  1037. */
  1038. public function testFormSecurityInputUnlockedFields() {
  1039. $key = 'testKey';
  1040. $this->Form->request['_Token'] = array(
  1041. 'key' => $key,
  1042. 'unlockedFields' => array('first_name', 'address')
  1043. );
  1044. $this->Form->create();
  1045. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1046. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1047. $this->Form->input('Addresses.title');
  1048. $this->Form->input('Addresses.first_name');
  1049. $this->Form->input('Addresses.last_name');
  1050. $this->Form->input('Addresses.address');
  1051. $this->Form->input('Addresses.city');
  1052. $this->Form->input('Addresses.phone');
  1053. $result = $this->Form->fields;
  1054. $expected = array(
  1055. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1056. 'Addresses.city', 'Addresses.phone'
  1057. );
  1058. $this->assertEquals($expected, $result);
  1059. $result = $this->Form->secure($expected);
  1060. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1061. $expected = array(
  1062. 'div' => array('style' => 'display:none;'),
  1063. array('input' => array(
  1064. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1065. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1066. )),
  1067. array('input' => array(
  1068. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1069. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1070. )),
  1071. '/div'
  1072. );
  1073. $this->assertTags($result, $expected);
  1074. }
  1075. /**
  1076. * test securing inputs with custom name attributes.
  1077. *
  1078. * @return void
  1079. */
  1080. public function testFormSecureWithCustomNameAttribute() {
  1081. $this->Form->request->params['_Token']['key'] = 'testKey';
  1082. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1083. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1084. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1085. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1086. }
  1087. /**
  1088. * testFormSecuredInput method
  1089. *
  1090. * Test generation of entire secure form, assertions made on input() output.
  1091. *
  1092. * @return void
  1093. */
  1094. public function testFormSecuredInput() {
  1095. $this->Form->request['_Token'] = array('key' => 'testKey');
  1096. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1097. $encoding = strtolower(Configure::read('App.encoding'));
  1098. $expected = array(
  1099. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1100. 'div' => array('style' => 'display:none;'),
  1101. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1102. array('input' => array(
  1103. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1104. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1105. )),
  1106. '/div'
  1107. );
  1108. $this->assertTags($result, $expected);
  1109. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1110. $expected = array(
  1111. 'div' => array('class' => 'input text'),
  1112. 'label' => array('for' => 'UserFormPublished'),
  1113. 'Published',
  1114. '/label',
  1115. array('input' => array(
  1116. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1117. 'id' => 'UserFormPublished'
  1118. )),
  1119. '/div'
  1120. );
  1121. $this->assertTags($result, $expected);
  1122. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1123. $expected = array(
  1124. 'div' => array('class' => 'input text'),
  1125. 'label' => array('for' => 'UserFormOther'),
  1126. 'Other',
  1127. '/label',
  1128. array('input' => array(
  1129. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1130. 'id' => 'UserFormOther'
  1131. )),
  1132. '/div'
  1133. );
  1134. $this->assertTags($result, $expected);
  1135. $result = $this->Form->hidden('UserForm.stuff');
  1136. $expected = array('input' => array(
  1137. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1138. 'id' => 'UserFormStuff'
  1139. ));
  1140. $this->assertTags($result, $expected);
  1141. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1142. $expected = array('input' => array(
  1143. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1144. 'value' => '0', 'id' => 'UserFormHidden'
  1145. ));
  1146. $this->assertTags($result, $expected);
  1147. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1148. $expected = array(
  1149. 'div' => array('class' => 'input checkbox'),
  1150. array('input' => array(
  1151. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1152. 'value' => '0', 'id' => 'UserFormSomething_'
  1153. )),
  1154. array('input' => array(
  1155. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1156. 'value' => '1', 'id' => 'UserFormSomething'
  1157. )),
  1158. 'label' => array('for' => 'UserFormSomething'),
  1159. 'Something',
  1160. '/label',
  1161. '/div'
  1162. );
  1163. $this->assertTags($result, $expected);
  1164. $result = $this->Form->fields;
  1165. $expected = array(
  1166. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1167. 'UserForm.hidden' => '0', 'UserForm.something'
  1168. );
  1169. $this->assertEquals($expected, $result);
  1170. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1171. $result = $this->Form->secure($this->Form->fields);
  1172. $expected = array(
  1173. 'div' => array('style' => 'display:none;'),
  1174. array('input' => array(
  1175. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1176. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1177. )),
  1178. array('input' => array(
  1179. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1180. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1181. )),
  1182. '/div'
  1183. );
  1184. $this->assertTags($result, $expected);
  1185. }
  1186. /**
  1187. * Tests that the correct keys are added to the field hash index
  1188. *
  1189. * @return void
  1190. */
  1191. public function testFormSecuredFileInput() {
  1192. $this->Form->request['_Token'] = array('key' => 'testKey');
  1193. $this->assertEquals(array(), $this->Form->fields);
  1194. $result = $this->Form->file('Attachment.file');
  1195. $expected = array(
  1196. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1197. 'Attachment.file.error', 'Attachment.file.size'
  1198. );
  1199. $this->assertEquals($expected, $this->Form->fields);
  1200. }
  1201. /**
  1202. * test that multiple selects keys are added to field hash
  1203. *
  1204. * @return void
  1205. */
  1206. public function testFormSecuredMultipleSelect() {
  1207. $this->Form->request['_Token'] = array('key' => 'testKey');
  1208. $this->assertEquals(array(), $this->Form->fields);
  1209. $options = array('1' => 'one', '2' => 'two');
  1210. $this->Form->select('Model.select', $options);
  1211. $expected = array('Model.select');
  1212. $this->assertEquals($expected, $this->Form->fields);
  1213. $this->Form->fields = array();
  1214. $this->Form->select('Model.select', $options, array('multiple' => true));
  1215. $this->assertEquals($expected, $this->Form->fields);
  1216. }
  1217. /**
  1218. * testFormSecuredRadio method
  1219. *
  1220. * @return void
  1221. */
  1222. public function testFormSecuredRadio() {
  1223. $this->Form->request['_Token'] = array('key' => 'testKey');
  1224. $this->assertEquals(array(), $this->Form->fields);
  1225. $options = array('1' => 'option1', '2' => 'option2');
  1226. $this->Form->radio('Test.test', $options);
  1227. $expected = array('Test.test');
  1228. $this->assertEquals($expected, $this->Form->fields);
  1229. }
  1230. /**
  1231. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1232. * hashing.
  1233. *
  1234. * @return void
  1235. */
  1236. public function testFormSecuredAndDisabled() {
  1237. $this->Form->request['_Token'] = array('key' => 'testKey');
  1238. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1239. $this->Form->text('Model.text', array('disabled' => true));
  1240. $this->Form->password('Model.text', array('disabled' => true));
  1241. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1242. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1243. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1244. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1245. $this->Form->month('Model.month', array('disabled' => true));
  1246. $this->Form->day('Model.day', array('disabled' => true));
  1247. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1248. $this->Form->minute('Model.minute', array('disabled' => true));
  1249. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1250. $expected = array(
  1251. 'Model.radio' => ''
  1252. );
  1253. $this->assertEquals($expected, $this->Form->fields);
  1254. }
  1255. /**
  1256. * testDisableSecurityUsingForm method
  1257. *
  1258. * @return void
  1259. */
  1260. public function testDisableSecurityUsingForm() {
  1261. $this->Form->request['_Token'] = array(
  1262. 'key' => 'testKey',
  1263. 'disabledFields' => array()
  1264. );
  1265. $this->Form->create();
  1266. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1267. $this->Form->input('Addresses.title');
  1268. $this->Form->input('Addresses.first_name', array('secure' => false));
  1269. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1270. $this->Form->input('Addresses.zip', array(
  1271. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1272. ));
  1273. $result = $this->Form->fields;
  1274. $expected = array(
  1275. 'Addresses.id' => '123456', 'Addresses.title',
  1276. );
  1277. $this->assertEquals($expected, $result);
  1278. }
  1279. /**
  1280. * test disableField
  1281. *
  1282. * @return void
  1283. */
  1284. public function testUnlockFieldAddsToList() {
  1285. $this->Form->request['_Token'] = array(
  1286. 'key' => 'testKey',
  1287. 'unlockedFields' => array()
  1288. );
  1289. $this->Form->create('Contact');
  1290. $this->Form->unlockField('Contact.name');
  1291. $this->Form->text('Contact.name');
  1292. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1293. $this->assertEquals(array(), $this->Form->fields);
  1294. }
  1295. /**
  1296. * test unlockField removing from fields array.
  1297. *
  1298. * @return void
  1299. */
  1300. public function testUnlockFieldRemovingFromFields() {
  1301. $this->Form->request['_Token'] = array(
  1302. 'key' => 'testKey',
  1303. 'unlockedFields' => array()
  1304. );
  1305. $this->Form->create('Contact');
  1306. $this->Form->hidden('Contact.id', array('value' => 1));
  1307. $this->Form->text('Contact.name');
  1308. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1309. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1310. $this->Form->unlockField('Contact.name');
  1311. $this->Form->unlockField('Contact.id');
  1312. $this->assertEquals(array(), $this->Form->fields);
  1313. }
  1314. /**
  1315. * testTagIsInvalid method
  1316. *
  1317. * @return void
  1318. */
  1319. public function testTagIsInvalid() {
  1320. $Contact = ClassRegistry::getObject('Contact');
  1321. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1322. $this->Form->setEntity('Contact.0.email');
  1323. $result = $this->Form->tagIsInvalid();
  1324. $expected = array('Please provide an email');
  1325. $this->assertEquals($expected, $result);
  1326. $this->Form->setEntity('Contact.1.email');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = false;
  1329. $this->assertSame($expected, $result);
  1330. $this->Form->setEntity('Contact.0.name');
  1331. $result = $this->Form->tagIsInvalid();
  1332. $expected = false;
  1333. $this->assertSame($expected, $result);
  1334. }
  1335. /**
  1336. * testPasswordValidation method
  1337. *
  1338. * test validation errors on password input.
  1339. *
  1340. * @return void
  1341. */
  1342. public function testPasswordValidation() {
  1343. $Contact = ClassRegistry::getObject('Contact');
  1344. $Contact->validationErrors['password'] = array('Please provide a password');
  1345. $result = $this->Form->input('Contact.password');
  1346. $expected = array(
  1347. 'div' => array('class' => 'input password error'),
  1348. 'label' => array('for' => 'ContactPassword'),
  1349. 'Password',
  1350. '/label',
  1351. 'input' => array(
  1352. 'type' => 'password', 'name' => 'data[Contact][password]',
  1353. 'id' => 'ContactPassword', 'class' => 'form-error'
  1354. ),
  1355. array('div' => array('class' => 'error-message')),
  1356. 'Please provide a password',
  1357. '/div',
  1358. '/div'
  1359. );
  1360. $this->assertTags($result, $expected);
  1361. }
  1362. /**
  1363. * testEmptyErrorValidation method
  1364. *
  1365. * test validation error div when validation message is an empty string
  1366. *
  1367. * @access public
  1368. * @return void
  1369. */
  1370. public function testEmptyErrorValidation() {
  1371. $this->Form->validationErrors['Contact']['password'] = '';
  1372. $result = $this->Form->input('Contact.password');
  1373. $expected = array(
  1374. 'div' => array('class' => 'input password error'),
  1375. 'label' => array('for' => 'ContactPassword'),
  1376. 'Password',
  1377. '/label',
  1378. 'input' => array(
  1379. 'type' => 'password', 'name' => 'data[Contact][password]',
  1380. 'id' => 'ContactPassword', 'class' => 'form-error'
  1381. ),
  1382. array('div' => array('class' => 'error-message')),
  1383. array(),
  1384. '/div',
  1385. '/div'
  1386. );
  1387. $this->assertTags($result, $expected);
  1388. }
  1389. /**
  1390. * testEmptyInputErrorValidation method
  1391. *
  1392. * test validation error div when validation message is overridden by an empty string when calling input()
  1393. *
  1394. * @access public
  1395. * @return void
  1396. */
  1397. public function testEmptyInputErrorValidation() {
  1398. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1399. $result = $this->Form->input('Contact.password', array('error' => ''));
  1400. $expected = array(
  1401. 'div' => array('class' => 'input password error'),
  1402. 'label' => array('for' => 'ContactPassword'),
  1403. 'Password',
  1404. '/label',
  1405. 'input' => array(
  1406. 'type' => 'password', 'name' => 'data[Contact][password]',
  1407. 'id' => 'ContactPassword', 'class' => 'form-error'
  1408. ),
  1409. array('div' => array('class' => 'error-message')),
  1410. array(),
  1411. '/div',
  1412. '/div'
  1413. );
  1414. $this->assertTags($result, $expected);
  1415. }
  1416. /**
  1417. * testFormValidationAssociated method
  1418. *
  1419. * test display of form errors in conjunction with model::validates.
  1420. *
  1421. * @return void
  1422. */
  1423. public function testFormValidationAssociated() {
  1424. $this->UserForm = ClassRegistry::getObject('UserForm');
  1425. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1426. $data = array(
  1427. 'UserForm' => array('name' => 'user'),
  1428. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1429. );
  1430. $result = $this->UserForm->OpenidUrl->create($data);
  1431. $this->assertFalse(empty($result));
  1432. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1433. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1434. $encoding = strtolower(Configure::read('App.encoding'));
  1435. $expected = array(
  1436. 'form' => array(
  1437. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1438. 'accept-charset' => $encoding
  1439. ),
  1440. 'div' => array('style' => 'display:none;'),
  1441. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1442. '/div'
  1443. );
  1444. $this->assertTags($result, $expected);
  1445. $result = $this->Form->error(
  1446. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1447. );
  1448. $this->assertEquals('Error, not registered', $result);
  1449. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1450. }
  1451. /**
  1452. * testFormValidationAssociatedFirstLevel method
  1453. *
  1454. * test form error display with associated model.
  1455. *
  1456. * @return void
  1457. */
  1458. public function testFormValidationAssociatedFirstLevel() {
  1459. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1460. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1461. $data = array(
  1462. 'ValidateUser' => array('name' => 'mariano'),
  1463. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1464. );
  1465. $result = $this->ValidateUser->create($data);
  1466. $this->assertFalse(empty($result));
  1467. $this->assertFalse($this->ValidateUser->validates());
  1468. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1469. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1470. $encoding = strtolower(Configure::read('App.encoding'));
  1471. $expected = array(
  1472. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1473. 'div' => array('style' => 'display:none;'),
  1474. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1475. '/div'
  1476. );
  1477. $this->assertTags($result, $expected);
  1478. $result = $this->Form->error(
  1479. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1480. );
  1481. $this->assertEquals('Invalid email', $result);
  1482. $result = $this->Form->error(
  1483. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1484. );
  1485. $this->assertEquals('Invalid name', $result);
  1486. $result = $this->Form->error(
  1487. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1488. );
  1489. $this->assertEquals('Invalid city', $result);
  1490. unset($this->ValidateUser->ValidateProfile);
  1491. unset($this->ValidateUser);
  1492. }
  1493. /**
  1494. * testFormValidationAssociatedSecondLevel method
  1495. *
  1496. * test form error display with associated model.
  1497. *
  1498. * @return void
  1499. */
  1500. public function testFormValidationAssociatedSecondLevel() {
  1501. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1502. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1503. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1504. $data = array(
  1505. 'ValidateUser' => array('name' => 'mariano'),
  1506. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1507. 'ValidateItem' => array('name' => 'Item')
  1508. );
  1509. $result = $this->ValidateUser->create($data);
  1510. $this->assertFalse(empty($result));
  1511. $this->assertFalse($this->ValidateUser->validates());
  1512. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1513. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1514. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1515. $encoding = strtolower(Configure::read('App.encoding'));
  1516. $expected = array(
  1517. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1518. 'div' => array('style' => 'display:none;'),
  1519. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1520. '/div'
  1521. );
  1522. $this->assertTags($result, $expected);
  1523. $result = $this->Form->error(
  1524. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1525. );
  1526. $this->assertEquals('Invalid email', $result);
  1527. $result = $this->Form->error(
  1528. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1529. );
  1530. $this->assertEquals('Invalid name', $result);
  1531. $result = $this->Form->error(
  1532. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1533. );
  1534. $result = $this->Form->error(
  1535. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1536. );
  1537. $this->assertEquals('Invalid description', $result);
  1538. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1539. unset($this->ValidateUser->ValidateProfile);
  1540. unset($this->ValidateUser);
  1541. }
  1542. /**
  1543. * testFormValidationMultiRecord method
  1544. *
  1545. * test form error display with multiple records.
  1546. *
  1547. * @return void
  1548. */
  1549. public function testFormValidationMultiRecord() {
  1550. $Contact = ClassRegistry::getObject('Contact');
  1551. $Contact->validationErrors[2] = array(
  1552. 'name' => array('This field cannot be left blank')
  1553. );
  1554. $result = $this->Form->input('Contact.2.name');
  1555. $expected = array(
  1556. 'div' => array('class' => 'input text error'),
  1557. 'label' => array('for' => 'Contact2Name'),
  1558. 'Name',
  1559. '/label',
  1560. 'input' => array(
  1561. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1562. 'class' => 'form-error', 'maxlength' => 255
  1563. ),
  1564. array('div' => array('class' => 'error-message')),
  1565. 'This field cannot be left blank',
  1566. '/div',
  1567. '/div'
  1568. );
  1569. $this->assertTags($result, $expected);
  1570. }
  1571. /**
  1572. * testMultipleInputValidation method
  1573. *
  1574. * test multiple record form validation error display.
  1575. *
  1576. * @return void
  1577. */
  1578. public function testMultipleInputValidation() {
  1579. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1580. $Address->validationErrors[0] = array(
  1581. 'title' => array('This field cannot be empty'),
  1582. 'first_name' => array('This field cannot be empty')
  1583. );
  1584. $Address->validationErrors[1] = array(
  1585. 'last_name' => array('You must have a last name')
  1586. );
  1587. $this->Form->create();
  1588. $result = $this->Form->input('Address.0.title');
  1589. $expected = array(
  1590. 'div' => array('class'),
  1591. 'label' => array('for'),
  1592. 'preg:/[^<]+/',
  1593. '/label',
  1594. 'input' => array(
  1595. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1596. ),
  1597. array('div' => array('class' => 'error-message')),
  1598. 'This field cannot be empty',
  1599. '/div',
  1600. '/div'
  1601. );
  1602. $this->assertTags($result, $expected);
  1603. $result = $this->Form->input('Address.0.first_name');
  1604. $expected = array(
  1605. 'div' => array('class'),
  1606. 'label' => array('for'),
  1607. 'preg:/[^<]+/',
  1608. '/label',
  1609. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1610. array('div' => array('class' => 'error-message')),
  1611. 'This field cannot be empty',
  1612. '/div',
  1613. '/div'
  1614. );
  1615. $this->assertTags($result, $expected);
  1616. $result = $this->Form->input('Address.0.last_name');
  1617. $expected = array(
  1618. 'div' => array('class'),
  1619. 'label' => array('for'),
  1620. 'preg:/[^<]+/',
  1621. '/label',
  1622. 'input' => array('type' => 'text', 'name', 'id'),
  1623. '/div'
  1624. );
  1625. $this->assertTags($result, $expected);
  1626. $result = $this->Form->input('Address.1.last_name');
  1627. $expected = array(
  1628. 'div' => array('class'),
  1629. 'label' => array('for'),
  1630. 'preg:/[^<]+/',
  1631. '/label',
  1632. 'input' => array(
  1633. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1634. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1635. ),
  1636. array('div' => array('class' => 'error-message')),
  1637. 'You must have a last name',
  1638. '/div',
  1639. '/div'
  1640. );
  1641. $this->assertTags($result, $expected);
  1642. }
  1643. /**
  1644. * testInput method
  1645. *
  1646. * Test various incarnations of input().
  1647. *
  1648. * @return void
  1649. */
  1650. public function testInput() {
  1651. $result = $this->Form->input('ValidateUser.balance');
  1652. $expected = array(
  1653. 'div' => array('class'),
  1654. 'label' => array('for'),
  1655. 'Balance',
  1656. '/label',
  1657. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1658. '/div',
  1659. );
  1660. $this->assertTags($result, $expected);
  1661. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1662. $expected = array(
  1663. 'div' => array('class' => 'input text'),
  1664. 'label' => array('for' => 'custom'),
  1665. 'Email',
  1666. '/label',
  1667. array('input' => array(
  1668. 'type' => 'text', 'name' => 'data[Contact][email]',
  1669. 'id' => 'custom', 'maxlength' => 255
  1670. )),
  1671. '/div'
  1672. );
  1673. $this->assertTags($result, $expected);
  1674. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1675. $expected = array(
  1676. '<div',
  1677. 'label' => array('for' => 'ContactEmail'),
  1678. 'Email',
  1679. '/label',
  1680. array('input' => array(
  1681. 'type' => 'text', 'name' => 'data[Contact][email]',
  1682. 'id' => 'ContactEmail', 'maxlength' => 255
  1683. )),
  1684. '/div'
  1685. );
  1686. $this->assertTags($result, $expected);
  1687. $result = $this->Form->hidden('Contact.idontexist');
  1688. $expected = array('input' => array(
  1689. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1690. 'id' => 'ContactIdontexist'
  1691. ));
  1692. $this->assertTags($result, $expected);
  1693. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1694. $expected = array(
  1695. 'div' => array('class' => 'input text'),
  1696. 'label' => array('for' => 'ContactEmail'),
  1697. 'Email',
  1698. '/label',
  1699. array('input' => array(
  1700. 'type' => 'text', 'name' => 'data[Contact][email]',
  1701. 'id' => 'ContactEmail'
  1702. )),
  1703. '/div'
  1704. );
  1705. $this->assertTags($result, $expected);
  1706. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1707. $expected = array(
  1708. 'div' => array('class' => 'input text'),
  1709. 'label' => array('for' => 'Contact5Email'),
  1710. 'Email',
  1711. '/label',
  1712. array('input' => array(
  1713. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1714. 'id' => 'Contact5Email'
  1715. )),
  1716. '/div'
  1717. );
  1718. $this->assertTags($result, $expected);
  1719. $result = $this->Form->input('Contact.password');
  1720. $expected = array(
  1721. 'div' => array('class' => 'input password'),
  1722. 'label' => array('for' => 'ContactPassword'),
  1723. 'Password',
  1724. '/label',
  1725. array('input' => array(
  1726. 'type' => 'password', 'name' => 'data[Contact][password]',
  1727. 'id' => 'ContactPassword'
  1728. )),
  1729. '/div'
  1730. );
  1731. $this->assertTags($result, $expected);
  1732. $result = $this->Form->input('Contact.email', array(
  1733. 'type' => 'file', 'class' => 'textbox'
  1734. ));
  1735. $expected = array(
  1736. 'div' => array('class' => 'input file'),
  1737. 'label' => array('for' => 'ContactEmail'),
  1738. 'Email',
  1739. '/label',
  1740. array('input' => array(
  1741. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1742. 'id' => 'ContactEmail'
  1743. )),
  1744. '/div'
  1745. );
  1746. $this->assertTags($result, $expected);
  1747. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1748. $result = $this->Form->input('Contact.phone');
  1749. $expected = array(
  1750. 'div' => array('class' => 'input text'),
  1751. 'label' => array('for' => 'ContactPhone'),
  1752. 'Phone',
  1753. '/label',
  1754. array('input' => array(
  1755. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1756. 'value' => 'Hello &amp; World &gt; weird chars',
  1757. 'id' => 'ContactPhone', 'maxlength' => 255
  1758. )),
  1759. '/div'
  1760. );
  1761. $this->assertTags($result, $expected);
  1762. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1763. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1764. $expected = array(
  1765. 'div' => array('class' => 'input text'),
  1766. 'label' => array('for' => 'myId'),
  1767. 'Field',
  1768. '/label',
  1769. 'input' => array(
  1770. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1771. 'value' => 'My value', 'id' => 'myId'
  1772. ),
  1773. '/div'
  1774. );
  1775. $this->assertTags($result, $expected);
  1776. unset($this->Form->request->data);
  1777. $Contact = ClassRegistry::getObject('Contact');
  1778. $Contact->validationErrors['field'] = array('Badness!');
  1779. $result = $this->Form->input('Contact.field');
  1780. $expected = array(
  1781. 'div' => array('class' => 'input text error'),
  1782. 'label' => array('for' => 'ContactField'),
  1783. 'Field',
  1784. '/label',
  1785. 'input' => array(
  1786. 'type' => 'text', 'name' => 'data[Contact][field]',
  1787. 'id' => 'ContactField', 'class' => 'form-error'
  1788. ),
  1789. array('div' => array('class' => 'error-message')),
  1790. 'Badness!',
  1791. '/div',
  1792. '/div'
  1793. );
  1794. $this->assertTags($result, $expected);
  1795. $result = $this->Form->input('Contact.field', array(
  1796. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1797. ));
  1798. $expected = array(
  1799. 'label' => array('for' => 'ContactField'),
  1800. 'Field',
  1801. '/label',
  1802. 'input' => array(
  1803. 'type' => 'text', 'name' => 'data[Contact][field]',
  1804. 'id' => 'ContactField', 'class' => 'form-error'
  1805. ),
  1806. array('span' => array('class' => 'error-message')),
  1807. 'Badness!',
  1808. '/span'
  1809. );
  1810. $this->assertTags($result, $expected);
  1811. $result = $this->Form->input('Contact.field', array(
  1812. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1813. ));
  1814. $expected = array(
  1815. 'div' => array('class' => 'input text error'),
  1816. 'label' => array('for' => 'ContactField'),
  1817. 'Field',
  1818. '/label',
  1819. 'input' => array(
  1820. 'type' => 'text', 'name' => 'data[Contact][field]',
  1821. 'id' => 'ContactField', 'class' => 'form-error'
  1822. ),
  1823. array('div' => array('class' => 'error')),
  1824. 'Badness!',
  1825. '/div'
  1826. );
  1827. $this->assertTags($result, $expected);
  1828. $result = $this->Form->input('Contact.field', array(
  1829. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1830. ));
  1831. $expected = array(
  1832. 'span' => array('class' => 'input text error'),
  1833. 'label' => array('for' => 'ContactField'),
  1834. 'Field',
  1835. '/label',
  1836. 'input' => array(
  1837. 'type' => 'text', 'name' => 'data[Contact][field]',
  1838. 'id' => 'ContactField', 'class' => 'form-error'
  1839. ),
  1840. 'Badness!',
  1841. '/span'
  1842. );
  1843. $this->assertTags($result, $expected);
  1844. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1845. $expected = array(
  1846. 'div' => array('class' => 'input text error'),
  1847. 'label' => array('for' => 'ContactField'),
  1848. 'Field',
  1849. '/label',
  1850. 'input' => array(
  1851. 'type' => 'text', 'name' => 'data[Contact][field]',
  1852. 'id' => 'ContactField', 'class' => 'form-error'
  1853. ),
  1854. 'A message to you, Rudy',
  1855. array('div' => array('class' => 'error-message')),
  1856. 'Badness!',
  1857. '/div',
  1858. '/div'
  1859. );
  1860. $this->assertTags($result, $expected);
  1861. $this->Form->setEntity(null);
  1862. $this->Form->setEntity('Contact.field');
  1863. $result = $this->Form->input('Contact.field', array(
  1864. 'after' => 'A message to you, Rudy', 'error' => false
  1865. ));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input text'),
  1868. 'label' => array('for' => 'ContactField'),
  1869. 'Field',
  1870. '/label',
  1871. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1872. 'A message to you, Rudy',
  1873. '/div'
  1874. );
  1875. $this->assertTags($result, $expected);
  1876. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1877. $expected = array(
  1878. 'div' => array('class' => 'input text'),
  1879. 'label' => array('for' => 'ObjectField'),
  1880. 'Field',
  1881. '/label',
  1882. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1883. 'A message to you, Rudy',
  1884. '/div'
  1885. );
  1886. $this->assertTags($result, $expected);
  1887. $Contact->validationErrors['field'] = array('minLength');
  1888. $result = $this->Form->input('Contact.field', array(
  1889. 'error' => array(
  1890. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1891. 'maxLength' => 'login too large'
  1892. )
  1893. ));
  1894. $expected = array(
  1895. 'div' => array('class' => 'input text error'),
  1896. 'label' => array('for' => 'ContactField'),
  1897. 'Field',
  1898. '/label',
  1899. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1900. array('div' => array('class' => 'error-message')),
  1901. 'Le login doit contenir au moins 2 caractères',
  1902. '/div',
  1903. '/div'
  1904. );
  1905. $this->assertTags($result, $expected);
  1906. $Contact->validationErrors['field'] = array('maxLength');
  1907. $result = $this->Form->input('Contact.field', array(
  1908. 'error' => array(
  1909. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1910. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1911. 'maxLength' => 'login too large',
  1912. )
  1913. ));
  1914. $expected = array(
  1915. 'div' => array('class' => 'input text error'),
  1916. 'label' => array('for' => 'ContactField'),
  1917. 'Field',
  1918. '/label',
  1919. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1920. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1921. 'login too large',
  1922. '/span',
  1923. '/div'
  1924. );
  1925. $this->assertTags($result, $expected);
  1926. }
  1927. /**
  1928. * test input() with checkbox creation
  1929. *
  1930. * @return void
  1931. */
  1932. public function testInputCheckbox() {
  1933. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1934. $expected = array(
  1935. 'div' => array('class' => 'input checkbox'),
  1936. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1937. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1938. '/div'
  1939. );
  1940. $this->assertTags($result, $expected);
  1941. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1942. $expected = array(
  1943. 'div' => array('class' => 'input checkbox'),
  1944. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1945. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1950. $expected = array(
  1951. 'div' => array('class' => 'input checkbox'),
  1952. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1953. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1954. '/div'
  1955. );
  1956. $this->assertTags($result, $expected);
  1957. }
  1958. /**
  1959. * test form->input() with time types.
  1960. *
  1961. */
  1962. public function testInputTime() {
  1963. extract($this->dateRegex);
  1964. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1965. $result = explode(':', $result);
  1966. $this->assertRegExp('/option value="23"/', $result[0]);
  1967. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1968. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1969. $result = explode(':', $result);
  1970. $this->assertRegExp('/option value="23"/', $result[0]);
  1971. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1972. $result = $this->Form->input('Model.field', array(
  1973. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1974. ));
  1975. $result = explode(':', $result);
  1976. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1977. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1978. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1979. $result = $this->Form->input('Model.field', array(
  1980. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1981. ));
  1982. $result = explode(':', $result);
  1983. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1984. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1985. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1986. $result = $this->Form->input('prueba', array(
  1987. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1988. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1989. ));
  1990. $result = explode(':', $result);
  1991. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1992. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1993. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1994. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1995. $result = $this->Form->input('Random.start_time', array(
  1996. 'type' => 'time',
  1997. 'selected' => '18:15'
  1998. ));
  1999. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2000. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2001. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2002. }
  2003. /**
  2004. * Test interval + selected near the hour roll over.
  2005. *
  2006. * @return void
  2007. */
  2008. public function testTimeSelectedWithInterval() {
  2009. $result = $this->Form->input('Model.start_time', array(
  2010. 'type' => 'time',
  2011. 'interval' => 15,
  2012. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2013. ));
  2014. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2015. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2016. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2017. $result = $this->Form->input('Model.start_time', array(
  2018. 'type' => 'time',
  2019. 'interval' => 15,
  2020. 'selected' => '2012-10-23 15:57:00'
  2021. ));
  2022. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2023. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2024. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2025. $result = $this->Form->input('Model.start_time', array(
  2026. 'timeFormat' => 24,
  2027. 'type' => 'time',
  2028. 'interval' => 15,
  2029. 'selected' => '15:57'
  2030. ));
  2031. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2032. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2033. $result = $this->Form->input('Model.start_time', array(
  2034. 'timeFormat' => 24,
  2035. 'type' => 'time',
  2036. 'interval' => 15,
  2037. 'selected' => '23:57'
  2038. ));
  2039. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2040. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2041. $result = $this->Form->input('Model.created', array(
  2042. 'timeFormat' => 24,
  2043. 'type' => 'datetime',
  2044. 'interval' => 15,
  2045. 'selected' => '2012-09-30 23:56'
  2046. ));
  2047. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2048. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2049. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2050. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2051. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2052. }
  2053. /**
  2054. * test form->input() with datetime, date and time types
  2055. *
  2056. * @return void
  2057. */
  2058. public function testInputDatetime() {
  2059. extract($this->dateRegex);
  2060. $result = $this->Form->input('prueba', array(
  2061. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2062. 'maxYear' => date('Y') + 1, 'interval' => 15
  2063. ));
  2064. $result = explode(':', $result);
  2065. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2066. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2067. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2068. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2069. //related to ticket #5013
  2070. $result = $this->Form->input('Contact.date', array(
  2071. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2072. ));
  2073. $this->assertRegExp('/class="customClass"/', $result);
  2074. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2075. $result = $this->Form->input('Contact.date', array(
  2076. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2077. ));
  2078. $this->assertRegExp('/id="customIdDay"/', $result);
  2079. $this->assertRegExp('/id="customIdMonth"/', $result);
  2080. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2081. $result = $this->Form->input('Model.field', array(
  2082. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2083. ));
  2084. $this->assertRegExp('/id="customIDDay"/', $result);
  2085. $this->assertRegExp('/id="customIDHour"/', $result);
  2086. $result = explode('</select><select', $result);
  2087. $result = explode(':', $result[1]);
  2088. $this->assertRegExp('/option value="23"/', $result[0]);
  2089. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2090. $result = $this->Form->input('Model.field', array(
  2091. 'type' => 'datetime', 'timeFormat' => 12
  2092. ));
  2093. $result = explode('</select><select', $result);
  2094. $result = explode(':', $result[1]);
  2095. $this->assertRegExp('/option value="12"/', $result[0]);
  2096. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2097. $this->Form->request->data = array('Contact' => array('created' => null));
  2098. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2099. $expected = array(
  2100. 'div' => array('class' => 'input date'),
  2101. 'label' => array('for' => 'ContactCreatedMonth'),
  2102. 'Created',
  2103. '/label',
  2104. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2105. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2106. $monthsRegex,
  2107. '/select', '-',
  2108. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2109. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2110. $daysRegex,
  2111. '/select', '-',
  2112. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2113. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2114. $yearsRegex,
  2115. '/select',
  2116. '/div'
  2117. );
  2118. $this->assertTags($result, $expected);
  2119. $this->Form->request->data = array('Contact' => array('created' => null));
  2120. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2121. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2122. $this->Form->request->data = array('Contact' => array('created' => null));
  2123. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2124. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2125. $result = $this->Form->input('Contact.created', array(
  2126. 'type' => 'date',
  2127. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2128. 'timeFormat' => 'NONE'
  2129. ));
  2130. $this->assertRegExp('/for\="created-month"/', $result);
  2131. }
  2132. /**
  2133. * Test generating checkboxes in a loop.
  2134. *
  2135. * @return void
  2136. */
  2137. public function testInputCheckboxesInLoop() {
  2138. for ($i = 1; $i < 5; $i++) {
  2139. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2140. $expected = array(
  2141. 'div' => array('class' => 'input checkbox'),
  2142. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2143. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2144. 'label' => array('for' => "Contact{$i}Email"),
  2145. 'Email',
  2146. '/label',
  2147. '/div'
  2148. );
  2149. $this->assertTags($result, $expected);
  2150. }
  2151. }
  2152. /**
  2153. * test input name with leading integer, ensure attributes are generated correctly.
  2154. *
  2155. * @return void
  2156. */
  2157. public function testInputWithLeadingInteger() {
  2158. $result = $this->Form->text('0.Node.title');
  2159. $expected = array(
  2160. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2161. );
  2162. $this->assertTags($result, $expected);
  2163. }
  2164. /**
  2165. * test form->input() with select type inputs.
  2166. *
  2167. * @return void
  2168. */
  2169. public function testInputSelectType() {
  2170. $result = $this->Form->input('email', array(
  2171. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2172. );
  2173. $expected = array(
  2174. 'div' => array('class' => 'input select'),
  2175. 'label' => array('for' => 'email'),
  2176. 'Email',
  2177. '/label',
  2178. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2179. array('option' => array('value' => '')),
  2180. '/option',
  2181. array('option' => array('value' => 'è')),
  2182. 'Firést',
  2183. '/option',
  2184. array('option' => array('value' => 'é')),
  2185. 'Secoènd',
  2186. '/option',
  2187. '/select',
  2188. '/div'
  2189. );
  2190. $this->assertTags($result, $expected);
  2191. $result = $this->Form->input('email', array(
  2192. 'options' => array('First', 'Second'), 'empty' => true)
  2193. );
  2194. $expected = array(
  2195. 'div' => array('class' => 'input select'),
  2196. 'label' => array('for' => 'email'),
  2197. 'Email',
  2198. '/label',
  2199. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2200. array('option' => array('value' => '')),
  2201. '/option',
  2202. array('option' => array('value' => '0')),
  2203. 'First',
  2204. '/option',
  2205. array('option' => array('value' => '1')),
  2206. 'Second',
  2207. '/option',
  2208. '/select',
  2209. '/div'
  2210. );
  2211. $this->assertTags($result, $expected);
  2212. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2213. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2214. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2215. $expected = array(
  2216. 'div' => array('class' => 'input select'),
  2217. 'label' => array('for' => 'ModelUserId'),
  2218. 'User',
  2219. '/label',
  2220. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2221. array('option' => array('value' => '')),
  2222. '/option',
  2223. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2224. 'good',
  2225. '/option',
  2226. array('option' => array('value' => 'other')),
  2227. 'bad',
  2228. '/option',
  2229. '/select',
  2230. '/div'
  2231. );
  2232. $this->assertTags($result, $expected);
  2233. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2234. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2235. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2236. $expected = array(
  2237. 'div' => array('class' => 'input select'),
  2238. 'label' => array('for' => 'ThingUserId'),
  2239. 'User',
  2240. '/label',
  2241. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2242. array('option' => array('value' => '')),
  2243. 'Some Empty',
  2244. '/option',
  2245. array('option' => array('value' => 'value')),
  2246. 'good',
  2247. '/option',
  2248. array('option' => array('value' => 'other')),
  2249. 'bad',
  2250. '/option',
  2251. '/select',
  2252. '/div'
  2253. );
  2254. $this->assertTags($result, $expected);
  2255. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2256. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2257. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2258. $expected = array(
  2259. 'div' => array('class' => 'input select'),
  2260. 'label' => array('for' => 'ThingUserId'),
  2261. 'User',
  2262. '/label',
  2263. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2264. array('option' => array('value' => '')),
  2265. 'Some Empty',
  2266. '/option',
  2267. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2268. 'good',
  2269. '/option',
  2270. array('option' => array('value' => 'other')),
  2271. 'bad',
  2272. '/option',
  2273. '/select',
  2274. '/div'
  2275. );
  2276. $this->assertTags($result, $expected);
  2277. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2278. $this->Form->request->data = array('User' => array('User' => array('value')));
  2279. $result = $this->Form->input('User.User', array('empty' => true));
  2280. $expected = array(
  2281. 'div' => array('class' => 'input select'),
  2282. 'label' => array('for' => 'UserUser'),
  2283. 'User',
  2284. '/label',
  2285. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2286. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2287. array('option' => array('value' => '')),
  2288. '/option',
  2289. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2290. 'good',
  2291. '/option',
  2292. array('option' => array('value' => 'other')),
  2293. 'bad',
  2294. '/option',
  2295. '/select',
  2296. '/div'
  2297. );
  2298. $this->assertTags($result, $expected);
  2299. $this->Form->data = array();
  2300. $result = $this->Form->input('Publisher.id', array(
  2301. 'label' => 'Publisher',
  2302. 'type' => 'select',
  2303. 'multiple' => 'checkbox',
  2304. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2305. ));
  2306. $expected = array(
  2307. array('div' => array('class' => 'input select')),
  2308. array('label' => array('for' => 'PublisherId')),
  2309. 'Publisher',
  2310. '/label',
  2311. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2312. array('div' => array('class' => 'checkbox')),
  2313. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2314. array('label' => array('for' => 'PublisherIdValue1')),
  2315. 'Label 1',
  2316. '/label',
  2317. '/div',
  2318. array('div' => array('class' => 'checkbox')),
  2319. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2320. array('label' => array('for' => 'PublisherIdValue2')),
  2321. 'Label 2',
  2322. '/label',
  2323. '/div',
  2324. '/div'
  2325. );
  2326. $this->assertTags($result, $expected);
  2327. }
  2328. /**
  2329. * test that input() and a non standard primary key makes a hidden input by default.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2334. $this->Form->create('User');
  2335. $this->Form->fieldset = array(
  2336. 'User' => array(
  2337. 'fields' => array(
  2338. 'model_id' => array('type' => 'integer')
  2339. ),
  2340. 'validates' => array(),
  2341. 'key' => 'model_id'
  2342. )
  2343. );
  2344. $result = $this->Form->input('model_id');
  2345. $expected = array(
  2346. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2347. );
  2348. $this->assertTags($result, $expected);
  2349. }
  2350. /**
  2351. * test that overriding the magic select type widget is possible
  2352. *
  2353. * @return void
  2354. */
  2355. public function testInputOverridingMagicSelectType() {
  2356. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2357. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2358. $expected = array(
  2359. 'div' => array('class' => 'input text'),
  2360. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2361. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2362. '/div'
  2363. );
  2364. $this->assertTags($result, $expected);
  2365. //Check that magic types still work for plural/singular vars
  2366. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2367. $result = $this->Form->input('Model.type');
  2368. $expected = array(
  2369. 'div' => array('class' => 'input select'),
  2370. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2371. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2372. array('option' => array('value' => 'value')), 'good', '/option',
  2373. array('option' => array('value' => 'other')), 'bad', '/option',
  2374. '/select',
  2375. '/div'
  2376. );
  2377. $this->assertTags($result, $expected);
  2378. }
  2379. /**
  2380. * Test that magic input() selects are created for type=number
  2381. *
  2382. * @return void
  2383. */
  2384. public function testInputMagicSelectForTypeNumber() {
  2385. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2386. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2387. $result = $this->Form->input('ValidateUser.balance');
  2388. $expected = array(
  2389. 'div' => array('class' => 'input select'),
  2390. 'label' => array('for' => 'ValidateUserBalance'),
  2391. 'Balance',
  2392. '/label',
  2393. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2394. array('option' => array('value' => '0')),
  2395. 'nothing',
  2396. '/option',
  2397. array('option' => array('value' => '1', 'selected' => 'selected')),
  2398. 'some',
  2399. '/option',
  2400. array('option' => array('value' => '100')),
  2401. 'a lot',
  2402. '/option',
  2403. '/select',
  2404. '/div'
  2405. );
  2406. $this->assertTags($result, $expected);
  2407. }
  2408. /**
  2409. * Test that magic input() selects can easily be converted into radio types without error.
  2410. *
  2411. * @return void
  2412. */
  2413. public function testInputMagicSelectChangeToRadio() {
  2414. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2415. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2416. $this->assertRegExp('/input type="radio"/', $result);
  2417. }
  2418. /**
  2419. * fields with the same name as the model should work.
  2420. *
  2421. * @return void
  2422. */
  2423. public function testInputWithMatchingFieldAndModelName() {
  2424. $this->Form->create('User');
  2425. $this->Form->fieldset = array(
  2426. 'User' => array(
  2427. 'fields' => array(
  2428. 'User' => array('type' => 'text')
  2429. ),
  2430. 'validates' => array(),
  2431. 'key' => 'id'
  2432. )
  2433. );
  2434. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2435. $result = $this->Form->input('User', array('type' => 'text'));
  2436. $expected = array(
  2437. 'div' => array('class' => 'input text'),
  2438. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2439. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2440. '/div'
  2441. );
  2442. $this->assertTags($result, $expected);
  2443. }
  2444. /**
  2445. * testFormInputs method
  2446. *
  2447. * test correct results from form::inputs().
  2448. *
  2449. * @return void
  2450. */
  2451. public function testFormInputs() {
  2452. $this->Form->create('Contact');
  2453. $result = $this->Form->inputs('The Legend');
  2454. $expected = array(
  2455. '<fieldset',
  2456. '<legend',
  2457. 'The Legend',
  2458. '/legend',
  2459. '*/fieldset',
  2460. );
  2461. $this->assertTags($result, $expected);
  2462. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2463. $expected = array(
  2464. 'fieldset' => array('class' => 'classy-stuff'),
  2465. '<legend',
  2466. 'Field of Dreams',
  2467. '/legend',
  2468. '*/fieldset'
  2469. );
  2470. $this->assertTags($result, $expected);
  2471. $this->Form->create('Contact');
  2472. $this->Form->request['prefix'] = 'admin';
  2473. $this->Form->request['action'] = 'admin_edit';
  2474. $result = $this->Form->inputs();
  2475. $expected = array(
  2476. '<fieldset',
  2477. '<legend',
  2478. 'Edit Contact',
  2479. '/legend',
  2480. '*/fieldset',
  2481. );
  2482. $this->assertTags($result, $expected);
  2483. $this->Form->create('Contact');
  2484. $result = $this->Form->inputs(false);
  2485. $expected = array(
  2486. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2487. array('div' => array('class' => 'input text')),
  2488. '*/div',
  2489. array('div' => array('class' => 'input text')),
  2490. '*/div',
  2491. array('div' => array('class' => 'input text')),
  2492. '*/div',
  2493. array('div' => array('class' => 'input password')),
  2494. '*/div',
  2495. array('div' => array('class' => 'input date')),
  2496. '*/div',
  2497. array('div' => array('class' => 'input date')),
  2498. '*/div',
  2499. array('div' => array('class' => 'input datetime')),
  2500. '*/div',
  2501. array('div' => array('class' => 'input number')),
  2502. '*/div',
  2503. array('div' => array('class' => 'input select')),
  2504. '*/div',
  2505. );
  2506. $this->assertTags($result, $expected);
  2507. $this->Form->create('Contact');
  2508. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2509. $expected = array(
  2510. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2511. array('div' => array('class' => 'input text')),
  2512. '*/div',
  2513. array('div' => array('class' => 'input text')),
  2514. '*/div',
  2515. array('div' => array('class' => 'input text')),
  2516. '*/div',
  2517. array('div' => array('class' => 'input password')),
  2518. '*/div',
  2519. array('div' => array('class' => 'input date')),
  2520. '*/div',
  2521. array('div' => array('class' => 'input date')),
  2522. '*/div',
  2523. array('div' => array('class' => 'input datetime')),
  2524. '*/div',
  2525. array('div' => array('class' => 'input number')),
  2526. '*/div',
  2527. array('div' => array('class' => 'input select')),
  2528. '*/div',
  2529. );
  2530. $this->assertTags($result, $expected);
  2531. $this->Form->create('Contact');
  2532. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2533. $expected = array(
  2534. 'fieldset' => array(),
  2535. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2536. array('div' => array('class' => 'input text')),
  2537. '*/div',
  2538. array('div' => array('class' => 'input text')),
  2539. '*/div',
  2540. array('div' => array('class' => 'input text')),
  2541. '*/div',
  2542. array('div' => array('class' => 'input password')),
  2543. '*/div',
  2544. array('div' => array('class' => 'input date')),
  2545. '*/div',
  2546. array('div' => array('class' => 'input date')),
  2547. '*/div',
  2548. array('div' => array('class' => 'input datetime')),
  2549. '*/div',
  2550. array('div' => array('class' => 'input number')),
  2551. '*/div',
  2552. array('div' => array('class' => 'input select')),
  2553. '*/div',
  2554. '/fieldset'
  2555. );
  2556. $this->assertTags($result, $expected);
  2557. $this->Form->create('Contact');
  2558. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2559. $expected = array(
  2560. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2561. array('div' => array('class' => 'input text')),
  2562. '*/div',
  2563. array('div' => array('class' => 'input text')),
  2564. '*/div',
  2565. array('div' => array('class' => 'input text')),
  2566. '*/div',
  2567. array('div' => array('class' => 'input password')),
  2568. '*/div',
  2569. array('div' => array('class' => 'input date')),
  2570. '*/div',
  2571. array('div' => array('class' => 'input date')),
  2572. '*/div',
  2573. array('div' => array('class' => 'input datetime')),
  2574. '*/div',
  2575. array('div' => array('class' => 'input number')),
  2576. '*/div',
  2577. array('div' => array('class' => 'input select')),
  2578. '*/div',
  2579. );
  2580. $this->assertTags($result, $expected);
  2581. $this->Form->create('Contact');
  2582. $result = $this->Form->inputs('Hello');
  2583. $expected = array(
  2584. 'fieldset' => array(),
  2585. 'legend' => array(),
  2586. 'Hello',
  2587. '/legend',
  2588. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2589. array('div' => array('class' => 'input text')),
  2590. '*/div',
  2591. array('div' => array('class' => 'input text')),
  2592. '*/div',
  2593. array('div' => array('class' => 'input text')),
  2594. '*/div',
  2595. array('div' => array('class' => 'input password')),
  2596. '*/div',
  2597. array('div' => array('class' => 'input date')),
  2598. '*/div',
  2599. array('div' => array('class' => 'input date')),
  2600. '*/div',
  2601. array('div' => array('class' => 'input datetime')),
  2602. '*/div',
  2603. array('div' => array('class' => 'input number')),
  2604. '*/div',
  2605. array('div' => array('class' => 'input select')),
  2606. '*/div',
  2607. '/fieldset'
  2608. );
  2609. $this->assertTags($result, $expected);
  2610. $this->Form->create('Contact');
  2611. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2612. $expected = array(
  2613. 'fieldset' => array(),
  2614. 'legend' => array(),
  2615. 'Hello',
  2616. '/legend',
  2617. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2618. array('div' => array('class' => 'input text')),
  2619. '*/div',
  2620. array('div' => array('class' => 'input text')),
  2621. '*/div',
  2622. array('div' => array('class' => 'input text')),
  2623. '*/div',
  2624. array('div' => array('class' => 'input password')),
  2625. '*/div',
  2626. array('div' => array('class' => 'input date')),
  2627. '*/div',
  2628. array('div' => array('class' => 'input date')),
  2629. '*/div',
  2630. array('div' => array('class' => 'input datetime')),
  2631. '*/div',
  2632. array('div' => array('class' => 'input number')),
  2633. '*/div',
  2634. array('div' => array('class' => 'input select')),
  2635. '*/div',
  2636. '/fieldset'
  2637. );
  2638. $this->assertTags($result, $expected);
  2639. }
  2640. /**
  2641. * testSelectAsCheckbox method
  2642. *
  2643. * test multi-select widget with checkbox formatting.
  2644. *
  2645. * @return void
  2646. */
  2647. public function testSelectAsCheckbox() {
  2648. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2649. $expected = array(
  2650. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2651. array('div' => array('class' => 'checkbox')),
  2652. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2653. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2654. 'first',
  2655. '/label',
  2656. '/div',
  2657. array('div' => array('class' => 'checkbox')),
  2658. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2659. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2660. 'second',
  2661. '/label',
  2662. '/div',
  2663. array('div' => array('class' => 'checkbox')),
  2664. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2665. array('label' => array('for' => 'ModelMultiField2')),
  2666. 'third',
  2667. '/label',
  2668. '/div',
  2669. );
  2670. $this->assertTags($result, $expected);
  2671. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2672. $expected = array(
  2673. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2674. array('div' => array('class' => 'checkbox')),
  2675. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2676. array('label' => array('for' => 'ModelMultiField12')),
  2677. 'half',
  2678. '/label',
  2679. '/div',
  2680. );
  2681. $this->assertTags($result, $expected);
  2682. }
  2683. /**
  2684. * testLabel method
  2685. *
  2686. * test label generation.
  2687. *
  2688. * @return void
  2689. */
  2690. public function testLabel() {
  2691. $this->Form->text('Person.name');
  2692. $result = $this->Form->label();
  2693. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2694. $this->Form->text('Person.name');
  2695. $result = $this->Form->label();
  2696. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2697. $result = $this->Form->label('Person.first_name');
  2698. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2699. $result = $this->Form->label('Person.first_name', 'Your first name');
  2700. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2701. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2702. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2703. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2704. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2705. $result = $this->Form->label('Person.first_name', '');
  2706. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2707. $result = $this->Form->label('Person.2.name', '');
  2708. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2709. }
  2710. /**
  2711. * testTextbox method
  2712. *
  2713. * test textbox element generation
  2714. *
  2715. * @return void
  2716. */
  2717. public function testTextbox() {
  2718. $result = $this->Form->text('Model.field');
  2719. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2720. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2721. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2722. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2723. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2724. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2725. $result = $this->Form->text('Model.text');
  2726. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2727. $Contact = ClassRegistry::getObject('Contact');
  2728. $Contact->validationErrors['text'] = array(true);
  2729. $this->Form->request->data['Contact']['text'] = 'test';
  2730. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2731. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2732. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2733. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2734. $expected = array(
  2735. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2736. );
  2737. $this->assertTags($result, $expected);
  2738. }
  2739. /**
  2740. * testDefaultValue method
  2741. *
  2742. * Test default value setting
  2743. *
  2744. * @return void
  2745. */
  2746. public function testDefaultValue() {
  2747. $this->Form->request->data['Model']['field'] = 'test';
  2748. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2749. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2750. unset($this->Form->request->data['Model']['field']);
  2751. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2752. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2753. }
  2754. /**
  2755. * testCheckboxDefaultValue method
  2756. *
  2757. * Test default value setting on checkbox() method
  2758. *
  2759. * @return void
  2760. */
  2761. public function testCheckboxDefaultValue() {
  2762. $this->Form->request->data['Model']['field'] = false;
  2763. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2764. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2765. unset($this->Form->request->data['Model']['field']);
  2766. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2767. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2768. $this->Form->request->data['Model']['field'] = true;
  2769. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2770. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2771. unset($this->Form->request->data['Model']['field']);
  2772. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2773. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2774. }
  2775. /**
  2776. * testError method
  2777. *
  2778. * Test field error generation
  2779. *
  2780. * @return void
  2781. */
  2782. public function testError() {
  2783. $Contact = ClassRegistry::getObject('Contact');
  2784. $Contact->validationErrors['field'] = array(1);
  2785. $result = $this->Form->error('Contact.field');
  2786. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2787. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2788. $this->assertEquals('Error in field Field', $result);
  2789. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2790. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2791. $this->assertEquals('This field contains invalid input', $result);
  2792. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2793. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2794. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2795. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2796. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2797. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2798. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2799. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2800. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2801. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2802. $this->assertEquals('<strong>Badness!</strong>', $result);
  2803. $Contact->validationErrors['field'] = array("email");
  2804. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2805. $expected = array(
  2806. 'div' => array('class' => 'field-error'),
  2807. 'No good!',
  2808. '/div'
  2809. );
  2810. $this->assertTags($result, $expected);
  2811. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2812. $result = $this->Form->error('Contact.field', array(
  2813. 'notEmpty' => 'Cannot be empty',
  2814. 'email' => 'No good!'
  2815. ));
  2816. $expected = array(
  2817. 'div' => array('class' => 'error-message'),
  2818. 'ul' => array(),
  2819. '<li', 'Cannot be empty', '/li',
  2820. '<li', 'No good!', '/li',
  2821. '<li', 'Something else', '/li',
  2822. '/ul',
  2823. '/div'
  2824. );
  2825. $this->assertTags($result, $expected);
  2826. /** Testing error messages list options **/
  2827. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2828. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2829. $expected = array(
  2830. 'div' => array('class' => 'error-message'),
  2831. 'ol' => array(),
  2832. '<li', 'notEmpty', '/li',
  2833. '<li', 'email', '/li',
  2834. '/ol',
  2835. '/div'
  2836. );
  2837. $this->assertTags($result, $expected);
  2838. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2839. $expected = array(
  2840. 'div' => array('class' => 'error-message'),
  2841. 'ol' => array(),
  2842. '<li', 'notEmpty', '/li',
  2843. '<li', 'email', '/li',
  2844. '/ol',
  2845. '/div'
  2846. );
  2847. $this->assertTags($result, $expected);
  2848. $result = $this->Form->error('Contact.field', null, array(
  2849. 'listOptions' => array(
  2850. 'class' => 'ul-class',
  2851. 'itemOptions' => array(
  2852. 'class' => 'li-class'
  2853. )
  2854. )
  2855. ));
  2856. $expected = array(
  2857. 'div' => array('class' => 'error-message'),
  2858. 'ul' => array('class' => 'ul-class'),
  2859. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2860. array('li' => array('class' => 'li-class')), 'email', '/li',
  2861. '/ul',
  2862. '/div'
  2863. );
  2864. $this->assertTags($result, $expected);
  2865. }
  2866. /**
  2867. * test error options when using form->input();
  2868. *
  2869. * @return void
  2870. */
  2871. public function testInputErrorEscape() {
  2872. $this->Form->create('ValidateProfile');
  2873. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2874. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2875. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2876. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2877. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2878. $this->assertRegExp('/required<br>/', $result);
  2879. }
  2880. /**
  2881. * testPassword method
  2882. *
  2883. * Test password element generation
  2884. *
  2885. * @return void
  2886. */
  2887. public function testPassword() {
  2888. $Contact = ClassRegistry::getObject('Contact');
  2889. $result = $this->Form->password('Contact.field');
  2890. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2891. $Contact->validationErrors['passwd'] = 1;
  2892. $this->Form->request->data['Contact']['passwd'] = 'test';
  2893. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2894. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2895. }
  2896. /**
  2897. * testRadio method
  2898. *
  2899. * Test radio element set generation
  2900. *
  2901. * @return void
  2902. */
  2903. public function testRadio() {
  2904. $result = $this->Form->radio('Model.field', array('option A'));
  2905. $expected = array(
  2906. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2907. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2908. 'label' => array('for' => 'ModelField0'),
  2909. 'option A',
  2910. '/label'
  2911. );
  2912. $this->assertTags($result, $expected);
  2913. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2914. $expected = array(
  2915. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2916. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2917. 'label' => array('for' => 'ModelField12'),
  2918. 'half',
  2919. '/label'
  2920. );
  2921. $this->assertTags($result, $expected);
  2922. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2923. $expected = array(
  2924. 'fieldset' => array(),
  2925. 'legend' => array(),
  2926. 'Field',
  2927. '/legend',
  2928. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2929. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2930. array('label' => array('for' => 'ModelField0')),
  2931. 'option A',
  2932. '/label',
  2933. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2934. array('label' => array('for' => 'ModelField1')),
  2935. 'option B',
  2936. '/label',
  2937. '/fieldset'
  2938. );
  2939. $this->assertTags($result, $expected);
  2940. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2941. $expected = array(
  2942. 'fieldset' => array(),
  2943. 'legend' => array(),
  2944. 'Field',
  2945. '/legend',
  2946. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2947. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2948. array('label' => array('for' => 'ModelField0')),
  2949. 'option A',
  2950. '/label',
  2951. 'br' => array(),
  2952. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2953. array('label' => array('for' => 'ModelField1')),
  2954. 'option B',
  2955. '/label',
  2956. '/fieldset'
  2957. );
  2958. $this->assertTags($result, $expected);
  2959. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2960. $expected = array(
  2961. 'div' => array('class' => 'input radio'),
  2962. 'fieldset' => array(),
  2963. 'legend' => array(),
  2964. 'Legend title',
  2965. '/legend',
  2966. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2967. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2968. array('label' => array('for' => 'NewsletterSubscribe0')),
  2969. 'Unsubscribe',
  2970. '/label',
  2971. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2972. array('label' => array('for' => 'NewsletterSubscribe1')),
  2973. 'Subscribe',
  2974. '/label',
  2975. '/fieldset',
  2976. '/div'
  2977. );
  2978. $this->assertTags($result, $expected);
  2979. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2980. $expected = array(
  2981. 'div' => array('class' => 'input radio'),
  2982. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2983. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2984. array('label' => array('for' => 'NewsletterSubscribe0')),
  2985. 'Unsubscribe',
  2986. '/label',
  2987. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2988. array('label' => array('for' => 'NewsletterSubscribe1')),
  2989. 'Subscribe',
  2990. '/label',
  2991. '/div'
  2992. );
  2993. $this->assertTags($result, $expected);
  2994. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2995. $expected = array(
  2996. 'div' => array('class' => 'input radio'),
  2997. 'fieldset' => array(),
  2998. 'legend' => array(),
  2999. 'Legend title',
  3000. '/legend',
  3001. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3002. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3003. 'Unsubscribe',
  3004. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3005. 'Subscribe',
  3006. '/fieldset',
  3007. '/div'
  3008. );
  3009. $this->assertTags($result, $expected);
  3010. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3011. $expected = array(
  3012. 'div' => array('class' => 'input radio'),
  3013. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3014. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3015. 'Unsubscribe',
  3016. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3017. 'Subscribe',
  3018. '/div'
  3019. );
  3020. $this->assertTags($result, $expected);
  3021. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3022. $expected = array(
  3023. 'div' => array('class' => 'input radio'),
  3024. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3025. 'Unsubscribe',
  3026. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3027. 'Subscribe',
  3028. '/div'
  3029. );
  3030. $this->assertTags($result, $expected);
  3031. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3032. $expected = array(
  3033. 'fieldset' => array(),
  3034. 'legend' => array(),
  3035. 'Gender',
  3036. '/legend',
  3037. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3038. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3039. array('label' => array('for' => 'EmployeeGenderMale')),
  3040. 'Male',
  3041. '/label',
  3042. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3043. array('label' => array('for' => 'EmployeeGenderFemale')),
  3044. 'Female',
  3045. '/label',
  3046. '/fieldset',
  3047. );
  3048. $this->assertTags($result, $expected);
  3049. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3050. $expected = array(
  3051. 'fieldset' => array(),
  3052. 'legend' => array(),
  3053. 'Gender',
  3054. '/legend',
  3055. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3056. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3057. array('label' => array('for' => 'OfficerGenderMale')),
  3058. 'Male',
  3059. '/label',
  3060. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3061. array('label' => array('for' => 'OfficerGenderFemale')),
  3062. 'Female',
  3063. '/label',
  3064. '/fieldset',
  3065. );
  3066. $this->assertTags($result, $expected);
  3067. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3068. $expected = array(
  3069. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3070. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3071. 'label' => array('for' => 'Contact1Imrequired0'),
  3072. 'option A',
  3073. '/label'
  3074. );
  3075. $this->assertTags($result, $expected);
  3076. $result = $this->Form->radio('Model.1.field', array('option A'));
  3077. $expected = array(
  3078. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3079. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3080. 'label' => array('for' => 'Model1Field0'),
  3081. 'option A',
  3082. '/label'
  3083. );
  3084. $this->assertTags($result, $expected);
  3085. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3086. $expected = array(
  3087. 'fieldset' => array(),
  3088. 'legend' => array(),
  3089. 'Field',
  3090. '/legend',
  3091. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3092. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3093. array('label' => array('for' => 'ModelField0')),
  3094. 'option A',
  3095. '/label',
  3096. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3097. array('label' => array('for' => 'ModelField1')),
  3098. 'option B',
  3099. '/label',
  3100. '/fieldset'
  3101. );
  3102. $this->assertTags($result, $expected);
  3103. $result = $this->Form->radio(
  3104. 'Model.field',
  3105. array('option A', 'option B'),
  3106. array('between' => 'I am between')
  3107. );
  3108. $expected = array(
  3109. 'fieldset' => array(),
  3110. 'legend' => array(),
  3111. 'Field',
  3112. '/legend',
  3113. 'I am between',
  3114. 'input' => array(
  3115. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3116. 'value' => '', 'id' => 'ModelField_'
  3117. ),
  3118. array('input' => array(
  3119. 'type' => 'radio', 'name' => 'data[Model][field]',
  3120. 'value' => '0', 'id' => 'ModelField0'
  3121. )),
  3122. array('label' => array('for' => 'ModelField0')),
  3123. 'option A',
  3124. '/label',
  3125. array('input' => array(
  3126. 'type' => 'radio', 'name' => 'data[Model][field]',
  3127. 'value' => '1', 'id' => 'ModelField1'
  3128. )),
  3129. array('label' => array('for' => 'ModelField1')),
  3130. 'option B',
  3131. '/label',
  3132. '/fieldset'
  3133. );
  3134. $this->assertTags($result, $expected);
  3135. }
  3136. /**
  3137. * Test that radios with a 0 value are selected under the correct conditions.
  3138. *
  3139. * @return void
  3140. */
  3141. public function testRadioOptionWithZeroValue() {
  3142. $expected = array(
  3143. 'fieldset' => array(),
  3144. 'legend' => array(),
  3145. 'Field',
  3146. '/legend',
  3147. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3148. array('label' => array('for' => 'ModelField1')),
  3149. 'Yes',
  3150. '/label',
  3151. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3152. array('label' => array('for' => 'ModelField0')),
  3153. 'No',
  3154. '/label',
  3155. '/fieldset'
  3156. );
  3157. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3158. $this->assertTags($result, $expected);
  3159. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3160. $this->assertTags($result, $expected);
  3161. $expected = array(
  3162. 'fieldset' => array(),
  3163. 'legend' => array(),
  3164. 'Field',
  3165. '/legend',
  3166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3168. array('label' => array('for' => 'ModelField1')),
  3169. 'Yes',
  3170. '/label',
  3171. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3172. array('label' => array('for' => 'ModelField0')),
  3173. 'No',
  3174. '/label',
  3175. '/fieldset'
  3176. );
  3177. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3178. $this->assertTags($result, $expected);
  3179. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3180. $this->assertTags($result, $expected);
  3181. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3182. $this->assertTags($result, $expected);
  3183. }
  3184. /**
  3185. * test disabled radio options
  3186. *
  3187. * @return void
  3188. */
  3189. public function testRadioDisabled() {
  3190. $result = $this->Form->radio(
  3191. 'Model.field',
  3192. array('option A', 'option B'),
  3193. array('disabled' => array('option A'), 'value' => '0')
  3194. );
  3195. $expected = array(
  3196. 'fieldset' => array(),
  3197. 'legend' => array(),
  3198. 'Field',
  3199. '/legend',
  3200. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3201. array('label' => array('for' => 'ModelField0')),
  3202. 'option A',
  3203. '/label',
  3204. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3205. array('label' => array('for' => 'ModelField1')),
  3206. 'option B',
  3207. '/label',
  3208. '/fieldset'
  3209. );
  3210. $this->assertTags($result, $expected);
  3211. $result = $this->Form->radio(
  3212. 'Model.field',
  3213. array('option A', 'option B'),
  3214. array('disabled' => true, 'value' => '0')
  3215. );
  3216. $expected = array(
  3217. 'fieldset' => array(),
  3218. 'legend' => array(),
  3219. 'Field',
  3220. '/legend',
  3221. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3222. array('label' => array('for' => 'ModelField0')),
  3223. 'option A',
  3224. '/label',
  3225. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3226. array('label' => array('for' => 'ModelField1')),
  3227. 'option B',
  3228. '/label',
  3229. '/fieldset'
  3230. );
  3231. $this->assertTags($result, $expected);
  3232. $result = $this->Form->radio(
  3233. 'Model.field',
  3234. array('option A', 'option B'),
  3235. array('disabled' => 'disabled', 'value' => '0')
  3236. );
  3237. $expected = array(
  3238. 'fieldset' => array(),
  3239. 'legend' => array(),
  3240. 'Field',
  3241. '/legend',
  3242. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3243. array('label' => array('for' => 'ModelField0')),
  3244. 'option A',
  3245. '/label',
  3246. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3247. array('label' => array('for' => 'ModelField1')),
  3248. 'option B',
  3249. '/label',
  3250. '/fieldset'
  3251. );
  3252. $this->assertTags($result, $expected);
  3253. }
  3254. /**
  3255. * test disabling the hidden input for radio buttons
  3256. *
  3257. * @return void
  3258. */
  3259. public function testRadioHiddenInputDisabling() {
  3260. $result = $this->Form->input('Model.1.field', array(
  3261. 'type' => 'radio',
  3262. 'options' => array('option A'),
  3263. 'hiddenField' => false
  3264. )
  3265. );
  3266. $expected = array(
  3267. 'div' => array('class' => 'input radio'),
  3268. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3269. 'label' => array('for' => 'Model1Field0'),
  3270. 'option A',
  3271. '/label',
  3272. '/div'
  3273. );
  3274. $this->assertTags($result, $expected);
  3275. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3276. $expected = array(
  3277. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3278. 'label' => array('for' => 'Model1Field0'),
  3279. 'option A',
  3280. '/label'
  3281. );
  3282. $this->assertTags($result, $expected);
  3283. }
  3284. /**
  3285. * test adding an empty option for radio buttons
  3286. *
  3287. * @return void
  3288. */
  3289. public function testRadioAddEmptyOption() {
  3290. $result = $this->Form->input('Model.1.field', array(
  3291. 'type' => 'radio',
  3292. 'options' => array('option A'),
  3293. 'empty' => true,
  3294. 'hiddenField' => false
  3295. ));
  3296. $expected = array(
  3297. 'div' => array('class' => 'input radio'),
  3298. 'fieldset' => array(),
  3299. 'legend' => array(),
  3300. 'Field',
  3301. '/legend',
  3302. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3303. array('label' => array('for' => 'Model1Field')),
  3304. __('empty'),
  3305. '/label',
  3306. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3307. array('label' => array('for' => 'Model1Field0')),
  3308. 'option A',
  3309. '/label',
  3310. '/fieldset',
  3311. '/div'
  3312. );
  3313. $this->assertTags($result, $expected);
  3314. $result = $this->Form->input('Model.1.field', array(
  3315. 'type' => 'radio',
  3316. 'options' => array('option A'),
  3317. 'empty' => 'CustomEmptyLabel',
  3318. 'hiddenField' => false
  3319. ));
  3320. $expected = array(
  3321. 'div' => array('class' => 'input radio'),
  3322. 'fieldset' => array(),
  3323. 'legend' => array(),
  3324. 'Field',
  3325. '/legend',
  3326. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3327. array('label' => array('for' => 'Model1Field')),
  3328. 'CustomEmptyLabel',
  3329. '/label',
  3330. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3331. array('label' => array('for' => 'Model1Field0')),
  3332. 'option A',
  3333. '/label',
  3334. '/fieldset',
  3335. '/div'
  3336. );
  3337. $this->assertTags($result, $expected);
  3338. $result = $this->Form->input('Model.1.field', array(
  3339. 'type' => 'radio',
  3340. 'options' => array('option A'),
  3341. 'empty' => false,
  3342. 'hiddenField' => false
  3343. ));
  3344. $this->assertTextNotContains('"Model1Field"', $result);
  3345. }
  3346. /**
  3347. * testSelect method
  3348. *
  3349. * Test select element generation.
  3350. *
  3351. * @return void
  3352. */
  3353. public function testSelect() {
  3354. $result = $this->Form->select('Model.field', array());
  3355. $expected = array(
  3356. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3357. array('option' => array('value' => '')),
  3358. '/option',
  3359. '/select'
  3360. );
  3361. $this->assertTags($result, $expected);
  3362. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3363. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3364. $expected = array(
  3365. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3366. array('option' => array('value' => '')),
  3367. '/option',
  3368. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3369. 'good',
  3370. '/option',
  3371. array('option' => array('value' => 'other')),
  3372. 'bad',
  3373. '/option',
  3374. '/select'
  3375. );
  3376. $this->assertTags($result, $expected);
  3377. $this->Form->request->data = array();
  3378. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3379. $expected = array(
  3380. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3381. array('option' => array('value' => '')),
  3382. '/option',
  3383. array('option' => array('value' => 'value')),
  3384. 'good',
  3385. '/option',
  3386. array('option' => array('value' => 'other')),
  3387. 'bad',
  3388. '/option',
  3389. '/select'
  3390. );
  3391. $this->assertTags($result, $expected);
  3392. $result = $this->Form->select(
  3393. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3394. array('empty' => false)
  3395. );
  3396. $expected = array(
  3397. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3398. array('option' => array('value' => 'first')),
  3399. 'first &quot;html&quot; &lt;chars&gt;',
  3400. '/option',
  3401. array('option' => array('value' => 'second')),
  3402. 'value',
  3403. '/option',
  3404. '/select'
  3405. );
  3406. $this->assertTags($result, $expected);
  3407. $result = $this->Form->select(
  3408. 'Model.field',
  3409. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3410. array('escape' => false, 'empty' => false)
  3411. );
  3412. $expected = array(
  3413. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3414. array('option' => array('value' => 'first')),
  3415. 'first "html" <chars>',
  3416. '/option',
  3417. array('option' => array('value' => 'second')),
  3418. 'value',
  3419. '/option',
  3420. '/select'
  3421. );
  3422. $this->assertTags($result, $expected);
  3423. $options = array(
  3424. array('value' => 'first', 'name' => 'First'),
  3425. array('value' => 'first', 'name' => 'Another First'),
  3426. );
  3427. $result = $this->Form->select(
  3428. 'Model.field',
  3429. $options,
  3430. array('escape' => false, 'empty' => false)
  3431. );
  3432. $expected = array(
  3433. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3434. array('option' => array('value' => 'first')),
  3435. 'First',
  3436. '/option',
  3437. array('option' => array('value' => 'first')),
  3438. 'Another First',
  3439. '/option',
  3440. '/select'
  3441. );
  3442. $this->assertTags($result, $expected);
  3443. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3444. $result = $this->Form->select(
  3445. 'Model.contact_id',
  3446. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3447. array('escape' => false, 'empty' => 'pick something')
  3448. );
  3449. $expected = array(
  3450. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3451. array('option' => array('value' => '')), 'pick something', '/option',
  3452. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3453. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3454. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3455. '/select'
  3456. );
  3457. $this->assertTags($result, $expected);
  3458. $this->Form->request->data['Model']['field'] = 0;
  3459. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3460. $expected = array(
  3461. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3462. array('option' => array('value' => '')), '/option',
  3463. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3464. array('option' => array('value' => '1')), 'Yes', '/option',
  3465. '/select'
  3466. );
  3467. $this->assertTags($result, $expected);
  3468. }
  3469. /**
  3470. * test that select() with optiongroups listens to the escape param.
  3471. *
  3472. * @return void
  3473. */
  3474. public function testSelectOptionGroupEscaping() {
  3475. $options = array(
  3476. '>< Key' => array(
  3477. 1 => 'One',
  3478. 2 => 'Two'
  3479. )
  3480. );
  3481. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3482. $expected = array(
  3483. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3484. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3485. array('option' => array('value' => '1')), 'One', '/option',
  3486. array('option' => array('value' => '2')), 'Two', '/option',
  3487. '/optgroup',
  3488. '/select'
  3489. );
  3490. $this->assertTags($result, $expected);
  3491. $options = array(
  3492. '>< Key' => array(
  3493. 1 => 'One',
  3494. 2 => 'Two'
  3495. )
  3496. );
  3497. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3498. $expected = array(
  3499. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3500. 'optgroup' => array('label' => '>< Key'),
  3501. array('option' => array('value' => '1')), 'One', '/option',
  3502. array('option' => array('value' => '2')), 'Two', '/option',
  3503. '/optgroup',
  3504. '/select'
  3505. );
  3506. $this->assertTags($result, $expected);
  3507. }
  3508. /**
  3509. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3510. *
  3511. * @return void
  3512. */
  3513. public function testSelectWithNullAttributes() {
  3514. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3515. $expected = array(
  3516. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3517. array('option' => array('value' => '0')),
  3518. 'first',
  3519. '/option',
  3520. array('option' => array('value' => '1')),
  3521. 'second',
  3522. '/option',
  3523. '/select'
  3524. );
  3525. $this->assertTags($result, $expected);
  3526. }
  3527. /**
  3528. * testNestedSelect method
  3529. *
  3530. * test select element generation with optgroups
  3531. *
  3532. * @return void
  3533. */
  3534. public function testNestedSelect() {
  3535. $result = $this->Form->select(
  3536. 'Model.field',
  3537. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3538. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3539. )), array('empty' => false)
  3540. );
  3541. $expected = array(
  3542. 'select' => array('name' => 'data[Model][field]',
  3543. 'id' => 'ModelField'),
  3544. array('option' => array('value' => 1)),
  3545. 'One',
  3546. '/option',
  3547. array('option' => array('value' => 2)),
  3548. 'Two',
  3549. '/option',
  3550. array('optgroup' => array('label' => 'Three')),
  3551. array('option' => array('value' => 4)),
  3552. 'Four',
  3553. '/option',
  3554. array('option' => array('value' => 5)),
  3555. 'Five',
  3556. '/option',
  3557. '/optgroup',
  3558. '/select'
  3559. );
  3560. $this->assertTags($result, $expected);
  3561. $result = $this->Form->select(
  3562. 'Model.field',
  3563. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3564. array('showParents' => true, 'empty' => false)
  3565. );
  3566. $expected = array(
  3567. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3568. array('option' => array('value' => 1)),
  3569. 'One',
  3570. '/option',
  3571. array('option' => array('value' => 2)),
  3572. 'Two',
  3573. '/option',
  3574. array('optgroup' => array('label' => 'Three')),
  3575. array('option' => array('value' => 3)),
  3576. 'Three',
  3577. '/option',
  3578. array('option' => array('value' => 4)),
  3579. 'Four',
  3580. '/option',
  3581. '/optgroup',
  3582. '/select'
  3583. );
  3584. $this->assertTags($result, $expected);
  3585. }
  3586. /**
  3587. * testSelectMultiple method
  3588. *
  3589. * test generation of multiple select elements
  3590. *
  3591. * @return void
  3592. */
  3593. public function testSelectMultiple() {
  3594. $options = array('first', 'second', 'third');
  3595. $result = $this->Form->select(
  3596. 'Model.multi_field', $options, array('multiple' => true)
  3597. );
  3598. $expected = array(
  3599. 'input' => array(
  3600. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3601. ),
  3602. 'select' => array(
  3603. 'name' => 'data[Model][multi_field][]',
  3604. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3605. ),
  3606. array('option' => array('value' => '0')),
  3607. 'first',
  3608. '/option',
  3609. array('option' => array('value' => '1')),
  3610. 'second',
  3611. '/option',
  3612. array('option' => array('value' => '2')),
  3613. 'third',
  3614. '/option',
  3615. '/select'
  3616. );
  3617. $this->assertTags($result, $expected);
  3618. $result = $this->Form->select(
  3619. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3620. );
  3621. $expected = array(
  3622. 'input' => array(
  3623. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3624. ),
  3625. 'select' => array(
  3626. 'name' => 'data[Model][multi_field][]',
  3627. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3628. ),
  3629. array('option' => array('value' => '0')),
  3630. 'first',
  3631. '/option',
  3632. array('option' => array('value' => '1')),
  3633. 'second',
  3634. '/option',
  3635. array('option' => array('value' => '2')),
  3636. 'third',
  3637. '/option',
  3638. '/select'
  3639. );
  3640. $this->assertTags($result, $expected);
  3641. $result = $this->Form->select(
  3642. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3643. );
  3644. $expected = array(
  3645. 'input' => array(
  3646. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3647. ),
  3648. 'select' => array(
  3649. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3650. 'multiple' => 'multiple'
  3651. ),
  3652. array('option' => array('value' => '0', 'selected' => 'selected')),
  3653. 'first',
  3654. '/option',
  3655. array('option' => array('value' => '1', 'selected' => 'selected')),
  3656. 'second',
  3657. '/option',
  3658. array('option' => array('value' => '2')),
  3659. 'third',
  3660. '/option',
  3661. '/select'
  3662. );
  3663. $this->assertTags($result, $expected);
  3664. $result = $this->Form->select(
  3665. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3666. );
  3667. $expected = array(
  3668. 'select' => array(
  3669. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3670. ),
  3671. array('option' => array('value' => '0', 'selected' => 'selected')),
  3672. 'first',
  3673. '/option',
  3674. array('option' => array('value' => '1', 'selected' => 'selected')),
  3675. 'second',
  3676. '/option',
  3677. array('option' => array('value' => '2')),
  3678. 'third',
  3679. '/option',
  3680. '/select'
  3681. );
  3682. $this->assertTags($result, $expected);
  3683. }
  3684. /**
  3685. * test generation of habtm select boxes.
  3686. *
  3687. * @return void
  3688. */
  3689. public function testHabtmSelectBox() {
  3690. $this->View->viewVars['contactTags'] = array(
  3691. 1 => 'blue',
  3692. 2 => 'red',
  3693. 3 => 'green'
  3694. );
  3695. $this->Form->request->data = array(
  3696. 'Contact' => array(),
  3697. 'ContactTag' => array(
  3698. array(
  3699. 'id' => 1,
  3700. 'name' => 'blue'
  3701. ),
  3702. array(
  3703. 'id' => 3,
  3704. 'name' => 'green'
  3705. )
  3706. )
  3707. );
  3708. $this->Form->create('Contact');
  3709. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3710. $expected = array(
  3711. 'input' => array(
  3712. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3713. ),
  3714. 'select' => array(
  3715. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3716. 'multiple' => 'multiple'
  3717. ),
  3718. array('option' => array('value' => '1', 'selected' => 'selected')),
  3719. 'blue',
  3720. '/option',
  3721. array('option' => array('value' => '2')),
  3722. 'red',
  3723. '/option',
  3724. array('option' => array('value' => '3', 'selected' => 'selected')),
  3725. 'green',
  3726. '/option',
  3727. '/select'
  3728. );
  3729. $this->assertTags($result, $expected);
  3730. }
  3731. /**
  3732. * test generation of multi select elements in checkbox format
  3733. *
  3734. * @return void
  3735. */
  3736. public function testSelectMultipleCheckboxes() {
  3737. $result = $this->Form->select(
  3738. 'Model.multi_field',
  3739. array('first', 'second', 'third'),
  3740. array('multiple' => 'checkbox')
  3741. );
  3742. $expected = array(
  3743. 'input' => array(
  3744. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3745. ),
  3746. array('div' => array('class' => 'checkbox')),
  3747. array('input' => array(
  3748. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3749. 'value' => '0', 'id' => 'ModelMultiField0'
  3750. )),
  3751. array('label' => array('for' => 'ModelMultiField0')),
  3752. 'first',
  3753. '/label',
  3754. '/div',
  3755. array('div' => array('class' => 'checkbox')),
  3756. array('input' => array(
  3757. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3758. 'value' => '1', 'id' => 'ModelMultiField1'
  3759. )),
  3760. array('label' => array('for' => 'ModelMultiField1')),
  3761. 'second',
  3762. '/label',
  3763. '/div',
  3764. array('div' => array('class' => 'checkbox')),
  3765. array('input' => array(
  3766. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3767. 'value' => '2', 'id' => 'ModelMultiField2'
  3768. )),
  3769. array('label' => array('for' => 'ModelMultiField2')),
  3770. 'third',
  3771. '/label',
  3772. '/div'
  3773. );
  3774. $this->assertTags($result, $expected);
  3775. $result = $this->Form->select(
  3776. 'Model.multi_field',
  3777. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3778. array('multiple' => 'checkbox')
  3779. );
  3780. $expected = array(
  3781. 'input' => array(
  3782. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3783. ),
  3784. array('div' => array('class' => 'checkbox')),
  3785. array('input' => array(
  3786. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3787. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3788. )),
  3789. array('label' => array('for' => 'ModelMultiFieldA')),
  3790. 'first',
  3791. '/label',
  3792. '/div',
  3793. array('div' => array('class' => 'checkbox')),
  3794. array('input' => array(
  3795. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3796. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3797. )),
  3798. array('label' => array('for' => 'ModelMultiFieldB')),
  3799. 'second',
  3800. '/label',
  3801. '/div',
  3802. array('div' => array('class' => 'checkbox')),
  3803. array('input' => array(
  3804. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3805. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3806. )),
  3807. array('label' => array('for' => 'ModelMultiFieldC')),
  3808. 'third',
  3809. '/label',
  3810. '/div'
  3811. );
  3812. $this->assertTags($result, $expected);
  3813. $result = $this->Form->select(
  3814. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3815. );
  3816. $expected = array(
  3817. 'input' => array(
  3818. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3819. ),
  3820. array('div' => array('class' => 'checkbox')),
  3821. array('input' => array(
  3822. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3823. 'value' => '1', 'id' => 'ModelMultiField1'
  3824. )),
  3825. array('label' => array('for' => 'ModelMultiField1')),
  3826. 'first',
  3827. '/label',
  3828. '/div'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3832. $result = $this->Form->select(
  3833. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3834. );
  3835. $expected = array(
  3836. 'input' => array(
  3837. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3838. ),
  3839. array('div' => array('class' => 'checkbox')),
  3840. array('input' => array(
  3841. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3842. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3843. )),
  3844. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3845. 'first',
  3846. '/label',
  3847. '/div',
  3848. array('div' => array('class' => 'checkbox')),
  3849. array('input' => array(
  3850. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3851. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3852. )),
  3853. array('label' => array('for' => 'ModelTagsArray')),
  3854. 'Array',
  3855. '/label',
  3856. '/div'
  3857. );
  3858. $this->assertTags($result, $expected);
  3859. }
  3860. /**
  3861. * test multiple checkboxes with div styles.
  3862. *
  3863. * @return void
  3864. */
  3865. public function testSelectMultipleCheckboxDiv() {
  3866. $result = $this->Form->select(
  3867. 'Model.tags',
  3868. array('first', 'second'),
  3869. array('multiple' => 'checkbox', 'class' => 'my-class')
  3870. );
  3871. $expected = array(
  3872. 'input' => array(
  3873. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3874. ),
  3875. array('div' => array('class' => 'my-class')),
  3876. array('input' => array(
  3877. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3878. 'value' => '0', 'id' => 'ModelTags0'
  3879. )),
  3880. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3881. '/div',
  3882. array('div' => array('class' => 'my-class')),
  3883. array('input' => array(
  3884. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3885. 'value' => '1', 'id' => 'ModelTags1'
  3886. )),
  3887. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3888. '/div'
  3889. );
  3890. $this->assertTags($result, $expected);
  3891. $result = $this->Form->input('Model.tags', array(
  3892. 'options' => array('first', 'second'),
  3893. 'multiple' => 'checkbox',
  3894. 'class' => 'my-class',
  3895. 'div' => false,
  3896. 'label' => false
  3897. ));
  3898. $this->assertTags($result, $expected);
  3899. $Contact = ClassRegistry::getObject('Contact');
  3900. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3901. $result = $this->Form->input('Contact.tags', array(
  3902. 'options' => array('one'),
  3903. 'multiple' => 'checkbox',
  3904. 'label' => false,
  3905. 'div' => false
  3906. ));
  3907. $expected = array(
  3908. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3909. array('div' => array('class' => 'checkbox form-error')),
  3910. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3911. array('label' => array('for' => 'ContactTags0')),
  3912. 'one',
  3913. '/label',
  3914. '/div'
  3915. );
  3916. $this->assertTags($result, $expected);
  3917. $result = $this->Form->input('Contact.tags', array(
  3918. 'options' => array('one'),
  3919. 'multiple' => 'checkbox',
  3920. 'class' => 'mycheckbox',
  3921. 'label' => false,
  3922. 'div' => false
  3923. ));
  3924. $expected = array(
  3925. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3926. array('div' => array('class' => 'mycheckbox form-error')),
  3927. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3928. array('label' => array('for' => 'ContactTags0')),
  3929. 'one',
  3930. '/label',
  3931. '/div'
  3932. );
  3933. $this->assertTags($result, $expected);
  3934. }
  3935. /**
  3936. * Checks the security hash array generated for multiple-input checkbox elements
  3937. *
  3938. * @return void
  3939. */
  3940. public function testSelectMultipleCheckboxSecurity() {
  3941. $this->Form->request['_Token'] = array('key' => 'testKey');
  3942. $this->assertEquals(array(), $this->Form->fields);
  3943. $result = $this->Form->select(
  3944. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3945. array('multiple' => 'checkbox')
  3946. );
  3947. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3948. $result = $this->Form->secure($this->Form->fields);
  3949. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3950. $this->assertRegExp('/"' . $key . '"/', $result);
  3951. }
  3952. /**
  3953. * Multiple select elements should always be secured as they always participate
  3954. * in the POST data.
  3955. *
  3956. * @return void
  3957. */
  3958. public function testSelectMultipleSecureWithNoOptions() {
  3959. $this->Form->request['_Token'] = array('key' => 'testkey');
  3960. $this->assertEquals(array(), $this->Form->fields);
  3961. $result = $this->Form->select(
  3962. 'Model.select',
  3963. array(),
  3964. array('multiple' => true)
  3965. );
  3966. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3967. }
  3968. /**
  3969. * When a select box has no options it should not be added to the fields list
  3970. * as it always fail post validation.
  3971. *
  3972. * @return void
  3973. */
  3974. public function testSelectNoSecureWithNoOptions() {
  3975. $this->Form->request['_Token'] = array('key' => 'testkey');
  3976. $this->assertEquals(array(), $this->Form->fields);
  3977. $this->Form->select(
  3978. 'Model.select',
  3979. array()
  3980. );
  3981. $this->assertEquals(array(), $this->Form->fields);
  3982. $this->Form->select(
  3983. 'Model.select',
  3984. array(),
  3985. array('empty' => true)
  3986. );
  3987. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3988. }
  3989. /**
  3990. * testInputMultipleCheckboxes method
  3991. *
  3992. * test input() resulting in multi select elements being generated.
  3993. *
  3994. * @return void
  3995. */
  3996. public function testInputMultipleCheckboxes() {
  3997. $result = $this->Form->input('Model.multi_field', array(
  3998. 'options' => array('first', 'second', 'third'),
  3999. 'multiple' => 'checkbox'
  4000. ));
  4001. $expected = array(
  4002. array('div' => array('class' => 'input select')),
  4003. array('label' => array('for' => 'ModelMultiField')),
  4004. 'Multi Field',
  4005. '/label',
  4006. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4007. array('div' => array('class' => 'checkbox')),
  4008. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4009. array('label' => array('for' => 'ModelMultiField0')),
  4010. 'first',
  4011. '/label',
  4012. '/div',
  4013. array('div' => array('class' => 'checkbox')),
  4014. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4015. array('label' => array('for' => 'ModelMultiField1')),
  4016. 'second',
  4017. '/label',
  4018. '/div',
  4019. array('div' => array('class' => 'checkbox')),
  4020. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4021. array('label' => array('for' => 'ModelMultiField2')),
  4022. 'third',
  4023. '/label',
  4024. '/div',
  4025. '/div'
  4026. );
  4027. $this->assertTags($result, $expected);
  4028. $result = $this->Form->input('Model.multi_field', array(
  4029. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4030. 'multiple' => 'checkbox'
  4031. ));
  4032. $expected = array(
  4033. array('div' => array('class' => 'input select')),
  4034. array('label' => array('for' => 'ModelMultiField')),
  4035. 'Multi Field',
  4036. '/label',
  4037. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4038. array('div' => array('class' => 'checkbox')),
  4039. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4040. array('label' => array('for' => 'ModelMultiFieldA')),
  4041. 'first',
  4042. '/label',
  4043. '/div',
  4044. array('div' => array('class' => 'checkbox')),
  4045. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4046. array('label' => array('for' => 'ModelMultiFieldB')),
  4047. 'second',
  4048. '/label',
  4049. '/div',
  4050. array('div' => array('class' => 'checkbox')),
  4051. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4052. array('label' => array('for' => 'ModelMultiFieldC')),
  4053. 'third',
  4054. '/label',
  4055. '/div',
  4056. '/div'
  4057. );
  4058. $this->assertTags($result, $expected);
  4059. $result = $this->Form->input('Model.multi_field', array(
  4060. 'options' => array('1' => 'first'),
  4061. 'multiple' => 'checkbox',
  4062. 'label' => false,
  4063. 'div' => false
  4064. ));
  4065. $expected = array(
  4066. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4067. array('div' => array('class' => 'checkbox')),
  4068. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4069. array('label' => array('for' => 'ModelMultiField1')),
  4070. 'first',
  4071. '/label',
  4072. '/div'
  4073. );
  4074. $this->assertTags($result, $expected);
  4075. $result = $this->Form->input('Model.multi_field', array(
  4076. 'options' => array('2' => 'second'),
  4077. 'multiple' => 'checkbox',
  4078. 'label' => false,
  4079. 'div' => false
  4080. ));
  4081. $expected = array(
  4082. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4083. array('div' => array('class' => 'checkbox')),
  4084. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4085. array('label' => array('for' => 'ModelMultiField2')),
  4086. 'second',
  4087. '/label',
  4088. '/div'
  4089. );
  4090. $this->assertTags($result, $expected);
  4091. }
  4092. /**
  4093. * testSelectHiddenFieldOmission method
  4094. *
  4095. * test that select() with 'hiddenField' => false omits the hidden field
  4096. *
  4097. * @return void
  4098. */
  4099. public function testSelectHiddenFieldOmission() {
  4100. $result = $this->Form->select('Model.multi_field',
  4101. array('first', 'second'),
  4102. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4103. );
  4104. $expected = array(
  4105. array('div' => array('class' => 'checkbox')),
  4106. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4107. array('label' => array('for' => 'ModelMultiField0')),
  4108. 'first',
  4109. '/label',
  4110. '/div',
  4111. array('div' => array('class' => 'checkbox')),
  4112. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4113. array('label' => array('for' => 'ModelMultiField1')),
  4114. 'second',
  4115. '/label',
  4116. '/div'
  4117. );
  4118. $this->assertTags($result, $expected);
  4119. $result = $this->Form->input('Model.multi_field', array(
  4120. 'options' => array('first', 'second'),
  4121. 'multiple' => 'checkbox',
  4122. 'hiddenField' => false
  4123. ));
  4124. $expected = array(
  4125. array('div' => array('class' => 'input select')),
  4126. array('label' => array('for' => 'ModelMultiField')),
  4127. 'Multi Field',
  4128. '/label',
  4129. array('div' => array('class' => 'checkbox')),
  4130. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4131. array('label' => array('for' => 'ModelMultiField0')),
  4132. 'first',
  4133. '/label',
  4134. '/div',
  4135. array('div' => array('class' => 'checkbox')),
  4136. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4137. array('label' => array('for' => 'ModelMultiField1')),
  4138. 'second',
  4139. '/label',
  4140. '/div',
  4141. '/div'
  4142. );
  4143. $this->assertTags($result, $expected);
  4144. }
  4145. /**
  4146. * test that select() with multiple = checkbox works with overriding name attribute.
  4147. *
  4148. * @return void
  4149. */
  4150. public function testSelectCheckboxMultipleOverrideName() {
  4151. $result = $this->Form->input('category', array(
  4152. 'type' => 'select',
  4153. 'multiple' => 'checkbox',
  4154. 'name' => 'data[fish]',
  4155. 'options' => array('1', '2'),
  4156. 'div' => false,
  4157. 'label' => false,
  4158. ));
  4159. $expected = array(
  4160. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4161. array('div' => array('class' => 'checkbox')),
  4162. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4163. array('label' => array('for' => 'Category0')), '1', '/label',
  4164. '/div',
  4165. array('div' => array('class' => 'checkbox')),
  4166. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4167. array('label' => array('for' => 'Category1')), '2', '/label',
  4168. '/div'
  4169. );
  4170. $this->assertTags($result, $expected);
  4171. }
  4172. /**
  4173. * Test that 'id' overrides all the checkbox id's as well.
  4174. *
  4175. * @return void
  4176. */
  4177. public function testSelectCheckboxMultipleId() {
  4178. $result = $this->Form->select(
  4179. 'Model.multi_field',
  4180. array('first', 'second', 'third'),
  4181. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4182. );
  4183. $expected = array(
  4184. 'input' => array(
  4185. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4186. ),
  4187. array('div' => array('class' => 'checkbox')),
  4188. array('input' => array(
  4189. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4190. 'value' => '0', 'id' => 'CustomId0'
  4191. )),
  4192. array('label' => array('for' => 'CustomId0')),
  4193. 'first',
  4194. '/label',
  4195. '/div',
  4196. array('div' => array('class' => 'checkbox')),
  4197. array('input' => array(
  4198. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4199. 'value' => '1', 'id' => 'CustomId1'
  4200. )),
  4201. array('label' => array('for' => 'CustomId1')),
  4202. 'second',
  4203. '/label',
  4204. '/div',
  4205. array('div' => array('class' => 'checkbox')),
  4206. array('input' => array(
  4207. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4208. 'value' => '2', 'id' => 'CustomId2'
  4209. )),
  4210. array('label' => array('for' => 'CustomId2')),
  4211. 'third',
  4212. '/label',
  4213. '/div'
  4214. );
  4215. $this->assertTags($result, $expected);
  4216. }
  4217. /**
  4218. * testCheckbox method
  4219. *
  4220. * Test generation of checkboxes
  4221. *
  4222. * @return void
  4223. */
  4224. public function testCheckbox() {
  4225. $result = $this->Form->checkbox('Model.field');
  4226. $expected = array(
  4227. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4228. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4229. );
  4230. $this->assertTags($result, $expected);
  4231. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4232. $expected = array(
  4233. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4234. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4235. );
  4236. $this->assertTags($result, $expected);
  4237. $Contact = ClassRegistry::getObject('Contact');
  4238. $Contact->validationErrors['field'] = 1;
  4239. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4240. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4241. $expected = array(
  4242. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4243. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4244. );
  4245. $this->assertTags($result, $expected);
  4246. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4247. $expected = array(
  4248. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4249. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4250. );
  4251. $this->assertTags($result, $expected);
  4252. $this->Form->request->data['Contact']['field'] = '';
  4253. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4254. $expected = array(
  4255. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4256. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4257. );
  4258. $this->assertTags($result, $expected);
  4259. $Contact->validationErrors = array();
  4260. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4261. $expected = array(
  4262. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4263. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4264. );
  4265. $this->assertTags($result, $expected);
  4266. $this->Form->request->data['Contact']['published'] = 1;
  4267. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4268. $expected = array(
  4269. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4270. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4271. );
  4272. $this->assertTags($result, $expected);
  4273. $this->Form->request->data['Contact']['published'] = 0;
  4274. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4275. $expected = array(
  4276. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4277. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4278. );
  4279. $this->assertTags($result, $expected);
  4280. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4281. $expected = array(
  4282. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4283. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4284. );
  4285. $this->assertTags($result, $expected);
  4286. $result = $this->Form->checkbox('CustomField.1.value');
  4287. $expected = array(
  4288. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4289. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4290. );
  4291. $this->assertTags($result, $expected);
  4292. }
  4293. /**
  4294. * test checkbox() with a custom name attribute
  4295. *
  4296. * @return void
  4297. */
  4298. public function testCheckboxCustomNameAttribute() {
  4299. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4300. $expected = array(
  4301. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4302. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4303. );
  4304. $this->assertTags($result, $expected);
  4305. }
  4306. /**
  4307. * test the checked option for checkboxes.
  4308. *
  4309. * @return void
  4310. */
  4311. public function testCheckboxCheckedOption() {
  4312. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4313. $expected = array(
  4314. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4315. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4316. );
  4317. $this->assertTags($result, $expected);
  4318. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4319. $expected = array(
  4320. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4321. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4322. );
  4323. $this->assertTags($result, $expected);
  4324. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4325. $expected = array(
  4326. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4327. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4331. $expected = array(
  4332. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4333. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4334. );
  4335. $this->assertTags($result, $expected);
  4336. $this->Form->request->data['Model']['field'] = 1;
  4337. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4338. $expected = array(
  4339. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4340. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4341. );
  4342. $this->assertTags($result, $expected);
  4343. }
  4344. /**
  4345. * Test that disabled attribute works on both the checkbox and hidden input.
  4346. *
  4347. * @return void
  4348. */
  4349. public function testCheckboxDisabling() {
  4350. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4351. $expected = array(
  4352. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4353. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4354. );
  4355. $this->assertTags($result, $expected);
  4356. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4357. $expected = array(
  4358. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4359. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4360. );
  4361. $this->assertTags($result, $expected);
  4362. }
  4363. /**
  4364. * Test that the hidden input for checkboxes can be omitted or set to a
  4365. * specific value.
  4366. *
  4367. * @return void
  4368. */
  4369. public function testCheckboxHiddenField() {
  4370. $result = $this->Form->input('UserForm.something', array(
  4371. 'type' => 'checkbox',
  4372. 'hiddenField' => false
  4373. ));
  4374. $expected = array(
  4375. 'div' => array('class' => 'input checkbox'),
  4376. array('input' => array(
  4377. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4378. 'value' => '1', 'id' => 'UserFormSomething'
  4379. )),
  4380. 'label' => array('for' => 'UserFormSomething'),
  4381. 'Something',
  4382. '/label',
  4383. '/div'
  4384. );
  4385. $this->assertTags($result, $expected);
  4386. $result = $this->Form->input('UserForm.something', array(
  4387. 'type' => 'checkbox',
  4388. 'value' => 'Y',
  4389. 'hiddenField' => 'N',
  4390. ));
  4391. $expected = array(
  4392. 'div' => array('class' => 'input checkbox'),
  4393. array('input' => array(
  4394. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4395. 'value' => 'N', 'id' => 'UserFormSomething_'
  4396. )),
  4397. array('input' => array(
  4398. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4399. 'value' => 'Y', 'id' => 'UserFormSomething'
  4400. )),
  4401. 'label' => array('for' => 'UserFormSomething'),
  4402. 'Something',
  4403. '/label',
  4404. '/div'
  4405. );
  4406. $this->assertTags($result, $expected);
  4407. }
  4408. /**
  4409. * testDateTime method
  4410. *
  4411. * Test generation of date/time select elements
  4412. *
  4413. * @return void
  4414. */
  4415. public function testDateTime() {
  4416. extract($this->dateRegex);
  4417. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4418. $now = strtotime('now');
  4419. $expected = array(
  4420. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4421. $daysRegex,
  4422. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4423. date('j', $now),
  4424. '/option',
  4425. '*/select',
  4426. '-',
  4427. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4428. $monthsRegex,
  4429. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4430. date('F', $now),
  4431. '/option',
  4432. '*/select',
  4433. '-',
  4434. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4435. $yearsRegex,
  4436. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4437. date('Y', $now),
  4438. '/option',
  4439. '*/select',
  4440. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4441. $hoursRegex,
  4442. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4443. date('g', $now),
  4444. '/option',
  4445. '*/select',
  4446. ':',
  4447. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4448. $minutesRegex,
  4449. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4450. date('i', $now),
  4451. '/option',
  4452. '*/select',
  4453. ' ',
  4454. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4455. $meridianRegex,
  4456. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4457. date('a', $now),
  4458. '/option',
  4459. '*/select'
  4460. );
  4461. $this->assertTags($result, $expected);
  4462. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4463. $expected = array(
  4464. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4465. $daysRegex,
  4466. array('option' => array('value' => '')),
  4467. '/option',
  4468. '*/select',
  4469. '-',
  4470. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4471. $monthsRegex,
  4472. array('option' => array('value' => '')),
  4473. '/option',
  4474. '*/select',
  4475. '-',
  4476. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4477. $yearsRegex,
  4478. array('option' => array('value' => '')),
  4479. '/option',
  4480. '*/select',
  4481. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4482. $hoursRegex,
  4483. array('option' => array('value' => '')),
  4484. '/option',
  4485. '*/select',
  4486. ':',
  4487. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4488. $minutesRegex,
  4489. array('option' => array('value' => '')),
  4490. '/option',
  4491. '*/select',
  4492. ' ',
  4493. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4494. $meridianRegex,
  4495. array('option' => array('value' => '')),
  4496. '/option',
  4497. '*/select'
  4498. );
  4499. $this->assertTags($result, $expected);
  4500. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4501. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4502. $this->assertTags($result, $expected);
  4503. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4504. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4505. $this->assertTags($result, $expected);
  4506. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4507. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4508. $expected = array(
  4509. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4510. $daysRegex,
  4511. array('option' => array('value' => '')),
  4512. '/option',
  4513. '*/select',
  4514. '-',
  4515. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4516. $monthsRegex,
  4517. array('option' => array('value' => '')),
  4518. '/option',
  4519. '*/select',
  4520. '-',
  4521. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4522. $yearsRegex,
  4523. array('option' => array('value' => '')),
  4524. '/option',
  4525. '*/select',
  4526. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4527. $hoursRegex,
  4528. array('option' => array('value' => '')),
  4529. '/option',
  4530. '*/select',
  4531. ':',
  4532. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4533. $minutesRegex,
  4534. array('option' => array('value' => '')),
  4535. '/option',
  4536. array('option' => array('value' => '00')),
  4537. '00',
  4538. '/option',
  4539. array('option' => array('value' => '05')),
  4540. '05',
  4541. '/option',
  4542. array('option' => array('value' => '10')),
  4543. '10',
  4544. '/option',
  4545. '*/select',
  4546. ' ',
  4547. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4548. $meridianRegex,
  4549. array('option' => array('value' => '')),
  4550. '/option',
  4551. '*/select'
  4552. );
  4553. $this->assertTags($result, $expected);
  4554. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4555. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4556. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4557. $this->Form->request->data['Contact']['data'] = null;
  4558. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4559. $expected = array(
  4560. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4561. $daysRegex,
  4562. array('option' => array('value' => '')),
  4563. '/option',
  4564. '*/select',
  4565. '-',
  4566. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4567. $monthsRegex,
  4568. array('option' => array('value' => '')),
  4569. '/option',
  4570. '*/select',
  4571. '-',
  4572. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4573. $yearsRegex,
  4574. array('option' => array('value' => '')),
  4575. '/option',
  4576. '*/select',
  4577. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4578. $hoursRegex,
  4579. array('option' => array('value' => '')),
  4580. '/option',
  4581. '*/select',
  4582. ':',
  4583. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4584. $minutesRegex,
  4585. array('option' => array('value' => '')),
  4586. '/option',
  4587. '*/select',
  4588. ' ',
  4589. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4590. $meridianRegex,
  4591. array('option' => array('value' => '')),
  4592. '/option',
  4593. '*/select'
  4594. );
  4595. $this->assertTags($result, $expected);
  4596. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4597. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4598. $now = strtotime($this->Form->data['Model']['field']);
  4599. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4600. $expected = array(
  4601. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4602. $daysRegex,
  4603. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4604. date('j', $now),
  4605. '/option',
  4606. '*/select',
  4607. '-',
  4608. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4609. $monthsRegex,
  4610. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4611. date('F', $now),
  4612. '/option',
  4613. '*/select',
  4614. '-',
  4615. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4616. $yearsRegex,
  4617. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4618. date('Y', $now),
  4619. '/option',
  4620. '*/select',
  4621. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4622. $hoursRegex,
  4623. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4624. date('g', $now),
  4625. '/option',
  4626. '*/select',
  4627. ':',
  4628. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4629. $minutesRegex,
  4630. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4631. date('i', $now),
  4632. '/option',
  4633. '*/select',
  4634. ' ',
  4635. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4636. $meridianRegex,
  4637. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4638. date('a', $now),
  4639. '/option',
  4640. '*/select'
  4641. );
  4642. $this->assertTags($result, $expected);
  4643. $selected = strtotime('2008-10-26 12:33:00');
  4644. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4645. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4646. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4647. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4648. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4649. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4650. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4651. $this->Form->create('Contact');
  4652. $result = $this->Form->input('published');
  4653. $now = strtotime('now');
  4654. $expected = array(
  4655. 'div' => array('class' => 'input date'),
  4656. 'label' => array('for' => 'ContactPublishedMonth'),
  4657. 'Published',
  4658. '/label',
  4659. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4660. $monthsRegex,
  4661. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4662. date('F', $now),
  4663. '/option',
  4664. '*/select',
  4665. '-',
  4666. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4667. $daysRegex,
  4668. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4669. date('j', $now),
  4670. '/option',
  4671. '*/select',
  4672. '-',
  4673. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4674. $yearsRegex,
  4675. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4676. date('Y', $now),
  4677. '/option',
  4678. '*/select',
  4679. '/div'
  4680. );
  4681. $this->assertTags($result, $expected);
  4682. $result = $this->Form->input('published2', array('type' => 'date'));
  4683. $now = strtotime('now');
  4684. $expected = array(
  4685. 'div' => array('class' => 'input date'),
  4686. 'label' => array('for' => 'ContactPublished2Month'),
  4687. 'Published2',
  4688. '/label',
  4689. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4690. $monthsRegex,
  4691. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4692. date('F', $now),
  4693. '/option',
  4694. '*/select',
  4695. '-',
  4696. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4697. $daysRegex,
  4698. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4699. date('j', $now),
  4700. '/option',
  4701. '*/select',
  4702. '-',
  4703. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4704. $yearsRegex,
  4705. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4706. date('Y', $now),
  4707. '/option',
  4708. '*/select',
  4709. '/div'
  4710. );
  4711. $this->assertTags($result, $expected);
  4712. $this->Form->create('Contact');
  4713. $result = $this->Form->input('published', array('monthNames' => false));
  4714. $now = strtotime('now');
  4715. $expected = array(
  4716. 'div' => array('class' => 'input date'),
  4717. 'label' => array('for' => 'ContactPublishedMonth'),
  4718. 'Published',
  4719. '/label',
  4720. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4721. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4722. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4723. date('m', $now),
  4724. '/option',
  4725. '*/select',
  4726. '-',
  4727. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4728. $daysRegex,
  4729. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4730. date('j', $now),
  4731. '/option',
  4732. '*/select',
  4733. '-',
  4734. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4735. $yearsRegex,
  4736. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4737. date('Y', $now),
  4738. '/option',
  4739. '*/select',
  4740. '/div'
  4741. );
  4742. $this->assertTags($result, $expected);
  4743. $result = $this->Form->input('published', array('type' => 'time'));
  4744. $now = strtotime('now');
  4745. $expected = array(
  4746. 'div' => array('class' => 'input time'),
  4747. 'label' => array('for' => 'ContactPublishedHour'),
  4748. 'Published',
  4749. '/label',
  4750. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4751. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4752. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4753. date('g', $now),
  4754. '/option',
  4755. '*/select',
  4756. ':',
  4757. );
  4758. $this->assertTags($result, $expected);
  4759. $result = $this->Form->input('published', array(
  4760. 'timeFormat' => 24,
  4761. 'interval' => 5,
  4762. 'selected' => strtotime('2009-09-03 13:37:00'),
  4763. 'type' => 'datetime'
  4764. ));
  4765. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4766. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4767. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4768. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4769. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4770. $this->assertNoErrors();
  4771. $this->Form->request->data['Contact'] = array(
  4772. 'date' => array(
  4773. 'day' => '',
  4774. 'month' => '',
  4775. 'year' => '',
  4776. 'hour' => '',
  4777. 'min' => '',
  4778. 'meridian' => ''
  4779. )
  4780. );
  4781. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4782. }
  4783. /**
  4784. * test that datetime() and default values work.
  4785. *
  4786. * @return void
  4787. */
  4788. public function testDatetimeWithDefault() {
  4789. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4790. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4791. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4792. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4793. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4794. 'default' => '2009-06-01 11:15:30'
  4795. ));
  4796. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4797. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4798. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4799. }
  4800. /**
  4801. * test that bogus non-date time data doesn't cause errors.
  4802. *
  4803. * @return void
  4804. */
  4805. public function testDateTimeWithBogusData() {
  4806. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4807. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4808. }
  4809. /**
  4810. * testDateTimeEmptyAsArray
  4811. *
  4812. * @return void
  4813. */
  4814. public function testDateTimeEmptyAsArray() {
  4815. $result = $this->Form->dateTime('Contact.date',
  4816. 'DMY',
  4817. '12',
  4818. array(
  4819. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4820. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4821. )
  4822. )
  4823. );
  4824. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4825. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4826. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4827. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4828. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4829. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4830. $result = $this->Form->dateTime('Contact.date',
  4831. 'DMY',
  4832. '12',
  4833. array(
  4834. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4835. )
  4836. );
  4837. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4838. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4839. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4840. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4841. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4842. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4843. }
  4844. /**
  4845. * testFormDateTimeMulti method
  4846. *
  4847. * test multiple datetime element generation
  4848. *
  4849. * @return void
  4850. */
  4851. public function testFormDateTimeMulti() {
  4852. extract($this->dateRegex);
  4853. $result = $this->Form->dateTime('Contact.1.updated');
  4854. $expected = array(
  4855. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4856. $daysRegex,
  4857. array('option' => array('value' => '')),
  4858. '/option',
  4859. '*/select',
  4860. '-',
  4861. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4862. $monthsRegex,
  4863. array('option' => array('value' => '')),
  4864. '/option',
  4865. '*/select',
  4866. '-',
  4867. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4868. $yearsRegex,
  4869. array('option' => array('value' => '')),
  4870. '/option',
  4871. '*/select',
  4872. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4873. $hoursRegex,
  4874. array('option' => array('value' => '')),
  4875. '/option',
  4876. '*/select',
  4877. ':',
  4878. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4879. $minutesRegex,
  4880. array('option' => array('value' => '')),
  4881. '/option',
  4882. '*/select',
  4883. ' ',
  4884. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4885. $meridianRegex,
  4886. array('option' => array('value' => '')),
  4887. '/option',
  4888. '*/select'
  4889. );
  4890. $this->assertTags($result, $expected);
  4891. $result = $this->Form->dateTime('Contact.2.updated');
  4892. $expected = array(
  4893. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4894. $daysRegex,
  4895. array('option' => array('value' => '')),
  4896. '/option',
  4897. '*/select',
  4898. '-',
  4899. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4900. $monthsRegex,
  4901. array('option' => array('value' => '')),
  4902. '/option',
  4903. '*/select',
  4904. '-',
  4905. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4906. $yearsRegex,
  4907. array('option' => array('value' => '')),
  4908. '/option',
  4909. '*/select',
  4910. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4911. $hoursRegex,
  4912. array('option' => array('value' => '')),
  4913. '/option',
  4914. '*/select',
  4915. ':',
  4916. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4917. $minutesRegex,
  4918. array('option' => array('value' => '')),
  4919. '/option',
  4920. '*/select',
  4921. ' ',
  4922. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4923. $meridianRegex,
  4924. array('option' => array('value' => '')),
  4925. '/option',
  4926. '*/select'
  4927. );
  4928. $this->assertTags($result, $expected);
  4929. }
  4930. /**
  4931. * When changing the date format, the label should always focus the first select box when
  4932. * clicked.
  4933. *
  4934. * @return void
  4935. */
  4936. public function testDateTimeLabelIdMatchesFirstInput() {
  4937. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4938. $this->assertContains('label for="ModelDateMonth"', $result);
  4939. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4940. $this->assertContains('label for="ModelDateDay"', $result);
  4941. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4942. $this->assertContains('label for="ModelDateYear"', $result);
  4943. }
  4944. /**
  4945. * testMonth method
  4946. *
  4947. * @return void
  4948. */
  4949. public function testMonth() {
  4950. $result = $this->Form->month('Model.field');
  4951. $expected = array(
  4952. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4953. array('option' => array('value' => '')),
  4954. '/option',
  4955. array('option' => array('value' => '01')),
  4956. date('F', strtotime('2008-01-01 00:00:00')),
  4957. '/option',
  4958. array('option' => array('value' => '02')),
  4959. date('F', strtotime('2008-02-01 00:00:00')),
  4960. '/option',
  4961. '*/select',
  4962. );
  4963. $this->assertTags($result, $expected);
  4964. $result = $this->Form->month('Model.field', array('empty' => true));
  4965. $expected = array(
  4966. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4967. array('option' => array('value' => '')),
  4968. '/option',
  4969. array('option' => array('value' => '01')),
  4970. date('F', strtotime('2008-01-01 00:00:00')),
  4971. '/option',
  4972. array('option' => array('value' => '02')),
  4973. date('F', strtotime('2008-02-01 00:00:00')),
  4974. '/option',
  4975. '*/select',
  4976. );
  4977. $this->assertTags($result, $expected);
  4978. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4979. $expected = array(
  4980. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4981. array('option' => array('value' => '')),
  4982. '/option',
  4983. array('option' => array('value' => '01')),
  4984. '01',
  4985. '/option',
  4986. array('option' => array('value' => '02')),
  4987. '02',
  4988. '/option',
  4989. '*/select',
  4990. );
  4991. $this->assertTags($result, $expected);
  4992. $monthNames = array(
  4993. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4994. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4995. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4996. $expected = array(
  4997. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4998. array('option' => array('value' => '')),
  4999. '/option',
  5000. array('option' => array('value' => '01')),
  5001. 'Jan',
  5002. '/option',
  5003. array('option' => array('value' => '02')),
  5004. 'Feb',
  5005. '/option',
  5006. '*/select',
  5007. );
  5008. $this->assertTags($result, $expected);
  5009. }
  5010. /**
  5011. * testDay method
  5012. *
  5013. * @return void
  5014. */
  5015. public function testDay() {
  5016. extract($this->dateRegex);
  5017. $result = $this->Form->day('Model.field', array('value' => false));
  5018. $expected = array(
  5019. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5020. array('option' => array('value' => '')),
  5021. '/option',
  5022. array('option' => array('value' => '01')),
  5023. '1',
  5024. '/option',
  5025. array('option' => array('value' => '02')),
  5026. '2',
  5027. '/option',
  5028. $daysRegex,
  5029. '/select',
  5030. );
  5031. $this->assertTags($result, $expected);
  5032. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5033. $result = $this->Form->day('Model.field');
  5034. $expected = array(
  5035. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5036. array('option' => array('value' => '')),
  5037. '/option',
  5038. array('option' => array('value' => '01')),
  5039. '1',
  5040. '/option',
  5041. array('option' => array('value' => '02')),
  5042. '2',
  5043. '/option',
  5044. $daysRegex,
  5045. array('option' => array('value' => '10', 'selected' => 'selected')),
  5046. '10',
  5047. '/option',
  5048. $daysRegex,
  5049. '/select',
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. $this->Form->request->data['Model']['field'] = '';
  5053. $result = $this->Form->day('Model.field', array('value' => '10'));
  5054. $expected = array(
  5055. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5056. array('option' => array('value' => '')),
  5057. '/option',
  5058. array('option' => array('value' => '01')),
  5059. '1',
  5060. '/option',
  5061. array('option' => array('value' => '02')),
  5062. '2',
  5063. '/option',
  5064. $daysRegex,
  5065. array('option' => array('value' => '10', 'selected' => 'selected')),
  5066. '10',
  5067. '/option',
  5068. $daysRegex,
  5069. '/select',
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5073. $result = $this->Form->day('Model.field', array('value' => true));
  5074. $expected = array(
  5075. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5076. array('option' => array('value' => '')),
  5077. '/option',
  5078. array('option' => array('value' => '01')),
  5079. '1',
  5080. '/option',
  5081. array('option' => array('value' => '02')),
  5082. '2',
  5083. '/option',
  5084. $daysRegex,
  5085. array('option' => array('value' => '10', 'selected' => 'selected')),
  5086. '10',
  5087. '/option',
  5088. $daysRegex,
  5089. '/select',
  5090. );
  5091. $this->assertTags($result, $expected);
  5092. }
  5093. /**
  5094. * testMinute method
  5095. *
  5096. * @return void
  5097. */
  5098. public function testMinute() {
  5099. extract($this->dateRegex);
  5100. $result = $this->Form->minute('Model.field');
  5101. $expected = array(
  5102. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5103. array('option' => array('value' => '')),
  5104. '/option',
  5105. array('option' => array('value' => '00')),
  5106. '00',
  5107. '/option',
  5108. array('option' => array('value' => '01')),
  5109. '01',
  5110. '/option',
  5111. array('option' => array('value' => '02')),
  5112. '02',
  5113. '/option',
  5114. $minutesRegex,
  5115. '/select',
  5116. );
  5117. $this->assertTags($result, $expected);
  5118. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5119. $result = $this->Form->minute('Model.field');
  5120. $expected = array(
  5121. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5122. array('option' => array('value' => '')),
  5123. '/option',
  5124. array('option' => array('value' => '00')),
  5125. '00',
  5126. '/option',
  5127. array('option' => array('value' => '01')),
  5128. '01',
  5129. '/option',
  5130. array('option' => array('value' => '02')),
  5131. '02',
  5132. '/option',
  5133. $minutesRegex,
  5134. array('option' => array('value' => '12', 'selected' => 'selected')),
  5135. '12',
  5136. '/option',
  5137. $minutesRegex,
  5138. '/select',
  5139. );
  5140. $this->assertTags($result, $expected);
  5141. $this->Form->request->data['Model']['field'] = '';
  5142. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5143. $expected = array(
  5144. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5145. array('option' => array('value' => '')),
  5146. '/option',
  5147. array('option' => array('value' => '00')),
  5148. '00',
  5149. '/option',
  5150. array('option' => array('value' => '05')),
  5151. '05',
  5152. '/option',
  5153. array('option' => array('value' => '10')),
  5154. '10',
  5155. '/option',
  5156. $minutesRegex,
  5157. '/select',
  5158. );
  5159. $this->assertTags($result, $expected);
  5160. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5161. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5162. $expected = array(
  5163. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5164. array('option' => array('value' => '')),
  5165. '/option',
  5166. array('option' => array('value' => '00')),
  5167. '00',
  5168. '/option',
  5169. array('option' => array('value' => '05')),
  5170. '05',
  5171. '/option',
  5172. array('option' => array('value' => '10', 'selected' => 'selected')),
  5173. '10',
  5174. '/option',
  5175. $minutesRegex,
  5176. '/select',
  5177. );
  5178. $this->assertTags($result, $expected);
  5179. }
  5180. /**
  5181. * testHour method
  5182. *
  5183. * @return void
  5184. */
  5185. public function testHour() {
  5186. extract($this->dateRegex);
  5187. $result = $this->Form->hour('Model.field', false);
  5188. $expected = array(
  5189. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5190. array('option' => array('value' => '')),
  5191. '/option',
  5192. array('option' => array('value' => '01')),
  5193. '1',
  5194. '/option',
  5195. array('option' => array('value' => '02')),
  5196. '2',
  5197. '/option',
  5198. $hoursRegex,
  5199. '/select',
  5200. );
  5201. $this->assertTags($result, $expected);
  5202. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5203. $result = $this->Form->hour('Model.field', false);
  5204. $expected = array(
  5205. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5206. array('option' => array('value' => '')),
  5207. '/option',
  5208. array('option' => array('value' => '01')),
  5209. '1',
  5210. '/option',
  5211. array('option' => array('value' => '02')),
  5212. '2',
  5213. '/option',
  5214. $hoursRegex,
  5215. array('option' => array('value' => '12', 'selected' => 'selected')),
  5216. '12',
  5217. '/option',
  5218. '/select',
  5219. );
  5220. $this->assertTags($result, $expected);
  5221. $this->Form->request->data['Model']['field'] = '';
  5222. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5223. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5224. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5225. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5226. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5227. $result = $this->Form->hour('Model.field', true);
  5228. $expected = array(
  5229. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5230. array('option' => array('value' => '')),
  5231. '/option',
  5232. array('option' => array('value' => '00', 'selected' => 'selected')),
  5233. '0',
  5234. '/option',
  5235. array('option' => array('value' => '01')),
  5236. '1',
  5237. '/option',
  5238. array('option' => array('value' => '02')),
  5239. '2',
  5240. '/option',
  5241. $hoursRegex,
  5242. '/select',
  5243. );
  5244. $this->assertTags($result, $expected);
  5245. unset($this->Form->request->data['Model']['field']);
  5246. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5247. $thisHour = date('H');
  5248. $optValue = date('G');
  5249. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5250. }
  5251. /**
  5252. * testYear method
  5253. *
  5254. * @return void
  5255. */
  5256. public function testYear() {
  5257. $result = $this->Form->year('Model.field', 2006, 2007);
  5258. $expected = array(
  5259. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5260. array('option' => array('value' => '')),
  5261. '/option',
  5262. array('option' => array('value' => '2007')),
  5263. '2007',
  5264. '/option',
  5265. array('option' => array('value' => '2006')),
  5266. '2006',
  5267. '/option',
  5268. '/select',
  5269. );
  5270. $this->assertTags($result, $expected);
  5271. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5272. $expected = array(
  5273. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5274. array('option' => array('value' => '')),
  5275. '/option',
  5276. array('option' => array('value' => '2006')),
  5277. '2006',
  5278. '/option',
  5279. array('option' => array('value' => '2007')),
  5280. '2007',
  5281. '/option',
  5282. '/select',
  5283. );
  5284. $this->assertTags($result, $expected);
  5285. $this->request->data['Contact']['published'] = '';
  5286. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5287. $expected = array(
  5288. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5289. array('option' => array('value' => '')),
  5290. '/option',
  5291. array('option' => array('value' => '2007')),
  5292. '2007',
  5293. '/option',
  5294. array('option' => array('value' => '2006')),
  5295. '2006',
  5296. '/option',
  5297. '/select',
  5298. );
  5299. $this->assertTags($result, $expected);
  5300. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5301. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5302. $expected = array(
  5303. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5304. array('option' => array('value' => '2007')),
  5305. '2007',
  5306. '/option',
  5307. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5308. '2006',
  5309. '/option',
  5310. '/select',
  5311. );
  5312. $this->assertTags($result, $expected);
  5313. $this->Form->request->data['Contact']['published'] = '';
  5314. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5315. $expected = array(
  5316. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5317. array('option' => array('value' => '')),
  5318. '/option',
  5319. array('option' => array('value' => '2007')),
  5320. '2007',
  5321. '/option',
  5322. array('option' => array('value' => '2006')),
  5323. '2006',
  5324. '/option',
  5325. '/select',
  5326. );
  5327. $this->assertTags($result, $expected);
  5328. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5329. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5330. $expected = array(
  5331. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5332. array('option' => array('value' => '2007')),
  5333. '2007',
  5334. '/option',
  5335. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5336. '2006',
  5337. '/option',
  5338. '/select',
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $this->Form->request->data['Contact']['published'] = '';
  5342. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5343. $expected = array(
  5344. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5345. array('option' => array('value' => '')),
  5346. '/option',
  5347. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5348. '2007',
  5349. '/option',
  5350. array('option' => array('value' => '2006')),
  5351. '2006',
  5352. '/option',
  5353. '/select',
  5354. );
  5355. $this->assertTags($result, $expected);
  5356. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5357. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5358. $expected = array(
  5359. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5360. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5361. '2007',
  5362. '/option',
  5363. array('option' => array('value' => '2006')),
  5364. '2006',
  5365. '/option',
  5366. '/select',
  5367. );
  5368. $this->assertTags($result, $expected);
  5369. $this->Form->request->data['Contact']['published'] = '';
  5370. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5371. $expected = array(
  5372. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5373. array('option' => array('value' => '2008')),
  5374. '2008',
  5375. '/option',
  5376. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5377. '2007',
  5378. '/option',
  5379. array('option' => array('value' => '2006')),
  5380. '2006',
  5381. '/option',
  5382. '/select',
  5383. );
  5384. $this->assertTags($result, $expected);
  5385. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5386. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5387. $expected = array(
  5388. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5389. array('option' => array('value' => '2008')),
  5390. '2008',
  5391. '/option',
  5392. array('option' => array('value' => '2007')),
  5393. '2007',
  5394. '/option',
  5395. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5396. '2006',
  5397. '/option',
  5398. '/select',
  5399. );
  5400. $this->assertTags($result, $expected);
  5401. $this->Form->request->data = array();
  5402. $this->Form->create('Contact');
  5403. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5404. $expected = array(
  5405. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5406. array('option' => array('value' => '2008')),
  5407. '2008',
  5408. '/option',
  5409. array('option' => array('value' => '2007')),
  5410. '2007',
  5411. '/option',
  5412. array('option' => array('value' => '2006')),
  5413. '2006',
  5414. '/option',
  5415. '/select',
  5416. );
  5417. $this->assertTags($result, $expected);
  5418. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5419. $this->assertContains('data[Contact][published][year]', $result);
  5420. }
  5421. /**
  5422. * testTextArea method
  5423. *
  5424. * @return void
  5425. */
  5426. public function testTextArea() {
  5427. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5428. $result = $this->Form->textarea('Model.field');
  5429. $expected = array(
  5430. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5431. 'some test data',
  5432. '/textarea',
  5433. );
  5434. $this->assertTags($result, $expected);
  5435. $result = $this->Form->textarea('Model.tmp');
  5436. $expected = array(
  5437. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5438. '/textarea',
  5439. );
  5440. $this->assertTags($result, $expected);
  5441. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5442. $result = $this->Form->textarea('Model.field');
  5443. $expected = array(
  5444. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5445. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5446. '/textarea',
  5447. );
  5448. $this->assertTags($result, $expected);
  5449. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5450. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5451. $expected = array(
  5452. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5453. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5454. '/textarea',
  5455. );
  5456. $this->assertTags($result, $expected);
  5457. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5458. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5459. $expected = array(
  5460. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5461. '/textarea'
  5462. );
  5463. $this->assertTags($result, $expected);
  5464. }
  5465. /**
  5466. * testTextAreaWithStupidCharacters method
  5467. *
  5468. * test text area with non-ascii characters
  5469. *
  5470. * @return void
  5471. */
  5472. public function testTextAreaWithStupidCharacters() {
  5473. $this->loadFixtures('Post');
  5474. $result = $this->Form->input('Post.content', array(
  5475. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5476. ));
  5477. $expected = array(
  5478. 'div' => array('class' => 'input text'),
  5479. 'label' => array('for' => 'PostContent'),
  5480. 'Current Text',
  5481. '/label',
  5482. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5483. 'GREAT®',
  5484. '/textarea',
  5485. '/div'
  5486. );
  5487. $this->assertTags($result, $expected);
  5488. }
  5489. /**
  5490. * testHiddenField method
  5491. *
  5492. * @return void
  5493. */
  5494. public function testHiddenField() {
  5495. $Contact = ClassRegistry::getObject('Contact');
  5496. $Contact->validationErrors['field'] = 1;
  5497. $this->Form->request->data['Contact']['field'] = 'test';
  5498. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5499. $this->assertTags($result, array(
  5500. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5501. );
  5502. }
  5503. /**
  5504. * testFileUploadField method
  5505. *
  5506. * @return void
  5507. */
  5508. public function testFileUploadField() {
  5509. $result = $this->Form->file('Model.upload');
  5510. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5511. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5512. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5513. $expected = array(
  5514. 'div' => array('class' => 'input file'),
  5515. 'label' => array('for' => 'ModelUpload'),
  5516. 'Upload',
  5517. '/label',
  5518. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5519. '/div'
  5520. );
  5521. $this->assertTags($result, $expected);
  5522. }
  5523. /**
  5524. * test File upload input on a model not used in create();
  5525. *
  5526. * @return void
  5527. */
  5528. public function testFileUploadOnOtherModel() {
  5529. $this->Form->create('ValidateUser', array('type' => 'file'));
  5530. $result = $this->Form->file('ValidateProfile.city');
  5531. $expected = array(
  5532. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5533. );
  5534. $this->assertTags($result, $expected);
  5535. }
  5536. /**
  5537. * testButton method
  5538. *
  5539. * @return void
  5540. */
  5541. public function testButton() {
  5542. $result = $this->Form->button('Hi');
  5543. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5544. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5545. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5546. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5547. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5548. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5549. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5550. $result = $this->Form->button('No type', array('type' => false));
  5551. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5552. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5553. $this->assertNotRegExp('/\&039/', $result);
  5554. }
  5555. /**
  5556. * Test that button() makes unlocked fields by default.
  5557. *
  5558. * @return void
  5559. */
  5560. public function testButtonUnlockedByDefault() {
  5561. $this->Form->request->params['_Token']['key'] = 'secured';
  5562. $this->Form->button('Save', array('name' => 'save'));
  5563. $this->Form->button('Clear');
  5564. $result = $this->Form->unlockField();
  5565. $this->assertEquals(array('save'), $result);
  5566. }
  5567. /**
  5568. * testPostButton method
  5569. *
  5570. * @return void
  5571. */
  5572. public function testPostButton() {
  5573. $result = $this->Form->postButton('Hi', '/controller/action');
  5574. $this->assertTags($result, array(
  5575. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5576. 'div' => array('style' => 'display:none;'),
  5577. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5578. '/div',
  5579. 'button' => array('type' => 'submit'),
  5580. 'Hi',
  5581. '/button',
  5582. '/form'
  5583. ));
  5584. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5585. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5586. }
  5587. /**
  5588. * Test that postButton adds _Token fields.
  5589. *
  5590. * @return void
  5591. */
  5592. public function testSecurePostButton() {
  5593. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5594. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5595. $expected = array(
  5596. 'form' => array(
  5597. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5598. ),
  5599. array('div' => array('style' => 'display:none;')),
  5600. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5601. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5602. '/div',
  5603. 'button' => array('type' => 'submit'),
  5604. 'Delete',
  5605. '/button',
  5606. array('div' => array('style' => 'display:none;')),
  5607. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5608. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5609. '/div',
  5610. '/form',
  5611. );
  5612. $this->assertTags($result, $expected);
  5613. }
  5614. /**
  5615. * testPostLink method
  5616. *
  5617. * @return void
  5618. */
  5619. public function testPostLink() {
  5620. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5621. $this->assertTags($result, array(
  5622. 'form' => array(
  5623. 'method' => 'post', 'action' => '/posts/delete/1',
  5624. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5625. ),
  5626. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5627. '/form',
  5628. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5629. 'Delete',
  5630. '/a'
  5631. ));
  5632. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5633. $this->assertTags($result, array(
  5634. 'form' => array(
  5635. 'method' => 'post', 'action' => '/posts/delete/1',
  5636. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5637. ),
  5638. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5639. '/form',
  5640. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5641. 'Delete',
  5642. '/a'
  5643. ));
  5644. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5645. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5646. }
  5647. /**
  5648. * Test that postLink adds _Token fields.
  5649. *
  5650. * @return void
  5651. */
  5652. public function testSecurePostLink() {
  5653. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5654. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5655. $expected = array(
  5656. 'form' => array(
  5657. 'method' => 'post', 'action' => '/posts/delete/1',
  5658. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5659. ),
  5660. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5661. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5662. 'div' => array('style' => 'display:none;'),
  5663. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5664. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5665. '/div',
  5666. '/form',
  5667. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5668. 'Delete',
  5669. '/a'
  5670. );
  5671. $this->assertTags($result, $expected);
  5672. }
  5673. /**
  5674. * testSubmitButton method
  5675. *
  5676. * @return void
  5677. */
  5678. public function testSubmitButton() {
  5679. $result = $this->Form->submit('');
  5680. $expected = array(
  5681. 'div' => array('class' => 'submit'),
  5682. 'input' => array('type' => 'submit', 'value' => ''),
  5683. '/div'
  5684. );
  5685. $this->assertTags($result, $expected);
  5686. $result = $this->Form->submit('Test Submit');
  5687. $expected = array(
  5688. 'div' => array('class' => 'submit'),
  5689. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5690. '/div'
  5691. );
  5692. $this->assertTags($result, $expected);
  5693. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5694. $expected = array(
  5695. 'span' => array('class' => 'submit'),
  5696. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5697. '/span'
  5698. );
  5699. $this->assertTags($result, $expected);
  5700. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5701. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5702. $this->assertTags($result, $expected);
  5703. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5704. $expected = array(
  5705. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5706. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5707. '/div'
  5708. );
  5709. $this->assertTags($result, $expected);
  5710. $result = $this->Form->submit('Next >');
  5711. $expected = array(
  5712. 'div' => array('class' => 'submit'),
  5713. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5714. '/div'
  5715. );
  5716. $this->assertTags($result, $expected);
  5717. $result = $this->Form->submit('Next >', array('escape' => false));
  5718. $expected = array(
  5719. 'div' => array('class' => 'submit'),
  5720. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5721. '/div'
  5722. );
  5723. $this->assertTags($result, $expected);
  5724. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5725. $expected = array(
  5726. 'div' => array('class' => 'submit'),
  5727. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5728. '/div'
  5729. );
  5730. $this->assertTags($result, $expected);
  5731. $before = '--before--';
  5732. $after = '--after--';
  5733. $result = $this->Form->submit('Test', array('before' => $before));
  5734. $expected = array(
  5735. 'div' => array('class' => 'submit'),
  5736. '--before--',
  5737. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5738. '/div'
  5739. );
  5740. $this->assertTags($result, $expected);
  5741. $result = $this->Form->submit('Test', array('after' => $after));
  5742. $expected = array(
  5743. 'div' => array('class' => 'submit'),
  5744. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5745. '--after--',
  5746. '/div'
  5747. );
  5748. $this->assertTags($result, $expected);
  5749. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5750. $expected = array(
  5751. 'div' => array('class' => 'submit'),
  5752. '--before--',
  5753. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5754. '--after--',
  5755. '/div'
  5756. );
  5757. $this->assertTags($result, $expected);
  5758. }
  5759. /**
  5760. * test image submit types.
  5761. *
  5762. * @return void
  5763. */
  5764. public function testSubmitImage() {
  5765. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5766. $expected = array(
  5767. 'div' => array('class' => 'submit'),
  5768. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5769. '/div'
  5770. );
  5771. $this->assertTags($result, $expected);
  5772. $result = $this->Form->submit('/relative/cake.power.gif');
  5773. $expected = array(
  5774. 'div' => array('class' => 'submit'),
  5775. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5776. '/div'
  5777. );
  5778. $this->assertTags($result, $expected);
  5779. $result = $this->Form->submit('cake.power.gif');
  5780. $expected = array(
  5781. 'div' => array('class' => 'submit'),
  5782. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5783. '/div'
  5784. );
  5785. $this->assertTags($result, $expected);
  5786. $result = $this->Form->submit('Not.an.image');
  5787. $expected = array(
  5788. 'div' => array('class' => 'submit'),
  5789. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5790. '/div'
  5791. );
  5792. $this->assertTags($result, $expected);
  5793. $after = '--after--';
  5794. $before = '--before--';
  5795. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5796. $expected = array(
  5797. 'div' => array('class' => 'submit'),
  5798. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5799. '--after--',
  5800. '/div'
  5801. );
  5802. $this->assertTags($result, $expected);
  5803. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5804. $expected = array(
  5805. 'div' => array('class' => 'submit'),
  5806. '--before--',
  5807. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5808. '/div'
  5809. );
  5810. $this->assertTags($result, $expected);
  5811. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5812. $expected = array(
  5813. 'div' => array('class' => 'submit'),
  5814. '--before--',
  5815. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5816. '--after--',
  5817. '/div'
  5818. );
  5819. $this->assertTags($result, $expected);
  5820. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5821. $expected = array(
  5822. 'div' => array('class' => 'submit'),
  5823. '--before--',
  5824. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5825. '--after--',
  5826. '/div'
  5827. );
  5828. $this->assertTags($result, $expected);
  5829. }
  5830. /**
  5831. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5832. * be submitted at a time.
  5833. *
  5834. * @return void
  5835. */
  5836. public function testSubmitUnlockedByDefault() {
  5837. $this->Form->request->params['_Token']['key'] = 'secured';
  5838. $this->Form->submit('Go go');
  5839. $this->Form->submit('Save', array('name' => 'save'));
  5840. $result = $this->Form->unlockField();
  5841. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5842. }
  5843. /**
  5844. * Test submit image with timestamps.
  5845. *
  5846. * @return void
  5847. */
  5848. public function testSubmitImageTimestamp() {
  5849. Configure::write('Asset.timestamp', 'force');
  5850. $result = $this->Form->submit('cake.power.gif');
  5851. $expected = array(
  5852. 'div' => array('class' => 'submit'),
  5853. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5854. '/div'
  5855. );
  5856. $this->assertTags($result, $expected);
  5857. }
  5858. /**
  5859. * test the create() method
  5860. *
  5861. * @return void
  5862. */
  5863. public function testCreate() {
  5864. $result = $this->Form->create('Contact');
  5865. $encoding = strtolower(Configure::read('App.encoding'));
  5866. $expected = array(
  5867. 'form' => array(
  5868. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5869. 'accept-charset' => $encoding
  5870. ),
  5871. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5872. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5873. '/div'
  5874. );
  5875. $this->assertTags($result, $expected);
  5876. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5877. $expected = array('form' => array(
  5878. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5879. 'accept-charset' => $encoding
  5880. ));
  5881. $this->assertTags($result, $expected);
  5882. $result = $this->Form->create('Contact', array('type' => 'get'));
  5883. $expected = array('form' => array(
  5884. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5885. 'accept-charset' => $encoding
  5886. ));
  5887. $this->assertTags($result, $expected);
  5888. $result = $this->Form->create('Contact', array('type' => 'put'));
  5889. $expected = array(
  5890. 'form' => array(
  5891. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5892. 'accept-charset' => $encoding
  5893. ),
  5894. 'div' => array('style' => 'display:none;'),
  5895. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5896. '/div'
  5897. );
  5898. $this->assertTags($result, $expected);
  5899. $result = $this->Form->create('Contact', array('type' => 'file'));
  5900. $expected = array(
  5901. 'form' => array(
  5902. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5903. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5904. ),
  5905. 'div' => array('style' => 'display:none;'),
  5906. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5907. '/div'
  5908. );
  5909. $this->assertTags($result, $expected);
  5910. $this->Form->request->data['Contact']['id'] = 1;
  5911. $this->Form->request->here = '/contacts/edit/1';
  5912. $this->Form->request['action'] = 'edit';
  5913. $result = $this->Form->create('Contact');
  5914. $expected = array(
  5915. 'form' => array(
  5916. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5917. 'accept-charset' => $encoding
  5918. ),
  5919. 'div' => array('style' => 'display:none;'),
  5920. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5921. '/div'
  5922. );
  5923. $this->assertTags($result, $expected);
  5924. $this->Form->request->data['Contact']['id'] = 1;
  5925. $this->Form->request->here = '/contacts/edit/1';
  5926. $this->Form->request['action'] = 'edit';
  5927. $result = $this->Form->create('Contact', array('type' => 'file'));
  5928. $expected = array(
  5929. 'form' => array(
  5930. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5931. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5932. ),
  5933. 'div' => array('style' => 'display:none;'),
  5934. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5935. '/div'
  5936. );
  5937. $this->assertTags($result, $expected);
  5938. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5939. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5940. $expected = array(
  5941. 'form' => array(
  5942. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5943. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5944. ),
  5945. 'div' => array('style' => 'display:none;'),
  5946. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5947. '/div'
  5948. );
  5949. $this->assertTags($result, $expected);
  5950. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5951. $expected = array(
  5952. 'form' => array(
  5953. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5954. 'accept-charset' => $encoding
  5955. ),
  5956. 'div' => array('style' => 'display:none;'),
  5957. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5958. '/div'
  5959. );
  5960. $this->assertTags($result, $expected);
  5961. $this->Form->request['action'] = 'add';
  5962. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5963. $expected = array(
  5964. 'form' => array(
  5965. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5966. 'accept-charset' => $encoding
  5967. ),
  5968. 'div' => array('style' => 'display:none;'),
  5969. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5970. '/div'
  5971. );
  5972. $this->assertTags($result, $expected);
  5973. $result = $this->Form->create('User', array('action' => 'login'));
  5974. $expected = array(
  5975. 'form' => array(
  5976. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5977. 'accept-charset' => $encoding
  5978. ),
  5979. 'div' => array('style' => 'display:none;'),
  5980. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5981. '/div'
  5982. );
  5983. $this->assertTags($result, $expected);
  5984. $result = $this->Form->create('User', array('url' => '/users/login'));
  5985. $expected = array(
  5986. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5987. 'div' => array('style' => 'display:none;'),
  5988. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5989. '/div'
  5990. );
  5991. $this->assertTags($result, $expected);
  5992. $this->Form->request['controller'] = 'pages';
  5993. $result = $this->Form->create('User', array('action' => 'signup'));
  5994. $expected = array(
  5995. 'form' => array(
  5996. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  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. $this->Form->request->data = array();
  6005. $this->Form->request['controller'] = 'contacts';
  6006. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6007. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6008. $expected = array(
  6009. 'form' => array(
  6010. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6011. 'accept-charset' => 'utf-8'
  6012. ),
  6013. 'div' => array('style' => 'display:none;'),
  6014. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6015. '/div'
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. }
  6019. /**
  6020. * Test the onsubmit option for create()
  6021. *
  6022. * @return void
  6023. */
  6024. public function testCreateOnSubmit() {
  6025. $this->Form->request->data = array();
  6026. $this->Form->request['controller'] = 'contacts';
  6027. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6028. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6029. $expected = array(
  6030. 'form' => array(
  6031. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6032. 'accept-charset' => 'utf-8'
  6033. ),
  6034. 'div' => array('style' => 'display:none;'),
  6035. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6036. '/div'
  6037. );
  6038. $this->assertTags($result, $expected);
  6039. $this->Form->request->data = array();
  6040. $this->Form->request['controller'] = 'contacts';
  6041. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6042. $result = $this->Form->create(array(
  6043. 'url' => array('action' => 'index', 'param'),
  6044. 'default' => false,
  6045. 'onsubmit' => 'someFunction();'
  6046. ));
  6047. $expected = array(
  6048. 'form' => array(
  6049. 'id' => 'ContactAddForm', 'method' => 'post',
  6050. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6051. 'action' => '/contacts/index/param',
  6052. 'accept-charset' => 'utf-8'
  6053. ),
  6054. 'div' => array('style' => 'display:none;'),
  6055. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6056. '/div'
  6057. );
  6058. $this->assertTags($result, $expected);
  6059. }
  6060. /**
  6061. * test create() with automatic url generation
  6062. *
  6063. * @return void
  6064. */
  6065. public function testCreateAutoUrl() {
  6066. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6067. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6068. $this->Form->request->base = '/base_url';
  6069. $result = $this->Form->create('Contact');
  6070. $expected = array(
  6071. 'form' => array(
  6072. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6073. 'accept-charset' => 'utf-8'
  6074. ),
  6075. 'div' => array('style' => 'display:none;'),
  6076. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6077. '/div'
  6078. );
  6079. $this->assertTags($result, $expected);
  6080. $this->Form->request['action'] = 'delete';
  6081. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6082. $this->Form->request->base = '/base_url';
  6083. $result = $this->Form->create('Contact');
  6084. $expected = array(
  6085. 'form' => array(
  6086. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6087. 'accept-charset' => 'utf-8'
  6088. ),
  6089. 'div' => array('style' => 'display:none;'),
  6090. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6091. '/div'
  6092. );
  6093. $this->assertTags($result, $expected);
  6094. }
  6095. /**
  6096. * test create() with a custom route
  6097. *
  6098. * @return void
  6099. */
  6100. public function testCreateCustomRoute() {
  6101. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6102. $encoding = strtolower(Configure::read('App.encoding'));
  6103. $result = $this->Form->create('User', array('action' => 'login'));
  6104. $expected = array(
  6105. 'form' => array(
  6106. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6107. 'accept-charset' => $encoding
  6108. ),
  6109. 'div' => array('style' => 'display:none;'),
  6110. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6111. '/div'
  6112. );
  6113. $this->assertTags($result, $expected);
  6114. }
  6115. /**
  6116. * test that inputDefaults are stored and used.
  6117. *
  6118. * @return void
  6119. */
  6120. public function testCreateWithInputDefaults() {
  6121. $this->Form->create('User', array(
  6122. 'inputDefaults' => array(
  6123. 'div' => false,
  6124. 'label' => false,
  6125. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6126. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6127. )
  6128. ));
  6129. $result = $this->Form->input('username');
  6130. $expected = array(
  6131. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6132. );
  6133. $this->assertTags($result, $expected);
  6134. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6135. $expected = array(
  6136. 'div' => array('class' => 'input text'),
  6137. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6138. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6139. '/div'
  6140. );
  6141. $this->assertTags($result, $expected);
  6142. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6143. $expected = array(
  6144. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6145. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6146. );
  6147. $this->assertTags($result, $expected);
  6148. $this->Form->create('User', array(
  6149. 'inputDefaults' => array(
  6150. 'div' => false,
  6151. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6152. )
  6153. ));
  6154. $result = $this->Form->input('username', array('div' => true));
  6155. $expected = array(
  6156. 'div' => array('class' => 'input text'),
  6157. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6158. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6159. '/div'
  6160. );
  6161. $this->assertTags($result, $expected);
  6162. }
  6163. /**
  6164. * test automatic accept-charset overriding
  6165. *
  6166. * @return void
  6167. */
  6168. public function testCreateWithAcceptCharset() {
  6169. $result = $this->Form->create('UserForm', array(
  6170. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6171. )
  6172. );
  6173. $expected = array(
  6174. 'form' => array(
  6175. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6176. 'accept-charset' => 'iso-8859-1'
  6177. ),
  6178. 'div' => array('style' => 'display:none;'),
  6179. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6180. '/div'
  6181. );
  6182. $this->assertTags($result, $expected);
  6183. }
  6184. /**
  6185. * Test base form url when url param is passed with multiple parameters (&)
  6186. *
  6187. */
  6188. public function testCreateQuerystringrequest() {
  6189. $encoding = strtolower(Configure::read('App.encoding'));
  6190. $result = $this->Form->create('Contact', array(
  6191. 'type' => 'post',
  6192. 'escape' => false,
  6193. 'url' => array(
  6194. 'controller' => 'controller',
  6195. 'action' => 'action',
  6196. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6197. )
  6198. ));
  6199. $expected = array(
  6200. 'form' => array(
  6201. 'id' => 'ContactAddForm',
  6202. 'method' => 'post',
  6203. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6204. 'accept-charset' => $encoding
  6205. ),
  6206. 'div' => array('style' => 'display:none;'),
  6207. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6208. '/div'
  6209. );
  6210. $this->assertTags($result, $expected);
  6211. $result = $this->Form->create('Contact', array(
  6212. 'type' => 'post',
  6213. 'url' => array(
  6214. 'controller' => 'controller',
  6215. 'action' => 'action',
  6216. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6217. )
  6218. ));
  6219. $expected = array(
  6220. 'form' => array(
  6221. 'id' => 'ContactAddForm',
  6222. 'method' => 'post',
  6223. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6224. 'accept-charset' => $encoding
  6225. ),
  6226. 'div' => array('style' => 'display:none;'),
  6227. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6228. '/div'
  6229. );
  6230. $this->assertTags($result, $expected);
  6231. }
  6232. /**
  6233. * test that create() doesn't cause errors by multiple id's being in the primary key
  6234. * as could happen with multiple select or checkboxes.
  6235. *
  6236. * @return void
  6237. */
  6238. public function testCreateWithMultipleIdInData() {
  6239. $encoding = strtolower(Configure::read('App.encoding'));
  6240. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6241. $result = $this->Form->create('Contact');
  6242. $expected = array(
  6243. 'form' => array(
  6244. 'id' => 'ContactAddForm',
  6245. 'method' => 'post',
  6246. 'action' => '/contacts/add',
  6247. 'accept-charset' => $encoding
  6248. ),
  6249. 'div' => array('style' => 'display:none;'),
  6250. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6251. '/div'
  6252. );
  6253. $this->assertTags($result, $expected);
  6254. }
  6255. /**
  6256. * test that create() doesn't add in extra passed params.
  6257. *
  6258. * @return void
  6259. */
  6260. public function testCreatePassedArgs() {
  6261. $encoding = strtolower(Configure::read('App.encoding'));
  6262. $this->Form->request->data['Contact']['id'] = 1;
  6263. $result = $this->Form->create('Contact', array(
  6264. 'type' => 'post',
  6265. 'escape' => false,
  6266. 'url' => array(
  6267. 'action' => 'edit',
  6268. 'myparam'
  6269. )
  6270. ));
  6271. $expected = array(
  6272. 'form' => array(
  6273. 'id' => 'ContactAddForm',
  6274. 'method' => 'post',
  6275. 'action' => '/contacts/edit/myparam',
  6276. 'accept-charset' => $encoding
  6277. ),
  6278. 'div' => array('style' => 'display:none;'),
  6279. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6280. '/div'
  6281. );
  6282. $this->assertTags($result, $expected);
  6283. }
  6284. /**
  6285. * test creating a get form, and get form inputs.
  6286. *
  6287. * @return void
  6288. */
  6289. public function testGetFormCreate() {
  6290. $encoding = strtolower(Configure::read('App.encoding'));
  6291. $result = $this->Form->create('Contact', array('type' => 'get'));
  6292. $this->assertTags($result, array('form' => array(
  6293. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6294. 'accept-charset' => $encoding
  6295. )));
  6296. $result = $this->Form->text('Contact.name');
  6297. $this->assertTags($result, array('input' => array(
  6298. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6299. )));
  6300. $result = $this->Form->password('password');
  6301. $this->assertTags($result, array('input' => array(
  6302. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6303. )));
  6304. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6305. $result = $this->Form->text('user_form');
  6306. $this->assertTags($result, array('input' => array(
  6307. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6308. )));
  6309. }
  6310. /**
  6311. * test get form, and inputs when the model param is false
  6312. *
  6313. * @return void
  6314. */
  6315. public function testGetFormWithFalseModel() {
  6316. $encoding = strtolower(Configure::read('App.encoding'));
  6317. $this->Form->request['controller'] = 'contact_test';
  6318. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6319. $expected = array('form' => array(
  6320. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6321. 'accept-charset' => $encoding
  6322. ));
  6323. $this->assertTags($result, $expected);
  6324. $result = $this->Form->text('reason');
  6325. $expected = array(
  6326. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6327. );
  6328. $this->assertTags($result, $expected);
  6329. }
  6330. /**
  6331. * test that datetime() works with GET style forms.
  6332. *
  6333. * @return void
  6334. */
  6335. public function testDateTimeWithGetForms() {
  6336. extract($this->dateRegex);
  6337. $this->Form->create('Contact', array('type' => 'get'));
  6338. $result = $this->Form->datetime('created');
  6339. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6340. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6341. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6342. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6343. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6344. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6345. }
  6346. /**
  6347. * testEditFormWithData method
  6348. *
  6349. * test auto populating form elements from submitted data.
  6350. *
  6351. * @return void
  6352. */
  6353. public function testEditFormWithData() {
  6354. $this->Form->request->data = array('Person' => array(
  6355. 'id' => 1,
  6356. 'first_name' => 'Nate',
  6357. 'last_name' => 'Abele',
  6358. 'email' => 'nate@example.com'
  6359. ));
  6360. $this->Form->request->addParams(array(
  6361. 'models' => array('Person'),
  6362. 'controller' => 'people',
  6363. 'action' => 'add'
  6364. ));
  6365. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6366. $this->Form->create();
  6367. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6368. $expected = array(
  6369. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6370. 'select' => array(
  6371. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6372. ),
  6373. array('option' => array('value' => 1)), 'Nate', '/option',
  6374. array('option' => array('value' => 2)), 'Garrett', '/option',
  6375. array('option' => array('value' => 3)), 'Larry', '/option',
  6376. '/select'
  6377. );
  6378. $this->assertTags($result, $expected);
  6379. }
  6380. /**
  6381. * Test that required fields are created for various types of validation.
  6382. *
  6383. * @return void
  6384. */
  6385. public function testFormInputRequiredDetection() {
  6386. $this->Form->create('Contact');
  6387. $result = $this->Form->input('Contact.non_existing');
  6388. $expected = array(
  6389. 'div' => array('class' => 'input text'),
  6390. 'label' => array('for' => 'ContactNonExisting'),
  6391. 'Non Existing',
  6392. '/label',
  6393. 'input' => array(
  6394. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6395. 'id' => 'ContactNonExisting'
  6396. ),
  6397. '/div'
  6398. );
  6399. $this->assertTags($result, $expected);
  6400. $result = $this->Form->input('Contact.imrequired');
  6401. $expected = array(
  6402. 'div' => array('class' => 'input text required'),
  6403. 'label' => array('for' => 'ContactImrequired'),
  6404. 'Imrequired',
  6405. '/label',
  6406. 'input' => array(
  6407. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6408. 'id' => 'ContactImrequired'
  6409. ),
  6410. '/div'
  6411. );
  6412. $this->assertTags($result, $expected);
  6413. $result = $this->Form->input('Contact.imalsorequired');
  6414. $expected = array(
  6415. 'div' => array('class' => 'input text required'),
  6416. 'label' => array('for' => 'ContactImalsorequired'),
  6417. 'Imalsorequired',
  6418. '/label',
  6419. 'input' => array(
  6420. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6421. 'id' => 'ContactImalsorequired'
  6422. ),
  6423. '/div'
  6424. );
  6425. $this->assertTags($result, $expected);
  6426. $result = $this->Form->input('Contact.imrequiredtoo');
  6427. $expected = array(
  6428. 'div' => array('class' => 'input text required'),
  6429. 'label' => array('for' => 'ContactImrequiredtoo'),
  6430. 'Imrequiredtoo',
  6431. '/label',
  6432. 'input' => array(
  6433. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6434. 'id' => 'ContactImrequiredtoo'
  6435. ),
  6436. '/div'
  6437. );
  6438. $this->assertTags($result, $expected);
  6439. $result = $this->Form->input('Contact.required_one');
  6440. $expected = array(
  6441. 'div' => array('class' => 'input text required'),
  6442. 'label' => array('for' => 'ContactRequiredOne'),
  6443. 'Required One',
  6444. '/label',
  6445. 'input' => array(
  6446. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6447. 'id' => 'ContactRequiredOne'
  6448. ),
  6449. '/div'
  6450. );
  6451. $this->assertTags($result, $expected);
  6452. $result = $this->Form->input('Contact.string_required');
  6453. $expected = array(
  6454. 'div' => array('class' => 'input text required'),
  6455. 'label' => array('for' => 'ContactStringRequired'),
  6456. 'String Required',
  6457. '/label',
  6458. 'input' => array(
  6459. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6460. 'id' => 'ContactStringRequired'
  6461. ),
  6462. '/div'
  6463. );
  6464. $this->assertTags($result, $expected);
  6465. $result = $this->Form->input('Contact.imnotrequired');
  6466. $expected = array(
  6467. 'div' => array('class' => 'input text'),
  6468. 'label' => array('for' => 'ContactImnotrequired'),
  6469. 'Imnotrequired',
  6470. '/label',
  6471. 'input' => array(
  6472. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6473. 'id' => 'ContactImnotrequired'
  6474. ),
  6475. '/div'
  6476. );
  6477. $this->assertTags($result, $expected);
  6478. $result = $this->Form->input('Contact.imalsonotrequired');
  6479. $expected = array(
  6480. 'div' => array('class' => 'input text'),
  6481. 'label' => array('for' => 'ContactImalsonotrequired'),
  6482. 'Imalsonotrequired',
  6483. '/label',
  6484. 'input' => array(
  6485. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6486. 'id' => 'ContactImalsonotrequired'
  6487. ),
  6488. '/div'
  6489. );
  6490. $this->assertTags($result, $expected);
  6491. $result = $this->Form->input('Contact.imnotrequiredeither');
  6492. $expected = array(
  6493. 'div' => array('class' => 'input text'),
  6494. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6495. 'Imnotrequiredeither',
  6496. '/label',
  6497. 'input' => array(
  6498. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6499. 'id' => 'ContactImnotrequiredeither'
  6500. ),
  6501. '/div'
  6502. );
  6503. $this->assertTags($result, $expected);
  6504. $result = $this->Form->input('Contact.iamrequiredalways');
  6505. $expected = array(
  6506. 'div' => array('class' => 'input text required'),
  6507. 'label' => array('for' => 'ContactIamrequiredalways'),
  6508. 'Iamrequiredalways',
  6509. '/label',
  6510. 'input' => array(
  6511. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6512. 'id' => 'ContactIamrequiredalways'
  6513. ),
  6514. '/div'
  6515. );
  6516. $this->assertTags($result, $expected);
  6517. }
  6518. /**
  6519. * testFormMagicInput method
  6520. *
  6521. * @return void
  6522. */
  6523. public function testFormMagicInput() {
  6524. $encoding = strtolower(Configure::read('App.encoding'));
  6525. $result = $this->Form->create('Contact');
  6526. $expected = array(
  6527. 'form' => array(
  6528. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6529. 'accept-charset' => $encoding
  6530. ),
  6531. 'div' => array('style' => 'display:none;'),
  6532. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6533. '/div'
  6534. );
  6535. $this->assertTags($result, $expected);
  6536. $result = $this->Form->input('name');
  6537. $expected = array(
  6538. 'div' => array('class' => 'input text'),
  6539. 'label' => array('for' => 'ContactName'),
  6540. 'Name',
  6541. '/label',
  6542. 'input' => array(
  6543. 'type' => 'text', 'name' => 'data[Contact][name]',
  6544. 'id' => 'ContactName', 'maxlength' => '255'
  6545. ),
  6546. '/div'
  6547. );
  6548. $this->assertTags($result, $expected);
  6549. $result = $this->Form->input('non_existing_field_in_contact_model');
  6550. $expected = array(
  6551. 'div' => array('class' => 'input text'),
  6552. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6553. 'Non Existing Field In Contact Model',
  6554. '/label',
  6555. 'input' => array(
  6556. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6557. 'id' => 'ContactNonExistingFieldInContactModel'
  6558. ),
  6559. '/div'
  6560. );
  6561. $this->assertTags($result, $expected);
  6562. $result = $this->Form->input('Address.street');
  6563. $expected = array(
  6564. 'div' => array('class' => 'input text'),
  6565. 'label' => array('for' => 'AddressStreet'),
  6566. 'Street',
  6567. '/label',
  6568. 'input' => array(
  6569. 'type' => 'text', 'name' => 'data[Address][street]',
  6570. 'id' => 'AddressStreet'
  6571. ),
  6572. '/div'
  6573. );
  6574. $this->assertTags($result, $expected);
  6575. $result = $this->Form->input('Address.non_existing_field_in_model');
  6576. $expected = array(
  6577. 'div' => array('class' => 'input text'),
  6578. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6579. 'Non Existing Field In Model',
  6580. '/label',
  6581. 'input' => array(
  6582. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6583. 'id' => 'AddressNonExistingFieldInModel'
  6584. ),
  6585. '/div'
  6586. );
  6587. $this->assertTags($result, $expected);
  6588. $result = $this->Form->input('name', array('div' => false));
  6589. $expected = array(
  6590. 'label' => array('for' => 'ContactName'),
  6591. 'Name',
  6592. '/label',
  6593. 'input' => array(
  6594. 'type' => 'text', 'name' => 'data[Contact][name]',
  6595. 'id' => 'ContactName', 'maxlength' => '255'
  6596. )
  6597. );
  6598. $this->assertTags($result, $expected);
  6599. extract($this->dateRegex);
  6600. $now = strtotime('now');
  6601. $result = $this->Form->input('Contact.published', array('div' => false));
  6602. $expected = array(
  6603. 'label' => array('for' => 'ContactPublishedMonth'),
  6604. 'Published',
  6605. '/label',
  6606. array('select' => array(
  6607. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6608. )),
  6609. $monthsRegex,
  6610. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6611. date('F', $now),
  6612. '/option',
  6613. '*/select',
  6614. '-',
  6615. array('select' => array(
  6616. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6617. )),
  6618. $daysRegex,
  6619. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6620. date('j', $now),
  6621. '/option',
  6622. '*/select',
  6623. '-',
  6624. array('select' => array(
  6625. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6626. )),
  6627. $yearsRegex,
  6628. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6629. date('Y', $now),
  6630. '*/select'
  6631. );
  6632. $this->assertTags($result, $expected);
  6633. $result = $this->Form->input('Contact.updated', array('div' => false));
  6634. $expected = array(
  6635. 'label' => array('for' => 'ContactUpdatedMonth'),
  6636. 'Updated',
  6637. '/label',
  6638. array('select' => array(
  6639. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6640. )),
  6641. $monthsRegex,
  6642. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6643. date('F', $now),
  6644. '/option',
  6645. '*/select',
  6646. '-',
  6647. array('select' => array(
  6648. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6649. )),
  6650. $daysRegex,
  6651. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6652. date('j', $now),
  6653. '/option',
  6654. '*/select',
  6655. '-',
  6656. array('select' => array(
  6657. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6658. )),
  6659. $yearsRegex,
  6660. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6661. date('Y', $now),
  6662. '*/select'
  6663. );
  6664. $this->assertTags($result, $expected);
  6665. $result = $this->Form->input('UserForm.stuff');
  6666. $expected = array(
  6667. 'div' => array('class' => 'input text'),
  6668. 'label' => array('for' => 'UserFormStuff'),
  6669. 'Stuff',
  6670. '/label',
  6671. 'input' => array(
  6672. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6673. 'id' => 'UserFormStuff', 'maxlength' => 10
  6674. ),
  6675. '/div'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. }
  6679. /**
  6680. * testForMagicInputNonExistingNorValidated method
  6681. *
  6682. * @return void
  6683. */
  6684. public function testForMagicInputNonExistingNorValidated() {
  6685. $encoding = strtolower(Configure::read('App.encoding'));
  6686. $result = $this->Form->create('Contact');
  6687. $expected = array(
  6688. 'form' => array(
  6689. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6690. 'accept-charset' => $encoding
  6691. ),
  6692. 'div' => array('style' => 'display:none;'),
  6693. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6694. '/div'
  6695. );
  6696. $this->assertTags($result, $expected);
  6697. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6698. $expected = array(
  6699. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6700. 'Non Existing Nor Validated',
  6701. '/label',
  6702. 'input' => array(
  6703. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6704. 'id' => 'ContactNonExistingNorValidated'
  6705. )
  6706. );
  6707. $this->assertTags($result, $expected);
  6708. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6709. 'div' => false, 'value' => 'my value'
  6710. ));
  6711. $expected = array(
  6712. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6713. 'Non Existing Nor Validated',
  6714. '/label',
  6715. 'input' => array(
  6716. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6717. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6718. )
  6719. );
  6720. $this->assertTags($result, $expected);
  6721. $this->Form->request->data = array(
  6722. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6723. ));
  6724. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6725. $expected = array(
  6726. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6727. 'Non Existing Nor Validated',
  6728. '/label',
  6729. 'input' => array(
  6730. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6731. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6732. )
  6733. );
  6734. $this->assertTags($result, $expected);
  6735. }
  6736. /**
  6737. * testFormMagicInputLabel method
  6738. *
  6739. * @return void
  6740. */
  6741. public function testFormMagicInputLabel() {
  6742. $encoding = strtolower(Configure::read('App.encoding'));
  6743. $result = $this->Form->create('Contact');
  6744. $expected = array(
  6745. 'form' => array(
  6746. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6747. 'accept-charset' => $encoding
  6748. ),
  6749. 'div' => array('style' => 'display:none;'),
  6750. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6751. '/div'
  6752. );
  6753. $this->assertTags($result, $expected);
  6754. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6755. $this->assertTags($result, array('input' => array(
  6756. 'name' => 'data[Contact][name]', 'type' => 'text',
  6757. 'id' => 'ContactName', 'maxlength' => '255')
  6758. ));
  6759. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6760. $expected = array(
  6761. 'label' => array('for' => 'ContactName'),
  6762. 'My label',
  6763. '/label',
  6764. 'input' => array(
  6765. 'type' => 'text', 'name' => 'data[Contact][name]',
  6766. 'id' => 'ContactName', 'maxlength' => '255'
  6767. )
  6768. );
  6769. $this->assertTags($result, $expected);
  6770. $result = $this->Form->input('Contact.name', array(
  6771. 'div' => false, 'label' => array('class' => 'mandatory')
  6772. ));
  6773. $expected = array(
  6774. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6775. 'Name',
  6776. '/label',
  6777. 'input' => array(
  6778. 'type' => 'text', 'name' => 'data[Contact][name]',
  6779. 'id' => 'ContactName', 'maxlength' => '255'
  6780. )
  6781. );
  6782. $this->assertTags($result, $expected);
  6783. $result = $this->Form->input('Contact.name', array(
  6784. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6785. ));
  6786. $expected = array(
  6787. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6788. 'My label',
  6789. '/label',
  6790. 'input' => array(
  6791. 'type' => 'text', 'name' => 'data[Contact][name]',
  6792. 'id' => 'ContactName', 'maxlength' => '255'
  6793. )
  6794. );
  6795. $this->assertTags($result, $expected);
  6796. $result = $this->Form->input('Contact.name', array(
  6797. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6798. ));
  6799. $expected = array(
  6800. 'label' => array('for' => 'my_id'),
  6801. 'Name',
  6802. '/label',
  6803. 'input' => array(
  6804. 'type' => 'text', 'name' => 'data[Contact][name]',
  6805. 'id' => 'my_id', 'maxlength' => '255'
  6806. )
  6807. );
  6808. $this->assertTags($result, $expected);
  6809. $result = $this->Form->input('1.id');
  6810. $this->assertTags($result, array('input' => array(
  6811. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6812. 'id' => 'Contact1Id'
  6813. )));
  6814. $result = $this->Form->input("1.name");
  6815. $expected = array(
  6816. 'div' => array('class' => 'input text'),
  6817. 'label' => array('for' => 'Contact1Name'),
  6818. 'Name',
  6819. '/label',
  6820. 'input' => array(
  6821. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6822. 'id' => 'Contact1Name', 'maxlength' => '255'
  6823. ),
  6824. '/div'
  6825. );
  6826. $this->assertTags($result, $expected);
  6827. $result = $this->Form->input('Contact.1.id');
  6828. $this->assertTags($result, array(
  6829. 'input' => array(
  6830. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6831. 'id' => 'Contact1Id'
  6832. )
  6833. ));
  6834. $result = $this->Form->input("Model.1.name");
  6835. $expected = array(
  6836. 'div' => array('class' => 'input text'),
  6837. 'label' => array('for' => 'Model1Name'),
  6838. 'Name',
  6839. '/label',
  6840. 'input' => array(
  6841. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6842. 'id' => 'Model1Name'
  6843. ),
  6844. '/div'
  6845. );
  6846. $this->assertTags($result, $expected);
  6847. }
  6848. /**
  6849. * testFormEnd method
  6850. *
  6851. * @return void
  6852. */
  6853. public function testFormEnd() {
  6854. $this->assertEquals('</form>', $this->Form->end());
  6855. $result = $this->Form->end('');
  6856. $expected = array(
  6857. 'div' => array('class' => 'submit'),
  6858. 'input' => array('type' => 'submit', 'value' => ''),
  6859. '/div',
  6860. '/form'
  6861. );
  6862. $this->assertTags($result, $expected);
  6863. $result = $this->Form->end(array('label' => ''));
  6864. $expected = array(
  6865. 'div' => array('class' => 'submit'),
  6866. 'input' => array('type' => 'submit', 'value' => ''),
  6867. '/div',
  6868. '/form'
  6869. );
  6870. $this->assertTags($result, $expected);
  6871. $result = $this->Form->end('save');
  6872. $expected = array(
  6873. 'div' => array('class' => 'submit'),
  6874. 'input' => array('type' => 'submit', 'value' => 'save'),
  6875. '/div',
  6876. '/form'
  6877. );
  6878. $this->assertTags($result, $expected);
  6879. $result = $this->Form->end(array('label' => 'save'));
  6880. $expected = array(
  6881. 'div' => array('class' => 'submit'),
  6882. 'input' => array('type' => 'submit', 'value' => 'save'),
  6883. '/div',
  6884. '/form'
  6885. );
  6886. $this->assertTags($result, $expected);
  6887. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6888. $expected = array(
  6889. 'div' => array('class' => 'submit'),
  6890. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6891. '/div',
  6892. '/form'
  6893. );
  6894. $this->assertTags($result, $expected);
  6895. $result = $this->Form->end(array('name' => 'Whatever'));
  6896. $expected = array(
  6897. 'div' => array('class' => 'submit'),
  6898. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6899. '/div',
  6900. '/form'
  6901. );
  6902. $this->assertTags($result, $expected);
  6903. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6904. $expected = array(
  6905. 'div' => array('class' => 'good'),
  6906. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6907. '/div',
  6908. '/form'
  6909. );
  6910. $this->assertTags($result, $expected);
  6911. $result = $this->Form->end(array(
  6912. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6913. ));
  6914. $expected = array(
  6915. 'div' => array('class' => 'good'),
  6916. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6917. '/div',
  6918. '/form'
  6919. );
  6920. $this->assertTags($result, $expected);
  6921. }
  6922. /**
  6923. * testMultipleFormWithIdFields method
  6924. *
  6925. * @return void
  6926. */
  6927. public function testMultipleFormWithIdFields() {
  6928. $this->Form->create('UserForm');
  6929. $result = $this->Form->input('id');
  6930. $this->assertTags($result, array('input' => array(
  6931. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6932. )));
  6933. $result = $this->Form->input('ValidateItem.id');
  6934. $this->assertTags($result, array('input' => array(
  6935. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6936. 'id' => 'ValidateItemId'
  6937. )));
  6938. $result = $this->Form->input('ValidateUser.id');
  6939. $this->assertTags($result, array('input' => array(
  6940. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6941. 'id' => 'ValidateUserId'
  6942. )));
  6943. }
  6944. /**
  6945. * testDbLessModel method
  6946. *
  6947. * @return void
  6948. */
  6949. public function testDbLessModel() {
  6950. $this->Form->create('TestMail');
  6951. $result = $this->Form->input('name');
  6952. $expected = array(
  6953. 'div' => array('class' => 'input text'),
  6954. 'label' => array('for' => 'TestMailName'),
  6955. 'Name',
  6956. '/label',
  6957. 'input' => array(
  6958. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6959. 'id' => 'TestMailName'
  6960. ),
  6961. '/div'
  6962. );
  6963. $this->assertTags($result, $expected);
  6964. ClassRegistry::init('TestMail');
  6965. $this->Form->create('TestMail');
  6966. $result = $this->Form->input('name');
  6967. $expected = array(
  6968. 'div' => array('class' => 'input text'),
  6969. 'label' => array('for' => 'TestMailName'),
  6970. 'Name',
  6971. '/label',
  6972. 'input' => array(
  6973. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6974. 'id' => 'TestMailName'
  6975. ),
  6976. '/div'
  6977. );
  6978. $this->assertTags($result, $expected);
  6979. }
  6980. /**
  6981. * testBrokenness method
  6982. *
  6983. * @return void
  6984. */
  6985. public function testBrokenness() {
  6986. /*
  6987. * #4 This test has two parents and four children. By default (as of r7117) both
  6988. * parents are show but the first parent is missing a child. This is the inconsistency
  6989. * in the default behaviour - one parent has all children, the other does not - dependent
  6990. * on the data values.
  6991. */
  6992. $result = $this->Form->select('Model.field', array(
  6993. 'Fred' => array(
  6994. 'freds_son_1' => 'Fred',
  6995. 'freds_son_2' => 'Freddie'
  6996. ),
  6997. 'Bert' => array(
  6998. 'berts_son_1' => 'Albert',
  6999. 'berts_son_2' => 'Bertie')
  7000. ),
  7001. array('showParents' => true, 'empty' => false)
  7002. );
  7003. $expected = array(
  7004. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7005. array('optgroup' => array('label' => 'Fred')),
  7006. array('option' => array('value' => 'freds_son_1')),
  7007. 'Fred',
  7008. '/option',
  7009. array('option' => array('value' => 'freds_son_2')),
  7010. 'Freddie',
  7011. '/option',
  7012. '/optgroup',
  7013. array('optgroup' => array('label' => 'Bert')),
  7014. array('option' => array('value' => 'berts_son_1')),
  7015. 'Albert',
  7016. '/option',
  7017. array('option' => array('value' => 'berts_son_2')),
  7018. 'Bertie',
  7019. '/option',
  7020. '/optgroup',
  7021. '/select'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. /*
  7025. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7026. * changed, so we should expect the same select list data, just with a different name
  7027. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7028. * This is where data corruption can occur, because when a select value is missing from
  7029. * a list a form will substitute the first value in the list - without the user knowing.
  7030. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7031. * affect the availability of 3 => 'Three' as a valid option.
  7032. */
  7033. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7034. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7035. ));
  7036. $result = $this->Form->select(
  7037. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7038. );
  7039. $expected = array(
  7040. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7041. array('option' => array('value' => 1)),
  7042. 'One',
  7043. '/option',
  7044. array('option' => array('value' => 2)),
  7045. 'Two',
  7046. '/option',
  7047. array('optgroup' => array('label' => 'Three')),
  7048. array('option' => array('value' => 3)),
  7049. 'Three',
  7050. '/option',
  7051. array('option' => array('value' => 4)),
  7052. 'Four',
  7053. '/option',
  7054. array('option' => array('value' => 5)),
  7055. 'Five',
  7056. '/option',
  7057. '/optgroup',
  7058. '/select'
  7059. );
  7060. $this->assertTags($result, $expected);
  7061. }
  7062. /**
  7063. * Test the generation of fields for a multi record form.
  7064. *
  7065. * @return void
  7066. */
  7067. public function testMultiRecordForm() {
  7068. $this->Form->create('ValidateProfile');
  7069. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7070. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7071. $expected = array(
  7072. 'div' => array('class' => 'input textarea'),
  7073. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7074. 'Name',
  7075. '/label',
  7076. 'textarea' => array(
  7077. 'id' => 'ValidateProfile1ValidateItem2Name',
  7078. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7079. 'cols' => 30,
  7080. 'rows' => 6
  7081. ),
  7082. 'Value',
  7083. '/textarea',
  7084. '/div'
  7085. );
  7086. $this->assertTags($result, $expected);
  7087. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7088. $expected = array(
  7089. 'div' => array('class' => 'input date'),
  7090. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7091. 'Created',
  7092. '/label',
  7093. array('select' => array(
  7094. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7095. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7096. )
  7097. ),
  7098. array('option' => array('value' => '')), '/option',
  7099. $this->dateRegex['monthsRegex'],
  7100. '/select', '-',
  7101. array('select' => array(
  7102. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7103. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7104. )
  7105. ),
  7106. array('option' => array('value' => '')), '/option',
  7107. $this->dateRegex['daysRegex'],
  7108. '/select', '-',
  7109. array('select' => array(
  7110. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7111. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7112. )
  7113. ),
  7114. array('option' => array('value' => '')), '/option',
  7115. $this->dateRegex['yearsRegex'],
  7116. '/select',
  7117. '/div'
  7118. );
  7119. $this->assertTags($result, $expected);
  7120. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7121. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7122. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7123. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7124. $expected = array(
  7125. 'div' => array('class' => 'input select error'),
  7126. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7127. 'Profile',
  7128. '/label',
  7129. 'select' => array(
  7130. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7131. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7132. 'class' => 'form-error'
  7133. ),
  7134. '/select',
  7135. array('div' => array('class' => 'error-message')),
  7136. 'Error',
  7137. '/div',
  7138. '/div'
  7139. );
  7140. $this->assertTags($result, $expected);
  7141. }
  7142. /**
  7143. * test the correct display of multi-record form validation errors.
  7144. *
  7145. * @return void
  7146. */
  7147. public function testMultiRecordFormValidationErrors() {
  7148. $this->Form->create('ValidateProfile');
  7149. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7150. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7151. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7152. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7153. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7154. $result = $this->Form->error('ValidateProfile.2.city');
  7155. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7156. $result = $this->Form->error('2.city');
  7157. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7158. }
  7159. /**
  7160. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7161. *
  7162. * @return void
  7163. */
  7164. public function testInputTemplate() {
  7165. $result = $this->Form->input('Contact.email', array(
  7166. 'type' => 'text', 'format' => array('input')
  7167. ));
  7168. $expected = array(
  7169. 'div' => array('class' => 'input text'),
  7170. 'input' => array(
  7171. 'type' => 'text', 'name' => 'data[Contact][email]',
  7172. 'id' => 'ContactEmail'
  7173. ),
  7174. '/div'
  7175. );
  7176. $this->assertTags($result, $expected);
  7177. $result = $this->Form->input('Contact.email', array(
  7178. 'type' => 'text', 'format' => array('input', 'label'),
  7179. 'label' => '<em>Email (required)</em>'
  7180. ));
  7181. $expected = array(
  7182. 'div' => array('class' => 'input text'),
  7183. array('input' => array(
  7184. 'type' => 'text', 'name' => 'data[Contact][email]',
  7185. 'id' => 'ContactEmail'
  7186. )),
  7187. 'label' => array('for' => 'ContactEmail'),
  7188. 'em' => array(),
  7189. 'Email (required)',
  7190. '/em',
  7191. '/label',
  7192. '/div'
  7193. );
  7194. $this->assertTags($result, $expected);
  7195. $result = $this->Form->input('Contact.email', array(
  7196. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7197. 'between' => '<div>Something in the middle</div>',
  7198. 'after' => '<span>Some text at the end</span>'
  7199. ));
  7200. $expected = array(
  7201. 'div' => array('class' => 'input text'),
  7202. array('input' => array(
  7203. 'type' => 'text', 'name' => 'data[Contact][email]',
  7204. 'id' => 'ContactEmail'
  7205. )),
  7206. array('div' => array()),
  7207. 'Something in the middle',
  7208. '/div',
  7209. 'label' => array('for' => 'ContactEmail'),
  7210. 'Email',
  7211. '/label',
  7212. 'span' => array(),
  7213. 'Some text at the end',
  7214. '/span',
  7215. '/div'
  7216. );
  7217. $this->assertTags($result, $expected);
  7218. $result = $this->Form->input('Contact.method', array(
  7219. 'type' => 'radio',
  7220. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7221. 'between' => 'I am between',
  7222. ));
  7223. $expected = array(
  7224. 'div' => array('class' => 'input radio'),
  7225. 'fieldset' => array(),
  7226. 'legend' => array(),
  7227. 'Method',
  7228. '/legend',
  7229. 'I am between',
  7230. 'input' => array(
  7231. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7232. 'value' => '', 'id' => 'ContactMethod_'
  7233. ),
  7234. array('input' => array(
  7235. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7236. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7237. )),
  7238. array('label' => array('for' => 'ContactMethodEmail')),
  7239. 'Email',
  7240. '/label',
  7241. array('input' => array(
  7242. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7243. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7244. )),
  7245. array('label' => array('for' => 'ContactMethodPigeon')),
  7246. 'Pigeon',
  7247. '/label',
  7248. '/fieldset',
  7249. '/div',
  7250. );
  7251. $this->assertTags($result, $expected);
  7252. }
  7253. /**
  7254. * test that some html5 inputs + FormHelper::__call() work
  7255. *
  7256. * @return void
  7257. */
  7258. public function testHtml5Inputs() {
  7259. $result = $this->Form->email('User.email');
  7260. $expected = array(
  7261. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7262. );
  7263. $this->assertTags($result, $expected);
  7264. $result = $this->Form->search('User.query');
  7265. $expected = array(
  7266. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. $result = $this->Form->search('User.query', array('value' => 'test'));
  7270. $expected = array(
  7271. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7272. );
  7273. $this->assertTags($result, $expected);
  7274. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7275. $expected = array(
  7276. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7277. );
  7278. $this->assertTags($result, $expected);
  7279. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7280. $expected = array(
  7281. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7282. );
  7283. $this->assertTags($result, $expected);
  7284. }
  7285. /**
  7286. *
  7287. * @expectedException CakeException
  7288. * @return void
  7289. */
  7290. public function testHtml5InputException() {
  7291. $this->Form->email();
  7292. }
  7293. /**
  7294. * Tests that a model can be loaded from the model names passed in the request object
  7295. *
  7296. * @return void
  7297. */
  7298. public function testIntrospectModelFromRequest() {
  7299. $this->loadFixtures('Post');
  7300. App::build(array(
  7301. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7302. ));
  7303. CakePlugin::load('TestPlugin');
  7304. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7305. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7306. $this->Form->create('TestPluginPost');
  7307. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7308. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7309. CakePlugin::unload();
  7310. App::build();
  7311. }
  7312. /**
  7313. * Tests that it is possible to set the validation errors directly in the helper for a field
  7314. *
  7315. * @return void
  7316. */
  7317. public function testCustomValidationErrors() {
  7318. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7319. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7320. $this->assertEquals('Badness!', $result);
  7321. }
  7322. /**
  7323. * Tests that the 'on' key validates as expected on create
  7324. *
  7325. * @return void
  7326. */
  7327. public function testRequiredOnCreate() {
  7328. $this->Form->create('Contact');
  7329. $result = $this->Form->input('Contact.imrequiredonupdate');
  7330. $expected = array(
  7331. 'div' => array('class' => 'input text'),
  7332. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7333. 'Imrequiredonupdate',
  7334. '/label',
  7335. 'input' => array(
  7336. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7337. 'id' => 'ContactImrequiredonupdate'
  7338. ),
  7339. '/div'
  7340. );
  7341. $this->assertTags($result, $expected);
  7342. $result = $this->Form->input('Contact.imrequiredoncreate');
  7343. $expected = array(
  7344. 'div' => array('class' => 'input text required'),
  7345. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7346. 'Imrequiredoncreate',
  7347. '/label',
  7348. 'input' => array(
  7349. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7350. 'id' => 'ContactImrequiredoncreate'
  7351. ),
  7352. '/div'
  7353. );
  7354. $this->assertTags($result, $expected);
  7355. $result = $this->Form->input('Contact.imrequiredonboth');
  7356. $expected = array(
  7357. 'div' => array('class' => 'input text required'),
  7358. 'label' => array('for' => 'ContactImrequiredonboth'),
  7359. 'Imrequiredonboth',
  7360. '/label',
  7361. 'input' => array(
  7362. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7363. 'id' => 'ContactImrequiredonboth'
  7364. ),
  7365. '/div'
  7366. );
  7367. $this->assertTags($result, $expected);
  7368. $result = $this->Form->input('Contact.imrequired');
  7369. $expected = array(
  7370. 'div' => array('class' => 'input text required'),
  7371. 'label' => array('for' => 'ContactImrequired'),
  7372. 'Imrequired',
  7373. '/label',
  7374. 'input' => array(
  7375. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7376. 'id' => 'ContactImrequired'
  7377. ),
  7378. '/div'
  7379. );
  7380. $this->assertTags($result, $expected);
  7381. }
  7382. /**
  7383. * Tests that the 'on' key validates as expected on update
  7384. *
  7385. * @return void
  7386. */
  7387. public function testRequiredOnUpdate() {
  7388. $this->Form->request->data['Contact']['id'] = 1;
  7389. $this->Form->create('Contact');
  7390. $result = $this->Form->input('Contact.imrequiredonupdate');
  7391. $expected = array(
  7392. 'div' => array('class' => 'input text required'),
  7393. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7394. 'Imrequiredonupdate',
  7395. '/label',
  7396. 'input' => array(
  7397. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7398. 'id' => 'ContactImrequiredonupdate'
  7399. ),
  7400. '/div'
  7401. );
  7402. $this->assertTags($result, $expected);
  7403. $result = $this->Form->input('Contact.imrequiredoncreate');
  7404. $expected = array(
  7405. 'div' => array('class' => 'input text'),
  7406. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7407. 'Imrequiredoncreate',
  7408. '/label',
  7409. 'input' => array(
  7410. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7411. 'id' => 'ContactImrequiredoncreate'
  7412. ),
  7413. '/div'
  7414. );
  7415. $this->assertTags($result, $expected);
  7416. $result = $this->Form->input('Contact.imrequiredonboth');
  7417. $expected = array(
  7418. 'div' => array('class' => 'input text required'),
  7419. 'label' => array('for' => 'ContactImrequiredonboth'),
  7420. 'Imrequiredonboth',
  7421. '/label',
  7422. 'input' => array(
  7423. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7424. 'id' => 'ContactImrequiredonboth'
  7425. ),
  7426. '/div'
  7427. );
  7428. $this->assertTags($result, $expected);
  7429. $result = $this->Form->input('Contact.imrequired');
  7430. $expected = array(
  7431. 'div' => array('class' => 'input text required'),
  7432. 'label' => array('for' => 'ContactImrequired'),
  7433. 'Imrequired',
  7434. '/label',
  7435. 'input' => array(
  7436. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7437. 'id' => 'ContactImrequired'
  7438. ),
  7439. '/div'
  7440. );
  7441. $this->assertTags($result, $expected);
  7442. }
  7443. /**
  7444. * Test inputDefaults setter and getter
  7445. *
  7446. * @return void
  7447. */
  7448. public function testInputDefaults() {
  7449. $this->Form->create('Contact');
  7450. $this->Form->inputDefaults(array(
  7451. 'label' => false,
  7452. 'div' => array(
  7453. 'style' => 'color: #000;'
  7454. )
  7455. ));
  7456. $result = $this->Form->input('Contact.field1');
  7457. $expected = array(
  7458. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7459. 'input' => array(
  7460. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7461. 'id' => 'ContactField1'
  7462. ),
  7463. '/div'
  7464. );
  7465. $this->assertTags($result, $expected);
  7466. $this->Form->inputDefaults(array(
  7467. 'div' => false,
  7468. 'label' => 'Label',
  7469. ));
  7470. $result = $this->Form->input('Contact.field1');
  7471. $expected = array(
  7472. 'label' => array('for' => 'ContactField1'),
  7473. 'Label',
  7474. '/label',
  7475. 'input' => array(
  7476. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7477. 'id' => 'ContactField1'
  7478. ),
  7479. );
  7480. $this->assertTags($result, $expected);
  7481. $this->Form->inputDefaults(array(
  7482. 'label' => false,
  7483. ), true);
  7484. $result = $this->Form->input('Contact.field1');
  7485. $expected = array(
  7486. 'input' => array(
  7487. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7488. 'id' => 'ContactField1'
  7489. ),
  7490. );
  7491. $this->assertTags($result, $expected);
  7492. $result = $this->Form->inputDefaults();
  7493. $expected = array(
  7494. 'div' => false,
  7495. 'label' => false,
  7496. );
  7497. $this->assertEqual($result, $expected);
  7498. }
  7499. }