PageRenderTime 96ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 2ms

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

https://github.com/vgarcias/expediamedicus
PHP | 8168 lines | 6370 code | 645 blank | 1153 comment | 1 complexity | 1a36228bca03103773d14fafd6d2f307 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)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $expected .= ':' . 'Model.valid';
  710. $expected = array(
  711. 'div' => array('style' => 'display:none;'),
  712. array('input' => array(
  713. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  714. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  715. )),
  716. array('input' => array(
  717. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  718. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  719. )),
  720. '/div'
  721. );
  722. $this->assertTags($result, $expected);
  723. }
  724. /**
  725. * Tests correct generation of number fields for double and float fields
  726. *
  727. * @return void
  728. */
  729. public function testTextFieldGenerationForFloats() {
  730. $model = ClassRegistry::getObject('Contact');
  731. $model->setSchema(array('foo' => array(
  732. 'type' => 'float',
  733. 'null' => false,
  734. 'default' => null,
  735. 'length' => null
  736. )));
  737. $this->Form->create('Contact');
  738. $result = $this->Form->input('foo');
  739. $expected = array(
  740. 'div' => array('class' => 'input number'),
  741. 'label' => array('for' => 'ContactFoo'),
  742. 'Foo',
  743. '/label',
  744. array('input' => array(
  745. 'type' => 'number',
  746. 'name' => 'data[Contact][foo]',
  747. 'id' => 'ContactFoo',
  748. 'step' => 'any'
  749. )),
  750. '/div'
  751. );
  752. $this->assertTags($result, $expected);
  753. $result = $this->Form->input('foo', array('step' => 0.5));
  754. $expected = array(
  755. 'div' => array('class' => 'input number'),
  756. 'label' => array('for' => 'ContactFoo'),
  757. 'Foo',
  758. '/label',
  759. array('input' => array(
  760. 'type' => 'number',
  761. 'name' => 'data[Contact][foo]',
  762. 'id' => 'ContactFoo',
  763. 'step' => '0.5'
  764. )),
  765. '/div'
  766. );
  767. $this->assertTags($result, $expected);
  768. }
  769. /**
  770. * Tests correct generation of number fields for integer fields
  771. *
  772. * @access public
  773. * @return void
  774. */
  775. public function testTextFieldTypeNumberGenerationForIntegers() {
  776. $model = ClassRegistry::getObject('Contact');
  777. $model->setSchema(array('foo' => array(
  778. 'type' => 'integer',
  779. 'null' => false,
  780. 'default' => null,
  781. 'length' => null
  782. )));
  783. $this->Form->create('Contact');
  784. $result = $this->Form->input('foo');
  785. $expected = array(
  786. 'div' => array('class' => 'input number'),
  787. 'label' => array('for' => 'ContactFoo'),
  788. 'Foo',
  789. '/label',
  790. array('input' => array(
  791. 'type' => 'number', 'name' => 'data[Contact][foo]',
  792. 'id' => 'ContactFoo'
  793. )),
  794. '/div'
  795. );
  796. $this->assertTags($result, $expected);
  797. }
  798. /**
  799. * testFormSecurityMultipleFields method
  800. *
  801. * Test secure() with multiple row form. Ensure hash is correct.
  802. *
  803. * @return void
  804. */
  805. public function testFormSecurityMultipleFields() {
  806. $key = 'testKey';
  807. $fields = array(
  808. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  809. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  810. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  811. );
  812. $this->Form->request['_Token'] = array('key' => $key);
  813. $result = $this->Form->secure($fields);
  814. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  815. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  816. $expected = array(
  817. 'div' => array('style' => 'display:none;'),
  818. array('input' => array(
  819. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  820. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  821. )),
  822. array('input' => array(
  823. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  824. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  825. )),
  826. '/div'
  827. );
  828. $this->assertTags($result, $expected);
  829. }
  830. /**
  831. * testFormSecurityMultipleSubmitButtons
  832. *
  833. * test form submit generation and ensure that _Token is only created on end()
  834. *
  835. * @return void
  836. */
  837. public function testFormSecurityMultipleSubmitButtons() {
  838. $key = 'testKey';
  839. $this->Form->request['_Token'] = array('key' => $key);
  840. $this->Form->create('Addresses');
  841. $this->Form->input('Address.title');
  842. $this->Form->input('Address.first_name');
  843. $result = $this->Form->submit('Save', array('name' => 'save'));
  844. $expected = array(
  845. 'div' => array('class' => 'submit'),
  846. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  847. '/div',
  848. );
  849. $this->assertTags($result, $expected);
  850. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  851. $expected = array(
  852. 'div' => array('class' => 'submit'),
  853. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  854. '/div',
  855. );
  856. $this->assertTags($result, $expected);
  857. $result = $this->Form->end(null);
  858. $expected = array(
  859. 'div' => array('style' => 'display:none;'),
  860. array('input' => array(
  861. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  862. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  863. )),
  864. array('input' => array(
  865. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  866. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  867. )),
  868. '/div'
  869. );
  870. $this->assertTags($result, $expected);
  871. }
  872. /**
  873. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  874. *
  875. * @return void
  876. */
  877. public function testSecurityButtonNestedNamed() {
  878. $key = 'testKey';
  879. $this->Form->request['_Token'] = array('key' => $key);
  880. $this->Form->create('Addresses');
  881. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  882. $result = $this->Form->unlockField();
  883. $this->assertEquals(array('Address.button'), $result);
  884. }
  885. /**
  886. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  887. *
  888. * @return void
  889. */
  890. public function testSecuritySubmitNestedNamed() {
  891. $key = 'testKey';
  892. $this->Form->request['_Token'] = array('key' => $key);
  893. $this->Form->create('Addresses');
  894. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  895. $result = $this->Form->unlockField();
  896. $this->assertEquals(array('Address.button'), $result);
  897. }
  898. /**
  899. * Test that the correct fields are unlocked for image submits with no names.
  900. *
  901. * @return void
  902. */
  903. public function testSecuritySubmitImageNoName() {
  904. $key = 'testKey';
  905. $this->Form->request['_Token'] = array('key' => $key);
  906. $this->Form->create('User');
  907. $result = $this->Form->submit('save.png');
  908. $expected = array(
  909. 'div' => array('class' => 'submit'),
  910. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  911. '/div'
  912. );
  913. $this->assertTags($result, $expected);
  914. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  915. }
  916. /**
  917. * Test that the correct fields are unlocked for image submits with names.
  918. *
  919. * @return void
  920. */
  921. public function testSecuritySubmitImageName() {
  922. $key = 'testKey';
  923. $this->Form->request['_Token'] = array('key' => $key);
  924. $this->Form->create('User');
  925. $result = $this->Form->submit('save.png', array('name' => 'test'));
  926. $expected = array(
  927. 'div' => array('class' => 'submit'),
  928. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  929. '/div'
  930. );
  931. $this->assertTags($result, $expected);
  932. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  933. }
  934. /**
  935. * testFormSecurityMultipleInputFields method
  936. *
  937. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  938. *
  939. * @return void
  940. */
  941. public function testFormSecurityMultipleInputFields() {
  942. $key = 'testKey';
  943. $this->Form->request['_Token'] = array('key' => $key);
  944. $this->Form->create('Addresses');
  945. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  946. $this->Form->input('Addresses.0.title');
  947. $this->Form->input('Addresses.0.first_name');
  948. $this->Form->input('Addresses.0.last_name');
  949. $this->Form->input('Addresses.0.address');
  950. $this->Form->input('Addresses.0.city');
  951. $this->Form->input('Addresses.0.phone');
  952. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  953. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  954. $this->Form->input('Addresses.1.title');
  955. $this->Form->input('Addresses.1.first_name');
  956. $this->Form->input('Addresses.1.last_name');
  957. $this->Form->input('Addresses.1.address');
  958. $this->Form->input('Addresses.1.city');
  959. $this->Form->input('Addresses.1.phone');
  960. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  961. $result = $this->Form->secure($this->Form->fields);
  962. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  963. $expected = array(
  964. 'div' => array('style' => 'display:none;'),
  965. array('input' => array(
  966. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  967. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  968. )),
  969. array('input' => array(
  970. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  971. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  972. )),
  973. '/div'
  974. );
  975. $this->assertTags($result, $expected);
  976. }
  977. /**
  978. * Test form security with Model.field.0 style inputs
  979. *
  980. * @return void
  981. */
  982. public function testFormSecurityArrayFields() {
  983. $key = 'testKey';
  984. $this->Form->request->params['_Token']['key'] = $key;
  985. $this->Form->create('Address');
  986. $this->Form->input('Address.primary.1');
  987. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  988. $this->Form->input('Address.secondary.1.0');
  989. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  990. }
  991. /**
  992. * testFormSecurityMultipleInputDisabledFields method
  993. *
  994. * test secure form generation with multiple records and disabled fields.
  995. *
  996. * @return void
  997. */
  998. public function testFormSecurityMultipleInputDisabledFields() {
  999. $key = 'testKey';
  1000. $this->Form->request->params['_Token'] = array(
  1001. 'key' => $key,
  1002. 'unlockedFields' => array('first_name', 'address')
  1003. );
  1004. $this->Form->create();
  1005. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1006. $this->Form->input('Addresses.0.title');
  1007. $this->Form->input('Addresses.0.first_name');
  1008. $this->Form->input('Addresses.0.last_name');
  1009. $this->Form->input('Addresses.0.address');
  1010. $this->Form->input('Addresses.0.city');
  1011. $this->Form->input('Addresses.0.phone');
  1012. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1013. $this->Form->input('Addresses.1.title');
  1014. $this->Form->input('Addresses.1.first_name');
  1015. $this->Form->input('Addresses.1.last_name');
  1016. $this->Form->input('Addresses.1.address');
  1017. $this->Form->input('Addresses.1.city');
  1018. $this->Form->input('Addresses.1.phone');
  1019. $result = $this->Form->secure($this->Form->fields);
  1020. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1021. $expected = array(
  1022. 'div' => array('style' => 'display:none;'),
  1023. array('input' => array(
  1024. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1025. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1026. )),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1029. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. }
  1035. /**
  1036. * testFormSecurityInputDisabledFields method
  1037. *
  1038. * Test single record form with disabled fields.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testFormSecurityInputUnlockedFields() {
  1043. $key = 'testKey';
  1044. $this->Form->request['_Token'] = array(
  1045. 'key' => $key,
  1046. 'unlockedFields' => array('first_name', 'address')
  1047. );
  1048. $this->Form->create();
  1049. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1050. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1051. $this->Form->input('Addresses.title');
  1052. $this->Form->input('Addresses.first_name');
  1053. $this->Form->input('Addresses.last_name');
  1054. $this->Form->input('Addresses.address');
  1055. $this->Form->input('Addresses.city');
  1056. $this->Form->input('Addresses.phone');
  1057. $result = $this->Form->fields;
  1058. $expected = array(
  1059. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1060. 'Addresses.city', 'Addresses.phone'
  1061. );
  1062. $this->assertEquals($expected, $result);
  1063. $result = $this->Form->secure($expected);
  1064. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1065. $expected = array(
  1066. 'div' => array('style' => 'display:none;'),
  1067. array('input' => array(
  1068. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1069. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1070. )),
  1071. array('input' => array(
  1072. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1073. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1074. )),
  1075. '/div'
  1076. );
  1077. $this->assertTags($result, $expected);
  1078. }
  1079. /**
  1080. * test securing inputs with custom name attributes.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testFormSecureWithCustomNameAttribute() {
  1085. $this->Form->request->params['_Token']['key'] = 'testKey';
  1086. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1087. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1088. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1089. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1090. }
  1091. /**
  1092. * testFormSecuredInput method
  1093. *
  1094. * Test generation of entire secure form, assertions made on input() output.
  1095. *
  1096. * @return void
  1097. */
  1098. public function testFormSecuredInput() {
  1099. $this->Form->request['_Token'] = array('key' => 'testKey');
  1100. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1101. $encoding = strtolower(Configure::read('App.encoding'));
  1102. $expected = array(
  1103. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1104. 'div' => array('style' => 'display:none;'),
  1105. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1106. array('input' => array(
  1107. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1108. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1109. )),
  1110. '/div'
  1111. );
  1112. $this->assertTags($result, $expected);
  1113. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1114. $expected = array(
  1115. 'div' => array('class' => 'input text'),
  1116. 'label' => array('for' => 'UserFormPublished'),
  1117. 'Published',
  1118. '/label',
  1119. array('input' => array(
  1120. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1121. 'id' => 'UserFormPublished'
  1122. )),
  1123. '/div'
  1124. );
  1125. $this->assertTags($result, $expected);
  1126. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1127. $expected = array(
  1128. 'div' => array('class' => 'input text'),
  1129. 'label' => array('for' => 'UserFormOther'),
  1130. 'Other',
  1131. '/label',
  1132. array('input' => array(
  1133. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1134. 'id' => 'UserFormOther'
  1135. )),
  1136. '/div'
  1137. );
  1138. $this->assertTags($result, $expected);
  1139. $result = $this->Form->hidden('UserForm.stuff');
  1140. $expected = array('input' => array(
  1141. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1142. 'id' => 'UserFormStuff'
  1143. ));
  1144. $this->assertTags($result, $expected);
  1145. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1146. $expected = array('input' => array(
  1147. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1148. 'value' => '0', 'id' => 'UserFormHidden'
  1149. ));
  1150. $this->assertTags($result, $expected);
  1151. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1152. $expected = array(
  1153. 'div' => array('class' => 'input checkbox'),
  1154. array('input' => array(
  1155. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1156. 'value' => '0', 'id' => 'UserFormSomething_'
  1157. )),
  1158. array('input' => array(
  1159. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1160. 'value' => '1', 'id' => 'UserFormSomething'
  1161. )),
  1162. 'label' => array('for' => 'UserFormSomething'),
  1163. 'Something',
  1164. '/label',
  1165. '/div'
  1166. );
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->fields;
  1169. $expected = array(
  1170. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1171. 'UserForm.hidden' => '0', 'UserForm.something'
  1172. );
  1173. $this->assertEquals($expected, $result);
  1174. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1175. $result = $this->Form->secure($this->Form->fields);
  1176. $expected = array(
  1177. 'div' => array('style' => 'display:none;'),
  1178. array('input' => array(
  1179. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1180. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1181. )),
  1182. array('input' => array(
  1183. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1184. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1185. )),
  1186. '/div'
  1187. );
  1188. $this->assertTags($result, $expected);
  1189. }
  1190. /**
  1191. * Tests that the correct keys are added to the field hash index
  1192. *
  1193. * @return void
  1194. */
  1195. public function testFormSecuredFileInput() {
  1196. $this->Form->request['_Token'] = array('key' => 'testKey');
  1197. $this->assertEquals(array(), $this->Form->fields);
  1198. $result = $this->Form->file('Attachment.file');
  1199. $expected = array(
  1200. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1201. 'Attachment.file.error', 'Attachment.file.size'
  1202. );
  1203. $this->assertEquals($expected, $this->Form->fields);
  1204. }
  1205. /**
  1206. * test that multiple selects keys are added to field hash
  1207. *
  1208. * @return void
  1209. */
  1210. public function testFormSecuredMultipleSelect() {
  1211. $this->Form->request['_Token'] = array('key' => 'testKey');
  1212. $this->assertEquals(array(), $this->Form->fields);
  1213. $options = array('1' => 'one', '2' => 'two');
  1214. $this->Form->select('Model.select', $options);
  1215. $expected = array('Model.select');
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. $this->Form->fields = array();
  1218. $this->Form->select('Model.select', $options, array('multiple' => true));
  1219. $this->assertEquals($expected, $this->Form->fields);
  1220. }
  1221. /**
  1222. * testFormSecuredRadio method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testFormSecuredRadio() {
  1227. $this->Form->request['_Token'] = array('key' => 'testKey');
  1228. $this->assertEquals(array(), $this->Form->fields);
  1229. $options = array('1' => 'option1', '2' => 'option2');
  1230. $this->Form->radio('Test.test', $options);
  1231. $expected = array('Test.test');
  1232. $this->assertEquals($expected, $this->Form->fields);
  1233. }
  1234. /**
  1235. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1236. * hashing.
  1237. *
  1238. * @return void
  1239. */
  1240. public function testFormSecuredAndDisabled() {
  1241. $this->Form->request['_Token'] = array('key' => 'testKey');
  1242. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1243. $this->Form->text('Model.text', array('disabled' => true));
  1244. $this->Form->password('Model.text', array('disabled' => true));
  1245. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1246. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1247. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1248. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1249. $this->Form->month('Model.month', array('disabled' => true));
  1250. $this->Form->day('Model.day', array('disabled' => true));
  1251. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1252. $this->Form->minute('Model.minute', array('disabled' => true));
  1253. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1254. $expected = array(
  1255. 'Model.radio' => ''
  1256. );
  1257. $this->assertEquals($expected, $this->Form->fields);
  1258. }
  1259. /**
  1260. * testDisableSecurityUsingForm method
  1261. *
  1262. * @return void
  1263. */
  1264. public function testDisableSecurityUsingForm() {
  1265. $this->Form->request['_Token'] = array(
  1266. 'key' => 'testKey',
  1267. 'disabledFields' => array()
  1268. );
  1269. $this->Form->create();
  1270. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1271. $this->Form->input('Addresses.title');
  1272. $this->Form->input('Addresses.first_name', array('secure' => false));
  1273. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1274. $this->Form->input('Addresses.zip', array(
  1275. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1276. ));
  1277. $result = $this->Form->fields;
  1278. $expected = array(
  1279. 'Addresses.id' => '123456', 'Addresses.title',
  1280. );
  1281. $this->assertEquals($expected, $result);
  1282. }
  1283. /**
  1284. * test disableField
  1285. *
  1286. * @return void
  1287. */
  1288. public function testUnlockFieldAddsToList() {
  1289. $this->Form->request['_Token'] = array(
  1290. 'key' => 'testKey',
  1291. 'unlockedFields' => array()
  1292. );
  1293. $this->Form->create('Contact');
  1294. $this->Form->unlockField('Contact.name');
  1295. $this->Form->text('Contact.name');
  1296. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1297. $this->assertEquals(array(), $this->Form->fields);
  1298. }
  1299. /**
  1300. * test unlockField removing from fields array.
  1301. *
  1302. * @return void
  1303. */
  1304. public function testUnlockFieldRemovingFromFields() {
  1305. $this->Form->request['_Token'] = array(
  1306. 'key' => 'testKey',
  1307. 'unlockedFields' => array()
  1308. );
  1309. $this->Form->create('Contact');
  1310. $this->Form->hidden('Contact.id', array('value' => 1));
  1311. $this->Form->text('Contact.name');
  1312. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1313. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1314. $this->Form->unlockField('Contact.name');
  1315. $this->Form->unlockField('Contact.id');
  1316. $this->assertEquals(array(), $this->Form->fields);
  1317. }
  1318. /**
  1319. * testTagIsInvalid method
  1320. *
  1321. * @return void
  1322. */
  1323. public function testTagIsInvalid() {
  1324. $Contact = ClassRegistry::getObject('Contact');
  1325. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1326. $this->Form->setEntity('Contact.0.email');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = array('Please provide an email');
  1329. $this->assertEquals($expected, $result);
  1330. $this->Form->setEntity('Contact.1.email');
  1331. $result = $this->Form->tagIsInvalid();
  1332. $expected = false;
  1333. $this->assertSame($expected, $result);
  1334. $this->Form->setEntity('Contact.0.name');
  1335. $result = $this->Form->tagIsInvalid();
  1336. $expected = false;
  1337. $this->assertSame($expected, $result);
  1338. }
  1339. /**
  1340. * testPasswordValidation method
  1341. *
  1342. * test validation errors on password input.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testPasswordValidation() {
  1347. $Contact = ClassRegistry::getObject('Contact');
  1348. $Contact->validationErrors['password'] = array('Please provide a password');
  1349. $result = $this->Form->input('Contact.password');
  1350. $expected = array(
  1351. 'div' => array('class' => 'input password error'),
  1352. 'label' => array('for' => 'ContactPassword'),
  1353. 'Password',
  1354. '/label',
  1355. 'input' => array(
  1356. 'type' => 'password', 'name' => 'data[Contact][password]',
  1357. 'id' => 'ContactPassword', 'class' => 'form-error'
  1358. ),
  1359. array('div' => array('class' => 'error-message')),
  1360. 'Please provide a password',
  1361. '/div',
  1362. '/div'
  1363. );
  1364. $this->assertTags($result, $expected);
  1365. }
  1366. /**
  1367. * testEmptyErrorValidation method
  1368. *
  1369. * test validation error div when validation message is an empty string
  1370. *
  1371. * @access public
  1372. * @return void
  1373. */
  1374. public function testEmptyErrorValidation() {
  1375. $this->Form->validationErrors['Contact']['password'] = '';
  1376. $result = $this->Form->input('Contact.password');
  1377. $expected = array(
  1378. 'div' => array('class' => 'input password error'),
  1379. 'label' => array('for' => 'ContactPassword'),
  1380. 'Password',
  1381. '/label',
  1382. 'input' => array(
  1383. 'type' => 'password', 'name' => 'data[Contact][password]',
  1384. 'id' => 'ContactPassword', 'class' => 'form-error'
  1385. ),
  1386. array('div' => array('class' => 'error-message')),
  1387. array(),
  1388. '/div',
  1389. '/div'
  1390. );
  1391. $this->assertTags($result, $expected);
  1392. }
  1393. /**
  1394. * testEmptyInputErrorValidation method
  1395. *
  1396. * test validation error div when validation message is overridden by an empty string when calling input()
  1397. *
  1398. * @access public
  1399. * @return void
  1400. */
  1401. public function testEmptyInputErrorValidation() {
  1402. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1403. $result = $this->Form->input('Contact.password', array('error' => ''));
  1404. $expected = array(
  1405. 'div' => array('class' => 'input password error'),
  1406. 'label' => array('for' => 'ContactPassword'),
  1407. 'Password',
  1408. '/label',
  1409. 'input' => array(
  1410. 'type' => 'password', 'name' => 'data[Contact][password]',
  1411. 'id' => 'ContactPassword', 'class' => 'form-error'
  1412. ),
  1413. array('div' => array('class' => 'error-message')),
  1414. array(),
  1415. '/div',
  1416. '/div'
  1417. );
  1418. $this->assertTags($result, $expected);
  1419. }
  1420. /**
  1421. * testFormValidationAssociated method
  1422. *
  1423. * test display of form errors in conjunction with model::validates.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFormValidationAssociated() {
  1428. $this->UserForm = ClassRegistry::getObject('UserForm');
  1429. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1430. $data = array(
  1431. 'UserForm' => array('name' => 'user'),
  1432. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1433. );
  1434. $result = $this->UserForm->OpenidUrl->create($data);
  1435. $this->assertFalse(empty($result));
  1436. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1437. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1438. $encoding = strtolower(Configure::read('App.encoding'));
  1439. $expected = array(
  1440. 'form' => array(
  1441. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1442. 'accept-charset' => $encoding
  1443. ),
  1444. 'div' => array('style' => 'display:none;'),
  1445. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1446. '/div'
  1447. );
  1448. $this->assertTags($result, $expected);
  1449. $result = $this->Form->error(
  1450. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1451. );
  1452. $this->assertEquals('Error, not registered', $result);
  1453. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1454. }
  1455. /**
  1456. * testFormValidationAssociatedFirstLevel method
  1457. *
  1458. * test form error display with associated model.
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormValidationAssociatedFirstLevel() {
  1463. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1464. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1465. $data = array(
  1466. 'ValidateUser' => array('name' => 'mariano'),
  1467. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1468. );
  1469. $result = $this->ValidateUser->create($data);
  1470. $this->assertFalse(empty($result));
  1471. $this->assertFalse($this->ValidateUser->validates());
  1472. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1473. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1474. $encoding = strtolower(Configure::read('App.encoding'));
  1475. $expected = array(
  1476. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1477. 'div' => array('style' => 'display:none;'),
  1478. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1479. '/div'
  1480. );
  1481. $this->assertTags($result, $expected);
  1482. $result = $this->Form->error(
  1483. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1484. );
  1485. $this->assertEquals('Invalid email', $result);
  1486. $result = $this->Form->error(
  1487. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1488. );
  1489. $this->assertEquals('Invalid name', $result);
  1490. $result = $this->Form->error(
  1491. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1492. );
  1493. $this->assertEquals('Invalid city', $result);
  1494. unset($this->ValidateUser->ValidateProfile);
  1495. unset($this->ValidateUser);
  1496. }
  1497. /**
  1498. * testFormValidationAssociatedSecondLevel method
  1499. *
  1500. * test form error display with associated model.
  1501. *
  1502. * @return void
  1503. */
  1504. public function testFormValidationAssociatedSecondLevel() {
  1505. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1506. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1507. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1508. $data = array(
  1509. 'ValidateUser' => array('name' => 'mariano'),
  1510. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1511. 'ValidateItem' => array('name' => 'Item')
  1512. );
  1513. $result = $this->ValidateUser->create($data);
  1514. $this->assertFalse(empty($result));
  1515. $this->assertFalse($this->ValidateUser->validates());
  1516. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1517. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1518. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1519. $encoding = strtolower(Configure::read('App.encoding'));
  1520. $expected = array(
  1521. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1522. 'div' => array('style' => 'display:none;'),
  1523. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1524. '/div'
  1525. );
  1526. $this->assertTags($result, $expected);
  1527. $result = $this->Form->error(
  1528. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1529. );
  1530. $this->assertEquals('Invalid email', $result);
  1531. $result = $this->Form->error(
  1532. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1533. );
  1534. $this->assertEquals('Invalid name', $result);
  1535. $result = $this->Form->error(
  1536. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1537. );
  1538. $result = $this->Form->error(
  1539. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1540. );
  1541. $this->assertEquals('Invalid description', $result);
  1542. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1543. unset($this->ValidateUser->ValidateProfile);
  1544. unset($this->ValidateUser);
  1545. }
  1546. /**
  1547. * testFormValidationMultiRecord method
  1548. *
  1549. * test form error display with multiple records.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormValidationMultiRecord() {
  1554. $Contact = ClassRegistry::getObject('Contact');
  1555. $Contact->validationErrors[2] = array(
  1556. 'name' => array('This field cannot be left blank')
  1557. );
  1558. $result = $this->Form->input('Contact.2.name');
  1559. $expected = array(
  1560. 'div' => array('class' => 'input text error'),
  1561. 'label' => array('for' => 'Contact2Name'),
  1562. 'Name',
  1563. '/label',
  1564. 'input' => array(
  1565. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1566. 'class' => 'form-error', 'maxlength' => 255
  1567. ),
  1568. array('div' => array('class' => 'error-message')),
  1569. 'This field cannot be left blank',
  1570. '/div',
  1571. '/div'
  1572. );
  1573. $this->assertTags($result, $expected);
  1574. }
  1575. /**
  1576. * testMultipleInputValidation method
  1577. *
  1578. * test multiple record form validation error display.
  1579. *
  1580. * @return void
  1581. */
  1582. public function testMultipleInputValidation() {
  1583. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1584. $Address->validationErrors[0] = array(
  1585. 'title' => array('This field cannot be empty'),
  1586. 'first_name' => array('This field cannot be empty')
  1587. );
  1588. $Address->validationErrors[1] = array(
  1589. 'last_name' => array('You must have a last name')
  1590. );
  1591. $this->Form->create();
  1592. $result = $this->Form->input('Address.0.title');
  1593. $expected = array(
  1594. 'div' => array('class'),
  1595. 'label' => array('for'),
  1596. 'preg:/[^<]+/',
  1597. '/label',
  1598. 'input' => array(
  1599. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1600. ),
  1601. array('div' => array('class' => 'error-message')),
  1602. 'This field cannot be empty',
  1603. '/div',
  1604. '/div'
  1605. );
  1606. $this->assertTags($result, $expected);
  1607. $result = $this->Form->input('Address.0.first_name');
  1608. $expected = array(
  1609. 'div' => array('class'),
  1610. 'label' => array('for'),
  1611. 'preg:/[^<]+/',
  1612. '/label',
  1613. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1614. array('div' => array('class' => 'error-message')),
  1615. 'This field cannot be empty',
  1616. '/div',
  1617. '/div'
  1618. );
  1619. $this->assertTags($result, $expected);
  1620. $result = $this->Form->input('Address.0.last_name');
  1621. $expected = array(
  1622. 'div' => array('class'),
  1623. 'label' => array('for'),
  1624. 'preg:/[^<]+/',
  1625. '/label',
  1626. 'input' => array('type' => 'text', 'name', 'id'),
  1627. '/div'
  1628. );
  1629. $this->assertTags($result, $expected);
  1630. $result = $this->Form->input('Address.1.last_name');
  1631. $expected = array(
  1632. 'div' => array('class'),
  1633. 'label' => array('for'),
  1634. 'preg:/[^<]+/',
  1635. '/label',
  1636. 'input' => array(
  1637. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1638. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1639. ),
  1640. array('div' => array('class' => 'error-message')),
  1641. 'You must have a last name',
  1642. '/div',
  1643. '/div'
  1644. );
  1645. $this->assertTags($result, $expected);
  1646. }
  1647. /**
  1648. * testInput method
  1649. *
  1650. * Test various incarnations of input().
  1651. *
  1652. * @return void
  1653. */
  1654. public function testInput() {
  1655. $result = $this->Form->input('ValidateUser.balance');
  1656. $expected = array(
  1657. 'div' => array('class'),
  1658. 'label' => array('for'),
  1659. 'Balance',
  1660. '/label',
  1661. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1662. '/div',
  1663. );
  1664. $this->assertTags($result, $expected);
  1665. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1666. $expected = array(
  1667. 'div' => array('class' => 'input text'),
  1668. 'label' => array('for' => 'custom'),
  1669. 'Email',
  1670. '/label',
  1671. array('input' => array(
  1672. 'type' => 'text', 'name' => 'data[Contact][email]',
  1673. 'id' => 'custom', 'maxlength' => 255
  1674. )),
  1675. '/div'
  1676. );
  1677. $this->assertTags($result, $expected);
  1678. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1679. $expected = array(
  1680. '<div',
  1681. 'label' => array('for' => 'ContactEmail'),
  1682. 'Email',
  1683. '/label',
  1684. array('input' => array(
  1685. 'type' => 'text', 'name' => 'data[Contact][email]',
  1686. 'id' => 'ContactEmail', 'maxlength' => 255
  1687. )),
  1688. '/div'
  1689. );
  1690. $this->assertTags($result, $expected);
  1691. $result = $this->Form->hidden('Contact.idontexist');
  1692. $expected = array('input' => array(
  1693. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1694. 'id' => 'ContactIdontexist'
  1695. ));
  1696. $this->assertTags($result, $expected);
  1697. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1698. $expected = array(
  1699. 'div' => array('class' => 'input text'),
  1700. 'label' => array('for' => 'ContactEmail'),
  1701. 'Email',
  1702. '/label',
  1703. array('input' => array(
  1704. 'type' => 'text', 'name' => 'data[Contact][email]',
  1705. 'id' => 'ContactEmail'
  1706. )),
  1707. '/div'
  1708. );
  1709. $this->assertTags($result, $expected);
  1710. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1711. $expected = array(
  1712. 'div' => array('class' => 'input text'),
  1713. 'label' => array('for' => 'Contact5Email'),
  1714. 'Email',
  1715. '/label',
  1716. array('input' => array(
  1717. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1718. 'id' => 'Contact5Email'
  1719. )),
  1720. '/div'
  1721. );
  1722. $this->assertTags($result, $expected);
  1723. $result = $this->Form->input('Contact.password');
  1724. $expected = array(
  1725. 'div' => array('class' => 'input password'),
  1726. 'label' => array('for' => 'ContactPassword'),
  1727. 'Password',
  1728. '/label',
  1729. array('input' => array(
  1730. 'type' => 'password', 'name' => 'data[Contact][password]',
  1731. 'id' => 'ContactPassword'
  1732. )),
  1733. '/div'
  1734. );
  1735. $this->assertTags($result, $expected);
  1736. $result = $this->Form->input('Contact.email', array(
  1737. 'type' => 'file', 'class' => 'textbox'
  1738. ));
  1739. $expected = array(
  1740. 'div' => array('class' => 'input file'),
  1741. 'label' => array('for' => 'ContactEmail'),
  1742. 'Email',
  1743. '/label',
  1744. array('input' => array(
  1745. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1746. 'id' => 'ContactEmail'
  1747. )),
  1748. '/div'
  1749. );
  1750. $this->assertTags($result, $expected);
  1751. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1752. $result = $this->Form->input('Contact.phone');
  1753. $expected = array(
  1754. 'div' => array('class' => 'input text'),
  1755. 'label' => array('for' => 'ContactPhone'),
  1756. 'Phone',
  1757. '/label',
  1758. array('input' => array(
  1759. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1760. 'value' => 'Hello &amp; World &gt; weird chars',
  1761. 'id' => 'ContactPhone', 'maxlength' => 255
  1762. )),
  1763. '/div'
  1764. );
  1765. $this->assertTags($result, $expected);
  1766. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1767. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1768. $expected = array(
  1769. 'div' => array('class' => 'input text'),
  1770. 'label' => array('for' => 'myId'),
  1771. 'Field',
  1772. '/label',
  1773. 'input' => array(
  1774. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1775. 'value' => 'My value', 'id' => 'myId'
  1776. ),
  1777. '/div'
  1778. );
  1779. $this->assertTags($result, $expected);
  1780. unset($this->Form->request->data);
  1781. $Contact = ClassRegistry::getObject('Contact');
  1782. $Contact->validationErrors['field'] = array('Badness!');
  1783. $result = $this->Form->input('Contact.field');
  1784. $expected = array(
  1785. 'div' => array('class' => 'input text error'),
  1786. 'label' => array('for' => 'ContactField'),
  1787. 'Field',
  1788. '/label',
  1789. 'input' => array(
  1790. 'type' => 'text', 'name' => 'data[Contact][field]',
  1791. 'id' => 'ContactField', 'class' => 'form-error'
  1792. ),
  1793. array('div' => array('class' => 'error-message')),
  1794. 'Badness!',
  1795. '/div',
  1796. '/div'
  1797. );
  1798. $this->assertTags($result, $expected);
  1799. $result = $this->Form->input('Contact.field', array(
  1800. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1801. ));
  1802. $expected = array(
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'data[Contact][field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('span' => array('class' => 'error-message')),
  1811. 'Badness!',
  1812. '/span'
  1813. );
  1814. $this->assertTags($result, $expected);
  1815. $result = $this->Form->input('Contact.field', array(
  1816. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1817. ));
  1818. $expected = array(
  1819. 'div' => array('class' => 'input text error'),
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Contact][field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. array('div' => array('class' => 'error')),
  1828. 'Badness!',
  1829. '/div'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1834. ));
  1835. $expected = array(
  1836. 'span' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'data[Contact][field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. 'Badness!',
  1845. '/span'
  1846. );
  1847. $this->assertTags($result, $expected);
  1848. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1849. $expected = array(
  1850. 'div' => array('class' => 'input text error'),
  1851. 'label' => array('for' => 'ContactField'),
  1852. 'Field',
  1853. '/label',
  1854. 'input' => array(
  1855. 'type' => 'text', 'name' => 'data[Contact][field]',
  1856. 'id' => 'ContactField', 'class' => 'form-error'
  1857. ),
  1858. 'A message to you, Rudy',
  1859. array('div' => array('class' => 'error-message')),
  1860. 'Badness!',
  1861. '/div',
  1862. '/div'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $this->Form->setEntity(null);
  1866. $this->Form->setEntity('Contact.field');
  1867. $result = $this->Form->input('Contact.field', array(
  1868. 'after' => 'A message to you, Rudy', 'error' => false
  1869. ));
  1870. $expected = array(
  1871. 'div' => array('class' => 'input text'),
  1872. 'label' => array('for' => 'ContactField'),
  1873. 'Field',
  1874. '/label',
  1875. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1876. 'A message to you, Rudy',
  1877. '/div'
  1878. );
  1879. $this->assertTags($result, $expected);
  1880. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1881. $expected = array(
  1882. 'div' => array('class' => 'input text'),
  1883. 'label' => array('for' => 'ObjectField'),
  1884. 'Field',
  1885. '/label',
  1886. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1887. 'A message to you, Rudy',
  1888. '/div'
  1889. );
  1890. $this->assertTags($result, $expected);
  1891. $Contact->validationErrors['field'] = array('minLength');
  1892. $result = $this->Form->input('Contact.field', array(
  1893. 'error' => array(
  1894. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1895. 'maxLength' => 'login too large'
  1896. )
  1897. ));
  1898. $expected = array(
  1899. 'div' => array('class' => 'input text error'),
  1900. 'label' => array('for' => 'ContactField'),
  1901. 'Field',
  1902. '/label',
  1903. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1904. array('div' => array('class' => 'error-message')),
  1905. 'Le login doit contenir au moins 2 caractères',
  1906. '/div',
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. $Contact->validationErrors['field'] = array('maxLength');
  1911. $result = $this->Form->input('Contact.field', array(
  1912. 'error' => array(
  1913. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1914. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1915. 'maxLength' => 'login too large',
  1916. )
  1917. ));
  1918. $expected = array(
  1919. 'div' => array('class' => 'input text error'),
  1920. 'label' => array('for' => 'ContactField'),
  1921. 'Field',
  1922. '/label',
  1923. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1924. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1925. 'login too large',
  1926. '/span',
  1927. '/div'
  1928. );
  1929. $this->assertTags($result, $expected);
  1930. }
  1931. /**
  1932. * test input() with checkbox creation
  1933. *
  1934. * @return void
  1935. */
  1936. public function testInputCheckbox() {
  1937. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1938. $expected = array(
  1939. 'div' => array('class' => 'input checkbox'),
  1940. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1941. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1942. '/div'
  1943. );
  1944. $this->assertTags($result, $expected);
  1945. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1946. $expected = array(
  1947. 'div' => array('class' => 'input checkbox'),
  1948. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1949. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1950. '/div'
  1951. );
  1952. $this->assertTags($result, $expected);
  1953. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1954. $expected = array(
  1955. 'div' => array('class' => 'input checkbox'),
  1956. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1957. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1958. '/div'
  1959. );
  1960. $this->assertTags($result, $expected);
  1961. }
  1962. /**
  1963. * test form->input() with time types.
  1964. *
  1965. */
  1966. public function testInputTime() {
  1967. extract($this->dateRegex);
  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('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1973. $result = explode(':', $result);
  1974. $this->assertRegExp('/option value="23"/', $result[0]);
  1975. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1976. $result = $this->Form->input('Model.field', array(
  1977. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1978. ));
  1979. $result = explode(':', $result);
  1980. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1981. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1982. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1983. $result = $this->Form->input('Model.field', array(
  1984. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1985. ));
  1986. $result = explode(':', $result);
  1987. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1988. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1989. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1990. $result = $this->Form->input('prueba', array(
  1991. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1992. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1993. ));
  1994. $result = explode(':', $result);
  1995. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1996. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1997. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1998. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1999. $result = $this->Form->input('Random.start_time', array(
  2000. 'type' => 'time',
  2001. 'selected' => '18:15'
  2002. ));
  2003. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2004. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2005. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2006. }
  2007. /**
  2008. * Test interval + selected near the hour roll over.
  2009. *
  2010. * @return void
  2011. */
  2012. public function testTimeSelectedWithInterval() {
  2013. $result = $this->Form->input('Model.start_time', array(
  2014. 'type' => 'time',
  2015. 'interval' => 15,
  2016. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2017. ));
  2018. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2019. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2020. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2021. $result = $this->Form->input('Model.start_time', array(
  2022. 'type' => 'time',
  2023. 'interval' => 15,
  2024. 'selected' => '2012-10-23 15:57:00'
  2025. ));
  2026. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2027. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2028. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2029. $result = $this->Form->input('Model.start_time', array(
  2030. 'timeFormat' => 24,
  2031. 'type' => 'time',
  2032. 'interval' => 15,
  2033. 'selected' => '15:57'
  2034. ));
  2035. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2036. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2037. $result = $this->Form->input('Model.start_time', array(
  2038. 'timeFormat' => 24,
  2039. 'type' => 'time',
  2040. 'interval' => 15,
  2041. 'selected' => '23:57'
  2042. ));
  2043. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2044. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2045. $result = $this->Form->input('Model.created', array(
  2046. 'timeFormat' => 24,
  2047. 'type' => 'datetime',
  2048. 'interval' => 15,
  2049. 'selected' => '2012-09-30 23:56'
  2050. ));
  2051. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2052. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2053. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2054. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2055. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2056. }
  2057. /**
  2058. * test form->input() with datetime, date and time types
  2059. *
  2060. * @return void
  2061. */
  2062. public function testInputDatetime() {
  2063. extract($this->dateRegex);
  2064. $result = $this->Form->input('prueba', array(
  2065. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2066. 'maxYear' => date('Y') + 1, 'interval' => 15
  2067. ));
  2068. $result = explode(':', $result);
  2069. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2070. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2071. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2072. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2073. //related to ticket #5013
  2074. $result = $this->Form->input('Contact.date', array(
  2075. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2076. ));
  2077. $this->assertRegExp('/class="customClass"/', $result);
  2078. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2079. $result = $this->Form->input('Contact.date', array(
  2080. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2081. ));
  2082. $this->assertRegExp('/id="customIdDay"/', $result);
  2083. $this->assertRegExp('/id="customIdMonth"/', $result);
  2084. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2085. $result = $this->Form->input('Model.field', array(
  2086. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2087. ));
  2088. $this->assertRegExp('/id="customIDDay"/', $result);
  2089. $this->assertRegExp('/id="customIDHour"/', $result);
  2090. $result = explode('</select><select', $result);
  2091. $result = explode(':', $result[1]);
  2092. $this->assertRegExp('/option value="23"/', $result[0]);
  2093. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2094. $result = $this->Form->input('Model.field', array(
  2095. 'type' => 'datetime', 'timeFormat' => 12
  2096. ));
  2097. $result = explode('</select><select', $result);
  2098. $result = explode(':', $result[1]);
  2099. $this->assertRegExp('/option value="12"/', $result[0]);
  2100. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2101. $this->Form->request->data = array('Contact' => array('created' => null));
  2102. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2103. $expected = array(
  2104. 'div' => array('class' => 'input date'),
  2105. 'label' => array('for' => 'ContactCreatedMonth'),
  2106. 'Created',
  2107. '/label',
  2108. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2109. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2110. $monthsRegex,
  2111. '/select', '-',
  2112. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2113. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2114. $daysRegex,
  2115. '/select', '-',
  2116. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2117. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2118. $yearsRegex,
  2119. '/select',
  2120. '/div'
  2121. );
  2122. $this->assertTags($result, $expected);
  2123. $this->Form->request->data = array('Contact' => array('created' => null));
  2124. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2125. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2126. $this->Form->request->data = array('Contact' => array('created' => null));
  2127. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2128. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2129. $result = $this->Form->input('Contact.created', array(
  2130. 'type' => 'date',
  2131. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2132. 'timeFormat' => 'NONE'
  2133. ));
  2134. $this->assertRegExp('/for\="created-month"/', $result);
  2135. }
  2136. /**
  2137. * Test generating checkboxes in a loop.
  2138. *
  2139. * @return void
  2140. */
  2141. public function testInputCheckboxesInLoop() {
  2142. for ($i = 1; $i < 5; $i++) {
  2143. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2144. $expected = array(
  2145. 'div' => array('class' => 'input checkbox'),
  2146. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2147. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2148. 'label' => array('for' => "Contact{$i}Email"),
  2149. 'Email',
  2150. '/label',
  2151. '/div'
  2152. );
  2153. $this->assertTags($result, $expected);
  2154. }
  2155. }
  2156. /**
  2157. * test input name with leading integer, ensure attributes are generated correctly.
  2158. *
  2159. * @return void
  2160. */
  2161. public function testInputWithLeadingInteger() {
  2162. $result = $this->Form->text('0.Node.title');
  2163. $expected = array(
  2164. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2165. );
  2166. $this->assertTags($result, $expected);
  2167. }
  2168. /**
  2169. * test form->input() with select type inputs.
  2170. *
  2171. * @return void
  2172. */
  2173. public function testInputSelectType() {
  2174. $result = $this->Form->input('email', array(
  2175. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2176. );
  2177. $expected = array(
  2178. 'div' => array('class' => 'input select'),
  2179. 'label' => array('for' => 'email'),
  2180. 'Email',
  2181. '/label',
  2182. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2183. array('option' => array('value' => '')),
  2184. '/option',
  2185. array('option' => array('value' => 'è')),
  2186. 'Firést',
  2187. '/option',
  2188. array('option' => array('value' => 'é')),
  2189. 'Secoènd',
  2190. '/option',
  2191. '/select',
  2192. '/div'
  2193. );
  2194. $this->assertTags($result, $expected);
  2195. $result = $this->Form->input('email', array(
  2196. 'options' => array('First', 'Second'), 'empty' => true)
  2197. );
  2198. $expected = array(
  2199. 'div' => array('class' => 'input select'),
  2200. 'label' => array('for' => 'email'),
  2201. 'Email',
  2202. '/label',
  2203. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2204. array('option' => array('value' => '')),
  2205. '/option',
  2206. array('option' => array('value' => '0')),
  2207. 'First',
  2208. '/option',
  2209. array('option' => array('value' => '1')),
  2210. 'Second',
  2211. '/option',
  2212. '/select',
  2213. '/div'
  2214. );
  2215. $this->assertTags($result, $expected);
  2216. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2217. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2218. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2219. $expected = array(
  2220. 'div' => array('class' => 'input select'),
  2221. 'label' => array('for' => 'ModelUserId'),
  2222. 'User',
  2223. '/label',
  2224. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2225. array('option' => array('value' => '')),
  2226. '/option',
  2227. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2228. 'good',
  2229. '/option',
  2230. array('option' => array('value' => 'other')),
  2231. 'bad',
  2232. '/option',
  2233. '/select',
  2234. '/div'
  2235. );
  2236. $this->assertTags($result, $expected);
  2237. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2238. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2239. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2240. $expected = array(
  2241. 'div' => array('class' => 'input select'),
  2242. 'label' => array('for' => 'ThingUserId'),
  2243. 'User',
  2244. '/label',
  2245. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2246. array('option' => array('value' => '')),
  2247. 'Some Empty',
  2248. '/option',
  2249. array('option' => array('value' => 'value')),
  2250. 'good',
  2251. '/option',
  2252. array('option' => array('value' => 'other')),
  2253. 'bad',
  2254. '/option',
  2255. '/select',
  2256. '/div'
  2257. );
  2258. $this->assertTags($result, $expected);
  2259. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2260. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2261. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2262. $expected = array(
  2263. 'div' => array('class' => 'input select'),
  2264. 'label' => array('for' => 'ThingUserId'),
  2265. 'User',
  2266. '/label',
  2267. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2268. array('option' => array('value' => '')),
  2269. 'Some Empty',
  2270. '/option',
  2271. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2272. 'good',
  2273. '/option',
  2274. array('option' => array('value' => 'other')),
  2275. 'bad',
  2276. '/option',
  2277. '/select',
  2278. '/div'
  2279. );
  2280. $this->assertTags($result, $expected);
  2281. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2282. $this->Form->request->data = array('User' => array('User' => array('value')));
  2283. $result = $this->Form->input('User.User', array('empty' => true));
  2284. $expected = array(
  2285. 'div' => array('class' => 'input select'),
  2286. 'label' => array('for' => 'UserUser'),
  2287. 'User',
  2288. '/label',
  2289. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2290. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2291. array('option' => array('value' => '')),
  2292. '/option',
  2293. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2294. 'good',
  2295. '/option',
  2296. array('option' => array('value' => 'other')),
  2297. 'bad',
  2298. '/option',
  2299. '/select',
  2300. '/div'
  2301. );
  2302. $this->assertTags($result, $expected);
  2303. $this->Form->data = array();
  2304. $result = $this->Form->input('Publisher.id', array(
  2305. 'label' => 'Publisher',
  2306. 'type' => 'select',
  2307. 'multiple' => 'checkbox',
  2308. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2309. ));
  2310. $expected = array(
  2311. array('div' => array('class' => 'input select')),
  2312. array('label' => array('for' => 'PublisherId')),
  2313. 'Publisher',
  2314. '/label',
  2315. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2316. array('div' => array('class' => 'checkbox')),
  2317. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2318. array('label' => array('for' => 'PublisherIdValue1')),
  2319. 'Label 1',
  2320. '/label',
  2321. '/div',
  2322. array('div' => array('class' => 'checkbox')),
  2323. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2324. array('label' => array('for' => 'PublisherIdValue2')),
  2325. 'Label 2',
  2326. '/label',
  2327. '/div',
  2328. '/div'
  2329. );
  2330. $this->assertTags($result, $expected);
  2331. }
  2332. /**
  2333. * test that input() and a non standard primary key makes a hidden input by default.
  2334. *
  2335. * @return void
  2336. */
  2337. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2338. $this->Form->create('User');
  2339. $this->Form->fieldset = array(
  2340. 'User' => array(
  2341. 'fields' => array(
  2342. 'model_id' => array('type' => 'integer')
  2343. ),
  2344. 'validates' => array(),
  2345. 'key' => 'model_id'
  2346. )
  2347. );
  2348. $result = $this->Form->input('model_id');
  2349. $expected = array(
  2350. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2351. );
  2352. $this->assertTags($result, $expected);
  2353. }
  2354. /**
  2355. * test that overriding the magic select type widget is possible
  2356. *
  2357. * @return void
  2358. */
  2359. public function testInputOverridingMagicSelectType() {
  2360. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2361. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2362. $expected = array(
  2363. 'div' => array('class' => 'input text'),
  2364. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2365. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2366. '/div'
  2367. );
  2368. $this->assertTags($result, $expected);
  2369. //Check that magic types still work for plural/singular vars
  2370. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2371. $result = $this->Form->input('Model.type');
  2372. $expected = array(
  2373. 'div' => array('class' => 'input select'),
  2374. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2375. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2376. array('option' => array('value' => 'value')), 'good', '/option',
  2377. array('option' => array('value' => 'other')), 'bad', '/option',
  2378. '/select',
  2379. '/div'
  2380. );
  2381. $this->assertTags($result, $expected);
  2382. }
  2383. /**
  2384. * Test that magic input() selects are created for type=number
  2385. *
  2386. * @return void
  2387. */
  2388. public function testInputMagicSelectForTypeNumber() {
  2389. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2390. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2391. $result = $this->Form->input('ValidateUser.balance');
  2392. $expected = array(
  2393. 'div' => array('class' => 'input select'),
  2394. 'label' => array('for' => 'ValidateUserBalance'),
  2395. 'Balance',
  2396. '/label',
  2397. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2398. array('option' => array('value' => '0')),
  2399. 'nothing',
  2400. '/option',
  2401. array('option' => array('value' => '1', 'selected' => 'selected')),
  2402. 'some',
  2403. '/option',
  2404. array('option' => array('value' => '100')),
  2405. 'a lot',
  2406. '/option',
  2407. '/select',
  2408. '/div'
  2409. );
  2410. $this->assertTags($result, $expected);
  2411. }
  2412. /**
  2413. * Test that magic input() selects can easily be converted into radio types without error.
  2414. *
  2415. * @return void
  2416. */
  2417. public function testInputMagicSelectChangeToRadio() {
  2418. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2419. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2420. $this->assertRegExp('/input type="radio"/', $result);
  2421. }
  2422. /**
  2423. * fields with the same name as the model should work.
  2424. *
  2425. * @return void
  2426. */
  2427. public function testInputWithMatchingFieldAndModelName() {
  2428. $this->Form->create('User');
  2429. $this->Form->fieldset = array(
  2430. 'User' => array(
  2431. 'fields' => array(
  2432. 'User' => array('type' => 'text')
  2433. ),
  2434. 'validates' => array(),
  2435. 'key' => 'id'
  2436. )
  2437. );
  2438. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2439. $result = $this->Form->input('User', array('type' => 'text'));
  2440. $expected = array(
  2441. 'div' => array('class' => 'input text'),
  2442. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2443. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2444. '/div'
  2445. );
  2446. $this->assertTags($result, $expected);
  2447. }
  2448. /**
  2449. * testFormInputs method
  2450. *
  2451. * test correct results from form::inputs().
  2452. *
  2453. * @return void
  2454. */
  2455. public function testFormInputs() {
  2456. $this->Form->create('Contact');
  2457. $result = $this->Form->inputs('The Legend');
  2458. $expected = array(
  2459. '<fieldset',
  2460. '<legend',
  2461. 'The Legend',
  2462. '/legend',
  2463. '*/fieldset',
  2464. );
  2465. $this->assertTags($result, $expected);
  2466. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2467. $expected = array(
  2468. 'fieldset' => array('class' => 'classy-stuff'),
  2469. '<legend',
  2470. 'Field of Dreams',
  2471. '/legend',
  2472. '*/fieldset'
  2473. );
  2474. $this->assertTags($result, $expected);
  2475. $this->Form->create('Contact');
  2476. $this->Form->request['prefix'] = 'admin';
  2477. $this->Form->request['action'] = 'admin_edit';
  2478. $result = $this->Form->inputs();
  2479. $expected = array(
  2480. '<fieldset',
  2481. '<legend',
  2482. 'Edit Contact',
  2483. '/legend',
  2484. '*/fieldset',
  2485. );
  2486. $this->assertTags($result, $expected);
  2487. $this->Form->create('Contact');
  2488. $result = $this->Form->inputs(false);
  2489. $expected = array(
  2490. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2491. array('div' => array('class' => 'input text')),
  2492. '*/div',
  2493. array('div' => array('class' => 'input text')),
  2494. '*/div',
  2495. array('div' => array('class' => 'input text')),
  2496. '*/div',
  2497. array('div' => array('class' => 'input password')),
  2498. '*/div',
  2499. array('div' => array('class' => 'input date')),
  2500. '*/div',
  2501. array('div' => array('class' => 'input date')),
  2502. '*/div',
  2503. array('div' => array('class' => 'input datetime')),
  2504. '*/div',
  2505. array('div' => array('class' => 'input number')),
  2506. '*/div',
  2507. array('div' => array('class' => 'input select')),
  2508. '*/div',
  2509. );
  2510. $this->assertTags($result, $expected);
  2511. $this->Form->create('Contact');
  2512. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2513. $expected = array(
  2514. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2515. array('div' => array('class' => 'input text')),
  2516. '*/div',
  2517. array('div' => array('class' => 'input text')),
  2518. '*/div',
  2519. array('div' => array('class' => 'input text')),
  2520. '*/div',
  2521. array('div' => array('class' => 'input password')),
  2522. '*/div',
  2523. array('div' => array('class' => 'input date')),
  2524. '*/div',
  2525. array('div' => array('class' => 'input date')),
  2526. '*/div',
  2527. array('div' => array('class' => 'input datetime')),
  2528. '*/div',
  2529. array('div' => array('class' => 'input number')),
  2530. '*/div',
  2531. array('div' => array('class' => 'input select')),
  2532. '*/div',
  2533. );
  2534. $this->assertTags($result, $expected);
  2535. $this->Form->create('Contact');
  2536. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2537. $expected = array(
  2538. 'fieldset' => array(),
  2539. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2540. array('div' => array('class' => 'input text')),
  2541. '*/div',
  2542. array('div' => array('class' => 'input text')),
  2543. '*/div',
  2544. array('div' => array('class' => 'input text')),
  2545. '*/div',
  2546. array('div' => array('class' => 'input password')),
  2547. '*/div',
  2548. array('div' => array('class' => 'input date')),
  2549. '*/div',
  2550. array('div' => array('class' => 'input date')),
  2551. '*/div',
  2552. array('div' => array('class' => 'input datetime')),
  2553. '*/div',
  2554. array('div' => array('class' => 'input number')),
  2555. '*/div',
  2556. array('div' => array('class' => 'input select')),
  2557. '*/div',
  2558. '/fieldset'
  2559. );
  2560. $this->assertTags($result, $expected);
  2561. $this->Form->create('Contact');
  2562. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2563. $expected = array(
  2564. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2565. array('div' => array('class' => 'input text')),
  2566. '*/div',
  2567. array('div' => array('class' => 'input text')),
  2568. '*/div',
  2569. array('div' => array('class' => 'input text')),
  2570. '*/div',
  2571. array('div' => array('class' => 'input password')),
  2572. '*/div',
  2573. array('div' => array('class' => 'input date')),
  2574. '*/div',
  2575. array('div' => array('class' => 'input date')),
  2576. '*/div',
  2577. array('div' => array('class' => 'input datetime')),
  2578. '*/div',
  2579. array('div' => array('class' => 'input number')),
  2580. '*/div',
  2581. array('div' => array('class' => 'input select')),
  2582. '*/div',
  2583. );
  2584. $this->assertTags($result, $expected);
  2585. $this->Form->create('Contact');
  2586. $result = $this->Form->inputs('Hello');
  2587. $expected = array(
  2588. 'fieldset' => array(),
  2589. 'legend' => array(),
  2590. 'Hello',
  2591. '/legend',
  2592. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2593. array('div' => array('class' => 'input text')),
  2594. '*/div',
  2595. array('div' => array('class' => 'input text')),
  2596. '*/div',
  2597. array('div' => array('class' => 'input text')),
  2598. '*/div',
  2599. array('div' => array('class' => 'input password')),
  2600. '*/div',
  2601. array('div' => array('class' => 'input date')),
  2602. '*/div',
  2603. array('div' => array('class' => 'input date')),
  2604. '*/div',
  2605. array('div' => array('class' => 'input datetime')),
  2606. '*/div',
  2607. array('div' => array('class' => 'input number')),
  2608. '*/div',
  2609. array('div' => array('class' => 'input select')),
  2610. '*/div',
  2611. '/fieldset'
  2612. );
  2613. $this->assertTags($result, $expected);
  2614. $this->Form->create('Contact');
  2615. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2616. $expected = array(
  2617. 'fieldset' => array(),
  2618. 'legend' => array(),
  2619. 'Hello',
  2620. '/legend',
  2621. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2622. array('div' => array('class' => 'input text')),
  2623. '*/div',
  2624. array('div' => array('class' => 'input text')),
  2625. '*/div',
  2626. array('div' => array('class' => 'input text')),
  2627. '*/div',
  2628. array('div' => array('class' => 'input password')),
  2629. '*/div',
  2630. array('div' => array('class' => 'input date')),
  2631. '*/div',
  2632. array('div' => array('class' => 'input date')),
  2633. '*/div',
  2634. array('div' => array('class' => 'input datetime')),
  2635. '*/div',
  2636. array('div' => array('class' => 'input number')),
  2637. '*/div',
  2638. array('div' => array('class' => 'input select')),
  2639. '*/div',
  2640. '/fieldset'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. }
  2644. /**
  2645. * testSelectAsCheckbox method
  2646. *
  2647. * test multi-select widget with checkbox formatting.
  2648. *
  2649. * @return void
  2650. */
  2651. public function testSelectAsCheckbox() {
  2652. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2653. $expected = array(
  2654. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2655. array('div' => array('class' => 'checkbox')),
  2656. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2657. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2658. 'first',
  2659. '/label',
  2660. '/div',
  2661. array('div' => array('class' => 'checkbox')),
  2662. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2663. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2664. 'second',
  2665. '/label',
  2666. '/div',
  2667. array('div' => array('class' => 'checkbox')),
  2668. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2669. array('label' => array('for' => 'ModelMultiField2')),
  2670. 'third',
  2671. '/label',
  2672. '/div',
  2673. );
  2674. $this->assertTags($result, $expected);
  2675. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2676. $expected = array(
  2677. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2678. array('div' => array('class' => 'checkbox')),
  2679. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2680. array('label' => array('for' => 'ModelMultiField12')),
  2681. 'half',
  2682. '/label',
  2683. '/div',
  2684. );
  2685. $this->assertTags($result, $expected);
  2686. }
  2687. /**
  2688. * testLabel method
  2689. *
  2690. * test label generation.
  2691. *
  2692. * @return void
  2693. */
  2694. public function testLabel() {
  2695. $this->Form->text('Person.name');
  2696. $result = $this->Form->label();
  2697. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2698. $this->Form->text('Person.name');
  2699. $result = $this->Form->label();
  2700. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2701. $result = $this->Form->label('Person.first_name');
  2702. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2703. $result = $this->Form->label('Person.first_name', 'Your first name');
  2704. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2705. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2706. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2707. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2708. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2709. $result = $this->Form->label('Person.first_name', '');
  2710. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2711. $result = $this->Form->label('Person.2.name', '');
  2712. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2713. }
  2714. /**
  2715. * testTextbox method
  2716. *
  2717. * test textbox element generation
  2718. *
  2719. * @return void
  2720. */
  2721. public function testTextbox() {
  2722. $result = $this->Form->text('Model.field');
  2723. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2724. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2725. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2726. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2727. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2728. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2729. $result = $this->Form->text('Model.text');
  2730. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2731. $Contact = ClassRegistry::getObject('Contact');
  2732. $Contact->validationErrors['text'] = array(true);
  2733. $this->Form->request->data['Contact']['text'] = 'test';
  2734. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2735. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2736. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2737. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2738. $expected = array(
  2739. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2740. );
  2741. $this->assertTags($result, $expected);
  2742. }
  2743. /**
  2744. * testDefaultValue method
  2745. *
  2746. * Test default value setting
  2747. *
  2748. * @return void
  2749. */
  2750. public function testDefaultValue() {
  2751. $this->Form->request->data['Model']['field'] = 'test';
  2752. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2753. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2754. unset($this->Form->request->data['Model']['field']);
  2755. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2756. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2757. }
  2758. /**
  2759. * testCheckboxDefaultValue method
  2760. *
  2761. * Test default value setting on checkbox() method
  2762. *
  2763. * @return void
  2764. */
  2765. public function testCheckboxDefaultValue() {
  2766. $this->Form->request->data['Model']['field'] = false;
  2767. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2768. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2769. unset($this->Form->request->data['Model']['field']);
  2770. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2771. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2772. $this->Form->request->data['Model']['field'] = true;
  2773. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2774. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2775. unset($this->Form->request->data['Model']['field']);
  2776. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2777. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2778. }
  2779. /**
  2780. * testError method
  2781. *
  2782. * Test field error generation
  2783. *
  2784. * @return void
  2785. */
  2786. public function testError() {
  2787. $Contact = ClassRegistry::getObject('Contact');
  2788. $Contact->validationErrors['field'] = array(1);
  2789. $result = $this->Form->error('Contact.field');
  2790. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2791. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2792. $this->assertEquals('Error in field Field', $result);
  2793. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2794. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2795. $this->assertEquals('This field contains invalid input', $result);
  2796. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2797. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2798. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2799. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2800. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2801. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2802. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2803. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2804. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2805. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2806. $this->assertEquals('<strong>Badness!</strong>', $result);
  2807. $Contact->validationErrors['field'] = array("email");
  2808. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2809. $expected = array(
  2810. 'div' => array('class' => 'field-error'),
  2811. 'No good!',
  2812. '/div'
  2813. );
  2814. $this->assertTags($result, $expected);
  2815. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2816. $result = $this->Form->error('Contact.field', array(
  2817. 'notEmpty' => 'Cannot be empty',
  2818. 'email' => 'No good!'
  2819. ));
  2820. $expected = array(
  2821. 'div' => array('class' => 'error-message'),
  2822. 'ul' => array(),
  2823. '<li', 'Cannot be empty', '/li',
  2824. '<li', 'No good!', '/li',
  2825. '<li', 'Something else', '/li',
  2826. '/ul',
  2827. '/div'
  2828. );
  2829. $this->assertTags($result, $expected);
  2830. /** Testing error messages list options **/
  2831. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2832. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2833. $expected = array(
  2834. 'div' => array('class' => 'error-message'),
  2835. 'ol' => array(),
  2836. '<li', 'notEmpty', '/li',
  2837. '<li', 'email', '/li',
  2838. '/ol',
  2839. '/div'
  2840. );
  2841. $this->assertTags($result, $expected);
  2842. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2843. $expected = array(
  2844. 'div' => array('class' => 'error-message'),
  2845. 'ol' => array(),
  2846. '<li', 'notEmpty', '/li',
  2847. '<li', 'email', '/li',
  2848. '/ol',
  2849. '/div'
  2850. );
  2851. $this->assertTags($result, $expected);
  2852. $result = $this->Form->error('Contact.field', null, array(
  2853. 'listOptions' => array(
  2854. 'class' => 'ul-class',
  2855. 'itemOptions' => array(
  2856. 'class' => 'li-class'
  2857. )
  2858. )
  2859. ));
  2860. $expected = array(
  2861. 'div' => array('class' => 'error-message'),
  2862. 'ul' => array('class' => 'ul-class'),
  2863. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2864. array('li' => array('class' => 'li-class')), 'email', '/li',
  2865. '/ul',
  2866. '/div'
  2867. );
  2868. $this->assertTags($result, $expected);
  2869. }
  2870. /**
  2871. * test error options when using form->input();
  2872. *
  2873. * @return void
  2874. */
  2875. public function testInputErrorEscape() {
  2876. $this->Form->create('ValidateProfile');
  2877. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2878. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2879. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2880. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2881. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2882. $this->assertRegExp('/required<br>/', $result);
  2883. }
  2884. /**
  2885. * testPassword method
  2886. *
  2887. * Test password element generation
  2888. *
  2889. * @return void
  2890. */
  2891. public function testPassword() {
  2892. $Contact = ClassRegistry::getObject('Contact');
  2893. $result = $this->Form->password('Contact.field');
  2894. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2895. $Contact->validationErrors['passwd'] = 1;
  2896. $this->Form->request->data['Contact']['passwd'] = 'test';
  2897. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2898. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2899. }
  2900. /**
  2901. * testRadio method
  2902. *
  2903. * Test radio element set generation
  2904. *
  2905. * @return void
  2906. */
  2907. public function testRadio() {
  2908. $result = $this->Form->radio('Model.field', array('option A'));
  2909. $expected = array(
  2910. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2911. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2912. 'label' => array('for' => 'ModelField0'),
  2913. 'option A',
  2914. '/label'
  2915. );
  2916. $this->assertTags($result, $expected);
  2917. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2918. $expected = array(
  2919. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2920. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2921. 'label' => array('for' => 'ModelField12'),
  2922. 'half',
  2923. '/label'
  2924. );
  2925. $this->assertTags($result, $expected);
  2926. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2927. $expected = array(
  2928. 'fieldset' => array(),
  2929. 'legend' => array(),
  2930. 'Field',
  2931. '/legend',
  2932. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2933. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2934. array('label' => array('for' => 'ModelField0')),
  2935. 'option A',
  2936. '/label',
  2937. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2938. array('label' => array('for' => 'ModelField1')),
  2939. 'option B',
  2940. '/label',
  2941. '/fieldset'
  2942. );
  2943. $this->assertTags($result, $expected);
  2944. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2945. $expected = array(
  2946. 'fieldset' => array(),
  2947. 'legend' => array(),
  2948. 'Field',
  2949. '/legend',
  2950. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2951. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2952. array('label' => array('for' => 'ModelField0')),
  2953. 'option A',
  2954. '/label',
  2955. 'br' => array(),
  2956. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2957. array('label' => array('for' => 'ModelField1')),
  2958. 'option B',
  2959. '/label',
  2960. '/fieldset'
  2961. );
  2962. $this->assertTags($result, $expected);
  2963. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2964. $expected = array(
  2965. 'div' => array('class' => 'input radio'),
  2966. 'fieldset' => array(),
  2967. 'legend' => array(),
  2968. 'Legend title',
  2969. '/legend',
  2970. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2971. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2972. array('label' => array('for' => 'NewsletterSubscribe0')),
  2973. 'Unsubscribe',
  2974. '/label',
  2975. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2976. array('label' => array('for' => 'NewsletterSubscribe1')),
  2977. 'Subscribe',
  2978. '/label',
  2979. '/fieldset',
  2980. '/div'
  2981. );
  2982. $this->assertTags($result, $expected);
  2983. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2984. $expected = array(
  2985. 'div' => array('class' => 'input radio'),
  2986. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2987. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2988. array('label' => array('for' => 'NewsletterSubscribe0')),
  2989. 'Unsubscribe',
  2990. '/label',
  2991. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2992. array('label' => array('for' => 'NewsletterSubscribe1')),
  2993. 'Subscribe',
  2994. '/label',
  2995. '/div'
  2996. );
  2997. $this->assertTags($result, $expected);
  2998. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2999. $expected = array(
  3000. 'div' => array('class' => 'input radio'),
  3001. 'fieldset' => array(),
  3002. 'legend' => array(),
  3003. 'Legend title',
  3004. '/legend',
  3005. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3006. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3007. 'Unsubscribe',
  3008. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3009. 'Subscribe',
  3010. '/fieldset',
  3011. '/div'
  3012. );
  3013. $this->assertTags($result, $expected);
  3014. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3015. $expected = array(
  3016. 'div' => array('class' => 'input radio'),
  3017. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3018. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3019. 'Unsubscribe',
  3020. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3021. 'Subscribe',
  3022. '/div'
  3023. );
  3024. $this->assertTags($result, $expected);
  3025. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3026. $expected = array(
  3027. 'div' => array('class' => 'input radio'),
  3028. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3029. 'Unsubscribe',
  3030. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3031. 'Subscribe',
  3032. '/div'
  3033. );
  3034. $this->assertTags($result, $expected);
  3035. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3036. $expected = array(
  3037. 'fieldset' => array(),
  3038. 'legend' => array(),
  3039. 'Gender',
  3040. '/legend',
  3041. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3042. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3043. array('label' => array('for' => 'EmployeeGenderMale')),
  3044. 'Male',
  3045. '/label',
  3046. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3047. array('label' => array('for' => 'EmployeeGenderFemale')),
  3048. 'Female',
  3049. '/label',
  3050. '/fieldset',
  3051. );
  3052. $this->assertTags($result, $expected);
  3053. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3054. $expected = array(
  3055. 'fieldset' => array(),
  3056. 'legend' => array(),
  3057. 'Gender',
  3058. '/legend',
  3059. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3060. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3061. array('label' => array('for' => 'OfficerGenderMale')),
  3062. 'Male',
  3063. '/label',
  3064. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3065. array('label' => array('for' => 'OfficerGenderFemale')),
  3066. 'Female',
  3067. '/label',
  3068. '/fieldset',
  3069. );
  3070. $this->assertTags($result, $expected);
  3071. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3072. $expected = array(
  3073. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3074. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3075. 'label' => array('for' => 'Contact1Imrequired0'),
  3076. 'option A',
  3077. '/label'
  3078. );
  3079. $this->assertTags($result, $expected);
  3080. $result = $this->Form->radio('Model.1.field', array('option A'));
  3081. $expected = array(
  3082. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3083. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3084. 'label' => array('for' => 'Model1Field0'),
  3085. 'option A',
  3086. '/label'
  3087. );
  3088. $this->assertTags($result, $expected);
  3089. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3090. $expected = array(
  3091. 'fieldset' => array(),
  3092. 'legend' => array(),
  3093. 'Field',
  3094. '/legend',
  3095. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3096. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3097. array('label' => array('for' => 'ModelField0')),
  3098. 'option A',
  3099. '/label',
  3100. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3101. array('label' => array('for' => 'ModelField1')),
  3102. 'option B',
  3103. '/label',
  3104. '/fieldset'
  3105. );
  3106. $this->assertTags($result, $expected);
  3107. $result = $this->Form->radio(
  3108. 'Model.field',
  3109. array('option A', 'option B'),
  3110. array('between' => 'I am between')
  3111. );
  3112. $expected = array(
  3113. 'fieldset' => array(),
  3114. 'legend' => array(),
  3115. 'Field',
  3116. '/legend',
  3117. 'I am between',
  3118. 'input' => array(
  3119. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3120. 'value' => '', 'id' => 'ModelField_'
  3121. ),
  3122. array('input' => array(
  3123. 'type' => 'radio', 'name' => 'data[Model][field]',
  3124. 'value' => '0', 'id' => 'ModelField0'
  3125. )),
  3126. array('label' => array('for' => 'ModelField0')),
  3127. 'option A',
  3128. '/label',
  3129. array('input' => array(
  3130. 'type' => 'radio', 'name' => 'data[Model][field]',
  3131. 'value' => '1', 'id' => 'ModelField1'
  3132. )),
  3133. array('label' => array('for' => 'ModelField1')),
  3134. 'option B',
  3135. '/label',
  3136. '/fieldset'
  3137. );
  3138. $this->assertTags($result, $expected);
  3139. }
  3140. /**
  3141. * Test that radios with a 0 value are selected under the correct conditions.
  3142. *
  3143. * @return void
  3144. */
  3145. public function testRadioOptionWithZeroValue() {
  3146. $expected = array(
  3147. 'fieldset' => array(),
  3148. 'legend' => array(),
  3149. 'Field',
  3150. '/legend',
  3151. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3152. array('label' => array('for' => 'ModelField1')),
  3153. 'Yes',
  3154. '/label',
  3155. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3156. array('label' => array('for' => 'ModelField0')),
  3157. 'No',
  3158. '/label',
  3159. '/fieldset'
  3160. );
  3161. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3162. $this->assertTags($result, $expected);
  3163. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3164. $this->assertTags($result, $expected);
  3165. $expected = array(
  3166. 'fieldset' => array(),
  3167. 'legend' => array(),
  3168. 'Field',
  3169. '/legend',
  3170. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3171. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3172. array('label' => array('for' => 'ModelField1')),
  3173. 'Yes',
  3174. '/label',
  3175. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3176. array('label' => array('for' => 'ModelField0')),
  3177. 'No',
  3178. '/label',
  3179. '/fieldset'
  3180. );
  3181. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3182. $this->assertTags($result, $expected);
  3183. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3184. $this->assertTags($result, $expected);
  3185. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3186. $this->assertTags($result, $expected);
  3187. }
  3188. /**
  3189. * test disabled radio options
  3190. *
  3191. * @return void
  3192. */
  3193. public function testRadioDisabled() {
  3194. $result = $this->Form->radio(
  3195. 'Model.field',
  3196. array('option A', 'option B'),
  3197. array('disabled' => array('option A'), 'value' => '0')
  3198. );
  3199. $expected = array(
  3200. 'fieldset' => array(),
  3201. 'legend' => array(),
  3202. 'Field',
  3203. '/legend',
  3204. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3205. array('label' => array('for' => 'ModelField0')),
  3206. 'option A',
  3207. '/label',
  3208. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3209. array('label' => array('for' => 'ModelField1')),
  3210. 'option B',
  3211. '/label',
  3212. '/fieldset'
  3213. );
  3214. $this->assertTags($result, $expected);
  3215. $result = $this->Form->radio(
  3216. 'Model.field',
  3217. array('option A', 'option B'),
  3218. array('disabled' => true, 'value' => '0')
  3219. );
  3220. $expected = array(
  3221. 'fieldset' => array(),
  3222. 'legend' => array(),
  3223. 'Field',
  3224. '/legend',
  3225. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3226. array('label' => array('for' => 'ModelField0')),
  3227. 'option A',
  3228. '/label',
  3229. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3230. array('label' => array('for' => 'ModelField1')),
  3231. 'option B',
  3232. '/label',
  3233. '/fieldset'
  3234. );
  3235. $this->assertTags($result, $expected);
  3236. $result = $this->Form->radio(
  3237. 'Model.field',
  3238. array('option A', 'option B'),
  3239. array('disabled' => 'disabled', 'value' => '0')
  3240. );
  3241. $expected = array(
  3242. 'fieldset' => array(),
  3243. 'legend' => array(),
  3244. 'Field',
  3245. '/legend',
  3246. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3247. array('label' => array('for' => 'ModelField0')),
  3248. 'option A',
  3249. '/label',
  3250. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3251. array('label' => array('for' => 'ModelField1')),
  3252. 'option B',
  3253. '/label',
  3254. '/fieldset'
  3255. );
  3256. $this->assertTags($result, $expected);
  3257. }
  3258. /**
  3259. * test disabling the hidden input for radio buttons
  3260. *
  3261. * @return void
  3262. */
  3263. public function testRadioHiddenInputDisabling() {
  3264. $result = $this->Form->input('Model.1.field', array(
  3265. 'type' => 'radio',
  3266. 'options' => array('option A'),
  3267. 'hiddenField' => false
  3268. )
  3269. );
  3270. $expected = array(
  3271. 'div' => array('class' => 'input radio'),
  3272. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3273. 'label' => array('for' => 'Model1Field0'),
  3274. 'option A',
  3275. '/label',
  3276. '/div'
  3277. );
  3278. $this->assertTags($result, $expected);
  3279. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3280. $expected = array(
  3281. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3282. 'label' => array('for' => 'Model1Field0'),
  3283. 'option A',
  3284. '/label'
  3285. );
  3286. $this->assertTags($result, $expected);
  3287. }
  3288. /**
  3289. * test adding an empty option for radio buttons
  3290. *
  3291. * @return void
  3292. */
  3293. public function testRadioAddEmptyOption() {
  3294. $result = $this->Form->input('Model.1.field', array(
  3295. 'type' => 'radio',
  3296. 'options' => array('option A'),
  3297. 'empty' => true,
  3298. 'hiddenField' => false
  3299. ));
  3300. $expected = array(
  3301. 'div' => array('class' => 'input radio'),
  3302. 'fieldset' => array(),
  3303. 'legend' => array(),
  3304. 'Field',
  3305. '/legend',
  3306. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3307. array('label' => array('for' => 'Model1Field')),
  3308. __('empty'),
  3309. '/label',
  3310. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3311. array('label' => array('for' => 'Model1Field0')),
  3312. 'option A',
  3313. '/label',
  3314. '/fieldset',
  3315. '/div'
  3316. );
  3317. $this->assertTags($result, $expected);
  3318. $result = $this->Form->input('Model.1.field', array(
  3319. 'type' => 'radio',
  3320. 'options' => array('option A'),
  3321. 'empty' => 'CustomEmptyLabel',
  3322. 'hiddenField' => false
  3323. ));
  3324. $expected = array(
  3325. 'div' => array('class' => 'input radio'),
  3326. 'fieldset' => array(),
  3327. 'legend' => array(),
  3328. 'Field',
  3329. '/legend',
  3330. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3331. array('label' => array('for' => 'Model1Field')),
  3332. 'CustomEmptyLabel',
  3333. '/label',
  3334. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3335. array('label' => array('for' => 'Model1Field0')),
  3336. 'option A',
  3337. '/label',
  3338. '/fieldset',
  3339. '/div'
  3340. );
  3341. $this->assertTags($result, $expected);
  3342. $result = $this->Form->input('Model.1.field', array(
  3343. 'type' => 'radio',
  3344. 'options' => array('option A'),
  3345. 'empty' => false,
  3346. 'hiddenField' => false
  3347. ));
  3348. $this->assertTextNotContains('"Model1Field"', $result);
  3349. }
  3350. /**
  3351. * testSelect method
  3352. *
  3353. * Test select element generation.
  3354. *
  3355. * @return void
  3356. */
  3357. public function testSelect() {
  3358. $result = $this->Form->select('Model.field', array());
  3359. $expected = array(
  3360. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3361. array('option' => array('value' => '')),
  3362. '/option',
  3363. '/select'
  3364. );
  3365. $this->assertTags($result, $expected);
  3366. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3367. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3368. $expected = array(
  3369. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3370. array('option' => array('value' => '')),
  3371. '/option',
  3372. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3373. 'good',
  3374. '/option',
  3375. array('option' => array('value' => 'other')),
  3376. 'bad',
  3377. '/option',
  3378. '/select'
  3379. );
  3380. $this->assertTags($result, $expected);
  3381. $this->Form->request->data = array();
  3382. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3383. $expected = array(
  3384. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3385. array('option' => array('value' => '')),
  3386. '/option',
  3387. array('option' => array('value' => 'value')),
  3388. 'good',
  3389. '/option',
  3390. array('option' => array('value' => 'other')),
  3391. 'bad',
  3392. '/option',
  3393. '/select'
  3394. );
  3395. $this->assertTags($result, $expected);
  3396. $result = $this->Form->select(
  3397. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3398. array('empty' => false)
  3399. );
  3400. $expected = array(
  3401. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3402. array('option' => array('value' => 'first')),
  3403. 'first &quot;html&quot; &lt;chars&gt;',
  3404. '/option',
  3405. array('option' => array('value' => 'second')),
  3406. 'value',
  3407. '/option',
  3408. '/select'
  3409. );
  3410. $this->assertTags($result, $expected);
  3411. $result = $this->Form->select(
  3412. 'Model.field',
  3413. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3414. array('escape' => false, 'empty' => false)
  3415. );
  3416. $expected = array(
  3417. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3418. array('option' => array('value' => 'first')),
  3419. 'first "html" <chars>',
  3420. '/option',
  3421. array('option' => array('value' => 'second')),
  3422. 'value',
  3423. '/option',
  3424. '/select'
  3425. );
  3426. $this->assertTags($result, $expected);
  3427. $options = array(
  3428. array('value' => 'first', 'name' => 'First'),
  3429. array('value' => 'first', 'name' => 'Another First'),
  3430. );
  3431. $result = $this->Form->select(
  3432. 'Model.field',
  3433. $options,
  3434. array('escape' => false, 'empty' => false)
  3435. );
  3436. $expected = array(
  3437. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3438. array('option' => array('value' => 'first')),
  3439. 'First',
  3440. '/option',
  3441. array('option' => array('value' => 'first')),
  3442. 'Another First',
  3443. '/option',
  3444. '/select'
  3445. );
  3446. $this->assertTags($result, $expected);
  3447. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3448. $result = $this->Form->select(
  3449. 'Model.contact_id',
  3450. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3451. array('escape' => false, 'empty' => 'pick something')
  3452. );
  3453. $expected = array(
  3454. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3455. array('option' => array('value' => '')), 'pick something', '/option',
  3456. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3457. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3458. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3459. '/select'
  3460. );
  3461. $this->assertTags($result, $expected);
  3462. $this->Form->request->data['Model']['field'] = 0;
  3463. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3464. $expected = array(
  3465. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3466. array('option' => array('value' => '')), '/option',
  3467. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3468. array('option' => array('value' => '1')), 'Yes', '/option',
  3469. '/select'
  3470. );
  3471. $this->assertTags($result, $expected);
  3472. }
  3473. /**
  3474. * test that select() with optiongroups listens to the escape param.
  3475. *
  3476. * @return void
  3477. */
  3478. public function testSelectOptionGroupEscaping() {
  3479. $options = array(
  3480. '>< Key' => array(
  3481. 1 => 'One',
  3482. 2 => 'Two'
  3483. )
  3484. );
  3485. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3486. $expected = array(
  3487. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3488. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3489. array('option' => array('value' => '1')), 'One', '/option',
  3490. array('option' => array('value' => '2')), 'Two', '/option',
  3491. '/optgroup',
  3492. '/select'
  3493. );
  3494. $this->assertTags($result, $expected);
  3495. $options = array(
  3496. '>< Key' => array(
  3497. 1 => 'One',
  3498. 2 => 'Two'
  3499. )
  3500. );
  3501. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3502. $expected = array(
  3503. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3504. 'optgroup' => array('label' => '>< Key'),
  3505. array('option' => array('value' => '1')), 'One', '/option',
  3506. array('option' => array('value' => '2')), 'Two', '/option',
  3507. '/optgroup',
  3508. '/select'
  3509. );
  3510. $this->assertTags($result, $expected);
  3511. }
  3512. /**
  3513. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3514. *
  3515. * @return void
  3516. */
  3517. public function testSelectWithNullAttributes() {
  3518. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3519. $expected = array(
  3520. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3521. array('option' => array('value' => '0')),
  3522. 'first',
  3523. '/option',
  3524. array('option' => array('value' => '1')),
  3525. 'second',
  3526. '/option',
  3527. '/select'
  3528. );
  3529. $this->assertTags($result, $expected);
  3530. }
  3531. /**
  3532. * testNestedSelect method
  3533. *
  3534. * test select element generation with optgroups
  3535. *
  3536. * @return void
  3537. */
  3538. public function testNestedSelect() {
  3539. $result = $this->Form->select(
  3540. 'Model.field',
  3541. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3542. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3543. )), array('empty' => false)
  3544. );
  3545. $expected = array(
  3546. 'select' => array('name' => 'data[Model][field]',
  3547. 'id' => 'ModelField'),
  3548. array('option' => array('value' => 1)),
  3549. 'One',
  3550. '/option',
  3551. array('option' => array('value' => 2)),
  3552. 'Two',
  3553. '/option',
  3554. array('optgroup' => array('label' => 'Three')),
  3555. array('option' => array('value' => 4)),
  3556. 'Four',
  3557. '/option',
  3558. array('option' => array('value' => 5)),
  3559. 'Five',
  3560. '/option',
  3561. '/optgroup',
  3562. '/select'
  3563. );
  3564. $this->assertTags($result, $expected);
  3565. $result = $this->Form->select(
  3566. 'Model.field',
  3567. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3568. array('showParents' => true, 'empty' => false)
  3569. );
  3570. $expected = array(
  3571. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3572. array('option' => array('value' => 1)),
  3573. 'One',
  3574. '/option',
  3575. array('option' => array('value' => 2)),
  3576. 'Two',
  3577. '/option',
  3578. array('optgroup' => array('label' => 'Three')),
  3579. array('option' => array('value' => 3)),
  3580. 'Three',
  3581. '/option',
  3582. array('option' => array('value' => 4)),
  3583. 'Four',
  3584. '/option',
  3585. '/optgroup',
  3586. '/select'
  3587. );
  3588. $this->assertTags($result, $expected);
  3589. }
  3590. /**
  3591. * testSelectMultiple method
  3592. *
  3593. * test generation of multiple select elements
  3594. *
  3595. * @return void
  3596. */
  3597. public function testSelectMultiple() {
  3598. $options = array('first', 'second', 'third');
  3599. $result = $this->Form->select(
  3600. 'Model.multi_field', $options, array('multiple' => true)
  3601. );
  3602. $expected = array(
  3603. 'input' => array(
  3604. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3605. ),
  3606. 'select' => array(
  3607. 'name' => 'data[Model][multi_field][]',
  3608. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3609. ),
  3610. array('option' => array('value' => '0')),
  3611. 'first',
  3612. '/option',
  3613. array('option' => array('value' => '1')),
  3614. 'second',
  3615. '/option',
  3616. array('option' => array('value' => '2')),
  3617. 'third',
  3618. '/option',
  3619. '/select'
  3620. );
  3621. $this->assertTags($result, $expected);
  3622. $result = $this->Form->select(
  3623. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3624. );
  3625. $expected = array(
  3626. 'input' => array(
  3627. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3628. ),
  3629. 'select' => array(
  3630. 'name' => 'data[Model][multi_field][]',
  3631. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3632. ),
  3633. array('option' => array('value' => '0')),
  3634. 'first',
  3635. '/option',
  3636. array('option' => array('value' => '1')),
  3637. 'second',
  3638. '/option',
  3639. array('option' => array('value' => '2')),
  3640. 'third',
  3641. '/option',
  3642. '/select'
  3643. );
  3644. $this->assertTags($result, $expected);
  3645. $result = $this->Form->select(
  3646. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3647. );
  3648. $expected = array(
  3649. 'input' => array(
  3650. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3651. ),
  3652. 'select' => array(
  3653. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3654. 'multiple' => 'multiple'
  3655. ),
  3656. array('option' => array('value' => '0', 'selected' => 'selected')),
  3657. 'first',
  3658. '/option',
  3659. array('option' => array('value' => '1', 'selected' => 'selected')),
  3660. 'second',
  3661. '/option',
  3662. array('option' => array('value' => '2')),
  3663. 'third',
  3664. '/option',
  3665. '/select'
  3666. );
  3667. $this->assertTags($result, $expected);
  3668. $result = $this->Form->select(
  3669. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3670. );
  3671. $expected = array(
  3672. 'select' => array(
  3673. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3674. ),
  3675. array('option' => array('value' => '0', 'selected' => 'selected')),
  3676. 'first',
  3677. '/option',
  3678. array('option' => array('value' => '1', 'selected' => 'selected')),
  3679. 'second',
  3680. '/option',
  3681. array('option' => array('value' => '2')),
  3682. 'third',
  3683. '/option',
  3684. '/select'
  3685. );
  3686. $this->assertTags($result, $expected);
  3687. }
  3688. /**
  3689. * test generation of habtm select boxes.
  3690. *
  3691. * @return void
  3692. */
  3693. public function testHabtmSelectBox() {
  3694. $this->View->viewVars['contactTags'] = array(
  3695. 1 => 'blue',
  3696. 2 => 'red',
  3697. 3 => 'green'
  3698. );
  3699. $this->Form->request->data = array(
  3700. 'Contact' => array(),
  3701. 'ContactTag' => array(
  3702. array(
  3703. 'id' => 1,
  3704. 'name' => 'blue'
  3705. ),
  3706. array(
  3707. 'id' => 3,
  3708. 'name' => 'green'
  3709. )
  3710. )
  3711. );
  3712. $this->Form->create('Contact');
  3713. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3714. $expected = array(
  3715. 'input' => array(
  3716. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3717. ),
  3718. 'select' => array(
  3719. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3720. 'multiple' => 'multiple'
  3721. ),
  3722. array('option' => array('value' => '1', 'selected' => 'selected')),
  3723. 'blue',
  3724. '/option',
  3725. array('option' => array('value' => '2')),
  3726. 'red',
  3727. '/option',
  3728. array('option' => array('value' => '3', 'selected' => 'selected')),
  3729. 'green',
  3730. '/option',
  3731. '/select'
  3732. );
  3733. $this->assertTags($result, $expected);
  3734. }
  3735. /**
  3736. * test generation of multi select elements in checkbox format
  3737. *
  3738. * @return void
  3739. */
  3740. public function testSelectMultipleCheckboxes() {
  3741. $result = $this->Form->select(
  3742. 'Model.multi_field',
  3743. array('first', 'second', 'third'),
  3744. array('multiple' => 'checkbox')
  3745. );
  3746. $expected = array(
  3747. 'input' => array(
  3748. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3749. ),
  3750. array('div' => array('class' => 'checkbox')),
  3751. array('input' => array(
  3752. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3753. 'value' => '0', 'id' => 'ModelMultiField0'
  3754. )),
  3755. array('label' => array('for' => 'ModelMultiField0')),
  3756. 'first',
  3757. '/label',
  3758. '/div',
  3759. array('div' => array('class' => 'checkbox')),
  3760. array('input' => array(
  3761. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3762. 'value' => '1', 'id' => 'ModelMultiField1'
  3763. )),
  3764. array('label' => array('for' => 'ModelMultiField1')),
  3765. 'second',
  3766. '/label',
  3767. '/div',
  3768. array('div' => array('class' => 'checkbox')),
  3769. array('input' => array(
  3770. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3771. 'value' => '2', 'id' => 'ModelMultiField2'
  3772. )),
  3773. array('label' => array('for' => 'ModelMultiField2')),
  3774. 'third',
  3775. '/label',
  3776. '/div'
  3777. );
  3778. $this->assertTags($result, $expected);
  3779. $result = $this->Form->select(
  3780. 'Model.multi_field',
  3781. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3782. array('multiple' => 'checkbox')
  3783. );
  3784. $expected = array(
  3785. 'input' => array(
  3786. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3787. ),
  3788. array('div' => array('class' => 'checkbox')),
  3789. array('input' => array(
  3790. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3791. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3792. )),
  3793. array('label' => array('for' => 'ModelMultiFieldA')),
  3794. 'first',
  3795. '/label',
  3796. '/div',
  3797. array('div' => array('class' => 'checkbox')),
  3798. array('input' => array(
  3799. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3800. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3801. )),
  3802. array('label' => array('for' => 'ModelMultiFieldB')),
  3803. 'second',
  3804. '/label',
  3805. '/div',
  3806. array('div' => array('class' => 'checkbox')),
  3807. array('input' => array(
  3808. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3809. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3810. )),
  3811. array('label' => array('for' => 'ModelMultiFieldC')),
  3812. 'third',
  3813. '/label',
  3814. '/div'
  3815. );
  3816. $this->assertTags($result, $expected);
  3817. $result = $this->Form->select(
  3818. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3819. );
  3820. $expected = array(
  3821. 'input' => array(
  3822. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3823. ),
  3824. array('div' => array('class' => 'checkbox')),
  3825. array('input' => array(
  3826. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3827. 'value' => '1', 'id' => 'ModelMultiField1'
  3828. )),
  3829. array('label' => array('for' => 'ModelMultiField1')),
  3830. 'first',
  3831. '/label',
  3832. '/div'
  3833. );
  3834. $this->assertTags($result, $expected);
  3835. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3836. $result = $this->Form->select(
  3837. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3838. );
  3839. $expected = array(
  3840. 'input' => array(
  3841. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3842. ),
  3843. array('div' => array('class' => 'checkbox')),
  3844. array('input' => array(
  3845. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3846. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3847. )),
  3848. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3849. 'first',
  3850. '/label',
  3851. '/div',
  3852. array('div' => array('class' => 'checkbox')),
  3853. array('input' => array(
  3854. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3855. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3856. )),
  3857. array('label' => array('for' => 'ModelTagsArray')),
  3858. 'Array',
  3859. '/label',
  3860. '/div'
  3861. );
  3862. $this->assertTags($result, $expected);
  3863. }
  3864. /**
  3865. * test multiple checkboxes with div styles.
  3866. *
  3867. * @return void
  3868. */
  3869. public function testSelectMultipleCheckboxDiv() {
  3870. $result = $this->Form->select(
  3871. 'Model.tags',
  3872. array('first', 'second'),
  3873. array('multiple' => 'checkbox', 'class' => 'my-class')
  3874. );
  3875. $expected = array(
  3876. 'input' => array(
  3877. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3878. ),
  3879. array('div' => array('class' => 'my-class')),
  3880. array('input' => array(
  3881. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3882. 'value' => '0', 'id' => 'ModelTags0'
  3883. )),
  3884. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3885. '/div',
  3886. array('div' => array('class' => 'my-class')),
  3887. array('input' => array(
  3888. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3889. 'value' => '1', 'id' => 'ModelTags1'
  3890. )),
  3891. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3892. '/div'
  3893. );
  3894. $this->assertTags($result, $expected);
  3895. $result = $this->Form->input('Model.tags', array(
  3896. 'options' => array('first', 'second'),
  3897. 'multiple' => 'checkbox',
  3898. 'class' => 'my-class',
  3899. 'div' => false,
  3900. 'label' => false
  3901. ));
  3902. $this->assertTags($result, $expected);
  3903. $Contact = ClassRegistry::getObject('Contact');
  3904. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3905. $result = $this->Form->input('Contact.tags', array(
  3906. 'options' => array('one'),
  3907. 'multiple' => 'checkbox',
  3908. 'label' => false,
  3909. 'div' => false
  3910. ));
  3911. $expected = array(
  3912. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3913. array('div' => array('class' => 'checkbox form-error')),
  3914. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3915. array('label' => array('for' => 'ContactTags0')),
  3916. 'one',
  3917. '/label',
  3918. '/div'
  3919. );
  3920. $this->assertTags($result, $expected);
  3921. $result = $this->Form->input('Contact.tags', array(
  3922. 'options' => array('one'),
  3923. 'multiple' => 'checkbox',
  3924. 'class' => 'mycheckbox',
  3925. 'label' => false,
  3926. 'div' => false
  3927. ));
  3928. $expected = array(
  3929. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3930. array('div' => array('class' => 'mycheckbox form-error')),
  3931. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3932. array('label' => array('for' => 'ContactTags0')),
  3933. 'one',
  3934. '/label',
  3935. '/div'
  3936. );
  3937. $this->assertTags($result, $expected);
  3938. }
  3939. /**
  3940. * Checks the security hash array generated for multiple-input checkbox elements
  3941. *
  3942. * @return void
  3943. */
  3944. public function testSelectMultipleCheckboxSecurity() {
  3945. $this->Form->request['_Token'] = array('key' => 'testKey');
  3946. $this->assertEquals(array(), $this->Form->fields);
  3947. $result = $this->Form->select(
  3948. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3949. array('multiple' => 'checkbox')
  3950. );
  3951. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3952. $result = $this->Form->secure($this->Form->fields);
  3953. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3954. $this->assertRegExp('/"' . $key . '"/', $result);
  3955. }
  3956. /**
  3957. * Multiple select elements should always be secured as they always participate
  3958. * in the POST data.
  3959. *
  3960. * @return void
  3961. */
  3962. public function testSelectMultipleSecureWithNoOptions() {
  3963. $this->Form->request['_Token'] = array('key' => 'testkey');
  3964. $this->assertEquals(array(), $this->Form->fields);
  3965. $result = $this->Form->select(
  3966. 'Model.select',
  3967. array(),
  3968. array('multiple' => true)
  3969. );
  3970. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3971. }
  3972. /**
  3973. * When a select box has no options it should not be added to the fields list
  3974. * as it always fail post validation.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testSelectNoSecureWithNoOptions() {
  3979. $this->Form->request['_Token'] = array('key' => 'testkey');
  3980. $this->assertEquals(array(), $this->Form->fields);
  3981. $this->Form->select(
  3982. 'Model.select',
  3983. array()
  3984. );
  3985. $this->assertEquals(array(), $this->Form->fields);
  3986. $this->Form->select(
  3987. 'Model.select',
  3988. array(),
  3989. array('empty' => true)
  3990. );
  3991. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3992. }
  3993. /**
  3994. * testInputMultipleCheckboxes method
  3995. *
  3996. * test input() resulting in multi select elements being generated.
  3997. *
  3998. * @return void
  3999. */
  4000. public function testInputMultipleCheckboxes() {
  4001. $result = $this->Form->input('Model.multi_field', array(
  4002. 'options' => array('first', 'second', 'third'),
  4003. 'multiple' => 'checkbox'
  4004. ));
  4005. $expected = array(
  4006. array('div' => array('class' => 'input select')),
  4007. array('label' => array('for' => 'ModelMultiField')),
  4008. 'Multi Field',
  4009. '/label',
  4010. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4011. array('div' => array('class' => 'checkbox')),
  4012. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4013. array('label' => array('for' => 'ModelMultiField0')),
  4014. 'first',
  4015. '/label',
  4016. '/div',
  4017. array('div' => array('class' => 'checkbox')),
  4018. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4019. array('label' => array('for' => 'ModelMultiField1')),
  4020. 'second',
  4021. '/label',
  4022. '/div',
  4023. array('div' => array('class' => 'checkbox')),
  4024. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4025. array('label' => array('for' => 'ModelMultiField2')),
  4026. 'third',
  4027. '/label',
  4028. '/div',
  4029. '/div'
  4030. );
  4031. $this->assertTags($result, $expected);
  4032. $result = $this->Form->input('Model.multi_field', array(
  4033. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4034. 'multiple' => 'checkbox'
  4035. ));
  4036. $expected = array(
  4037. array('div' => array('class' => 'input select')),
  4038. array('label' => array('for' => 'ModelMultiField')),
  4039. 'Multi Field',
  4040. '/label',
  4041. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4042. array('div' => array('class' => 'checkbox')),
  4043. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4044. array('label' => array('for' => 'ModelMultiFieldA')),
  4045. 'first',
  4046. '/label',
  4047. '/div',
  4048. array('div' => array('class' => 'checkbox')),
  4049. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4050. array('label' => array('for' => 'ModelMultiFieldB')),
  4051. 'second',
  4052. '/label',
  4053. '/div',
  4054. array('div' => array('class' => 'checkbox')),
  4055. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4056. array('label' => array('for' => 'ModelMultiFieldC')),
  4057. 'third',
  4058. '/label',
  4059. '/div',
  4060. '/div'
  4061. );
  4062. $this->assertTags($result, $expected);
  4063. $result = $this->Form->input('Model.multi_field', array(
  4064. 'options' => array('1' => 'first'),
  4065. 'multiple' => 'checkbox',
  4066. 'label' => false,
  4067. 'div' => false
  4068. ));
  4069. $expected = array(
  4070. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4071. array('div' => array('class' => 'checkbox')),
  4072. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4073. array('label' => array('for' => 'ModelMultiField1')),
  4074. 'first',
  4075. '/label',
  4076. '/div'
  4077. );
  4078. $this->assertTags($result, $expected);
  4079. $result = $this->Form->input('Model.multi_field', array(
  4080. 'options' => array('2' => 'second'),
  4081. 'multiple' => 'checkbox',
  4082. 'label' => false,
  4083. 'div' => false
  4084. ));
  4085. $expected = array(
  4086. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4087. array('div' => array('class' => 'checkbox')),
  4088. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4089. array('label' => array('for' => 'ModelMultiField2')),
  4090. 'second',
  4091. '/label',
  4092. '/div'
  4093. );
  4094. $this->assertTags($result, $expected);
  4095. }
  4096. /**
  4097. * testSelectHiddenFieldOmission method
  4098. *
  4099. * test that select() with 'hiddenField' => false omits the hidden field
  4100. *
  4101. * @return void
  4102. */
  4103. public function testSelectHiddenFieldOmission() {
  4104. $result = $this->Form->select('Model.multi_field',
  4105. array('first', 'second'),
  4106. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4107. );
  4108. $expected = array(
  4109. array('div' => array('class' => 'checkbox')),
  4110. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4111. array('label' => array('for' => 'ModelMultiField0')),
  4112. 'first',
  4113. '/label',
  4114. '/div',
  4115. array('div' => array('class' => 'checkbox')),
  4116. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4117. array('label' => array('for' => 'ModelMultiField1')),
  4118. 'second',
  4119. '/label',
  4120. '/div'
  4121. );
  4122. $this->assertTags($result, $expected);
  4123. $result = $this->Form->input('Model.multi_field', array(
  4124. 'options' => array('first', 'second'),
  4125. 'multiple' => 'checkbox',
  4126. 'hiddenField' => false
  4127. ));
  4128. $expected = array(
  4129. array('div' => array('class' => 'input select')),
  4130. array('label' => array('for' => 'ModelMultiField')),
  4131. 'Multi Field',
  4132. '/label',
  4133. array('div' => array('class' => 'checkbox')),
  4134. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4135. array('label' => array('for' => 'ModelMultiField0')),
  4136. 'first',
  4137. '/label',
  4138. '/div',
  4139. array('div' => array('class' => 'checkbox')),
  4140. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4141. array('label' => array('for' => 'ModelMultiField1')),
  4142. 'second',
  4143. '/label',
  4144. '/div',
  4145. '/div'
  4146. );
  4147. $this->assertTags($result, $expected);
  4148. }
  4149. /**
  4150. * test that select() with multiple = checkbox works with overriding name attribute.
  4151. *
  4152. * @return void
  4153. */
  4154. public function testSelectCheckboxMultipleOverrideName() {
  4155. $result = $this->Form->input('category', array(
  4156. 'type' => 'select',
  4157. 'multiple' => 'checkbox',
  4158. 'name' => 'data[fish]',
  4159. 'options' => array('1', '2'),
  4160. 'div' => false,
  4161. 'label' => false,
  4162. ));
  4163. $expected = array(
  4164. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4165. array('div' => array('class' => 'checkbox')),
  4166. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4167. array('label' => array('for' => 'Category0')), '1', '/label',
  4168. '/div',
  4169. array('div' => array('class' => 'checkbox')),
  4170. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4171. array('label' => array('for' => 'Category1')), '2', '/label',
  4172. '/div'
  4173. );
  4174. $this->assertTags($result, $expected);
  4175. }
  4176. /**
  4177. * Test that 'id' overrides all the checkbox id's as well.
  4178. *
  4179. * @return void
  4180. */
  4181. public function testSelectCheckboxMultipleId() {
  4182. $result = $this->Form->select(
  4183. 'Model.multi_field',
  4184. array('first', 'second', 'third'),
  4185. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4186. );
  4187. $expected = array(
  4188. 'input' => array(
  4189. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4190. ),
  4191. array('div' => array('class' => 'checkbox')),
  4192. array('input' => array(
  4193. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4194. 'value' => '0', 'id' => 'CustomId0'
  4195. )),
  4196. array('label' => array('for' => 'CustomId0')),
  4197. 'first',
  4198. '/label',
  4199. '/div',
  4200. array('div' => array('class' => 'checkbox')),
  4201. array('input' => array(
  4202. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4203. 'value' => '1', 'id' => 'CustomId1'
  4204. )),
  4205. array('label' => array('for' => 'CustomId1')),
  4206. 'second',
  4207. '/label',
  4208. '/div',
  4209. array('div' => array('class' => 'checkbox')),
  4210. array('input' => array(
  4211. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4212. 'value' => '2', 'id' => 'CustomId2'
  4213. )),
  4214. array('label' => array('for' => 'CustomId2')),
  4215. 'third',
  4216. '/label',
  4217. '/div'
  4218. );
  4219. $this->assertTags($result, $expected);
  4220. }
  4221. /**
  4222. * testCheckbox method
  4223. *
  4224. * Test generation of checkboxes
  4225. *
  4226. * @return void
  4227. */
  4228. public function testCheckbox() {
  4229. $result = $this->Form->checkbox('Model.field');
  4230. $expected = array(
  4231. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4232. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4233. );
  4234. $this->assertTags($result, $expected);
  4235. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4236. $expected = array(
  4237. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4238. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4239. );
  4240. $this->assertTags($result, $expected);
  4241. $Contact = ClassRegistry::getObject('Contact');
  4242. $Contact->validationErrors['field'] = 1;
  4243. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4244. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4245. $expected = array(
  4246. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4247. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4248. );
  4249. $this->assertTags($result, $expected);
  4250. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4251. $expected = array(
  4252. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4253. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4254. );
  4255. $this->assertTags($result, $expected);
  4256. $this->Form->request->data['Contact']['field'] = '';
  4257. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4258. $expected = array(
  4259. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4260. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4261. );
  4262. $this->assertTags($result, $expected);
  4263. $Contact->validationErrors = array();
  4264. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4265. $expected = array(
  4266. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4267. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4268. );
  4269. $this->assertTags($result, $expected);
  4270. $this->Form->request->data['Contact']['published'] = 1;
  4271. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4272. $expected = array(
  4273. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4274. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4275. );
  4276. $this->assertTags($result, $expected);
  4277. $this->Form->request->data['Contact']['published'] = 0;
  4278. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4279. $expected = array(
  4280. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4281. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4282. );
  4283. $this->assertTags($result, $expected);
  4284. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4285. $expected = array(
  4286. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4287. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4288. );
  4289. $this->assertTags($result, $expected);
  4290. $result = $this->Form->checkbox('CustomField.1.value');
  4291. $expected = array(
  4292. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4293. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4294. );
  4295. $this->assertTags($result, $expected);
  4296. }
  4297. /**
  4298. * test checkbox() with a custom name attribute
  4299. *
  4300. * @return void
  4301. */
  4302. public function testCheckboxCustomNameAttribute() {
  4303. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4304. $expected = array(
  4305. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4306. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4307. );
  4308. $this->assertTags($result, $expected);
  4309. }
  4310. /**
  4311. * test the checked option for checkboxes.
  4312. *
  4313. * @return void
  4314. */
  4315. public function testCheckboxCheckedOption() {
  4316. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4317. $expected = array(
  4318. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4319. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4320. );
  4321. $this->assertTags($result, $expected);
  4322. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4323. $expected = array(
  4324. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4325. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4326. );
  4327. $this->assertTags($result, $expected);
  4328. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4329. $expected = array(
  4330. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4331. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4332. );
  4333. $this->assertTags($result, $expected);
  4334. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4335. $expected = array(
  4336. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4337. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4338. );
  4339. $this->assertTags($result, $expected);
  4340. $this->Form->request->data['Model']['field'] = 1;
  4341. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4342. $expected = array(
  4343. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4344. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4345. );
  4346. $this->assertTags($result, $expected);
  4347. }
  4348. /**
  4349. * Test that disabled attribute works on both the checkbox and hidden input.
  4350. *
  4351. * @return void
  4352. */
  4353. public function testCheckboxDisabling() {
  4354. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4355. $expected = array(
  4356. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4357. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4358. );
  4359. $this->assertTags($result, $expected);
  4360. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4361. $expected = array(
  4362. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4363. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4364. );
  4365. $this->assertTags($result, $expected);
  4366. }
  4367. /**
  4368. * Test that the hidden input for checkboxes can be omitted or set to a
  4369. * specific value.
  4370. *
  4371. * @return void
  4372. */
  4373. public function testCheckboxHiddenField() {
  4374. $result = $this->Form->input('UserForm.something', array(
  4375. 'type' => 'checkbox',
  4376. 'hiddenField' => false
  4377. ));
  4378. $expected = array(
  4379. 'div' => array('class' => 'input checkbox'),
  4380. array('input' => array(
  4381. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4382. 'value' => '1', 'id' => 'UserFormSomething'
  4383. )),
  4384. 'label' => array('for' => 'UserFormSomething'),
  4385. 'Something',
  4386. '/label',
  4387. '/div'
  4388. );
  4389. $this->assertTags($result, $expected);
  4390. $result = $this->Form->input('UserForm.something', array(
  4391. 'type' => 'checkbox',
  4392. 'value' => 'Y',
  4393. 'hiddenField' => 'N',
  4394. ));
  4395. $expected = array(
  4396. 'div' => array('class' => 'input checkbox'),
  4397. array('input' => array(
  4398. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4399. 'value' => 'N', 'id' => 'UserFormSomething_'
  4400. )),
  4401. array('input' => array(
  4402. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4403. 'value' => 'Y', 'id' => 'UserFormSomething'
  4404. )),
  4405. 'label' => array('for' => 'UserFormSomething'),
  4406. 'Something',
  4407. '/label',
  4408. '/div'
  4409. );
  4410. $this->assertTags($result, $expected);
  4411. }
  4412. /**
  4413. * testDateTime method
  4414. *
  4415. * Test generation of date/time select elements
  4416. *
  4417. * @return void
  4418. */
  4419. public function testDateTime() {
  4420. extract($this->dateRegex);
  4421. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4422. $now = strtotime('now');
  4423. $expected = array(
  4424. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4425. $daysRegex,
  4426. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4427. date('j', $now),
  4428. '/option',
  4429. '*/select',
  4430. '-',
  4431. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4432. $monthsRegex,
  4433. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4434. date('F', $now),
  4435. '/option',
  4436. '*/select',
  4437. '-',
  4438. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4439. $yearsRegex,
  4440. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4441. date('Y', $now),
  4442. '/option',
  4443. '*/select',
  4444. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4445. $hoursRegex,
  4446. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4447. date('g', $now),
  4448. '/option',
  4449. '*/select',
  4450. ':',
  4451. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4452. $minutesRegex,
  4453. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4454. date('i', $now),
  4455. '/option',
  4456. '*/select',
  4457. ' ',
  4458. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4459. $meridianRegex,
  4460. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4461. date('a', $now),
  4462. '/option',
  4463. '*/select'
  4464. );
  4465. $this->assertTags($result, $expected);
  4466. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4467. $expected = array(
  4468. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4469. $daysRegex,
  4470. array('option' => array('value' => '')),
  4471. '/option',
  4472. '*/select',
  4473. '-',
  4474. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4475. $monthsRegex,
  4476. array('option' => array('value' => '')),
  4477. '/option',
  4478. '*/select',
  4479. '-',
  4480. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4481. $yearsRegex,
  4482. array('option' => array('value' => '')),
  4483. '/option',
  4484. '*/select',
  4485. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4486. $hoursRegex,
  4487. array('option' => array('value' => '')),
  4488. '/option',
  4489. '*/select',
  4490. ':',
  4491. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4492. $minutesRegex,
  4493. array('option' => array('value' => '')),
  4494. '/option',
  4495. '*/select',
  4496. ' ',
  4497. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4498. $meridianRegex,
  4499. array('option' => array('value' => '')),
  4500. '/option',
  4501. '*/select'
  4502. );
  4503. $this->assertTags($result, $expected);
  4504. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4505. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4506. $this->assertTags($result, $expected);
  4507. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4508. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4509. $this->assertTags($result, $expected);
  4510. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4511. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4512. $expected = array(
  4513. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4514. $daysRegex,
  4515. array('option' => array('value' => '')),
  4516. '/option',
  4517. '*/select',
  4518. '-',
  4519. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4520. $monthsRegex,
  4521. array('option' => array('value' => '')),
  4522. '/option',
  4523. '*/select',
  4524. '-',
  4525. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4526. $yearsRegex,
  4527. array('option' => array('value' => '')),
  4528. '/option',
  4529. '*/select',
  4530. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4531. $hoursRegex,
  4532. array('option' => array('value' => '')),
  4533. '/option',
  4534. '*/select',
  4535. ':',
  4536. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4537. $minutesRegex,
  4538. array('option' => array('value' => '')),
  4539. '/option',
  4540. array('option' => array('value' => '00')),
  4541. '00',
  4542. '/option',
  4543. array('option' => array('value' => '05')),
  4544. '05',
  4545. '/option',
  4546. array('option' => array('value' => '10')),
  4547. '10',
  4548. '/option',
  4549. '*/select',
  4550. ' ',
  4551. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4552. $meridianRegex,
  4553. array('option' => array('value' => '')),
  4554. '/option',
  4555. '*/select'
  4556. );
  4557. $this->assertTags($result, $expected);
  4558. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4559. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4560. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4561. $this->Form->request->data['Contact']['data'] = null;
  4562. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4563. $expected = array(
  4564. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4565. $daysRegex,
  4566. array('option' => array('value' => '')),
  4567. '/option',
  4568. '*/select',
  4569. '-',
  4570. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4571. $monthsRegex,
  4572. array('option' => array('value' => '')),
  4573. '/option',
  4574. '*/select',
  4575. '-',
  4576. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4577. $yearsRegex,
  4578. array('option' => array('value' => '')),
  4579. '/option',
  4580. '*/select',
  4581. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4582. $hoursRegex,
  4583. array('option' => array('value' => '')),
  4584. '/option',
  4585. '*/select',
  4586. ':',
  4587. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4588. $minutesRegex,
  4589. array('option' => array('value' => '')),
  4590. '/option',
  4591. '*/select',
  4592. ' ',
  4593. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4594. $meridianRegex,
  4595. array('option' => array('value' => '')),
  4596. '/option',
  4597. '*/select'
  4598. );
  4599. $this->assertTags($result, $expected);
  4600. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4601. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4602. $now = strtotime($this->Form->data['Model']['field']);
  4603. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4604. $expected = array(
  4605. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4606. $daysRegex,
  4607. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4608. date('j', $now),
  4609. '/option',
  4610. '*/select',
  4611. '-',
  4612. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4613. $monthsRegex,
  4614. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4615. date('F', $now),
  4616. '/option',
  4617. '*/select',
  4618. '-',
  4619. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4620. $yearsRegex,
  4621. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4622. date('Y', $now),
  4623. '/option',
  4624. '*/select',
  4625. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4626. $hoursRegex,
  4627. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4628. date('g', $now),
  4629. '/option',
  4630. '*/select',
  4631. ':',
  4632. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4633. $minutesRegex,
  4634. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4635. date('i', $now),
  4636. '/option',
  4637. '*/select',
  4638. ' ',
  4639. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4640. $meridianRegex,
  4641. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4642. date('a', $now),
  4643. '/option',
  4644. '*/select'
  4645. );
  4646. $this->assertTags($result, $expected);
  4647. $selected = strtotime('2008-10-26 12:33:00');
  4648. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4649. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4650. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4651. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4652. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4653. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4654. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4655. $this->Form->create('Contact');
  4656. $result = $this->Form->input('published');
  4657. $now = strtotime('now');
  4658. $expected = array(
  4659. 'div' => array('class' => 'input date'),
  4660. 'label' => array('for' => 'ContactPublishedMonth'),
  4661. 'Published',
  4662. '/label',
  4663. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4664. $monthsRegex,
  4665. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4666. date('F', $now),
  4667. '/option',
  4668. '*/select',
  4669. '-',
  4670. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4671. $daysRegex,
  4672. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4673. date('j', $now),
  4674. '/option',
  4675. '*/select',
  4676. '-',
  4677. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4678. $yearsRegex,
  4679. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4680. date('Y', $now),
  4681. '/option',
  4682. '*/select',
  4683. '/div'
  4684. );
  4685. $this->assertTags($result, $expected);
  4686. $result = $this->Form->input('published2', array('type' => 'date'));
  4687. $now = strtotime('now');
  4688. $expected = array(
  4689. 'div' => array('class' => 'input date'),
  4690. 'label' => array('for' => 'ContactPublished2Month'),
  4691. 'Published2',
  4692. '/label',
  4693. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4694. $monthsRegex,
  4695. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4696. date('F', $now),
  4697. '/option',
  4698. '*/select',
  4699. '-',
  4700. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4701. $daysRegex,
  4702. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4703. date('j', $now),
  4704. '/option',
  4705. '*/select',
  4706. '-',
  4707. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4708. $yearsRegex,
  4709. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4710. date('Y', $now),
  4711. '/option',
  4712. '*/select',
  4713. '/div'
  4714. );
  4715. $this->assertTags($result, $expected);
  4716. $this->Form->create('Contact');
  4717. $result = $this->Form->input('published', array('monthNames' => false));
  4718. $now = strtotime('now');
  4719. $expected = array(
  4720. 'div' => array('class' => 'input date'),
  4721. 'label' => array('for' => 'ContactPublishedMonth'),
  4722. 'Published',
  4723. '/label',
  4724. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4725. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4726. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4727. date('m', $now),
  4728. '/option',
  4729. '*/select',
  4730. '-',
  4731. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4732. $daysRegex,
  4733. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4734. date('j', $now),
  4735. '/option',
  4736. '*/select',
  4737. '-',
  4738. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4739. $yearsRegex,
  4740. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4741. date('Y', $now),
  4742. '/option',
  4743. '*/select',
  4744. '/div'
  4745. );
  4746. $this->assertTags($result, $expected);
  4747. $result = $this->Form->input('published', array('type' => 'time'));
  4748. $now = strtotime('now');
  4749. $expected = array(
  4750. 'div' => array('class' => 'input time'),
  4751. 'label' => array('for' => 'ContactPublishedHour'),
  4752. 'Published',
  4753. '/label',
  4754. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4755. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4756. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4757. date('g', $now),
  4758. '/option',
  4759. '*/select',
  4760. ':',
  4761. );
  4762. $this->assertTags($result, $expected);
  4763. $result = $this->Form->input('published', array(
  4764. 'timeFormat' => 24,
  4765. 'interval' => 5,
  4766. 'selected' => strtotime('2009-09-03 13:37:00'),
  4767. 'type' => 'datetime'
  4768. ));
  4769. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4770. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4771. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4772. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4773. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4774. $this->assertNoErrors();
  4775. $this->Form->request->data['Contact'] = array(
  4776. 'date' => array(
  4777. 'day' => '',
  4778. 'month' => '',
  4779. 'year' => '',
  4780. 'hour' => '',
  4781. 'min' => '',
  4782. 'meridian' => ''
  4783. )
  4784. );
  4785. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4786. }
  4787. /**
  4788. * test that datetime() and default values work.
  4789. *
  4790. * @return void
  4791. */
  4792. public function testDatetimeWithDefault() {
  4793. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4794. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4795. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4796. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4797. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4798. 'default' => '2009-06-01 11:15:30'
  4799. ));
  4800. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4801. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4802. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4803. }
  4804. /**
  4805. * test that bogus non-date time data doesn't cause errors.
  4806. *
  4807. * @return void
  4808. */
  4809. public function testDateTimeWithBogusData() {
  4810. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4811. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4812. }
  4813. /**
  4814. * testDateTimeEmptyAsArray
  4815. *
  4816. * @return void
  4817. */
  4818. public function testDateTimeEmptyAsArray() {
  4819. $result = $this->Form->dateTime('Contact.date',
  4820. 'DMY',
  4821. '12',
  4822. array(
  4823. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4824. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4825. )
  4826. )
  4827. );
  4828. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4829. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4830. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4831. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4832. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4833. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4834. $result = $this->Form->dateTime('Contact.date',
  4835. 'DMY',
  4836. '12',
  4837. array(
  4838. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4839. )
  4840. );
  4841. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4842. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4843. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4844. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4845. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4846. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4847. }
  4848. /**
  4849. * testFormDateTimeMulti method
  4850. *
  4851. * test multiple datetime element generation
  4852. *
  4853. * @return void
  4854. */
  4855. public function testFormDateTimeMulti() {
  4856. extract($this->dateRegex);
  4857. $result = $this->Form->dateTime('Contact.1.updated');
  4858. $expected = array(
  4859. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4860. $daysRegex,
  4861. array('option' => array('value' => '')),
  4862. '/option',
  4863. '*/select',
  4864. '-',
  4865. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4866. $monthsRegex,
  4867. array('option' => array('value' => '')),
  4868. '/option',
  4869. '*/select',
  4870. '-',
  4871. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4872. $yearsRegex,
  4873. array('option' => array('value' => '')),
  4874. '/option',
  4875. '*/select',
  4876. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4877. $hoursRegex,
  4878. array('option' => array('value' => '')),
  4879. '/option',
  4880. '*/select',
  4881. ':',
  4882. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4883. $minutesRegex,
  4884. array('option' => array('value' => '')),
  4885. '/option',
  4886. '*/select',
  4887. ' ',
  4888. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4889. $meridianRegex,
  4890. array('option' => array('value' => '')),
  4891. '/option',
  4892. '*/select'
  4893. );
  4894. $this->assertTags($result, $expected);
  4895. $result = $this->Form->dateTime('Contact.2.updated');
  4896. $expected = array(
  4897. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4898. $daysRegex,
  4899. array('option' => array('value' => '')),
  4900. '/option',
  4901. '*/select',
  4902. '-',
  4903. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4904. $monthsRegex,
  4905. array('option' => array('value' => '')),
  4906. '/option',
  4907. '*/select',
  4908. '-',
  4909. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4910. $yearsRegex,
  4911. array('option' => array('value' => '')),
  4912. '/option',
  4913. '*/select',
  4914. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4915. $hoursRegex,
  4916. array('option' => array('value' => '')),
  4917. '/option',
  4918. '*/select',
  4919. ':',
  4920. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4921. $minutesRegex,
  4922. array('option' => array('value' => '')),
  4923. '/option',
  4924. '*/select',
  4925. ' ',
  4926. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4927. $meridianRegex,
  4928. array('option' => array('value' => '')),
  4929. '/option',
  4930. '*/select'
  4931. );
  4932. $this->assertTags($result, $expected);
  4933. }
  4934. /**
  4935. * When changing the date format, the label should always focus the first select box when
  4936. * clicked.
  4937. *
  4938. * @return void
  4939. */
  4940. public function testDateTimeLabelIdMatchesFirstInput() {
  4941. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4942. $this->assertContains('label for="ModelDateMonth"', $result);
  4943. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4944. $this->assertContains('label for="ModelDateDay"', $result);
  4945. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4946. $this->assertContains('label for="ModelDateYear"', $result);
  4947. }
  4948. /**
  4949. * testMonth method
  4950. *
  4951. * @return void
  4952. */
  4953. public function testMonth() {
  4954. $result = $this->Form->month('Model.field');
  4955. $expected = array(
  4956. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4957. array('option' => array('value' => '')),
  4958. '/option',
  4959. array('option' => array('value' => '01')),
  4960. date('F', strtotime('2008-01-01 00:00:00')),
  4961. '/option',
  4962. array('option' => array('value' => '02')),
  4963. date('F', strtotime('2008-02-01 00:00:00')),
  4964. '/option',
  4965. '*/select',
  4966. );
  4967. $this->assertTags($result, $expected);
  4968. $result = $this->Form->month('Model.field', array('empty' => true));
  4969. $expected = array(
  4970. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4971. array('option' => array('value' => '')),
  4972. '/option',
  4973. array('option' => array('value' => '01')),
  4974. date('F', strtotime('2008-01-01 00:00:00')),
  4975. '/option',
  4976. array('option' => array('value' => '02')),
  4977. date('F', strtotime('2008-02-01 00:00:00')),
  4978. '/option',
  4979. '*/select',
  4980. );
  4981. $this->assertTags($result, $expected);
  4982. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4983. $expected = array(
  4984. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4985. array('option' => array('value' => '')),
  4986. '/option',
  4987. array('option' => array('value' => '01')),
  4988. '01',
  4989. '/option',
  4990. array('option' => array('value' => '02')),
  4991. '02',
  4992. '/option',
  4993. '*/select',
  4994. );
  4995. $this->assertTags($result, $expected);
  4996. $monthNames = array(
  4997. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4998. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4999. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5000. $expected = array(
  5001. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5002. array('option' => array('value' => '')),
  5003. '/option',
  5004. array('option' => array('value' => '01')),
  5005. 'Jan',
  5006. '/option',
  5007. array('option' => array('value' => '02')),
  5008. 'Feb',
  5009. '/option',
  5010. '*/select',
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. }
  5014. /**
  5015. * testDay method
  5016. *
  5017. * @return void
  5018. */
  5019. public function testDay() {
  5020. extract($this->dateRegex);
  5021. $result = $this->Form->day('Model.field', array('value' => false));
  5022. $expected = array(
  5023. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5024. array('option' => array('value' => '')),
  5025. '/option',
  5026. array('option' => array('value' => '01')),
  5027. '1',
  5028. '/option',
  5029. array('option' => array('value' => '02')),
  5030. '2',
  5031. '/option',
  5032. $daysRegex,
  5033. '/select',
  5034. );
  5035. $this->assertTags($result, $expected);
  5036. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5037. $result = $this->Form->day('Model.field');
  5038. $expected = array(
  5039. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5040. array('option' => array('value' => '')),
  5041. '/option',
  5042. array('option' => array('value' => '01')),
  5043. '1',
  5044. '/option',
  5045. array('option' => array('value' => '02')),
  5046. '2',
  5047. '/option',
  5048. $daysRegex,
  5049. array('option' => array('value' => '10', 'selected' => 'selected')),
  5050. '10',
  5051. '/option',
  5052. $daysRegex,
  5053. '/select',
  5054. );
  5055. $this->assertTags($result, $expected);
  5056. $this->Form->request->data['Model']['field'] = '';
  5057. $result = $this->Form->day('Model.field', array('value' => '10'));
  5058. $expected = array(
  5059. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5060. array('option' => array('value' => '')),
  5061. '/option',
  5062. array('option' => array('value' => '01')),
  5063. '1',
  5064. '/option',
  5065. array('option' => array('value' => '02')),
  5066. '2',
  5067. '/option',
  5068. $daysRegex,
  5069. array('option' => array('value' => '10', 'selected' => 'selected')),
  5070. '10',
  5071. '/option',
  5072. $daysRegex,
  5073. '/select',
  5074. );
  5075. $this->assertTags($result, $expected);
  5076. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5077. $result = $this->Form->day('Model.field', array('value' => true));
  5078. $expected = array(
  5079. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5080. array('option' => array('value' => '')),
  5081. '/option',
  5082. array('option' => array('value' => '01')),
  5083. '1',
  5084. '/option',
  5085. array('option' => array('value' => '02')),
  5086. '2',
  5087. '/option',
  5088. $daysRegex,
  5089. array('option' => array('value' => '10', 'selected' => 'selected')),
  5090. '10',
  5091. '/option',
  5092. $daysRegex,
  5093. '/select',
  5094. );
  5095. $this->assertTags($result, $expected);
  5096. }
  5097. /**
  5098. * testMinute method
  5099. *
  5100. * @return void
  5101. */
  5102. public function testMinute() {
  5103. extract($this->dateRegex);
  5104. $result = $this->Form->minute('Model.field');
  5105. $expected = array(
  5106. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5107. array('option' => array('value' => '')),
  5108. '/option',
  5109. array('option' => array('value' => '00')),
  5110. '00',
  5111. '/option',
  5112. array('option' => array('value' => '01')),
  5113. '01',
  5114. '/option',
  5115. array('option' => array('value' => '02')),
  5116. '02',
  5117. '/option',
  5118. $minutesRegex,
  5119. '/select',
  5120. );
  5121. $this->assertTags($result, $expected);
  5122. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5123. $result = $this->Form->minute('Model.field');
  5124. $expected = array(
  5125. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5126. array('option' => array('value' => '')),
  5127. '/option',
  5128. array('option' => array('value' => '00')),
  5129. '00',
  5130. '/option',
  5131. array('option' => array('value' => '01')),
  5132. '01',
  5133. '/option',
  5134. array('option' => array('value' => '02')),
  5135. '02',
  5136. '/option',
  5137. $minutesRegex,
  5138. array('option' => array('value' => '12', 'selected' => 'selected')),
  5139. '12',
  5140. '/option',
  5141. $minutesRegex,
  5142. '/select',
  5143. );
  5144. $this->assertTags($result, $expected);
  5145. $this->Form->request->data['Model']['field'] = '';
  5146. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5147. $expected = array(
  5148. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5149. array('option' => array('value' => '')),
  5150. '/option',
  5151. array('option' => array('value' => '00')),
  5152. '00',
  5153. '/option',
  5154. array('option' => array('value' => '05')),
  5155. '05',
  5156. '/option',
  5157. array('option' => array('value' => '10')),
  5158. '10',
  5159. '/option',
  5160. $minutesRegex,
  5161. '/select',
  5162. );
  5163. $this->assertTags($result, $expected);
  5164. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5165. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5166. $expected = array(
  5167. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5168. array('option' => array('value' => '')),
  5169. '/option',
  5170. array('option' => array('value' => '00')),
  5171. '00',
  5172. '/option',
  5173. array('option' => array('value' => '05')),
  5174. '05',
  5175. '/option',
  5176. array('option' => array('value' => '10', 'selected' => 'selected')),
  5177. '10',
  5178. '/option',
  5179. $minutesRegex,
  5180. '/select',
  5181. );
  5182. $this->assertTags($result, $expected);
  5183. }
  5184. /**
  5185. * testHour method
  5186. *
  5187. * @return void
  5188. */
  5189. public function testHour() {
  5190. extract($this->dateRegex);
  5191. $result = $this->Form->hour('Model.field', false);
  5192. $expected = array(
  5193. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5194. array('option' => array('value' => '')),
  5195. '/option',
  5196. array('option' => array('value' => '01')),
  5197. '1',
  5198. '/option',
  5199. array('option' => array('value' => '02')),
  5200. '2',
  5201. '/option',
  5202. $hoursRegex,
  5203. '/select',
  5204. );
  5205. $this->assertTags($result, $expected);
  5206. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5207. $result = $this->Form->hour('Model.field', false);
  5208. $expected = array(
  5209. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5210. array('option' => array('value' => '')),
  5211. '/option',
  5212. array('option' => array('value' => '01')),
  5213. '1',
  5214. '/option',
  5215. array('option' => array('value' => '02')),
  5216. '2',
  5217. '/option',
  5218. $hoursRegex,
  5219. array('option' => array('value' => '12', 'selected' => 'selected')),
  5220. '12',
  5221. '/option',
  5222. '/select',
  5223. );
  5224. $this->assertTags($result, $expected);
  5225. $this->Form->request->data['Model']['field'] = '';
  5226. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5227. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5228. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5229. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5230. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5231. $result = $this->Form->hour('Model.field', true);
  5232. $expected = array(
  5233. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5234. array('option' => array('value' => '')),
  5235. '/option',
  5236. array('option' => array('value' => '00', 'selected' => 'selected')),
  5237. '0',
  5238. '/option',
  5239. array('option' => array('value' => '01')),
  5240. '1',
  5241. '/option',
  5242. array('option' => array('value' => '02')),
  5243. '2',
  5244. '/option',
  5245. $hoursRegex,
  5246. '/select',
  5247. );
  5248. $this->assertTags($result, $expected);
  5249. unset($this->Form->request->data['Model']['field']);
  5250. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5251. $thisHour = date('H');
  5252. $optValue = date('G');
  5253. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5254. }
  5255. /**
  5256. * testYear method
  5257. *
  5258. * @return void
  5259. */
  5260. public function testYear() {
  5261. $result = $this->Form->year('Model.field', 2006, 2007);
  5262. $expected = array(
  5263. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5264. array('option' => array('value' => '')),
  5265. '/option',
  5266. array('option' => array('value' => '2007')),
  5267. '2007',
  5268. '/option',
  5269. array('option' => array('value' => '2006')),
  5270. '2006',
  5271. '/option',
  5272. '/select',
  5273. );
  5274. $this->assertTags($result, $expected);
  5275. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5276. $expected = array(
  5277. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5278. array('option' => array('value' => '')),
  5279. '/option',
  5280. array('option' => array('value' => '2006')),
  5281. '2006',
  5282. '/option',
  5283. array('option' => array('value' => '2007')),
  5284. '2007',
  5285. '/option',
  5286. '/select',
  5287. );
  5288. $this->assertTags($result, $expected);
  5289. $this->request->data['Contact']['published'] = '';
  5290. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5291. $expected = array(
  5292. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5293. array('option' => array('value' => '')),
  5294. '/option',
  5295. array('option' => array('value' => '2007')),
  5296. '2007',
  5297. '/option',
  5298. array('option' => array('value' => '2006')),
  5299. '2006',
  5300. '/option',
  5301. '/select',
  5302. );
  5303. $this->assertTags($result, $expected);
  5304. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5305. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5306. $expected = array(
  5307. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5308. array('option' => array('value' => '2007')),
  5309. '2007',
  5310. '/option',
  5311. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5312. '2006',
  5313. '/option',
  5314. '/select',
  5315. );
  5316. $this->assertTags($result, $expected);
  5317. $this->Form->request->data['Contact']['published'] = '';
  5318. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5319. $expected = array(
  5320. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5321. array('option' => array('value' => '')),
  5322. '/option',
  5323. array('option' => array('value' => '2007')),
  5324. '2007',
  5325. '/option',
  5326. array('option' => array('value' => '2006')),
  5327. '2006',
  5328. '/option',
  5329. '/select',
  5330. );
  5331. $this->assertTags($result, $expected);
  5332. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5333. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5334. $expected = array(
  5335. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5336. array('option' => array('value' => '2007')),
  5337. '2007',
  5338. '/option',
  5339. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5340. '2006',
  5341. '/option',
  5342. '/select',
  5343. );
  5344. $this->assertTags($result, $expected);
  5345. $this->Form->request->data['Contact']['published'] = '';
  5346. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5347. $expected = array(
  5348. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5349. array('option' => array('value' => '')),
  5350. '/option',
  5351. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5352. '2007',
  5353. '/option',
  5354. array('option' => array('value' => '2006')),
  5355. '2006',
  5356. '/option',
  5357. '/select',
  5358. );
  5359. $this->assertTags($result, $expected);
  5360. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5361. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5362. $expected = array(
  5363. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5364. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5365. '2007',
  5366. '/option',
  5367. array('option' => array('value' => '2006')),
  5368. '2006',
  5369. '/option',
  5370. '/select',
  5371. );
  5372. $this->assertTags($result, $expected);
  5373. $this->Form->request->data['Contact']['published'] = '';
  5374. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5375. $expected = array(
  5376. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5377. array('option' => array('value' => '2008')),
  5378. '2008',
  5379. '/option',
  5380. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5381. '2007',
  5382. '/option',
  5383. array('option' => array('value' => '2006')),
  5384. '2006',
  5385. '/option',
  5386. '/select',
  5387. );
  5388. $this->assertTags($result, $expected);
  5389. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5390. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5391. $expected = array(
  5392. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5393. array('option' => array('value' => '2008')),
  5394. '2008',
  5395. '/option',
  5396. array('option' => array('value' => '2007')),
  5397. '2007',
  5398. '/option',
  5399. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5400. '2006',
  5401. '/option',
  5402. '/select',
  5403. );
  5404. $this->assertTags($result, $expected);
  5405. $this->Form->request->data = array();
  5406. $this->Form->create('Contact');
  5407. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5408. $expected = array(
  5409. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5410. array('option' => array('value' => '2008')),
  5411. '2008',
  5412. '/option',
  5413. array('option' => array('value' => '2007')),
  5414. '2007',
  5415. '/option',
  5416. array('option' => array('value' => '2006')),
  5417. '2006',
  5418. '/option',
  5419. '/select',
  5420. );
  5421. $this->assertTags($result, $expected);
  5422. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5423. $this->assertContains('data[Contact][published][year]', $result);
  5424. }
  5425. /**
  5426. * testTextArea method
  5427. *
  5428. * @return void
  5429. */
  5430. public function testTextArea() {
  5431. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5432. $result = $this->Form->textarea('Model.field');
  5433. $expected = array(
  5434. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5435. 'some test data',
  5436. '/textarea',
  5437. );
  5438. $this->assertTags($result, $expected);
  5439. $result = $this->Form->textarea('Model.tmp');
  5440. $expected = array(
  5441. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5442. '/textarea',
  5443. );
  5444. $this->assertTags($result, $expected);
  5445. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5446. $result = $this->Form->textarea('Model.field');
  5447. $expected = array(
  5448. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5449. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5450. '/textarea',
  5451. );
  5452. $this->assertTags($result, $expected);
  5453. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5454. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5455. $expected = array(
  5456. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5457. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5458. '/textarea',
  5459. );
  5460. $this->assertTags($result, $expected);
  5461. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5462. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5463. $expected = array(
  5464. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5465. '/textarea'
  5466. );
  5467. $this->assertTags($result, $expected);
  5468. }
  5469. /**
  5470. * testTextAreaWithStupidCharacters method
  5471. *
  5472. * test text area with non-ascii characters
  5473. *
  5474. * @return void
  5475. */
  5476. public function testTextAreaWithStupidCharacters() {
  5477. $this->loadFixtures('Post');
  5478. $result = $this->Form->input('Post.content', array(
  5479. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5480. ));
  5481. $expected = array(
  5482. 'div' => array('class' => 'input text'),
  5483. 'label' => array('for' => 'PostContent'),
  5484. 'Current Text',
  5485. '/label',
  5486. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5487. 'GREAT®',
  5488. '/textarea',
  5489. '/div'
  5490. );
  5491. $this->assertTags($result, $expected);
  5492. }
  5493. /**
  5494. * testHiddenField method
  5495. *
  5496. * @return void
  5497. */
  5498. public function testHiddenField() {
  5499. $Contact = ClassRegistry::getObject('Contact');
  5500. $Contact->validationErrors['field'] = 1;
  5501. $this->Form->request->data['Contact']['field'] = 'test';
  5502. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5503. $this->assertTags($result, array(
  5504. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5505. );
  5506. }
  5507. /**
  5508. * testFileUploadField method
  5509. *
  5510. * @return void
  5511. */
  5512. public function testFileUploadField() {
  5513. $result = $this->Form->file('Model.upload');
  5514. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5515. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5516. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5517. $expected = array(
  5518. 'div' => array('class' => 'input file'),
  5519. 'label' => array('for' => 'ModelUpload'),
  5520. 'Upload',
  5521. '/label',
  5522. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5523. '/div'
  5524. );
  5525. $this->assertTags($result, $expected);
  5526. }
  5527. /**
  5528. * test File upload input on a model not used in create();
  5529. *
  5530. * @return void
  5531. */
  5532. public function testFileUploadOnOtherModel() {
  5533. $this->Form->create('ValidateUser', array('type' => 'file'));
  5534. $result = $this->Form->file('ValidateProfile.city');
  5535. $expected = array(
  5536. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5537. );
  5538. $this->assertTags($result, $expected);
  5539. }
  5540. /**
  5541. * testButton method
  5542. *
  5543. * @return void
  5544. */
  5545. public function testButton() {
  5546. $result = $this->Form->button('Hi');
  5547. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5548. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5549. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5550. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5551. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5552. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5553. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5554. $result = $this->Form->button('No type', array('type' => false));
  5555. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5556. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5557. $this->assertNotRegExp('/\&039/', $result);
  5558. }
  5559. /**
  5560. * Test that button() makes unlocked fields by default.
  5561. *
  5562. * @return void
  5563. */
  5564. public function testButtonUnlockedByDefault() {
  5565. $this->Form->request->params['_Token']['key'] = 'secured';
  5566. $this->Form->button('Save', array('name' => 'save'));
  5567. $this->Form->button('Clear');
  5568. $result = $this->Form->unlockField();
  5569. $this->assertEquals(array('save'), $result);
  5570. }
  5571. /**
  5572. * testPostButton method
  5573. *
  5574. * @return void
  5575. */
  5576. public function testPostButton() {
  5577. $result = $this->Form->postButton('Hi', '/controller/action');
  5578. $this->assertTags($result, array(
  5579. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5580. 'div' => array('style' => 'display:none;'),
  5581. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5582. '/div',
  5583. 'button' => array('type' => 'submit'),
  5584. 'Hi',
  5585. '/button',
  5586. '/form'
  5587. ));
  5588. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5589. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5590. }
  5591. /**
  5592. * Test that postButton adds _Token fields.
  5593. *
  5594. * @return void
  5595. */
  5596. public function testSecurePostButton() {
  5597. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5598. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5599. $expected = array(
  5600. 'form' => array(
  5601. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5602. ),
  5603. array('div' => array('style' => 'display:none;')),
  5604. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5605. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5606. '/div',
  5607. 'button' => array('type' => 'submit'),
  5608. 'Delete',
  5609. '/button',
  5610. array('div' => array('style' => 'display:none;')),
  5611. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5612. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5613. '/div',
  5614. '/form',
  5615. );
  5616. $this->assertTags($result, $expected);
  5617. }
  5618. /**
  5619. * testPostLink method
  5620. *
  5621. * @return void
  5622. */
  5623. public function testPostLink() {
  5624. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5625. $this->assertTags($result, array(
  5626. 'form' => array(
  5627. 'method' => 'post', 'action' => '/posts/delete/1',
  5628. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5629. ),
  5630. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5631. '/form',
  5632. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5633. 'Delete',
  5634. '/a'
  5635. ));
  5636. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5637. $this->assertTags($result, array(
  5638. 'form' => array(
  5639. 'method' => 'post', 'action' => '/posts/delete/1',
  5640. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5641. ),
  5642. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5643. '/form',
  5644. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5645. 'Delete',
  5646. '/a'
  5647. ));
  5648. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5649. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5650. }
  5651. /**
  5652. * Test that postLink adds _Token fields.
  5653. *
  5654. * @return void
  5655. */
  5656. public function testSecurePostLink() {
  5657. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5658. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5659. $expected = array(
  5660. 'form' => array(
  5661. 'method' => 'post', 'action' => '/posts/delete/1',
  5662. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5663. ),
  5664. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5665. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5666. 'div' => array('style' => 'display:none;'),
  5667. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5668. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5669. '/div',
  5670. '/form',
  5671. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5672. 'Delete',
  5673. '/a'
  5674. );
  5675. $this->assertTags($result, $expected);
  5676. }
  5677. /**
  5678. * testSubmitButton method
  5679. *
  5680. * @return void
  5681. */
  5682. public function testSubmitButton() {
  5683. $result = $this->Form->submit('');
  5684. $expected = array(
  5685. 'div' => array('class' => 'submit'),
  5686. 'input' => array('type' => 'submit', 'value' => ''),
  5687. '/div'
  5688. );
  5689. $this->assertTags($result, $expected);
  5690. $result = $this->Form->submit('Test Submit');
  5691. $expected = array(
  5692. 'div' => array('class' => 'submit'),
  5693. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5694. '/div'
  5695. );
  5696. $this->assertTags($result, $expected);
  5697. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5698. $expected = array(
  5699. 'span' => array('class' => 'submit'),
  5700. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5701. '/span'
  5702. );
  5703. $this->assertTags($result, $expected);
  5704. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5705. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5706. $this->assertTags($result, $expected);
  5707. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5708. $expected = array(
  5709. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5710. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5711. '/div'
  5712. );
  5713. $this->assertTags($result, $expected);
  5714. $result = $this->Form->submit('Next >');
  5715. $expected = array(
  5716. 'div' => array('class' => 'submit'),
  5717. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5718. '/div'
  5719. );
  5720. $this->assertTags($result, $expected);
  5721. $result = $this->Form->submit('Next >', array('escape' => false));
  5722. $expected = array(
  5723. 'div' => array('class' => 'submit'),
  5724. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5725. '/div'
  5726. );
  5727. $this->assertTags($result, $expected);
  5728. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5729. $expected = array(
  5730. 'div' => array('class' => 'submit'),
  5731. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5732. '/div'
  5733. );
  5734. $this->assertTags($result, $expected);
  5735. $before = '--before--';
  5736. $after = '--after--';
  5737. $result = $this->Form->submit('Test', array('before' => $before));
  5738. $expected = array(
  5739. 'div' => array('class' => 'submit'),
  5740. '--before--',
  5741. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5742. '/div'
  5743. );
  5744. $this->assertTags($result, $expected);
  5745. $result = $this->Form->submit('Test', array('after' => $after));
  5746. $expected = array(
  5747. 'div' => array('class' => 'submit'),
  5748. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5749. '--after--',
  5750. '/div'
  5751. );
  5752. $this->assertTags($result, $expected);
  5753. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5754. $expected = array(
  5755. 'div' => array('class' => 'submit'),
  5756. '--before--',
  5757. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5758. '--after--',
  5759. '/div'
  5760. );
  5761. $this->assertTags($result, $expected);
  5762. }
  5763. /**
  5764. * test image submit types.
  5765. *
  5766. * @return void
  5767. */
  5768. public function testSubmitImage() {
  5769. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5770. $expected = array(
  5771. 'div' => array('class' => 'submit'),
  5772. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5773. '/div'
  5774. );
  5775. $this->assertTags($result, $expected);
  5776. $result = $this->Form->submit('/relative/cake.power.gif');
  5777. $expected = array(
  5778. 'div' => array('class' => 'submit'),
  5779. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5780. '/div'
  5781. );
  5782. $this->assertTags($result, $expected);
  5783. $result = $this->Form->submit('cake.power.gif');
  5784. $expected = array(
  5785. 'div' => array('class' => 'submit'),
  5786. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5787. '/div'
  5788. );
  5789. $this->assertTags($result, $expected);
  5790. $result = $this->Form->submit('Not.an.image');
  5791. $expected = array(
  5792. 'div' => array('class' => 'submit'),
  5793. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5794. '/div'
  5795. );
  5796. $this->assertTags($result, $expected);
  5797. $after = '--after--';
  5798. $before = '--before--';
  5799. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5800. $expected = array(
  5801. 'div' => array('class' => 'submit'),
  5802. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5803. '--after--',
  5804. '/div'
  5805. );
  5806. $this->assertTags($result, $expected);
  5807. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5808. $expected = array(
  5809. 'div' => array('class' => 'submit'),
  5810. '--before--',
  5811. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5812. '/div'
  5813. );
  5814. $this->assertTags($result, $expected);
  5815. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5816. $expected = array(
  5817. 'div' => array('class' => 'submit'),
  5818. '--before--',
  5819. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5820. '--after--',
  5821. '/div'
  5822. );
  5823. $this->assertTags($result, $expected);
  5824. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5825. $expected = array(
  5826. 'div' => array('class' => 'submit'),
  5827. '--before--',
  5828. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5829. '--after--',
  5830. '/div'
  5831. );
  5832. $this->assertTags($result, $expected);
  5833. }
  5834. /**
  5835. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5836. * be submitted at a time.
  5837. *
  5838. * @return void
  5839. */
  5840. public function testSubmitUnlockedByDefault() {
  5841. $this->Form->request->params['_Token']['key'] = 'secured';
  5842. $this->Form->submit('Go go');
  5843. $this->Form->submit('Save', array('name' => 'save'));
  5844. $result = $this->Form->unlockField();
  5845. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5846. }
  5847. /**
  5848. * Test submit image with timestamps.
  5849. *
  5850. * @return void
  5851. */
  5852. public function testSubmitImageTimestamp() {
  5853. Configure::write('Asset.timestamp', 'force');
  5854. $result = $this->Form->submit('cake.power.gif');
  5855. $expected = array(
  5856. 'div' => array('class' => 'submit'),
  5857. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5858. '/div'
  5859. );
  5860. $this->assertTags($result, $expected);
  5861. }
  5862. /**
  5863. * test the create() method
  5864. *
  5865. * @return void
  5866. */
  5867. public function testCreate() {
  5868. $result = $this->Form->create('Contact');
  5869. $encoding = strtolower(Configure::read('App.encoding'));
  5870. $expected = array(
  5871. 'form' => array(
  5872. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5873. 'accept-charset' => $encoding
  5874. ),
  5875. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5876. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5877. '/div'
  5878. );
  5879. $this->assertTags($result, $expected);
  5880. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5881. $expected = array('form' => array(
  5882. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5883. 'accept-charset' => $encoding
  5884. ));
  5885. $this->assertTags($result, $expected);
  5886. $result = $this->Form->create('Contact', array('type' => 'get'));
  5887. $expected = array('form' => array(
  5888. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5889. 'accept-charset' => $encoding
  5890. ));
  5891. $this->assertTags($result, $expected);
  5892. $result = $this->Form->create('Contact', array('type' => 'put'));
  5893. $expected = array(
  5894. 'form' => array(
  5895. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5896. 'accept-charset' => $encoding
  5897. ),
  5898. 'div' => array('style' => 'display:none;'),
  5899. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5900. '/div'
  5901. );
  5902. $this->assertTags($result, $expected);
  5903. $result = $this->Form->create('Contact', array('type' => 'file'));
  5904. $expected = array(
  5905. 'form' => array(
  5906. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5907. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5908. ),
  5909. 'div' => array('style' => 'display:none;'),
  5910. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5911. '/div'
  5912. );
  5913. $this->assertTags($result, $expected);
  5914. $this->Form->request->data['Contact']['id'] = 1;
  5915. $this->Form->request->here = '/contacts/edit/1';
  5916. $this->Form->request['action'] = 'edit';
  5917. $result = $this->Form->create('Contact');
  5918. $expected = array(
  5919. 'form' => array(
  5920. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5921. 'accept-charset' => $encoding
  5922. ),
  5923. 'div' => array('style' => 'display:none;'),
  5924. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5925. '/div'
  5926. );
  5927. $this->assertTags($result, $expected);
  5928. $this->Form->request->data['Contact']['id'] = 1;
  5929. $this->Form->request->here = '/contacts/edit/1';
  5930. $this->Form->request['action'] = 'edit';
  5931. $result = $this->Form->create('Contact', array('type' => 'file'));
  5932. $expected = array(
  5933. 'form' => array(
  5934. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5935. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5936. ),
  5937. 'div' => array('style' => 'display:none;'),
  5938. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5939. '/div'
  5940. );
  5941. $this->assertTags($result, $expected);
  5942. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5943. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5944. $expected = array(
  5945. 'form' => array(
  5946. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5947. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5948. ),
  5949. 'div' => array('style' => 'display:none;'),
  5950. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5951. '/div'
  5952. );
  5953. $this->assertTags($result, $expected);
  5954. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5955. $expected = array(
  5956. 'form' => array(
  5957. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5958. 'accept-charset' => $encoding
  5959. ),
  5960. 'div' => array('style' => 'display:none;'),
  5961. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5962. '/div'
  5963. );
  5964. $this->assertTags($result, $expected);
  5965. $this->Form->request['action'] = 'add';
  5966. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5967. $expected = array(
  5968. 'form' => array(
  5969. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5970. 'accept-charset' => $encoding
  5971. ),
  5972. 'div' => array('style' => 'display:none;'),
  5973. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. $result = $this->Form->create('User', array('action' => 'login'));
  5978. $expected = array(
  5979. 'form' => array(
  5980. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5981. 'accept-charset' => $encoding
  5982. ),
  5983. 'div' => array('style' => 'display:none;'),
  5984. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5985. '/div'
  5986. );
  5987. $this->assertTags($result, $expected);
  5988. $result = $this->Form->create('User', array('url' => '/users/login'));
  5989. $expected = array(
  5990. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5991. 'div' => array('style' => 'display:none;'),
  5992. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5993. '/div'
  5994. );
  5995. $this->assertTags($result, $expected);
  5996. $this->Form->request['controller'] = 'pages';
  5997. $result = $this->Form->create('User', array('action' => 'signup'));
  5998. $expected = array(
  5999. 'form' => array(
  6000. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6001. 'accept-charset' => $encoding
  6002. ),
  6003. 'div' => array('style' => 'display:none;'),
  6004. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6005. '/div'
  6006. );
  6007. $this->assertTags($result, $expected);
  6008. $this->Form->request->data = array();
  6009. $this->Form->request['controller'] = 'contacts';
  6010. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6011. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6012. $expected = array(
  6013. 'form' => array(
  6014. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6015. 'accept-charset' => 'utf-8'
  6016. ),
  6017. 'div' => array('style' => 'display:none;'),
  6018. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6019. '/div'
  6020. );
  6021. $this->assertTags($result, $expected);
  6022. }
  6023. /**
  6024. * Test the onsubmit option for create()
  6025. *
  6026. * @return void
  6027. */
  6028. public function testCreateOnSubmit() {
  6029. $this->Form->request->data = array();
  6030. $this->Form->request['controller'] = 'contacts';
  6031. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6032. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6033. $expected = array(
  6034. 'form' => array(
  6035. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6036. 'accept-charset' => 'utf-8'
  6037. ),
  6038. 'div' => array('style' => 'display:none;'),
  6039. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6040. '/div'
  6041. );
  6042. $this->assertTags($result, $expected);
  6043. $this->Form->request->data = array();
  6044. $this->Form->request['controller'] = 'contacts';
  6045. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6046. $result = $this->Form->create(array(
  6047. 'url' => array('action' => 'index', 'param'),
  6048. 'default' => false,
  6049. 'onsubmit' => 'someFunction();'
  6050. ));
  6051. $expected = array(
  6052. 'form' => array(
  6053. 'id' => 'ContactAddForm', 'method' => 'post',
  6054. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6055. 'action' => '/contacts/index/param',
  6056. 'accept-charset' => 'utf-8'
  6057. ),
  6058. 'div' => array('style' => 'display:none;'),
  6059. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6060. '/div'
  6061. );
  6062. $this->assertTags($result, $expected);
  6063. }
  6064. /**
  6065. * test create() with automatic url generation
  6066. *
  6067. * @return void
  6068. */
  6069. public function testCreateAutoUrl() {
  6070. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6071. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6072. $this->Form->request->base = '/base_url';
  6073. $result = $this->Form->create('Contact');
  6074. $expected = array(
  6075. 'form' => array(
  6076. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6077. 'accept-charset' => 'utf-8'
  6078. ),
  6079. 'div' => array('style' => 'display:none;'),
  6080. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6081. '/div'
  6082. );
  6083. $this->assertTags($result, $expected);
  6084. $this->Form->request['action'] = 'delete';
  6085. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6086. $this->Form->request->base = '/base_url';
  6087. $result = $this->Form->create('Contact');
  6088. $expected = array(
  6089. 'form' => array(
  6090. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6091. 'accept-charset' => 'utf-8'
  6092. ),
  6093. 'div' => array('style' => 'display:none;'),
  6094. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6095. '/div'
  6096. );
  6097. $this->assertTags($result, $expected);
  6098. }
  6099. /**
  6100. * test create() with a custom route
  6101. *
  6102. * @return void
  6103. */
  6104. public function testCreateCustomRoute() {
  6105. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6106. $encoding = strtolower(Configure::read('App.encoding'));
  6107. $result = $this->Form->create('User', array('action' => 'login'));
  6108. $expected = array(
  6109. 'form' => array(
  6110. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6111. 'accept-charset' => $encoding
  6112. ),
  6113. 'div' => array('style' => 'display:none;'),
  6114. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6115. '/div'
  6116. );
  6117. $this->assertTags($result, $expected);
  6118. }
  6119. /**
  6120. * test that inputDefaults are stored and used.
  6121. *
  6122. * @return void
  6123. */
  6124. public function testCreateWithInputDefaults() {
  6125. $this->Form->create('User', array(
  6126. 'inputDefaults' => array(
  6127. 'div' => false,
  6128. 'label' => false,
  6129. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6130. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6131. )
  6132. ));
  6133. $result = $this->Form->input('username');
  6134. $expected = array(
  6135. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6136. );
  6137. $this->assertTags($result, $expected);
  6138. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6139. $expected = array(
  6140. 'div' => array('class' => 'input text'),
  6141. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6142. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6143. '/div'
  6144. );
  6145. $this->assertTags($result, $expected);
  6146. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6147. $expected = array(
  6148. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6149. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6150. );
  6151. $this->assertTags($result, $expected);
  6152. $this->Form->create('User', array(
  6153. 'inputDefaults' => array(
  6154. 'div' => false,
  6155. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6156. )
  6157. ));
  6158. $result = $this->Form->input('username', array('div' => true));
  6159. $expected = array(
  6160. 'div' => array('class' => 'input text'),
  6161. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6162. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6163. '/div'
  6164. );
  6165. $this->assertTags($result, $expected);
  6166. }
  6167. /**
  6168. * test automatic accept-charset overriding
  6169. *
  6170. * @return void
  6171. */
  6172. public function testCreateWithAcceptCharset() {
  6173. $result = $this->Form->create('UserForm', array(
  6174. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6175. )
  6176. );
  6177. $expected = array(
  6178. 'form' => array(
  6179. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6180. 'accept-charset' => 'iso-8859-1'
  6181. ),
  6182. 'div' => array('style' => 'display:none;'),
  6183. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6184. '/div'
  6185. );
  6186. $this->assertTags($result, $expected);
  6187. }
  6188. /**
  6189. * Test base form url when url param is passed with multiple parameters (&)
  6190. *
  6191. */
  6192. public function testCreateQuerystringrequest() {
  6193. $encoding = strtolower(Configure::read('App.encoding'));
  6194. $result = $this->Form->create('Contact', array(
  6195. 'type' => 'post',
  6196. 'escape' => false,
  6197. 'url' => array(
  6198. 'controller' => 'controller',
  6199. 'action' => 'action',
  6200. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6201. )
  6202. ));
  6203. $expected = array(
  6204. 'form' => array(
  6205. 'id' => 'ContactAddForm',
  6206. 'method' => 'post',
  6207. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6208. 'accept-charset' => $encoding
  6209. ),
  6210. 'div' => array('style' => 'display:none;'),
  6211. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6212. '/div'
  6213. );
  6214. $this->assertTags($result, $expected);
  6215. $result = $this->Form->create('Contact', array(
  6216. 'type' => 'post',
  6217. 'url' => array(
  6218. 'controller' => 'controller',
  6219. 'action' => 'action',
  6220. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6221. )
  6222. ));
  6223. $expected = array(
  6224. 'form' => array(
  6225. 'id' => 'ContactAddForm',
  6226. 'method' => 'post',
  6227. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6228. 'accept-charset' => $encoding
  6229. ),
  6230. 'div' => array('style' => 'display:none;'),
  6231. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6232. '/div'
  6233. );
  6234. $this->assertTags($result, $expected);
  6235. }
  6236. /**
  6237. * test that create() doesn't cause errors by multiple id's being in the primary key
  6238. * as could happen with multiple select or checkboxes.
  6239. *
  6240. * @return void
  6241. */
  6242. public function testCreateWithMultipleIdInData() {
  6243. $encoding = strtolower(Configure::read('App.encoding'));
  6244. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6245. $result = $this->Form->create('Contact');
  6246. $expected = array(
  6247. 'form' => array(
  6248. 'id' => 'ContactAddForm',
  6249. 'method' => 'post',
  6250. 'action' => '/contacts/add',
  6251. 'accept-charset' => $encoding
  6252. ),
  6253. 'div' => array('style' => 'display:none;'),
  6254. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6255. '/div'
  6256. );
  6257. $this->assertTags($result, $expected);
  6258. }
  6259. /**
  6260. * test that create() doesn't add in extra passed params.
  6261. *
  6262. * @return void
  6263. */
  6264. public function testCreatePassedArgs() {
  6265. $encoding = strtolower(Configure::read('App.encoding'));
  6266. $this->Form->request->data['Contact']['id'] = 1;
  6267. $result = $this->Form->create('Contact', array(
  6268. 'type' => 'post',
  6269. 'escape' => false,
  6270. 'url' => array(
  6271. 'action' => 'edit',
  6272. 'myparam'
  6273. )
  6274. ));
  6275. $expected = array(
  6276. 'form' => array(
  6277. 'id' => 'ContactAddForm',
  6278. 'method' => 'post',
  6279. 'action' => '/contacts/edit/myparam',
  6280. 'accept-charset' => $encoding
  6281. ),
  6282. 'div' => array('style' => 'display:none;'),
  6283. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6284. '/div'
  6285. );
  6286. $this->assertTags($result, $expected);
  6287. }
  6288. /**
  6289. * test creating a get form, and get form inputs.
  6290. *
  6291. * @return void
  6292. */
  6293. public function testGetFormCreate() {
  6294. $encoding = strtolower(Configure::read('App.encoding'));
  6295. $result = $this->Form->create('Contact', array('type' => 'get'));
  6296. $this->assertTags($result, array('form' => array(
  6297. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6298. 'accept-charset' => $encoding
  6299. )));
  6300. $result = $this->Form->text('Contact.name');
  6301. $this->assertTags($result, array('input' => array(
  6302. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6303. )));
  6304. $result = $this->Form->password('password');
  6305. $this->assertTags($result, array('input' => array(
  6306. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6307. )));
  6308. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6309. $result = $this->Form->text('user_form');
  6310. $this->assertTags($result, array('input' => array(
  6311. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6312. )));
  6313. }
  6314. /**
  6315. * test get form, and inputs when the model param is false
  6316. *
  6317. * @return void
  6318. */
  6319. public function testGetFormWithFalseModel() {
  6320. $encoding = strtolower(Configure::read('App.encoding'));
  6321. $this->Form->request['controller'] = 'contact_test';
  6322. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6323. $expected = array('form' => array(
  6324. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6325. 'accept-charset' => $encoding
  6326. ));
  6327. $this->assertTags($result, $expected);
  6328. $result = $this->Form->text('reason');
  6329. $expected = array(
  6330. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6331. );
  6332. $this->assertTags($result, $expected);
  6333. }
  6334. /**
  6335. * test that datetime() works with GET style forms.
  6336. *
  6337. * @return void
  6338. */
  6339. public function testDateTimeWithGetForms() {
  6340. extract($this->dateRegex);
  6341. $this->Form->create('Contact', array('type' => 'get'));
  6342. $result = $this->Form->datetime('created');
  6343. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6344. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6345. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6346. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6347. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6348. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6349. }
  6350. /**
  6351. * testEditFormWithData method
  6352. *
  6353. * test auto populating form elements from submitted data.
  6354. *
  6355. * @return void
  6356. */
  6357. public function testEditFormWithData() {
  6358. $this->Form->request->data = array('Person' => array(
  6359. 'id' => 1,
  6360. 'first_name' => 'Nate',
  6361. 'last_name' => 'Abele',
  6362. 'email' => 'nate@example.com'
  6363. ));
  6364. $this->Form->request->addParams(array(
  6365. 'models' => array('Person'),
  6366. 'controller' => 'people',
  6367. 'action' => 'add'
  6368. ));
  6369. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6370. $this->Form->create();
  6371. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6372. $expected = array(
  6373. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6374. 'select' => array(
  6375. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6376. ),
  6377. array('option' => array('value' => 1)), 'Nate', '/option',
  6378. array('option' => array('value' => 2)), 'Garrett', '/option',
  6379. array('option' => array('value' => 3)), 'Larry', '/option',
  6380. '/select'
  6381. );
  6382. $this->assertTags($result, $expected);
  6383. }
  6384. /**
  6385. * Test that required fields are created for various types of validation.
  6386. *
  6387. * @return void
  6388. */
  6389. public function testFormInputRequiredDetection() {
  6390. $this->Form->create('Contact');
  6391. $result = $this->Form->input('Contact.non_existing');
  6392. $expected = array(
  6393. 'div' => array('class' => 'input text'),
  6394. 'label' => array('for' => 'ContactNonExisting'),
  6395. 'Non Existing',
  6396. '/label',
  6397. 'input' => array(
  6398. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6399. 'id' => 'ContactNonExisting'
  6400. ),
  6401. '/div'
  6402. );
  6403. $this->assertTags($result, $expected);
  6404. $result = $this->Form->input('Contact.imrequired');
  6405. $expected = array(
  6406. 'div' => array('class' => 'input text required'),
  6407. 'label' => array('for' => 'ContactImrequired'),
  6408. 'Imrequired',
  6409. '/label',
  6410. 'input' => array(
  6411. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6412. 'id' => 'ContactImrequired'
  6413. ),
  6414. '/div'
  6415. );
  6416. $this->assertTags($result, $expected);
  6417. $result = $this->Form->input('Contact.imalsorequired');
  6418. $expected = array(
  6419. 'div' => array('class' => 'input text required'),
  6420. 'label' => array('for' => 'ContactImalsorequired'),
  6421. 'Imalsorequired',
  6422. '/label',
  6423. 'input' => array(
  6424. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6425. 'id' => 'ContactImalsorequired'
  6426. ),
  6427. '/div'
  6428. );
  6429. $this->assertTags($result, $expected);
  6430. $result = $this->Form->input('Contact.imrequiredtoo');
  6431. $expected = array(
  6432. 'div' => array('class' => 'input text required'),
  6433. 'label' => array('for' => 'ContactImrequiredtoo'),
  6434. 'Imrequiredtoo',
  6435. '/label',
  6436. 'input' => array(
  6437. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6438. 'id' => 'ContactImrequiredtoo'
  6439. ),
  6440. '/div'
  6441. );
  6442. $this->assertTags($result, $expected);
  6443. $result = $this->Form->input('Contact.required_one');
  6444. $expected = array(
  6445. 'div' => array('class' => 'input text required'),
  6446. 'label' => array('for' => 'ContactRequiredOne'),
  6447. 'Required One',
  6448. '/label',
  6449. 'input' => array(
  6450. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6451. 'id' => 'ContactRequiredOne'
  6452. ),
  6453. '/div'
  6454. );
  6455. $this->assertTags($result, $expected);
  6456. $result = $this->Form->input('Contact.string_required');
  6457. $expected = array(
  6458. 'div' => array('class' => 'input text required'),
  6459. 'label' => array('for' => 'ContactStringRequired'),
  6460. 'String Required',
  6461. '/label',
  6462. 'input' => array(
  6463. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6464. 'id' => 'ContactStringRequired'
  6465. ),
  6466. '/div'
  6467. );
  6468. $this->assertTags($result, $expected);
  6469. $result = $this->Form->input('Contact.imnotrequired');
  6470. $expected = array(
  6471. 'div' => array('class' => 'input text'),
  6472. 'label' => array('for' => 'ContactImnotrequired'),
  6473. 'Imnotrequired',
  6474. '/label',
  6475. 'input' => array(
  6476. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6477. 'id' => 'ContactImnotrequired'
  6478. ),
  6479. '/div'
  6480. );
  6481. $this->assertTags($result, $expected);
  6482. $result = $this->Form->input('Contact.imalsonotrequired');
  6483. $expected = array(
  6484. 'div' => array('class' => 'input text'),
  6485. 'label' => array('for' => 'ContactImalsonotrequired'),
  6486. 'Imalsonotrequired',
  6487. '/label',
  6488. 'input' => array(
  6489. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6490. 'id' => 'ContactImalsonotrequired'
  6491. ),
  6492. '/div'
  6493. );
  6494. $this->assertTags($result, $expected);
  6495. $result = $this->Form->input('Contact.imalsonotrequired2');
  6496. $expected = array(
  6497. 'div' => array('class' => 'input text'),
  6498. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6499. 'Imalsonotrequired2',
  6500. '/label',
  6501. 'input' => array(
  6502. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6503. 'id' => 'ContactImalsonotrequired2'
  6504. ),
  6505. '/div'
  6506. );
  6507. $this->assertTags($result, $expected);
  6508. $result = $this->Form->input('Contact.imnotrequiredeither');
  6509. $expected = array(
  6510. 'div' => array('class' => 'input text'),
  6511. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6512. 'Imnotrequiredeither',
  6513. '/label',
  6514. 'input' => array(
  6515. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6516. 'id' => 'ContactImnotrequiredeither'
  6517. ),
  6518. '/div'
  6519. );
  6520. $this->assertTags($result, $expected);
  6521. $result = $this->Form->input('Contact.iamrequiredalways');
  6522. $expected = array(
  6523. 'div' => array('class' => 'input text required'),
  6524. 'label' => array('for' => 'ContactIamrequiredalways'),
  6525. 'Iamrequiredalways',
  6526. '/label',
  6527. 'input' => array(
  6528. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6529. 'id' => 'ContactIamrequiredalways'
  6530. ),
  6531. '/div'
  6532. );
  6533. $this->assertTags($result, $expected);
  6534. }
  6535. /**
  6536. * testFormMagicInput method
  6537. *
  6538. * @return void
  6539. */
  6540. public function testFormMagicInput() {
  6541. $encoding = strtolower(Configure::read('App.encoding'));
  6542. $result = $this->Form->create('Contact');
  6543. $expected = array(
  6544. 'form' => array(
  6545. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6546. 'accept-charset' => $encoding
  6547. ),
  6548. 'div' => array('style' => 'display:none;'),
  6549. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6550. '/div'
  6551. );
  6552. $this->assertTags($result, $expected);
  6553. $result = $this->Form->input('name');
  6554. $expected = array(
  6555. 'div' => array('class' => 'input text'),
  6556. 'label' => array('for' => 'ContactName'),
  6557. 'Name',
  6558. '/label',
  6559. 'input' => array(
  6560. 'type' => 'text', 'name' => 'data[Contact][name]',
  6561. 'id' => 'ContactName', 'maxlength' => '255'
  6562. ),
  6563. '/div'
  6564. );
  6565. $this->assertTags($result, $expected);
  6566. $result = $this->Form->input('non_existing_field_in_contact_model');
  6567. $expected = array(
  6568. 'div' => array('class' => 'input text'),
  6569. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6570. 'Non Existing Field In Contact Model',
  6571. '/label',
  6572. 'input' => array(
  6573. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6574. 'id' => 'ContactNonExistingFieldInContactModel'
  6575. ),
  6576. '/div'
  6577. );
  6578. $this->assertTags($result, $expected);
  6579. $result = $this->Form->input('Address.street');
  6580. $expected = array(
  6581. 'div' => array('class' => 'input text'),
  6582. 'label' => array('for' => 'AddressStreet'),
  6583. 'Street',
  6584. '/label',
  6585. 'input' => array(
  6586. 'type' => 'text', 'name' => 'data[Address][street]',
  6587. 'id' => 'AddressStreet'
  6588. ),
  6589. '/div'
  6590. );
  6591. $this->assertTags($result, $expected);
  6592. $result = $this->Form->input('Address.non_existing_field_in_model');
  6593. $expected = array(
  6594. 'div' => array('class' => 'input text'),
  6595. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6596. 'Non Existing Field In Model',
  6597. '/label',
  6598. 'input' => array(
  6599. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6600. 'id' => 'AddressNonExistingFieldInModel'
  6601. ),
  6602. '/div'
  6603. );
  6604. $this->assertTags($result, $expected);
  6605. $result = $this->Form->input('name', array('div' => false));
  6606. $expected = array(
  6607. 'label' => array('for' => 'ContactName'),
  6608. 'Name',
  6609. '/label',
  6610. 'input' => array(
  6611. 'type' => 'text', 'name' => 'data[Contact][name]',
  6612. 'id' => 'ContactName', 'maxlength' => '255'
  6613. )
  6614. );
  6615. $this->assertTags($result, $expected);
  6616. extract($this->dateRegex);
  6617. $now = strtotime('now');
  6618. $result = $this->Form->input('Contact.published', array('div' => false));
  6619. $expected = array(
  6620. 'label' => array('for' => 'ContactPublishedMonth'),
  6621. 'Published',
  6622. '/label',
  6623. array('select' => array(
  6624. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6625. )),
  6626. $monthsRegex,
  6627. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6628. date('F', $now),
  6629. '/option',
  6630. '*/select',
  6631. '-',
  6632. array('select' => array(
  6633. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6634. )),
  6635. $daysRegex,
  6636. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6637. date('j', $now),
  6638. '/option',
  6639. '*/select',
  6640. '-',
  6641. array('select' => array(
  6642. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6643. )),
  6644. $yearsRegex,
  6645. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6646. date('Y', $now),
  6647. '*/select'
  6648. );
  6649. $this->assertTags($result, $expected);
  6650. $result = $this->Form->input('Contact.updated', array('div' => false));
  6651. $expected = array(
  6652. 'label' => array('for' => 'ContactUpdatedMonth'),
  6653. 'Updated',
  6654. '/label',
  6655. array('select' => array(
  6656. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6657. )),
  6658. $monthsRegex,
  6659. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6660. date('F', $now),
  6661. '/option',
  6662. '*/select',
  6663. '-',
  6664. array('select' => array(
  6665. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6666. )),
  6667. $daysRegex,
  6668. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6669. date('j', $now),
  6670. '/option',
  6671. '*/select',
  6672. '-',
  6673. array('select' => array(
  6674. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6675. )),
  6676. $yearsRegex,
  6677. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6678. date('Y', $now),
  6679. '*/select'
  6680. );
  6681. $this->assertTags($result, $expected);
  6682. $result = $this->Form->input('UserForm.stuff');
  6683. $expected = array(
  6684. 'div' => array('class' => 'input text'),
  6685. 'label' => array('for' => 'UserFormStuff'),
  6686. 'Stuff',
  6687. '/label',
  6688. 'input' => array(
  6689. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6690. 'id' => 'UserFormStuff', 'maxlength' => 10
  6691. ),
  6692. '/div'
  6693. );
  6694. $this->assertTags($result, $expected);
  6695. }
  6696. /**
  6697. * testForMagicInputNonExistingNorValidated method
  6698. *
  6699. * @return void
  6700. */
  6701. public function testForMagicInputNonExistingNorValidated() {
  6702. $encoding = strtolower(Configure::read('App.encoding'));
  6703. $result = $this->Form->create('Contact');
  6704. $expected = array(
  6705. 'form' => array(
  6706. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6707. 'accept-charset' => $encoding
  6708. ),
  6709. 'div' => array('style' => 'display:none;'),
  6710. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6711. '/div'
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6715. $expected = array(
  6716. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6717. 'Non Existing Nor Validated',
  6718. '/label',
  6719. 'input' => array(
  6720. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6721. 'id' => 'ContactNonExistingNorValidated'
  6722. )
  6723. );
  6724. $this->assertTags($result, $expected);
  6725. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6726. 'div' => false, 'value' => 'my value'
  6727. ));
  6728. $expected = array(
  6729. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6730. 'Non Existing Nor Validated',
  6731. '/label',
  6732. 'input' => array(
  6733. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6734. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6735. )
  6736. );
  6737. $this->assertTags($result, $expected);
  6738. $this->Form->request->data = array(
  6739. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6740. ));
  6741. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6742. $expected = array(
  6743. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6744. 'Non Existing Nor Validated',
  6745. '/label',
  6746. 'input' => array(
  6747. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6748. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6749. )
  6750. );
  6751. $this->assertTags($result, $expected);
  6752. }
  6753. /**
  6754. * testFormMagicInputLabel method
  6755. *
  6756. * @return void
  6757. */
  6758. public function testFormMagicInputLabel() {
  6759. $encoding = strtolower(Configure::read('App.encoding'));
  6760. $result = $this->Form->create('Contact');
  6761. $expected = array(
  6762. 'form' => array(
  6763. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6764. 'accept-charset' => $encoding
  6765. ),
  6766. 'div' => array('style' => 'display:none;'),
  6767. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6768. '/div'
  6769. );
  6770. $this->assertTags($result, $expected);
  6771. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6772. $this->assertTags($result, array('input' => array(
  6773. 'name' => 'data[Contact][name]', 'type' => 'text',
  6774. 'id' => 'ContactName', 'maxlength' => '255')
  6775. ));
  6776. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6777. $expected = array(
  6778. 'label' => array('for' => 'ContactName'),
  6779. 'My label',
  6780. '/label',
  6781. 'input' => array(
  6782. 'type' => 'text', 'name' => 'data[Contact][name]',
  6783. 'id' => 'ContactName', 'maxlength' => '255'
  6784. )
  6785. );
  6786. $this->assertTags($result, $expected);
  6787. $result = $this->Form->input('Contact.name', array(
  6788. 'div' => false, 'label' => array('class' => 'mandatory')
  6789. ));
  6790. $expected = array(
  6791. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6792. 'Name',
  6793. '/label',
  6794. 'input' => array(
  6795. 'type' => 'text', 'name' => 'data[Contact][name]',
  6796. 'id' => 'ContactName', 'maxlength' => '255'
  6797. )
  6798. );
  6799. $this->assertTags($result, $expected);
  6800. $result = $this->Form->input('Contact.name', array(
  6801. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6802. ));
  6803. $expected = array(
  6804. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6805. 'My label',
  6806. '/label',
  6807. 'input' => array(
  6808. 'type' => 'text', 'name' => 'data[Contact][name]',
  6809. 'id' => 'ContactName', 'maxlength' => '255'
  6810. )
  6811. );
  6812. $this->assertTags($result, $expected);
  6813. $result = $this->Form->input('Contact.name', array(
  6814. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6815. ));
  6816. $expected = array(
  6817. 'label' => array('for' => 'my_id'),
  6818. 'Name',
  6819. '/label',
  6820. 'input' => array(
  6821. 'type' => 'text', 'name' => 'data[Contact][name]',
  6822. 'id' => 'my_id', 'maxlength' => '255'
  6823. )
  6824. );
  6825. $this->assertTags($result, $expected);
  6826. $result = $this->Form->input('1.id');
  6827. $this->assertTags($result, array('input' => array(
  6828. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6829. 'id' => 'Contact1Id'
  6830. )));
  6831. $result = $this->Form->input("1.name");
  6832. $expected = array(
  6833. 'div' => array('class' => 'input text'),
  6834. 'label' => array('for' => 'Contact1Name'),
  6835. 'Name',
  6836. '/label',
  6837. 'input' => array(
  6838. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6839. 'id' => 'Contact1Name', 'maxlength' => '255'
  6840. ),
  6841. '/div'
  6842. );
  6843. $this->assertTags($result, $expected);
  6844. $result = $this->Form->input('Contact.1.id');
  6845. $this->assertTags($result, array(
  6846. 'input' => array(
  6847. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6848. 'id' => 'Contact1Id'
  6849. )
  6850. ));
  6851. $result = $this->Form->input("Model.1.name");
  6852. $expected = array(
  6853. 'div' => array('class' => 'input text'),
  6854. 'label' => array('for' => 'Model1Name'),
  6855. 'Name',
  6856. '/label',
  6857. 'input' => array(
  6858. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6859. 'id' => 'Model1Name'
  6860. ),
  6861. '/div'
  6862. );
  6863. $this->assertTags($result, $expected);
  6864. }
  6865. /**
  6866. * testFormEnd method
  6867. *
  6868. * @return void
  6869. */
  6870. public function testFormEnd() {
  6871. $this->assertEquals('</form>', $this->Form->end());
  6872. $result = $this->Form->end('');
  6873. $expected = array(
  6874. 'div' => array('class' => 'submit'),
  6875. 'input' => array('type' => 'submit', 'value' => ''),
  6876. '/div',
  6877. '/form'
  6878. );
  6879. $this->assertTags($result, $expected);
  6880. $result = $this->Form->end(array('label' => ''));
  6881. $expected = array(
  6882. 'div' => array('class' => 'submit'),
  6883. 'input' => array('type' => 'submit', 'value' => ''),
  6884. '/div',
  6885. '/form'
  6886. );
  6887. $this->assertTags($result, $expected);
  6888. $result = $this->Form->end('save');
  6889. $expected = array(
  6890. 'div' => array('class' => 'submit'),
  6891. 'input' => array('type' => 'submit', 'value' => 'save'),
  6892. '/div',
  6893. '/form'
  6894. );
  6895. $this->assertTags($result, $expected);
  6896. $result = $this->Form->end(array('label' => 'save'));
  6897. $expected = array(
  6898. 'div' => array('class' => 'submit'),
  6899. 'input' => array('type' => 'submit', 'value' => 'save'),
  6900. '/div',
  6901. '/form'
  6902. );
  6903. $this->assertTags($result, $expected);
  6904. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6905. $expected = array(
  6906. 'div' => array('class' => 'submit'),
  6907. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6908. '/div',
  6909. '/form'
  6910. );
  6911. $this->assertTags($result, $expected);
  6912. $result = $this->Form->end(array('name' => 'Whatever'));
  6913. $expected = array(
  6914. 'div' => array('class' => 'submit'),
  6915. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6916. '/div',
  6917. '/form'
  6918. );
  6919. $this->assertTags($result, $expected);
  6920. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6921. $expected = array(
  6922. 'div' => array('class' => 'good'),
  6923. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6924. '/div',
  6925. '/form'
  6926. );
  6927. $this->assertTags($result, $expected);
  6928. $result = $this->Form->end(array(
  6929. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6930. ));
  6931. $expected = array(
  6932. 'div' => array('class' => 'good'),
  6933. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6934. '/div',
  6935. '/form'
  6936. );
  6937. $this->assertTags($result, $expected);
  6938. }
  6939. /**
  6940. * testMultipleFormWithIdFields method
  6941. *
  6942. * @return void
  6943. */
  6944. public function testMultipleFormWithIdFields() {
  6945. $this->Form->create('UserForm');
  6946. $result = $this->Form->input('id');
  6947. $this->assertTags($result, array('input' => array(
  6948. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6949. )));
  6950. $result = $this->Form->input('ValidateItem.id');
  6951. $this->assertTags($result, array('input' => array(
  6952. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6953. 'id' => 'ValidateItemId'
  6954. )));
  6955. $result = $this->Form->input('ValidateUser.id');
  6956. $this->assertTags($result, array('input' => array(
  6957. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6958. 'id' => 'ValidateUserId'
  6959. )));
  6960. }
  6961. /**
  6962. * testDbLessModel method
  6963. *
  6964. * @return void
  6965. */
  6966. public function testDbLessModel() {
  6967. $this->Form->create('TestMail');
  6968. $result = $this->Form->input('name');
  6969. $expected = array(
  6970. 'div' => array('class' => 'input text'),
  6971. 'label' => array('for' => 'TestMailName'),
  6972. 'Name',
  6973. '/label',
  6974. 'input' => array(
  6975. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6976. 'id' => 'TestMailName'
  6977. ),
  6978. '/div'
  6979. );
  6980. $this->assertTags($result, $expected);
  6981. ClassRegistry::init('TestMail');
  6982. $this->Form->create('TestMail');
  6983. $result = $this->Form->input('name');
  6984. $expected = array(
  6985. 'div' => array('class' => 'input text'),
  6986. 'label' => array('for' => 'TestMailName'),
  6987. 'Name',
  6988. '/label',
  6989. 'input' => array(
  6990. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6991. 'id' => 'TestMailName'
  6992. ),
  6993. '/div'
  6994. );
  6995. $this->assertTags($result, $expected);
  6996. }
  6997. /**
  6998. * testBrokenness method
  6999. *
  7000. * @return void
  7001. */
  7002. public function testBrokenness() {
  7003. /*
  7004. * #4 This test has two parents and four children. By default (as of r7117) both
  7005. * parents are show but the first parent is missing a child. This is the inconsistency
  7006. * in the default behaviour - one parent has all children, the other does not - dependent
  7007. * on the data values.
  7008. */
  7009. $result = $this->Form->select('Model.field', array(
  7010. 'Fred' => array(
  7011. 'freds_son_1' => 'Fred',
  7012. 'freds_son_2' => 'Freddie'
  7013. ),
  7014. 'Bert' => array(
  7015. 'berts_son_1' => 'Albert',
  7016. 'berts_son_2' => 'Bertie')
  7017. ),
  7018. array('showParents' => true, 'empty' => false)
  7019. );
  7020. $expected = array(
  7021. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7022. array('optgroup' => array('label' => 'Fred')),
  7023. array('option' => array('value' => 'freds_son_1')),
  7024. 'Fred',
  7025. '/option',
  7026. array('option' => array('value' => 'freds_son_2')),
  7027. 'Freddie',
  7028. '/option',
  7029. '/optgroup',
  7030. array('optgroup' => array('label' => 'Bert')),
  7031. array('option' => array('value' => 'berts_son_1')),
  7032. 'Albert',
  7033. '/option',
  7034. array('option' => array('value' => 'berts_son_2')),
  7035. 'Bertie',
  7036. '/option',
  7037. '/optgroup',
  7038. '/select'
  7039. );
  7040. $this->assertTags($result, $expected);
  7041. /*
  7042. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7043. * changed, so we should expect the same select list data, just with a different name
  7044. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7045. * This is where data corruption can occur, because when a select value is missing from
  7046. * a list a form will substitute the first value in the list - without the user knowing.
  7047. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7048. * affect the availability of 3 => 'Three' as a valid option.
  7049. */
  7050. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7051. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7052. ));
  7053. $result = $this->Form->select(
  7054. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7055. );
  7056. $expected = array(
  7057. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7058. array('option' => array('value' => 1)),
  7059. 'One',
  7060. '/option',
  7061. array('option' => array('value' => 2)),
  7062. 'Two',
  7063. '/option',
  7064. array('optgroup' => array('label' => 'Three')),
  7065. array('option' => array('value' => 3)),
  7066. 'Three',
  7067. '/option',
  7068. array('option' => array('value' => 4)),
  7069. 'Four',
  7070. '/option',
  7071. array('option' => array('value' => 5)),
  7072. 'Five',
  7073. '/option',
  7074. '/optgroup',
  7075. '/select'
  7076. );
  7077. $this->assertTags($result, $expected);
  7078. }
  7079. /**
  7080. * Test the generation of fields for a multi record form.
  7081. *
  7082. * @return void
  7083. */
  7084. public function testMultiRecordForm() {
  7085. $this->Form->create('ValidateProfile');
  7086. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7087. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7088. $expected = array(
  7089. 'div' => array('class' => 'input textarea'),
  7090. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7091. 'Name',
  7092. '/label',
  7093. 'textarea' => array(
  7094. 'id' => 'ValidateProfile1ValidateItem2Name',
  7095. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7096. 'cols' => 30,
  7097. 'rows' => 6
  7098. ),
  7099. 'Value',
  7100. '/textarea',
  7101. '/div'
  7102. );
  7103. $this->assertTags($result, $expected);
  7104. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7105. $expected = array(
  7106. 'div' => array('class' => 'input date'),
  7107. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7108. 'Created',
  7109. '/label',
  7110. array('select' => array(
  7111. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7112. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7113. )
  7114. ),
  7115. array('option' => array('value' => '')), '/option',
  7116. $this->dateRegex['monthsRegex'],
  7117. '/select', '-',
  7118. array('select' => array(
  7119. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7120. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7121. )
  7122. ),
  7123. array('option' => array('value' => '')), '/option',
  7124. $this->dateRegex['daysRegex'],
  7125. '/select', '-',
  7126. array('select' => array(
  7127. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7128. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7129. )
  7130. ),
  7131. array('option' => array('value' => '')), '/option',
  7132. $this->dateRegex['yearsRegex'],
  7133. '/select',
  7134. '/div'
  7135. );
  7136. $this->assertTags($result, $expected);
  7137. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7138. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7139. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7140. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7141. $expected = array(
  7142. 'div' => array('class' => 'input select error'),
  7143. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7144. 'Profile',
  7145. '/label',
  7146. 'select' => array(
  7147. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7148. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7149. 'class' => 'form-error'
  7150. ),
  7151. '/select',
  7152. array('div' => array('class' => 'error-message')),
  7153. 'Error',
  7154. '/div',
  7155. '/div'
  7156. );
  7157. $this->assertTags($result, $expected);
  7158. }
  7159. /**
  7160. * test the correct display of multi-record form validation errors.
  7161. *
  7162. * @return void
  7163. */
  7164. public function testMultiRecordFormValidationErrors() {
  7165. $this->Form->create('ValidateProfile');
  7166. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7167. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7168. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7169. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7170. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7171. $result = $this->Form->error('ValidateProfile.2.city');
  7172. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7173. $result = $this->Form->error('2.city');
  7174. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7175. }
  7176. /**
  7177. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7178. *
  7179. * @return void
  7180. */
  7181. public function testInputTemplate() {
  7182. $result = $this->Form->input('Contact.email', array(
  7183. 'type' => 'text', 'format' => array('input')
  7184. ));
  7185. $expected = array(
  7186. 'div' => array('class' => 'input text'),
  7187. 'input' => array(
  7188. 'type' => 'text', 'name' => 'data[Contact][email]',
  7189. 'id' => 'ContactEmail'
  7190. ),
  7191. '/div'
  7192. );
  7193. $this->assertTags($result, $expected);
  7194. $result = $this->Form->input('Contact.email', array(
  7195. 'type' => 'text', 'format' => array('input', 'label'),
  7196. 'label' => '<em>Email (required)</em>'
  7197. ));
  7198. $expected = array(
  7199. 'div' => array('class' => 'input text'),
  7200. array('input' => array(
  7201. 'type' => 'text', 'name' => 'data[Contact][email]',
  7202. 'id' => 'ContactEmail'
  7203. )),
  7204. 'label' => array('for' => 'ContactEmail'),
  7205. 'em' => array(),
  7206. 'Email (required)',
  7207. '/em',
  7208. '/label',
  7209. '/div'
  7210. );
  7211. $this->assertTags($result, $expected);
  7212. $result = $this->Form->input('Contact.email', array(
  7213. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7214. 'between' => '<div>Something in the middle</div>',
  7215. 'after' => '<span>Some text at the end</span>'
  7216. ));
  7217. $expected = array(
  7218. 'div' => array('class' => 'input text'),
  7219. array('input' => array(
  7220. 'type' => 'text', 'name' => 'data[Contact][email]',
  7221. 'id' => 'ContactEmail'
  7222. )),
  7223. array('div' => array()),
  7224. 'Something in the middle',
  7225. '/div',
  7226. 'label' => array('for' => 'ContactEmail'),
  7227. 'Email',
  7228. '/label',
  7229. 'span' => array(),
  7230. 'Some text at the end',
  7231. '/span',
  7232. '/div'
  7233. );
  7234. $this->assertTags($result, $expected);
  7235. $result = $this->Form->input('Contact.method', array(
  7236. 'type' => 'radio',
  7237. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7238. 'between' => 'I am between',
  7239. ));
  7240. $expected = array(
  7241. 'div' => array('class' => 'input radio'),
  7242. 'fieldset' => array(),
  7243. 'legend' => array(),
  7244. 'Method',
  7245. '/legend',
  7246. 'I am between',
  7247. 'input' => array(
  7248. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7249. 'value' => '', 'id' => 'ContactMethod_'
  7250. ),
  7251. array('input' => array(
  7252. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7253. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7254. )),
  7255. array('label' => array('for' => 'ContactMethodEmail')),
  7256. 'Email',
  7257. '/label',
  7258. array('input' => array(
  7259. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7260. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7261. )),
  7262. array('label' => array('for' => 'ContactMethodPigeon')),
  7263. 'Pigeon',
  7264. '/label',
  7265. '/fieldset',
  7266. '/div',
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. }
  7270. /**
  7271. * test that some html5 inputs + FormHelper::__call() work
  7272. *
  7273. * @return void
  7274. */
  7275. public function testHtml5Inputs() {
  7276. $result = $this->Form->email('User.email');
  7277. $expected = array(
  7278. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7279. );
  7280. $this->assertTags($result, $expected);
  7281. $result = $this->Form->search('User.query');
  7282. $expected = array(
  7283. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7284. );
  7285. $this->assertTags($result, $expected);
  7286. $result = $this->Form->search('User.query', array('value' => 'test'));
  7287. $expected = array(
  7288. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7289. );
  7290. $this->assertTags($result, $expected);
  7291. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7292. $expected = array(
  7293. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7294. );
  7295. $this->assertTags($result, $expected);
  7296. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7297. $expected = array(
  7298. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7299. );
  7300. $this->assertTags($result, $expected);
  7301. }
  7302. /**
  7303. *
  7304. * @expectedException CakeException
  7305. * @return void
  7306. */
  7307. public function testHtml5InputException() {
  7308. $this->Form->email();
  7309. }
  7310. /**
  7311. * Tests that a model can be loaded from the model names passed in the request object
  7312. *
  7313. * @return void
  7314. */
  7315. public function testIntrospectModelFromRequest() {
  7316. $this->loadFixtures('Post');
  7317. App::build(array(
  7318. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7319. ));
  7320. CakePlugin::load('TestPlugin');
  7321. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7322. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7323. $this->Form->create('TestPluginPost');
  7324. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7325. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7326. CakePlugin::unload();
  7327. App::build();
  7328. }
  7329. /**
  7330. * Tests that it is possible to set the validation errors directly in the helper for a field
  7331. *
  7332. * @return void
  7333. */
  7334. public function testCustomValidationErrors() {
  7335. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7336. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7337. $this->assertEquals('Badness!', $result);
  7338. }
  7339. /**
  7340. * Tests that the 'on' key validates as expected on create
  7341. *
  7342. * @return void
  7343. */
  7344. public function testRequiredOnCreate() {
  7345. $this->Form->create('Contact');
  7346. $result = $this->Form->input('Contact.imrequiredonupdate');
  7347. $expected = array(
  7348. 'div' => array('class' => 'input text'),
  7349. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7350. 'Imrequiredonupdate',
  7351. '/label',
  7352. 'input' => array(
  7353. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7354. 'id' => 'ContactImrequiredonupdate'
  7355. ),
  7356. '/div'
  7357. );
  7358. $this->assertTags($result, $expected);
  7359. $result = $this->Form->input('Contact.imrequiredoncreate');
  7360. $expected = array(
  7361. 'div' => array('class' => 'input text required'),
  7362. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7363. 'Imrequiredoncreate',
  7364. '/label',
  7365. 'input' => array(
  7366. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7367. 'id' => 'ContactImrequiredoncreate'
  7368. ),
  7369. '/div'
  7370. );
  7371. $this->assertTags($result, $expected);
  7372. $result = $this->Form->input('Contact.imrequiredonboth');
  7373. $expected = array(
  7374. 'div' => array('class' => 'input text required'),
  7375. 'label' => array('for' => 'ContactImrequiredonboth'),
  7376. 'Imrequiredonboth',
  7377. '/label',
  7378. 'input' => array(
  7379. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7380. 'id' => 'ContactImrequiredonboth'
  7381. ),
  7382. '/div'
  7383. );
  7384. $this->assertTags($result, $expected);
  7385. $result = $this->Form->input('Contact.imrequired');
  7386. $expected = array(
  7387. 'div' => array('class' => 'input text required'),
  7388. 'label' => array('for' => 'ContactImrequired'),
  7389. 'Imrequired',
  7390. '/label',
  7391. 'input' => array(
  7392. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7393. 'id' => 'ContactImrequired'
  7394. ),
  7395. '/div'
  7396. );
  7397. $this->assertTags($result, $expected);
  7398. }
  7399. /**
  7400. * Tests that the 'on' key validates as expected on update
  7401. *
  7402. * @return void
  7403. */
  7404. public function testRequiredOnUpdate() {
  7405. $this->Form->request->data['Contact']['id'] = 1;
  7406. $this->Form->create('Contact');
  7407. $result = $this->Form->input('Contact.imrequiredonupdate');
  7408. $expected = array(
  7409. 'div' => array('class' => 'input text required'),
  7410. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7411. 'Imrequiredonupdate',
  7412. '/label',
  7413. 'input' => array(
  7414. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7415. 'id' => 'ContactImrequiredonupdate'
  7416. ),
  7417. '/div'
  7418. );
  7419. $this->assertTags($result, $expected);
  7420. $result = $this->Form->input('Contact.imrequiredoncreate');
  7421. $expected = array(
  7422. 'div' => array('class' => 'input text'),
  7423. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7424. 'Imrequiredoncreate',
  7425. '/label',
  7426. 'input' => array(
  7427. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7428. 'id' => 'ContactImrequiredoncreate'
  7429. ),
  7430. '/div'
  7431. );
  7432. $this->assertTags($result, $expected);
  7433. $result = $this->Form->input('Contact.imrequiredonboth');
  7434. $expected = array(
  7435. 'div' => array('class' => 'input text required'),
  7436. 'label' => array('for' => 'ContactImrequiredonboth'),
  7437. 'Imrequiredonboth',
  7438. '/label',
  7439. 'input' => array(
  7440. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7441. 'id' => 'ContactImrequiredonboth'
  7442. ),
  7443. '/div'
  7444. );
  7445. $this->assertTags($result, $expected);
  7446. $result = $this->Form->input('Contact.imrequired');
  7447. $expected = array(
  7448. 'div' => array('class' => 'input text required'),
  7449. 'label' => array('for' => 'ContactImrequired'),
  7450. 'Imrequired',
  7451. '/label',
  7452. 'input' => array(
  7453. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7454. 'id' => 'ContactImrequired'
  7455. ),
  7456. '/div'
  7457. );
  7458. $this->assertTags($result, $expected);
  7459. }
  7460. /**
  7461. * Test inputDefaults setter and getter
  7462. *
  7463. * @return void
  7464. */
  7465. public function testInputDefaults() {
  7466. $this->Form->create('Contact');
  7467. $this->Form->inputDefaults(array(
  7468. 'label' => false,
  7469. 'div' => array(
  7470. 'style' => 'color: #000;'
  7471. )
  7472. ));
  7473. $result = $this->Form->input('Contact.field1');
  7474. $expected = array(
  7475. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7476. 'input' => array(
  7477. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7478. 'id' => 'ContactField1'
  7479. ),
  7480. '/div'
  7481. );
  7482. $this->assertTags($result, $expected);
  7483. $this->Form->inputDefaults(array(
  7484. 'div' => false,
  7485. 'label' => 'Label',
  7486. ));
  7487. $result = $this->Form->input('Contact.field1');
  7488. $expected = array(
  7489. 'label' => array('for' => 'ContactField1'),
  7490. 'Label',
  7491. '/label',
  7492. 'input' => array(
  7493. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7494. 'id' => 'ContactField1'
  7495. ),
  7496. );
  7497. $this->assertTags($result, $expected);
  7498. $this->Form->inputDefaults(array(
  7499. 'label' => false,
  7500. ), true);
  7501. $result = $this->Form->input('Contact.field1');
  7502. $expected = array(
  7503. 'input' => array(
  7504. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7505. 'id' => 'ContactField1'
  7506. ),
  7507. );
  7508. $this->assertTags($result, $expected);
  7509. $result = $this->Form->inputDefaults();
  7510. $expected = array(
  7511. 'div' => false,
  7512. 'label' => false,
  7513. );
  7514. $this->assertEqual($result, $expected);
  7515. }
  7516. }