/Core/Libs/Test/Case/Model/Behavior/ValidationBehaviorTest.php

http://github.com/infinitas/infinitas · PHP · 653 lines · 497 code · 66 blank · 90 comment · 2 complexity · 770d60fe47c99142a61ab9164bc7b287 MD5 · raw file

  1. <?php
  2. App::import('Behavior', 'libs.Validation');
  3. InfinitasPlugin::load('Users');
  4. class ValidationBehaviorTestCase extends CakeTestCase {
  5. /**
  6. * fixtures
  7. *
  8. * @var array
  9. */
  10. public $fixtures = array(
  11. 'plugin.users.user',
  12. 'plugin.management.ticket',
  13. 'plugin.users.group',
  14. 'plugin.installer.plugin',
  15. );
  16. /**
  17. * @brief set up at the start
  18. */
  19. public function setUp() {
  20. parent::setUp();
  21. $this->User = ClassRegistry::init('Users.User');
  22. $this->User->Behaviors->attach('Libs.Validation');
  23. }
  24. /**
  25. * @brief break down at the end
  26. */
  27. public function tearDown() {
  28. parent::tearDown();
  29. unset($this->Infinitas);
  30. }
  31. /**
  32. * check validation is attached
  33. */
  34. public function testValidationAttached() {
  35. $this->assertTrue($this->User->Behaviors->attached('Validation'));
  36. }
  37. /**
  38. * test json validation
  39. */
  40. public function testValidateJson() {
  41. $this->User->validate = array(
  42. 'field_1' => array(
  43. 'validateJson' => array(
  44. 'rule' => 'validateJson',
  45. 'message' => 'thats not json'
  46. )
  47. )
  48. );
  49. $expected = array('field_1' => array('thats not json'));
  50. $data['User']['field_1'] = 'foo';
  51. $this->User->set($data); $this->User->validates();
  52. $this->assertEqual($this->User->validationErrors, $expected);
  53. $data['User']['field_1'] = '';
  54. $this->User->set($data); $this->User->validates();
  55. $this->assertEqual($this->User->validationErrors, $expected);
  56. $data['User']['field_1'] = json_encode(array('asd' => 'meh'));
  57. $this->User->set($data); $this->User->validates();
  58. $this->assertEqual($this->User->validationErrors, array());
  59. }
  60. /**
  61. * test either or
  62. *
  63. * @dataProvider eitherOrData
  64. */
  65. public function testValidateEitherOr($data, $expected) {
  66. if (empty($data['data'])) {
  67. $data['data'] = $data;
  68. }
  69. if (empty($data['validate'])) {
  70. $data['validate'] = array(
  71. 'field_1' => array(
  72. 'validateEitherOr' => array(
  73. 'allowEmpty' => true,
  74. 'rule' => array('validateEitherOr', array('field_1', 'field_2')),
  75. 'message' => 'pick one, only one'
  76. )
  77. )
  78. );
  79. }
  80. $this->User->validate = $data['validate'];
  81. $this->User->set($data['data']);
  82. $this->User->validates();
  83. $this->assertEqual($this->User->validationErrors, $expected);
  84. }
  85. /**
  86. * tests is url or absolute path
  87. */
  88. public function testValidateUrlOrAbsolute() {
  89. $this->User->validate = array(
  90. 'url' => array(
  91. 'validateUrlOrAbsolute' => array(
  92. 'rule' => 'validateUrlOrAbsolute',
  93. 'message' => 'not url or absolute path'
  94. )
  95. )
  96. );
  97. $expected = array('url' => array('not url or absolute path'));
  98. /**
  99. * invalid
  100. */
  101. $data['User']['url'] = 'this/is/not/an/absolute.path';
  102. $this->User->set($data); $this->User->validates();
  103. $this->assertEqual($this->User->validationErrors, $expected);
  104. $data['User']['url'] = 'http:/this.is/an/url.ext';
  105. $this->User->set($data); $this->User->validates();
  106. $this->assertEqual($this->User->validationErrors, $expected);
  107. /**
  108. * valid
  109. */
  110. $data['User']['url'] = '/this/is/an/absolute.path';
  111. $this->User->set($data); $this->User->validates();
  112. $this->assertEqual($this->User->validationErrors, array());
  113. $data['User']['url'] = 'http://this.is/an/url.ext';
  114. $this->User->set($data); $this->User->validates();
  115. $this->assertEqual($this->User->validationErrors, array());
  116. $data['User']['url'] = 'ftp://this.is/an/url.ext';
  117. $this->User->set($data); $this->User->validates();
  118. $this->assertEqual($this->User->validationErrors, array());
  119. }
  120. /**
  121. * tests comparing passwords
  122. */
  123. public function testValidateComparePasswords() {
  124. $this->User->validate = array(
  125. 'password' => array(
  126. 'validateCompareFields' => array(
  127. 'rule' => array('validateCompareFields', array('password', 'password_match')),
  128. 'message' => 'fields dont match'
  129. )
  130. )
  131. );
  132. $data['User']['password'] = 'abc';
  133. $data['User']['password_match'] = 'xyz';
  134. $this->User->set($data); $this->User->validates();
  135. $expected = array('password' => array('fields dont match'));
  136. $this->assertEqual($this->User->validationErrors, $expected);
  137. $data['User']['password'] = Security::hash('abc', null, true);
  138. $this->User->set($data); $this->User->validates();
  139. $this->assertEqual($this->User->validationErrors, $expected);
  140. }
  141. /**
  142. * test comparing normal fields
  143. */
  144. public function testCompareFields() {
  145. $this->User->validate = array(
  146. 'field_1' => array(
  147. 'validateCompareFields' => array(
  148. 'rule' => array('validateCompareFields', array('field_1', 'field_2')),
  149. 'message' => 'fields dont match'
  150. )
  151. )
  152. );
  153. $data['User']['field_1'] = 'abc';
  154. $data['User']['field_2'] = 'xyz';
  155. $this->User->set($data); $this->User->validates();
  156. $expected = array('field_1' => array('fields dont match'));
  157. $this->assertEqual($this->User->validationErrors, $expected);
  158. $data['User']['field_2'] = 'abc';
  159. $this->User->set($data); $this->User->validates();
  160. $this->assertEqual($this->User->validationErrors, array());
  161. }
  162. /**
  163. * check a related record exists
  164. */
  165. public function testValidateRecordExists() {
  166. //Add validation rule for the user record
  167. $this->User->validate['group_id'] = array(
  168. 'rule' => 'validateRecordExists',
  169. 'message' => 'Invalid group',
  170. 'required' => true
  171. );
  172. // Test for an non existing group
  173. $data = array(
  174. 'User' => array(
  175. 'username' => 'test-user',
  176. 'group_id' => 'non-existing-group-id'
  177. )
  178. );
  179. $this->User->set($data);
  180. $this->assertFalse($this->User->validates());
  181. $expected = array('group_id' => array('Invalid group'));
  182. $this->assertEqual($this->User->validationErrors, $expected);
  183. //Test for an existing group
  184. $data['User']['group_id'] = 1;
  185. $this->User->set($data);
  186. $this->assertTrue($this->User->validates());
  187. $expected = array();
  188. $this->assertEqual($this->User->validationErrors, $expected);
  189. }
  190. /**
  191. * test validation
  192. *
  193. * @dataProvider pluginValidationData
  194. */
  195. public function testValidatePluginExists($data, $expected) {
  196. $this->User->validate = array(
  197. 'plugin' => array(
  198. 'rule' => 'validatePluginExists',
  199. 'message' => 'plugin does not exist',
  200. 'required' => true
  201. )
  202. );
  203. $this->User->set($data); $this->User->validates();
  204. $result = $this->User->validationErrors;
  205. $this->assertEquals($expected, $result);
  206. }
  207. /**
  208. * pluginValidationData data provider
  209. */
  210. public function pluginValidationData() {
  211. return array(
  212. array(
  213. array('plugin' => 'foo'),
  214. array('plugin' => array('plugin does not exist'))
  215. ),
  216. array(
  217. array('plugin' => ''),
  218. array('plugin' => array('plugin does not exist'))
  219. ),
  220. array(
  221. array('plugin' => 123),
  222. array('plugin' => array('plugin does not exist'))
  223. ),
  224. array(
  225. array('plugin' => 'Users'),
  226. array()
  227. )
  228. );
  229. }
  230. /**
  231. * test validate model exists
  232. *
  233. * @dataProvider validateModelExists
  234. */
  235. public function testValidateModelExists($data, $expected) {
  236. CakePlugin::load('Menus');
  237. $this->User->validate = array(
  238. 'model' => array(
  239. 'rule' => 'validateModelExists',
  240. 'message' => 'model does not exist',
  241. 'required' => true
  242. )
  243. );
  244. $this->User->set($data); $this->User->validates();
  245. $result = $this->User->validationErrors;
  246. $this->assertEquals($expected, $result);
  247. }
  248. public function validateModelExists() {
  249. return array(
  250. 'fake' => array(
  251. array('model' => 'Fake.Fake'),
  252. array('model' => array('model does not exist'))
  253. ),
  254. 'fake model' => array(
  255. array('model' => 'Users.Fake'),
  256. array('model' => array('model does not exist'))
  257. ),
  258. 'good' => array(
  259. array('model' => 'Users.User'),
  260. array()
  261. )
  262. );
  263. }
  264. /**
  265. * test validation
  266. */
  267. public function testValidatePluginExistsAdvanced() {
  268. $this->User->validate = array(
  269. 'plugin' => array(
  270. 'validatePluginExists' => array(
  271. 'allowEmpty' => true,
  272. 'rule' => 'validatePluginExists',
  273. 'message' => 'plugin does not exist'
  274. )
  275. )
  276. );
  277. $data = array(
  278. 'plugin' => 'fake'
  279. );
  280. $this->User->create($data); $this->User->validates();
  281. $expected = array('plugin' => array('plugin does not exist'));
  282. $this->assertEquals($expected, $this->User->validationErrors);
  283. $data = array(
  284. 'plugin' => 'DebugKit'
  285. );
  286. $this->User->create($data); $this->User->validates();
  287. $expected = array();
  288. $this->assertEquals($expected, $this->User->validationErrors);
  289. $this->User->validate = array(
  290. 'plugin' => array(
  291. 'validatePluginExists' => array(
  292. 'allowEmpty' => true,
  293. 'rule' => array('validatePluginExists', array('pluginType' => 'installed')),
  294. 'message' => 'plugin does not exist'
  295. )
  296. )
  297. );
  298. $data = array(
  299. 'plugin' => 'DebugKit'
  300. );
  301. $this->User->create($data); $this->User->validates();
  302. $expected = array();
  303. $this->assertEquals($expected, $this->User->validationErrors);
  304. $this->User->validate = array(
  305. 'plugin' => array(
  306. 'validatePluginExists' => array(
  307. 'allowEmpty' => true,
  308. 'rule' => array('validatePluginExists', array('pluginType' => 'core')),
  309. 'message' => 'plugin does not exist'
  310. )
  311. )
  312. );
  313. $data = array(
  314. 'plugin' => 'DebugKit'
  315. );
  316. $this->User->create($data); $this->User->validates();
  317. $expected = array('plugin' => array('plugin does not exist'));
  318. $this->assertEquals($expected, $this->User->validationErrors);
  319. $this->User->validate = array(
  320. 'plugin' => array(
  321. 'validatePluginExists' => array(
  322. 'allowEmpty' => true,
  323. 'rule' => array('validatePluginExists', array('pluginType' => 'core')),
  324. 'message' => 'plugin does not exist'
  325. )
  326. )
  327. );
  328. $data = array(
  329. 'plugin' => 'Menus'
  330. );
  331. $this->User->create($data); $this->User->validates();
  332. $expected = array();
  333. $this->assertEquals($expected, $this->User->validationErrors);
  334. }
  335. /**
  336. * test a controller exists within the selected plugin
  337. *
  338. * @dataProvider controllerValidationData
  339. */
  340. public function testValidateControllerExists($data, $expected) {
  341. $this->User->validate = array(
  342. 'controller' => array(
  343. 'validateControllerExists' => array(
  344. 'rule' => array('ValidateControllerExists', array('pluginField' => 'plugin')),
  345. 'message' => 'Invalid controller'
  346. )
  347. )
  348. );
  349. $this->User->create($data);
  350. $this->User->validates();
  351. $this->assertEquals($expected, $this->User->validationErrors);
  352. }
  353. /**
  354. * test a controller exists within hardcoded plugin
  355. *
  356. * @dataProvider controllerValidationAdvancedData
  357. */
  358. public function testValidateControllerAdvancedExists($data, $expected) {
  359. $this->User->validate = array(
  360. 'controller' => array(
  361. 'validateControllerExists' => array(
  362. 'rule' => array('ValidateControllerExists', array('setPlugin' => 'Users')),
  363. 'message' => 'Invalid controller'
  364. )
  365. )
  366. );
  367. $this->User->create($data);
  368. $this->User->validates();
  369. $this->assertEquals($expected, $this->User->validationErrors);
  370. }
  371. /**
  372. * test a controller exists within hardcoded plugin
  373. *
  374. * @dataProvider actionValidationData
  375. */
  376. public function testValidateActionExists($data, $expected) {
  377. $this->User->validate = array(
  378. 'action' => array(
  379. 'validateActionExists' => array(
  380. 'rule' => 'validateActionExists',
  381. 'message' => 'Invalid action'
  382. )
  383. )
  384. );
  385. $this->User->create($data);
  386. $this->User->validates();
  387. $this->assertEquals($expected, $this->User->validationErrors);
  388. $this->User->validate = array(
  389. 'action' => array(
  390. 'validateActionExists' => array(
  391. 'rule' => array(
  392. 'validateActionExists',
  393. 'pluginField' => 'User.plugin',
  394. 'controllerField' => 'User.controller',
  395. ),
  396. 'message' => 'Invalid action'
  397. )
  398. )
  399. );
  400. $this->User->create($data);
  401. $this->User->validates();
  402. $this->assertEquals($expected, $this->User->validationErrors);
  403. }
  404. public function testValidateActionExistsBadForm() {
  405. $this->User->validate = array(
  406. 'action' => array(
  407. 'validateActionExists' => array(
  408. 'rule' => array(
  409. 'validateActionExists',
  410. 'pluginField' => '.plugin',
  411. 'controllerField' => 'User.controller',
  412. ),
  413. 'message' => 'Invalid action'
  414. )
  415. )
  416. );
  417. $data = array(
  418. 'plugin' => 'users',
  419. 'controller' => 'users_controller',
  420. 'action' => 'register'
  421. );
  422. $this->User->create($data);
  423. $this->assertTrue($this->User->validates());
  424. $this->User->validate = array(
  425. 'action' => array(
  426. 'validateActionExists' => array(
  427. 'rule' => array(
  428. 'validateActionExists',
  429. 'pluginField' => 'User.plugin',
  430. 'controllerField' => '.controller',
  431. ),
  432. 'message' => 'Invalid action'
  433. )
  434. )
  435. );
  436. $data = array(
  437. 'plugin' => 'users',
  438. 'controller' => 'users_controller',
  439. 'action' => 'register'
  440. );
  441. $this->User->create($data);
  442. $this->assertTrue($this->User->validates());
  443. }
  444. /**
  445. * actionValidationData data provider
  446. */
  447. public function actionValidationData() {
  448. return array(
  449. array(
  450. array('plugin' => 'false-plugin', 'controller' => 'false-controller', 'action' => 'fake'),
  451. array('action' => array('Invalid action'))),
  452. array(
  453. array('plugin' => 'Users', 'controller' => 'false-controller', 'action' => 'fake'),
  454. array('action' => array('Invalid action'))),
  455. array(
  456. array('plugin' => 'Users', 'controller' => 'UsersController', 'action' => 'fake'),
  457. array('action' => array('Invalid action'))),
  458. array(
  459. array('plugin' => 'Users', 'controller' => 'UsersController', 'action' => 'register'),
  460. array()),
  461. array(
  462. array('plugin' => 'Users', 'controller' => 'UsersController', 'action' => 'admin_add'),
  463. array()),
  464. array(
  465. array('plugin' => 'fake-plugin', 'controller' => 'UsersController', 'action' => 'add'),
  466. array('action' => array('Invalid action'))),
  467. array(
  468. array('plugin' => 'fake-plugin', 'controller' => 'fake-controller', 'action' => 'add'),
  469. array('action' => array('Invalid action'))),
  470. array(
  471. array('plugin' => 'users', 'controller' => 'users_controller', 'action' => 'register'),
  472. array()),
  473. array(
  474. array('plugin' => 'Users', 'controller' => 'UsersController', 'action' => '_getUserData'),
  475. array('action' => array('Invalid action'))),
  476. );
  477. }
  478. /**
  479. * testValidateEitherOr data provider
  480. */
  481. public function eitherOrData() {
  482. $validation = array(
  483. array(
  484. 'field_1' => array(
  485. 'validateEitherOr' => array(
  486. 'allowEmpty' => true,
  487. 'rule' => array('validateEitherOr', array('field_1', 'field_2')),
  488. 'message' => 'pick one, only one'
  489. )
  490. ),
  491. 'field_2' => array(
  492. 'validateEitherOr' => array(
  493. 'allowEmpty' => true,
  494. 'rule' => array('validateEitherOr', array('field_1', 'field_2')),
  495. 'message' => 'cant pick both'
  496. )
  497. )
  498. ),
  499. array(
  500. 'field_2' => array(
  501. 'validateEitherOr' => array(
  502. 'allowEmpty' => true,
  503. 'rule' => array('validateEitherOr', array('field_1', 'field_2')),
  504. 'message' => 'cant pick both'
  505. )
  506. )
  507. )
  508. );
  509. return array(
  510. // test with first field having rules
  511. array(
  512. array('field_1' => '', 'field_2' => ''),
  513. array()),
  514. array(
  515. array('field_1' => 'foo', 'field_2' => 'bar'),
  516. array('field_1' => array('pick one, only one'))),
  517. array(
  518. array('field_1' => 'foo', 'field_2' => ''),
  519. array()),
  520. array(
  521. array('field_1' => '', 'field_2' => 'foo'),
  522. array()),
  523. // test with second field having rules
  524. array(
  525. array('validate' => $validation[1], 'data' => array('field_1' => 'asdf', 'field_2' => 'asdf')),
  526. array('field_2' => array('cant pick both'))),
  527. // test with both fields having rules
  528. array(
  529. array('validate' => $validation[0], 'data' => array('field_1' => '', 'field_2' => '')),
  530. array()),
  531. array(
  532. array('validate' => $validation[0], 'data' => array('field_1' => 'foo', 'field_2' => 'bar')),
  533. array('field_1' => array('pick one, only one'), 'field_2' => array('cant pick both'))),
  534. array(
  535. array('validate' => $validation[0], 'data' => array('field_1' => 'foo', 'field_2' => '')),
  536. array()),
  537. array(
  538. array('validate' => $validation[0], 'data' => array('field_1' => '', 'field_2' => 'bar')),
  539. array()),
  540. );
  541. }
  542. /**
  543. * controllerValidationData data provider
  544. * @return type
  545. */
  546. public function controllerValidationData() {
  547. return array(
  548. array(
  549. array('plugin' => 'false-plugin', 'controller' => 'false-controller'),
  550. array('controller' => array('Invalid controller'))),
  551. array(
  552. array('plugin' => 'Users', 'controller' => 'false-controller'),
  553. array('controller' => array('Invalid controller'))),
  554. array(
  555. array('plugin' => 'Users', 'controller' => 'UsersController'),
  556. array()),
  557. array(
  558. array('plugin' => 'fake-plugin', 'controller' => 'UsersController'),
  559. array('controller' => array('Invalid controller'))),
  560. array(
  561. array('plugin' => 'users', 'controller' => 'UsersController'),
  562. array())
  563. );
  564. }
  565. /**
  566. * controllerValidationAdvancedData data provider
  567. */
  568. public function controllerValidationAdvancedData() {
  569. return array(
  570. array(
  571. array('plugin' => 'false-plugin', 'controller' => 'false-controller'),
  572. array('controller' => array('Invalid controller'))),
  573. array(
  574. array('plugin' => 'Users', 'controller' => 'false-controller'),
  575. array('controller' => array('Invalid controller'))),
  576. array(
  577. array('plugin' => 'Users', 'controller' => 'UsersController'),
  578. array()),
  579. array( // hard coded plugin in the validation
  580. array('plugin' => 'fake-plugin', 'controller' => 'UsersController'),
  581. array()),
  582. array(
  583. array('plugin' => 'users', 'controller' => 'UsersController'),
  584. array()),
  585. );
  586. }
  587. }