Browse Source

Simplify behaviors

euromark 11 years ago
parent
commit
59ace0c210

+ 13 - 5
src/Model/Behavior/PasswordableBehavior.php

@@ -4,7 +4,6 @@ namespace Tools\Model\Behavior;
 use Cake\Event\Event;
 use Cake\ORM\Behavior;
 use Cake\ORM\Entity;
-use Cake\ORM\Query;
 use Cake\ORM\Table;
 use Cake\Utility\Inflector;
 use Cake\Core\Configure;
@@ -124,13 +123,24 @@ class PasswordableBehavior extends Behavior {
 	 * @return void
 	 */
 	public function __construct(Table $table, array $config = []) {
-		$defaults = $this->_defaultConfig;
+				$defaults = $this->_defaultConfig;
 		if ($configureDefaults = Configure::read('Passwordable')) {
 			$defaults = $configureDefaults + $defaults;
 		}
 		$config + $defaults;
 		parent::__construct($table, $config);
+	}
 
+	/**
+	 * Constructor hook method.
+	 *
+	 * Implement this method to avoid having to overwrite
+	 * the constructor and call parent.
+	 *
+	 * @param array $config The configuration array this behavior is using.
+	 * @return void
+	 */
+	public function initialize(array $config) {
 		$formField = $this->_config['formField'];
 		$formFieldRepeat = $this->_config['formFieldRepeat'];
 		$formFieldCurrent = $this->_config['formFieldCurrent'];
@@ -162,7 +172,7 @@ class PasswordableBehavior extends Behavior {
 			$rules[$field] = $fieldRules;
 		}
 
-		$validator = $table->validator($this->_config['validator']);
+		$validator = $this->_table->validator($this->_config['validator']);
 
 		// Add the validation rules if not already attached
 		if (!count($validator->field($formField))) {
@@ -201,8 +211,6 @@ class PasswordableBehavior extends Behavior {
 				$validator->allowEmpty($formField, !$this->_config['require']);
 			}
 		}
-
-		$this->_table = $table;
 	}
 
 	/**

+ 0 - 2
src/Model/Behavior/ResetBehavior.php

@@ -70,8 +70,6 @@ class ResetBehavior extends Behavior {
 		}
 		$config + $defaults;
 		parent::__construct($table, $config);
-
-		$this->_table = $table;
 	}
 
 	/**

+ 11 - 2
src/Model/Behavior/SluggedBehavior.php

@@ -94,13 +94,22 @@ class SluggedBehavior extends Behavior {
 		$config += (array)Configure::read('Slugged');
 
 		parent::__construct($table, $config);
+	}
 
+	/**
+	 * Constructor hook method.
+	 *
+	 * Implement this method to avoid having to overwrite
+	 * the constructor and call parent.
+	 *
+	 * @param array $config The configuration array this behavior is using.
+	 * @return void
+	 */
+	public function initialize(array $config) {
 		if ($this->_config['length'] === null) {
 			$length = $table->schema()->column($this->_config['field'])['length'];
 			$this->_config['length'] = $length ?: 0;
 		}
-
-		$this->_table = $table;
 	}
 
 	/**

+ 1 - 15
tests/TestCase/Model/Behavior/PasswordableBehaviorTest.php

@@ -35,20 +35,6 @@ class PasswordableBehaviorTest extends TestCase {
 		Configure::write('Passwordable.auth', 'AuthTest');
 
 		$this->Users = TableRegistry::get('ToolsUsers');
-		/*
-		if (isset($this->Users->validate['pwd'])) {
-			unset($this->Users->validate['pwd']);
-		}
-		if (isset($this->Users->validate['pwd_repeat'])) {
-			unset($this->Users->validate['pwd_repeat']);
-		}
-		if (isset($this->Users->validate['pwd_current'])) {
-			unset($this->Users->validate['pwd_current']);
-		}
-		if (isset($this->Users->order)) {
-			unset($this->Users->order);
-		}
-		*/
 
 		$this->hasher = PasswordHasherFactory::build('Default');
 		$user = $this->Users->newEntity();
@@ -60,7 +46,7 @@ class PasswordableBehaviorTest extends TestCase {
 		);
 		$this->Users->patchEntity($user, $data);
 		$result = $this->Users->save($user);
-		//$this->assertTrue();
+		$this->assertTrue((bool)$result);
 
 		Router::setRequestInfo(new Request());
 	}