Browse Source

Making behaviors listeners of rules events

Jose Lorenzo Rodriguez 11 years ago
parent
commit
abfe59f1a0
2 changed files with 31 additions and 15 deletions
  1. 12 7
      src/ORM/Behavior.php
  2. 19 8
      tests/TestCase/ORM/BehaviorTest.php

+ 12 - 7
src/ORM/Behavior.php

@@ -53,12 +53,16 @@ use Cake\Event\EventListenerInterface;
  *   $primary parameter indicates whether or not this is the root query,
  *   or an associated query.
  *
- * - `beforeValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
- *   Fired before an entity is validated. By stopping this event, you can abort
- *   the validate + save operations.
+ * - `buildRules(Event $event, RulesChecker $rules)`
+ *   Allows listeners to modify the rules checker by adding more rules.
  *
- * - `afterValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
- *   Fired after an entity is validated.
+ * - `beforeRules(Event $event, Entity $entity, RulesChecker $rules)`
+ *   Fired before an entity is validated using the rules checker. By stopping this event,
+ *   you can return the final value of the rules checking operation.
+ *
+ * - `afterRules(Event $event, Entity $entity,RulesChecker $rules, bool $result)`
+ *   Fired after the rules have been checked on the entity.By stopping this event,
+ *   you can return the final value of the rules checking operation.
  *
  * - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
  *   Fired before each entity is saved. Stopping this event will abort the save
@@ -239,8 +243,9 @@ class Behavior implements EventListenerInterface {
 			'Model.afterSave' => 'afterSave',
 			'Model.beforeDelete' => 'beforeDelete',
 			'Model.afterDelete' => 'afterDelete',
-			'Model.beforeValidate' => 'beforeValidate',
-			'Model.afterValidate' => 'afterValidate',
+			'Model.buildRules' => 'buildRules',
+			'Model.beforeRules' => 'beforeRules',
+			'Model.afterRules' => 'afterRules',
 		];
 		$config = $this->config();
 		$priority = isset($config['priority']) ? $config['priority'] : null;

+ 19 - 8
tests/TestCase/ORM/BehaviorTest.php

@@ -31,13 +31,19 @@ class TestBehavior extends Behavior {
 /**
  * Test for event bindings.
  */
-	public function beforeValidate() {
+	public function beforeRules() {
 	}
 
 /**
  * Test for event bindings.
  */
-	public function afterValidate() {
+	public function afterRules() {
+	}
+
+/**
+ * Test for event bindings.
+ */
+	public function buildRules() {
 	}
 
 }
@@ -184,8 +190,9 @@ class BehaviorTest extends TestCase {
 		$behavior = new TestBehavior($table);
 		$expected = [
 			'Model.beforeFind' => 'beforeFind',
-			'Model.beforeValidate' => 'beforeValidate',
-			'Model.afterValidate' => 'afterValidate',
+			'Model.buildRules' => 'buildRules',
+			'Model.beforeRules' => 'beforeRules',
+			'Model.afterRules' => 'afterRules',
 		];
 		$this->assertEquals($expected, $behavior->implementedEvents());
 	}
@@ -203,13 +210,17 @@ class BehaviorTest extends TestCase {
 				'priority' => 10,
 				'callable' => 'beforeFind'
 			],
-			'Model.beforeValidate' => [
+			'Model.beforeRules' => [
+				'priority' => 10,
+				'callable' => 'beforeRules'
+			],
+			'Model.afterRules' => [
 				'priority' => 10,
-				'callable' => 'beforeValidate'
+				'callable' => 'afterRules'
 			],
-			'Model.afterValidate' => [
+			'Model.buildRules' => [
 				'priority' => 10,
-				'callable' => 'afterValidate'
+				'callable' => 'buildRules'
 			],
 		];
 		$this->assertEquals($expected, $behavior->implementedEvents());