RulesCheckerIntegrationTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. * Test adding rule with name
  263. *
  264. * @group save
  265. * @return void
  266. */
  267. public function testAddingRuleWithName()
  268. {
  269. $entity = new Entity([
  270. 'name' => 'larry'
  271. ]);
  272. $table = TableRegistry::get('Authors');
  273. $rules = $table->rulesChecker();
  274. $rules->add(
  275. function () {
  276. return false;
  277. },
  278. 'ruleName',
  279. ['errorField' => 'name']
  280. );
  281. $this->assertFalse($table->save($entity));
  282. $this->assertEquals(['ruleName' => 'invalid'], $entity->errors('name'));
  283. }
  284. /**
  285. * Tests the isUnique domain rule
  286. *
  287. * @group save
  288. * @return void
  289. */
  290. public function testIsUniqueDomainRule()
  291. {
  292. $entity = new Entity([
  293. 'name' => 'larry'
  294. ]);
  295. $table = TableRegistry::get('Authors');
  296. $rules = $table->rulesChecker();
  297. $rules->add($rules->isUnique(['name']));
  298. $this->assertFalse($table->save($entity));
  299. $this->assertEquals(['_isUnique' => 'This value is already in use'], $entity->errors('name'));
  300. $entity->name = 'jose';
  301. $this->assertSame($entity, $table->save($entity));
  302. $entity = $table->get(1);
  303. $entity->dirty('name', true);
  304. $this->assertSame($entity, $table->save($entity));
  305. }
  306. /**
  307. * Tests isUnique with multiple fields
  308. *
  309. * @group save
  310. * @return void
  311. */
  312. public function testIsUniqueMultipleFields()
  313. {
  314. $entity = new Entity([
  315. 'author_id' => 1,
  316. 'title' => 'First Article'
  317. ]);
  318. $table = TableRegistry::get('Articles');
  319. $rules = $table->rulesChecker();
  320. $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
  321. $this->assertFalse($table->save($entity));
  322. $this->assertEquals(['title' => ['_isUnique' => 'Nope']], $entity->errors());
  323. $entity->clean();
  324. $entity->author_id = 2;
  325. $this->assertSame($entity, $table->save($entity));
  326. }
  327. /**
  328. * Tests the existsIn domain rule
  329. *
  330. * @group save
  331. * @return void
  332. */
  333. public function testExistsInDomainRule()
  334. {
  335. $entity = new Entity([
  336. 'title' => 'An Article',
  337. 'author_id' => 500
  338. ]);
  339. $table = TableRegistry::get('Articles');
  340. $table->belongsTo('Authors');
  341. $rules = $table->rulesChecker();
  342. $rules->add($rules->existsIn('author_id', 'Authors'));
  343. $this->assertFalse($table->save($entity));
  344. $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->errors('author_id'));
  345. }
  346. /**
  347. * Tests the existsIn domain rule when passing an object
  348. *
  349. * @group save
  350. * @return void
  351. */
  352. public function testExistsInDomainRuleWithObject()
  353. {
  354. $entity = new Entity([
  355. 'title' => 'An Article',
  356. 'author_id' => 500
  357. ]);
  358. $table = TableRegistry::get('Articles');
  359. $rules = $table->rulesChecker();
  360. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  361. $this->assertFalse($table->save($entity));
  362. $this->assertEquals(['_existsIn' => 'Nope'], $entity->errors('author_id'));
  363. }
  364. /**
  365. * ExistsIn uses the schema to verify that nullable fields are ok.
  366. *
  367. * @return
  368. */
  369. public function testExistsInNullValue()
  370. {
  371. $entity = new Entity([
  372. 'title' => 'An Article',
  373. 'author_id' => null
  374. ]);
  375. $table = TableRegistry::get('Articles');
  376. $table->belongsTo('Authors');
  377. $rules = $table->rulesChecker();
  378. $rules->add($rules->existsIn('author_id', 'Authors'));
  379. $this->assertEquals($entity, $table->save($entity));
  380. $this->assertEquals([], $entity->errors('author_id'));
  381. }
  382. /**
  383. * Tests the checkRules save option
  384. *
  385. * @group save
  386. * @return void
  387. */
  388. public function testSkipRulesChecking()
  389. {
  390. $entity = new Entity([
  391. 'title' => 'An Article',
  392. 'author_id' => 500
  393. ]);
  394. $table = TableRegistry::get('Articles');
  395. $rules = $table->rulesChecker();
  396. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  397. $this->assertSame($entity, $table->save($entity, ['checkRules' => false]));
  398. }
  399. /**
  400. * Tests the beforeRules event
  401. *
  402. * @group save
  403. * @return void
  404. */
  405. public function testUseBeforeRules()
  406. {
  407. $entity = new Entity([
  408. 'title' => 'An Article',
  409. 'author_id' => 500
  410. ]);
  411. $table = TableRegistry::get('Articles');
  412. $rules = $table->rulesChecker();
  413. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  414. $table->eventManager()->attach(
  415. function ($event, Entity $entity, \ArrayObject $options, $operation) {
  416. $this->assertEquals(
  417. [
  418. 'atomic' => true,
  419. 'associated' => true,
  420. 'checkRules' => true,
  421. 'checkExisting' => true,
  422. '_primary' => true,
  423. ],
  424. $options->getArrayCopy()
  425. );
  426. $this->assertEquals('create', $operation);
  427. $event->stopPropagation();
  428. return true;
  429. },
  430. 'Model.beforeRules'
  431. );
  432. $this->assertSame($entity, $table->save($entity));
  433. }
  434. /**
  435. * Tests the afterRules event
  436. *
  437. * @group save
  438. * @return void
  439. */
  440. public function testUseAfterRules()
  441. {
  442. $entity = new Entity([
  443. 'title' => 'An Article',
  444. 'author_id' => 500
  445. ]);
  446. $table = TableRegistry::get('Articles');
  447. $rules = $table->rulesChecker();
  448. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  449. $table->eventManager()->attach(
  450. function ($event, Entity $entity, \ArrayObject $options, $result, $operation) {
  451. $this->assertEquals(
  452. [
  453. 'atomic' => true,
  454. 'associated' => true,
  455. 'checkRules' => true,
  456. 'checkExisting' => true,
  457. '_primary' => true,
  458. ],
  459. $options->getArrayCopy()
  460. );
  461. $this->assertEquals('create', $operation);
  462. $this->assertFalse($result);
  463. $event->stopPropagation();
  464. return true;
  465. },
  466. 'Model.afterRules'
  467. );
  468. $this->assertSame($entity, $table->save($entity));
  469. }
  470. /**
  471. * Tests that rules can be changed using the buildRules event
  472. *
  473. * @group save
  474. * @return void
  475. */
  476. public function testUseBuildRulesEvent()
  477. {
  478. $entity = new Entity([
  479. 'title' => 'An Article',
  480. 'author_id' => 500
  481. ]);
  482. $table = TableRegistry::get('Articles');
  483. $table->eventManager()->attach(function ($event, $rules) {
  484. $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));
  485. }, 'Model.buildRules');
  486. $this->assertFalse($table->save($entity));
  487. }
  488. /**
  489. * Tests isUnique with untouched fields
  490. *
  491. * @group save
  492. * @return void
  493. */
  494. public function testIsUniqueWithCleanFields()
  495. {
  496. $table = TableRegistry::get('Articles');
  497. $entity = $table->get(1);
  498. $rules = $table->rulesChecker();
  499. $rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));
  500. $entity->body = 'Foo';
  501. $this->assertSame($entity, $table->save($entity));
  502. $entity->title = 'Third Article';
  503. $this->assertFalse($table->save($entity));
  504. }
  505. /**
  506. * Tests isUnique rule with coflicting columns
  507. *
  508. * @group save
  509. * @return void
  510. */
  511. public function testIsUniqueAliasPrefix()
  512. {
  513. $entity = new Entity([
  514. 'title' => 'An Article',
  515. 'author_id' => 1
  516. ]);
  517. $table = TableRegistry::get('Articles');
  518. $table->belongsTo('Authors');
  519. $rules = $table->rulesChecker();
  520. $rules->add($rules->isUnique(['author_id']));
  521. $table->Authors->eventManager()->on('Model.beforeFind', function ($event, $query) {
  522. $query->leftJoin(['a2' => 'authors']);
  523. });
  524. $this->assertFalse($table->save($entity));
  525. $this->assertEquals(['_isUnique' => 'This value is already in use'], $entity->errors('author_id'));
  526. }
  527. /**
  528. * Tests the existsIn rule when passing non dirty fields
  529. *
  530. * @group save
  531. * @return void
  532. */
  533. public function testExistsInWithCleanFields()
  534. {
  535. $table = TableRegistry::get('Articles');
  536. $table->belongsTo('Authors');
  537. $rules = $table->rulesChecker();
  538. $rules->add($rules->existsIn('author_id', 'Authors'));
  539. $entity = $table->get(1);
  540. $entity->title = 'Foo';
  541. $entity->author_id = 1000;
  542. $entity->dirty('author_id', false);
  543. $this->assertSame($entity, $table->save($entity));
  544. }
  545. /**
  546. * Tests the existsIn with coflicting columns
  547. *
  548. * @group save
  549. * @return void
  550. */
  551. public function testExistsInAliasPrefix()
  552. {
  553. $entity = new Entity([
  554. 'title' => 'An Article',
  555. 'author_id' => 500
  556. ]);
  557. $table = TableRegistry::get('Articles');
  558. $table->belongsTo('Authors');
  559. $rules = $table->rulesChecker();
  560. $rules->add($rules->existsIn('author_id', 'Authors'));
  561. $table->Authors->eventManager()->on('Model.beforeFind', function ($event, $query) {
  562. $query->leftJoin(['a2' => 'authors']);
  563. });
  564. $this->assertFalse($table->save($entity));
  565. $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->errors('author_id'));
  566. }
  567. /**
  568. * Tests that using an array in existsIn() sets the error message correctly
  569. *
  570. * @return
  571. */
  572. public function testExistsInErrorWithArrayField()
  573. {
  574. $entity = new Entity([
  575. 'title' => 'An Article',
  576. 'author_id' => 500
  577. ]);
  578. $table = TableRegistry::get('Articles');
  579. $table->belongsTo('Authors');
  580. $rules = $table->rulesChecker();
  581. $rules->add($rules->existsIn(['author_id'], 'Authors'));
  582. $this->assertFalse($table->save($entity));
  583. $this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->errors('author_id'));
  584. }
  585. /**
  586. * Tests using rules to prevent delete operations
  587. *
  588. * @group delete
  589. * @return void
  590. */
  591. public function testDeleteRules()
  592. {
  593. $table = TableRegistry::get('Articles');
  594. $rules = $table->rulesChecker();
  595. $rules->addDelete(function ($entity) {
  596. return false;
  597. });
  598. $entity = $table->get(1);
  599. $this->assertFalse($table->delete($entity));
  600. }
  601. /**
  602. * Checks that it is possible to pass custom options to rules when saving
  603. *
  604. * @group save
  605. * @return void
  606. */
  607. public function testCustomOptionsPassingSave()
  608. {
  609. $entity = new Entity([
  610. 'name' => 'jose'
  611. ]);
  612. $table = TableRegistry::get('Authors');
  613. $rules = $table->rulesChecker();
  614. $rules->add(function ($entity, $options) {
  615. $this->assertEquals('bar', $options['foo']);
  616. $this->assertEquals('option', $options['another']);
  617. return false;
  618. }, ['another' => 'option']);
  619. $this->assertFalse($table->save($entity, ['foo' => 'bar']));
  620. }
  621. /**
  622. * Tests passing custom options to rules from delete
  623. *
  624. * @group delete
  625. * @return void
  626. */
  627. public function testCustomOptionsPassingDelete()
  628. {
  629. $table = TableRegistry::get('Articles');
  630. $rules = $table->rulesChecker();
  631. $rules->addDelete(function ($entity, $options) {
  632. $this->assertEquals('bar', $options['foo']);
  633. $this->assertEquals('option', $options['another']);
  634. return false;
  635. }, ['another' => 'option']);
  636. $entity = $table->get(1);
  637. $this->assertFalse($table->delete($entity, ['foo' => 'bar']));
  638. }
  639. /**
  640. * Test adding rules that return error string
  641. *
  642. * @group save
  643. * @return void
  644. */
  645. public function testCustomErrorMessageFromRule()
  646. {
  647. $entity = new Entity([
  648. 'name' => 'larry'
  649. ]);
  650. $table = TableRegistry::get('Authors');
  651. $rules = $table->rulesChecker();
  652. $rules->add(function () {
  653. return 'So much nope';
  654. }, ['errorField' => 'name']);
  655. $this->assertFalse($table->save($entity));
  656. $this->assertEquals(['So much nope'], $entity->errors('name'));
  657. }
  658. /**
  659. * Test adding rules with no errorField do not accept strings
  660. *
  661. * @group save
  662. * @return void
  663. */
  664. public function testCustomErrorMessageFromRuleNoErrorField()
  665. {
  666. $entity = new Entity([
  667. 'name' => 'larry'
  668. ]);
  669. $table = TableRegistry::get('Authors');
  670. $rules = $table->rulesChecker();
  671. $rules->add(function () {
  672. return 'So much nope';
  673. });
  674. $this->assertFalse($table->save($entity));
  675. $this->assertEmpty($entity->errors());
  676. }
  677. /**
  678. * Tests that using existsIn for a hasMany association will not be called
  679. * as the foreign key for the association was automatically validated already.
  680. *
  681. * @group save
  682. * @return void
  683. */
  684. public function testAvoidExistsInOnAutomaticSaving()
  685. {
  686. $entity = new \Cake\ORM\Entity([
  687. 'name' => 'Jose'
  688. ]);
  689. $entity->articles = [
  690. new \Cake\ORM\Entity([
  691. 'title' => '1',
  692. 'body' => 'A body'
  693. ]),
  694. new \Cake\ORM\Entity([
  695. 'title' => 'Another Title',
  696. 'body' => 'Another body'
  697. ])
  698. ];
  699. $table = TableRegistry::get('authors');
  700. $table->hasMany('articles');
  701. $table->association('articles')->belongsTo('authors');
  702. $checker = $table->association('articles')->target()->rulesChecker();
  703. $checker->add(function ($entity, $options) use ($checker) {
  704. $rule = $checker->existsIn('author_id', 'authors');
  705. $id = $entity->author_id;
  706. $entity->author_id = 5000;
  707. $result = $rule($entity, $options);
  708. $this->assertTrue($result);
  709. $entity->author_id = $id;
  710. return true;
  711. });
  712. $this->assertSame($entity, $table->save($entity));
  713. }
  714. }