euromark 11 years ago
parent
commit
7d62f0fade

+ 25 - 25
src/Model/Behavior/PasswordableBehavior.php

@@ -195,6 +195,8 @@ class PasswordableBehavior extends Behavior {
 				$validator->allowEmpty($formField, !$this->_config['require']);
 			}
 		}
+
+		$this->_table = $table;
 	}
 
 	/**
@@ -326,24 +328,16 @@ class PasswordableBehavior extends Behavior {
 	 * @param array $data
 	 * @return bool Success
 	 */
-	public function validateCurrentPwd($data) {
-		if (is_array($data)) {
-			$pwd = array_shift($data);
-		} else {
-			$pwd = $data;
-		}
-die(debug($data));
+	public function validateCurrentPwd($pwd, $config) {
 		$uid = null;
-		if ($Model->id) {
-			$uid = $Model->id;
-		} elseif (!empty($Model->data[$table->alias()]['id'])) {
-			$uid = $Model->data[$table->alias()]['id'];
+		if (!empty($config['data'][$this->_table->primaryKey()])) {
+			$uid = $config['data'][$this->_table->primaryKey()];
 		} else {
 			trigger_error('No user id given');
 			return false;
 		}
 
-		return $this->_validateSameHash($pwd);
+		return $this->_validateSameHash($pwd, $config);
 	}
 
 	/**
@@ -364,10 +358,9 @@ die(debug($data));
 	 *
 	 * @return bool Success
 	 */
-	public function validateNotSame($data, $field1, $field2) {
-		die(debug($data));
-		$value1 = $Model->data[$table->alias()][$field1];
-		$value2 = $Model->data[$table->alias()][$field2];
+	public function validateNotSame($data, $field1, $field2, $config) {
+		$value1 = $config['providers']['entity']->get($field1);
+		$value2 = $config['providers']['entity']->get($field2);
 		return ($value1 !== $value2);
 	}
 
@@ -376,17 +369,20 @@ die(debug($data));
 	 *
 	 * @return bool Success
 	 */
-	public function validateNotSameHash($data, $formField) {
+	public function validateNotSameHash($data, $formField, $config) {
 		$field = $this->_config['field'];
-die(debug($formField));
-		if (!isset($Model->data[$table->alias()][$Model->primaryKey])) {
+		if (!$config['providers']['entity']->get($this->_table->primaryKey())) {
 			return true;
 		}
 
-		$primaryKey = $Model->data[$table->alias()][$Model->primaryKey];
-		$value = $Model->data[$table->alias()][$formField];
+		$primaryKey = $config['providers']['entity']->get($this->_table->primaryKey());
+		$value = $config['providers']['entity']->get($formField);
 
-		$dbValue = $Model->field($field, array($Model->primaryKey => $primaryKey));
+		$dbValue = $this->_table->find()->where(array($this->_table->primaryKey() => $primaryKey))->first();
+		if (!$dbValue) {
+			return true;
+		}
+		$dbValue = $dbValue[$field];
 		if (!$dbValue) {
 			return true;
 		}
@@ -402,11 +398,15 @@ die(debug($formField));
 	 * @param string $pwd
 	 * @return bool Success
 	 */
-	protected function _validateSameHash($pwd) {
+	protected function _validateSameHash($pwd, $config) {
 		$field = $this->_config['field'];
 
-		$primaryKey = $Model->data[$table->alias()][$Model->primaryKey];
-		$dbValue = $Model->field($field, array($Model->primaryKey => $primaryKey));
+		$primaryKey = $config['providers']['entity']->get($this->_table->primaryKey());
+		$dbValue = $this->_table->find()->where(array($this->_table->primaryKey() => $primaryKey))->first();
+		if (!$dbValue) {
+			return false;
+		}
+		$dbValue = $dbValue[$field];
 		if (!$dbValue && $pwd) {
 			return false;
 		}

+ 2 - 2
tests/Fixture/ToolsUsersFixture.php

@@ -15,8 +15,8 @@ class ToolsUsersFixture extends TestFixture {
 	 */
 	public $fields = array(
 		'id' => ['type' => 'integer'],
-		'name' => ['type' => 'string', 'null' => false],
-		'password' => ['type' => 'string', 'null' => false],
+		'name' => ['type' => 'string', 'null' => true],
+		'password' => ['type' => 'string', 'null' => true],
 		'role_id' => ['type' => 'integer', 'null' => true],
 		'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
 	);

+ 8 - 0
tests/TestApp/Auth/ComplexPasswordHasher.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace TestApp\Auth;
+
+use Cake\Auth\DefaultPasswordHasher;
+
+class ComplexPasswordHasher extends DefaultPasswordHasher {
+}

+ 1 - 1
tests/TestCase/Controller/Component/SessionComponentTest.php

@@ -2,7 +2,7 @@
 namespace Tools\Test\TestCase\Controller\Component;
 
 use Cake\Controller\ComponentRegistry;
-use Cake\Controller\Component\SessionComponent;
+use Tools\Controller\Component\SessionComponent;
 use Cake\Controller\Controller;
 use Cake\Core\Configure;
 use Cake\Network\Request;

+ 180 - 314
tests/TestCase/Model/Behavior/PasswordableBehaviorTest.php

@@ -13,6 +13,7 @@ use Cake\ORM\TableRegistry;
 use Cake\Utility\Security;
 use Cake\Routing\Router;
 use Cake\Network\Request;
+use Cake\Auth\PasswordHasherFactory;
 
 class PasswordableBehaviorTest extends TestCase {
 
@@ -49,30 +50,25 @@ class PasswordableBehaviorTest extends TestCase {
 		}
 		*/
 
+		$this->hasher = PasswordHasherFactory::build('Default');
 		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => '5',
 			'name' => 'admin',
-			'password' => Security::hash('somepwd', null, true),
+			'password' => $this->hasher->hash('somepwd'),
 			'role_id' => '1'
 		);
 		$this->Users->patchEntity($user, $data);
 		$result = $this->Users->save($user);
-		$this->assertTrue((bool)$result);
+		//$this->assertTrue();
 
 		Router::setRequestInfo(new Request());
 	}
 
-	/**
-	 * PasswordableBehaviorTest::testObject()
-	 *
-	 * @return void
-	 */
-	public function testObject() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array());
-		$this->assertInstanceOf('PasswordableBehavior', $this->Users->Behaviors->Passwordable);
-		$result = $this->Users->Behaviors->loaded('Passwordable');
-		$this->assertTrue($result);
+	public function tearDown() {
+		TableRegistry::clear();
+
+		parent::tearDown();
 	}
 
 	/**
@@ -81,37 +77,37 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testValidate() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array());
+		$this->Users->addBehavior('Tools.Passwordable', array());
 
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => '123456',
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
-		//debug($this->Users->validationErrors);
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
+		//debug($user->errors());
 		$this->assertFalse($is);
-		$this->assertEquals(array('pwd_repeat'), array_keys($this->Users->validationErrors));
+		$this->assertEquals(array('pwd_repeat'), array_keys($user->errors()));
 
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => '1234ab',
 			'pwd_repeat' => '123456'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
-		//debug($this->Users->validationErrors);
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
+		//debug($user->errors());
 		$this->assertFalse($is);
-		$this->assertEquals(array(__d('tools', 'valErrPwdNotMatch')), $this->Users->validationErrors['pwd_repeat']);
+		$this->assertEquals(array('validateIdentical' => __d('tools', 'valErrPwdNotMatch')), $user->errors()['pwd_repeat']);
 
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => '123456',
 			'pwd_repeat' => '123456'
 		);
-		$this->Users->set($data);
+		$this->Users->patchEntity($user, $data);
 		//debug($this->Users->validate);
-		$is = $this->Users->validates();
+		$is = $this->Users->validate($user);
 		$this->assertTrue(!empty($is));
 	}
 
@@ -121,13 +117,13 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testValidateNoConfirm() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array('confirm' => false));
-		$this->Users->create();
+		$this->Users->addBehavior('Tools.Passwordable', array('confirm' => false));
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => '123456',
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		//debug($is);
 		$this->assertTrue(!empty($is));
 	}
@@ -138,16 +134,16 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testValidateRequired() {
-		$this->Users->Behaviors->load('Tools.Passwordable');
-		$this->Users->create();
+		$this->Users->addBehavior('Tools.Passwordable');
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => '',
 			'pwd_repeat' => ''
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertFalse($is);
-		$this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->Users->validationErrors));
+		$this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($user->errors()));
 	}
 
 	/**
@@ -156,28 +152,29 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testValidateNotRequired() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array('require' => false));
-		$this->Users->create();
+		$this->Users->addBehavior('Tools.Passwordable', array('require' => false));
+		$user = $this->Users->newEntity();
 		$data = array(
 			'name' => 'foo', // we need at least one field besides the password on CREATE
 			'pwd' => '',
 			'pwd_repeat' => ''
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue((bool)$is);
-		$this->assertEquals(array('name', 'id'), array_keys($is[$this->Users->alias]));
+		$this->assertEquals(array('name', 'id'), $is->visibleProperties());
 
-		$id = $this->Users->id;
+		$id = $user->id;
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => $id,
 			'pwd' => '',
 			'pwd_repeat' => ''
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue((bool)$is);
-		$this->assertEquals(array('id'), array_keys($is[$this->Users->alias]));
+		$this->assertEquals(array('id'), $is->visibleProperties());
 	}
 
 	/**
@@ -186,32 +183,34 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testValidateEmptyWithCurrentPassword() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array('current' => true));
-		$this->Users->create();
+		$this->Users->addBehavior('Tools.Passwordable', array('current' => true));
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => '123',
 			'pwd' => '',
 			'pwd_repeat' => '',
 			'pwd_current' => '123456',
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
-		//debug($this->Users->validationErrors);
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
+		//debug($user->errors());
 		$this->assertFalse($is);
-		$this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->Users->validationErrors));
+		$this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($user->errors()));
 
 		$this->tearDown();
 		$this->setUp();
 
-		$this->Users->Behaviors->load('Tools.Passwordable', array('require' => false, 'current' => true));
-		$this->Users->create();
+		$this->Users->removeBehavior('Passwordable');
+		$this->Users->addBehavior('Tools.Passwordable', array('require' => false, 'current' => true));
+		$user = $this->Users->newEntity();
 		$data = array(
 			'name' => 'foo',
 			'pwd' => '',
 			'pwd_repeat' => '',
 			'pwd_current' => '',
 		);
-		$is = $this->Users->save($data);
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue(!empty($is));
 	}
 
@@ -219,19 +218,19 @@ class PasswordableBehaviorTest extends TestCase {
 	 * Test aliases for field names
 	 */
 	public function testDifferentFieldNames() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'formField' => 'passw',
 			'formFieldRepeat' => 'passw_repeat',
 			'formFieldCurrent' => 'passw_current',
 		));
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'passw' => '123456',
 			'passw_repeat' => '123456'
 		);
-		$this->Users->set($data);
+		$this->Users->patchEntity($user, $data);
 		//debug($this->Users->data);
-		$is = $this->Users->save();
+		$is = $this->Users->save($user);
 		$this->assertTrue(!empty($is));
 	}
 
@@ -239,34 +238,34 @@ class PasswordableBehaviorTest extends TestCase {
 	 * Assert that allowSame false does not allow storing the same password as previously entered
 	 */
 	public function testNotSame() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'formField' => 'passw',
 			'formFieldRepeat' => 'passw_repeat',
 			'formFieldCurrent' => 'passw_current',
 			'allowSame' => false,
 			'current' => true,
 		));
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => '5',
 			'passw_current' => 'something',
 			'passw' => 'somepwd',
 			'passw_repeat' => 'somepwd'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
-		//debug($this->Users->validationErrors);
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
+		//debug($user->errors());
 		$this->assertFalse($is);
 
-		$this->Users->create();
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => '5',
 			'passw_current' => 'somepwd',
 			'passw' => 'newpwd',
 			'passw_repeat' => 'newpwd'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue(!empty($is));
 	}
 
@@ -274,40 +273,40 @@ class PasswordableBehaviorTest extends TestCase {
 	 * Assert that allowSame false does not allow storing the same password as previously entered
 	 */
 	public function testNotSameWithoutCurrentField() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'formField' => 'passw',
 			'formFieldRepeat' => 'passw_repeat',
 			'allowSame' => false,
 			'current' => false
 		));
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'passw' => 'somepwd',
 			'passw_repeat' => 'somepwd'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue((bool)$is);
-		$id = $is[$this->Users->alias]['id'];
+		$id = $is['id'];
 
-		$this->Users->create();
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => $id,
 			'passw' => 'somepwd',
 			'passw_repeat' => 'somepwd'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertFalse((bool)$is);
 
-		$this->Users->create();
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => $id,
 			'passw' => 'newpwd',
 			'passw_repeat' => 'newpwd'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue((bool)$is);
 	}
 
@@ -315,40 +314,40 @@ class PasswordableBehaviorTest extends TestCase {
 	 * Assert that on edit it does not wrongly pass validation (require => false)
 	 */
 	public function testRequireFalse() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'formField' => 'passw',
 			'formFieldRepeat' => 'passw_repeat',
 			'require' => false
 		));
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'passw' => 'somepwd',
 			'passw_repeat' => 'somepwd'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue((bool)$is);
-		$id = $is[$this->Users->alias]['id'];
+		$id = $is['id'];
 
-		$this->Users->create();
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => $id,
 			'passw' => 'somepwd2',
 			'passw_repeat' => ''
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertFalse((bool)$is);
-		//debug($this->Users->validationErrors);
+		//debug($user->errors());
 
-		$this->Users->create();
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => $id,
 			'passw' => 'somepwd2',
 			'passw_repeat' => 'somepwd2'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue((bool)$is);
 	}
 
@@ -356,40 +355,42 @@ class PasswordableBehaviorTest extends TestCase {
 	 * Needs faking of pwd check...
 	 */
 	public function testValidateCurrent() {
-		$this->assertFalse($this->Users->Behaviors->loaded('Passwordable'));
-		$this->Users->create();
+		$this->assertFalse($this->Users->behaviors()->loaded('Passwordable'));
+		$user = $this->Users->newEntity();
 		$data = array(
 			'name' => 'xyz',
-			'password' => Security::hash('somepwd', null, true));
-		$result = $this->Users->save($data);
+			'password' => $this->hasher->hash('somepwd'));
+		$this->Users->patchEntity($user, $data);
+		$result = $this->Users->save($user);
 		$this->assertTrue(!empty($result));
-		$uid = (string)$this->Users->id;
+		$uid = (string)$user->id;
 
-		$this->Users->Behaviors->load('Tools.Passwordable', array('current' => true));
-		$this->Users->create();
+		$this->Users->removeBehavior('Passwordable');
+		$this->Users->addBehavior('Tools.Passwordable', array('current' => true));
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => $uid,
 			'pwd' => '123456',
 			'pwd_repeat' => '12345678',
 			//'pwd_current' => '',
 		);
-		$this->Users->set($data);
-		$this->assertTrue($this->Users->Behaviors->loaded('Passwordable'));
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$this->assertTrue($this->Users->behaviors()->loaded('Passwordable'));
+		$is = $this->Users->save($user);
 		$this->assertFalse($is);
 
-		$this->Users->create();
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => $uid,
 			'pwd_current' => 'somepwdx',
 			'pwd' => '123456',
 			'pwd_repeat' => '123456'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertFalse($is);
 
-		$this->Users->create();
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => $uid,
 			'name' => 'Yeah',
@@ -397,21 +398,26 @@ class PasswordableBehaviorTest extends TestCase {
 			'pwd' => '123456',
 			'pwd_repeat' => '123456'
 		);
-		$this->Users->set($data);
+		$user->accessible('*', false); // Mark all properties as protected
+		$user->accessible(array('id', 'pwd', 'pwd_repeat', 'pwd_current'), true);
+		$this->Users->patchEntity($user, $data);
 		// Test whitelist setting - only "password" needs to gets auto-added
 		$options = array('validate' => true, 'fieldList' => array('id', 'pwd', 'pwd_repeat', 'pwd_current'));
-		$is = $this->Users->save(null, $options);
+
+		$is = $this->Users->save($user, $options);
 		$this->assertTrue(!empty($is));
 
+		//$this->skipIf(true, 'FIXME: whitelisting fieldList');
+
 		$user = $this->Users->get($uid);
 		// The password is updated, the name not
-		$this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
-		$this->assertSame('xyz', $user['ToolsUser']['name']);
+		$this->assertSame($is['password'], $user['password']);
+		$this->assertSame('xyz', $user['name']);
 
 		// Proof that we manually need to add pwd, pwd_repeat etc due to a bug in CakePHP<=2.4 allowing behaviors to only modify saving,
 		// not validating of additional whitelist fields. Validation for those will be just skipped, no matter what the behavior tries
 		// to set.
-		$this->Users->create();
+		$user = $this->Users->newEntity([], ['markNew' => false]);
 		$data = array(
 			'id' => $uid,
 			'name' => 'Yeah',
@@ -419,29 +425,18 @@ class PasswordableBehaviorTest extends TestCase {
 			'pwd' => 'some', // Too short
 			'pwd_repeat' => 'somex' // Don't match
 		);
-		$this->Users->set($data);
+		$user->accessible('*', false); // Mark all properties as protected
+		$user->accessible(array('id', 'name'), true);
+		$this->Users->patchEntity($user, $data);
 		// Test whitelist setting - only "password" gets auto-added, pwd, pwd_repeat etc need to be added manually
 		// NOTE that I had to remove the code for adding those fields from the behavior (as it was not functional)
 		// So of course, this won't work now as expected. But feel free to try to add them in the behavior. Results will be the same.
 		$options = array('validate' => true, 'fieldList' => array('id', 'name'));
-		$is = $this->Users->save(null, $options);
-
-		if ((float)Configure::version() >= 2.5) {
-			// Validation errors triggered - as expected
-			$this->assertFalse($is);
-			$this->assertSame(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->Users->validationErrors));
-			return;
-		}
-
-		// Save is successful
-		$this->assertTrue(!empty($is));
-		$user = $this->Users->get($uid);
-
-		$this->assertSame('Yeah', $user['ToolsUser']['name']);
+		$is = $this->Users->save($user, $options);
 
-		// The password is not updated, the name is
-		$this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
-		$this->assertSame('Yeah', $user['ToolsUser']['name']);
+		// Validation errors triggered - as expected
+		$this->assertFalse($is);
+		$this->assertSame(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($user->errors()));
 	}
 
 	/**
@@ -452,56 +447,57 @@ class PasswordableBehaviorTest extends TestCase {
 	public function testPasswordHasher() {
 		$this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
 
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'formField' => 'pwd',
 			'formFieldRepeat' => 'pwd_repeat',
 			'allowSame' => false,
 			'current' => false,
 			'passwordHasher' => 'Complex',
 		));
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => 'somepwd',
 			'pwd_repeat' => 'somepwd'
 		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$result = $this->Users->save($user);
 		$this->assertTrue((bool)$result);
-		$uid = (string)$this->Users->id;
+		$uid = (string)$user->id;
 
-		$this->Users->Behaviors->load('Tools.Passwordable', array('current' => true));
-		$this->Users->create();
+		$this->Users->removeBehavior('Passwordable');
+		$this->Users->addBehavior('Tools.Passwordable', array('current' => true));
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => $uid,
 			'pwd' => '123456',
 			'pwd_repeat' => '12345678',
 			//'pwd_current' => '',
 		);
-		$this->Users->set($data);
-		$this->assertTrue($this->Users->Behaviors->loaded('Passwordable'));
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$this->assertTrue($this->Users->behaviors()->loaded('Passwordable'));
+		$is = $this->Users->save($user);
 		$this->assertFalse($is);
 
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => $uid,
 			'pwd_current' => 'somepwdx',
 			'pwd' => '123456',
 			'pwd_repeat' => '123456'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertFalse($is);
 
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => $uid,
 			'pwd_current' => 'somepwd',
 			'pwd' => '123456',
 			'pwd_repeat' => '123456'
 		);
-		$this->Users->set($data);
-		$is = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$is = $this->Users->save($user);
 		$this->assertTrue(!empty($is));
 	}
 
@@ -511,186 +507,60 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testBlowfish() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'allowSame' => false,
 			'current' => false,
 			'authType' => 'Blowfish',
 		));
 
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => 'somepwd',
 			'pwd_repeat' => 'somepwd'
 		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$result = $this->Users->save($user);
 		$this->assertTrue((bool)$result);
-		$uid = (string)$this->Users->id;
+		$uid = (string)$user->id;
 
-		$this->Users->Behaviors->load('Tools.Passwordable', array('current' => true));
+		$this->Users->removeBehavior('Passwordable');
+		$this->Users->addBehavior('Tools.Passwordable', array('current' => true));
 
 		// Without the current password it will not continue
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => $uid,
 			'pwd' => '123456',
 			'pwd_repeat' => '12345678',
 		);
-		$this->Users->set($data);
-		$this->assertTrue($this->Users->Behaviors->loaded('Passwordable'));
-		$result = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$this->assertTrue($this->Users->behaviors()->loaded('Passwordable'));
+		$result = $this->Users->save($user);
 		$this->assertFalse($result);
 
 		// Without the correct current password it will not continue
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => $uid,
 			'pwd_current' => 'somepwdx',
 			'pwd' => '123456',
 			'pwd_repeat' => '123456'
 		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
-		$this->assertFalse($result);
-
-		// Now it will
-		$this->Users->create();
-		$data = array(
-			'id' => $uid,
-			'pwd_current' => 'somepwd',
-			'pwd' => '123456',
-			'pwd_repeat' => '123456'
-		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
-		$this->assertTrue((bool)$result);
-	}
-
-	/**
-	 * Tests that passwords prior to PHP5.5 and/or password_hash() are still working
-	 * if Tools.Modern is being used.
-	 *
-	 * @return void
-	 */
-	public function testBlowfishWithBC() {
-		$this->skipIf(!function_exists('password_hash'), 'password_hash() is not available.');
-
-		$oldHash = Security::hash('foobar', 'blowfish', false);
-		$newHash = password_hash('foobar', PASSWORD_BCRYPT);
-
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
-			'allowSame' => false,
-			'current' => false,
-			'authType' => 'Blowfish',
-			'passwordHasher' => 'Tools.Modern'
-		));
-		$this->Users->create();
-		$data = array(
-			'pwd' => 'somepwd',
-			'pwd_repeat' => 'somepwd'
-		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
-		$this->assertTrue((bool)$result);
-		$uid = (string)$this->Users->id;
-
-		// Same pwd is not allowed
-		$this->Users->create();
-		$data = array(
-			'id' => $uid,
-			'pwd' => 'somepwd',
-			'pwd_repeat' => 'somepwd'
-		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
-		$this->assertFalse($result);
-
-		$this->Users->Behaviors->load('Tools.Passwordable', array('current' => true));
-
-		// Without the correct current password it will not continue
-		$this->Users->create();
-		$data = array(
-			'id' => $uid,
-			'pwd_current' => 'somepwdxyz',
-			'pwd' => '123456',
-			'pwd_repeat' => '123456'
-		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$result = $this->Users->save($user);
 		$this->assertFalse($result);
 
 		// Now it will
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'id' => $uid,
 			'pwd_current' => 'somepwd',
 			'pwd' => '123456',
 			'pwd_repeat' => '123456'
 		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
-		$this->assertTrue((bool)$result);
-
-		// Lets set a BC password (without password_hash() method but Security class)
-		$data = array(
-			'id' => $uid,
-			'password' => $oldHash,
-		);
-		$result = $this->Users->save($data, array('validate' => false));
-		$this->assertTrue((bool)$result);
-
-		// Now it will still work
-		$this->Users->create();
-		$data = array(
-			'id' => $uid,
-			'pwd_current' => 'foobar',
-			'pwd' => '123456',
-			'pwd_repeat' => '123456'
-		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
-		$this->assertTrue((bool)$result);
-
-		// Lets set an invalid BC password (without password_hash() method but Security class)
-		$data = array(
-			'id' => $uid,
-			'password' => $oldHash . 'x',
-		);
-		$result = $this->Users->save($data, array('validate' => false));
-		$this->assertTrue((bool)$result);
-
-		// Now it will still work
-		$this->Users->create();
-		$data = array(
-			'id' => $uid,
-			'pwd_current' => 'foobar',
-			'pwd' => '123456',
-			'pwd_repeat' => '123456'
-		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
-		$this->assertFalse($result);
-
-		// Lets set a valid BC password (without password_hash() method but Security class)
-		// But the provided pwd is incorrect
-		$data = array(
-			'id' => $uid,
-			'password' => $oldHash,
-		);
-		$result = $this->Users->save($data, array('validate' => false));
+		$this->Users->patchEntity($user, $data);
+		$result = $this->Users->save($user);
 		$this->assertTrue((bool)$result);
-
-		// Now it will still work
-		$this->Users->create();
-		$data = array(
-			'id' => $uid,
-			'pwd_current' => 'foobarx',
-			'pwd' => '123456',
-			'pwd_repeat' => '123456'
-		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
-		$this->assertFalse($result);
 	}
 
 	/**
@@ -699,13 +569,11 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testNeedsPasswordRehash() {
-		$this->skipIf(!function_exists('password_hash'), 'password_hash() is not available.');
-
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'allowSame' => false,
 			'current' => false,
 			'authType' => 'Blowfish',
-			'passwordHasher' => 'Tools.Modern'
+			'passwordHasher' => 'Default'
 		));
 
 		$hash =  password_hash('foobar', PASSWORD_BCRYPT);
@@ -723,7 +591,7 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testNeedsPasswordRehashWithNotSupportedHasher() {
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'allowSame' => false,
 			'current' => false,
 			'authType' => 'Blowfish',
@@ -733,11 +601,12 @@ class PasswordableBehaviorTest extends TestCase {
 		$result = $this->Users->needsPasswordRehash($hash);
 		$this->assertFalse($result);
 
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->removeBehavior('Passwordable');
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'allowSame' => false,
 			'current' => false,
 			'authType' => 'Blowfish',
-			'passwordHasher' => 'Simple'
+			'passwordHasher' => 'Default'
 		));
 
 		$hash =  password_hash('foobar', PASSWORD_BCRYPT);
@@ -752,34 +621,34 @@ class PasswordableBehaviorTest extends TestCase {
 	 */
 	public function testSettings() {
 		// Pwd min and max length
-		$this->Users->Behaviors->load('Tools.Passwordable', array(
+		$this->Users->addBehavior('Tools.Passwordable', array(
 			'allowSame' => false,
 			'current' => false,
 			'minLength' => 3,
 			'maxLength' => 6,
 		));
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => '123',
 			'pwd_repeat' => '123'
 		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$result = $this->Users->save($user);
 		$this->assertTrue((bool)$result);
 
-		$this->Users->create();
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => '12345678',
 			'pwd_repeat' => '12345678'
 		);
-		$this->Users->set($data);
-		$result = $this->Users->save();
+		$this->Users->patchEntity($user, $data);
+		$result = $this->Users->save($user);
 		$this->assertFalse($result);
 		$expected = array(
-			'pwd' => array(__d('tools', 'valErrBetweenCharacters %s %s', 3, 6)),
-			'pwd_repeat' => array(__d('tools', 'valErrBetweenCharacters %s %s', 3, 6))
+			'pwd' => array('between' => __d('tools', 'valErrBetweenCharacters {0} {1}', 3, 6)),
+			'pwd_repeat' => array('between' =>__d('tools', 'valErrBetweenCharacters {0} {1}', 3, 6))
 		);
-		$this->assertEquals($expected, $this->Users->validationErrors);
+		$this->assertEquals($expected, $user->errors());
 	}
 
 	/**
@@ -788,30 +657,27 @@ class PasswordableBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testSaveWithValidateFalse() {
-		$this->Users->Behaviors->load('Tools.Passwordable');
-		$this->Users->create();
+		$this->Users->addBehavior('Tools.Passwordable');
+		$user = $this->Users->newEntity();
 		$data = array(
 			'pwd' => '123',
 		);
-		$this->Users->set($data);
-		$result = $this->Users->save(null, array('validate' => false));
+		$this->Users->patchEntity($user, $data);
+		$result = $this->Users->save($user, array('validate' => false));
 		$this->assertTrue((bool)$result);
 
-		$uid = (string)$this->Users->id;
+		$uid = (string)$user->id;
+		$hash = $user['password'];
 
 		$data = array(
 			'id' => $uid,
 			'pwd' => '1234'
 		);
-		$this->Users->set($data);
-		$result2 = $this->Users->save(null, array('validate' => false));
+		$this->Users->patchEntity($user, $data);
+		$result2 = $this->Users->save($user, array('validate' => false));
 		$this->assertTrue((bool)$result2);
-
-		$this->assertTrue($result['ToolsUser']['password'] !== $result2['ToolsUser']['password']);
+		$hash2 = $user['password'];
+		$this->assertTrue($hash !== $hash2);
 	}
 
 }
-
-class ComplexPasswordHasher extends DefaultPasswordHasher {
-
-}