PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

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

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