RulesCheckerIntegrationTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\Entity;
  17. use Cake\ORM\RulesChecker;
  18. use Cake\ORM\TableRegistry;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests the integration between the ORM and the domain checker
  22. */
  23. class RulesCheckerIntegrationTest extends TestCase
  24. {
  25. /**
  26. * Fixtures to be loaded
  27. *
  28. * @var array
  29. */
  30. public $fixtures = ['core.articles', 'core.authors', 'core.tags', 'core.articles_tags'];
  31. /**
  32. * Tear down
  33. *
  34. * @return void
  35. */
  36. public function tearDown()
  37. {
  38. parent::tearDown();
  39. TableRegistry::clear();
  40. }
  41. /**
  42. * Tests saving belongsTo association and get a validation error
  43. *
  44. * @group save
  45. * @return void
  46. */
  47. public function testsSaveBelongsToWithValidationError()
  48. {
  49. $entity = new Entity([
  50. 'title' => 'A Title',
  51. 'body' => 'A body'
  52. ]);
  53. $entity->author = new Entity([
  54. 'name' => 'Jose'
  55. ]);
  56. $table = TableRegistry::get('articles');
  57. $table->belongsTo('authors');
  58. $table->association('authors')
  59. ->target()
  60. ->rulesChecker()
  61. ->add(function (Entity $author, array $options) use ($table) {
  62. $this->assertSame($options['repository'], $table->association('authors')->target());
  63. return false;
  64. }, ['errorField' => 'name', 'message' => 'This is an error']);
  65. $this->assertFalse($table->save($entity));
  66. $this->assertTrue($entity->isNew());
  67. $this->assertTrue($entity->author->isNew());
  68. $this->assertNull($entity->get('author_id'));
  69. $this->assertNotEmpty($entity->author->errors('name'));
  70. $this->assertEquals(['This is an error'], $entity->author->errors('name'));
  71. }
  72. /**
  73. * Tests saving hasOne association and returning a validation error will
  74. * abort the saving process
  75. *
  76. * @group save
  77. * @return void
  78. */
  79. public function testSaveHasOneWithValidationError()
  80. {
  81. $entity = new \Cake\ORM\Entity([
  82. 'name' => 'Jose'
  83. ]);
  84. $entity->article = new \Cake\ORM\Entity([
  85. 'title' => 'A Title',
  86. 'body' => 'A body'
  87. ]);
  88. $table = TableRegistry::get('authors');
  89. $table->hasOne('articles');
  90. $table->association('articles')
  91. ->target()
  92. ->rulesChecker()
  93. ->add(function (Entity $entity) {
  94. return false;
  95. }, ['errorField' => 'title', 'message' => 'This is an error']);
  96. $this->assertFalse($table->save($entity));
  97. $this->assertTrue($entity->isNew());
  98. $this->assertTrue($entity->article->isNew());
  99. $this->assertNull($entity->article->id);
  100. $this->assertNull($entity->article->get('author_id'));
  101. $this->assertFalse($entity->article->dirty('author_id'));
  102. $this->assertNotEmpty($entity->article->errors('title'));
  103. }
  104. /**
  105. * Tests saving multiple entities in a hasMany association and getting and
  106. * error while saving one of them. It should abort all the save operation
  107. * when options are set to defaults
  108. *
  109. * @return void
  110. */
  111. public function testSaveHasManyWithErrorsAtomic()
  112. {
  113. $entity = new \Cake\ORM\Entity([
  114. 'name' => 'Jose'
  115. ]);
  116. $entity->articles = [
  117. new \Cake\ORM\Entity([
  118. 'title' => '1',
  119. 'body' => 'A body'
  120. ]),
  121. new \Cake\ORM\Entity([
  122. 'title' => 'Another Title',
  123. 'body' => 'Another body'
  124. ])
  125. ];
  126. $table = TableRegistry::get('authors');
  127. $table->hasMany('articles');
  128. $table->association('articles')
  129. ->target()
  130. ->rulesChecker()
  131. ->add(function (Entity $entity, $options) use ($table) {
  132. $this->assertSame($table, $options['_sourceTable']);
  133. return $entity->title === '1';
  134. }, ['errorField' => 'title', 'message' => 'This is an error']);
  135. $this->assertFalse($table->save($entity));
  136. $this->assertTrue($entity->isNew());
  137. $this->assertTrue($entity->articles[0]->isNew());
  138. $this->assertTrue($entity->articles[1]->isNew());
  139. $this->assertNull($entity->articles[0]->id);
  140. $this->assertNull($entity->articles[1]->id);
  141. $this->assertNull($entity->articles[0]->author_id);
  142. $this->assertNull($entity->articles[1]->author_id);
  143. $this->assertEmpty($entity->articles[0]->errors());
  144. $this->assertNotEmpty($entity->articles[1]->errors());
  145. }
  146. /**
  147. * Tests that it is possible to continue saving hasMany associations
  148. * even if any of the records fail validation when atomic is set
  149. * to false
  150. *
  151. * @return void
  152. */
  153. public function testSaveHasManyWithErrorsNonAtomic()
  154. {
  155. $entity = new \Cake\ORM\Entity([
  156. 'name' => 'Jose'
  157. ]);
  158. $entity->articles = [
  159. new \Cake\ORM\Entity([
  160. 'title' => 'A title',
  161. 'body' => 'A body'
  162. ]),
  163. new \Cake\ORM\Entity([
  164. 'title' => '1',
  165. 'body' => 'Another body'
  166. ])
  167. ];
  168. $table = TableRegistry::get('authors');
  169. $table->hasMany('articles');
  170. $table->association('articles')
  171. ->target()
  172. ->rulesChecker()
  173. ->add(function (Entity $article) {
  174. return is_numeric($article->title);
  175. }, ['errorField' => 'title', 'message' => 'This is an error']);
  176. $result = $table->save($entity, ['atomic' => false]);
  177. $this->assertSame($entity, $result);
  178. $this->assertFalse($entity->isNew());
  179. $this->assertTrue($entity->articles[0]->isNew());
  180. $this->assertFalse($entity->articles[1]->isNew());
  181. $this->assertEquals(4, $entity->articles[1]->id);
  182. $this->assertNull($entity->articles[0]->id);
  183. $this->assertNotEmpty($entity->articles[0]->errors('title'));
  184. }
  185. /**
  186. * Tests saving belongsToMany records with a validation error in a joint entity
  187. *
  188. * @group save
  189. * @return void
  190. */
  191. public function testSaveBelongsToManyWithValidationErrorInJointEntity()
  192. {
  193. $entity = new \Cake\ORM\Entity([
  194. 'title' => 'A Title',
  195. 'body' => 'A body'
  196. ]);
  197. $entity->tags = [
  198. new \Cake\ORM\Entity([
  199. 'name' => 'Something New'
  200. ]),
  201. new \Cake\ORM\Entity([
  202. 'name' => '100'
  203. ])
  204. ];
  205. $table = TableRegistry::get('articles');
  206. $table->belongsToMany('tags');
  207. $table->association('tags')
  208. ->junction()
  209. ->rulesChecker()
  210. ->add(function (Entity $entity) {
  211. return $entity->article_id > 4;
  212. });
  213. $this->assertFalse($table->save($entity));
  214. $this->assertTrue($entity->isNew());
  215. $this->assertTrue($entity->tags[0]->isNew());
  216. $this->assertTrue($entity->tags[1]->isNew());
  217. $this->assertNull($entity->tags[0]->id);
  218. $this->assertNull($entity->tags[1]->id);
  219. $this->assertNull($entity->tags[0]->_joinData);
  220. $this->assertNull($entity->tags[1]->_joinData);
  221. }
  222. /**
  223. * Tests saving belongsToMany records with a validation error in a joint entity
  224. * and atomic set to false
  225. *
  226. * @group save
  227. * @return void
  228. */
  229. public function testSaveBelongsToManyWithValidationErrorInJointEntityNonAtomic()
  230. {
  231. $entity = new \Cake\ORM\Entity([
  232. 'title' => 'A Title',
  233. 'body' => 'A body'
  234. ]);
  235. $entity->tags = [
  236. new \Cake\ORM\Entity([
  237. 'name' => 'Something New'
  238. ]),
  239. new \Cake\ORM\Entity([
  240. 'name' => 'New one'
  241. ])
  242. ];
  243. $table = TableRegistry::get('articles');
  244. $table->belongsToMany('tags');
  245. $table->association('tags')
  246. ->junction()
  247. ->rulesChecker()
  248. ->add(function (Entity $entity) {
  249. return $entity->tag_id > 4;
  250. });
  251. $this->assertSame($entity, $table->save($entity, ['atomic' => false]));
  252. $this->assertFalse($entity->isNew());
  253. $this->assertFalse($entity->tags[0]->isNew());
  254. $this->assertFalse($entity->tags[1]->isNew());
  255. $this->assertEquals(4, $entity->tags[0]->id);
  256. $this->assertEquals(5, $entity->tags[1]->id);
  257. $this->assertTrue($entity->tags[0]->_joinData->isNew());
  258. $this->assertEquals(4, $entity->tags[1]->_joinData->article_id);
  259. $this->assertEquals(5, $entity->tags[1]->_joinData->tag_id);
  260. }
  261. /**
  262. * Tests the isUnique domain rule
  263. *
  264. * @group save
  265. * @return void
  266. */
  267. public function testIsUniqueDomainRule()
  268. {
  269. $entity = new Entity([
  270. 'name' => 'larry'
  271. ]);
  272. $table = TableRegistry::get('Authors');
  273. $rules = $table->rulesChecker();
  274. $rules->add($rules->isUnique(['name']));
  275. $this->assertFalse($table->save($entity));
  276. $this->assertEquals(['This value is already in use'], $entity->errors('name'));
  277. $entity->name = 'jose';
  278. $this->assertSame($entity, $table->save($entity));
  279. $entity = $table->get(1);
  280. $entity->dirty('name', true);
  281. $this->assertSame($entity, $table->save($entity));
  282. }
  283. /**
  284. * Tests isUnique with multiple fields
  285. *
  286. * @group save
  287. * @return void
  288. */
  289. public function testIsUniqueMultipleFields()
  290. {
  291. $entity = new Entity([
  292. 'author_id' => 1,
  293. 'title' => 'First Article'
  294. ]);
  295. $table = TableRegistry::get('Articles');
  296. $rules = $table->rulesChecker();
  297. $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
  298. $this->assertFalse($table->save($entity));
  299. $this->assertEquals(['title' => ['Nope']], $entity->errors());
  300. $entity->clean();
  301. $entity->author_id = 2;
  302. $this->assertSame($entity, $table->save($entity));
  303. }
  304. /**
  305. * Tests the existsIn domain rule
  306. *
  307. * @group save
  308. * @return void
  309. */
  310. public function testExistsInDomainRule()
  311. {
  312. $entity = new Entity([
  313. 'title' => 'An Article',
  314. 'author_id' => 500
  315. ]);
  316. $table = TableRegistry::get('Articles');
  317. $table->belongsTo('Authors');
  318. $rules = $table->rulesChecker();
  319. $rules->add($rules->existsIn('author_id', 'Authors'));
  320. $this->assertFalse($table->save($entity));
  321. $this->assertEquals(['This value does not exist'], $entity->errors('author_id'));
  322. }
  323. /**
  324. * Tests the existsIn domain rule when passing an object
  325. *
  326. * @group save
  327. * @return void
  328. */
  329. public function testExistsInDomainRuleWithObject()
  330. {
  331. $entity = new Entity([
  332. 'title' => 'An Article',
  333. 'author_id' => 500
  334. ]);
  335. $table = TableRegistry::get('Articles');
  336. $rules = $table->rulesChecker();
  337. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  338. $this->assertFalse($table->save($entity));
  339. $this->assertEquals(['Nope'], $entity->errors('author_id'));
  340. }
  341. /**
  342. * Tests the checkRules save option
  343. *
  344. * @group save
  345. * @return void
  346. */
  347. public function testSkipRulesChecking()
  348. {
  349. $entity = new Entity([
  350. 'title' => 'An Article',
  351. 'author_id' => 500
  352. ]);
  353. $table = TableRegistry::get('Articles');
  354. $rules = $table->rulesChecker();
  355. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  356. $this->assertSame($entity, $table->save($entity, ['checkRules' => false]));
  357. }
  358. /**
  359. * Tests the beforeRules event
  360. *
  361. * @group save
  362. * @return void
  363. */
  364. public function testUseBeforeRules()
  365. {
  366. $entity = new Entity([
  367. 'title' => 'An Article',
  368. 'author_id' => 500
  369. ]);
  370. $table = TableRegistry::get('Articles');
  371. $rules = $table->rulesChecker();
  372. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  373. $table->eventManager()->attach(
  374. function ($event, Entity $entity, \ArrayObject $options, $operation) {
  375. $this->assertEquals(
  376. ['atomic' => true, 'associated' => true, 'checkRules' => true],
  377. $options->getArrayCopy()
  378. );
  379. $this->assertEquals('create', $operation);
  380. $event->stopPropagation();
  381. return true;
  382. },
  383. 'Model.beforeRules'
  384. );
  385. $this->assertSame($entity, $table->save($entity));
  386. }
  387. /**
  388. * Tests the afterRules event
  389. *
  390. * @group save
  391. * @return void
  392. */
  393. public function testUseAfterRules()
  394. {
  395. $entity = new Entity([
  396. 'title' => 'An Article',
  397. 'author_id' => 500
  398. ]);
  399. $table = TableRegistry::get('Articles');
  400. $rules = $table->rulesChecker();
  401. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  402. $table->eventManager()->attach(
  403. function ($event, Entity $entity, \ArrayObject $options, $result, $operation) {
  404. $this->assertEquals(
  405. ['atomic' => true, 'associated' => true, 'checkRules' => true],
  406. $options->getArrayCopy()
  407. );
  408. $this->assertEquals('create', $operation);
  409. $this->assertFalse($result);
  410. $event->stopPropagation();
  411. return true;
  412. },
  413. 'Model.afterRules'
  414. );
  415. $this->assertSame($entity, $table->save($entity));
  416. }
  417. /**
  418. * Tests that rules can be changed using the buildRules event
  419. *
  420. * @group save
  421. * @return void
  422. */
  423. public function testUseBuildRulesEvent()
  424. {
  425. $entity = new Entity([
  426. 'title' => 'An Article',
  427. 'author_id' => 500
  428. ]);
  429. $table = TableRegistry::get('Articles');
  430. $table->eventManager()->attach(function ($event, $rules) {
  431. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  432. }, 'Model.buildRules');
  433. $this->assertFalse($table->save($entity));
  434. }
  435. /**
  436. * Tests isUnique with untouched fields
  437. *
  438. * @group save
  439. * @return void
  440. */
  441. public function testIsUniqueWithCleanFields()
  442. {
  443. $table = TableRegistry::get('Articles');
  444. $entity = $table->get(1);
  445. $rules = $table->rulesChecker();
  446. $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
  447. $entity->body = 'Foo';
  448. $this->assertSame($entity, $table->save($entity));
  449. $entity->title = 'Third Article';
  450. $this->assertFalse($table->save($entity));
  451. }
  452. /**
  453. * Tests the existsIn rule when passing non dirty fields
  454. *
  455. * @group save
  456. * @return void
  457. */
  458. public function testExistsInWithCleanFields()
  459. {
  460. $table = TableRegistry::get('Articles');
  461. $table->belongsTo('Authors');
  462. $rules = $table->rulesChecker();
  463. $rules->add($rules->existsIn('author_id', 'Authors'));
  464. $entity = $table->get(1);
  465. $entity->title = 'Foo';
  466. $entity->author_id = 1000;
  467. $entity->dirty('author_id', false);
  468. $this->assertSame($entity, $table->save($entity));
  469. }
  470. /**
  471. * Tests using rules to prevent delete operations
  472. *
  473. * @group delete
  474. * @return void
  475. */
  476. public function testDeleteRules()
  477. {
  478. $table = TableRegistry::get('Articles');
  479. $rules = $table->rulesChecker();
  480. $rules->addDelete(function ($entity) {
  481. return false;
  482. });
  483. $entity = $table->get(1);
  484. $this->assertFalse($table->delete($entity));
  485. }
  486. /**
  487. * Checks that it is possible to pass custom options to rules when saving
  488. *
  489. * @group save
  490. * @return void
  491. */
  492. public function testCustomOptionsPassingSave()
  493. {
  494. $entity = new Entity([
  495. 'name' => 'jose'
  496. ]);
  497. $table = TableRegistry::get('Authors');
  498. $rules = $table->rulesChecker();
  499. $rules->add(function ($entity, $options) {
  500. $this->assertEquals('bar', $options['foo']);
  501. $this->assertEquals('option', $options['another']);
  502. return false;
  503. }, ['another' => 'option']);
  504. $this->assertFalse($table->save($entity, ['foo' => 'bar']));
  505. }
  506. /**
  507. * Tests passing custom options to rules from delete
  508. *
  509. * @group delete
  510. * @return void
  511. */
  512. public function testCustomOptionsPassingDelete()
  513. {
  514. $table = TableRegistry::get('Articles');
  515. $rules = $table->rulesChecker();
  516. $rules->addDelete(function ($entity, $options) {
  517. $this->assertEquals('bar', $options['foo']);
  518. $this->assertEquals('option', $options['another']);
  519. return false;
  520. }, ['another' => 'option']);
  521. $entity = $table->get(1);
  522. $this->assertFalse($table->delete($entity, ['foo' => 'bar']));
  523. }
  524. /**
  525. * Tests that using existsIn for a hasMany association will not be called
  526. * as the foreign key for the association was automatically validated already.
  527. *
  528. * @group save
  529. * @return void
  530. */
  531. public function testAvoidExistsInOnAutomaticSaving()
  532. {
  533. $entity = new \Cake\ORM\Entity([
  534. 'name' => 'Jose'
  535. ]);
  536. $entity->articles = [
  537. new \Cake\ORM\Entity([
  538. 'title' => '1',
  539. 'body' => 'A body'
  540. ]),
  541. new \Cake\ORM\Entity([
  542. 'title' => 'Another Title',
  543. 'body' => 'Another body'
  544. ])
  545. ];
  546. $table = TableRegistry::get('authors');
  547. $table->hasMany('articles');
  548. $table->association('articles')->belongsTo('authors');
  549. $checker = $table->association('articles')->target()->rulesChecker();
  550. $checker->add(function ($entity, $options) use ($checker) {
  551. $rule = $checker->existsIn('author_id', 'authors');
  552. $id = $entity->author_id;
  553. $entity->author_id = 5000;
  554. $result = $rule($entity, $options);
  555. $this->assertTrue($result);
  556. $entity->author_id = $id;
  557. return true;
  558. });
  559. $this->assertSame($entity, $table->save($entity));
  560. }
  561. }