From d4f79f5737b8a61d35c78dc5d85cc7d0c50a307f Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Thu, 19 Jan 2017 22:42:58 +0300 Subject: [PATCH 1/4] Emit postSetEnabled. Closes #23970 --- lib/private/User/User.php | 3 +++ tests/lib/User/UserTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/private/User/User.php b/lib/private/User/User.php index c161f64b2977..406920f605d3 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -337,6 +337,9 @@ public function setEnabled($enabled) { $this->enabled = $enabled; $enabled = ($enabled) ? 'true' : 'false'; $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled); + if ($this->emitter) { + $this->emitter->emit('\OC\User', 'postSetEnabled', [$this, $enabled]); + } } /** diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php index b25f390df136..ccbfcd6ebd35 100644 --- a/tests/lib/User/UserTest.php +++ b/tests/lib/User/UserTest.php @@ -502,6 +502,35 @@ public function testDeleteHooks() { $this->assertEquals(2, $hooksCalled); } + public function testSetEnabledHook(){ + $hooksCalled = 0; + $test = $this; + + /** + * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->createMock(Dummy::class); + + /** + * @param User $user + * @param bool $enabled + */ + $hook = function ($user, $enabled) use ($test, &$hooksCalled) { + $hooksCalled++; + $expectedState = ($user->isEnabled()) ? 'true' : 'false'; + $test->assertEquals($expectedState, $enabled); + $test->assertEquals('foo', $user->getUID()); + }; + + $emitter = new PublicEmitter(); + $emitter->listen('\OC\User', 'postSetEnabled', $hook); + + $user = new User('foo', $backend, $emitter); + $user->setEnabled(true); + $user->setEnabled(false); + $this->assertEquals(2, $hooksCalled); + } + public function testGetCloudId() { /** * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend From d8c2c63f5f7f109987df20d5b302d190a1b9fd8b Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Tue, 24 Jan 2017 00:10:51 +0300 Subject: [PATCH 2/4] Dispatch postSetEnabled --- lib/private/User/User.php | 7 ++++--- tests/lib/User/UserTest.php | 16 +++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 406920f605d3..cc529b9d7f42 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -39,6 +39,7 @@ use OCP\IConfig; use OCP\UserInterface; use \OCP\IUserBackend; +use Symfony\Component\EventDispatcher\GenericEvent; class User implements IUser { /** @var string $uid */ @@ -337,9 +338,9 @@ public function setEnabled($enabled) { $this->enabled = $enabled; $enabled = ($enabled) ? 'true' : 'false'; $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled); - if ($this->emitter) { - $this->emitter->emit('\OC\User', 'postSetEnabled', [$this, $enabled]); - } + + $eventDispatcher = \OC::$server->getEventDispatcher(); + $eventDispatcher->dispatch(self::class . '::postSetEnabled', new GenericEvent($this)); } /** diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php index ccbfcd6ebd35..e1dee4a22ace 100644 --- a/tests/lib/User/UserTest.php +++ b/tests/lib/User/UserTest.php @@ -14,6 +14,7 @@ use OC\User\Database; use OC\User\User; use OCP\IConfig; +use Symfony\Component\EventDispatcher\GenericEvent; use Test\TestCase; use Test\Util\User\Dummy; @@ -512,20 +513,17 @@ public function testSetEnabledHook(){ $backend = $this->createMock(Dummy::class); /** - * @param User $user - * @param bool $enabled + * @param GenericEvent $event */ - $hook = function ($user, $enabled) use ($test, &$hooksCalled) { + $hook = function ($event) use ($test, &$hooksCalled) { $hooksCalled++; - $expectedState = ($user->isEnabled()) ? 'true' : 'false'; - $test->assertEquals($expectedState, $enabled); - $test->assertEquals('foo', $user->getUID()); + $test->assertEquals('foo', $event->getSubject()->getUID()); }; - $emitter = new PublicEmitter(); - $emitter->listen('\OC\User', 'postSetEnabled', $hook); + $eventDispatcher = \OC::$server->getEventDispatcher(); + $eventDispatcher->addListener(User::class . '::postSetEnabled', $hook); - $user = new User('foo', $backend, $emitter); + $user = new User('foo', $backend, new PublicEmitter()); $user->setEnabled(true); $user->setEnabled(false); $this->assertEquals(2, $hooksCalled); From 48b4ec6b8fd8270fdb83ac53c7c203a43e91b0c6 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 27 Jan 2017 22:41:19 +0300 Subject: [PATCH 3/4] Do not use listener in test --- lib/private/User/User.php | 15 +++++++++++--- tests/lib/User/UserTest.php | 39 ++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/private/User/User.php b/lib/private/User/User.php index cc529b9d7f42..f75c5be17f43 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -39,6 +39,7 @@ use OCP\IConfig; use OCP\UserInterface; use \OCP\IUserBackend; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\GenericEvent; class User implements IUser { @@ -72,17 +73,24 @@ class User implements IUser { /** @var IURLGenerator */ private $urlGenerator; + /** @var EventDispatcher */ + private $eventDispatcher; + /** * @param string $uid * @param UserInterface $backend * @param \OC\Hooks\Emitter $emitter * @param IConfig|null $config * @param IURLGenerator $urlGenerator + * @param EventDispatcher $eventDispatcher */ - public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $urlGenerator = null) { + public function __construct($uid, $backend, $emitter = null, IConfig $config = null, + $urlGenerator = null, EventDispatcher $eventDispatcher = null + ) { $this->uid = $uid; $this->backend = $backend; $this->emitter = $emitter; + $this->eventDispatcher = $eventDispatcher; if(is_null($config)) { $config = \OC::$server->getConfig(); } @@ -339,8 +347,9 @@ public function setEnabled($enabled) { $enabled = ($enabled) ? 'true' : 'false'; $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled); - $eventDispatcher = \OC::$server->getEventDispatcher(); - $eventDispatcher->dispatch(self::class . '::postSetEnabled', new GenericEvent($this)); + if ($this->eventDispatcher){ + $this->eventDispatcher->dispatch(self::class . '::postSetEnabled', new GenericEvent($this)); + } } /** diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php index e1dee4a22ace..341bb56c9515 100644 --- a/tests/lib/User/UserTest.php +++ b/tests/lib/User/UserTest.php @@ -14,6 +14,7 @@ use OC\User\Database; use OC\User\User; use OCP\IConfig; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\GenericEvent; use Test\TestCase; use Test\Util\User\Dummy; @@ -504,29 +505,31 @@ public function testDeleteHooks() { } public function testSetEnabledHook(){ - $hooksCalled = 0; - $test = $this; - /** * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend */ $backend = $this->createMock(Dummy::class); - - /** - * @param GenericEvent $event - */ - $hook = function ($event) use ($test, &$hooksCalled) { - $hooksCalled++; - $test->assertEquals('foo', $event->getSubject()->getUID()); - }; - - $eventDispatcher = \OC::$server->getEventDispatcher(); - $eventDispatcher->addListener(User::class . '::postSetEnabled', $hook); - - $user = new User('foo', $backend, new PublicEmitter()); - $user->setEnabled(true); + $eventDispatcherMock = $this->createMock(EventDispatcher::class); + + $expectations = [true, false]; + $eventDispatcherMock->expects($this->exactly(2)) + ->method('dispatch') + ->with( + $this->callback( + function($eventName){ + if ($eventName === User::class . '::postSetEnabled' ){ + return true; + } + return false; + } + ), + $this->anything() + ) + ; + + $user = new User('foo', $backend, null, null, null, $eventDispatcherMock); $user->setEnabled(false); - $this->assertEquals(2, $hooksCalled); + $user->setEnabled(true); } public function testGetCloudId() { From 50c134eab658af8c8ae30c1e41a34ce9b9e96002 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Tue, 31 Jan 2017 19:49:34 +0300 Subject: [PATCH 4/4] Pass EventDispatcher --- lib/private/User/Manager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index badbd26de754..a1986d8261a3 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -158,7 +158,7 @@ protected function getUserObject($uid, $backend, $cacheUser = true) { } } - $user = new User($uid, $backend, $this, $this->config); + $user = new User($uid, $backend, $this, $this->config, null, \OC::$server->getEventDispatcher() ); if ($cacheUser) { $this->cachedUsers[$uid] = $user; }