PageRenderTime 178ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 2ms

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

https://github.com/vgarcias/expediamedicus
PHP | 8168 lines | 6370 code | 645 blank | 1153 comment | 1 complexity | 1a36228bca03103773d14fafd6d2f307 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $expected .= ':' . 'Model.valid';
  710. $expected = array(
  711. 'div' => array('style' => 'display:none;'),
  712. array('input' => array(
  713. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  714. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  715. )),
  716. array('input' => array(
  717. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  718. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  719. )),
  720. '/div'
  721. );
  722. $this->assertTags($result, $expected);
  723. }
  724. /**
  725. * Tests correct generation of number fields for double and float fields
  726. *
  727. * @return void
  728. */
  729. public function testTextFieldGenerationForFloats() {
  730. $model = ClassRegistry::getObject('Contact');
  731. $model->setSchema(array('foo' => array(
  732. 'type' => 'float',
  733. 'null' => false,
  734. 'default' => null,
  735. 'length' => null
  736. )));
  737. $this->Form->create('Contact');
  738. $result = $this->Form->input('foo');
  739. $expected = array(
  740. 'div' => array('class' => 'input number'),
  741. 'label' => array('for' => 'ContactFoo'),
  742. 'Foo',
  743. '/label',
  744. array('input' => array(
  745. 'type' => 'number',
  746. 'name' => 'data[Contact][foo]',
  747. 'id' => 'ContactFoo',
  748. 'step' => 'any'
  749. )),
  750. '/div'
  751. );
  752. $this->assertTags($result, $expected);
  753. $result = $this->Form->input('foo', array('step' => 0.5));
  754. $expected = array(
  755. 'div' => array('class' => 'input number'),
  756. 'label' => array('for' => 'ContactFoo'),
  757. 'Foo',
  758. '/label',
  759. array('input' => array(
  760. 'type' => 'number',
  761. 'name' => 'data[Contact][foo]',
  762. 'id' => 'ContactFoo',
  763. 'step' => '0.5'
  764. )),
  765. '/div'
  766. );
  767. $this->assertTags($result, $expected);
  768. }
  769. /**
  770. * Tests correct generation of number fields for integer fields
  771. *
  772. * @access public
  773. * @return void
  774. */
  775. public function testTextFieldTypeNumberGenerationForIntegers() {
  776. $model = ClassRegistry::getObject('Contact');
  777. $model->setSchema(array('foo' => array(
  778. 'type' => 'integer',
  779. 'null' => false,
  780. 'default' => null,
  781. 'length' => null
  782. )));
  783. $this->Form->create('Contact');
  784. $result = $this->Form->input('foo');
  785. $expected = array(
  786. 'div' => array('class' => 'input number'),
  787. 'label' => array('for' => 'ContactFoo'),
  788. 'Foo',
  789. '/label',
  790. array('input' => array(
  791. 'type' => 'number', 'name' => 'data[Contact][foo]',
  792. 'id' => 'ContactFoo'
  793. )),
  794. '/div'
  795. );
  796. $this->assertTags($result, $expected);
  797. }
  798. /**
  799. * testFormSecurityMultipleFields method
  800. *
  801. * Test secure() with multiple row form. Ensure hash is correct.
  802. *
  803. * @return void
  804. */
  805. public function testFormSecurityMultipleFields() {
  806. $key = 'testKey';
  807. $fields = array(
  808. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  809. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  810. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  811. );
  812. $this->Form->request['_Token'] = array('key' => $key);
  813. $result = $this->Form->secure($fields);
  814. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  815. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  816. $expected = array(
  817. 'div' => array('style' => 'display:none;'),
  818. array('input' => array(
  819. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  820. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  821. )),
  822. array('input' => array(
  823. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  824. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  825. )),
  826. '/div'
  827. );
  828. $this->assertTags($result, $expected);
  829. }
  830. /**
  831. * testFormSecurityMultipleSubmitButtons
  832. *
  833. * test form submit generation and ensure that _Token is only created on end()
  834. *
  835. * @return void
  836. */
  837. public function testFormSecurityMultipleSubmitButtons() {
  838. $key = 'testKey';
  839. $this->Form->request['_Token'] = array('key' => $key);
  840. $this->Form->create('Addresses');
  841. $this->Form->input('Address.title');
  842. $this->Form->input('Address.first_name');
  843. $result = $this->Form->submit('Save', array('name' => 'save'));
  844. $expected = array(
  845. 'div' => array('class' => 'submit'),
  846. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  847. '/div',
  848. );
  849. $this->assertTags($result, $expected);
  850. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  851. $expected = array(
  852. 'div' => array('class' => 'submit'),
  853. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  854. '/div',
  855. );
  856. $this->assertTags($result, $expected);
  857. $result = $this->Form->end(null);
  858. $expected = array(
  859. 'div' => array('style' => 'display:none;'),
  860. array('input' => array(
  861. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  862. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  863. )),
  864. array('input' => array(
  865. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  866. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  867. )),
  868. '/div'
  869. );
  870. $this->assertTags($result, $expected);
  871. }
  872. /**
  873. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  874. *
  875. * @return void
  876. */
  877. public function testSecurityButtonNestedNamed() {
  878. $key = 'testKey';
  879. $this->Form->request['_Token'] = array('key' => $key);
  880. $this->Form->create('Addresses');
  881. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  882. $result = $this->Form->unlockField();
  883. $this->assertEquals(array('Address.button'), $result);
  884. }
  885. /**
  886. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  887. *
  888. * @return void
  889. */
  890. public function testSecuritySubmitNestedNamed() {
  891. $key = 'testKey';
  892. $this->Form->request['_Token'] = array('key' => $key);
  893. $this->Form->create('Addresses');
  894. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  895. $result = $this->Form->unlockField();
  896. $this->assertEquals(array('Address.button'), $result);
  897. }
  898. /**
  899. * Test that the correct fields are unlocked for image submits with no names.
  900. *
  901. * @return void
  902. */
  903. public function testSecuritySubmitImageNoName() {
  904. $key = 'testKey';
  905. $this->Form->request['_Token'] = array('key' => $key);
  906. $this->Form->create('User');
  907. $result = $this->Form->submit('save.png');
  908. $expected = array(
  909. 'div' => array('class' => 'submit'),
  910. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  911. '/div'
  912. );
  913. $this->assertTags($result, $expected);
  914. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  915. }
  916. /**
  917. * Test that the correct fields are unlocked for image submits with names.
  918. *
  919. * @return void
  920. */
  921. public function testSecuritySubmitImageName() {
  922. $key = 'testKey';
  923. $this->Form->request['_Token'] = array('key' => $key);
  924. $this->Form->create('User');
  925. $result = $this->Form->submit('save.png', array('name' => 'test'));
  926. $expected = array(
  927. 'div' => array('class' => 'submit'),
  928. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  929. '/div'
  930. );
  931. $this->assertTags($result, $expected);
  932. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  933. }
  934. /**
  935. * testFormSecurityMultipleInputFields method
  936. *
  937. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  938. *
  939. * @return void
  940. */
  941. public function testFormSecurityMultipleInputFields() {
  942. $key = 'testKey';
  943. $this->Form->request['_Token'] = array('key' => $key);
  944. $this->Form->create('Addresses');
  945. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  946. $this->Form->input('Addresses.0.title');
  947. $this->Form->input('Addresses.0.first_name');
  948. $this->Form->input('Addresses.0.last_name');
  949. $this->Form->input('Addresses.0.address');
  950. $this->Form->input('Addresses.0.city');
  951. $this->Form->input('Addresses.0.phone');
  952. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  953. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  954. $this->Form->input('Addresses.1.title');
  955. $this->Form->input('Addresses.1.first_name');
  956. $this->Form->input('Addresses.1.last_name');
  957. $this->Form->input('Addresses.1.address');
  958. $this->Form->input('Addresses.1.city');
  959. $this->Form->input('Addresses.1.phone');
  960. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  961. $result = $this->Form->secure($this->Form->fields);
  962. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  963. $expected = array(
  964. 'div' => array('style' => 'display:none;'),
  965. array('input' => array(
  966. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  967. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  968. )),
  969. array('input' => array(
  970. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  971. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  972. )),
  973. '/div'
  974. );
  975. $this->assertTags($result, $expected);
  976. }
  977. /**
  978. * Test form security with Model.field.0 style inputs
  979. *
  980. * @return void
  981. */
  982. public function testFormSecurityArrayFields() {
  983. $key = 'testKey';
  984. $this->Form->request->params['_Token']['key'] = $key;
  985. $this->Form->create('Address');
  986. $this->Form->input('Address.primary.1');
  987. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  988. $this->Form->input('Address.secondary.1.0');
  989. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  990. }
  991. /**
  992. * testFormSecurityMultipleInputDisabledFields method
  993. *
  994. * test secure form generation with multiple records and disabled fields.
  995. *
  996. * @return void
  997. */
  998. public function testFormSecurityMultipleInputDisabledFields() {
  999. $key = 'testKey';
  1000. $this->Form->request->params['_Token'] = array(
  1001. 'key' => $key,
  1002. 'unlockedFields' => array('first_name', 'address')
  1003. );
  1004. $this->Form->create();
  1005. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1006. $this->Form->input('Addresses.0.title');
  1007. $this->Form->input('Addresses.0.first_name');
  1008. $this->Form->input('Addresses.0.last_name');
  1009. $this->Form->input('Addresses.0.address');
  1010. $this->Form->input('Addresses.0.city');
  1011. $this->Form->input('Addresses.0.phone');
  1012. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1013. $this->Form->input('Addresses.1.title');
  1014. $this->Form->input('Addresses.1.first_name');
  1015. $this->Form->input('Addresses.1.last_name');
  1016. $this->Form->input('Addresses.1.address');
  1017. $this->Form->input('Addresses.1.city');
  1018. $this->Form->input('Addresses.1.phone');
  1019. $result = $this->Form->secure($this->Form->fields);
  1020. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1021. $expected = array(
  1022. 'div' => array('style' => 'display:none;'),
  1023. array('input' => array(
  1024. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1025. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1026. )),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1029. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. }
  1035. /**
  1036. * testFormSecurityInputDisabledFields method
  1037. *
  1038. * Test single record form with disabled fields.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testFormSecurityInputUnlockedFields() {
  1043. $key = 'testKey';
  1044. $this->Form->request['_Token'] = array(
  1045. 'key' => $key,
  1046. 'unlockedFields' => array('first_name', 'address')
  1047. );
  1048. $this->Form->create();
  1049. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1050. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1051. $this->Form->input('Addresses.title');
  1052. $this->Form->input('Addresses.first_name');
  1053. $this->Form->input('Addresses.last_name');
  1054. $this->Form->input('Addresses.address');
  1055. $this->Form->input('Addresses.city');
  1056. $this->Form->input('Addresses.phone');
  1057. $result = $this->Form->fields;
  1058. $expected = array(
  1059. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1060. 'Addresses.city', 'Addresses.phone'
  1061. );
  1062. $this->assertEquals($expected, $result);
  1063. $result = $this->Form->secure($expected);
  1064. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1065. $expected = array(
  1066. 'div' => array('style' => 'display:none;'),
  1067. array('input' => array(
  1068. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1069. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1070. )),
  1071. array('input' => array(
  1072. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1073. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1074. )),
  1075. '/div'
  1076. );
  1077. $this->assertTags($result, $expected);
  1078. }
  1079. /**
  1080. * test securing inputs with custom name attributes.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testFormSecureWithCustomNameAttribute() {
  1085. $this->Form->request->params['_Token']['key'] = 'testKey';
  1086. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1087. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1088. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1089. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1090. }
  1091. /**
  1092. * testFormSecuredInput method
  1093. *
  1094. * Test generation of entire secure form, assertions made on input() output.
  1095. *
  1096. * @return void
  1097. */
  1098. public function testFormSecuredInput() {
  1099. $this->Form->request['_Token'] = array('key' => 'testKey');
  1100. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1101. $encoding = strtolower(Configure::read('App.encoding'));
  1102. $expected = array(
  1103. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1104. 'div' => array('style' => 'display:none;'),
  1105. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1106. array('input' => array(
  1107. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1108. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1109. )),
  1110. '/div'
  1111. );
  1112. $this->assertTags($result, $expected);
  1113. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1114. $expected = array(
  1115. 'div' => array('class' => 'input text'),
  1116. 'label' => array('for' => 'UserFormPublished'),
  1117. 'Published',
  1118. '/label',
  1119. array('input' => array(
  1120. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1121. 'id' => 'UserFormPublished'
  1122. )),
  1123. '/div'
  1124. );
  1125. $this->assertTags($result, $expected);
  1126. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1127. $expected = array(
  1128. 'div' => array('class' => 'input text'),
  1129. 'label' => array('for' => 'UserFormOther'),
  1130. 'Other',
  1131. '/label',
  1132. array('input' => array(
  1133. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1134. 'id' => 'UserFormOther'
  1135. )),
  1136. '/div'
  1137. );
  1138. $this->assertTags($result, $expected);
  1139. $result = $this->Form->hidden('UserForm.stuff');
  1140. $expected = array('input' => array(
  1141. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1142. 'id' => 'UserFormStuff'
  1143. ));
  1144. $this->assertTags($result, $expected);
  1145. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1146. $expected = array('input' => array(
  1147. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1148. 'value' => '0', 'id' => 'UserFormHidden'
  1149. ));
  1150. $this->assertTags($result, $expected);
  1151. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1152. $expected = array(
  1153. 'div' => array('class' => 'input checkbox'),
  1154. array('input' => array(
  1155. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1156. 'value' => '0', 'id' => 'UserFormSomething_'
  1157. )),
  1158. array('input' => array(
  1159. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1160. 'value' => '1', 'id' => 'UserFormSomething'
  1161. )),
  1162. 'label' => array('for' => 'UserFormSomething'),
  1163. 'Something',
  1164. '/label',
  1165. '/div'
  1166. );
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->fields;
  1169. $expected = array(
  1170. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1171. 'UserForm.hidden' => '0', 'UserForm.something'
  1172. );
  1173. $this->assertEquals($expected, $result);
  1174. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1175. $result = $this->Form->secure($this->Form->fields);
  1176. $expected = array(
  1177. 'div' => array('style' => 'display:none;'),
  1178. array('input' => array(
  1179. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1180. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1181. )),
  1182. array('input' => array(
  1183. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1184. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1185. )),
  1186. '/div'
  1187. );
  1188. $this->assertTags($result, $expected);
  1189. }
  1190. /**
  1191. * Tests that the correct keys are added to the field hash index
  1192. *
  1193. * @return void
  1194. */
  1195. public function testFormSecuredFileInput() {
  1196. $this->Form->request['_Token'] = array('key' => 'testKey');
  1197. $this->assertEquals(array(), $this->Form->fields);
  1198. $result = $this->Form->file('Attachment.file');
  1199. $expected = array(
  1200. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1201. 'Attachment.file.error', 'Attachment.file.size'
  1202. );
  1203. $this->assertEquals($expected, $this->Form->fields);
  1204. }
  1205. /**
  1206. * test that multiple selects keys are added to field hash
  1207. *
  1208. * @return void
  1209. */
  1210. public function testFormSecuredMultipleSelect() {
  1211. $this->Form->request['_Token'] = array('key' => 'testKey');
  1212. $this->assertEquals(array(), $this->Form->fields);
  1213. $options = array('1' => 'one', '2' => 'two');
  1214. $this->Form->select('Model.select', $options);
  1215. $expected = array('Model.select');
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. $this->Form->fields = array();
  1218. $this->Form->select('Model.select', $options, array('multiple' => true));
  1219. $this->assertEquals($expected, $this->Form->fields);
  1220. }
  1221. /**
  1222. * testFormSecuredRadio method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testFormSecuredRadio() {
  1227. $this->Form->request['_Token'] = array('key' => 'testKey');
  1228. $this->assertEquals(array(), $this->Form->fields);
  1229. $options = array('1' => 'option1', '2' => 'option2');
  1230. $this->Form->radio('Test.test', $options);
  1231. $expected = array('Test.test');
  1232. $this->assertEquals($expected, $this->Form->fields);
  1233. }
  1234. /**
  1235. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1236. * hashing.
  1237. *
  1238. * @return void
  1239. */
  1240. public function testFormSecuredAndDisabled() {
  1241. $this->Form->request['_Token'] = array('key' => 'testKey');
  1242. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1243. $this->Form->text('Model.text', array('disabled' => true));
  1244. $this->Form->password('Model.text', array('disabled' => true));
  1245. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1246. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1247. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1248. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1249. $this->Form->month('Model.month', array('disabled' => true));
  1250. $this->Form->day('Model.day', array('disabled' => true));
  1251. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1252. $this->Form->minute('Model.minute', array('disabled' => true));
  1253. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1254. $expected = array(
  1255. 'Model.radio' => ''
  1256. );
  1257. $this->assertEquals($expected, $this->Form->fields);
  1258. }
  1259. /**
  1260. * testDisableSecurityUsingForm method
  1261. *
  1262. * @return void
  1263. */
  1264. public function testDisableSecurityUsingForm() {
  1265. $this->Form->request['_Token'] = array(
  1266. 'key' => 'testKey',
  1267. 'disabledFields' => array()
  1268. );
  1269. $this->Form->create();
  1270. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1271. $this->Form->input('Addresses.title');
  1272. $this->Form->input('Addresses.first_name', array('secure' => false));
  1273. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1274. $this->Form->input('Addresses.zip', array(
  1275. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1276. ));
  1277. $result = $this->Form->fields;
  1278. $expected = array(
  1279. 'Addresses.id' => '123456', 'Addresses.title',
  1280. );
  1281. $this->assertEquals($expected, $result);
  1282. }
  1283. /**
  1284. * test disableField
  1285. *
  1286. * @return void
  1287. */
  1288. public function testUnlockFieldAddsToList() {
  1289. $this->Form->request['_Token'] = array(
  1290. 'key' => 'testKey',
  1291. 'unlockedFields' => array()
  1292. );
  1293. $this->Form->create('Contact');
  1294. $this->Form->unlockField('Contact.name');
  1295. $this->Form->text('Contact.name');
  1296. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1297. $this->assertEquals(array(), $this->Form->fields);
  1298. }
  1299. /**
  1300. * test unlockField removing from fields array.
  1301. *
  1302. * @return void
  1303. */
  1304. public function testUnlockFieldRemovingFromFields() {
  1305. $this->Form->request['_Token'] = array(
  1306. 'key' => 'testKey',
  1307. 'unlockedFields' => array()
  1308. );
  1309. $this->Form->create('Contact');
  1310. $this->Form->hidden('Contact.id', array('value' => 1));
  1311. $this->Form->text('Contact.name');
  1312. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1313. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1314. $this->Form->unlockField('Contact.name');
  1315. $this->Form->unlockField('Contact.id');
  1316. $this->assertEquals(array(), $this->Form->fields);
  1317. }
  1318. /**
  1319. * testTagIsInvalid method
  1320. *
  1321. * @return void
  1322. */
  1323. public function testTagIsInvalid() {
  1324. $Contact = ClassRegistry::getObject('Contact');
  1325. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1326. $this->Form->setEntity('Contact.0.email');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = array('Please provide an email');
  1329. $this->assertEquals($expected, $result);
  1330. $this->Form->setEntity('Contact.1.email');
  1331. $result = $this->Form->tagIsInvalid();
  1332. $expected = false;
  1333. $this->assertSame($expected, $result);
  1334. $this->Form->setEntity('Contact.0.name');
  1335. $result = $this->Form->tagIsInvalid();
  1336. $expected = false;
  1337. $this->assertSame($expected, $result);
  1338. }
  1339. /**
  1340. * testPasswordValidation method
  1341. *
  1342. * test validation errors on password input.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testPasswordValidation() {
  1347. $Contact = ClassRegistry::getObject('Contact');
  1348. $Contact->validationErrors['password'] = array('Please provide a password');
  1349. $result = $this->Form->input('Contact.password');
  1350. $expected = array(
  1351. 'div' => array('class' => 'input password error'),
  1352. 'label' => array('for' => 'ContactPassword'),
  1353. 'Password',
  1354. '/label',
  1355. 'input' => array(
  1356. 'type' => 'password', 'name' => 'data[Contact][password]',
  1357. 'id' => 'ContactPassword', 'class' => 'form-error'
  1358. ),
  1359. array('div' => array('class' => 'error-message')),
  1360. 'Please provide a password',
  1361. '/div',
  1362. '/div'
  1363. );
  1364. $this->assertTags($result, $expected);
  1365. }
  1366. /**
  1367. * testEmptyErrorValidation method
  1368. *
  1369. * test validation error div when validation message is an empty string
  1370. *
  1371. * @access public
  1372. * @return void
  1373. */
  1374. public function testEmptyErrorValidation() {
  1375. $this->Form->validationErrors['Contact']['password'] = '';
  1376. $result = $this->Form->input('Contact.password');
  1377. $expected = array(
  1378. 'div' => array('class' => 'input password error'),
  1379. 'label' => array('for' => 'ContactPassword'),
  1380. 'Password',
  1381. '/label',
  1382. 'input' => array(
  1383. 'type' => 'password', 'name' => 'data[Contact][password]',
  1384. 'id' => 'ContactPassword', 'class' => 'form-error'
  1385. ),
  1386. array('div' => array('class' => 'error-message')),
  1387. array(),
  1388. '/div',
  1389. '/div'
  1390. );
  1391. $this->assertTags($result, $expected);
  1392. }
  1393. /**
  1394. * testEmptyInputErrorValidation method
  1395. *
  1396. * test validation error div when validation message is overridden by an empty string when calling input()
  1397. *
  1398. * @access public
  1399. * @return void
  1400. */
  1401. public function testEmptyInputErrorValidation() {
  1402. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1403. $result = $this->Form->input('Contact.password', array('error' => ''));
  1404. $expected = array(
  1405. 'div' => array('class' => 'input password error'),
  1406. 'label' => array('for' => 'ContactPassword'),
  1407. 'Password',
  1408. '/label',
  1409. 'input' => array(
  1410. 'type' => 'password', 'name' => 'data[Contact][password]',
  1411. 'id' => 'ContactPassword', 'class' => 'form-error'
  1412. ),
  1413. array('div' => array('class' => 'error-message')),
  1414. array(),
  1415. '/div',
  1416. '/div'
  1417. );
  1418. $this->assertTags($result, $expected);
  1419. }
  1420. /**
  1421. * testFormValidationAssociated method
  1422. *
  1423. * test display of form errors in conjunction with model::validates.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFormValidationAssociated() {
  1428. $this->UserForm = ClassRegistry::getObject('UserForm');
  1429. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1430. $data = array(
  1431. 'UserForm' => array('name' => 'user'),
  1432. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1433. );
  1434. $result = $this->UserForm->OpenidUrl->create($data);
  1435. $this->assertFalse(empty($result));
  1436. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1437. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1438. $encoding = strtolower(Configure::read('App.encoding'));
  1439. $expected = array(
  1440. 'form' => array(
  1441. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1442. 'accept-charset' => $encoding
  1443. ),
  1444. 'div' => array('style' => 'display:none;'),
  1445. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1446. '/div'
  1447. );
  1448. $this->assertTags($result, $expected);
  1449. $result = $this->Form->error(
  1450. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1451. );
  1452. $this->assertEquals('Error, not registered', $result);
  1453. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1454. }
  1455. /**
  1456. * testFormValidationAssociatedFirstLevel method
  1457. *
  1458. * test form error display with associated model.
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormValidationAssociatedFirstLevel() {
  1463. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1464. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1465. $data = array(
  1466. 'ValidateUser' => array('name' => 'mariano'),
  1467. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1468. );
  1469. $result = $this->ValidateUser->create($data);
  1470. $this->assertFalse(empty($result));
  1471. $this->assertFalse($this->ValidateUser->validates());
  1472. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1473. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1474. $encoding = strtolower(Configure::read('App.encoding'));
  1475. $expected = array(
  1476. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1477. 'div' => array('style' => 'display:none;'),
  1478. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1479. '/div'
  1480. );
  1481. $this->assertTags($result, $expected);
  1482. $result = $this->Form->error(
  1483. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1484. );
  1485. $this->assertEquals('Invalid email', $result);
  1486. $result = $this->Form->error(
  1487. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1488. );
  1489. $this->assertEquals('Invalid name', $result);
  1490. $result = $this->Form->error(
  1491. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1492. );
  1493. $this->assertEquals('Invalid city', $result);
  1494. unset($this->ValidateUser->ValidateProfile);
  1495. unset($this->ValidateUser);
  1496. }
  1497. /**
  1498. * testFormValidationAssociatedSecondLevel method
  1499. *
  1500. * test form error display with associated model.
  1501. *
  1502. * @return void
  1503. */
  1504. public function testFormValidationAssociatedSecondLevel() {
  1505. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1506. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1507. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1508. $data = array(
  1509. 'ValidateUser' => array('name' => 'mariano'),
  1510. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1511. 'ValidateItem' => array('name' => 'Item')
  1512. );
  1513. $result = $this->ValidateUser->create($data);
  1514. $this->assertFalse(empty($result));
  1515. $this->assertFalse($this->ValidateUser->validates());
  1516. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1517. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1518. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1519. $encoding = strtolower(Configure::read('App.encoding'));
  1520. $expected = array(
  1521. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1522. 'div' => array('style' => 'display:none;'),
  1523. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1524. '/div'
  1525. );
  1526. $this->assertTags($result, $expected);
  1527. $result = $this->Form->error(
  1528. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1529. );
  1530. $this->assertEquals('Invalid email', $result);
  1531. $result = $this->Form->error(
  1532. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1533. );
  1534. $this->assertEquals('Invalid name', $result);
  1535. $result = $this->Form->error(
  1536. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1537. );
  1538. $result = $this->Form->error(
  1539. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1540. );
  1541. $this->assertEquals('Invalid description', $result);
  1542. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1543. unset($this->ValidateUser->ValidateProfile);
  1544. unset($this->ValidateUser);
  1545. }
  1546. /**
  1547. * testFormValidationMultiRecord method
  1548. *
  1549. * test form error display with multiple records.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormValidationMultiRecord() {
  1554. $Contact = ClassRegistry::getObject('Contact');
  1555. $Contact->validationErrors[2] = array(
  1556. 'name' => array('This field cannot be left blank')
  1557. );
  1558. $result = $this->Form->input('Contact.2.name');
  1559. $expected = array(
  1560. 'div' => array('class' => 'input text error'),
  1561. 'label' => array('for' => 'Contact2Name'),
  1562. 'Name',
  1563. '/label',
  1564. 'input' => array(
  1565. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1566. 'class' => 'form-error', 'maxlength' => 255
  1567. ),
  1568. array('div' => array('class' => 'error-message')),
  1569. 'This field cannot be left blank',
  1570. '/div',
  1571. '/div'
  1572. );
  1573. $this->assertTags($result, $expected);
  1574. }
  1575. /**
  1576. * testMultipleInputValidation method
  1577. *
  1578. * test multiple record form validation error display.
  1579. *
  1580. * @return void
  1581. */
  1582. public function testMultipleInputValidation() {
  1583. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1584. $Address->validationErrors[0] = array(
  1585. 'title' => array('This field cannot be empty'),
  1586. 'first_name' => array('This field cannot be empty')
  1587. );
  1588. $Address->validationErrors[1] = array(
  1589. 'last_name' => array('You must have a last name')
  1590. );
  1591. $this->Form->create();
  1592. $result = $this->Form->input('Address.0.title');
  1593. $expected = array(
  1594. 'div' => array('…

Large files files are truncated, but you can click here to view the full file