From 1f3c3720f285a5519036175c4f87680719f9be04 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Tue, 2 Dec 2014 20:46:25 +0100 Subject: [PATCH] Renaming more methods and implemeting events for the rules checking --- src/ORM/Rule/ExistsIn.php | 2 +- src/ORM/Table.php | 29 ++++++-- .../ORM/RulesCheckerIntegrationTest.php | 73 ++++++++++++++++--- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/src/ORM/Rule/ExistsIn.php b/src/ORM/Rule/ExistsIn.php index b1f2c37d344..283ed0976d8 100644 --- a/src/ORM/Rule/ExistsIn.php +++ b/src/ORM/Rule/ExistsIn.php @@ -37,7 +37,7 @@ class ExistsIn { * or the association name for the repository. */ public function __construct($fields, $repository) { - $this->_field = (array)$fields; + $this->_fields = (array)$fields; $this->_repository = $repository; } diff --git a/src/ORM/Table.php b/src/ORM/Table.php index 2da7a255075..66fc8cf96a6 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -1231,7 +1231,7 @@ protected function _processSave($entity, $options) { $entity->isNew(!$this->exists($conditions)); } - if ($options['checkRules'] && !$this->checkDomainRules($entity)) { + if ($options['checkRules'] && !$this->checkRules($entity)) { return false; } @@ -1842,17 +1842,30 @@ public function validateUnique($value, array $options, array $context = []) { return !$this->exists($conditions); } - public function checkDomainRules($entity) { - $rules = $this->domainRules(); + public function checkRules($entity) { + $rules = $this->rulesChecker(); + $event = $this->dispatchEvent('Model.beforeRules', compact('entity', 'rules')); + if ($event->isStopped()) { + return $event->result; + } + if ($entity->isNew()) { - return $rules->checkCreate($entity); + $result = $rules->checkCreate($entity); + } else { + $result = $rules->checkUpdate($entity); + } + + $event = $this->dispatchEvent('Model.afterRules', compact('entity', 'rules', 'result')); + + if ($event->isStopped()) { + return $event->result; } - return $rules->checkUpdate($entity); + return $result; } - public function domainRules() { + public function rulesChecker() { if ($this->_rulesChecker !== null) { return $this->_rulesChecker; } @@ -1881,8 +1894,8 @@ public function implementedEvents() { 'Model.afterSave' => 'afterSave', 'Model.beforeDelete' => 'beforeDelete', 'Model.afterDelete' => 'afterDelete', - 'Model.beforeValidate' => 'beforeValidate', - 'Model.afterValidate' => 'afterValidate', + 'Model.beforeRules' => 'beforeRules', + 'Model.afterRules' => 'afterRules', ]; $events = []; diff --git a/tests/TestCase/ORM/RulesCheckerIntegrationTest.php b/tests/TestCase/ORM/RulesCheckerIntegrationTest.php index 0385303ba25..c3faef6cc83 100644 --- a/tests/TestCase/ORM/RulesCheckerIntegrationTest.php +++ b/tests/TestCase/ORM/RulesCheckerIntegrationTest.php @@ -15,6 +15,7 @@ namespace Cake\Test\TestCase\ORM; use Cake\ORM\Entity; +use Cake\ORM\RulesChecker; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; @@ -59,7 +60,7 @@ public function testsSaveBelongsToWithValidationError() { $table->belongsTo('authors'); $table->association('authors') ->target() - ->domainRules() + ->rulesChecker() ->add(function (Entity $author, array $options) use ($table) { $this->assertSame($options['repository'], $table->association('authors')->target()); return false; @@ -92,7 +93,7 @@ public function testSaveHasOneWithValidationError() { $table->hasOne('articles'); $table->association('articles') ->target() - ->domainRules() + ->rulesChecker() ->add(function (Entity $entity) { return false; }, ['errorField' => 'title', 'message' => 'This is an error']); @@ -132,7 +133,7 @@ public function testSaveHasManyWithErrorsAtomic() { $table->hasMany('articles'); $table->association('articles') ->target() - ->domainRules() + ->rulesChecker() ->add(function (Entity $entity) { return $entity->title === '1'; }, ['errorField' => 'title', 'message' => 'This is an error']); @@ -175,7 +176,7 @@ public function testSaveHasManyWithErrorsNonAtomic() { $table->hasMany('articles'); $table->association('articles') ->target() - ->domainRules() + ->rulesChecker() ->add(function (Entity $article) { return is_numeric($article->title); }, ['errorField' => 'title', 'message' => 'This is an error']); @@ -213,7 +214,7 @@ public function testSaveBelongsToManyWithValidationErrorInJointEntity() { $table->belongsToMany('tags'); $table->association('tags') ->junction() - ->domainRules() + ->rulesChecker() ->add(function (Entity $entity) { return $entity->article_id > 4; }); @@ -252,7 +253,7 @@ public function testSaveBelongsToManyWithValidationErrorInJointEntityNonAtomic() $table->belongsToMany('tags'); $table->association('tags') ->junction() - ->domainRules() + ->rulesChecker() ->add(function (Entity $entity) { return $entity->tag_id > 4; }); @@ -280,7 +281,7 @@ public function testIsUniqueDomainRule() { ]); $table = TableRegistry::get('Authors'); - $rules = $table->domainRules(); + $rules = $table->rulesChecker(); $rules->add($rules->isUnique(['name'])); $this->assertFalse($table->save($entity)); @@ -307,7 +308,7 @@ public function testIsUniqueMultipleFields() { ]); $table = TableRegistry::get('Articles'); - $rules = $table->domainRules(); + $rules = $table->rulesChecker(); $rules->add($rules->isUnique(['title', 'author_id'], 'Nope')); $this->assertFalse($table->save($entity)); @@ -332,7 +333,7 @@ public function testExistsInDomainRule() { $table = TableRegistry::get('Articles'); $table->belongsTo('Authors'); - $rules = $table->domainRules(); + $rules = $table->rulesChecker(); $rules->add($rules->existsIn('author_id', 'Authors')); $this->assertFalse($table->save($entity)); @@ -352,7 +353,7 @@ public function testExistsInDomainRuleWithObject() { ]); $table = TableRegistry::get('Articles'); - $rules = $table->domainRules(); + $rules = $table->rulesChecker(); $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope')); $this->assertFalse($table->save($entity)); @@ -372,10 +373,60 @@ public function testSkipRulesChecking() { ]); $table = TableRegistry::get('Articles'); - $rules = $table->domainRules(); + $rules = $table->rulesChecker(); $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope')); $this->assertSame($entity, $table->save($entity, ['checkRules' => false])); } +/** + * Tests the beforeRules event + * + * @group save + * @return void + */ + public function testUseBeforeRules() { + $entity = new Entity([ + 'title' => 'An Article', + 'author_id' => 500 + ]); + + $table = TableRegistry::get('Articles'); + $rules = $table->rulesChecker(); + $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope')); + + $table->eventManager()->attach(function ($event, Entity $entity, RulesChecker $check) { + $this->assertSame($event->subject()->rulesChecker(), $check); + $event->stopPropagation(); + return true; + }, 'Model.beforeRules'); + + $this->assertSame($entity, $table->save($entity)); + } + +/** + * Tests the afterRules event + * + * @group save + * @return void + */ + public function testUseAfterRules() { + $entity = new Entity([ + 'title' => 'An Article', + 'author_id' => 500 + ]); + + $table = TableRegistry::get('Articles'); + $rules = $table->rulesChecker(); + $rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope')); + + $table->eventManager()->attach(function ($event, Entity $entity, RulesChecker $check, $result) { + $this->assertSame($event->subject()->rulesChecker(), $check); + $this->assertFalse($result); + $event->stopPropagation(); + return true; + }, 'Model.afterRules'); + + $this->assertSame($entity, $table->save($entity)); + } }