PageRenderTime 66ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 2ms

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

https://bitbucket.org/tlorens/cakefoundation
PHP | 8099 lines | 6312 code | 639 blank | 1148 comment | 1 complexity | b4b95fe97f22b12feaba9c1ceb0a8c4a MD5 | raw file
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  111. ),
  112. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  113. );
  114. /**
  115. * schema method
  116. *
  117. * @return void
  118. */
  119. public function setSchema($schema) {
  120. $this->_schema = $schema;
  121. }
  122. /**
  123. * hasAndBelongsToMany property
  124. *
  125. * @var array
  126. */
  127. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  128. /**
  129. * hasAndBelongsToMany property
  130. *
  131. * @var array
  132. */
  133. public $belongsTo = array('User' => array('className' => 'UserForm'));
  134. }
  135. /**
  136. * ContactTagsContact class
  137. *
  138. * @package cake
  139. * @package Cake.Test.Case.View.Helper
  140. */
  141. class ContactTagsContact extends CakeTestModel {
  142. /**
  143. * useTable property
  144. *
  145. * @var bool false
  146. */
  147. public $useTable = false;
  148. /**
  149. * name property
  150. *
  151. * @var string 'Contact'
  152. */
  153. public $name = 'ContactTagsContact';
  154. /**
  155. * Default schema
  156. *
  157. * @var array
  158. */
  159. protected $_schema = array(
  160. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  161. 'contact_tag_id' => array(
  162. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  163. )
  164. );
  165. /**
  166. * schema method
  167. *
  168. * @return void
  169. */
  170. public function setSchema($schema) {
  171. $this->_schema = $schema;
  172. }
  173. }
  174. /**
  175. * ContactNonStandardPk class
  176. *
  177. * @package cake
  178. * @package Cake.Test.Case.View.Helper
  179. */
  180. class ContactNonStandardPk extends Contact {
  181. /**
  182. * primaryKey property
  183. *
  184. * @var string 'pk'
  185. */
  186. public $primaryKey = 'pk';
  187. /**
  188. * name property
  189. *
  190. * @var string 'ContactNonStandardPk'
  191. */
  192. public $name = 'ContactNonStandardPk';
  193. /**
  194. * schema method
  195. *
  196. * @return void
  197. */
  198. public function schema($field = false) {
  199. $this->_schema = parent::schema();
  200. $this->_schema['pk'] = $this->_schema['id'];
  201. unset($this->_schema['id']);
  202. return $this->_schema;
  203. }
  204. }
  205. /**
  206. * ContactTag class
  207. *
  208. * @package cake
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class ContactTag extends Model {
  212. /**
  213. * useTable property
  214. *
  215. * @var bool false
  216. */
  217. public $useTable = false;
  218. /**
  219. * schema definition
  220. *
  221. * @var array
  222. */
  223. protected $_schema = array(
  224. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  225. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  226. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  227. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  228. );
  229. }
  230. /**
  231. * UserForm class
  232. *
  233. * @package cake
  234. * @package Cake.Test.Case.View.Helper
  235. */
  236. class UserForm extends CakeTestModel {
  237. /**
  238. * useTable property
  239. *
  240. * @var bool false
  241. */
  242. public $useTable = false;
  243. /**
  244. * primaryKey property
  245. *
  246. * @var string 'id'
  247. */
  248. public $primaryKey = 'id';
  249. /**
  250. * name property
  251. *
  252. * @var string 'UserForm'
  253. */
  254. public $name = 'UserForm';
  255. /**
  256. * hasMany property
  257. *
  258. * @var array
  259. */
  260. public $hasMany = array(
  261. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  262. ));
  263. /**
  264. * schema definition
  265. *
  266. * @var array
  267. */
  268. protected $_schema = array(
  269. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  270. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  271. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  272. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  273. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  274. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  275. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  276. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  277. );
  278. }
  279. /**
  280. * OpenidUrl class
  281. *
  282. * @package cake
  283. * @package Cake.Test.Case.View.Helper
  284. */
  285. class OpenidUrl extends CakeTestModel {
  286. /**
  287. * useTable property
  288. *
  289. * @var bool false
  290. */
  291. public $useTable = false;
  292. /**
  293. * primaryKey property
  294. *
  295. * @var string 'id'
  296. */
  297. public $primaryKey = 'id';
  298. /**
  299. * name property
  300. *
  301. * @var string 'OpenidUrl'
  302. */
  303. public $name = 'OpenidUrl';
  304. /**
  305. * belongsTo property
  306. *
  307. * @var array
  308. */
  309. public $belongsTo = array('UserForm' => array(
  310. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  311. ));
  312. /**
  313. * validate property
  314. *
  315. * @var array
  316. */
  317. public $validate = array('openid_not_registered' => array());
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'user_form_id' => array(
  326. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  327. ),
  328. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  329. );
  330. /**
  331. * beforeValidate method
  332. *
  333. * @return void
  334. */
  335. public function beforeValidate($options = array()) {
  336. $this->invalidate('openid_not_registered');
  337. return true;
  338. }
  339. }
  340. /**
  341. * ValidateUser class
  342. *
  343. * @package cake
  344. * @package Cake.Test.Case.View.Helper
  345. */
  346. class ValidateUser extends CakeTestModel {
  347. /**
  348. * primaryKey property
  349. *
  350. * @var string 'id'
  351. */
  352. public $primaryKey = 'id';
  353. /**
  354. * useTable property
  355. *
  356. * @var bool false
  357. */
  358. public $useTable = false;
  359. /**
  360. * name property
  361. *
  362. * @var string 'ValidateUser'
  363. */
  364. public $name = 'ValidateUser';
  365. /**
  366. * hasOne property
  367. *
  368. * @var array
  369. */
  370. public $hasOne = array('ValidateProfile' => array(
  371. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  372. ));
  373. /**
  374. * schema method
  375. *
  376. * @var array
  377. */
  378. protected $_schema = array(
  379. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  380. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  381. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  382. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  383. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  384. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  385. );
  386. /**
  387. * beforeValidate method
  388. *
  389. * @return void
  390. */
  391. public function beforeValidate($options = array()) {
  392. $this->invalidate('email');
  393. return false;
  394. }
  395. }
  396. /**
  397. * ValidateProfile class
  398. *
  399. * @package cake
  400. * @package Cake.Test.Case.View.Helper
  401. */
  402. class ValidateProfile extends CakeTestModel {
  403. /**
  404. * primaryKey property
  405. *
  406. * @var string 'id'
  407. */
  408. public $primaryKey = 'id';
  409. /**
  410. * useTable property
  411. *
  412. * @var bool false
  413. */
  414. public $useTable = false;
  415. /**
  416. * schema property
  417. *
  418. * @var array
  419. */
  420. protected $_schema = array(
  421. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  422. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  423. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  424. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  425. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  426. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  427. );
  428. /**
  429. * name property
  430. *
  431. * @var string 'ValidateProfile'
  432. */
  433. public $name = 'ValidateProfile';
  434. /**
  435. * hasOne property
  436. *
  437. * @var array
  438. */
  439. public $hasOne = array('ValidateItem' => array(
  440. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  441. ));
  442. /**
  443. * belongsTo property
  444. *
  445. * @var array
  446. */
  447. public $belongsTo = array('ValidateUser' => array(
  448. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  449. ));
  450. /**
  451. * beforeValidate method
  452. *
  453. * @return void
  454. */
  455. public function beforeValidate($options = array()) {
  456. $this->invalidate('full_name');
  457. $this->invalidate('city');
  458. return false;
  459. }
  460. }
  461. /**
  462. * ValidateItem class
  463. *
  464. * @package cake
  465. * @package Cake.Test.Case.View.Helper
  466. */
  467. class ValidateItem extends CakeTestModel {
  468. /**
  469. * primaryKey property
  470. *
  471. * @var string 'id'
  472. */
  473. public $primaryKey = 'id';
  474. /**
  475. * useTable property
  476. *
  477. * @var bool false
  478. */
  479. public $useTable = false;
  480. /**
  481. * name property
  482. *
  483. * @var string 'ValidateItem'
  484. */
  485. public $name = 'ValidateItem';
  486. /**
  487. * schema property
  488. *
  489. * @var array
  490. */
  491. protected $_schema = array(
  492. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  493. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  495. 'description' => array(
  496. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  497. ),
  498. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  499. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  500. );
  501. /**
  502. * belongsTo property
  503. *
  504. * @var array
  505. */
  506. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  507. /**
  508. * beforeValidate method
  509. *
  510. * @return void
  511. */
  512. public function beforeValidate($options = array()) {
  513. $this->invalidate('description');
  514. return false;
  515. }
  516. }
  517. /**
  518. * TestMail class
  519. *
  520. * @package cake
  521. * @package Cake.Test.Case.View.Helper
  522. */
  523. class TestMail extends CakeTestModel {
  524. /**
  525. * primaryKey property
  526. *
  527. * @var string 'id'
  528. */
  529. public $primaryKey = 'id';
  530. /**
  531. * useTable property
  532. *
  533. * @var bool false
  534. */
  535. public $useTable = false;
  536. /**
  537. * name property
  538. *
  539. * @var string 'TestMail'
  540. */
  541. public $name = 'TestMail';
  542. }
  543. /**
  544. * FormHelperTest class
  545. *
  546. * @package cake
  547. * @subpackage Cake.Test.Case.View.Helper
  548. * @property FormHelper $Form
  549. */
  550. class FormHelperTest extends CakeTestCase {
  551. /**
  552. * Fixtures to be used
  553. *
  554. * @var array
  555. */
  556. public $fixtures = array('core.post');
  557. /**
  558. * Do not load the fixtures by default
  559. *
  560. * @var boolean
  561. */
  562. public $autoFixtures = false;
  563. /**
  564. * setUp method
  565. *
  566. * @return void
  567. */
  568. public function setUp() {
  569. parent::setUp();
  570. Configure::write('App.base', '');
  571. $this->Controller = new ContactTestController();
  572. $this->View = new View($this->Controller);
  573. $this->Form = new FormHelper($this->View);
  574. $this->Form->Html = new HtmlHelper($this->View);
  575. $this->Form->request = new CakeRequest('contacts/add', false);
  576. $this->Form->request->here = '/contacts/add';
  577. $this->Form->request['action'] = 'add';
  578. $this->Form->request->webroot = '';
  579. $this->Form->request->base = '';
  580. ClassRegistry::addObject('Contact', new Contact());
  581. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  582. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  583. ClassRegistry::addObject('User', new UserForm());
  584. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  585. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  586. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  587. $this->oldSalt = Configure::read('Security.salt');
  588. $this->dateRegex = array(
  589. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  590. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  591. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  592. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  593. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  594. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  595. );
  596. Configure::write('Security.salt', 'foo!');
  597. }
  598. /**
  599. * tearDown method
  600. *
  601. * @return void
  602. */
  603. public function tearDown() {
  604. parent::tearDown();
  605. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  606. Configure::write('Security.salt', $this->oldSalt);
  607. }
  608. /**
  609. * testFormCreateWithSecurity method
  610. *
  611. * Test form->create() with security key.
  612. *
  613. * @return void
  614. */
  615. public function testCreateWithSecurity() {
  616. $this->Form->request['_Token'] = array('key' => 'testKey');
  617. $encoding = strtolower(Configure::read('App.encoding'));
  618. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  619. $expected = array(
  620. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  621. 'div' => array('style' => 'display:none;'),
  622. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  623. array('input' => array(
  624. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  625. )),
  626. '/div'
  627. );
  628. $this->assertTags($result, $expected);
  629. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  630. $expected['form']['id'] = 'MyForm';
  631. $this->assertTags($result, $expected);
  632. }
  633. /**
  634. * test that create() clears the fields property so it starts fresh
  635. *
  636. * @return void
  637. */
  638. public function testCreateClearingFields() {
  639. $this->Form->fields = array('model_id');
  640. $this->Form->create('Contact');
  641. $this->assertEquals(array(), $this->Form->fields);
  642. }
  643. /**
  644. * Tests form hash generation with model-less data
  645. *
  646. * @return void
  647. */
  648. public function testValidateHashNoModel() {
  649. $this->Form->request['_Token'] = array('key' => 'foo');
  650. $result = $this->Form->secure(array('anything'));
  651. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  652. }
  653. /**
  654. * Tests that models with identical field names get resolved properly
  655. *
  656. * @return void
  657. */
  658. public function testDuplicateFieldNameResolution() {
  659. $result = $this->Form->create('ValidateUser');
  660. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  661. $result = $this->Form->input('ValidateItem.name');
  662. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  663. $result = $this->Form->input('ValidateUser.name');
  664. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  665. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  666. $this->assertRegExp('/type="text"/', $result);
  667. $result = $this->Form->input('ValidateItem.name');
  668. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  669. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  670. $this->assertRegExp('/<textarea/', $result);
  671. $result = $this->Form->input('name');
  672. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  673. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  674. $this->assertRegExp('/type="text"/', $result);
  675. }
  676. /**
  677. * Tests that hidden fields generated for checkboxes don't get locked
  678. *
  679. * @return void
  680. */
  681. public function testNoCheckboxLocking() {
  682. $this->Form->request['_Token'] = array('key' => 'foo');
  683. $this->assertSame(array(), $this->Form->fields);
  684. $this->Form->checkbox('check', array('value' => '1'));
  685. $this->assertSame($this->Form->fields, array('check'));
  686. }
  687. /**
  688. * testFormSecurityFields method
  689. *
  690. * Test generation of secure form hash generation.
  691. *
  692. * @return void
  693. */
  694. public function testFormSecurityFields() {
  695. $key = 'testKey';
  696. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  697. $this->Form->request['_Token'] = array('key' => $key);
  698. $result = $this->Form->secure($fields);
  699. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  700. $expected .= ':' . 'Model.valid';
  701. $expected = array(
  702. 'div' => array('style' => 'display:none;'),
  703. array('input' => array(
  704. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  705. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  706. )),
  707. array('input' => array(
  708. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  709. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  710. )),
  711. '/div'
  712. );
  713. $this->assertTags($result, $expected);
  714. }
  715. /**
  716. * Tests correct generation of number fields for double and float fields
  717. *
  718. * @return void
  719. */
  720. public function testTextFieldGenerationForFloats() {
  721. $model = ClassRegistry::getObject('Contact');
  722. $model->setSchema(array('foo' => array(
  723. 'type' => 'float',
  724. 'null' => false,
  725. 'default' => null,
  726. 'length' => null
  727. )));
  728. $this->Form->create('Contact');
  729. $result = $this->Form->input('foo');
  730. $expected = array(
  731. 'div' => array('class' => 'input number'),
  732. 'label' => array('for' => 'ContactFoo'),
  733. 'Foo',
  734. '/label',
  735. array('input' => array(
  736. 'type' => 'number',
  737. 'name' => 'data[Contact][foo]',
  738. 'id' => 'ContactFoo',
  739. 'step' => 'any'
  740. )),
  741. '/div'
  742. );
  743. $this->assertTags($result, $expected);
  744. $result = $this->Form->input('foo', array('step' => 0.5));
  745. $expected = array(
  746. 'div' => array('class' => 'input number'),
  747. 'label' => array('for' => 'ContactFoo'),
  748. 'Foo',
  749. '/label',
  750. array('input' => array(
  751. 'type' => 'number',
  752. 'name' => 'data[Contact][foo]',
  753. 'id' => 'ContactFoo',
  754. 'step' => '0.5'
  755. )),
  756. '/div'
  757. );
  758. $this->assertTags($result, $expected);
  759. }
  760. /**
  761. * Tests correct generation of number fields for integer fields
  762. *
  763. * @access public
  764. * @return void
  765. */
  766. public function testTextFieldTypeNumberGenerationForIntegers() {
  767. $model = ClassRegistry::getObject('Contact');
  768. $model->setSchema(array('foo' => array(
  769. 'type' => 'integer',
  770. 'null' => false,
  771. 'default' => null,
  772. 'length' => null
  773. )));
  774. $this->Form->create('Contact');
  775. $result = $this->Form->input('foo');
  776. $expected = array(
  777. 'div' => array('class' => 'input number'),
  778. 'label' => array('for' => 'ContactFoo'),
  779. 'Foo',
  780. '/label',
  781. array('input' => array(
  782. 'type' => 'number', 'name' => 'data[Contact][foo]',
  783. 'id' => 'ContactFoo'
  784. )),
  785. '/div'
  786. );
  787. $this->assertTags($result, $expected);
  788. }
  789. /**
  790. * testFormSecurityMultipleFields method
  791. *
  792. * Test secure() with multiple row form. Ensure hash is correct.
  793. *
  794. * @return void
  795. */
  796. public function testFormSecurityMultipleFields() {
  797. $key = 'testKey';
  798. $fields = array(
  799. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  800. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  801. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  802. );
  803. $this->Form->request['_Token'] = array('key' => $key);
  804. $result = $this->Form->secure($fields);
  805. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  806. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  807. $expected = array(
  808. 'div' => array('style' => 'display:none;'),
  809. array('input' => array(
  810. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  811. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  812. )),
  813. array('input' => array(
  814. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  815. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  816. )),
  817. '/div'
  818. );
  819. $this->assertTags($result, $expected);
  820. }
  821. /**
  822. * testFormSecurityMultipleSubmitButtons
  823. *
  824. * test form submit generation and ensure that _Token is only created on end()
  825. *
  826. * @return void
  827. */
  828. public function testFormSecurityMultipleSubmitButtons() {
  829. $key = 'testKey';
  830. $this->Form->request['_Token'] = array('key' => $key);
  831. $this->Form->create('Addresses');
  832. $this->Form->input('Address.title');
  833. $this->Form->input('Address.first_name');
  834. $result = $this->Form->submit('Save', array('name' => 'save'));
  835. $expected = array(
  836. 'div' => array('class' => 'submit'),
  837. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  838. '/div',
  839. );
  840. $this->assertTags($result, $expected);
  841. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  842. $expected = array(
  843. 'div' => array('class' => 'submit'),
  844. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  845. '/div',
  846. );
  847. $this->assertTags($result, $expected);
  848. $result = $this->Form->end(null);
  849. $expected = array(
  850. 'div' => array('style' => 'display:none;'),
  851. array('input' => array(
  852. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  853. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  854. )),
  855. array('input' => array(
  856. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  857. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  858. )),
  859. '/div'
  860. );
  861. $this->assertTags($result, $expected);
  862. }
  863. /**
  864. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  865. *
  866. * @return void
  867. */
  868. public function testSecurityButtonNestedNamed() {
  869. $key = 'testKey';
  870. $this->Form->request['_Token'] = array('key' => $key);
  871. $this->Form->create('Addresses');
  872. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  873. $result = $this->Form->unlockField();
  874. $this->assertEquals(array('Address.button'), $result);
  875. }
  876. /**
  877. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  878. *
  879. * @return void
  880. */
  881. public function testSecuritySubmitNestedNamed() {
  882. $key = 'testKey';
  883. $this->Form->request['_Token'] = array('key' => $key);
  884. $this->Form->create('Addresses');
  885. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  886. $result = $this->Form->unlockField();
  887. $this->assertEquals(array('Address.button'), $result);
  888. }
  889. /**
  890. * Test that the correct fields are unlocked for image submits with no names.
  891. *
  892. * @return void
  893. */
  894. public function testSecuritySubmitImageNoName() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('User');
  898. $result = $this->Form->submit('save.png');
  899. $expected = array(
  900. 'div' => array('class' => 'submit'),
  901. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  902. '/div'
  903. );
  904. $this->assertTags($result, $expected);
  905. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  906. }
  907. /**
  908. * Test that the correct fields are unlocked for image submits with names.
  909. *
  910. * @return void
  911. */
  912. public function testSecuritySubmitImageName() {
  913. $key = 'testKey';
  914. $this->Form->request['_Token'] = array('key' => $key);
  915. $this->Form->create('User');
  916. $result = $this->Form->submit('save.png', array('name' => 'test'));
  917. $expected = array(
  918. 'div' => array('class' => 'submit'),
  919. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  920. '/div'
  921. );
  922. $this->assertTags($result, $expected);
  923. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  924. }
  925. /**
  926. * testFormSecurityMultipleInputFields method
  927. *
  928. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  929. *
  930. * @return void
  931. */
  932. public function testFormSecurityMultipleInputFields() {
  933. $key = 'testKey';
  934. $this->Form->request['_Token'] = array('key' => $key);
  935. $this->Form->create('Addresses');
  936. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  937. $this->Form->input('Addresses.0.title');
  938. $this->Form->input('Addresses.0.first_name');
  939. $this->Form->input('Addresses.0.last_name');
  940. $this->Form->input('Addresses.0.address');
  941. $this->Form->input('Addresses.0.city');
  942. $this->Form->input('Addresses.0.phone');
  943. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  944. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  945. $this->Form->input('Addresses.1.title');
  946. $this->Form->input('Addresses.1.first_name');
  947. $this->Form->input('Addresses.1.last_name');
  948. $this->Form->input('Addresses.1.address');
  949. $this->Form->input('Addresses.1.city');
  950. $this->Form->input('Addresses.1.phone');
  951. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  952. $result = $this->Form->secure($this->Form->fields);
  953. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  954. $expected = array(
  955. 'div' => array('style' => 'display:none;'),
  956. array('input' => array(
  957. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  958. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  959. )),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  962. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  963. )),
  964. '/div'
  965. );
  966. $this->assertTags($result, $expected);
  967. }
  968. /**
  969. * Test form security with Model.field.0 style inputs
  970. *
  971. * @return void
  972. */
  973. public function testFormSecurityArrayFields() {
  974. $key = 'testKey';
  975. $this->Form->request->params['_Token']['key'] = $key;
  976. $this->Form->create('Address');
  977. $this->Form->input('Address.primary.1');
  978. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  979. $this->Form->input('Address.secondary.1.0');
  980. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  981. }
  982. /**
  983. * testFormSecurityMultipleInputDisabledFields method
  984. *
  985. * test secure form generation with multiple records and disabled fields.
  986. *
  987. * @return void
  988. */
  989. public function testFormSecurityMultipleInputDisabledFields() {
  990. $key = 'testKey';
  991. $this->Form->request->params['_Token'] = array(
  992. 'key' => $key,
  993. 'unlockedFields' => array('first_name', 'address')
  994. );
  995. $this->Form->create();
  996. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  997. $this->Form->input('Addresses.0.title');
  998. $this->Form->input('Addresses.0.first_name');
  999. $this->Form->input('Addresses.0.last_name');
  1000. $this->Form->input('Addresses.0.address');
  1001. $this->Form->input('Addresses.0.city');
  1002. $this->Form->input('Addresses.0.phone');
  1003. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1004. $this->Form->input('Addresses.1.title');
  1005. $this->Form->input('Addresses.1.first_name');
  1006. $this->Form->input('Addresses.1.last_name');
  1007. $this->Form->input('Addresses.1.address');
  1008. $this->Form->input('Addresses.1.city');
  1009. $this->Form->input('Addresses.1.phone');
  1010. $result = $this->Form->secure($this->Form->fields);
  1011. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1012. $expected = array(
  1013. 'div' => array('style' => 'display:none;'),
  1014. array('input' => array(
  1015. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1016. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1017. )),
  1018. array('input' => array(
  1019. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1020. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1021. )),
  1022. '/div'
  1023. );
  1024. $this->assertTags($result, $expected);
  1025. }
  1026. /**
  1027. * testFormSecurityInputDisabledFields method
  1028. *
  1029. * Test single record form with disabled fields.
  1030. *
  1031. * @return void
  1032. */
  1033. public function testFormSecurityInputUnlockedFields() {
  1034. $key = 'testKey';
  1035. $this->Form->request['_Token'] = array(
  1036. 'key' => $key,
  1037. 'unlockedFields' => array('first_name', 'address')
  1038. );
  1039. $this->Form->create();
  1040. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1041. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1042. $this->Form->input('Addresses.title');
  1043. $this->Form->input('Addresses.first_name');
  1044. $this->Form->input('Addresses.last_name');
  1045. $this->Form->input('Addresses.address');
  1046. $this->Form->input('Addresses.city');
  1047. $this->Form->input('Addresses.phone');
  1048. $result = $this->Form->fields;
  1049. $expected = array(
  1050. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1051. 'Addresses.city', 'Addresses.phone'
  1052. );
  1053. $this->assertEquals($expected, $result);
  1054. $result = $this->Form->secure($expected);
  1055. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1056. $expected = array(
  1057. 'div' => array('style' => 'display:none;'),
  1058. array('input' => array(
  1059. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1060. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1061. )),
  1062. array('input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1064. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1065. )),
  1066. '/div'
  1067. );
  1068. $this->assertTags($result, $expected);
  1069. }
  1070. /**
  1071. * test securing inputs with custom name attributes.
  1072. *
  1073. * @return void
  1074. */
  1075. public function testFormSecureWithCustomNameAttribute() {
  1076. $this->Form->request->params['_Token']['key'] = 'testKey';
  1077. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1078. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1079. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1080. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1081. }
  1082. /**
  1083. * testFormSecuredInput method
  1084. *
  1085. * Test generation of entire secure form, assertions made on input() output.
  1086. *
  1087. * @return void
  1088. */
  1089. public function testFormSecuredInput() {
  1090. $this->Form->request['_Token'] = array('key' => 'testKey');
  1091. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1092. $encoding = strtolower(Configure::read('App.encoding'));
  1093. $expected = array(
  1094. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1095. 'div' => array('style' => 'display:none;'),
  1096. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1097. array('input' => array(
  1098. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1099. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1100. )),
  1101. '/div'
  1102. );
  1103. $this->assertTags($result, $expected);
  1104. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1105. $expected = array(
  1106. 'div' => array('class' => 'input text'),
  1107. 'label' => array('for' => 'UserFormPublished'),
  1108. 'Published',
  1109. '/label',
  1110. array('input' => array(
  1111. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1112. 'id' => 'UserFormPublished'
  1113. )),
  1114. '/div'
  1115. );
  1116. $this->assertTags($result, $expected);
  1117. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1118. $expected = array(
  1119. 'div' => array('class' => 'input text'),
  1120. 'label' => array('for' => 'UserFormOther'),
  1121. 'Other',
  1122. '/label',
  1123. array('input' => array(
  1124. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1125. 'id' => 'UserFormOther'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->hidden('UserForm.stuff');
  1131. $expected = array('input' => array(
  1132. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1133. 'id' => 'UserFormStuff'
  1134. ));
  1135. $this->assertTags($result, $expected);
  1136. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1137. $expected = array('input' => array(
  1138. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1139. 'value' => '0', 'id' => 'UserFormHidden'
  1140. ));
  1141. $this->assertTags($result, $expected);
  1142. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1143. $expected = array(
  1144. 'div' => array('class' => 'input checkbox'),
  1145. array('input' => array(
  1146. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1147. 'value' => '0', 'id' => 'UserFormSomething_'
  1148. )),
  1149. array('input' => array(
  1150. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1151. 'value' => '1', 'id' => 'UserFormSomething'
  1152. )),
  1153. 'label' => array('for' => 'UserFormSomething'),
  1154. 'Something',
  1155. '/label',
  1156. '/div'
  1157. );
  1158. $this->assertTags($result, $expected);
  1159. $result = $this->Form->fields;
  1160. $expected = array(
  1161. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1162. 'UserForm.hidden' => '0', 'UserForm.something'
  1163. );
  1164. $this->assertEquals($expected, $result);
  1165. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1166. $result = $this->Form->secure($this->Form->fields);
  1167. $expected = array(
  1168. 'div' => array('style' => 'display:none;'),
  1169. array('input' => array(
  1170. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1171. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1172. )),
  1173. array('input' => array(
  1174. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1175. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1176. )),
  1177. '/div'
  1178. );
  1179. $this->assertTags($result, $expected);
  1180. }
  1181. /**
  1182. * Tests that the correct keys are added to the field hash index
  1183. *
  1184. * @return void
  1185. */
  1186. public function testFormSecuredFileInput() {
  1187. $this->Form->request['_Token'] = array('key' => 'testKey');
  1188. $this->assertEquals(array(), $this->Form->fields);
  1189. $result = $this->Form->file('Attachment.file');
  1190. $expected = array(
  1191. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1192. 'Attachment.file.error', 'Attachment.file.size'
  1193. );
  1194. $this->assertEquals($expected, $this->Form->fields);
  1195. }
  1196. /**
  1197. * test that multiple selects keys are added to field hash
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFormSecuredMultipleSelect() {
  1202. $this->Form->request['_Token'] = array('key' => 'testKey');
  1203. $this->assertEquals(array(), $this->Form->fields);
  1204. $options = array('1' => 'one', '2' => 'two');
  1205. $this->Form->select('Model.select', $options);
  1206. $expected = array('Model.select');
  1207. $this->assertEquals($expected, $this->Form->fields);
  1208. $this->Form->fields = array();
  1209. $this->Form->select('Model.select', $options, array('multiple' => true));
  1210. $this->assertEquals($expected, $this->Form->fields);
  1211. }
  1212. /**
  1213. * testFormSecuredRadio method
  1214. *
  1215. * @return void
  1216. */
  1217. public function testFormSecuredRadio() {
  1218. $this->Form->request['_Token'] = array('key' => 'testKey');
  1219. $this->assertEquals(array(), $this->Form->fields);
  1220. $options = array('1' => 'option1', '2' => 'option2');
  1221. $this->Form->radio('Test.test', $options);
  1222. $expected = array('Test.test');
  1223. $this->assertEquals($expected, $this->Form->fields);
  1224. }
  1225. /**
  1226. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1227. * hashing.
  1228. *
  1229. * @return void
  1230. */
  1231. public function testFormSecuredAndDisabled() {
  1232. $this->Form->request['_Token'] = array('key' => 'testKey');
  1233. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1234. $this->Form->text('Model.text', array('disabled' => true));
  1235. $this->Form->password('Model.text', array('disabled' => true));
  1236. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1237. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1238. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1239. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1240. $this->Form->month('Model.month', array('disabled' => true));
  1241. $this->Form->day('Model.day', array('disabled' => true));
  1242. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1243. $this->Form->minute('Model.minute', array('disabled' => true));
  1244. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1245. $expected = array(
  1246. 'Model.radio' => ''
  1247. );
  1248. $this->assertEquals($expected, $this->Form->fields);
  1249. }
  1250. /**
  1251. * testDisableSecurityUsingForm method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testDisableSecurityUsingForm() {
  1256. $this->Form->request['_Token'] = array(
  1257. 'key' => 'testKey',
  1258. 'disabledFields' => array()
  1259. );
  1260. $this->Form->create();
  1261. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1262. $this->Form->input('Addresses.title');
  1263. $this->Form->input('Addresses.first_name', array('secure' => false));
  1264. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1265. $this->Form->input('Addresses.zip', array(
  1266. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1267. ));
  1268. $result = $this->Form->fields;
  1269. $expected = array(
  1270. 'Addresses.id' => '123456', 'Addresses.title',
  1271. );
  1272. $this->assertEquals($expected, $result);
  1273. }
  1274. /**
  1275. * test disableField
  1276. *
  1277. * @return void
  1278. */
  1279. public function testUnlockFieldAddsToList() {
  1280. $this->Form->request['_Token'] = array(
  1281. 'key' => 'testKey',
  1282. 'unlockedFields' => array()
  1283. );
  1284. $this->Form->create('Contact');
  1285. $this->Form->unlockField('Contact.name');
  1286. $this->Form->text('Contact.name');
  1287. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1288. $this->assertEquals(array(), $this->Form->fields);
  1289. }
  1290. /**
  1291. * test unlockField removing from fields array.
  1292. *
  1293. * @return void
  1294. */
  1295. public function testUnlockFieldRemovingFromFields() {
  1296. $this->Form->request['_Token'] = array(
  1297. 'key' => 'testKey',
  1298. 'unlockedFields' => array()
  1299. );
  1300. $this->Form->create('Contact');
  1301. $this->Form->hidden('Contact.id', array('value' => 1));
  1302. $this->Form->text('Contact.name');
  1303. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1304. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1305. $this->Form->unlockField('Contact.name');
  1306. $this->Form->unlockField('Contact.id');
  1307. $this->assertEquals(array(), $this->Form->fields);
  1308. }
  1309. /**
  1310. * testTagIsInvalid method
  1311. *
  1312. * @return void
  1313. */
  1314. public function testTagIsInvalid() {
  1315. $Contact = ClassRegistry::getObject('Contact');
  1316. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1317. $this->Form->setEntity('Contact.0.email');
  1318. $result = $this->Form->tagIsInvalid();
  1319. $expected = array('Please provide an email');
  1320. $this->assertEquals($expected, $result);
  1321. $this->Form->setEntity('Contact.1.email');
  1322. $result = $this->Form->tagIsInvalid();
  1323. $expected = false;
  1324. $this->assertSame($expected, $result);
  1325. $this->Form->setEntity('Contact.0.name');
  1326. $result = $this->Form->tagIsInvalid();
  1327. $expected = false;
  1328. $this->assertSame($expected, $result);
  1329. }
  1330. /**
  1331. * testPasswordValidation method
  1332. *
  1333. * test validation errors on password input.
  1334. *
  1335. * @return void
  1336. */
  1337. public function testPasswordValidation() {
  1338. $Contact = ClassRegistry::getObject('Contact');
  1339. $Contact->validationErrors['password'] = array('Please provide a password');
  1340. $result = $this->Form->input('Contact.password');
  1341. $expected = array(
  1342. 'div' => array('class' => 'input password error'),
  1343. 'label' => array('for' => 'ContactPassword'),
  1344. 'Password',
  1345. '/label',
  1346. 'input' => array(
  1347. 'type' => 'password', 'name' => 'data[Contact][password]',
  1348. 'id' => 'ContactPassword', 'class' => 'form-error'
  1349. ),
  1350. array('div' => array('class' => 'error-message')),
  1351. 'Please provide a password',
  1352. '/div',
  1353. '/div'
  1354. );
  1355. $this->assertTags($result, $expected);
  1356. }
  1357. /**
  1358. * testEmptyErrorValidation method
  1359. *
  1360. * test validation error div when validation message is an empty string
  1361. *
  1362. * @access public
  1363. * @return void
  1364. */
  1365. public function testEmptyErrorValidation() {
  1366. $this->Form->validationErrors['Contact']['password'] = '';
  1367. $result = $this->Form->input('Contact.password');
  1368. $expected = array(
  1369. 'div' => array('class' => 'input password error'),
  1370. 'label' => array('for' => 'ContactPassword'),
  1371. 'Password',
  1372. '/label',
  1373. 'input' => array(
  1374. 'type' => 'password', 'name' => 'data[Contact][password]',
  1375. 'id' => 'ContactPassword', 'class' => 'form-error'
  1376. ),
  1377. array('div' => array('class' => 'error-message')),
  1378. array(),
  1379. '/div',
  1380. '/div'
  1381. );
  1382. $this->assertTags($result, $expected);
  1383. }
  1384. /**
  1385. * testEmptyInputErrorValidation method
  1386. *
  1387. * test validation error div when validation message is overridden by an empty string when calling input()
  1388. *
  1389. * @access public
  1390. * @return void
  1391. */
  1392. public function testEmptyInputErrorValidation() {
  1393. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1394. $result = $this->Form->input('Contact.password', array('error' => ''));
  1395. $expected = array(
  1396. 'div' => array('class' => 'input password error'),
  1397. 'label' => array('for' => 'ContactPassword'),
  1398. 'Password',
  1399. '/label',
  1400. 'input' => array(
  1401. 'type' => 'password', 'name' => 'data[Contact][password]',
  1402. 'id' => 'ContactPassword', 'class' => 'form-error'
  1403. ),
  1404. array('div' => array('class' => 'error-message')),
  1405. array(),
  1406. '/div',
  1407. '/div'
  1408. );
  1409. $this->assertTags($result, $expected);
  1410. }
  1411. /**
  1412. * testFormValidationAssociated method
  1413. *
  1414. * test display of form errors in conjunction with model::validates.
  1415. *
  1416. * @return void
  1417. */
  1418. public function testFormValidationAssociated() {
  1419. $this->UserForm = ClassRegistry::getObject('UserForm');
  1420. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1421. $data = array(
  1422. 'UserForm' => array('name' => 'user'),
  1423. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1424. );
  1425. $result = $this->UserForm->OpenidUrl->create($data);
  1426. $this->assertFalse(empty($result));
  1427. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1428. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1429. $encoding = strtolower(Configure::read('App.encoding'));
  1430. $expected = array(
  1431. 'form' => array(
  1432. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1433. 'accept-charset' => $encoding
  1434. ),
  1435. 'div' => array('style' => 'display:none;'),
  1436. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1437. '/div'
  1438. );
  1439. $this->assertTags($result, $expected);
  1440. $result = $this->Form->error(
  1441. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1442. );
  1443. $this->assertEquals('Error, not registered', $result);
  1444. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1445. }
  1446. /**
  1447. * testFormValidationAssociatedFirstLevel method
  1448. *
  1449. * test form error display with associated model.
  1450. *
  1451. * @return void
  1452. */
  1453. public function testFormValidationAssociatedFirstLevel() {
  1454. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1455. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1456. $data = array(
  1457. 'ValidateUser' => array('name' => 'mariano'),
  1458. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1459. );
  1460. $result = $this->ValidateUser->create($data);
  1461. $this->assertFalse(empty($result));
  1462. $this->assertFalse($this->ValidateUser->validates());
  1463. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1464. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1465. $encoding = strtolower(Configure::read('App.encoding'));
  1466. $expected = array(
  1467. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1468. 'div' => array('style' => 'display:none;'),
  1469. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1470. '/div'
  1471. );
  1472. $this->assertTags($result, $expected);
  1473. $result = $this->Form->error(
  1474. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1475. );
  1476. $this->assertEquals('Invalid email', $result);
  1477. $result = $this->Form->error(
  1478. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1479. );
  1480. $this->assertEquals('Invalid name', $result);
  1481. $result = $this->Form->error(
  1482. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1483. );
  1484. $this->assertEquals('Invalid city', $result);
  1485. unset($this->ValidateUser->ValidateProfile);
  1486. unset($this->ValidateUser);
  1487. }
  1488. /**
  1489. * testFormValidationAssociatedSecondLevel method
  1490. *
  1491. * test form error display with associated model.
  1492. *
  1493. * @return void
  1494. */
  1495. public function testFormValidationAssociatedSecondLevel() {
  1496. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1497. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1498. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1499. $data = array(
  1500. 'ValidateUser' => array('name' => 'mariano'),
  1501. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1502. 'ValidateItem' => array('name' => 'Item')
  1503. );
  1504. $result = $this->ValidateUser->create($data);
  1505. $this->assertFalse(empty($result));
  1506. $this->assertFalse($this->ValidateUser->validates());
  1507. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1508. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1509. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1510. $encoding = strtolower(Configure::read('App.encoding'));
  1511. $expected = array(
  1512. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1513. 'div' => array('style' => 'display:none;'),
  1514. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1515. '/div'
  1516. );
  1517. $this->assertTags($result, $expected);
  1518. $result = $this->Form->error(
  1519. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1520. );
  1521. $this->assertEquals('Invalid email', $result);
  1522. $result = $this->Form->error(
  1523. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1524. );
  1525. $this->assertEquals('Invalid name', $result);
  1526. $result = $this->Form->error(
  1527. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1528. );
  1529. $result = $this->Form->error(
  1530. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1531. );
  1532. $this->assertEquals('Invalid description', $result);
  1533. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1534. unset($this->ValidateUser->ValidateProfile);
  1535. unset($this->ValidateUser);
  1536. }
  1537. /**
  1538. * testFormValidationMultiRecord method
  1539. *
  1540. * test form error display with multiple records.
  1541. *
  1542. * @return void
  1543. */
  1544. public function testFormValidationMultiRecord() {
  1545. $Contact = ClassRegistry::getObject('Contact');
  1546. $Contact->validationErrors[2] = array(
  1547. 'name' => array('This field cannot be left blank')
  1548. );
  1549. $result = $this->Form->input('Contact.2.name');
  1550. $expected = array(
  1551. 'div' => array('class' => 'input text error'),
  1552. 'label' => array('for' => 'Contact2Name'),
  1553. 'Name',
  1554. '/label',
  1555. 'input' => array(
  1556. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1557. 'class' => 'form-error', 'maxlength' => 255
  1558. ),
  1559. array('div' => array('class' => 'error-message')),
  1560. 'This field cannot be left blank',
  1561. '/div',
  1562. '/div'
  1563. );
  1564. $this->assertTags($result, $expected);
  1565. }
  1566. /**
  1567. * testMultipleInputValidation method
  1568. *
  1569. * test multiple record form validation error display.
  1570. *
  1571. * @return void
  1572. */
  1573. public function testMultipleInputValidation() {
  1574. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1575. $Address->validationErrors[0] = array(
  1576. 'title' => array('This field cannot be empty'),
  1577. 'first_name' => array('This field cannot be empty')
  1578. );
  1579. $Address->validationErrors[1] = array(
  1580. 'last_name' => array('You must have a last name')
  1581. );
  1582. $this->Form->create();
  1583. $result = $this->Form->input('Address.0.title');
  1584. $expected = array(
  1585. 'div' => array('class'),
  1586. 'label' => array('for'),
  1587. 'preg:/[^<]+/',
  1588. '/label',
  1589. 'input' => array(
  1590. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1591. ),
  1592. array('div' => array('class' => 'error-message')),
  1593. 'This field cannot be empty',
  1594. '/div',
  1595. '/div'
  1596. );
  1597. $this->assertTags($result, $expected);
  1598. $result = $this->Form->input('Address.0.first_name');
  1599. $expected = array(
  1600. 'div' => array('class'),
  1601. 'label' => array('for'),
  1602. 'preg:/[^<]+/',
  1603. '/label',
  1604. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1605. array('div' => array('class' => 'error-message')),
  1606. 'This field cannot be empty',
  1607. '/div',
  1608. '/div'
  1609. );
  1610. $this->assertTags($result, $expected);
  1611. $result = $this->Form->input('Address.0.last_name');
  1612. $expected = array(
  1613. 'div' => array('class'),
  1614. 'label' => array('for'),
  1615. 'preg:/[^<]+/',
  1616. '/label',
  1617. 'input' => array('type' => 'text', 'name', 'id'),
  1618. '/div'
  1619. );
  1620. $this->assertTags($result, $expected);
  1621. $result = $this->Form->input('Address.1.last_name');
  1622. $expected = array(
  1623. 'div' => array('class'),
  1624. 'label' => array('for'),
  1625. 'preg:/[^<]+/',
  1626. '/label',
  1627. 'input' => array(
  1628. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1629. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1630. ),
  1631. array('div' => array('class' => 'error-message')),
  1632. 'You must have a last name',
  1633. '/div',
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. }
  1638. /**
  1639. * testInput method
  1640. *
  1641. * Test various incarnations of input().
  1642. *
  1643. * @return void
  1644. */
  1645. public function testInput() {
  1646. $result = $this->Form->input('ValidateUser.balance');
  1647. $expected = array(
  1648. 'div' => array('class'),
  1649. 'label' => array('for'),
  1650. 'Balance',
  1651. '/label',
  1652. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1653. '/div',
  1654. );
  1655. $this->assertTags($result, $expected);
  1656. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1657. $expected = array(
  1658. 'div' => array('class' => 'input text'),
  1659. 'label' => array('for' => 'custom'),
  1660. 'Email',
  1661. '/label',
  1662. array('input' => array(
  1663. 'type' => 'text', 'name' => 'data[Contact][email]',
  1664. 'id' => 'custom', 'maxlength' => 255
  1665. )),
  1666. '/div'
  1667. );
  1668. $this->assertTags($result, $expected);
  1669. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1670. $expected = array(
  1671. '<div',
  1672. 'label' => array('for' => 'ContactEmail'),
  1673. 'Email',
  1674. '/label',
  1675. array('input' => array(
  1676. 'type' => 'text', 'name' => 'data[Contact][email]',
  1677. 'id' => 'ContactEmail', 'maxlength' => 255
  1678. )),
  1679. '/div'
  1680. );
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->hidden('Contact.idontexist');
  1683. $expected = array('input' => array(
  1684. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1685. 'id' => 'ContactIdontexist'
  1686. ));
  1687. $this->assertTags($result, $expected);
  1688. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1689. $expected = array(
  1690. 'div' => array('class' => 'input text'),
  1691. 'label' => array('for' => 'ContactEmail'),
  1692. 'Email',
  1693. '/label',
  1694. array('input' => array(
  1695. 'type' => 'text', 'name' => 'data[Contact][email]',
  1696. 'id' => 'ContactEmail'
  1697. )),
  1698. '/div'
  1699. );
  1700. $this->assertTags($result, $expected);
  1701. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1702. $expected = array(
  1703. 'div' => array('class' => 'input text'),
  1704. 'label' => array('for' => 'Contact5Email'),
  1705. 'Email',
  1706. '/label',
  1707. array('input' => array(
  1708. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1709. 'id' => 'Contact5Email'
  1710. )),
  1711. '/div'
  1712. );
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('Contact.password');
  1715. $expected = array(
  1716. 'div' => array('class' => 'input password'),
  1717. 'label' => array('for' => 'ContactPassword'),
  1718. 'Password',
  1719. '/label',
  1720. array('input' => array(
  1721. 'type' => 'password', 'name' => 'data[Contact][password]',
  1722. 'id' => 'ContactPassword'
  1723. )),
  1724. '/div'
  1725. );
  1726. $this->assertTags($result, $expected);
  1727. $result = $this->Form->input('Contact.email', array(
  1728. 'type' => 'file', 'class' => 'textbox'
  1729. ));
  1730. $expected = array(
  1731. 'div' => array('class' => 'input file'),
  1732. 'label' => array('for' => 'ContactEmail'),
  1733. 'Email',
  1734. '/label',
  1735. array('input' => array(
  1736. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1737. 'id' => 'ContactEmail'
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1743. $result = $this->Form->input('Contact.phone');
  1744. $expected = array(
  1745. 'div' => array('class' => 'input text'),
  1746. 'label' => array('for' => 'ContactPhone'),
  1747. 'Phone',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1751. 'value' => 'Hello &amp; World &gt; weird chars',
  1752. 'id' => 'ContactPhone', 'maxlength' => 255
  1753. )),
  1754. '/div'
  1755. );
  1756. $this->assertTags($result, $expected);
  1757. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1758. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1759. $expected = array(
  1760. 'div' => array('class' => 'input text'),
  1761. 'label' => array('for' => 'myId'),
  1762. 'Field',
  1763. '/label',
  1764. 'input' => array(
  1765. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1766. 'value' => 'My value', 'id' => 'myId'
  1767. ),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. unset($this->Form->request->data);
  1772. $Contact = ClassRegistry::getObject('Contact');
  1773. $Contact->validationErrors['field'] = array('Badness!');
  1774. $result = $this->Form->input('Contact.field');
  1775. $expected = array(
  1776. 'div' => array('class' => 'input text error'),
  1777. 'label' => array('for' => 'ContactField'),
  1778. 'Field',
  1779. '/label',
  1780. 'input' => array(
  1781. 'type' => 'text', 'name' => 'data[Contact][field]',
  1782. 'id' => 'ContactField', 'class' => 'form-error'
  1783. ),
  1784. array('div' => array('class' => 'error-message')),
  1785. 'Badness!',
  1786. '/div',
  1787. '/div'
  1788. );
  1789. $this->assertTags($result, $expected);
  1790. $result = $this->Form->input('Contact.field', array(
  1791. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1792. ));
  1793. $expected = array(
  1794. 'label' => array('for' => 'ContactField'),
  1795. 'Field',
  1796. '/label',
  1797. 'input' => array(
  1798. 'type' => 'text', 'name' => 'data[Contact][field]',
  1799. 'id' => 'ContactField', 'class' => 'form-error'
  1800. ),
  1801. array('span' => array('class' => 'error-message')),
  1802. 'Badness!',
  1803. '/span'
  1804. );
  1805. $this->assertTags($result, $expected);
  1806. $result = $this->Form->input('Contact.field', array(
  1807. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1808. ));
  1809. $expected = array(
  1810. 'div' => array('class' => 'input text error'),
  1811. 'label' => array('for' => 'ContactField'),
  1812. 'Field',
  1813. '/label',
  1814. 'input' => array(
  1815. 'type' => 'text', 'name' => 'data[Contact][field]',
  1816. 'id' => 'ContactField', 'class' => 'form-error'
  1817. ),
  1818. array('div' => array('class' => 'error')),
  1819. 'Badness!',
  1820. '/div'
  1821. );
  1822. $this->assertTags($result, $expected);
  1823. $result = $this->Form->input('Contact.field', array(
  1824. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1825. ));
  1826. $expected = array(
  1827. 'span' => array('class' => 'input text error'),
  1828. 'label' => array('for' => 'ContactField'),
  1829. 'Field',
  1830. '/label',
  1831. 'input' => array(
  1832. 'type' => 'text', 'name' => 'data[Contact][field]',
  1833. 'id' => 'ContactField', 'class' => 'form-error'
  1834. ),
  1835. 'Badness!',
  1836. '/span'
  1837. );
  1838. $this->assertTags($result, $expected);
  1839. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1840. $expected = array(
  1841. 'div' => array('class' => 'input text error'),
  1842. 'label' => array('for' => 'ContactField'),
  1843. 'Field',
  1844. '/label',
  1845. 'input' => array(
  1846. 'type' => 'text', 'name' => 'data[Contact][field]',
  1847. 'id' => 'ContactField', 'class' => 'form-error'
  1848. ),
  1849. 'A message to you, Rudy',
  1850. array('div' => array('class' => 'error-message')),
  1851. 'Badness!',
  1852. '/div',
  1853. '/div'
  1854. );
  1855. $this->assertTags($result, $expected);
  1856. $this->Form->setEntity(null);
  1857. $this->Form->setEntity('Contact.field');
  1858. $result = $this->Form->input('Contact.field', array(
  1859. 'after' => 'A message to you, Rudy', 'error' => false
  1860. ));
  1861. $expected = array(
  1862. 'div' => array('class' => 'input text'),
  1863. 'label' => array('for' => 'ContactField'),
  1864. 'Field',
  1865. '/label',
  1866. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1867. 'A message to you, Rudy',
  1868. '/div'
  1869. );
  1870. $this->assertTags($result, $expected);
  1871. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1872. $expected = array(
  1873. 'div' => array('class' => 'input text'),
  1874. 'label' => array('for' => 'ObjectField'),
  1875. 'Field',
  1876. '/label',
  1877. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1878. 'A message to you, Rudy',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $Contact->validationErrors['field'] = array('minLength');
  1883. $result = $this->Form->input('Contact.field', array(
  1884. 'error' => array(
  1885. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1886. 'maxLength' => 'login too large'
  1887. )
  1888. ));
  1889. $expected = array(
  1890. 'div' => array('class' => 'input text error'),
  1891. 'label' => array('for' => 'ContactField'),
  1892. 'Field',
  1893. '/label',
  1894. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1895. array('div' => array('class' => 'error-message')),
  1896. 'Le login doit contenir au moins 2 caractères',
  1897. '/div',
  1898. '/div'
  1899. );
  1900. $this->assertTags($result, $expected);
  1901. $Contact->validationErrors['field'] = array('maxLength');
  1902. $result = $this->Form->input('Contact.field', array(
  1903. 'error' => array(
  1904. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1905. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1906. 'maxLength' => 'login too large',
  1907. )
  1908. ));
  1909. $expected = array(
  1910. 'div' => array('class' => 'input text error'),
  1911. 'label' => array('for' => 'ContactField'),
  1912. 'Field',
  1913. '/label',
  1914. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1915. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1916. 'login too large',
  1917. '/span',
  1918. '/div'
  1919. );
  1920. $this->assertTags($result, $expected);
  1921. }
  1922. /**
  1923. * test input() with checkbox creation
  1924. *
  1925. * @return void
  1926. */
  1927. public function testInputCheckbox() {
  1928. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1929. $expected = array(
  1930. 'div' => array('class' => 'input checkbox'),
  1931. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1932. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1933. '/div'
  1934. );
  1935. $this->assertTags($result, $expected);
  1936. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1937. $expected = array(
  1938. 'div' => array('class' => 'input checkbox'),
  1939. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1940. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1941. '/div'
  1942. );
  1943. $this->assertTags($result, $expected);
  1944. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1945. $expected = array(
  1946. 'div' => array('class' => 'input checkbox'),
  1947. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1948. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1949. '/div'
  1950. );
  1951. $this->assertTags($result, $expected);
  1952. }
  1953. /**
  1954. * test form->input() with time types.
  1955. *
  1956. */
  1957. public function testInputTime() {
  1958. extract($this->dateRegex);
  1959. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1960. $result = explode(':', $result);
  1961. $this->assertRegExp('/option value="23"/', $result[0]);
  1962. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1963. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1964. $result = explode(':', $result);
  1965. $this->assertRegExp('/option value="23"/', $result[0]);
  1966. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1967. $result = $this->Form->input('Model.field', array(
  1968. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1969. ));
  1970. $result = explode(':', $result);
  1971. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1972. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1973. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1974. $result = $this->Form->input('Model.field', array(
  1975. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1976. ));
  1977. $result = explode(':', $result);
  1978. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1979. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1980. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1981. $result = $this->Form->input('prueba', array(
  1982. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1983. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1984. ));
  1985. $result = explode(':', $result);
  1986. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1987. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1988. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1989. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1990. $result = $this->Form->input('Random.start_time', array(
  1991. 'type' => 'time',
  1992. 'selected' => '18:15'
  1993. ));
  1994. $this->assertRegExp('#<option value="06"[^>]*>6</option>#', $result);
  1995. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result);
  1996. $this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
  1997. }
  1998. /**
  1999. * Test interval + selected near the hour roll over.
  2000. *
  2001. * @return void
  2002. */
  2003. public function testTimeSelectedWithInterval() {
  2004. $result = $this->Form->input('Model.start_time', array(
  2005. 'timeFormat' => 24,
  2006. 'type' => 'time',
  2007. 'interval' => 15,
  2008. 'selected' => '15:57'
  2009. ));
  2010. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2011. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2012. $result = $this->Form->input('Model.start_time', array(
  2013. 'timeFormat' => 24,
  2014. 'type' => 'time',
  2015. 'interval' => 15,
  2016. 'selected' => '23:57'
  2017. ));
  2018. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2019. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2020. $result = $this->Form->input('Model.created', array(
  2021. 'timeFormat' => 24,
  2022. 'type' => 'datetime',
  2023. 'interval' => 15,
  2024. 'selected' => '2012-09-30 23:56'
  2025. ));
  2026. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2027. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2028. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2029. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2030. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2031. }
  2032. /**
  2033. * test form->input() with datetime, date and time types
  2034. *
  2035. * @return void
  2036. */
  2037. public function testInputDatetime() {
  2038. extract($this->dateRegex);
  2039. $result = $this->Form->input('prueba', array(
  2040. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2041. 'maxYear' => date('Y') + 1, 'interval' => 15
  2042. ));
  2043. $result = explode(':', $result);
  2044. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2045. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2046. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2047. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2048. //related to ticket #5013
  2049. $result = $this->Form->input('Contact.date', array(
  2050. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2051. ));
  2052. $this->assertRegExp('/class="customClass"/', $result);
  2053. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2054. $result = $this->Form->input('Contact.date', array(
  2055. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2056. ));
  2057. $this->assertRegExp('/id="customIdDay"/', $result);
  2058. $this->assertRegExp('/id="customIdMonth"/', $result);
  2059. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2060. $result = $this->Form->input('Model.field', array(
  2061. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2062. ));
  2063. $this->assertRegExp('/id="customIDDay"/', $result);
  2064. $this->assertRegExp('/id="customIDHour"/', $result);
  2065. $result = explode('</select><select', $result);
  2066. $result = explode(':', $result[1]);
  2067. $this->assertRegExp('/option value="23"/', $result[0]);
  2068. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2069. $result = $this->Form->input('Model.field', array(
  2070. 'type' => 'datetime', 'timeFormat' => 12
  2071. ));
  2072. $result = explode('</select><select', $result);
  2073. $result = explode(':', $result[1]);
  2074. $this->assertRegExp('/option value="12"/', $result[0]);
  2075. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2076. $this->Form->request->data = array('Contact' => array('created' => null));
  2077. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2078. $expected = array(
  2079. 'div' => array('class' => 'input date'),
  2080. 'label' => array('for' => 'ContactCreatedMonth'),
  2081. 'Created',
  2082. '/label',
  2083. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2084. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2085. $monthsRegex,
  2086. '/select', '-',
  2087. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2088. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2089. $daysRegex,
  2090. '/select', '-',
  2091. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2092. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2093. $yearsRegex,
  2094. '/select',
  2095. '/div'
  2096. );
  2097. $this->assertTags($result, $expected);
  2098. $this->Form->request->data = array('Contact' => array('created' => null));
  2099. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2100. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2101. $this->Form->request->data = array('Contact' => array('created' => null));
  2102. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2103. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2104. $result = $this->Form->input('Contact.created', array(
  2105. 'type' => 'date',
  2106. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2107. 'timeFormat' => 'NONE'
  2108. ));
  2109. $this->assertRegExp('/for\="created-month"/', $result);
  2110. }
  2111. /**
  2112. * Test generating checkboxes in a loop.
  2113. *
  2114. * @return void
  2115. */
  2116. public function testInputCheckboxesInLoop() {
  2117. for ($i = 1; $i < 5; $i++) {
  2118. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2119. $expected = array(
  2120. 'div' => array('class' => 'input checkbox'),
  2121. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2122. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2123. 'label' => array('for' => "Contact{$i}Email"),
  2124. 'Email',
  2125. '/label',
  2126. '/div'
  2127. );
  2128. $this->assertTags($result, $expected);
  2129. }
  2130. }
  2131. /**
  2132. * test input name with leading integer, ensure attributes are generated correctly.
  2133. *
  2134. * @return void
  2135. */
  2136. public function testInputWithLeadingInteger() {
  2137. $result = $this->Form->text('0.Node.title');
  2138. $expected = array(
  2139. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2140. );
  2141. $this->assertTags($result, $expected);
  2142. }
  2143. /**
  2144. * test form->input() with select type inputs.
  2145. *
  2146. * @return void
  2147. */
  2148. public function testInputSelectType() {
  2149. $result = $this->Form->input('email', array(
  2150. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2151. );
  2152. $expected = array(
  2153. 'div' => array('class' => 'input select'),
  2154. 'label' => array('for' => 'email'),
  2155. 'Email',
  2156. '/label',
  2157. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2158. array('option' => array('value' => '')),
  2159. '/option',
  2160. array('option' => array('value' => 'è')),
  2161. 'Firést',
  2162. '/option',
  2163. array('option' => array('value' => 'é')),
  2164. 'Secoènd',
  2165. '/option',
  2166. '/select',
  2167. '/div'
  2168. );
  2169. $this->assertTags($result, $expected);
  2170. $result = $this->Form->input('email', array(
  2171. 'options' => array('First', 'Second'), 'empty' => true)
  2172. );
  2173. $expected = array(
  2174. 'div' => array('class' => 'input select'),
  2175. 'label' => array('for' => 'email'),
  2176. 'Email',
  2177. '/label',
  2178. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2179. array('option' => array('value' => '')),
  2180. '/option',
  2181. array('option' => array('value' => '0')),
  2182. 'First',
  2183. '/option',
  2184. array('option' => array('value' => '1')),
  2185. 'Second',
  2186. '/option',
  2187. '/select',
  2188. '/div'
  2189. );
  2190. $this->assertTags($result, $expected);
  2191. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2192. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2193. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2194. $expected = array(
  2195. 'div' => array('class' => 'input select'),
  2196. 'label' => array('for' => 'ModelUserId'),
  2197. 'User',
  2198. '/label',
  2199. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2200. array('option' => array('value' => '')),
  2201. '/option',
  2202. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2203. 'good',
  2204. '/option',
  2205. array('option' => array('value' => 'other')),
  2206. 'bad',
  2207. '/option',
  2208. '/select',
  2209. '/div'
  2210. );
  2211. $this->assertTags($result, $expected);
  2212. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2213. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2214. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2215. $expected = array(
  2216. 'div' => array('class' => 'input select'),
  2217. 'label' => array('for' => 'ThingUserId'),
  2218. 'User',
  2219. '/label',
  2220. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2221. array('option' => array('value' => '')),
  2222. 'Some Empty',
  2223. '/option',
  2224. array('option' => array('value' => 'value')),
  2225. 'good',
  2226. '/option',
  2227. array('option' => array('value' => 'other')),
  2228. 'bad',
  2229. '/option',
  2230. '/select',
  2231. '/div'
  2232. );
  2233. $this->assertTags($result, $expected);
  2234. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2235. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2236. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2237. $expected = array(
  2238. 'div' => array('class' => 'input select'),
  2239. 'label' => array('for' => 'ThingUserId'),
  2240. 'User',
  2241. '/label',
  2242. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2243. array('option' => array('value' => '')),
  2244. 'Some Empty',
  2245. '/option',
  2246. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2247. 'good',
  2248. '/option',
  2249. array('option' => array('value' => 'other')),
  2250. 'bad',
  2251. '/option',
  2252. '/select',
  2253. '/div'
  2254. );
  2255. $this->assertTags($result, $expected);
  2256. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2257. $this->Form->request->data = array('User' => array('User' => array('value')));
  2258. $result = $this->Form->input('User.User', array('empty' => true));
  2259. $expected = array(
  2260. 'div' => array('class' => 'input select'),
  2261. 'label' => array('for' => 'UserUser'),
  2262. 'User',
  2263. '/label',
  2264. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2265. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2266. array('option' => array('value' => '')),
  2267. '/option',
  2268. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2269. 'good',
  2270. '/option',
  2271. array('option' => array('value' => 'other')),
  2272. 'bad',
  2273. '/option',
  2274. '/select',
  2275. '/div'
  2276. );
  2277. $this->assertTags($result, $expected);
  2278. $this->Form->data = array();
  2279. $result = $this->Form->input('Publisher.id', array(
  2280. 'label' => 'Publisher',
  2281. 'type' => 'select',
  2282. 'multiple' => 'checkbox',
  2283. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2284. ));
  2285. $expected = array(
  2286. array('div' => array('class' => 'input select')),
  2287. array('label' => array('for' => 'PublisherId')),
  2288. 'Publisher',
  2289. '/label',
  2290. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2291. array('div' => array('class' => 'checkbox')),
  2292. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2293. array('label' => array('for' => 'PublisherIdValue1')),
  2294. 'Label 1',
  2295. '/label',
  2296. '/div',
  2297. array('div' => array('class' => 'checkbox')),
  2298. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2299. array('label' => array('for' => 'PublisherIdValue2')),
  2300. 'Label 2',
  2301. '/label',
  2302. '/div',
  2303. '/div'
  2304. );
  2305. $this->assertTags($result, $expected);
  2306. }
  2307. /**
  2308. * test that input() and a non standard primary key makes a hidden input by default.
  2309. *
  2310. * @return void
  2311. */
  2312. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2313. $this->Form->create('User');
  2314. $this->Form->fieldset = array(
  2315. 'User' => array(
  2316. 'fields' => array(
  2317. 'model_id' => array('type' => 'integer')
  2318. ),
  2319. 'validates' => array(),
  2320. 'key' => 'model_id'
  2321. )
  2322. );
  2323. $result = $this->Form->input('model_id');
  2324. $expected = array(
  2325. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2326. );
  2327. $this->assertTags($result, $expected);
  2328. }
  2329. /**
  2330. * test that overriding the magic select type widget is possible
  2331. *
  2332. * @return void
  2333. */
  2334. public function testInputOverridingMagicSelectType() {
  2335. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2336. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2337. $expected = array(
  2338. 'div' => array('class' => 'input text'),
  2339. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2340. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2341. '/div'
  2342. );
  2343. $this->assertTags($result, $expected);
  2344. //Check that magic types still work for plural/singular vars
  2345. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2346. $result = $this->Form->input('Model.type');
  2347. $expected = array(
  2348. 'div' => array('class' => 'input select'),
  2349. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2350. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2351. array('option' => array('value' => 'value')), 'good', '/option',
  2352. array('option' => array('value' => 'other')), 'bad', '/option',
  2353. '/select',
  2354. '/div'
  2355. );
  2356. $this->assertTags($result, $expected);
  2357. }
  2358. /**
  2359. * Test that magic input() selects can easily be converted into radio types without error.
  2360. *
  2361. * @return void
  2362. */
  2363. public function testInputMagicSelectChangeToRadio() {
  2364. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2365. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2366. $this->assertRegExp('/input type="radio"/', $result);
  2367. }
  2368. /**
  2369. * fields with the same name as the model should work.
  2370. *
  2371. * @return void
  2372. */
  2373. public function testInputWithMatchingFieldAndModelName() {
  2374. $this->Form->create('User');
  2375. $this->Form->fieldset = array(
  2376. 'User' => array(
  2377. 'fields' => array(
  2378. 'User' => array('type' => 'text')
  2379. ),
  2380. 'validates' => array(),
  2381. 'key' => 'id'
  2382. )
  2383. );
  2384. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2385. $result = $this->Form->input('User', array('type' => 'text'));
  2386. $expected = array(
  2387. 'div' => array('class' => 'input text'),
  2388. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2389. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2390. '/div'
  2391. );
  2392. $this->assertTags($result, $expected);
  2393. }
  2394. /**
  2395. * testFormInputs method
  2396. *
  2397. * test correct results from form::inputs().
  2398. *
  2399. * @return void
  2400. */
  2401. public function testFormInputs() {
  2402. $this->Form->create('Contact');
  2403. $result = $this->Form->inputs('The Legend');
  2404. $expected = array(
  2405. '<fieldset',
  2406. '<legend',
  2407. 'The Legend',
  2408. '/legend',
  2409. '*/fieldset',
  2410. );
  2411. $this->assertTags($result, $expected);
  2412. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2413. $expected = array(
  2414. 'fieldset' => array('class' => 'classy-stuff'),
  2415. '<legend',
  2416. 'Field of Dreams',
  2417. '/legend',
  2418. '*/fieldset'
  2419. );
  2420. $this->assertTags($result, $expected);
  2421. $this->Form->create('Contact');
  2422. $this->Form->request['prefix'] = 'admin';
  2423. $this->Form->request['action'] = 'admin_edit';
  2424. $result = $this->Form->inputs();
  2425. $expected = array(
  2426. '<fieldset',
  2427. '<legend',
  2428. 'Edit Contact',
  2429. '/legend',
  2430. '*/fieldset',
  2431. );
  2432. $this->assertTags($result, $expected);
  2433. $this->Form->create('Contact');
  2434. $result = $this->Form->inputs(false);
  2435. $expected = array(
  2436. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2437. array('div' => array('class' => 'input text')),
  2438. '*/div',
  2439. array('div' => array('class' => 'input text')),
  2440. '*/div',
  2441. array('div' => array('class' => 'input text')),
  2442. '*/div',
  2443. array('div' => array('class' => 'input password')),
  2444. '*/div',
  2445. array('div' => array('class' => 'input date')),
  2446. '*/div',
  2447. array('div' => array('class' => 'input date')),
  2448. '*/div',
  2449. array('div' => array('class' => 'input datetime')),
  2450. '*/div',
  2451. array('div' => array('class' => 'input number')),
  2452. '*/div',
  2453. array('div' => array('class' => 'input select')),
  2454. '*/div',
  2455. );
  2456. $this->assertTags($result, $expected);
  2457. $this->Form->create('Contact');
  2458. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2459. $expected = array(
  2460. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2461. array('div' => array('class' => 'input text')),
  2462. '*/div',
  2463. array('div' => array('class' => 'input text')),
  2464. '*/div',
  2465. array('div' => array('class' => 'input text')),
  2466. '*/div',
  2467. array('div' => array('class' => 'input password')),
  2468. '*/div',
  2469. array('div' => array('class' => 'input date')),
  2470. '*/div',
  2471. array('div' => array('class' => 'input date')),
  2472. '*/div',
  2473. array('div' => array('class' => 'input datetime')),
  2474. '*/div',
  2475. array('div' => array('class' => 'input number')),
  2476. '*/div',
  2477. array('div' => array('class' => 'input select')),
  2478. '*/div',
  2479. );
  2480. $this->assertTags($result, $expected);
  2481. $this->Form->create('Contact');
  2482. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2483. $expected = array(
  2484. 'fieldset' => array(),
  2485. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2486. array('div' => array('class' => 'input text')),
  2487. '*/div',
  2488. array('div' => array('class' => 'input text')),
  2489. '*/div',
  2490. array('div' => array('class' => 'input text')),
  2491. '*/div',
  2492. array('div' => array('class' => 'input password')),
  2493. '*/div',
  2494. array('div' => array('class' => 'input date')),
  2495. '*/div',
  2496. array('div' => array('class' => 'input date')),
  2497. '*/div',
  2498. array('div' => array('class' => 'input datetime')),
  2499. '*/div',
  2500. array('div' => array('class' => 'input number')),
  2501. '*/div',
  2502. array('div' => array('class' => 'input select')),
  2503. '*/div',
  2504. '/fieldset'
  2505. );
  2506. $this->assertTags($result, $expected);
  2507. $this->Form->create('Contact');
  2508. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2509. $expected = array(
  2510. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2511. array('div' => array('class' => 'input text')),
  2512. '*/div',
  2513. array('div' => array('class' => 'input text')),
  2514. '*/div',
  2515. array('div' => array('class' => 'input text')),
  2516. '*/div',
  2517. array('div' => array('class' => 'input password')),
  2518. '*/div',
  2519. array('div' => array('class' => 'input date')),
  2520. '*/div',
  2521. array('div' => array('class' => 'input date')),
  2522. '*/div',
  2523. array('div' => array('class' => 'input datetime')),
  2524. '*/div',
  2525. array('div' => array('class' => 'input number')),
  2526. '*/div',
  2527. array('div' => array('class' => 'input select')),
  2528. '*/div',
  2529. );
  2530. $this->assertTags($result, $expected);
  2531. $this->Form->create('Contact');
  2532. $result = $this->Form->inputs('Hello');
  2533. $expected = array(
  2534. 'fieldset' => array(),
  2535. 'legend' => array(),
  2536. 'Hello',
  2537. '/legend',
  2538. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2539. array('div' => array('class' => 'input text')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input text')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input text')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input password')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input date')),
  2548. '*/div',
  2549. array('div' => array('class' => 'input date')),
  2550. '*/div',
  2551. array('div' => array('class' => 'input datetime')),
  2552. '*/div',
  2553. array('div' => array('class' => 'input number')),
  2554. '*/div',
  2555. array('div' => array('class' => 'input select')),
  2556. '*/div',
  2557. '/fieldset'
  2558. );
  2559. $this->assertTags($result, $expected);
  2560. $this->Form->create('Contact');
  2561. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2562. $expected = array(
  2563. 'fieldset' => array(),
  2564. 'legend' => array(),
  2565. 'Hello',
  2566. '/legend',
  2567. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2568. array('div' => array('class' => 'input text')),
  2569. '*/div',
  2570. array('div' => array('class' => 'input text')),
  2571. '*/div',
  2572. array('div' => array('class' => 'input text')),
  2573. '*/div',
  2574. array('div' => array('class' => 'input password')),
  2575. '*/div',
  2576. array('div' => array('class' => 'input date')),
  2577. '*/div',
  2578. array('div' => array('class' => 'input date')),
  2579. '*/div',
  2580. array('div' => array('class' => 'input datetime')),
  2581. '*/div',
  2582. array('div' => array('class' => 'input number')),
  2583. '*/div',
  2584. array('div' => array('class' => 'input select')),
  2585. '*/div',
  2586. '/fieldset'
  2587. );
  2588. $this->assertTags($result, $expected);
  2589. }
  2590. /**
  2591. * testSelectAsCheckbox method
  2592. *
  2593. * test multi-select widget with checkbox formatting.
  2594. *
  2595. * @return void
  2596. */
  2597. public function testSelectAsCheckbox() {
  2598. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2599. $expected = array(
  2600. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2601. array('div' => array('class' => 'checkbox')),
  2602. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2603. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2604. 'first',
  2605. '/label',
  2606. '/div',
  2607. array('div' => array('class' => 'checkbox')),
  2608. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2609. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2610. 'second',
  2611. '/label',
  2612. '/div',
  2613. array('div' => array('class' => 'checkbox')),
  2614. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2615. array('label' => array('for' => 'ModelMultiField2')),
  2616. 'third',
  2617. '/label',
  2618. '/div',
  2619. );
  2620. $this->assertTags($result, $expected);
  2621. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2622. $expected = array(
  2623. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2624. array('div' => array('class' => 'checkbox')),
  2625. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2626. array('label' => array('for' => 'ModelMultiField12')),
  2627. 'half',
  2628. '/label',
  2629. '/div',
  2630. );
  2631. $this->assertTags($result, $expected);
  2632. }
  2633. /**
  2634. * testLabel method
  2635. *
  2636. * test label generation.
  2637. *
  2638. * @return void
  2639. */
  2640. public function testLabel() {
  2641. $this->Form->text('Person.name');
  2642. $result = $this->Form->label();
  2643. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2644. $this->Form->text('Person.name');
  2645. $result = $this->Form->label();
  2646. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2647. $result = $this->Form->label('Person.first_name');
  2648. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2649. $result = $this->Form->label('Person.first_name', 'Your first name');
  2650. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2651. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2652. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2653. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2654. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2655. $result = $this->Form->label('Person.first_name', '');
  2656. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2657. $result = $this->Form->label('Person.2.name', '');
  2658. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2659. }
  2660. /**
  2661. * testTextbox method
  2662. *
  2663. * test textbox element generation
  2664. *
  2665. * @return void
  2666. */
  2667. public function testTextbox() {
  2668. $result = $this->Form->text('Model.field');
  2669. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2670. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2671. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2672. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2673. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2674. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2675. $result = $this->Form->text('Model.text');
  2676. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2677. $Contact = ClassRegistry::getObject('Contact');
  2678. $Contact->validationErrors['text'] = array(true);
  2679. $this->Form->request->data['Contact']['text'] = 'test';
  2680. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2681. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2682. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2683. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2684. $expected = array(
  2685. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2686. );
  2687. $this->assertTags($result, $expected);
  2688. }
  2689. /**
  2690. * testDefaultValue method
  2691. *
  2692. * Test default value setting
  2693. *
  2694. * @return void
  2695. */
  2696. public function testDefaultValue() {
  2697. $this->Form->request->data['Model']['field'] = 'test';
  2698. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2699. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2700. unset($this->Form->request->data['Model']['field']);
  2701. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2702. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2703. }
  2704. /**
  2705. * testCheckboxDefaultValue method
  2706. *
  2707. * Test default value setting on checkbox() method
  2708. *
  2709. * @return void
  2710. */
  2711. public function testCheckboxDefaultValue() {
  2712. $this->Form->request->data['Model']['field'] = false;
  2713. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2714. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2715. unset($this->Form->request->data['Model']['field']);
  2716. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2717. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2718. $this->Form->request->data['Model']['field'] = true;
  2719. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2720. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2721. unset($this->Form->request->data['Model']['field']);
  2722. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2723. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2724. }
  2725. /**
  2726. * testError method
  2727. *
  2728. * Test field error generation
  2729. *
  2730. * @return void
  2731. */
  2732. public function testError() {
  2733. $Contact = ClassRegistry::getObject('Contact');
  2734. $Contact->validationErrors['field'] = array(1);
  2735. $result = $this->Form->error('Contact.field');
  2736. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2737. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2738. $this->assertEquals('Error in field Field', $result);
  2739. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2740. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2741. $this->assertEquals('This field contains invalid input', $result);
  2742. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2743. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2744. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2745. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2746. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2747. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2748. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2749. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2750. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2751. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2752. $this->assertEquals('<strong>Badness!</strong>', $result);
  2753. $Contact->validationErrors['field'] = array("email");
  2754. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2755. $expected = array(
  2756. 'div' => array('class' => 'field-error'),
  2757. 'No good!',
  2758. '/div'
  2759. );
  2760. $this->assertTags($result, $expected);
  2761. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2762. $result = $this->Form->error('Contact.field', array(
  2763. 'notEmpty' => 'Cannot be empty',
  2764. 'email' => 'No good!'
  2765. ));
  2766. $expected = array(
  2767. 'div' => array('class' => 'error-message'),
  2768. 'ul' => array(),
  2769. '<li', 'Cannot be empty', '/li',
  2770. '<li', 'No good!', '/li',
  2771. '<li', 'Something else', '/li',
  2772. '/ul',
  2773. '/div'
  2774. );
  2775. $this->assertTags($result, $expected);
  2776. /** Testing error messages list options **/
  2777. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2778. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2779. $expected = array(
  2780. 'div' => array('class' => 'error-message'),
  2781. 'ol' => array(),
  2782. '<li', 'notEmpty', '/li',
  2783. '<li', 'email', '/li',
  2784. '/ol',
  2785. '/div'
  2786. );
  2787. $this->assertTags($result, $expected);
  2788. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2789. $expected = array(
  2790. 'div' => array('class' => 'error-message'),
  2791. 'ol' => array(),
  2792. '<li', 'notEmpty', '/li',
  2793. '<li', 'email', '/li',
  2794. '/ol',
  2795. '/div'
  2796. );
  2797. $this->assertTags($result, $expected);
  2798. $result = $this->Form->error('Contact.field', null, array(
  2799. 'listOptions' => array(
  2800. 'class' => 'ul-class',
  2801. 'itemOptions' => array(
  2802. 'class' => 'li-class'
  2803. )
  2804. )
  2805. ));
  2806. $expected = array(
  2807. 'div' => array('class' => 'error-message'),
  2808. 'ul' => array('class' => 'ul-class'),
  2809. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2810. array('li' => array('class' => 'li-class')), 'email', '/li',
  2811. '/ul',
  2812. '/div'
  2813. );
  2814. $this->assertTags($result, $expected);
  2815. }
  2816. /**
  2817. * test error options when using form->input();
  2818. *
  2819. * @return void
  2820. */
  2821. public function testInputErrorEscape() {
  2822. $this->Form->create('ValidateProfile');
  2823. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2824. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2825. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2826. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2827. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2828. $this->assertRegExp('/required<br>/', $result);
  2829. }
  2830. /**
  2831. * testPassword method
  2832. *
  2833. * Test password element generation
  2834. *
  2835. * @return void
  2836. */
  2837. public function testPassword() {
  2838. $Contact = ClassRegistry::getObject('Contact');
  2839. $result = $this->Form->password('Contact.field');
  2840. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2841. $Contact->validationErrors['passwd'] = 1;
  2842. $this->Form->request->data['Contact']['passwd'] = 'test';
  2843. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2844. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2845. }
  2846. /**
  2847. * testRadio method
  2848. *
  2849. * Test radio element set generation
  2850. *
  2851. * @return void
  2852. */
  2853. public function testRadio() {
  2854. $result = $this->Form->radio('Model.field', array('option A'));
  2855. $expected = array(
  2856. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2857. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2858. 'label' => array('for' => 'ModelField0'),
  2859. 'option A',
  2860. '/label'
  2861. );
  2862. $this->assertTags($result, $expected);
  2863. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2864. $expected = array(
  2865. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2866. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2867. 'label' => array('for' => 'ModelField12'),
  2868. 'half',
  2869. '/label'
  2870. );
  2871. $this->assertTags($result, $expected);
  2872. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2873. $expected = array(
  2874. 'fieldset' => array(),
  2875. 'legend' => array(),
  2876. 'Field',
  2877. '/legend',
  2878. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2879. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2880. array('label' => array('for' => 'ModelField0')),
  2881. 'option A',
  2882. '/label',
  2883. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2884. array('label' => array('for' => 'ModelField1')),
  2885. 'option B',
  2886. '/label',
  2887. '/fieldset'
  2888. );
  2889. $this->assertTags($result, $expected);
  2890. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2891. $expected = array(
  2892. 'fieldset' => array(),
  2893. 'legend' => array(),
  2894. 'Field',
  2895. '/legend',
  2896. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2897. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2898. array('label' => array('for' => 'ModelField0')),
  2899. 'option A',
  2900. '/label',
  2901. 'br' => array(),
  2902. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2903. array('label' => array('for' => 'ModelField1')),
  2904. 'option B',
  2905. '/label',
  2906. '/fieldset'
  2907. );
  2908. $this->assertTags($result, $expected);
  2909. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2910. $expected = array(
  2911. 'div' => array('class' => 'input radio'),
  2912. 'fieldset' => array(),
  2913. 'legend' => array(),
  2914. 'Legend title',
  2915. '/legend',
  2916. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2917. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2918. array('label' => array('for' => 'NewsletterSubscribe0')),
  2919. 'Unsubscribe',
  2920. '/label',
  2921. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2922. array('label' => array('for' => 'NewsletterSubscribe1')),
  2923. 'Subscribe',
  2924. '/label',
  2925. '/fieldset',
  2926. '/div'
  2927. );
  2928. $this->assertTags($result, $expected);
  2929. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2930. $expected = array(
  2931. 'div' => array('class' => 'input radio'),
  2932. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2933. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2934. array('label' => array('for' => 'NewsletterSubscribe0')),
  2935. 'Unsubscribe',
  2936. '/label',
  2937. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2938. array('label' => array('for' => 'NewsletterSubscribe1')),
  2939. 'Subscribe',
  2940. '/label',
  2941. '/div'
  2942. );
  2943. $this->assertTags($result, $expected);
  2944. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2945. $expected = array(
  2946. 'div' => array('class' => 'input radio'),
  2947. 'fieldset' => array(),
  2948. 'legend' => array(),
  2949. 'Legend title',
  2950. '/legend',
  2951. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2952. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2953. 'Unsubscribe',
  2954. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2955. 'Subscribe',
  2956. '/fieldset',
  2957. '/div'
  2958. );
  2959. $this->assertTags($result, $expected);
  2960. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2961. $expected = array(
  2962. 'div' => array('class' => 'input radio'),
  2963. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2964. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2965. 'Unsubscribe',
  2966. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2967. 'Subscribe',
  2968. '/div'
  2969. );
  2970. $this->assertTags($result, $expected);
  2971. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2972. $expected = array(
  2973. 'div' => array('class' => 'input radio'),
  2974. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2975. 'Unsubscribe',
  2976. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  2977. 'Subscribe',
  2978. '/div'
  2979. );
  2980. $this->assertTags($result, $expected);
  2981. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2982. $expected = array(
  2983. 'fieldset' => array(),
  2984. 'legend' => array(),
  2985. 'Gender',
  2986. '/legend',
  2987. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  2988. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  2989. array('label' => array('for' => 'EmployeeGenderMale')),
  2990. 'Male',
  2991. '/label',
  2992. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  2993. array('label' => array('for' => 'EmployeeGenderFemale')),
  2994. 'Female',
  2995. '/label',
  2996. '/fieldset',
  2997. );
  2998. $this->assertTags($result, $expected);
  2999. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3000. $expected = array(
  3001. 'fieldset' => array(),
  3002. 'legend' => array(),
  3003. 'Gender',
  3004. '/legend',
  3005. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3006. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3007. array('label' => array('for' => 'OfficerGenderMale')),
  3008. 'Male',
  3009. '/label',
  3010. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3011. array('label' => array('for' => 'OfficerGenderFemale')),
  3012. 'Female',
  3013. '/label',
  3014. '/fieldset',
  3015. );
  3016. $this->assertTags($result, $expected);
  3017. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3018. $expected = array(
  3019. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3020. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3021. 'label' => array('for' => 'Contact1Imrequired0'),
  3022. 'option A',
  3023. '/label'
  3024. );
  3025. $this->assertTags($result, $expected);
  3026. $result = $this->Form->radio('Model.1.field', array('option A'));
  3027. $expected = array(
  3028. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3029. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3030. 'label' => array('for' => 'Model1Field0'),
  3031. 'option A',
  3032. '/label'
  3033. );
  3034. $this->assertTags($result, $expected);
  3035. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3036. $expected = array(
  3037. 'fieldset' => array(),
  3038. 'legend' => array(),
  3039. 'Field',
  3040. '/legend',
  3041. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3042. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3043. array('label' => array('for' => 'ModelField0')),
  3044. 'option A',
  3045. '/label',
  3046. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3047. array('label' => array('for' => 'ModelField1')),
  3048. 'option B',
  3049. '/label',
  3050. '/fieldset'
  3051. );
  3052. $this->assertTags($result, $expected);
  3053. $result = $this->Form->radio(
  3054. 'Model.field',
  3055. array('option A', 'option B'),
  3056. array('between' => 'I am between')
  3057. );
  3058. $expected = array(
  3059. 'fieldset' => array(),
  3060. 'legend' => array(),
  3061. 'Field',
  3062. '/legend',
  3063. 'I am between',
  3064. 'input' => array(
  3065. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3066. 'value' => '', 'id' => 'ModelField_'
  3067. ),
  3068. array('input' => array(
  3069. 'type' => 'radio', 'name' => 'data[Model][field]',
  3070. 'value' => '0', 'id' => 'ModelField0'
  3071. )),
  3072. array('label' => array('for' => 'ModelField0')),
  3073. 'option A',
  3074. '/label',
  3075. array('input' => array(
  3076. 'type' => 'radio', 'name' => 'data[Model][field]',
  3077. 'value' => '1', 'id' => 'ModelField1'
  3078. )),
  3079. array('label' => array('for' => 'ModelField1')),
  3080. 'option B',
  3081. '/label',
  3082. '/fieldset'
  3083. );
  3084. $this->assertTags($result, $expected);
  3085. }
  3086. /**
  3087. * Test that radios with a 0 value are selected under the correct conditions.
  3088. *
  3089. * @return void
  3090. */
  3091. public function testRadioOptionWithZeroValue() {
  3092. $expected = array(
  3093. 'fieldset' => array(),
  3094. 'legend' => array(),
  3095. 'Field',
  3096. '/legend',
  3097. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3098. array('label' => array('for' => 'ModelField1')),
  3099. 'Yes',
  3100. '/label',
  3101. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3102. array('label' => array('for' => 'ModelField0')),
  3103. 'No',
  3104. '/label',
  3105. '/fieldset'
  3106. );
  3107. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3108. $this->assertTags($result, $expected);
  3109. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3110. $this->assertTags($result, $expected);
  3111. $expected = array(
  3112. 'fieldset' => array(),
  3113. 'legend' => array(),
  3114. 'Field',
  3115. '/legend',
  3116. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3117. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3118. array('label' => array('for' => 'ModelField1')),
  3119. 'Yes',
  3120. '/label',
  3121. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3122. array('label' => array('for' => 'ModelField0')),
  3123. 'No',
  3124. '/label',
  3125. '/fieldset'
  3126. );
  3127. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3128. $this->assertTags($result, $expected);
  3129. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3130. $this->assertTags($result, $expected);
  3131. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3132. $this->assertTags($result, $expected);
  3133. }
  3134. /**
  3135. * test disabled radio options
  3136. *
  3137. * @return void
  3138. */
  3139. public function testRadioDisabled() {
  3140. $result = $this->Form->radio(
  3141. 'Model.field',
  3142. array('option A', 'option B'),
  3143. array('disabled' => array('option A'), 'value' => '0')
  3144. );
  3145. $expected = array(
  3146. 'fieldset' => array(),
  3147. 'legend' => array(),
  3148. 'Field',
  3149. '/legend',
  3150. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3151. array('label' => array('for' => 'ModelField0')),
  3152. 'option A',
  3153. '/label',
  3154. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3155. array('label' => array('for' => 'ModelField1')),
  3156. 'option B',
  3157. '/label',
  3158. '/fieldset'
  3159. );
  3160. $this->assertTags($result, $expected);
  3161. $result = $this->Form->radio(
  3162. 'Model.field',
  3163. array('option A', 'option B'),
  3164. array('disabled' => true, 'value' => '0')
  3165. );
  3166. $expected = array(
  3167. 'fieldset' => array(),
  3168. 'legend' => array(),
  3169. 'Field',
  3170. '/legend',
  3171. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3172. array('label' => array('for' => 'ModelField0')),
  3173. 'option A',
  3174. '/label',
  3175. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3176. array('label' => array('for' => 'ModelField1')),
  3177. 'option B',
  3178. '/label',
  3179. '/fieldset'
  3180. );
  3181. $this->assertTags($result, $expected);
  3182. $result = $this->Form->radio(
  3183. 'Model.field',
  3184. array('option A', 'option B'),
  3185. array('disabled' => 'disabled', 'value' => '0')
  3186. );
  3187. $expected = array(
  3188. 'fieldset' => array(),
  3189. 'legend' => array(),
  3190. 'Field',
  3191. '/legend',
  3192. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3193. array('label' => array('for' => 'ModelField0')),
  3194. 'option A',
  3195. '/label',
  3196. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3197. array('label' => array('for' => 'ModelField1')),
  3198. 'option B',
  3199. '/label',
  3200. '/fieldset'
  3201. );
  3202. $this->assertTags($result, $expected);
  3203. }
  3204. /**
  3205. * test disabling the hidden input for radio buttons
  3206. *
  3207. * @return void
  3208. */
  3209. public function testRadioHiddenInputDisabling() {
  3210. $result = $this->Form->input('Model.1.field', array(
  3211. 'type' => 'radio',
  3212. 'options' => array('option A'),
  3213. 'hiddenField' => false
  3214. )
  3215. );
  3216. $expected = array(
  3217. 'div' => array('class' => 'input radio'),
  3218. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3219. 'label' => array('for' => 'Model1Field0'),
  3220. 'option A',
  3221. '/label',
  3222. '/div'
  3223. );
  3224. $this->assertTags($result, $expected);
  3225. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3226. $expected = array(
  3227. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3228. 'label' => array('for' => 'Model1Field0'),
  3229. 'option A',
  3230. '/label'
  3231. );
  3232. $this->assertTags($result, $expected);
  3233. }
  3234. /**
  3235. * test adding an empty option for radio buttons
  3236. *
  3237. * @return void
  3238. */
  3239. public function testRadioAddEmptyOption() {
  3240. $result = $this->Form->input('Model.1.field', array(
  3241. 'type' => 'radio',
  3242. 'options' => array('option A'),
  3243. 'empty' => true,
  3244. 'hiddenField' => false
  3245. ));
  3246. $expected = array(
  3247. 'div' => array('class' => 'input radio'),
  3248. 'fieldset' => array(),
  3249. 'legend' => array(),
  3250. 'Field',
  3251. '/legend',
  3252. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3253. array('label' => array('for' => 'Model1Field')),
  3254. __('empty'),
  3255. '/label',
  3256. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3257. array('label' => array('for' => 'Model1Field0')),
  3258. 'option A',
  3259. '/label',
  3260. '/fieldset',
  3261. '/div'
  3262. );
  3263. $this->assertTags($result, $expected);
  3264. $result = $this->Form->input('Model.1.field', array(
  3265. 'type' => 'radio',
  3266. 'options' => array('option A'),
  3267. 'empty' => 'CustomEmptyLabel',
  3268. 'hiddenField' => false
  3269. ));
  3270. $expected = array(
  3271. 'div' => array('class' => 'input radio'),
  3272. 'fieldset' => array(),
  3273. 'legend' => array(),
  3274. 'Field',
  3275. '/legend',
  3276. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3277. array('label' => array('for' => 'Model1Field')),
  3278. 'CustomEmptyLabel',
  3279. '/label',
  3280. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3281. array('label' => array('for' => 'Model1Field0')),
  3282. 'option A',
  3283. '/label',
  3284. '/fieldset',
  3285. '/div'
  3286. );
  3287. $this->assertTags($result, $expected);
  3288. $result = $this->Form->input('Model.1.field', array(
  3289. 'type' => 'radio',
  3290. 'options' => array('option A'),
  3291. 'empty' => false,
  3292. 'hiddenField' => false
  3293. ));
  3294. $this->assertTextNotContains('"Model1Field"', $result);
  3295. }
  3296. /**
  3297. * testSelect method
  3298. *
  3299. * Test select element generation.
  3300. *
  3301. * @return void
  3302. */
  3303. public function testSelect() {
  3304. $result = $this->Form->select('Model.field', array());
  3305. $expected = array(
  3306. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3307. array('option' => array('value' => '')),
  3308. '/option',
  3309. '/select'
  3310. );
  3311. $this->assertTags($result, $expected);
  3312. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3313. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3314. $expected = array(
  3315. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3316. array('option' => array('value' => '')),
  3317. '/option',
  3318. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3319. 'good',
  3320. '/option',
  3321. array('option' => array('value' => 'other')),
  3322. 'bad',
  3323. '/option',
  3324. '/select'
  3325. );
  3326. $this->assertTags($result, $expected);
  3327. $this->Form->request->data = array();
  3328. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3329. $expected = array(
  3330. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3331. array('option' => array('value' => '')),
  3332. '/option',
  3333. array('option' => array('value' => 'value')),
  3334. 'good',
  3335. '/option',
  3336. array('option' => array('value' => 'other')),
  3337. 'bad',
  3338. '/option',
  3339. '/select'
  3340. );
  3341. $this->assertTags($result, $expected);
  3342. $result = $this->Form->select(
  3343. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3344. array('empty' => false)
  3345. );
  3346. $expected = array(
  3347. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3348. array('option' => array('value' => 'first')),
  3349. 'first &quot;html&quot; &lt;chars&gt;',
  3350. '/option',
  3351. array('option' => array('value' => 'second')),
  3352. 'value',
  3353. '/option',
  3354. '/select'
  3355. );
  3356. $this->assertTags($result, $expected);
  3357. $result = $this->Form->select(
  3358. 'Model.field',
  3359. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3360. array('escape' => false, 'empty' => false)
  3361. );
  3362. $expected = array(
  3363. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3364. array('option' => array('value' => 'first')),
  3365. 'first "html" <chars>',
  3366. '/option',
  3367. array('option' => array('value' => 'second')),
  3368. 'value',
  3369. '/option',
  3370. '/select'
  3371. );
  3372. $this->assertTags($result, $expected);
  3373. $options = array(
  3374. array('value' => 'first', 'name' => 'First'),
  3375. array('value' => 'first', 'name' => 'Another First'),
  3376. );
  3377. $result = $this->Form->select(
  3378. 'Model.field',
  3379. $options,
  3380. array('escape' => false, 'empty' => false)
  3381. );
  3382. $expected = array(
  3383. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3384. array('option' => array('value' => 'first')),
  3385. 'First',
  3386. '/option',
  3387. array('option' => array('value' => 'first')),
  3388. 'Another First',
  3389. '/option',
  3390. '/select'
  3391. );
  3392. $this->assertTags($result, $expected);
  3393. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3394. $result = $this->Form->select(
  3395. 'Model.contact_id',
  3396. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3397. array('escape' => false, 'empty' => 'pick something')
  3398. );
  3399. $expected = array(
  3400. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3401. array('option' => array('value' => '')), 'pick something', '/option',
  3402. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3403. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3404. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3405. '/select'
  3406. );
  3407. $this->assertTags($result, $expected);
  3408. $this->Form->request->data['Model']['field'] = 0;
  3409. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3410. $expected = array(
  3411. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3412. array('option' => array('value' => '')), '/option',
  3413. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3414. array('option' => array('value' => '1')), 'Yes', '/option',
  3415. '/select'
  3416. );
  3417. $this->assertTags($result, $expected);
  3418. }
  3419. /**
  3420. * test that select() with optiongroups listens to the escape param.
  3421. *
  3422. * @return void
  3423. */
  3424. public function testSelectOptionGroupEscaping() {
  3425. $options = array(
  3426. '>< Key' => array(
  3427. 1 => 'One',
  3428. 2 => 'Two'
  3429. )
  3430. );
  3431. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3432. $expected = array(
  3433. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3434. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3435. array('option' => array('value' => '1')), 'One', '/option',
  3436. array('option' => array('value' => '2')), 'Two', '/option',
  3437. '/optgroup',
  3438. '/select'
  3439. );
  3440. $this->assertTags($result, $expected);
  3441. $options = array(
  3442. '>< Key' => array(
  3443. 1 => 'One',
  3444. 2 => 'Two'
  3445. )
  3446. );
  3447. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3448. $expected = array(
  3449. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3450. 'optgroup' => array('label' => '>< Key'),
  3451. array('option' => array('value' => '1')), 'One', '/option',
  3452. array('option' => array('value' => '2')), 'Two', '/option',
  3453. '/optgroup',
  3454. '/select'
  3455. );
  3456. $this->assertTags($result, $expected);
  3457. }
  3458. /**
  3459. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3460. *
  3461. * @return void
  3462. */
  3463. public function testSelectWithNullAttributes() {
  3464. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3465. $expected = array(
  3466. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3467. array('option' => array('value' => '0')),
  3468. 'first',
  3469. '/option',
  3470. array('option' => array('value' => '1')),
  3471. 'second',
  3472. '/option',
  3473. '/select'
  3474. );
  3475. $this->assertTags($result, $expected);
  3476. }
  3477. /**
  3478. * testNestedSelect method
  3479. *
  3480. * test select element generation with optgroups
  3481. *
  3482. * @return void
  3483. */
  3484. public function testNestedSelect() {
  3485. $result = $this->Form->select(
  3486. 'Model.field',
  3487. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3488. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3489. )), array('empty' => false)
  3490. );
  3491. $expected = array(
  3492. 'select' => array('name' => 'data[Model][field]',
  3493. 'id' => 'ModelField'),
  3494. array('option' => array('value' => 1)),
  3495. 'One',
  3496. '/option',
  3497. array('option' => array('value' => 2)),
  3498. 'Two',
  3499. '/option',
  3500. array('optgroup' => array('label' => 'Three')),
  3501. array('option' => array('value' => 4)),
  3502. 'Four',
  3503. '/option',
  3504. array('option' => array('value' => 5)),
  3505. 'Five',
  3506. '/option',
  3507. '/optgroup',
  3508. '/select'
  3509. );
  3510. $this->assertTags($result, $expected);
  3511. $result = $this->Form->select(
  3512. 'Model.field',
  3513. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3514. array('showParents' => true, 'empty' => false)
  3515. );
  3516. $expected = array(
  3517. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3518. array('option' => array('value' => 1)),
  3519. 'One',
  3520. '/option',
  3521. array('option' => array('value' => 2)),
  3522. 'Two',
  3523. '/option',
  3524. array('optgroup' => array('label' => 'Three')),
  3525. array('option' => array('value' => 3)),
  3526. 'Three',
  3527. '/option',
  3528. array('option' => array('value' => 4)),
  3529. 'Four',
  3530. '/option',
  3531. '/optgroup',
  3532. '/select'
  3533. );
  3534. $this->assertTags($result, $expected);
  3535. }
  3536. /**
  3537. * testSelectMultiple method
  3538. *
  3539. * test generation of multiple select elements
  3540. *
  3541. * @return void
  3542. */
  3543. public function testSelectMultiple() {
  3544. $options = array('first', 'second', 'third');
  3545. $result = $this->Form->select(
  3546. 'Model.multi_field', $options, array('multiple' => true)
  3547. );
  3548. $expected = array(
  3549. 'input' => array(
  3550. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3551. ),
  3552. 'select' => array(
  3553. 'name' => 'data[Model][multi_field][]',
  3554. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3555. ),
  3556. array('option' => array('value' => '0')),
  3557. 'first',
  3558. '/option',
  3559. array('option' => array('value' => '1')),
  3560. 'second',
  3561. '/option',
  3562. array('option' => array('value' => '2')),
  3563. 'third',
  3564. '/option',
  3565. '/select'
  3566. );
  3567. $this->assertTags($result, $expected);
  3568. $result = $this->Form->select(
  3569. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3570. );
  3571. $expected = array(
  3572. 'input' => array(
  3573. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3574. ),
  3575. 'select' => array(
  3576. 'name' => 'data[Model][multi_field][]',
  3577. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3578. ),
  3579. array('option' => array('value' => '0')),
  3580. 'first',
  3581. '/option',
  3582. array('option' => array('value' => '1')),
  3583. 'second',
  3584. '/option',
  3585. array('option' => array('value' => '2')),
  3586. 'third',
  3587. '/option',
  3588. '/select'
  3589. );
  3590. $this->assertTags($result, $expected);
  3591. $result = $this->Form->select(
  3592. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3593. );
  3594. $expected = array(
  3595. 'input' => array(
  3596. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3597. ),
  3598. 'select' => array(
  3599. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3600. 'multiple' => 'multiple'
  3601. ),
  3602. array('option' => array('value' => '0', 'selected' => 'selected')),
  3603. 'first',
  3604. '/option',
  3605. array('option' => array('value' => '1', 'selected' => 'selected')),
  3606. 'second',
  3607. '/option',
  3608. array('option' => array('value' => '2')),
  3609. 'third',
  3610. '/option',
  3611. '/select'
  3612. );
  3613. $this->assertTags($result, $expected);
  3614. $result = $this->Form->select(
  3615. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3616. );
  3617. $expected = array(
  3618. 'select' => array(
  3619. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3620. ),
  3621. array('option' => array('value' => '0', 'selected' => 'selected')),
  3622. 'first',
  3623. '/option',
  3624. array('option' => array('value' => '1', 'selected' => 'selected')),
  3625. 'second',
  3626. '/option',
  3627. array('option' => array('value' => '2')),
  3628. 'third',
  3629. '/option',
  3630. '/select'
  3631. );
  3632. $this->assertTags($result, $expected);
  3633. }
  3634. /**
  3635. * test generation of habtm select boxes.
  3636. *
  3637. * @return void
  3638. */
  3639. public function testHabtmSelectBox() {
  3640. $this->View->viewVars['contactTags'] = array(
  3641. 1 => 'blue',
  3642. 2 => 'red',
  3643. 3 => 'green'
  3644. );
  3645. $this->Form->request->data = array(
  3646. 'Contact' => array(),
  3647. 'ContactTag' => array(
  3648. array(
  3649. 'id' => 1,
  3650. 'name' => 'blue'
  3651. ),
  3652. array(
  3653. 'id' => 3,
  3654. 'name' => 'green'
  3655. )
  3656. )
  3657. );
  3658. $this->Form->create('Contact');
  3659. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3660. $expected = array(
  3661. 'input' => array(
  3662. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3663. ),
  3664. 'select' => array(
  3665. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3666. 'multiple' => 'multiple'
  3667. ),
  3668. array('option' => array('value' => '1', 'selected' => 'selected')),
  3669. 'blue',
  3670. '/option',
  3671. array('option' => array('value' => '2')),
  3672. 'red',
  3673. '/option',
  3674. array('option' => array('value' => '3', 'selected' => 'selected')),
  3675. 'green',
  3676. '/option',
  3677. '/select'
  3678. );
  3679. $this->assertTags($result, $expected);
  3680. }
  3681. /**
  3682. * test generation of multi select elements in checkbox format
  3683. *
  3684. * @return void
  3685. */
  3686. public function testSelectMultipleCheckboxes() {
  3687. $result = $this->Form->select(
  3688. 'Model.multi_field',
  3689. array('first', 'second', 'third'),
  3690. array('multiple' => 'checkbox')
  3691. );
  3692. $expected = array(
  3693. 'input' => array(
  3694. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3695. ),
  3696. array('div' => array('class' => 'checkbox')),
  3697. array('input' => array(
  3698. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3699. 'value' => '0', 'id' => 'ModelMultiField0'
  3700. )),
  3701. array('label' => array('for' => 'ModelMultiField0')),
  3702. 'first',
  3703. '/label',
  3704. '/div',
  3705. array('div' => array('class' => 'checkbox')),
  3706. array('input' => array(
  3707. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3708. 'value' => '1', 'id' => 'ModelMultiField1'
  3709. )),
  3710. array('label' => array('for' => 'ModelMultiField1')),
  3711. 'second',
  3712. '/label',
  3713. '/div',
  3714. array('div' => array('class' => 'checkbox')),
  3715. array('input' => array(
  3716. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3717. 'value' => '2', 'id' => 'ModelMultiField2'
  3718. )),
  3719. array('label' => array('for' => 'ModelMultiField2')),
  3720. 'third',
  3721. '/label',
  3722. '/div'
  3723. );
  3724. $this->assertTags($result, $expected);
  3725. $result = $this->Form->select(
  3726. 'Model.multi_field',
  3727. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3728. array('multiple' => 'checkbox')
  3729. );
  3730. $expected = array(
  3731. 'input' => array(
  3732. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3733. ),
  3734. array('div' => array('class' => 'checkbox')),
  3735. array('input' => array(
  3736. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3737. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3738. )),
  3739. array('label' => array('for' => 'ModelMultiFieldA')),
  3740. 'first',
  3741. '/label',
  3742. '/div',
  3743. array('div' => array('class' => 'checkbox')),
  3744. array('input' => array(
  3745. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3746. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3747. )),
  3748. array('label' => array('for' => 'ModelMultiFieldB')),
  3749. 'second',
  3750. '/label',
  3751. '/div',
  3752. array('div' => array('class' => 'checkbox')),
  3753. array('input' => array(
  3754. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3755. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3756. )),
  3757. array('label' => array('for' => 'ModelMultiFieldC')),
  3758. 'third',
  3759. '/label',
  3760. '/div'
  3761. );
  3762. $this->assertTags($result, $expected);
  3763. $result = $this->Form->select(
  3764. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3765. );
  3766. $expected = array(
  3767. 'input' => array(
  3768. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3769. ),
  3770. array('div' => array('class' => 'checkbox')),
  3771. array('input' => array(
  3772. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3773. 'value' => '1', 'id' => 'ModelMultiField1'
  3774. )),
  3775. array('label' => array('for' => 'ModelMultiField1')),
  3776. 'first',
  3777. '/label',
  3778. '/div'
  3779. );
  3780. $this->assertTags($result, $expected);
  3781. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3782. $result = $this->Form->select(
  3783. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3784. );
  3785. $expected = array(
  3786. 'input' => array(
  3787. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3788. ),
  3789. array('div' => array('class' => 'checkbox')),
  3790. array('input' => array(
  3791. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3792. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3793. )),
  3794. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3795. 'first',
  3796. '/label',
  3797. '/div',
  3798. array('div' => array('class' => 'checkbox')),
  3799. array('input' => array(
  3800. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3801. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3802. )),
  3803. array('label' => array('for' => 'ModelTagsArray')),
  3804. 'Array',
  3805. '/label',
  3806. '/div'
  3807. );
  3808. $this->assertTags($result, $expected);
  3809. }
  3810. /**
  3811. * test multiple checkboxes with div styles.
  3812. *
  3813. * @return void
  3814. */
  3815. public function testSelectMultipleCheckboxDiv() {
  3816. $result = $this->Form->select(
  3817. 'Model.tags',
  3818. array('first', 'second'),
  3819. array('multiple' => 'checkbox', 'class' => 'my-class')
  3820. );
  3821. $expected = array(
  3822. 'input' => array(
  3823. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3824. ),
  3825. array('div' => array('class' => 'my-class')),
  3826. array('input' => array(
  3827. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3828. 'value' => '0', 'id' => 'ModelTags0'
  3829. )),
  3830. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3831. '/div',
  3832. array('div' => array('class' => 'my-class')),
  3833. array('input' => array(
  3834. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3835. 'value' => '1', 'id' => 'ModelTags1'
  3836. )),
  3837. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3838. '/div'
  3839. );
  3840. $this->assertTags($result, $expected);
  3841. $result = $this->Form->input('Model.tags', array(
  3842. 'options' => array('first', 'second'),
  3843. 'multiple' => 'checkbox',
  3844. 'class' => 'my-class',
  3845. 'div' => false,
  3846. 'label' => false
  3847. ));
  3848. $this->assertTags($result, $expected);
  3849. $Contact = ClassRegistry::getObject('Contact');
  3850. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3851. $result = $this->Form->input('Contact.tags', array(
  3852. 'options' => array('one'),
  3853. 'multiple' => 'checkbox',
  3854. 'label' => false,
  3855. 'div' => false
  3856. ));
  3857. $expected = array(
  3858. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3859. array('div' => array('class' => 'checkbox form-error')),
  3860. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3861. array('label' => array('for' => 'ContactTags0')),
  3862. 'one',
  3863. '/label',
  3864. '/div'
  3865. );
  3866. $this->assertTags($result, $expected);
  3867. $result = $this->Form->input('Contact.tags', array(
  3868. 'options' => array('one'),
  3869. 'multiple' => 'checkbox',
  3870. 'class' => 'mycheckbox',
  3871. 'label' => false,
  3872. 'div' => false
  3873. ));
  3874. $expected = array(
  3875. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3876. array('div' => array('class' => 'mycheckbox form-error')),
  3877. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3878. array('label' => array('for' => 'ContactTags0')),
  3879. 'one',
  3880. '/label',
  3881. '/div'
  3882. );
  3883. $this->assertTags($result, $expected);
  3884. }
  3885. /**
  3886. * Checks the security hash array generated for multiple-input checkbox elements
  3887. *
  3888. * @return void
  3889. */
  3890. public function testSelectMultipleCheckboxSecurity() {
  3891. $this->Form->request['_Token'] = array('key' => 'testKey');
  3892. $this->assertEquals(array(), $this->Form->fields);
  3893. $result = $this->Form->select(
  3894. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3895. array('multiple' => 'checkbox')
  3896. );
  3897. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3898. $result = $this->Form->secure($this->Form->fields);
  3899. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3900. $this->assertRegExp('/"' . $key . '"/', $result);
  3901. }
  3902. /**
  3903. * Multiple select elements should always be secured as they always participate
  3904. * in the POST data.
  3905. *
  3906. * @return void
  3907. */
  3908. public function testSelectMultipleSecureWithNoOptions() {
  3909. $this->Form->request['_Token'] = array('key' => 'testkey');
  3910. $this->assertEquals(array(), $this->Form->fields);
  3911. $result = $this->Form->select(
  3912. 'Model.select',
  3913. array(),
  3914. array('multiple' => true)
  3915. );
  3916. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3917. }
  3918. /**
  3919. * When a select box has no options it should not be added to the fields list
  3920. * as it always fail post validation.
  3921. *
  3922. * @return void
  3923. */
  3924. public function testSelectNoSecureWithNoOptions() {
  3925. $this->Form->request['_Token'] = array('key' => 'testkey');
  3926. $this->assertEquals(array(), $this->Form->fields);
  3927. $this->Form->select(
  3928. 'Model.select',
  3929. array()
  3930. );
  3931. $this->assertEquals(array(), $this->Form->fields);
  3932. $this->Form->select(
  3933. 'Model.select',
  3934. array(),
  3935. array('empty' => true)
  3936. );
  3937. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3938. }
  3939. /**
  3940. * testInputMultipleCheckboxes method
  3941. *
  3942. * test input() resulting in multi select elements being generated.
  3943. *
  3944. * @return void
  3945. */
  3946. public function testInputMultipleCheckboxes() {
  3947. $result = $this->Form->input('Model.multi_field', array(
  3948. 'options' => array('first', 'second', 'third'),
  3949. 'multiple' => 'checkbox'
  3950. ));
  3951. $expected = array(
  3952. array('div' => array('class' => 'input select')),
  3953. array('label' => array('for' => 'ModelMultiField')),
  3954. 'Multi Field',
  3955. '/label',
  3956. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3957. array('div' => array('class' => 'checkbox')),
  3958. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3959. array('label' => array('for' => 'ModelMultiField0')),
  3960. 'first',
  3961. '/label',
  3962. '/div',
  3963. array('div' => array('class' => 'checkbox')),
  3964. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3965. array('label' => array('for' => 'ModelMultiField1')),
  3966. 'second',
  3967. '/label',
  3968. '/div',
  3969. array('div' => array('class' => 'checkbox')),
  3970. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3971. array('label' => array('for' => 'ModelMultiField2')),
  3972. 'third',
  3973. '/label',
  3974. '/div',
  3975. '/div'
  3976. );
  3977. $this->assertTags($result, $expected);
  3978. $result = $this->Form->input('Model.multi_field', array(
  3979. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3980. 'multiple' => 'checkbox'
  3981. ));
  3982. $expected = array(
  3983. array('div' => array('class' => 'input select')),
  3984. array('label' => array('for' => 'ModelMultiField')),
  3985. 'Multi Field',
  3986. '/label',
  3987. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3988. array('div' => array('class' => 'checkbox')),
  3989. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3990. array('label' => array('for' => 'ModelMultiFieldA')),
  3991. 'first',
  3992. '/label',
  3993. '/div',
  3994. array('div' => array('class' => 'checkbox')),
  3995. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3996. array('label' => array('for' => 'ModelMultiFieldB')),
  3997. 'second',
  3998. '/label',
  3999. '/div',
  4000. array('div' => array('class' => 'checkbox')),
  4001. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4002. array('label' => array('for' => 'ModelMultiFieldC')),
  4003. 'third',
  4004. '/label',
  4005. '/div',
  4006. '/div'
  4007. );
  4008. $this->assertTags($result, $expected);
  4009. $result = $this->Form->input('Model.multi_field', array(
  4010. 'options' => array('1' => 'first'),
  4011. 'multiple' => 'checkbox',
  4012. 'label' => false,
  4013. 'div' => false
  4014. ));
  4015. $expected = array(
  4016. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  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. 'first',
  4021. '/label',
  4022. '/div'
  4023. );
  4024. $this->assertTags($result, $expected);
  4025. $result = $this->Form->input('Model.multi_field', array(
  4026. 'options' => array('2' => 'second'),
  4027. 'multiple' => 'checkbox',
  4028. 'label' => false,
  4029. 'div' => false
  4030. ));
  4031. $expected = array(
  4032. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4033. array('div' => array('class' => 'checkbox')),
  4034. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4035. array('label' => array('for' => 'ModelMultiField2')),
  4036. 'second',
  4037. '/label',
  4038. '/div'
  4039. );
  4040. $this->assertTags($result, $expected);
  4041. }
  4042. /**
  4043. * testSelectHiddenFieldOmission method
  4044. *
  4045. * test that select() with 'hiddenField' => false omits the hidden field
  4046. *
  4047. * @return void
  4048. */
  4049. public function testSelectHiddenFieldOmission() {
  4050. $result = $this->Form->select('Model.multi_field',
  4051. array('first', 'second'),
  4052. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4053. );
  4054. $expected = array(
  4055. array('div' => array('class' => 'checkbox')),
  4056. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4057. array('label' => array('for' => 'ModelMultiField0')),
  4058. 'first',
  4059. '/label',
  4060. '/div',
  4061. array('div' => array('class' => 'checkbox')),
  4062. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4063. array('label' => array('for' => 'ModelMultiField1')),
  4064. 'second',
  4065. '/label',
  4066. '/div'
  4067. );
  4068. $this->assertTags($result, $expected);
  4069. $result = $this->Form->input('Model.multi_field', array(
  4070. 'options' => array('first', 'second'),
  4071. 'multiple' => 'checkbox',
  4072. 'hiddenField' => false
  4073. ));
  4074. $expected = array(
  4075. array('div' => array('class' => 'input select')),
  4076. array('label' => array('for' => 'ModelMultiField')),
  4077. 'Multi Field',
  4078. '/label',
  4079. array('div' => array('class' => 'checkbox')),
  4080. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4081. array('label' => array('for' => 'ModelMultiField0')),
  4082. 'first',
  4083. '/label',
  4084. '/div',
  4085. array('div' => array('class' => 'checkbox')),
  4086. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4087. array('label' => array('for' => 'ModelMultiField1')),
  4088. 'second',
  4089. '/label',
  4090. '/div',
  4091. '/div'
  4092. );
  4093. $this->assertTags($result, $expected);
  4094. }
  4095. /**
  4096. * test that select() with multiple = checkbox works with overriding name attribute.
  4097. *
  4098. * @return void
  4099. */
  4100. public function testSelectCheckboxMultipleOverrideName() {
  4101. $result = $this->Form->input('category', array(
  4102. 'type' => 'select',
  4103. 'multiple' => 'checkbox',
  4104. 'name' => 'data[fish]',
  4105. 'options' => array('1', '2'),
  4106. 'div' => false,
  4107. 'label' => false,
  4108. ));
  4109. $expected = array(
  4110. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4111. array('div' => array('class' => 'checkbox')),
  4112. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4113. array('label' => array('for' => 'Category0')), '1', '/label',
  4114. '/div',
  4115. array('div' => array('class' => 'checkbox')),
  4116. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4117. array('label' => array('for' => 'Category1')), '2', '/label',
  4118. '/div'
  4119. );
  4120. $this->assertTags($result, $expected);
  4121. }
  4122. /**
  4123. * Test that 'id' overrides all the checkbox id's as well.
  4124. *
  4125. * @return void
  4126. */
  4127. public function testSelectCheckboxMultipleId() {
  4128. $result = $this->Form->select(
  4129. 'Model.multi_field',
  4130. array('first', 'second', 'third'),
  4131. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4132. );
  4133. $expected = array(
  4134. 'input' => array(
  4135. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4136. ),
  4137. array('div' => array('class' => 'checkbox')),
  4138. array('input' => array(
  4139. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4140. 'value' => '0', 'id' => 'CustomId0'
  4141. )),
  4142. array('label' => array('for' => 'CustomId0')),
  4143. 'first',
  4144. '/label',
  4145. '/div',
  4146. array('div' => array('class' => 'checkbox')),
  4147. array('input' => array(
  4148. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4149. 'value' => '1', 'id' => 'CustomId1'
  4150. )),
  4151. array('label' => array('for' => 'CustomId1')),
  4152. 'second',
  4153. '/label',
  4154. '/div',
  4155. array('div' => array('class' => 'checkbox')),
  4156. array('input' => array(
  4157. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4158. 'value' => '2', 'id' => 'CustomId2'
  4159. )),
  4160. array('label' => array('for' => 'CustomId2')),
  4161. 'third',
  4162. '/label',
  4163. '/div'
  4164. );
  4165. $this->assertTags($result, $expected);
  4166. }
  4167. /**
  4168. * testCheckbox method
  4169. *
  4170. * Test generation of checkboxes
  4171. *
  4172. * @return void
  4173. */
  4174. public function testCheckbox() {
  4175. $result = $this->Form->checkbox('Model.field');
  4176. $expected = array(
  4177. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4178. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4179. );
  4180. $this->assertTags($result, $expected);
  4181. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4182. $expected = array(
  4183. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4184. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4185. );
  4186. $this->assertTags($result, $expected);
  4187. $Contact = ClassRegistry::getObject('Contact');
  4188. $Contact->validationErrors['field'] = 1;
  4189. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4190. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4191. $expected = array(
  4192. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4193. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4194. );
  4195. $this->assertTags($result, $expected);
  4196. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4197. $expected = array(
  4198. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4199. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4200. );
  4201. $this->assertTags($result, $expected);
  4202. $this->Form->request->data['Contact']['field'] = '';
  4203. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4204. $expected = array(
  4205. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4206. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4207. );
  4208. $this->assertTags($result, $expected);
  4209. $Contact->validationErrors = array();
  4210. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4211. $expected = array(
  4212. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4213. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4214. );
  4215. $this->assertTags($result, $expected);
  4216. $this->Form->request->data['Contact']['published'] = 1;
  4217. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4218. $expected = array(
  4219. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4220. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4221. );
  4222. $this->assertTags($result, $expected);
  4223. $this->Form->request->data['Contact']['published'] = 0;
  4224. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4225. $expected = array(
  4226. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4227. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4228. );
  4229. $this->assertTags($result, $expected);
  4230. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4231. $expected = array(
  4232. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4233. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4234. );
  4235. $this->assertTags($result, $expected);
  4236. $result = $this->Form->checkbox('CustomField.1.value');
  4237. $expected = array(
  4238. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4239. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4240. );
  4241. $this->assertTags($result, $expected);
  4242. }
  4243. /**
  4244. * test checkbox() with a custom name attribute
  4245. *
  4246. * @return void
  4247. */
  4248. public function testCheckboxCustomNameAttribute() {
  4249. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4250. $expected = array(
  4251. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4252. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4253. );
  4254. $this->assertTags($result, $expected);
  4255. }
  4256. /**
  4257. * test the checked option for checkboxes.
  4258. *
  4259. * @return void
  4260. */
  4261. public function testCheckboxCheckedOption() {
  4262. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4263. $expected = array(
  4264. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4265. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4266. );
  4267. $this->assertTags($result, $expected);
  4268. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4269. $expected = array(
  4270. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4271. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4272. );
  4273. $this->assertTags($result, $expected);
  4274. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4275. $expected = array(
  4276. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4277. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4278. );
  4279. $this->assertTags($result, $expected);
  4280. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4281. $expected = array(
  4282. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4283. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4284. );
  4285. $this->assertTags($result, $expected);
  4286. $this->Form->request->data['Model']['field'] = 1;
  4287. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4288. $expected = array(
  4289. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4290. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4291. );
  4292. $this->assertTags($result, $expected);
  4293. }
  4294. /**
  4295. * Test that disabled attribute works on both the checkbox and hidden input.
  4296. *
  4297. * @return void
  4298. */
  4299. public function testCheckboxDisabling() {
  4300. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4301. $expected = array(
  4302. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4303. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4304. );
  4305. $this->assertTags($result, $expected);
  4306. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4307. $expected = array(
  4308. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4309. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4310. );
  4311. $this->assertTags($result, $expected);
  4312. }
  4313. /**
  4314. * Test that the hidden input for checkboxes can be omitted or set to a
  4315. * specific value.
  4316. *
  4317. * @return void
  4318. */
  4319. public function testCheckboxHiddenField() {
  4320. $result = $this->Form->input('UserForm.something', array(
  4321. 'type' => 'checkbox',
  4322. 'hiddenField' => false
  4323. ));
  4324. $expected = array(
  4325. 'div' => array('class' => 'input checkbox'),
  4326. array('input' => array(
  4327. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4328. 'value' => '1', 'id' => 'UserFormSomething'
  4329. )),
  4330. 'label' => array('for' => 'UserFormSomething'),
  4331. 'Something',
  4332. '/label',
  4333. '/div'
  4334. );
  4335. $this->assertTags($result, $expected);
  4336. $result = $this->Form->input('UserForm.something', array(
  4337. 'type' => 'checkbox',
  4338. 'value' => 'Y',
  4339. 'hiddenField' => 'N',
  4340. ));
  4341. $expected = array(
  4342. 'div' => array('class' => 'input checkbox'),
  4343. array('input' => array(
  4344. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4345. 'value' => 'N', 'id' => 'UserFormSomething_'
  4346. )),
  4347. array('input' => array(
  4348. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4349. 'value' => 'Y', 'id' => 'UserFormSomething'
  4350. )),
  4351. 'label' => array('for' => 'UserFormSomething'),
  4352. 'Something',
  4353. '/label',
  4354. '/div'
  4355. );
  4356. $this->assertTags($result, $expected);
  4357. }
  4358. /**
  4359. * testDateTime method
  4360. *
  4361. * Test generation of date/time select elements
  4362. *
  4363. * @return void
  4364. */
  4365. public function testDateTime() {
  4366. extract($this->dateRegex);
  4367. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4368. $now = strtotime('now');
  4369. $expected = array(
  4370. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4371. $daysRegex,
  4372. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4373. date('j', $now),
  4374. '/option',
  4375. '*/select',
  4376. '-',
  4377. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4378. $monthsRegex,
  4379. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4380. date('F', $now),
  4381. '/option',
  4382. '*/select',
  4383. '-',
  4384. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4385. $yearsRegex,
  4386. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4387. date('Y', $now),
  4388. '/option',
  4389. '*/select',
  4390. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4391. $hoursRegex,
  4392. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4393. date('g', $now),
  4394. '/option',
  4395. '*/select',
  4396. ':',
  4397. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4398. $minutesRegex,
  4399. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4400. date('i', $now),
  4401. '/option',
  4402. '*/select',
  4403. ' ',
  4404. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4405. $meridianRegex,
  4406. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4407. date('a', $now),
  4408. '/option',
  4409. '*/select'
  4410. );
  4411. $this->assertTags($result, $expected);
  4412. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4413. $expected = array(
  4414. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4415. $daysRegex,
  4416. array('option' => array('value' => '')),
  4417. '/option',
  4418. '*/select',
  4419. '-',
  4420. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4421. $monthsRegex,
  4422. array('option' => array('value' => '')),
  4423. '/option',
  4424. '*/select',
  4425. '-',
  4426. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4427. $yearsRegex,
  4428. array('option' => array('value' => '')),
  4429. '/option',
  4430. '*/select',
  4431. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4432. $hoursRegex,
  4433. array('option' => array('value' => '')),
  4434. '/option',
  4435. '*/select',
  4436. ':',
  4437. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4438. $minutesRegex,
  4439. array('option' => array('value' => '')),
  4440. '/option',
  4441. '*/select',
  4442. ' ',
  4443. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4444. $meridianRegex,
  4445. array('option' => array('value' => '')),
  4446. '/option',
  4447. '*/select'
  4448. );
  4449. $this->assertTags($result, $expected);
  4450. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4451. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4452. $this->assertTags($result, $expected);
  4453. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4454. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4455. $this->assertTags($result, $expected);
  4456. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4457. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4458. $expected = array(
  4459. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4460. $daysRegex,
  4461. array('option' => array('value' => '')),
  4462. '/option',
  4463. '*/select',
  4464. '-',
  4465. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4466. $monthsRegex,
  4467. array('option' => array('value' => '')),
  4468. '/option',
  4469. '*/select',
  4470. '-',
  4471. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4472. $yearsRegex,
  4473. array('option' => array('value' => '')),
  4474. '/option',
  4475. '*/select',
  4476. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4477. $hoursRegex,
  4478. array('option' => array('value' => '')),
  4479. '/option',
  4480. '*/select',
  4481. ':',
  4482. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4483. $minutesRegex,
  4484. array('option' => array('value' => '')),
  4485. '/option',
  4486. array('option' => array('value' => '00')),
  4487. '00',
  4488. '/option',
  4489. array('option' => array('value' => '05')),
  4490. '05',
  4491. '/option',
  4492. array('option' => array('value' => '10')),
  4493. '10',
  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('minuteInterval' => 5, 'value' => ''));
  4506. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4507. $this->Form->request->data['Contact']['data'] = null;
  4508. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4509. $expected = array(
  4510. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4511. $daysRegex,
  4512. array('option' => array('value' => '')),
  4513. '/option',
  4514. '*/select',
  4515. '-',
  4516. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4517. $monthsRegex,
  4518. array('option' => array('value' => '')),
  4519. '/option',
  4520. '*/select',
  4521. '-',
  4522. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4523. $yearsRegex,
  4524. array('option' => array('value' => '')),
  4525. '/option',
  4526. '*/select',
  4527. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4528. $hoursRegex,
  4529. array('option' => array('value' => '')),
  4530. '/option',
  4531. '*/select',
  4532. ':',
  4533. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4534. $minutesRegex,
  4535. array('option' => array('value' => '')),
  4536. '/option',
  4537. '*/select',
  4538. ' ',
  4539. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4540. $meridianRegex,
  4541. array('option' => array('value' => '')),
  4542. '/option',
  4543. '*/select'
  4544. );
  4545. $this->assertTags($result, $expected);
  4546. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4547. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4548. $now = strtotime($this->Form->data['Model']['field']);
  4549. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4550. $expected = array(
  4551. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4552. $daysRegex,
  4553. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4554. date('j', $now),
  4555. '/option',
  4556. '*/select',
  4557. '-',
  4558. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4559. $monthsRegex,
  4560. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4561. date('F', $now),
  4562. '/option',
  4563. '*/select',
  4564. '-',
  4565. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4566. $yearsRegex,
  4567. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4568. date('Y', $now),
  4569. '/option',
  4570. '*/select',
  4571. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4572. $hoursRegex,
  4573. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4574. date('g', $now),
  4575. '/option',
  4576. '*/select',
  4577. ':',
  4578. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4579. $minutesRegex,
  4580. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4581. date('i', $now),
  4582. '/option',
  4583. '*/select',
  4584. ' ',
  4585. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4586. $meridianRegex,
  4587. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4588. date('a', $now),
  4589. '/option',
  4590. '*/select'
  4591. );
  4592. $this->assertTags($result, $expected);
  4593. $selected = strtotime('2008-10-26 12:33:00');
  4594. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4595. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4596. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4597. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4598. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4599. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4600. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4601. $this->Form->create('Contact');
  4602. $result = $this->Form->input('published');
  4603. $now = strtotime('now');
  4604. $expected = array(
  4605. 'div' => array('class' => 'input date'),
  4606. 'label' => array('for' => 'ContactPublishedMonth'),
  4607. 'Published',
  4608. '/label',
  4609. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4610. $monthsRegex,
  4611. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4612. date('F', $now),
  4613. '/option',
  4614. '*/select',
  4615. '-',
  4616. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4617. $daysRegex,
  4618. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4619. date('j', $now),
  4620. '/option',
  4621. '*/select',
  4622. '-',
  4623. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4624. $yearsRegex,
  4625. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4626. date('Y', $now),
  4627. '/option',
  4628. '*/select',
  4629. '/div'
  4630. );
  4631. $this->assertTags($result, $expected);
  4632. $result = $this->Form->input('published2', array('type' => 'date'));
  4633. $now = strtotime('now');
  4634. $expected = array(
  4635. 'div' => array('class' => 'input date'),
  4636. 'label' => array('for' => 'ContactPublished2Month'),
  4637. 'Published2',
  4638. '/label',
  4639. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4640. $monthsRegex,
  4641. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4642. date('F', $now),
  4643. '/option',
  4644. '*/select',
  4645. '-',
  4646. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4647. $daysRegex,
  4648. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4649. date('j', $now),
  4650. '/option',
  4651. '*/select',
  4652. '-',
  4653. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4654. $yearsRegex,
  4655. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4656. date('Y', $now),
  4657. '/option',
  4658. '*/select',
  4659. '/div'
  4660. );
  4661. $this->assertTags($result, $expected);
  4662. $this->Form->create('Contact');
  4663. $result = $this->Form->input('published', array('monthNames' => false));
  4664. $now = strtotime('now');
  4665. $expected = array(
  4666. 'div' => array('class' => 'input date'),
  4667. 'label' => array('for' => 'ContactPublishedMonth'),
  4668. 'Published',
  4669. '/label',
  4670. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4671. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4672. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4673. date('m', $now),
  4674. '/option',
  4675. '*/select',
  4676. '-',
  4677. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4678. $daysRegex,
  4679. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4680. date('j', $now),
  4681. '/option',
  4682. '*/select',
  4683. '-',
  4684. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4685. $yearsRegex,
  4686. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4687. date('Y', $now),
  4688. '/option',
  4689. '*/select',
  4690. '/div'
  4691. );
  4692. $this->assertTags($result, $expected);
  4693. $result = $this->Form->input('published', array('type' => 'time'));
  4694. $now = strtotime('now');
  4695. $expected = array(
  4696. 'div' => array('class' => 'input time'),
  4697. 'label' => array('for' => 'ContactPublishedHour'),
  4698. 'Published',
  4699. '/label',
  4700. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4701. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4702. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4703. date('g', $now),
  4704. '/option',
  4705. '*/select',
  4706. ':',
  4707. );
  4708. $this->assertTags($result, $expected);
  4709. $result = $this->Form->input('published', array(
  4710. 'timeFormat' => 24,
  4711. 'interval' => 5,
  4712. 'selected' => strtotime('2009-09-03 13:37:00'),
  4713. 'type' => 'datetime'
  4714. ));
  4715. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4716. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4717. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4718. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4719. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4720. $this->assertNoErrors();
  4721. $this->Form->request->data['Contact'] = array(
  4722. 'date' => array(
  4723. 'day' => '',
  4724. 'month' => '',
  4725. 'year' => '',
  4726. 'hour' => '',
  4727. 'min' => '',
  4728. 'meridian' => ''
  4729. )
  4730. );
  4731. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4732. }
  4733. /**
  4734. * test that datetime() and default values work.
  4735. *
  4736. * @return void
  4737. */
  4738. public function testDatetimeWithDefault() {
  4739. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4740. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4741. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4742. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4743. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4744. 'default' => '2009-06-01 11:15:30'
  4745. ));
  4746. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4747. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4748. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4749. }
  4750. /**
  4751. * test that bogus non-date time data doesn't cause errors.
  4752. *
  4753. * @return void
  4754. */
  4755. public function testDateTimeWithBogusData() {
  4756. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4757. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4758. }
  4759. /**
  4760. * testDateTimeEmptyAsArray
  4761. *
  4762. * @return void
  4763. */
  4764. public function testDateTimeEmptyAsArray() {
  4765. $result = $this->Form->dateTime('Contact.date',
  4766. 'DMY',
  4767. '12',
  4768. array(
  4769. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4770. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4771. )
  4772. )
  4773. );
  4774. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4775. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4776. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4777. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4778. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4779. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4780. $result = $this->Form->dateTime('Contact.date',
  4781. 'DMY',
  4782. '12',
  4783. array(
  4784. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4785. )
  4786. );
  4787. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4788. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4789. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4790. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4791. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4792. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4793. }
  4794. /**
  4795. * testFormDateTimeMulti method
  4796. *
  4797. * test multiple datetime element generation
  4798. *
  4799. * @return void
  4800. */
  4801. public function testFormDateTimeMulti() {
  4802. extract($this->dateRegex);
  4803. $result = $this->Form->dateTime('Contact.1.updated');
  4804. $expected = array(
  4805. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4806. $daysRegex,
  4807. array('option' => array('value' => '')),
  4808. '/option',
  4809. '*/select',
  4810. '-',
  4811. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4812. $monthsRegex,
  4813. array('option' => array('value' => '')),
  4814. '/option',
  4815. '*/select',
  4816. '-',
  4817. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4818. $yearsRegex,
  4819. array('option' => array('value' => '')),
  4820. '/option',
  4821. '*/select',
  4822. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4823. $hoursRegex,
  4824. array('option' => array('value' => '')),
  4825. '/option',
  4826. '*/select',
  4827. ':',
  4828. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4829. $minutesRegex,
  4830. array('option' => array('value' => '')),
  4831. '/option',
  4832. '*/select',
  4833. ' ',
  4834. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4835. $meridianRegex,
  4836. array('option' => array('value' => '')),
  4837. '/option',
  4838. '*/select'
  4839. );
  4840. $this->assertTags($result, $expected);
  4841. $result = $this->Form->dateTime('Contact.2.updated');
  4842. $expected = array(
  4843. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4844. $daysRegex,
  4845. array('option' => array('value' => '')),
  4846. '/option',
  4847. '*/select',
  4848. '-',
  4849. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4850. $monthsRegex,
  4851. array('option' => array('value' => '')),
  4852. '/option',
  4853. '*/select',
  4854. '-',
  4855. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4856. $yearsRegex,
  4857. array('option' => array('value' => '')),
  4858. '/option',
  4859. '*/select',
  4860. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4861. $hoursRegex,
  4862. array('option' => array('value' => '')),
  4863. '/option',
  4864. '*/select',
  4865. ':',
  4866. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4867. $minutesRegex,
  4868. array('option' => array('value' => '')),
  4869. '/option',
  4870. '*/select',
  4871. ' ',
  4872. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4873. $meridianRegex,
  4874. array('option' => array('value' => '')),
  4875. '/option',
  4876. '*/select'
  4877. );
  4878. $this->assertTags($result, $expected);
  4879. }
  4880. /**
  4881. * When changing the date format, the label should always focus the first select box when
  4882. * clicked.
  4883. *
  4884. * @return void
  4885. */
  4886. public function testDateTimeLabelIdMatchesFirstInput() {
  4887. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4888. $this->assertContains('label for="ModelDateMonth"', $result);
  4889. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4890. $this->assertContains('label for="ModelDateDay"', $result);
  4891. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4892. $this->assertContains('label for="ModelDateYear"', $result);
  4893. }
  4894. /**
  4895. * testMonth method
  4896. *
  4897. * @return void
  4898. */
  4899. public function testMonth() {
  4900. $result = $this->Form->month('Model.field');
  4901. $expected = array(
  4902. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4903. array('option' => array('value' => '')),
  4904. '/option',
  4905. array('option' => array('value' => '01')),
  4906. date('F', strtotime('2008-01-01 00:00:00')),
  4907. '/option',
  4908. array('option' => array('value' => '02')),
  4909. date('F', strtotime('2008-02-01 00:00:00')),
  4910. '/option',
  4911. '*/select',
  4912. );
  4913. $this->assertTags($result, $expected);
  4914. $result = $this->Form->month('Model.field', array('empty' => true));
  4915. $expected = array(
  4916. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4917. array('option' => array('value' => '')),
  4918. '/option',
  4919. array('option' => array('value' => '01')),
  4920. date('F', strtotime('2008-01-01 00:00:00')),
  4921. '/option',
  4922. array('option' => array('value' => '02')),
  4923. date('F', strtotime('2008-02-01 00:00:00')),
  4924. '/option',
  4925. '*/select',
  4926. );
  4927. $this->assertTags($result, $expected);
  4928. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4929. $expected = array(
  4930. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4931. array('option' => array('value' => '')),
  4932. '/option',
  4933. array('option' => array('value' => '01')),
  4934. '01',
  4935. '/option',
  4936. array('option' => array('value' => '02')),
  4937. '02',
  4938. '/option',
  4939. '*/select',
  4940. );
  4941. $this->assertTags($result, $expected);
  4942. $monthNames = array(
  4943. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4944. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4945. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4946. $expected = array(
  4947. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4948. array('option' => array('value' => '')),
  4949. '/option',
  4950. array('option' => array('value' => '01')),
  4951. 'Jan',
  4952. '/option',
  4953. array('option' => array('value' => '02')),
  4954. 'Feb',
  4955. '/option',
  4956. '*/select',
  4957. );
  4958. $this->assertTags($result, $expected);
  4959. }
  4960. /**
  4961. * testDay method
  4962. *
  4963. * @return void
  4964. */
  4965. public function testDay() {
  4966. extract($this->dateRegex);
  4967. $result = $this->Form->day('Model.field', array('value' => false));
  4968. $expected = array(
  4969. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4970. array('option' => array('value' => '')),
  4971. '/option',
  4972. array('option' => array('value' => '01')),
  4973. '1',
  4974. '/option',
  4975. array('option' => array('value' => '02')),
  4976. '2',
  4977. '/option',
  4978. $daysRegex,
  4979. '/select',
  4980. );
  4981. $this->assertTags($result, $expected);
  4982. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4983. $result = $this->Form->day('Model.field');
  4984. $expected = array(
  4985. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4986. array('option' => array('value' => '')),
  4987. '/option',
  4988. array('option' => array('value' => '01')),
  4989. '1',
  4990. '/option',
  4991. array('option' => array('value' => '02')),
  4992. '2',
  4993. '/option',
  4994. $daysRegex,
  4995. array('option' => array('value' => '10', 'selected' => 'selected')),
  4996. '10',
  4997. '/option',
  4998. $daysRegex,
  4999. '/select',
  5000. );
  5001. $this->assertTags($result, $expected);
  5002. $this->Form->request->data['Model']['field'] = '';
  5003. $result = $this->Form->day('Model.field', array('value' => '10'));
  5004. $expected = array(
  5005. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5006. array('option' => array('value' => '')),
  5007. '/option',
  5008. array('option' => array('value' => '01')),
  5009. '1',
  5010. '/option',
  5011. array('option' => array('value' => '02')),
  5012. '2',
  5013. '/option',
  5014. $daysRegex,
  5015. array('option' => array('value' => '10', 'selected' => 'selected')),
  5016. '10',
  5017. '/option',
  5018. $daysRegex,
  5019. '/select',
  5020. );
  5021. $this->assertTags($result, $expected);
  5022. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5023. $result = $this->Form->day('Model.field', array('value' => true));
  5024. $expected = array(
  5025. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5026. array('option' => array('value' => '')),
  5027. '/option',
  5028. array('option' => array('value' => '01')),
  5029. '1',
  5030. '/option',
  5031. array('option' => array('value' => '02')),
  5032. '2',
  5033. '/option',
  5034. $daysRegex,
  5035. array('option' => array('value' => '10', 'selected' => 'selected')),
  5036. '10',
  5037. '/option',
  5038. $daysRegex,
  5039. '/select',
  5040. );
  5041. $this->assertTags($result, $expected);
  5042. }
  5043. /**
  5044. * testMinute method
  5045. *
  5046. * @return void
  5047. */
  5048. public function testMinute() {
  5049. extract($this->dateRegex);
  5050. $result = $this->Form->minute('Model.field');
  5051. $expected = array(
  5052. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5053. array('option' => array('value' => '')),
  5054. '/option',
  5055. array('option' => array('value' => '00')),
  5056. '00',
  5057. '/option',
  5058. array('option' => array('value' => '01')),
  5059. '01',
  5060. '/option',
  5061. array('option' => array('value' => '02')),
  5062. '02',
  5063. '/option',
  5064. $minutesRegex,
  5065. '/select',
  5066. );
  5067. $this->assertTags($result, $expected);
  5068. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5069. $result = $this->Form->minute('Model.field');
  5070. $expected = array(
  5071. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5072. array('option' => array('value' => '')),
  5073. '/option',
  5074. array('option' => array('value' => '00')),
  5075. '00',
  5076. '/option',
  5077. array('option' => array('value' => '01')),
  5078. '01',
  5079. '/option',
  5080. array('option' => array('value' => '02')),
  5081. '02',
  5082. '/option',
  5083. $minutesRegex,
  5084. array('option' => array('value' => '12', 'selected' => 'selected')),
  5085. '12',
  5086. '/option',
  5087. $minutesRegex,
  5088. '/select',
  5089. );
  5090. $this->assertTags($result, $expected);
  5091. $this->Form->request->data['Model']['field'] = '';
  5092. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5093. $expected = array(
  5094. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5095. array('option' => array('value' => '')),
  5096. '/option',
  5097. array('option' => array('value' => '00')),
  5098. '00',
  5099. '/option',
  5100. array('option' => array('value' => '05')),
  5101. '05',
  5102. '/option',
  5103. array('option' => array('value' => '10')),
  5104. '10',
  5105. '/option',
  5106. $minutesRegex,
  5107. '/select',
  5108. );
  5109. $this->assertTags($result, $expected);
  5110. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5111. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5112. $expected = array(
  5113. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5114. array('option' => array('value' => '')),
  5115. '/option',
  5116. array('option' => array('value' => '00')),
  5117. '00',
  5118. '/option',
  5119. array('option' => array('value' => '05')),
  5120. '05',
  5121. '/option',
  5122. array('option' => array('value' => '10', 'selected' => 'selected')),
  5123. '10',
  5124. '/option',
  5125. $minutesRegex,
  5126. '/select',
  5127. );
  5128. $this->assertTags($result, $expected);
  5129. }
  5130. /**
  5131. * testHour method
  5132. *
  5133. * @return void
  5134. */
  5135. public function testHour() {
  5136. extract($this->dateRegex);
  5137. $result = $this->Form->hour('Model.field', false);
  5138. $expected = array(
  5139. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5140. array('option' => array('value' => '')),
  5141. '/option',
  5142. array('option' => array('value' => '01')),
  5143. '1',
  5144. '/option',
  5145. array('option' => array('value' => '02')),
  5146. '2',
  5147. '/option',
  5148. $hoursRegex,
  5149. '/select',
  5150. );
  5151. $this->assertTags($result, $expected);
  5152. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5153. $result = $this->Form->hour('Model.field', false);
  5154. $expected = array(
  5155. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5156. array('option' => array('value' => '')),
  5157. '/option',
  5158. array('option' => array('value' => '01')),
  5159. '1',
  5160. '/option',
  5161. array('option' => array('value' => '02')),
  5162. '2',
  5163. '/option',
  5164. $hoursRegex,
  5165. array('option' => array('value' => '12', 'selected' => 'selected')),
  5166. '12',
  5167. '/option',
  5168. '/select',
  5169. );
  5170. $this->assertTags($result, $expected);
  5171. $this->Form->request->data['Model']['field'] = '';
  5172. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5173. $expected = array(
  5174. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5175. array('option' => array('value' => '')),
  5176. '/option',
  5177. array('option' => array('value' => '00')),
  5178. '0',
  5179. '/option',
  5180. array('option' => array('value' => '01')),
  5181. '1',
  5182. '/option',
  5183. array('option' => array('value' => '02')),
  5184. '2',
  5185. '/option',
  5186. $hoursRegex,
  5187. array('option' => array('value' => '23', 'selected' => 'selected')),
  5188. '23',
  5189. '/option',
  5190. '/select',
  5191. );
  5192. $this->assertTags($result, $expected);
  5193. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5194. $result = $this->Form->hour('Model.field', true);
  5195. $expected = array(
  5196. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5197. array('option' => array('value' => '')),
  5198. '/option',
  5199. array('option' => array('value' => '00', 'selected' => 'selected')),
  5200. '0',
  5201. '/option',
  5202. array('option' => array('value' => '01')),
  5203. '1',
  5204. '/option',
  5205. array('option' => array('value' => '02')),
  5206. '2',
  5207. '/option',
  5208. $hoursRegex,
  5209. '/select',
  5210. );
  5211. $this->assertTags($result, $expected);
  5212. unset($this->Form->request->data['Model']['field']);
  5213. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5214. $thisHour = date('H');
  5215. $optValue = date('G');
  5216. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5217. }
  5218. /**
  5219. * testYear method
  5220. *
  5221. * @return void
  5222. */
  5223. public function testYear() {
  5224. $result = $this->Form->year('Model.field', 2006, 2007);
  5225. $expected = array(
  5226. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5227. array('option' => array('value' => '')),
  5228. '/option',
  5229. array('option' => array('value' => '2007')),
  5230. '2007',
  5231. '/option',
  5232. array('option' => array('value' => '2006')),
  5233. '2006',
  5234. '/option',
  5235. '/select',
  5236. );
  5237. $this->assertTags($result, $expected);
  5238. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5239. $expected = array(
  5240. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5241. array('option' => array('value' => '')),
  5242. '/option',
  5243. array('option' => array('value' => '2006')),
  5244. '2006',
  5245. '/option',
  5246. array('option' => array('value' => '2007')),
  5247. '2007',
  5248. '/option',
  5249. '/select',
  5250. );
  5251. $this->assertTags($result, $expected);
  5252. $this->request->data['Contact']['published'] = '';
  5253. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5254. $expected = array(
  5255. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5256. array('option' => array('value' => '')),
  5257. '/option',
  5258. array('option' => array('value' => '2007')),
  5259. '2007',
  5260. '/option',
  5261. array('option' => array('value' => '2006')),
  5262. '2006',
  5263. '/option',
  5264. '/select',
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5268. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5269. $expected = array(
  5270. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5271. array('option' => array('value' => '2007')),
  5272. '2007',
  5273. '/option',
  5274. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5275. '2006',
  5276. '/option',
  5277. '/select',
  5278. );
  5279. $this->assertTags($result, $expected);
  5280. $this->Form->request->data['Contact']['published'] = '';
  5281. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5282. $expected = array(
  5283. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5284. array('option' => array('value' => '')),
  5285. '/option',
  5286. array('option' => array('value' => '2007')),
  5287. '2007',
  5288. '/option',
  5289. array('option' => array('value' => '2006')),
  5290. '2006',
  5291. '/option',
  5292. '/select',
  5293. );
  5294. $this->assertTags($result, $expected);
  5295. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5296. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5297. $expected = array(
  5298. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5299. array('option' => array('value' => '2007')),
  5300. '2007',
  5301. '/option',
  5302. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5303. '2006',
  5304. '/option',
  5305. '/select',
  5306. );
  5307. $this->assertTags($result, $expected);
  5308. $this->Form->request->data['Contact']['published'] = '';
  5309. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5310. $expected = array(
  5311. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5312. array('option' => array('value' => '')),
  5313. '/option',
  5314. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5315. '2007',
  5316. '/option',
  5317. array('option' => array('value' => '2006')),
  5318. '2006',
  5319. '/option',
  5320. '/select',
  5321. );
  5322. $this->assertTags($result, $expected);
  5323. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5324. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5325. $expected = array(
  5326. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5327. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5328. '2007',
  5329. '/option',
  5330. array('option' => array('value' => '2006')),
  5331. '2006',
  5332. '/option',
  5333. '/select',
  5334. );
  5335. $this->assertTags($result, $expected);
  5336. $this->Form->request->data['Contact']['published'] = '';
  5337. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5338. $expected = array(
  5339. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5340. array('option' => array('value' => '2008')),
  5341. '2008',
  5342. '/option',
  5343. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5344. '2007',
  5345. '/option',
  5346. array('option' => array('value' => '2006')),
  5347. '2006',
  5348. '/option',
  5349. '/select',
  5350. );
  5351. $this->assertTags($result, $expected);
  5352. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5353. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5354. $expected = array(
  5355. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5356. array('option' => array('value' => '2008')),
  5357. '2008',
  5358. '/option',
  5359. array('option' => array('value' => '2007')),
  5360. '2007',
  5361. '/option',
  5362. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5363. '2006',
  5364. '/option',
  5365. '/select',
  5366. );
  5367. $this->assertTags($result, $expected);
  5368. $this->Form->request->data = array();
  5369. $this->Form->create('Contact');
  5370. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5371. $expected = array(
  5372. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5373. array('option' => array('value' => '2008')),
  5374. '2008',
  5375. '/option',
  5376. array('option' => array('value' => '2007')),
  5377. '2007',
  5378. '/option',
  5379. array('option' => array('value' => '2006')),
  5380. '2006',
  5381. '/option',
  5382. '/select',
  5383. );
  5384. $this->assertTags($result, $expected);
  5385. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5386. $this->assertContains('data[Contact][published][year]', $result);
  5387. }
  5388. /**
  5389. * testTextArea method
  5390. *
  5391. * @return void
  5392. */
  5393. public function testTextArea() {
  5394. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5395. $result = $this->Form->textarea('Model.field');
  5396. $expected = array(
  5397. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5398. 'some test data',
  5399. '/textarea',
  5400. );
  5401. $this->assertTags($result, $expected);
  5402. $result = $this->Form->textarea('Model.tmp');
  5403. $expected = array(
  5404. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5405. '/textarea',
  5406. );
  5407. $this->assertTags($result, $expected);
  5408. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5409. $result = $this->Form->textarea('Model.field');
  5410. $expected = array(
  5411. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5412. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5413. '/textarea',
  5414. );
  5415. $this->assertTags($result, $expected);
  5416. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5417. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5418. $expected = array(
  5419. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5420. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5421. '/textarea',
  5422. );
  5423. $this->assertTags($result, $expected);
  5424. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5425. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5426. $expected = array(
  5427. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5428. '/textarea'
  5429. );
  5430. $this->assertTags($result, $expected);
  5431. }
  5432. /**
  5433. * testTextAreaWithStupidCharacters method
  5434. *
  5435. * test text area with non-ascii characters
  5436. *
  5437. * @return void
  5438. */
  5439. public function testTextAreaWithStupidCharacters() {
  5440. $this->loadFixtures('Post');
  5441. $result = $this->Form->input('Post.content', array(
  5442. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5443. ));
  5444. $expected = array(
  5445. 'div' => array('class' => 'input text'),
  5446. 'label' => array('for' => 'PostContent'),
  5447. 'Current Text',
  5448. '/label',
  5449. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5450. 'GREAT®',
  5451. '/textarea',
  5452. '/div'
  5453. );
  5454. $this->assertTags($result, $expected);
  5455. }
  5456. /**
  5457. * testHiddenField method
  5458. *
  5459. * @return void
  5460. */
  5461. public function testHiddenField() {
  5462. $Contact = ClassRegistry::getObject('Contact');
  5463. $Contact->validationErrors['field'] = 1;
  5464. $this->Form->request->data['Contact']['field'] = 'test';
  5465. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5466. $this->assertTags($result, array(
  5467. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5468. );
  5469. }
  5470. /**
  5471. * testFileUploadField method
  5472. *
  5473. * @return void
  5474. */
  5475. public function testFileUploadField() {
  5476. $result = $this->Form->file('Model.upload');
  5477. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5478. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5479. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5480. $expected = array(
  5481. 'div' => array('class' => 'input file'),
  5482. 'label' => array('for' => 'ModelUpload'),
  5483. 'Upload',
  5484. '/label',
  5485. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5486. '/div'
  5487. );
  5488. $this->assertTags($result, $expected);
  5489. }
  5490. /**
  5491. * test File upload input on a model not used in create();
  5492. *
  5493. * @return void
  5494. */
  5495. public function testFileUploadOnOtherModel() {
  5496. $this->Form->create('ValidateUser', array('type' => 'file'));
  5497. $result = $this->Form->file('ValidateProfile.city');
  5498. $expected = array(
  5499. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5500. );
  5501. $this->assertTags($result, $expected);
  5502. }
  5503. /**
  5504. * testButton method
  5505. *
  5506. * @return void
  5507. */
  5508. public function testButton() {
  5509. $result = $this->Form->button('Hi');
  5510. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5511. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5512. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5513. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5514. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5515. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5516. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5517. $result = $this->Form->button('No type', array('type' => false));
  5518. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5519. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5520. $this->assertNotRegExp('/\&039/', $result);
  5521. }
  5522. /**
  5523. * Test that button() makes unlocked fields by default.
  5524. *
  5525. * @return void
  5526. */
  5527. public function testButtonUnlockedByDefault() {
  5528. $this->Form->request->params['_Token']['key'] = 'secured';
  5529. $this->Form->button('Save', array('name' => 'save'));
  5530. $this->Form->button('Clear');
  5531. $result = $this->Form->unlockField();
  5532. $this->assertEquals(array('save'), $result);
  5533. }
  5534. /**
  5535. * testPostButton method
  5536. *
  5537. * @return void
  5538. */
  5539. public function testPostButton() {
  5540. $result = $this->Form->postButton('Hi', '/controller/action');
  5541. $this->assertTags($result, array(
  5542. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5543. 'div' => array('style' => 'display:none;'),
  5544. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5545. '/div',
  5546. 'button' => array('type' => 'submit'),
  5547. 'Hi',
  5548. '/button',
  5549. '/form'
  5550. ));
  5551. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5552. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5553. }
  5554. /**
  5555. * Test that postButton adds _Token fields.
  5556. *
  5557. * @return void
  5558. */
  5559. public function testSecurePostButton() {
  5560. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5561. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5562. $expected = array(
  5563. 'form' => array(
  5564. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5565. ),
  5566. array('div' => array('style' => 'display:none;')),
  5567. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5568. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5569. '/div',
  5570. 'button' => array('type' => 'submit'),
  5571. 'Delete',
  5572. '/button',
  5573. array('div' => array('style' => 'display:none;')),
  5574. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5575. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5576. '/div',
  5577. '/form',
  5578. );
  5579. $this->assertTags($result, $expected);
  5580. }
  5581. /**
  5582. * testPostLink method
  5583. *
  5584. * @return void
  5585. */
  5586. public function testPostLink() {
  5587. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5588. $this->assertTags($result, array(
  5589. 'form' => array(
  5590. 'method' => 'post', 'action' => '/posts/delete/1',
  5591. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5592. ),
  5593. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5594. '/form',
  5595. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5596. 'Delete',
  5597. '/a'
  5598. ));
  5599. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5600. $this->assertTags($result, array(
  5601. 'form' => array(
  5602. 'method' => 'post', 'action' => '/posts/delete/1',
  5603. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5604. ),
  5605. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5606. '/form',
  5607. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5608. 'Delete',
  5609. '/a'
  5610. ));
  5611. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5612. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5613. }
  5614. /**
  5615. * Test that postLink adds _Token fields.
  5616. *
  5617. * @return void
  5618. */
  5619. public function testSecurePostLink() {
  5620. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5621. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5622. $expected = array(
  5623. 'form' => array(
  5624. 'method' => 'post', 'action' => '/posts/delete/1',
  5625. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5626. ),
  5627. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5628. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5629. 'div' => array('style' => 'display:none;'),
  5630. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5631. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5632. '/div',
  5633. '/form',
  5634. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5635. 'Delete',
  5636. '/a'
  5637. );
  5638. $this->assertTags($result, $expected);
  5639. }
  5640. /**
  5641. * testSubmitButton method
  5642. *
  5643. * @return void
  5644. */
  5645. public function testSubmitButton() {
  5646. $result = $this->Form->submit('');
  5647. $expected = array(
  5648. 'div' => array('class' => 'submit'),
  5649. 'input' => array('type' => 'submit', 'value' => ''),
  5650. '/div'
  5651. );
  5652. $this->assertTags($result, $expected);
  5653. $result = $this->Form->submit('Test Submit');
  5654. $expected = array(
  5655. 'div' => array('class' => 'submit'),
  5656. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5657. '/div'
  5658. );
  5659. $this->assertTags($result, $expected);
  5660. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5661. $expected = array(
  5662. 'span' => array('class' => 'submit'),
  5663. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5664. '/span'
  5665. );
  5666. $this->assertTags($result, $expected);
  5667. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5668. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5669. $this->assertTags($result, $expected);
  5670. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5671. $expected = array(
  5672. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5673. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5674. '/div'
  5675. );
  5676. $this->assertTags($result, $expected);
  5677. $result = $this->Form->submit('Next >');
  5678. $expected = array(
  5679. 'div' => array('class' => 'submit'),
  5680. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5681. '/div'
  5682. );
  5683. $this->assertTags($result, $expected);
  5684. $result = $this->Form->submit('Next >', array('escape' => false));
  5685. $expected = array(
  5686. 'div' => array('class' => 'submit'),
  5687. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5688. '/div'
  5689. );
  5690. $this->assertTags($result, $expected);
  5691. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5692. $expected = array(
  5693. 'div' => array('class' => 'submit'),
  5694. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5695. '/div'
  5696. );
  5697. $this->assertTags($result, $expected);
  5698. $before = '--before--';
  5699. $after = '--after--';
  5700. $result = $this->Form->submit('Test', array('before' => $before));
  5701. $expected = array(
  5702. 'div' => array('class' => 'submit'),
  5703. '--before--',
  5704. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5705. '/div'
  5706. );
  5707. $this->assertTags($result, $expected);
  5708. $result = $this->Form->submit('Test', array('after' => $after));
  5709. $expected = array(
  5710. 'div' => array('class' => 'submit'),
  5711. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5712. '--after--',
  5713. '/div'
  5714. );
  5715. $this->assertTags($result, $expected);
  5716. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5717. $expected = array(
  5718. 'div' => array('class' => 'submit'),
  5719. '--before--',
  5720. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5721. '--after--',
  5722. '/div'
  5723. );
  5724. $this->assertTags($result, $expected);
  5725. }
  5726. /**
  5727. * test image submit types.
  5728. *
  5729. * @return void
  5730. */
  5731. public function testSubmitImage() {
  5732. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5733. $expected = array(
  5734. 'div' => array('class' => 'submit'),
  5735. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5736. '/div'
  5737. );
  5738. $this->assertTags($result, $expected);
  5739. $result = $this->Form->submit('/relative/cake.power.gif');
  5740. $expected = array(
  5741. 'div' => array('class' => 'submit'),
  5742. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5743. '/div'
  5744. );
  5745. $this->assertTags($result, $expected);
  5746. $result = $this->Form->submit('cake.power.gif');
  5747. $expected = array(
  5748. 'div' => array('class' => 'submit'),
  5749. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5750. '/div'
  5751. );
  5752. $this->assertTags($result, $expected);
  5753. $result = $this->Form->submit('Not.an.image');
  5754. $expected = array(
  5755. 'div' => array('class' => 'submit'),
  5756. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5757. '/div'
  5758. );
  5759. $this->assertTags($result, $expected);
  5760. $after = '--after--';
  5761. $before = '--before--';
  5762. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5763. $expected = array(
  5764. 'div' => array('class' => 'submit'),
  5765. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5766. '--after--',
  5767. '/div'
  5768. );
  5769. $this->assertTags($result, $expected);
  5770. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5771. $expected = array(
  5772. 'div' => array('class' => 'submit'),
  5773. '--before--',
  5774. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5775. '/div'
  5776. );
  5777. $this->assertTags($result, $expected);
  5778. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5779. $expected = array(
  5780. 'div' => array('class' => 'submit'),
  5781. '--before--',
  5782. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5783. '--after--',
  5784. '/div'
  5785. );
  5786. $this->assertTags($result, $expected);
  5787. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5788. $expected = array(
  5789. 'div' => array('class' => 'submit'),
  5790. '--before--',
  5791. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5792. '--after--',
  5793. '/div'
  5794. );
  5795. $this->assertTags($result, $expected);
  5796. }
  5797. /**
  5798. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5799. * be submitted at a time.
  5800. *
  5801. * @return void
  5802. */
  5803. public function testSubmitUnlockedByDefault() {
  5804. $this->Form->request->params['_Token']['key'] = 'secured';
  5805. $this->Form->submit('Go go');
  5806. $this->Form->submit('Save', array('name' => 'save'));
  5807. $result = $this->Form->unlockField();
  5808. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5809. }
  5810. /**
  5811. * Test submit image with timestamps.
  5812. *
  5813. * @return void
  5814. */
  5815. public function testSubmitImageTimestamp() {
  5816. Configure::write('Asset.timestamp', 'force');
  5817. $result = $this->Form->submit('cake.power.gif');
  5818. $expected = array(
  5819. 'div' => array('class' => 'submit'),
  5820. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5821. '/div'
  5822. );
  5823. $this->assertTags($result, $expected);
  5824. }
  5825. /**
  5826. * test the create() method
  5827. *
  5828. * @return void
  5829. */
  5830. public function testCreate() {
  5831. $result = $this->Form->create('Contact');
  5832. $encoding = strtolower(Configure::read('App.encoding'));
  5833. $expected = array(
  5834. 'form' => array(
  5835. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5836. 'accept-charset' => $encoding
  5837. ),
  5838. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5839. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5840. '/div'
  5841. );
  5842. $this->assertTags($result, $expected);
  5843. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5844. $expected = array('form' => array(
  5845. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5846. 'accept-charset' => $encoding
  5847. ));
  5848. $this->assertTags($result, $expected);
  5849. $result = $this->Form->create('Contact', array('type' => 'get'));
  5850. $expected = array('form' => array(
  5851. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5852. 'accept-charset' => $encoding
  5853. ));
  5854. $this->assertTags($result, $expected);
  5855. $result = $this->Form->create('Contact', array('type' => 'put'));
  5856. $expected = array(
  5857. 'form' => array(
  5858. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5859. 'accept-charset' => $encoding
  5860. ),
  5861. 'div' => array('style' => 'display:none;'),
  5862. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5863. '/div'
  5864. );
  5865. $this->assertTags($result, $expected);
  5866. $result = $this->Form->create('Contact', array('type' => 'file'));
  5867. $expected = array(
  5868. 'form' => array(
  5869. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5870. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5871. ),
  5872. 'div' => array('style' => 'display:none;'),
  5873. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5874. '/div'
  5875. );
  5876. $this->assertTags($result, $expected);
  5877. $this->Form->request->data['Contact']['id'] = 1;
  5878. $this->Form->request->here = '/contacts/edit/1';
  5879. $this->Form->request['action'] = 'edit';
  5880. $result = $this->Form->create('Contact');
  5881. $expected = array(
  5882. 'form' => array(
  5883. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5884. 'accept-charset' => $encoding
  5885. ),
  5886. 'div' => array('style' => 'display:none;'),
  5887. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5888. '/div'
  5889. );
  5890. $this->assertTags($result, $expected);
  5891. $this->Form->request->data['Contact']['id'] = 1;
  5892. $this->Form->request->here = '/contacts/edit/1';
  5893. $this->Form->request['action'] = 'edit';
  5894. $result = $this->Form->create('Contact', array('type' => 'file'));
  5895. $expected = array(
  5896. 'form' => array(
  5897. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5898. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5899. ),
  5900. 'div' => array('style' => 'display:none;'),
  5901. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5902. '/div'
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5906. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5907. $expected = array(
  5908. 'form' => array(
  5909. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5910. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5911. ),
  5912. 'div' => array('style' => 'display:none;'),
  5913. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5914. '/div'
  5915. );
  5916. $this->assertTags($result, $expected);
  5917. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5918. $expected = array(
  5919. 'form' => array(
  5920. 'id' => 'TestId', '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['action'] = 'add';
  5929. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5930. $expected = array(
  5931. 'form' => array(
  5932. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5933. 'accept-charset' => $encoding
  5934. ),
  5935. 'div' => array('style' => 'display:none;'),
  5936. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5937. '/div'
  5938. );
  5939. $this->assertTags($result, $expected);
  5940. $result = $this->Form->create('User', array('action' => 'login'));
  5941. $expected = array(
  5942. 'form' => array(
  5943. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5944. 'accept-charset' => $encoding
  5945. ),
  5946. 'div' => array('style' => 'display:none;'),
  5947. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5948. '/div'
  5949. );
  5950. $this->assertTags($result, $expected);
  5951. $result = $this->Form->create('User', array('url' => '/users/login'));
  5952. $expected = array(
  5953. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5954. 'div' => array('style' => 'display:none;'),
  5955. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5956. '/div'
  5957. );
  5958. $this->assertTags($result, $expected);
  5959. $this->Form->request['controller'] = 'pages';
  5960. $result = $this->Form->create('User', array('action' => 'signup'));
  5961. $expected = array(
  5962. 'form' => array(
  5963. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5964. 'accept-charset' => $encoding
  5965. ),
  5966. 'div' => array('style' => 'display:none;'),
  5967. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5968. '/div'
  5969. );
  5970. $this->assertTags($result, $expected);
  5971. $this->Form->request->data = array();
  5972. $this->Form->request['controller'] = 'contacts';
  5973. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5974. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5975. $expected = array(
  5976. 'form' => array(
  5977. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5978. 'accept-charset' => 'utf-8'
  5979. ),
  5980. 'div' => array('style' => 'display:none;'),
  5981. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5982. '/div'
  5983. );
  5984. $this->assertTags($result, $expected);
  5985. }
  5986. /**
  5987. * Test the onsubmit option for create()
  5988. *
  5989. * @return void
  5990. */
  5991. public function testCreateOnSubmit() {
  5992. $this->Form->request->data = array();
  5993. $this->Form->request['controller'] = 'contacts';
  5994. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5995. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5996. $expected = array(
  5997. 'form' => array(
  5998. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5999. 'accept-charset' => 'utf-8'
  6000. ),
  6001. 'div' => array('style' => 'display:none;'),
  6002. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6003. '/div'
  6004. );
  6005. $this->assertTags($result, $expected);
  6006. $this->Form->request->data = array();
  6007. $this->Form->request['controller'] = 'contacts';
  6008. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6009. $result = $this->Form->create(array(
  6010. 'url' => array('action' => 'index', 'param'),
  6011. 'default' => false,
  6012. 'onsubmit' => 'someFunction();'
  6013. ));
  6014. $expected = array(
  6015. 'form' => array(
  6016. 'id' => 'ContactAddForm', 'method' => 'post',
  6017. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6018. 'action' => '/contacts/index/param',
  6019. 'accept-charset' => 'utf-8'
  6020. ),
  6021. 'div' => array('style' => 'display:none;'),
  6022. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6023. '/div'
  6024. );
  6025. $this->assertTags($result, $expected);
  6026. }
  6027. /**
  6028. * test create() with automatic url generation
  6029. *
  6030. * @return void
  6031. */
  6032. public function testCreateAutoUrl() {
  6033. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6034. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6035. $this->Form->request->base = '/base_url';
  6036. $result = $this->Form->create('Contact');
  6037. $expected = array(
  6038. 'form' => array(
  6039. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6040. 'accept-charset' => 'utf-8'
  6041. ),
  6042. 'div' => array('style' => 'display:none;'),
  6043. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6044. '/div'
  6045. );
  6046. $this->assertTags($result, $expected);
  6047. $this->Form->request['action'] = 'delete';
  6048. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6049. $this->Form->request->base = '/base_url';
  6050. $result = $this->Form->create('Contact');
  6051. $expected = array(
  6052. 'form' => array(
  6053. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6054. 'accept-charset' => 'utf-8'
  6055. ),
  6056. 'div' => array('style' => 'display:none;'),
  6057. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6058. '/div'
  6059. );
  6060. $this->assertTags($result, $expected);
  6061. }
  6062. /**
  6063. * test create() with a custom route
  6064. *
  6065. * @return void
  6066. */
  6067. public function testCreateCustomRoute() {
  6068. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6069. $encoding = strtolower(Configure::read('App.encoding'));
  6070. $result = $this->Form->create('User', array('action' => 'login'));
  6071. $expected = array(
  6072. 'form' => array(
  6073. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6074. 'accept-charset' => $encoding
  6075. ),
  6076. 'div' => array('style' => 'display:none;'),
  6077. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6078. '/div'
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. }
  6082. /**
  6083. * test that inputDefaults are stored and used.
  6084. *
  6085. * @return void
  6086. */
  6087. public function testCreateWithInputDefaults() {
  6088. $this->Form->create('User', array(
  6089. 'inputDefaults' => array(
  6090. 'div' => false,
  6091. 'label' => false,
  6092. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6093. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6094. )
  6095. ));
  6096. $result = $this->Form->input('username');
  6097. $expected = array(
  6098. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6099. );
  6100. $this->assertTags($result, $expected);
  6101. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6102. $expected = array(
  6103. 'div' => array('class' => 'input text'),
  6104. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6105. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6106. '/div'
  6107. );
  6108. $this->assertTags($result, $expected);
  6109. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6110. $expected = array(
  6111. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6112. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6113. );
  6114. $this->assertTags($result, $expected);
  6115. $this->Form->create('User', array(
  6116. 'inputDefaults' => array(
  6117. 'div' => false,
  6118. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6119. )
  6120. ));
  6121. $result = $this->Form->input('username', array('div' => true));
  6122. $expected = array(
  6123. 'div' => array('class' => 'input text'),
  6124. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6125. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6126. '/div'
  6127. );
  6128. $this->assertTags($result, $expected);
  6129. }
  6130. /**
  6131. * test automatic accept-charset overriding
  6132. *
  6133. * @return void
  6134. */
  6135. public function testCreateWithAcceptCharset() {
  6136. $result = $this->Form->create('UserForm', array(
  6137. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6138. )
  6139. );
  6140. $expected = array(
  6141. 'form' => array(
  6142. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6143. 'accept-charset' => 'iso-8859-1'
  6144. ),
  6145. 'div' => array('style' => 'display:none;'),
  6146. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6147. '/div'
  6148. );
  6149. $this->assertTags($result, $expected);
  6150. }
  6151. /**
  6152. * Test base form url when url param is passed with multiple parameters (&)
  6153. *
  6154. */
  6155. public function testCreateQuerystringrequest() {
  6156. $encoding = strtolower(Configure::read('App.encoding'));
  6157. $result = $this->Form->create('Contact', array(
  6158. 'type' => 'post',
  6159. 'escape' => false,
  6160. 'url' => array(
  6161. 'controller' => 'controller',
  6162. 'action' => 'action',
  6163. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6164. )
  6165. ));
  6166. $expected = array(
  6167. 'form' => array(
  6168. 'id' => 'ContactAddForm',
  6169. 'method' => 'post',
  6170. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6171. 'accept-charset' => $encoding
  6172. ),
  6173. 'div' => array('style' => 'display:none;'),
  6174. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6175. '/div'
  6176. );
  6177. $this->assertTags($result, $expected);
  6178. $result = $this->Form->create('Contact', array(
  6179. 'type' => 'post',
  6180. 'url' => array(
  6181. 'controller' => 'controller',
  6182. 'action' => 'action',
  6183. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6184. )
  6185. ));
  6186. $expected = array(
  6187. 'form' => array(
  6188. 'id' => 'ContactAddForm',
  6189. 'method' => 'post',
  6190. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6191. 'accept-charset' => $encoding
  6192. ),
  6193. 'div' => array('style' => 'display:none;'),
  6194. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6195. '/div'
  6196. );
  6197. $this->assertTags($result, $expected);
  6198. }
  6199. /**
  6200. * test that create() doesn't cause errors by multiple id's being in the primary key
  6201. * as could happen with multiple select or checkboxes.
  6202. *
  6203. * @return void
  6204. */
  6205. public function testCreateWithMultipleIdInData() {
  6206. $encoding = strtolower(Configure::read('App.encoding'));
  6207. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6208. $result = $this->Form->create('Contact');
  6209. $expected = array(
  6210. 'form' => array(
  6211. 'id' => 'ContactAddForm',
  6212. 'method' => 'post',
  6213. 'action' => '/contacts/add',
  6214. 'accept-charset' => $encoding
  6215. ),
  6216. 'div' => array('style' => 'display:none;'),
  6217. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6218. '/div'
  6219. );
  6220. $this->assertTags($result, $expected);
  6221. }
  6222. /**
  6223. * test that create() doesn't add in extra passed params.
  6224. *
  6225. * @return void
  6226. */
  6227. public function testCreatePassedArgs() {
  6228. $encoding = strtolower(Configure::read('App.encoding'));
  6229. $this->Form->request->data['Contact']['id'] = 1;
  6230. $result = $this->Form->create('Contact', array(
  6231. 'type' => 'post',
  6232. 'escape' => false,
  6233. 'url' => array(
  6234. 'action' => 'edit',
  6235. 'myparam'
  6236. )
  6237. ));
  6238. $expected = array(
  6239. 'form' => array(
  6240. 'id' => 'ContactAddForm',
  6241. 'method' => 'post',
  6242. 'action' => '/contacts/edit/myparam',
  6243. 'accept-charset' => $encoding
  6244. ),
  6245. 'div' => array('style' => 'display:none;'),
  6246. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6247. '/div'
  6248. );
  6249. $this->assertTags($result, $expected);
  6250. }
  6251. /**
  6252. * test creating a get form, and get form inputs.
  6253. *
  6254. * @return void
  6255. */
  6256. public function testGetFormCreate() {
  6257. $encoding = strtolower(Configure::read('App.encoding'));
  6258. $result = $this->Form->create('Contact', array('type' => 'get'));
  6259. $this->assertTags($result, array('form' => array(
  6260. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6261. 'accept-charset' => $encoding
  6262. )));
  6263. $result = $this->Form->text('Contact.name');
  6264. $this->assertTags($result, array('input' => array(
  6265. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6266. )));
  6267. $result = $this->Form->password('password');
  6268. $this->assertTags($result, array('input' => array(
  6269. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6270. )));
  6271. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6272. $result = $this->Form->text('user_form');
  6273. $this->assertTags($result, array('input' => array(
  6274. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6275. )));
  6276. }
  6277. /**
  6278. * test get form, and inputs when the model param is false
  6279. *
  6280. * @return void
  6281. */
  6282. public function testGetFormWithFalseModel() {
  6283. $encoding = strtolower(Configure::read('App.encoding'));
  6284. $this->Form->request['controller'] = 'contact_test';
  6285. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6286. $expected = array('form' => array(
  6287. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6288. 'accept-charset' => $encoding
  6289. ));
  6290. $this->assertTags($result, $expected);
  6291. $result = $this->Form->text('reason');
  6292. $expected = array(
  6293. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6294. );
  6295. $this->assertTags($result, $expected);
  6296. }
  6297. /**
  6298. * test that datetime() works with GET style forms.
  6299. *
  6300. * @return void
  6301. */
  6302. public function testDateTimeWithGetForms() {
  6303. extract($this->dateRegex);
  6304. $this->Form->create('Contact', array('type' => 'get'));
  6305. $result = $this->Form->datetime('created');
  6306. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6307. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6308. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6309. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6310. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6311. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6312. }
  6313. /**
  6314. * testEditFormWithData method
  6315. *
  6316. * test auto populating form elements from submitted data.
  6317. *
  6318. * @return void
  6319. */
  6320. public function testEditFormWithData() {
  6321. $this->Form->request->data = array('Person' => array(
  6322. 'id' => 1,
  6323. 'first_name' => 'Nate',
  6324. 'last_name' => 'Abele',
  6325. 'email' => 'nate@example.com'
  6326. ));
  6327. $this->Form->request->addParams(array(
  6328. 'models' => array('Person'),
  6329. 'controller' => 'people',
  6330. 'action' => 'add'
  6331. ));
  6332. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6333. $this->Form->create();
  6334. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6335. $expected = array(
  6336. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6337. 'select' => array(
  6338. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6339. ),
  6340. array('option' => array('value' => 1)), 'Nate', '/option',
  6341. array('option' => array('value' => 2)), 'Garrett', '/option',
  6342. array('option' => array('value' => 3)), 'Larry', '/option',
  6343. '/select'
  6344. );
  6345. $this->assertTags($result, $expected);
  6346. }
  6347. /**
  6348. * Test that required fields are created for various types of validation.
  6349. *
  6350. * @return void
  6351. */
  6352. public function testFormInputRequiredDetection() {
  6353. $this->Form->create('Contact');
  6354. $result = $this->Form->input('Contact.non_existing');
  6355. $expected = array(
  6356. 'div' => array('class' => 'input text'),
  6357. 'label' => array('for' => 'ContactNonExisting'),
  6358. 'Non Existing',
  6359. '/label',
  6360. 'input' => array(
  6361. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6362. 'id' => 'ContactNonExisting'
  6363. ),
  6364. '/div'
  6365. );
  6366. $this->assertTags($result, $expected);
  6367. $result = $this->Form->input('Contact.imrequired');
  6368. $expected = array(
  6369. 'div' => array('class' => 'input text required'),
  6370. 'label' => array('for' => 'ContactImrequired'),
  6371. 'Imrequired',
  6372. '/label',
  6373. 'input' => array(
  6374. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6375. 'id' => 'ContactImrequired'
  6376. ),
  6377. '/div'
  6378. );
  6379. $this->assertTags($result, $expected);
  6380. $result = $this->Form->input('Contact.imalsorequired');
  6381. $expected = array(
  6382. 'div' => array('class' => 'input text required'),
  6383. 'label' => array('for' => 'ContactImalsorequired'),
  6384. 'Imalsorequired',
  6385. '/label',
  6386. 'input' => array(
  6387. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6388. 'id' => 'ContactImalsorequired'
  6389. ),
  6390. '/div'
  6391. );
  6392. $this->assertTags($result, $expected);
  6393. $result = $this->Form->input('Contact.imrequiredtoo');
  6394. $expected = array(
  6395. 'div' => array('class' => 'input text required'),
  6396. 'label' => array('for' => 'ContactImrequiredtoo'),
  6397. 'Imrequiredtoo',
  6398. '/label',
  6399. 'input' => array(
  6400. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6401. 'id' => 'ContactImrequiredtoo'
  6402. ),
  6403. '/div'
  6404. );
  6405. $this->assertTags($result, $expected);
  6406. $result = $this->Form->input('Contact.required_one');
  6407. $expected = array(
  6408. 'div' => array('class' => 'input text required'),
  6409. 'label' => array('for' => 'ContactRequiredOne'),
  6410. 'Required One',
  6411. '/label',
  6412. 'input' => array(
  6413. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6414. 'id' => 'ContactRequiredOne'
  6415. ),
  6416. '/div'
  6417. );
  6418. $this->assertTags($result, $expected);
  6419. $result = $this->Form->input('Contact.string_required');
  6420. $expected = array(
  6421. 'div' => array('class' => 'input text required'),
  6422. 'label' => array('for' => 'ContactStringRequired'),
  6423. 'String Required',
  6424. '/label',
  6425. 'input' => array(
  6426. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6427. 'id' => 'ContactStringRequired'
  6428. ),
  6429. '/div'
  6430. );
  6431. $this->assertTags($result, $expected);
  6432. $result = $this->Form->input('Contact.imnotrequired');
  6433. $expected = array(
  6434. 'div' => array('class' => 'input text'),
  6435. 'label' => array('for' => 'ContactImnotrequired'),
  6436. 'Imnotrequired',
  6437. '/label',
  6438. 'input' => array(
  6439. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6440. 'id' => 'ContactImnotrequired'
  6441. ),
  6442. '/div'
  6443. );
  6444. $this->assertTags($result, $expected);
  6445. $result = $this->Form->input('Contact.imalsonotrequired');
  6446. $expected = array(
  6447. 'div' => array('class' => 'input text'),
  6448. 'label' => array('for' => 'ContactImalsonotrequired'),
  6449. 'Imalsonotrequired',
  6450. '/label',
  6451. 'input' => array(
  6452. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6453. 'id' => 'ContactImalsonotrequired'
  6454. ),
  6455. '/div'
  6456. );
  6457. $this->assertTags($result, $expected);
  6458. $result = $this->Form->input('Contact.imnotrequiredeither');
  6459. $expected = array(
  6460. 'div' => array('class' => 'input text'),
  6461. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6462. 'Imnotrequiredeither',
  6463. '/label',
  6464. 'input' => array(
  6465. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6466. 'id' => 'ContactImnotrequiredeither'
  6467. ),
  6468. '/div'
  6469. );
  6470. $this->assertTags($result, $expected);
  6471. }
  6472. /**
  6473. * testFormMagicInput method
  6474. *
  6475. * @return void
  6476. */
  6477. public function testFormMagicInput() {
  6478. $encoding = strtolower(Configure::read('App.encoding'));
  6479. $result = $this->Form->create('Contact');
  6480. $expected = array(
  6481. 'form' => array(
  6482. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6483. 'accept-charset' => $encoding
  6484. ),
  6485. 'div' => array('style' => 'display:none;'),
  6486. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6487. '/div'
  6488. );
  6489. $this->assertTags($result, $expected);
  6490. $result = $this->Form->input('name');
  6491. $expected = array(
  6492. 'div' => array('class' => 'input text'),
  6493. 'label' => array('for' => 'ContactName'),
  6494. 'Name',
  6495. '/label',
  6496. 'input' => array(
  6497. 'type' => 'text', 'name' => 'data[Contact][name]',
  6498. 'id' => 'ContactName', 'maxlength' => '255'
  6499. ),
  6500. '/div'
  6501. );
  6502. $this->assertTags($result, $expected);
  6503. $result = $this->Form->input('non_existing_field_in_contact_model');
  6504. $expected = array(
  6505. 'div' => array('class' => 'input text'),
  6506. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6507. 'Non Existing Field In Contact Model',
  6508. '/label',
  6509. 'input' => array(
  6510. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6511. 'id' => 'ContactNonExistingFieldInContactModel'
  6512. ),
  6513. '/div'
  6514. );
  6515. $this->assertTags($result, $expected);
  6516. $result = $this->Form->input('Address.street');
  6517. $expected = array(
  6518. 'div' => array('class' => 'input text'),
  6519. 'label' => array('for' => 'AddressStreet'),
  6520. 'Street',
  6521. '/label',
  6522. 'input' => array(
  6523. 'type' => 'text', 'name' => 'data[Address][street]',
  6524. 'id' => 'AddressStreet'
  6525. ),
  6526. '/div'
  6527. );
  6528. $this->assertTags($result, $expected);
  6529. $result = $this->Form->input('Address.non_existing_field_in_model');
  6530. $expected = array(
  6531. 'div' => array('class' => 'input text'),
  6532. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6533. 'Non Existing Field In Model',
  6534. '/label',
  6535. 'input' => array(
  6536. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6537. 'id' => 'AddressNonExistingFieldInModel'
  6538. ),
  6539. '/div'
  6540. );
  6541. $this->assertTags($result, $expected);
  6542. $result = $this->Form->input('name', array('div' => false));
  6543. $expected = array(
  6544. 'label' => array('for' => 'ContactName'),
  6545. 'Name',
  6546. '/label',
  6547. 'input' => array(
  6548. 'type' => 'text', 'name' => 'data[Contact][name]',
  6549. 'id' => 'ContactName', 'maxlength' => '255'
  6550. )
  6551. );
  6552. $this->assertTags($result, $expected);
  6553. extract($this->dateRegex);
  6554. $now = strtotime('now');
  6555. $result = $this->Form->input('Contact.published', array('div' => false));
  6556. $expected = array(
  6557. 'label' => array('for' => 'ContactPublishedMonth'),
  6558. 'Published',
  6559. '/label',
  6560. array('select' => array(
  6561. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6562. )),
  6563. $monthsRegex,
  6564. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6565. date('F', $now),
  6566. '/option',
  6567. '*/select',
  6568. '-',
  6569. array('select' => array(
  6570. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6571. )),
  6572. $daysRegex,
  6573. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6574. date('j', $now),
  6575. '/option',
  6576. '*/select',
  6577. '-',
  6578. array('select' => array(
  6579. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6580. )),
  6581. $yearsRegex,
  6582. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6583. date('Y', $now),
  6584. '*/select'
  6585. );
  6586. $this->assertTags($result, $expected);
  6587. $result = $this->Form->input('Contact.updated', array('div' => false));
  6588. $expected = array(
  6589. 'label' => array('for' => 'ContactUpdatedMonth'),
  6590. 'Updated',
  6591. '/label',
  6592. array('select' => array(
  6593. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6594. )),
  6595. $monthsRegex,
  6596. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6597. date('F', $now),
  6598. '/option',
  6599. '*/select',
  6600. '-',
  6601. array('select' => array(
  6602. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6603. )),
  6604. $daysRegex,
  6605. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6606. date('j', $now),
  6607. '/option',
  6608. '*/select',
  6609. '-',
  6610. array('select' => array(
  6611. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6612. )),
  6613. $yearsRegex,
  6614. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6615. date('Y', $now),
  6616. '*/select'
  6617. );
  6618. $this->assertTags($result, $expected);
  6619. $result = $this->Form->input('UserForm.stuff');
  6620. $expected = array(
  6621. 'div' => array('class' => 'input text'),
  6622. 'label' => array('for' => 'UserFormStuff'),
  6623. 'Stuff',
  6624. '/label',
  6625. 'input' => array(
  6626. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6627. 'id' => 'UserFormStuff', 'maxlength' => 10
  6628. ),
  6629. '/div'
  6630. );
  6631. $this->assertTags($result, $expected);
  6632. }
  6633. /**
  6634. * testForMagicInputNonExistingNorValidated method
  6635. *
  6636. * @return void
  6637. */
  6638. public function testForMagicInputNonExistingNorValidated() {
  6639. $encoding = strtolower(Configure::read('App.encoding'));
  6640. $result = $this->Form->create('Contact');
  6641. $expected = array(
  6642. 'form' => array(
  6643. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6644. 'accept-charset' => $encoding
  6645. ),
  6646. 'div' => array('style' => 'display:none;'),
  6647. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6648. '/div'
  6649. );
  6650. $this->assertTags($result, $expected);
  6651. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6652. $expected = array(
  6653. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6654. 'Non Existing Nor Validated',
  6655. '/label',
  6656. 'input' => array(
  6657. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6658. 'id' => 'ContactNonExistingNorValidated'
  6659. )
  6660. );
  6661. $this->assertTags($result, $expected);
  6662. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6663. 'div' => false, 'value' => 'my value'
  6664. ));
  6665. $expected = array(
  6666. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6667. 'Non Existing Nor Validated',
  6668. '/label',
  6669. 'input' => array(
  6670. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6671. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6672. )
  6673. );
  6674. $this->assertTags($result, $expected);
  6675. $this->Form->request->data = array(
  6676. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6677. ));
  6678. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6679. $expected = array(
  6680. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6681. 'Non Existing Nor Validated',
  6682. '/label',
  6683. 'input' => array(
  6684. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6685. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6686. )
  6687. );
  6688. $this->assertTags($result, $expected);
  6689. }
  6690. /**
  6691. * testFormMagicInputLabel method
  6692. *
  6693. * @return void
  6694. */
  6695. public function testFormMagicInputLabel() {
  6696. $encoding = strtolower(Configure::read('App.encoding'));
  6697. $result = $this->Form->create('Contact');
  6698. $expected = array(
  6699. 'form' => array(
  6700. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6701. 'accept-charset' => $encoding
  6702. ),
  6703. 'div' => array('style' => 'display:none;'),
  6704. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6705. '/div'
  6706. );
  6707. $this->assertTags($result, $expected);
  6708. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6709. $this->assertTags($result, array('input' => array(
  6710. 'name' => 'data[Contact][name]', 'type' => 'text',
  6711. 'id' => 'ContactName', 'maxlength' => '255')
  6712. ));
  6713. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6714. $expected = array(
  6715. 'label' => array('for' => 'ContactName'),
  6716. 'My label',
  6717. '/label',
  6718. 'input' => array(
  6719. 'type' => 'text', 'name' => 'data[Contact][name]',
  6720. 'id' => 'ContactName', 'maxlength' => '255'
  6721. )
  6722. );
  6723. $this->assertTags($result, $expected);
  6724. $result = $this->Form->input('Contact.name', array(
  6725. 'div' => false, 'label' => array('class' => 'mandatory')
  6726. ));
  6727. $expected = array(
  6728. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6729. 'Name',
  6730. '/label',
  6731. 'input' => array(
  6732. 'type' => 'text', 'name' => 'data[Contact][name]',
  6733. 'id' => 'ContactName', 'maxlength' => '255'
  6734. )
  6735. );
  6736. $this->assertTags($result, $expected);
  6737. $result = $this->Form->input('Contact.name', array(
  6738. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6739. ));
  6740. $expected = array(
  6741. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6742. 'My label',
  6743. '/label',
  6744. 'input' => array(
  6745. 'type' => 'text', 'name' => 'data[Contact][name]',
  6746. 'id' => 'ContactName', 'maxlength' => '255'
  6747. )
  6748. );
  6749. $this->assertTags($result, $expected);
  6750. $result = $this->Form->input('Contact.name', array(
  6751. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6752. ));
  6753. $expected = array(
  6754. 'label' => array('for' => 'my_id'),
  6755. 'Name',
  6756. '/label',
  6757. 'input' => array(
  6758. 'type' => 'text', 'name' => 'data[Contact][name]',
  6759. 'id' => 'my_id', 'maxlength' => '255'
  6760. )
  6761. );
  6762. $this->assertTags($result, $expected);
  6763. $result = $this->Form->input('1.id');
  6764. $this->assertTags($result, array('input' => array(
  6765. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6766. 'id' => 'Contact1Id'
  6767. )));
  6768. $result = $this->Form->input("1.name");
  6769. $expected = array(
  6770. 'div' => array('class' => 'input text'),
  6771. 'label' => array('for' => 'Contact1Name'),
  6772. 'Name',
  6773. '/label',
  6774. 'input' => array(
  6775. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6776. 'id' => 'Contact1Name', 'maxlength' => '255'
  6777. ),
  6778. '/div'
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. $result = $this->Form->input('Contact.1.id');
  6782. $this->assertTags($result, array(
  6783. 'input' => array(
  6784. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6785. 'id' => 'Contact1Id'
  6786. )
  6787. ));
  6788. $result = $this->Form->input("Model.1.name");
  6789. $expected = array(
  6790. 'div' => array('class' => 'input text'),
  6791. 'label' => array('for' => 'Model1Name'),
  6792. 'Name',
  6793. '/label',
  6794. 'input' => array(
  6795. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6796. 'id' => 'Model1Name'
  6797. ),
  6798. '/div'
  6799. );
  6800. $this->assertTags($result, $expected);
  6801. }
  6802. /**
  6803. * testFormEnd method
  6804. *
  6805. * @return void
  6806. */
  6807. public function testFormEnd() {
  6808. $this->assertEquals('</form>', $this->Form->end());
  6809. $result = $this->Form->end('');
  6810. $expected = array(
  6811. 'div' => array('class' => 'submit'),
  6812. 'input' => array('type' => 'submit', 'value' => ''),
  6813. '/div',
  6814. '/form'
  6815. );
  6816. $this->assertTags($result, $expected);
  6817. $result = $this->Form->end(array('label' => ''));
  6818. $expected = array(
  6819. 'div' => array('class' => 'submit'),
  6820. 'input' => array('type' => 'submit', 'value' => ''),
  6821. '/div',
  6822. '/form'
  6823. );
  6824. $this->assertTags($result, $expected);
  6825. $result = $this->Form->end('save');
  6826. $expected = array(
  6827. 'div' => array('class' => 'submit'),
  6828. 'input' => array('type' => 'submit', 'value' => 'save'),
  6829. '/div',
  6830. '/form'
  6831. );
  6832. $this->assertTags($result, $expected);
  6833. $result = $this->Form->end(array('label' => 'save'));
  6834. $expected = array(
  6835. 'div' => array('class' => 'submit'),
  6836. 'input' => array('type' => 'submit', 'value' => 'save'),
  6837. '/div',
  6838. '/form'
  6839. );
  6840. $this->assertTags($result, $expected);
  6841. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6842. $expected = array(
  6843. 'div' => array('class' => 'submit'),
  6844. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6845. '/div',
  6846. '/form'
  6847. );
  6848. $this->assertTags($result, $expected);
  6849. $result = $this->Form->end(array('name' => 'Whatever'));
  6850. $expected = array(
  6851. 'div' => array('class' => 'submit'),
  6852. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6853. '/div',
  6854. '/form'
  6855. );
  6856. $this->assertTags($result, $expected);
  6857. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6858. $expected = array(
  6859. 'div' => array('class' => 'good'),
  6860. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6861. '/div',
  6862. '/form'
  6863. );
  6864. $this->assertTags($result, $expected);
  6865. $result = $this->Form->end(array(
  6866. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6867. ));
  6868. $expected = array(
  6869. 'div' => array('class' => 'good'),
  6870. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6871. '/div',
  6872. '/form'
  6873. );
  6874. $this->assertTags($result, $expected);
  6875. }
  6876. /**
  6877. * testMultipleFormWithIdFields method
  6878. *
  6879. * @return void
  6880. */
  6881. public function testMultipleFormWithIdFields() {
  6882. $this->Form->create('UserForm');
  6883. $result = $this->Form->input('id');
  6884. $this->assertTags($result, array('input' => array(
  6885. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6886. )));
  6887. $result = $this->Form->input('ValidateItem.id');
  6888. $this->assertTags($result, array('input' => array(
  6889. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6890. 'id' => 'ValidateItemId'
  6891. )));
  6892. $result = $this->Form->input('ValidateUser.id');
  6893. $this->assertTags($result, array('input' => array(
  6894. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6895. 'id' => 'ValidateUserId'
  6896. )));
  6897. }
  6898. /**
  6899. * testDbLessModel method
  6900. *
  6901. * @return void
  6902. */
  6903. public function testDbLessModel() {
  6904. $this->Form->create('TestMail');
  6905. $result = $this->Form->input('name');
  6906. $expected = array(
  6907. 'div' => array('class' => 'input text'),
  6908. 'label' => array('for' => 'TestMailName'),
  6909. 'Name',
  6910. '/label',
  6911. 'input' => array(
  6912. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6913. 'id' => 'TestMailName'
  6914. ),
  6915. '/div'
  6916. );
  6917. $this->assertTags($result, $expected);
  6918. ClassRegistry::init('TestMail');
  6919. $this->Form->create('TestMail');
  6920. $result = $this->Form->input('name');
  6921. $expected = array(
  6922. 'div' => array('class' => 'input text'),
  6923. 'label' => array('for' => 'TestMailName'),
  6924. 'Name',
  6925. '/label',
  6926. 'input' => array(
  6927. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6928. 'id' => 'TestMailName'
  6929. ),
  6930. '/div'
  6931. );
  6932. $this->assertTags($result, $expected);
  6933. }
  6934. /**
  6935. * testBrokenness method
  6936. *
  6937. * @return void
  6938. */
  6939. public function testBrokenness() {
  6940. /*
  6941. * #4 This test has two parents and four children. By default (as of r7117) both
  6942. * parents are show but the first parent is missing a child. This is the inconsistency
  6943. * in the default behaviour - one parent has all children, the other does not - dependent
  6944. * on the data values.
  6945. */
  6946. $result = $this->Form->select('Model.field', array(
  6947. 'Fred' => array(
  6948. 'freds_son_1' => 'Fred',
  6949. 'freds_son_2' => 'Freddie'
  6950. ),
  6951. 'Bert' => array(
  6952. 'berts_son_1' => 'Albert',
  6953. 'berts_son_2' => 'Bertie')
  6954. ),
  6955. array('showParents' => true, 'empty' => false)
  6956. );
  6957. $expected = array(
  6958. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6959. array('optgroup' => array('label' => 'Fred')),
  6960. array('option' => array('value' => 'freds_son_1')),
  6961. 'Fred',
  6962. '/option',
  6963. array('option' => array('value' => 'freds_son_2')),
  6964. 'Freddie',
  6965. '/option',
  6966. '/optgroup',
  6967. array('optgroup' => array('label' => 'Bert')),
  6968. array('option' => array('value' => 'berts_son_1')),
  6969. 'Albert',
  6970. '/option',
  6971. array('option' => array('value' => 'berts_son_2')),
  6972. 'Bertie',
  6973. '/option',
  6974. '/optgroup',
  6975. '/select'
  6976. );
  6977. $this->assertTags($result, $expected);
  6978. /*
  6979. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6980. * changed, so we should expect the same select list data, just with a different name
  6981. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6982. * This is where data corruption can occur, because when a select value is missing from
  6983. * a list a form will substitute the first value in the list - without the user knowing.
  6984. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6985. * affect the availability of 3 => 'Three' as a valid option.
  6986. */
  6987. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6988. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6989. ));
  6990. $result = $this->Form->select(
  6991. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6992. );
  6993. $expected = array(
  6994. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6995. array('option' => array('value' => 1)),
  6996. 'One',
  6997. '/option',
  6998. array('option' => array('value' => 2)),
  6999. 'Two',
  7000. '/option',
  7001. array('optgroup' => array('label' => 'Three')),
  7002. array('option' => array('value' => 3)),
  7003. 'Three',
  7004. '/option',
  7005. array('option' => array('value' => 4)),
  7006. 'Four',
  7007. '/option',
  7008. array('option' => array('value' => 5)),
  7009. 'Five',
  7010. '/option',
  7011. '/optgroup',
  7012. '/select'
  7013. );
  7014. $this->assertTags($result, $expected);
  7015. }
  7016. /**
  7017. * Test the generation of fields for a multi record form.
  7018. *
  7019. * @return void
  7020. */
  7021. public function testMultiRecordForm() {
  7022. $this->Form->create('ValidateProfile');
  7023. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7024. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7025. $expected = array(
  7026. 'div' => array('class' => 'input textarea'),
  7027. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7028. 'Name',
  7029. '/label',
  7030. 'textarea' => array(
  7031. 'id' => 'ValidateProfile1ValidateItem2Name',
  7032. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7033. 'cols' => 30,
  7034. 'rows' => 6
  7035. ),
  7036. 'Value',
  7037. '/textarea',
  7038. '/div'
  7039. );
  7040. $this->assertTags($result, $expected);
  7041. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7042. $expected = array(
  7043. 'div' => array('class' => 'input date'),
  7044. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7045. 'Created',
  7046. '/label',
  7047. array('select' => array(
  7048. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7049. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7050. )
  7051. ),
  7052. array('option' => array('value' => '')), '/option',
  7053. $this->dateRegex['monthsRegex'],
  7054. '/select', '-',
  7055. array('select' => array(
  7056. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7057. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7058. )
  7059. ),
  7060. array('option' => array('value' => '')), '/option',
  7061. $this->dateRegex['daysRegex'],
  7062. '/select', '-',
  7063. array('select' => array(
  7064. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7065. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7066. )
  7067. ),
  7068. array('option' => array('value' => '')), '/option',
  7069. $this->dateRegex['yearsRegex'],
  7070. '/select',
  7071. '/div'
  7072. );
  7073. $this->assertTags($result, $expected);
  7074. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7075. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7076. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7077. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7078. $expected = array(
  7079. 'div' => array('class' => 'input select error'),
  7080. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7081. 'Profile',
  7082. '/label',
  7083. 'select' => array(
  7084. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7085. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7086. 'class' => 'form-error'
  7087. ),
  7088. '/select',
  7089. array('div' => array('class' => 'error-message')),
  7090. 'Error',
  7091. '/div',
  7092. '/div'
  7093. );
  7094. $this->assertTags($result, $expected);
  7095. }
  7096. /**
  7097. * test the correct display of multi-record form validation errors.
  7098. *
  7099. * @return void
  7100. */
  7101. public function testMultiRecordFormValidationErrors() {
  7102. $this->Form->create('ValidateProfile');
  7103. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7104. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7105. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7106. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7107. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7108. $result = $this->Form->error('ValidateProfile.2.city');
  7109. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7110. $result = $this->Form->error('2.city');
  7111. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7112. }
  7113. /**
  7114. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7115. *
  7116. * @return void
  7117. */
  7118. public function testInputTemplate() {
  7119. $result = $this->Form->input('Contact.email', array(
  7120. 'type' => 'text', 'format' => array('input')
  7121. ));
  7122. $expected = array(
  7123. 'div' => array('class' => 'input text'),
  7124. 'input' => array(
  7125. 'type' => 'text', 'name' => 'data[Contact][email]',
  7126. 'id' => 'ContactEmail'
  7127. ),
  7128. '/div'
  7129. );
  7130. $this->assertTags($result, $expected);
  7131. $result = $this->Form->input('Contact.email', array(
  7132. 'type' => 'text', 'format' => array('input', 'label'),
  7133. 'label' => '<em>Email (required)</em>'
  7134. ));
  7135. $expected = array(
  7136. 'div' => array('class' => 'input text'),
  7137. array('input' => array(
  7138. 'type' => 'text', 'name' => 'data[Contact][email]',
  7139. 'id' => 'ContactEmail'
  7140. )),
  7141. 'label' => array('for' => 'ContactEmail'),
  7142. 'em' => array(),
  7143. 'Email (required)',
  7144. '/em',
  7145. '/label',
  7146. '/div'
  7147. );
  7148. $this->assertTags($result, $expected);
  7149. $result = $this->Form->input('Contact.email', array(
  7150. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7151. 'between' => '<div>Something in the middle</div>',
  7152. 'after' => '<span>Some text at the end</span>'
  7153. ));
  7154. $expected = array(
  7155. 'div' => array('class' => 'input text'),
  7156. array('input' => array(
  7157. 'type' => 'text', 'name' => 'data[Contact][email]',
  7158. 'id' => 'ContactEmail'
  7159. )),
  7160. array('div' => array()),
  7161. 'Something in the middle',
  7162. '/div',
  7163. 'label' => array('for' => 'ContactEmail'),
  7164. 'Email',
  7165. '/label',
  7166. 'span' => array(),
  7167. 'Some text at the end',
  7168. '/span',
  7169. '/div'
  7170. );
  7171. $this->assertTags($result, $expected);
  7172. $result = $this->Form->input('Contact.method', array(
  7173. 'type' => 'radio',
  7174. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7175. 'between' => 'I am between',
  7176. ));
  7177. $expected = array(
  7178. 'div' => array('class' => 'input radio'),
  7179. 'fieldset' => array(),
  7180. 'legend' => array(),
  7181. 'Method',
  7182. '/legend',
  7183. 'I am between',
  7184. 'input' => array(
  7185. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7186. 'value' => '', 'id' => 'ContactMethod_'
  7187. ),
  7188. array('input' => array(
  7189. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7190. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7191. )),
  7192. array('label' => array('for' => 'ContactMethodEmail')),
  7193. 'Email',
  7194. '/label',
  7195. array('input' => array(
  7196. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7197. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7198. )),
  7199. array('label' => array('for' => 'ContactMethodPigeon')),
  7200. 'Pigeon',
  7201. '/label',
  7202. '/fieldset',
  7203. '/div',
  7204. );
  7205. $this->assertTags($result, $expected);
  7206. }
  7207. /**
  7208. * test that some html5 inputs + FormHelper::__call() work
  7209. *
  7210. * @return void
  7211. */
  7212. public function testHtml5Inputs() {
  7213. $result = $this->Form->email('User.email');
  7214. $expected = array(
  7215. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7216. );
  7217. $this->assertTags($result, $expected);
  7218. $result = $this->Form->search('User.query');
  7219. $expected = array(
  7220. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7221. );
  7222. $this->assertTags($result, $expected);
  7223. $result = $this->Form->search('User.query', array('value' => 'test'));
  7224. $expected = array(
  7225. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7226. );
  7227. $this->assertTags($result, $expected);
  7228. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7229. $expected = array(
  7230. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7231. );
  7232. $this->assertTags($result, $expected);
  7233. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7234. $expected = array(
  7235. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7236. );
  7237. $this->assertTags($result, $expected);
  7238. }
  7239. /**
  7240. *
  7241. * @expectedException CakeException
  7242. * @return void
  7243. */
  7244. public function testHtml5InputException() {
  7245. $this->Form->email();
  7246. }
  7247. /**
  7248. * Tests that a model can be loaded from the model names passed in the request object
  7249. *
  7250. * @return void
  7251. */
  7252. public function testIntrospectModelFromRequest() {
  7253. $this->loadFixtures('Post');
  7254. App::build(array(
  7255. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7256. ));
  7257. CakePlugin::load('TestPlugin');
  7258. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7259. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7260. $this->Form->create('TestPluginPost');
  7261. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7262. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7263. CakePlugin::unload();
  7264. App::build();
  7265. }
  7266. /**
  7267. * Tests that it is possible to set the validation errors directly in the helper for a field
  7268. *
  7269. * @return void
  7270. */
  7271. public function testCustomValidationErrors() {
  7272. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7273. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7274. $this->assertEquals('Badness!', $result);
  7275. }
  7276. /**
  7277. * Tests that the 'on' key validates as expected on create
  7278. *
  7279. * @return void
  7280. */
  7281. public function testRequiredOnCreate() {
  7282. $this->Form->create('Contact');
  7283. $result = $this->Form->input('Contact.imrequiredonupdate');
  7284. $expected = array(
  7285. 'div' => array('class' => 'input text'),
  7286. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7287. 'Imrequiredonupdate',
  7288. '/label',
  7289. 'input' => array(
  7290. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7291. 'id' => 'ContactImrequiredonupdate'
  7292. ),
  7293. '/div'
  7294. );
  7295. $this->assertTags($result, $expected);
  7296. $result = $this->Form->input('Contact.imrequiredoncreate');
  7297. $expected = array(
  7298. 'div' => array('class' => 'input text required'),
  7299. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7300. 'Imrequiredoncreate',
  7301. '/label',
  7302. 'input' => array(
  7303. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7304. 'id' => 'ContactImrequiredoncreate'
  7305. ),
  7306. '/div'
  7307. );
  7308. $this->assertTags($result, $expected);
  7309. $result = $this->Form->input('Contact.imrequiredonboth');
  7310. $expected = array(
  7311. 'div' => array('class' => 'input text required'),
  7312. 'label' => array('for' => 'ContactImrequiredonboth'),
  7313. 'Imrequiredonboth',
  7314. '/label',
  7315. 'input' => array(
  7316. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7317. 'id' => 'ContactImrequiredonboth'
  7318. ),
  7319. '/div'
  7320. );
  7321. $this->assertTags($result, $expected);
  7322. $result = $this->Form->input('Contact.imrequired');
  7323. $expected = array(
  7324. 'div' => array('class' => 'input text required'),
  7325. 'label' => array('for' => 'ContactImrequired'),
  7326. 'Imrequired',
  7327. '/label',
  7328. 'input' => array(
  7329. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7330. 'id' => 'ContactImrequired'
  7331. ),
  7332. '/div'
  7333. );
  7334. $this->assertTags($result, $expected);
  7335. }
  7336. /**
  7337. * Tests that the 'on' key validates as expected on update
  7338. *
  7339. * @return void
  7340. */
  7341. public function testRequiredOnUpdate() {
  7342. $this->Form->request->data['Contact']['id'] = 1;
  7343. $this->Form->create('Contact');
  7344. $result = $this->Form->input('Contact.imrequiredonupdate');
  7345. $expected = array(
  7346. 'div' => array('class' => 'input text required'),
  7347. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7348. 'Imrequiredonupdate',
  7349. '/label',
  7350. 'input' => array(
  7351. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7352. 'id' => 'ContactImrequiredonupdate'
  7353. ),
  7354. '/div'
  7355. );
  7356. $this->assertTags($result, $expected);
  7357. $result = $this->Form->input('Contact.imrequiredoncreate');
  7358. $expected = array(
  7359. 'div' => array('class' => 'input text'),
  7360. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7361. 'Imrequiredoncreate',
  7362. '/label',
  7363. 'input' => array(
  7364. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7365. 'id' => 'ContactImrequiredoncreate'
  7366. ),
  7367. '/div'
  7368. );
  7369. $this->assertTags($result, $expected);
  7370. $result = $this->Form->input('Contact.imrequiredonboth');
  7371. $expected = array(
  7372. 'div' => array('class' => 'input text required'),
  7373. 'label' => array('for' => 'ContactImrequiredonboth'),
  7374. 'Imrequiredonboth',
  7375. '/label',
  7376. 'input' => array(
  7377. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7378. 'id' => 'ContactImrequiredonboth'
  7379. ),
  7380. '/div'
  7381. );
  7382. $this->assertTags($result, $expected);
  7383. $result = $this->Form->input('Contact.imrequired');
  7384. $expected = array(
  7385. 'div' => array('class' => 'input text required'),
  7386. 'label' => array('for' => 'ContactImrequired'),
  7387. 'Imrequired',
  7388. '/label',
  7389. 'input' => array(
  7390. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7391. 'id' => 'ContactImrequired'
  7392. ),
  7393. '/div'
  7394. );
  7395. $this->assertTags($result, $expected);
  7396. }
  7397. /**
  7398. * Test inputDefaults setter and getter
  7399. *
  7400. * @return void
  7401. */
  7402. public function testInputDefaults() {
  7403. $this->Form->create('Contact');
  7404. $this->Form->inputDefaults(array(
  7405. 'label' => false,
  7406. 'div' => array(
  7407. 'style' => 'color: #000;'
  7408. )
  7409. ));
  7410. $result = $this->Form->input('Contact.field1');
  7411. $expected = array(
  7412. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7413. 'input' => array(
  7414. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7415. 'id' => 'ContactField1'
  7416. ),
  7417. '/div'
  7418. );
  7419. $this->assertTags($result, $expected);
  7420. $this->Form->inputDefaults(array(
  7421. 'div' => false,
  7422. 'label' => 'Label',
  7423. ));
  7424. $result = $this->Form->input('Contact.field1');
  7425. $expected = array(
  7426. 'label' => array('for' => 'ContactField1'),
  7427. 'Label',
  7428. '/label',
  7429. 'input' => array(
  7430. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7431. 'id' => 'ContactField1'
  7432. ),
  7433. );
  7434. $this->assertTags($result, $expected);
  7435. $this->Form->inputDefaults(array(
  7436. 'label' => false,
  7437. ), true);
  7438. $result = $this->Form->input('Contact.field1');
  7439. $expected = array(
  7440. 'input' => array(
  7441. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7442. 'id' => 'ContactField1'
  7443. ),
  7444. );
  7445. $this->assertTags($result, $expected);
  7446. $result = $this->Form->inputDefaults();
  7447. $expected = array(
  7448. 'div' => false,
  7449. 'label' => false,
  7450. );
  7451. $this->assertEqual($result, $expected);
  7452. }
  7453. }