From 43413f029eef41922d3c3b407742d6a1befa8310 Mon Sep 17 00:00:00 2001 From: Sebastien Barre Date: Wed, 5 Nov 2014 12:34:25 -0500 Subject: [PATCH] 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 165dee00ca6..bde9c206fd2 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -608,6 +608,9 @@ public function login($user = null) { 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 29266cf1f37..260b0ec3c76 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -266,6 +266,27 @@ public function redirect($url, $status = null, $exit = true) { } +/** + * 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 @@ public function testLogin() { $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);