From 0cdb93b265ca4cdbf4f6fbb0e4a79bad80956aca Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 12:34:25 -0500 Subject: [PATCH 01/11] Ticket 5041: have Auth::login() send Auth.afterIdentify event --- .../Controller/Component/AuthComponent.php | 3 ++ .../Component/AuthComponentTest.php | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/Cake/Controller/Component/AuthComponent.php b/lib/Cake/Controller/Component/AuthComponent.php index b2730a56f..71cb7c008 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -608,6 +608,9 @@ class AuthComponent extends Component { if ($user) { $this->Session->renew(); $this->Session->write(self::$sessionKey, $user); + App::uses('CakeEvent', 'Event'); + $event = new CakeEvent('Auth.afterIdentify', $this, array('user' => $user)); + $this->_Collection->getController()->getEventManager()->dispatch($event); } return (bool)$this->user(); } diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index 29266cf1f..260b0ec3c 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -266,6 +266,27 @@ class AjaxAuthController extends Controller { } +/** + * Mock class used to test event dispatching + * + * @package Cake.Test.Case.Event + */ +class CakeEventTestListener { + + public $callStack = array(); + +/** + * Test function to be used in event dispatching + * + * @return void + */ + public function listenerFunction() { + $this->callStack[] = __FUNCTION__; + } + +} + + /** * AuthComponentTest class * @@ -404,6 +425,13 @@ class AuthComponentTest extends CakeTestCase { $this->Auth->Session->expects($this->once()) ->method('renew'); + $manager = $this->Controller->getEventManager(); + $listener = $this->getMock('CakeEventTestListener'); + $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); + App::uses('CakeEvent', 'Event'); + $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); + $listener->expects($this->once())->method('listenerFunction')->with($event); + $result = $this->Auth->login(); $this->assertTrue($result); From 4c59ab6ecade090a51e026dbdc9e11e6e84d10e6 Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 13:36:29 -0500 Subject: [PATCH 02/11] Move App::uses(), rename helper class to avoid conflict --- .../Test/Case/Controller/Component/AuthComponentTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index 260b0ec3c..56c989ecb 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -20,6 +20,7 @@ App::uses('Controller', 'Controller'); App::uses('AuthComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component'); App::uses('FormAuthenticate', 'Controller/Component/Auth'); +App::uses('CakeEvent', 'Event'); /** * TestAuthComponent class @@ -271,7 +272,7 @@ class AjaxAuthController extends Controller { * * @package Cake.Test.Case.Event */ -class CakeEventTestListener { +class AuthEventTestListener { public $callStack = array(); @@ -426,9 +427,8 @@ class AuthComponentTest extends CakeTestCase { ->method('renew'); $manager = $this->Controller->getEventManager(); - $listener = $this->getMock('CakeEventTestListener'); + $listener = $this->getMock('AuthEventTestListener'); $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); - App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $listener->expects($this->once())->method('listenerFunction')->with($event); From f78e6c06213b94796d7a5d9d32b1697ea26fcf95 Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 13:46:45 -0500 Subject: [PATCH 03/11] Move App::uses() again --- lib/Cake/Controller/Component/AuthComponent.php | 2 +- lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Controller/Component/AuthComponent.php b/lib/Cake/Controller/Component/AuthComponent.php index 71cb7c008..1d9915662 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -26,6 +26,7 @@ App::uses('Hash', 'Utility'); App::uses('CakeSession', 'Model/Datasource'); App::uses('BaseAuthorize', 'Controller/Component/Auth'); App::uses('BaseAuthenticate', 'Controller/Component/Auth'); +App::uses('CakeEvent', 'Event'); /** * Authentication control component class @@ -608,7 +609,6 @@ class AuthComponent extends Component { if ($user) { $this->Session->renew(); $this->Session->write(self::$sessionKey, $user); - App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this, array('user' => $user)); $this->_Collection->getController()->getEventManager()->dispatch($event); } diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index 56c989ecb..e607cdd8f 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -20,7 +20,6 @@ App::uses('Controller', 'Controller'); App::uses('AuthComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component'); App::uses('FormAuthenticate', 'Controller/Component/Auth'); -App::uses('CakeEvent', 'Event'); /** * TestAuthComponent class @@ -429,6 +428,7 @@ class AuthComponentTest extends CakeTestCase { $manager = $this->Controller->getEventManager(); $listener = $this->getMock('AuthEventTestListener'); $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); + App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $listener->expects($this->once())->method('listenerFunction')->with($event); From ee73c1732bbe2990e1e62e685312f52e506c221b Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 18:03:26 -0500 Subject: [PATCH 04/11] Have BaseAuthenticate implement CakeEventListener instead --- .../Component/Auth/BaseAuthenticate.php | 12 ++- .../Controller/Component/AuthComponent.php | 4 +- .../Component/AuthComponentTest.php | 83 ++++++++++++++++++- 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php b/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php index 77629ee39..df9d3c06e 100644 --- a/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php +++ b/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php @@ -14,13 +14,14 @@ App::uses('Security', 'Utility'); App::uses('Hash', 'Utility'); +App::uses('CakeEventListener', 'Event'); /** * Base Authentication class with common methods and properties. * * @package Cake.Controller.Component.Auth */ -abstract class BaseAuthenticate { +abstract class BaseAuthenticate implements CakeEventListener { /** * Settings for this object. @@ -65,6 +66,15 @@ abstract class BaseAuthenticate { */ protected $_passwordHasher; +/** + * Implemented events + * + * @return array of events => callbacks. + */ + public function implementedEvents() { + return array(); + } + /** * Constructor * diff --git a/lib/Cake/Controller/Component/AuthComponent.php b/lib/Cake/Controller/Component/AuthComponent.php index 1d9915662..3d117f0ac 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -799,7 +799,9 @@ class AuthComponent extends Component { throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()')); } $settings = array_merge($global, (array)$settings); - $this->_authenticateObjects[] = new $className($this->_Collection, $settings); + $auth = new $className($this->_Collection, $settings); + $this->_Collection->getController()->getEventManager()->attach($auth); + $this->_authenticateObjects[] = $auth; } return $this->_authenticateObjects; } diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index e607cdd8f..adcf27775 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -19,8 +19,53 @@ App::uses('Controller', 'Controller'); App::uses('AuthComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component'); +App::uses('BaseAuthenticate', 'Controller/Component/Auth'); App::uses('FormAuthenticate', 'Controller/Component/Auth'); +/** + * TestFormAuthenticate class + * + * @package Cake.Test.Case.Controller.Component + */ +class TestBaseAuthenticate extends BaseAuthenticate { + +/** + * Implemented events + * + * @return array of events => callbacks. + */ + public function implementedEvents() { + return array( + 'Auth.afterIdentify' => 'afterIdentify' + ); + } + + public $afterIdentifyCallable = null; + +/** + * Test function to be used in event dispatching + * + * @return void + */ + public function afterIdentify($event) { + call_user_func($this->afterIdentifyCallable, $event); + } + +/** + * Authenticate a user based on the request information. + * + * @param CakeRequest $request Request to get authentication information from. + * @param CakeResponse $response A response object that can have headers added. + * @return mixed Either false on failure, or an array of user data on success. + */ + public function authenticate(CakeRequest $request, CakeResponse $response) { + return array( + 'id' => 1, + 'username' => 'mark' + ); + } +} + /** * TestAuthComponent class * @@ -46,6 +91,17 @@ class TestAuthComponent extends AuthComponent { $this->_authenticateObjects[$index] = $object; } +/** + * Helper method to get an authenticate object instance + * + * @param int $index The index at which to get the object + * @return Object $object + */ + public function getAuthenticateObject($index) { + $this->constructAuthenticate(); + return isset($this->_authenticateObjects[$index]) ? $this->_authenticateObjects[$index] : null; + } + /** * Helper method to add/set an authorize object instance * @@ -425,16 +481,37 @@ class AuthComponentTest extends CakeTestCase { $this->Auth->Session->expects($this->once()) ->method('renew'); - $manager = $this->Controller->getEventManager(); + $result = $this->Auth->login(); + $this->assertTrue($result); + + $this->assertTrue($this->Auth->loggedIn()); + $this->assertEquals($user, $this->Auth->user()); + } + +/** + * testLogin afterIdentify event method + * + * @return void + */ + public function testLoginAfterIdentify() { + $this->Auth->authenticate = array( + 'TestBase', + ); + + $user = array( + 'id' => 1, + 'username' => 'mark' + ); + + $auth = $this->Auth->getAuthenticateObject(0); $listener = $this->getMock('AuthEventTestListener'); - $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); + $auth->afterIdentifyCallable = array($listener, 'listenerFunction'); App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $listener->expects($this->once())->method('listenerFunction')->with($event); $result = $this->Auth->login(); $this->assertTrue($result); - $this->assertTrue($this->Auth->loggedIn()); $this->assertEquals($user, $this->Auth->user()); } From 4bada050284276cba663ab0a9763b8389794fc8a Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 18:37:20 -0500 Subject: [PATCH 05/11] Fix doc/style --- .../Test/Case/Controller/Component/AuthComponentTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index adcf27775..411873148 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -56,7 +56,7 @@ class TestBaseAuthenticate extends BaseAuthenticate { * * @param CakeRequest $request Request to get authentication information from. * @param CakeResponse $response A response object that can have headers added. - * @return mixed Either false on failure, or an array of user data on success. + * @return array|bool Either false on failure, or an array of user data on success. */ public function authenticate(CakeRequest $request, CakeResponse $response) { return array( @@ -84,7 +84,7 @@ class TestAuthComponent extends AuthComponent { * Helper method to add/set an authenticate object instance * * @param int $index The index at which to add/set the object - * @param Object $object The object to add/set + * @param object $object The object to add/set * @return void */ public function setAuthenticateObject($index, $object) { @@ -95,10 +95,10 @@ class TestAuthComponent extends AuthComponent { * Helper method to get an authenticate object instance * * @param int $index The index at which to get the object - * @return Object $object + * @return object $object */ public function getAuthenticateObject($index) { - $this->constructAuthenticate(); + $this->constructAuthenticate(); return isset($this->_authenticateObjects[$index]) ? $this->_authenticateObjects[$index] : null; } From 43413f029eef41922d3c3b407742d6a1befa8310 Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 12:34:25 -0500 Subject: [PATCH 06/11] Ticket 5041: have Auth::login() send Auth.afterIdentify event --- .../Controller/Component/AuthComponent.php | 3 ++ .../Component/AuthComponentTest.php | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/Cake/Controller/Component/AuthComponent.php b/lib/Cake/Controller/Component/AuthComponent.php index 165dee00c..bde9c206f 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -608,6 +608,9 @@ class AuthComponent extends Component { if ($user) { $this->Session->renew(); $this->Session->write(self::$sessionKey, $user); + App::uses('CakeEvent', 'Event'); + $event = new CakeEvent('Auth.afterIdentify', $this, array('user' => $user)); + $this->_Collection->getController()->getEventManager()->dispatch($event); } return (bool)$this->user(); } diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index 29266cf1f..260b0ec3c 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -266,6 +266,27 @@ class AjaxAuthController extends Controller { } +/** + * Mock class used to test event dispatching + * + * @package Cake.Test.Case.Event + */ +class CakeEventTestListener { + + public $callStack = array(); + +/** + * Test function to be used in event dispatching + * + * @return void + */ + public function listenerFunction() { + $this->callStack[] = __FUNCTION__; + } + +} + + /** * AuthComponentTest class * @@ -404,6 +425,13 @@ class AuthComponentTest extends CakeTestCase { $this->Auth->Session->expects($this->once()) ->method('renew'); + $manager = $this->Controller->getEventManager(); + $listener = $this->getMock('CakeEventTestListener'); + $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); + App::uses('CakeEvent', 'Event'); + $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); + $listener->expects($this->once())->method('listenerFunction')->with($event); + $result = $this->Auth->login(); $this->assertTrue($result); From d7b353dcf929b53d63869aeb179dc6635e873d1e Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 13:36:29 -0500 Subject: [PATCH 07/11] Move App::uses(), rename helper class to avoid conflict --- .../Test/Case/Controller/Component/AuthComponentTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index 260b0ec3c..56c989ecb 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -20,6 +20,7 @@ App::uses('Controller', 'Controller'); App::uses('AuthComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component'); App::uses('FormAuthenticate', 'Controller/Component/Auth'); +App::uses('CakeEvent', 'Event'); /** * TestAuthComponent class @@ -271,7 +272,7 @@ class AjaxAuthController extends Controller { * * @package Cake.Test.Case.Event */ -class CakeEventTestListener { +class AuthEventTestListener { public $callStack = array(); @@ -426,9 +427,8 @@ class AuthComponentTest extends CakeTestCase { ->method('renew'); $manager = $this->Controller->getEventManager(); - $listener = $this->getMock('CakeEventTestListener'); + $listener = $this->getMock('AuthEventTestListener'); $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); - App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $listener->expects($this->once())->method('listenerFunction')->with($event); From 50e5b5e8febca070562f37f3c8105e2042f5d630 Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 13:46:45 -0500 Subject: [PATCH 08/11] Move App::uses() again --- lib/Cake/Controller/Component/AuthComponent.php | 2 +- lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Controller/Component/AuthComponent.php b/lib/Cake/Controller/Component/AuthComponent.php index bde9c206f..f19f73394 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -26,6 +26,7 @@ App::uses('Hash', 'Utility'); App::uses('CakeSession', 'Model/Datasource'); App::uses('BaseAuthorize', 'Controller/Component/Auth'); App::uses('BaseAuthenticate', 'Controller/Component/Auth'); +App::uses('CakeEvent', 'Event'); /** * Authentication control component class @@ -608,7 +609,6 @@ class AuthComponent extends Component { if ($user) { $this->Session->renew(); $this->Session->write(self::$sessionKey, $user); - App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this, array('user' => $user)); $this->_Collection->getController()->getEventManager()->dispatch($event); } diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index 56c989ecb..e607cdd8f 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -20,7 +20,6 @@ App::uses('Controller', 'Controller'); App::uses('AuthComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component'); App::uses('FormAuthenticate', 'Controller/Component/Auth'); -App::uses('CakeEvent', 'Event'); /** * TestAuthComponent class @@ -429,6 +428,7 @@ class AuthComponentTest extends CakeTestCase { $manager = $this->Controller->getEventManager(); $listener = $this->getMock('AuthEventTestListener'); $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); + App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $listener->expects($this->once())->method('listenerFunction')->with($event); From 7da48669c83781c23790213690719f229568239b Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 18:03:26 -0500 Subject: [PATCH 09/11] Have BaseAuthenticate implement CakeEventListener instead --- .../Component/Auth/BaseAuthenticate.php | 12 ++- .../Controller/Component/AuthComponent.php | 4 +- .../Component/AuthComponentTest.php | 83 ++++++++++++++++++- 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php b/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php index 77629ee39..df9d3c06e 100644 --- a/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php +++ b/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php @@ -14,13 +14,14 @@ App::uses('Security', 'Utility'); App::uses('Hash', 'Utility'); +App::uses('CakeEventListener', 'Event'); /** * Base Authentication class with common methods and properties. * * @package Cake.Controller.Component.Auth */ -abstract class BaseAuthenticate { +abstract class BaseAuthenticate implements CakeEventListener { /** * Settings for this object. @@ -65,6 +66,15 @@ abstract class BaseAuthenticate { */ protected $_passwordHasher; +/** + * Implemented events + * + * @return array of events => callbacks. + */ + public function implementedEvents() { + return array(); + } + /** * Constructor * diff --git a/lib/Cake/Controller/Component/AuthComponent.php b/lib/Cake/Controller/Component/AuthComponent.php index f19f73394..7d8ab61a9 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -799,7 +799,9 @@ class AuthComponent extends Component { throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()')); } $settings = array_merge($global, (array)$settings); - $this->_authenticateObjects[] = new $className($this->_Collection, $settings); + $auth = new $className($this->_Collection, $settings); + $this->_Collection->getController()->getEventManager()->attach($auth); + $this->_authenticateObjects[] = $auth; } return $this->_authenticateObjects; } diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index e607cdd8f..adcf27775 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -19,8 +19,53 @@ App::uses('Controller', 'Controller'); App::uses('AuthComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component'); +App::uses('BaseAuthenticate', 'Controller/Component/Auth'); App::uses('FormAuthenticate', 'Controller/Component/Auth'); +/** + * TestFormAuthenticate class + * + * @package Cake.Test.Case.Controller.Component + */ +class TestBaseAuthenticate extends BaseAuthenticate { + +/** + * Implemented events + * + * @return array of events => callbacks. + */ + public function implementedEvents() { + return array( + 'Auth.afterIdentify' => 'afterIdentify' + ); + } + + public $afterIdentifyCallable = null; + +/** + * Test function to be used in event dispatching + * + * @return void + */ + public function afterIdentify($event) { + call_user_func($this->afterIdentifyCallable, $event); + } + +/** + * Authenticate a user based on the request information. + * + * @param CakeRequest $request Request to get authentication information from. + * @param CakeResponse $response A response object that can have headers added. + * @return mixed Either false on failure, or an array of user data on success. + */ + public function authenticate(CakeRequest $request, CakeResponse $response) { + return array( + 'id' => 1, + 'username' => 'mark' + ); + } +} + /** * TestAuthComponent class * @@ -46,6 +91,17 @@ class TestAuthComponent extends AuthComponent { $this->_authenticateObjects[$index] = $object; } +/** + * Helper method to get an authenticate object instance + * + * @param int $index The index at which to get the object + * @return Object $object + */ + public function getAuthenticateObject($index) { + $this->constructAuthenticate(); + return isset($this->_authenticateObjects[$index]) ? $this->_authenticateObjects[$index] : null; + } + /** * Helper method to add/set an authorize object instance * @@ -425,16 +481,37 @@ class AuthComponentTest extends CakeTestCase { $this->Auth->Session->expects($this->once()) ->method('renew'); - $manager = $this->Controller->getEventManager(); + $result = $this->Auth->login(); + $this->assertTrue($result); + + $this->assertTrue($this->Auth->loggedIn()); + $this->assertEquals($user, $this->Auth->user()); + } + +/** + * testLogin afterIdentify event method + * + * @return void + */ + public function testLoginAfterIdentify() { + $this->Auth->authenticate = array( + 'TestBase', + ); + + $user = array( + 'id' => 1, + 'username' => 'mark' + ); + + $auth = $this->Auth->getAuthenticateObject(0); $listener = $this->getMock('AuthEventTestListener'); - $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); + $auth->afterIdentifyCallable = array($listener, 'listenerFunction'); App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $listener->expects($this->once())->method('listenerFunction')->with($event); $result = $this->Auth->login(); $this->assertTrue($result); - $this->assertTrue($this->Auth->loggedIn()); $this->assertEquals($user, $this->Auth->user()); } From a2e7896038c1002b83c2ca2eadff551b9f88022b Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Sat, 22 Nov 2014 13:30:27 -0500 Subject: [PATCH 10/11] Fix uses, style --- lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index adcf27775..482041fe6 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -21,6 +21,7 @@ App::uses('AuthComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component'); App::uses('BaseAuthenticate', 'Controller/Component/Auth'); App::uses('FormAuthenticate', 'Controller/Component/Auth'); +App::uses('CakeEvent', 'Event'); /** * TestFormAuthenticate class @@ -64,6 +65,7 @@ class TestBaseAuthenticate extends BaseAuthenticate { 'username' => 'mark' ); } + } /** @@ -506,7 +508,6 @@ class AuthComponentTest extends CakeTestCase { $auth = $this->Auth->getAuthenticateObject(0); $listener = $this->getMock('AuthEventTestListener'); $auth->afterIdentifyCallable = array($listener, 'listenerFunction'); - App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $listener->expects($this->once())->method('listenerFunction')->with($event); From 20e2882bf6458027d1163778f4519e3d64632086 Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Sun, 23 Nov 2014 21:49:29 -0500 Subject: [PATCH 11/11] Remove duplicate class declaration --- .../Component/AuthComponentTest.php | 44 ------------------- 1 file changed, 44 deletions(-) diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index 5240d3bbb..89341b4e0 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -68,50 +68,6 @@ class TestBaseAuthenticate extends BaseAuthenticate { } -/** - * TestFormAuthenticate class - * - * @package Cake.Test.Case.Controller.Component - */ -class TestBaseAuthenticate extends BaseAuthenticate { - -/** - * Implemented events - * - * @return array of events => callbacks. - */ - public function implementedEvents() { - return array( - 'Auth.afterIdentify' => 'afterIdentify' - ); - } - - public $afterIdentifyCallable = null; - -/** - * Test function to be used in event dispatching - * - * @return void - */ - public function afterIdentify($event) { - call_user_func($this->afterIdentifyCallable, $event); - } - -/** - * Authenticate a user based on the request information. - * - * @param CakeRequest $request Request to get authentication information from. - * @param CakeResponse $response A response object that can have headers added. - * @return array|bool Either false on failure, or an array of user data on success. - */ - public function authenticate(CakeRequest $request, CakeResponse $response) { - return array( - 'id' => 1, - 'username' => 'mark' - ); - } -} - /** * TestAuthComponent class *