From ad12a740cb619d701de332eedda91dcbb1f53047 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 25 Jul 2023 21:45:07 +0200 Subject: [PATCH 1/3] fix!: Remove symfony EventDispatcherInterface from User Signed-off-by: Joas Schilling --- .../GlobalStoragesControllerTest.php | 4 +- .../Controller/UserStoragesControllerTest.php | 4 +- .../Service/UserGlobalStoragesServiceTest.php | 3 +- apps/user_ldap/tests/LDAPProviderTest.php | 2 - .../Collaboration/Resources/Listener.php | 6 +-- lib/private/Server.php | 17 +----- lib/private/User/Manager.php | 8 +-- lib/private/User/User.php | 29 +++------- tests/lib/Files/Cache/CacheTest.php | 3 +- .../lib/Files/Cache/Wrapper/CacheJailTest.php | 4 +- tests/lib/Files/Config/UserMountCacheTest.php | 3 +- .../Files/Storage/Wrapper/EncryptionTest.php | 4 -- tests/lib/Files/Stream/EncryptionTest.php | 2 - tests/lib/Group/GroupTest.php | 16 +++--- tests/lib/Group/ManagerTest.php | 6 ++- tests/lib/User/DatabaseTest.php | 5 +- tests/lib/User/ManagerTest.php | 54 +++++++++---------- tests/lib/User/SessionTest.php | 14 ++--- tests/lib/User/UserTest.php | 14 ++--- 19 files changed, 77 insertions(+), 121 deletions(-) diff --git a/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php b/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php index 0963cd06f0015..5536ae80006c1 100644 --- a/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php +++ b/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php @@ -28,13 +28,13 @@ use OC\User\User; use OCA\Files_External\Controller\GlobalStoragesController; use OCA\Files_External\Service\BackendService; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; use OCP\ILogger; use OCP\IRequest; use OCP\IUserSession; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; class GlobalStoragesControllerTest extends StoragesControllerTest { protected function setUp(): void { @@ -53,7 +53,7 @@ protected function setUp(): void { private function createController($allowCreateLocal = true) { $session = $this->createMock(IUserSession::class); $session->method('getUser') - ->willReturn(new User('test', null, $this->createMock(EventDispatcherInterface::class))); + ->willReturn(new User('test', null, $this->createMock(IEventDispatcher::class))); $config = $this->createMock(IConfig::class); $config->method('getSystemValue') diff --git a/apps/files_external/tests/Controller/UserStoragesControllerTest.php b/apps/files_external/tests/Controller/UserStoragesControllerTest.php index f4de39fcd2b5e..13efbf278b6d4 100644 --- a/apps/files_external/tests/Controller/UserStoragesControllerTest.php +++ b/apps/files_external/tests/Controller/UserStoragesControllerTest.php @@ -31,13 +31,13 @@ use OCA\Files_External\Lib\StorageConfig; use OCA\Files_External\Service\BackendService; use OCP\AppFramework\Http; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; use OCP\ILogger; use OCP\IRequest; use OCP\IUserSession; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; class UserStoragesControllerTest extends StoragesControllerTest { @@ -61,7 +61,7 @@ protected function setUp(): void { private function createController($allowCreateLocal = true) { $session = $this->createMock(IUserSession::class); $session->method('getUser') - ->willReturn(new User('test', null, $this->createMock(EventDispatcherInterface::class))); + ->willReturn(new User('test', null, $this->createMock(IEventDispatcher::class))); $config = $this->createMock(IConfig::class); $config->method('getSystemValue') diff --git a/apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php b/apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php index aa5aa1df431e0..5e54e8acb302f 100644 --- a/apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php +++ b/apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php @@ -31,6 +31,7 @@ use OCA\Files_External\NotFoundException; use OCA\Files_External\Service\StoragesService; use OCA\Files_External\Service\UserGlobalStoragesService; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IGroupManager; use OCP\IUser; use OCP\IUserSession; @@ -66,7 +67,7 @@ protected function setUp(): void { $this->globalStoragesService = $this->service; - $this->user = new \OC\User\User(self::USER_ID, null, \OC::$server->getEventDispatcher()); + $this->user = new \OC\User\User(self::USER_ID, null, \OC::$server->get(IEventDispatcher::class)); /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject $userSession */ $userSession = $this->createMock(IUserSession::class); $userSession diff --git a/apps/user_ldap/tests/LDAPProviderTest.php b/apps/user_ldap/tests/LDAPProviderTest.php index 8a071119d16a6..3e34ce7bdbb14 100644 --- a/apps/user_ldap/tests/LDAPProviderTest.php +++ b/apps/user_ldap/tests/LDAPProviderTest.php @@ -39,7 +39,6 @@ use OCP\ICacheFactory; use OCP\IConfig; use OCP\IServerContainer; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Class LDAPProviderTest @@ -76,7 +75,6 @@ private function getUserManagerMock(IUserLDAP $userBackend) { ->setMethods(['getBackends']) ->setConstructorArgs([ $this->createMock(IConfig::class), - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), ]) diff --git a/lib/private/Collaboration/Resources/Listener.php b/lib/private/Collaboration/Resources/Listener.php index d3b2f012a2916..2263f5d3b1a83 100644 --- a/lib/private/Collaboration/Resources/Listener.php +++ b/lib/private/Collaboration/Resources/Listener.php @@ -32,6 +32,7 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\IGroup; use OCP\IUser; +use OCP\User\Events\UserDeletedEvent; use Symfony\Component\EventDispatcher\GenericEvent; class Listener { @@ -47,9 +48,8 @@ public static function register(SymfonyAdapter $symfonyDispatcher, IEventDispatc $symfonyDispatcher->addListener(IGroup::class . '::postAddUser', $listener); $symfonyDispatcher->addListener(IGroup::class . '::postRemoveUser', $listener); - $symfonyDispatcher->addListener(IUser::class . '::postDelete', function (GenericEvent $event) { - /** @var IUser $user */ - $user = $event->getSubject(); + $eventDispatcher->addListener(UserDeletedEvent::class, function (UserDeletedEvent $event) { + $user = $event->getUser(); /** @var IManager $resourceManager */ $resourceManager = \OCP\Server::get(IManager::class); diff --git a/lib/private/Server.php b/lib/private/Server.php index 86f5192a39df9..6c7dcb730ebd0 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -566,8 +566,6 @@ public function __construct($webRoot, \OC\Config $config) { $provider = null; } - $legacyDispatcher = $c->get(SymfonyAdapter::class); - $userSession = new \OC\User\Session( $manager, $session, @@ -589,10 +587,9 @@ public function __construct($webRoot, \OC\Config $config) { \OC_Hook::emit('OC_User', 'post_createUser', ['uid' => $user->getUID(), 'password' => $password]); }); /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */ - $userSession->listen('\OC\User', 'preDelete', function ($user) use ($legacyDispatcher) { + $userSession->listen('\OC\User', 'preDelete', function ($user) { /** @var \OC\User\User $user */ \OC_Hook::emit('OC_User', 'pre_deleteUser', ['run' => true, 'uid' => $user->getUID()]); - $legacyDispatcher->dispatch('OCP\IUser::preDelete', new GenericEvent($user)); }); /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */ $userSession->listen('\OC\User', 'postDelete', function ($user) { @@ -602,18 +599,10 @@ public function __construct($webRoot, \OC\Config $config) { $userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) { /** @var \OC\User\User $user */ \OC_Hook::emit('OC_User', 'pre_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]); - - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new BeforePasswordUpdatedEvent($user, $password, $recoveryPassword)); }); $userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) { /** @var \OC\User\User $user */ \OC_Hook::emit('OC_User', 'post_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]); - - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new PasswordUpdatedEvent($user, $password, $recoveryPassword)); }); $userSession->listen('\OC\User', 'preLogin', function ($uid, $password) { \OC_Hook::emit('OC_User', 'pre_login', ['run' => true, 'uid' => $uid, 'password' => $password]); @@ -658,10 +647,6 @@ public function __construct($webRoot, \OC\Config $config) { $userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) { /** @var \OC\User\User $user */ \OC_Hook::emit('OC_User', 'changeUser', ['run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue]); - - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new UserChangedEvent($user, $feature, $value, $oldValue)); }); return $userSession; }); diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index 60059d5baddbe..fb1afb6582564 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -55,7 +55,6 @@ use OCP\User\Events\BeforeUserCreatedEvent; use OCP\User\Events\UserCreatedEvent; use OCP\UserInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Class Manager @@ -88,9 +87,6 @@ class Manager extends PublicEmitter implements IUserManager { /** @var IConfig */ private $config; - /** @var EventDispatcherInterface */ - private $dispatcher; - /** @var ICache */ private $cache; @@ -100,11 +96,9 @@ class Manager extends PublicEmitter implements IUserManager { private DisplayNameCache $displayNameCache; public function __construct(IConfig $config, - EventDispatcherInterface $oldDispatcher, ICacheFactory $cacheFactory, IEventDispatcher $eventDispatcher) { $this->config = $config; - $this->dispatcher = $oldDispatcher; $this->cache = new WithLocalCache($cacheFactory->createDistributed('user_backend_map')); $cachedUsers = &$this->cachedUsers; $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { @@ -211,7 +205,7 @@ public function getUserObject($uid, $backend, $cacheUser = true) { return $this->cachedUsers[$uid]; } - $user = new User($uid, $backend, $this->dispatcher, $this, $this->config); + $user = new User($uid, $backend, $this->eventDispatcher, $this, $this->config); if ($cacheUser) { $this->cachedUsers[$uid] = $user; } diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 2530173bea444..d1185e17aef42 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -49,7 +49,10 @@ use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserBackend; +use OCP\User\Events\BeforePasswordUpdatedEvent; use OCP\User\Events\BeforeUserDeletedEvent; +use OCP\User\Events\PasswordUpdatedEvent; +use OCP\User\Events\UserChangedEvent; use OCP\User\Events\UserDeletedEvent; use OCP\User\GetQuotaEvent; use OCP\User\Backend\ISetDisplayNameBackend; @@ -58,8 +61,6 @@ use OCP\User\Backend\IProvideEnabledStateBackend; use OCP\User\Backend\IGetHomeBackend; use OCP\UserInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; use function json_decode; use function json_encode; @@ -76,8 +77,6 @@ class User implements IUser { /** @var UserInterface|null */ private $backend; - /** @var EventDispatcherInterface */ - private $legacyDispatcher; /** @var IEventDispatcher */ private $dispatcher; @@ -103,10 +102,9 @@ class User implements IUser { /** @var IURLGenerator */ private $urlGenerator; - public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { + public function __construct(string $uid, ?UserInterface $backend, IEventDispatcher $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) { $this->uid = $uid; $this->backend = $backend; - $this->legacyDispatcher = $dispatcher; $this->emitter = $emitter; if (is_null($config)) { $config = \OC::$server->getConfig(); @@ -116,8 +114,7 @@ public function __construct(string $uid, ?UserInterface $backend, EventDispatche if (is_null($this->urlGenerator)) { $this->urlGenerator = \OC::$server->getURLGenerator(); } - // TODO: inject - $this->dispatcher = \OCP\Server::get(IEventDispatcher::class); + $this->dispatcher = $dispatcher; } /** @@ -325,10 +322,7 @@ public function delete() { * @return bool */ public function setPassword($password, $recoveryPassword = null) { - $this->legacyDispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [ - 'password' => $password, - 'recoveryPassword' => $recoveryPassword, - ])); + $this->dispatcher->dispatchTyped(new BeforePasswordUpdatedEvent($this, $password, $recoveryPassword)); if ($this->emitter) { $this->emitter->emit('\OC\User', 'preSetPassword', [$this, $password, $recoveryPassword]); } @@ -338,10 +332,7 @@ public function setPassword($password, $recoveryPassword = null) { $result = $backend->setPassword($this->uid, $password); if ($result !== false) { - $this->legacyDispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [ - 'password' => $password, - 'recoveryPassword' => $recoveryPassword, - ])); + $this->dispatcher->dispatchTyped(new PasswordUpdatedEvent($this, $password, $recoveryPassword)); if ($this->emitter) { $this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]); } @@ -622,11 +613,7 @@ private function removeProtocolFromUrl(string $url): string { } public function triggerChange($feature, $value = null, $oldValue = null) { - $this->legacyDispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [ - 'feature' => $feature, - 'value' => $value, - 'oldValue' => $oldValue, - ])); + $this->dispatcher->dispatchTyped(new UserChangedEvent($this, $feature, $value, $oldValue)); if ($this->emitter) { $this->emitter->emit('\OC\User', 'changeUser', [$this, $feature, $value, $oldValue]); } diff --git a/tests/lib/Files/Cache/CacheTest.php b/tests/lib/Files/Cache/CacheTest.php index 7f8e1af15774d..a5086a3eb3c65 100644 --- a/tests/lib/Files/Cache/CacheTest.php +++ b/tests/lib/Files/Cache/CacheTest.php @@ -12,6 +12,7 @@ use OC\Files\Cache\Cache; use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchQuery; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Search\ISearchComparison; use OCP\IUser; @@ -329,7 +330,7 @@ public function testSearchQueryByTag() { $userId = static::getUniqueID('user'); \OC::$server->getUserManager()->createUser($userId, $userId); static::loginAsUser($userId); - $user = new \OC\User\User($userId, null, \OC::$server->getEventDispatcher()); + $user = new \OC\User\User($userId, null, \OC::$server->get(IEventDispatcher::class)); $file1 = 'folder'; $file2 = 'folder/foobar'; diff --git a/tests/lib/Files/Cache/Wrapper/CacheJailTest.php b/tests/lib/Files/Cache/Wrapper/CacheJailTest.php index 4c3dce740878b..8d7e6536418db 100644 --- a/tests/lib/Files/Cache/Wrapper/CacheJailTest.php +++ b/tests/lib/Files/Cache/Wrapper/CacheJailTest.php @@ -12,8 +12,8 @@ use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchQuery; use OC\User\User; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Search\ISearchComparison; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\Files\Cache\CacheTest; /** @@ -85,7 +85,7 @@ public function testSearchQueryOutsideJail() { $this->sourceCache->put($file1, $data1); $this->sourceCache->put($file2, $data1); - $user = new User('foo', null, $this->createMock(EventDispatcherInterface::class)); + $user = new User('foo', null, $this->createMock(IEventDispatcher::class)); $query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foobar'), 10, 0, [], $user); $result = $this->cache->searchQuery($query); diff --git a/tests/lib/Files/Config/UserMountCacheTest.php b/tests/lib/Files/Config/UserMountCacheTest.php index a9a88cb85c92e..ccad4671ae91a 100644 --- a/tests/lib/Files/Config/UserMountCacheTest.php +++ b/tests/lib/Files/Config/UserMountCacheTest.php @@ -21,7 +21,6 @@ use OCP\IDBConnection; use OCP\IUserManager; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; use Test\Util\User\Dummy; @@ -62,7 +61,7 @@ protected function setUp(): void { ->expects($this->any()) ->method('getAppValue') ->willReturnArgument(2); - $this->userManager = new Manager($config, $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)); + $this->userManager = new Manager($config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class)); $userBackend = new Dummy(); $userBackend->createUser('u1', ''); $userBackend->createUser('u2', ''); diff --git a/tests/lib/Files/Storage/Wrapper/EncryptionTest.php b/tests/lib/Files/Storage/Wrapper/EncryptionTest.php index ae776e40666a2..11be0c60fbd1e 100644 --- a/tests/lib/Files/Storage/Wrapper/EncryptionTest.php +++ b/tests/lib/Files/Storage/Wrapper/EncryptionTest.php @@ -20,7 +20,6 @@ use OCP\ICacheFactory; use OCP\IConfig; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\Files\Storage\Storage; class EncryptionTest extends Storage { @@ -133,7 +132,6 @@ protected function setUp(): void { ->setMethods(['getUidAndFilename', 'isFile', 'isExcluded']) ->setConstructorArgs([new View(), new Manager( $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class) ), $this->groupManager, $this->config, $this->arrayCache]) @@ -574,7 +572,6 @@ public function testGetHeader($path, $strippedPathExists, $strippedPath) { new View(), new Manager( $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class) ), @@ -657,7 +654,6 @@ public function testGetHeaderAddLegacyModule($header, $isEncrypted, $exists, $ex $util = $this->getMockBuilder('\OC\Encryption\Util') ->setConstructorArgs([new View(), new Manager( $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class) ), $this->groupManager, $this->config, $this->arrayCache]) diff --git a/tests/lib/Files/Stream/EncryptionTest.php b/tests/lib/Files/Stream/EncryptionTest.php index 67311e886e8f0..8cdb379512a8d 100644 --- a/tests/lib/Files/Stream/EncryptionTest.php +++ b/tests/lib/Files/Stream/EncryptionTest.php @@ -9,7 +9,6 @@ use OCP\Files\Cache\ICache; use OCP\ICacheFactory; use OCP\IConfig; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; class EncryptionTest extends \Test\TestCase { public const DEFAULT_WRAPPER = '\OC\Files\Stream\Encryption'; @@ -53,7 +52,6 @@ protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = self ->setMethods(['getUidAndFilename']) ->setConstructorArgs([new View(), new \OC\User\Manager( $config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class) ), $groupManager, $config, $arrayCache]) diff --git a/tests/lib/Group/GroupTest.php b/tests/lib/Group/GroupTest.php index ac648fd7b4bbf..798b997e82a3f 100644 --- a/tests/lib/Group/GroupTest.php +++ b/tests/lib/Group/GroupTest.php @@ -10,6 +10,7 @@ namespace Test\Group; use OC\User\User; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -17,10 +18,13 @@ class GroupTest extends \Test\TestCase { /** @var EventDispatcherInterface|MockObject */ protected $dispatcher; + /** @var EventDispatcherInterface|MockObject */ + protected $eventDispatcher; protected function setUp(): void { parent::setUp(); $this->dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); } /** @@ -305,7 +309,7 @@ public function testSearchUsers() { $backend->expects($this->once()) ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); $users = $group->searchUsers('2'); @@ -327,11 +331,11 @@ public function testSearchUsersMultipleBackends() { $backend1->expects($this->once()) ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); $backend2->expects($this->once()) ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); $users = $group->searchUsers('2'); @@ -350,7 +354,7 @@ public function testSearchUsersLimitAndOffset() { $backend->expects($this->once()) ->method('searchInGroup') ->with('group1', 'user', 1, 1) - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); $users = $group->searchUsers('user', 1, 1); @@ -372,11 +376,11 @@ public function testSearchUsersMultipleBackendsLimitAndOffset() { $backend1->expects($this->once()) ->method('searchInGroup') ->with('group1', 'user', 2, 1) - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); $backend2->expects($this->once()) ->method('searchInGroup') ->with('group1', 'user', 2, 1) - ->willReturn(['user1' => new \OC\User\User('user1', null, $this->dispatcher)]); + ->willReturn(['user1' => new \OC\User\User('user1', null, $this->eventDispatcher)]); $users = $group->searchUsers('user', 2, 1); diff --git a/tests/lib/Group/ManagerTest.php b/tests/lib/Group/ManagerTest.php index 710d3888d55ab..679223888bbb6 100644 --- a/tests/lib/Group/ManagerTest.php +++ b/tests/lib/Group/ManagerTest.php @@ -26,6 +26,7 @@ use OC\Group\Database; use OC\User\User; use OC\User\Manager; +use OCP\EventDispatcher\IEventDispatcher; use OCP\GroupInterface; use OCP\Group\Backend\ISearchableGroupBackend; use OCP\ICacheFactory; @@ -43,6 +44,8 @@ class ManagerTest extends TestCase { protected $userManager; /** @var EventDispatcherInterface|MockObject */ protected $dispatcher; + /** @var IEventDispatcher|MockObject */ + protected $eventDispatcher; /** @var LoggerInterface|MockObject */ protected $logger; /** @var ICacheFactory|MockObject */ @@ -53,6 +56,7 @@ protected function setUp(): void { $this->userManager = $this->createMock(Manager::class); $this->dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->logger = $this->createMock(LoggerInterface::class); $this->cache = $this->createMock(ICacheFactory::class); } @@ -769,7 +773,7 @@ public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitS $backend->expects($this->once()) ->method('searchInGroup') ->with('testgroup', '', 1, 0) - ->willReturn([new User('user2', null, $this->dispatcher)]); + ->willReturn([new User('user2', null, $this->eventDispatcher)]); $this->userManager->expects($this->never())->method('get'); diff --git a/tests/lib/User/DatabaseTest.php b/tests/lib/User/DatabaseTest.php index 49b691cf9bc4f..1a375cdebae6b 100644 --- a/tests/lib/User/DatabaseTest.php +++ b/tests/lib/User/DatabaseTest.php @@ -28,7 +28,6 @@ use OCP\HintException; use OCP\Security\Events\ValidatePasswordPolicyEvent; use PHPUnit\Framework\MockObject\MockObject; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Class DatabaseTest @@ -130,8 +129,8 @@ public function testSearch() { $user2 = $this->getUser(); $this->backend->createUser($user2, 'pass1'); - $user1Obj = new User($user1, $this->backend, $this->createMock(EventDispatcherInterface::class)); - $user2Obj = new User($user2, $this->backend, $this->createMock(EventDispatcherInterface::class)); + $user1Obj = new User($user1, $this->backend, $this->createMock(IEventDispatcher::class)); + $user2Obj = new User($user2, $this->backend, $this->createMock(IEventDispatcher::class)); $emailAddr1 = "$user1@nextcloud.com"; $emailAddr2 = "$user2@nextcloud.com"; diff --git a/tests/lib/User/ManagerTest.php b/tests/lib/User/ManagerTest.php index cf6f03a5a0d5f..aa597f758b089 100644 --- a/tests/lib/User/ManagerTest.php +++ b/tests/lib/User/ManagerTest.php @@ -17,7 +17,6 @@ use OCP\ICacheFactory; use OCP\IConfig; use OCP\IUser; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; /** @@ -30,8 +29,6 @@ class ManagerTest extends TestCase { /** @var IConfig */ private $config; - /** @var EventDispatcherInterface */ - private $oldDispatcher; /** @var IEventDispatcher */ private $eventDispatcher; /** @var ICacheFactory */ @@ -43,7 +40,6 @@ protected function setUp(): void { parent::setUp(); $this->config = $this->createMock(IConfig::class); - $this->oldDispatcher = $this->createMock(EventDispatcherInterface::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->cache = $this->createMock(ICache::class); @@ -54,7 +50,7 @@ protected function setUp(): void { public function testGetBackends() { $userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($userDummyBackend); $this->assertEquals([$userDummyBackend], $manager->getBackends()); $dummyDatabaseBackend = $this->createMock(Database::class); @@ -73,7 +69,7 @@ public function testUserExistsSingleBackendExists() { ->with($this->equalTo('foo')) ->willReturn(true); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertTrue($manager->userExists('foo')); @@ -89,14 +85,14 @@ public function testUserExistsSingleBackendNotExists() { ->with($this->equalTo('foo')) ->willReturn(false); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertFalse($manager->userExists('foo')); } public function testUserExistsNoBackends() { - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $this->assertFalse($manager->userExists('foo')); } @@ -120,7 +116,7 @@ public function testUserExistsTwoBackendsSecondExists() { ->with($this->equalTo('foo')) ->willReturn(true); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -144,7 +140,7 @@ public function testUserExistsTwoBackendsFirstExists() { $backend2->expects($this->never()) ->method('userExists'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -171,7 +167,7 @@ public function testCheckPassword() { } }); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $user = $manager->checkPassword('foo', 'bar'); @@ -190,7 +186,7 @@ public function testCheckPasswordNotSupported() { ->method('implementsActions') ->willReturn(false); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertFalse($manager->checkPassword('foo', 'bar')); @@ -208,7 +204,7 @@ public function testGetOneBackendExists() { $backend->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertEquals('foo', $manager->get('foo')->getUID()); @@ -224,7 +220,7 @@ public function testGetOneBackendNotExists() { ->with($this->equalTo('foo')) ->willReturn(false); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertEquals(null, $manager->get('foo')); @@ -242,7 +238,7 @@ public function testGetOneBackendDoNotTranslateLoginNames() { $backend->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertEquals('bLeNdEr', $manager->get('bLeNdEr')->getUID()); @@ -260,7 +256,7 @@ public function testSearchOneBackend() { $backend->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $result = $manager->search('fo'); @@ -294,7 +290,7 @@ public function testSearchTwoBackendLimitOffset() { $backend2->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -348,7 +344,7 @@ public function testCreateUserInvalid($uid, $password, $exception) { ->willReturn(true); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $this->expectException(\InvalidArgumentException::class, $exception); @@ -375,7 +371,7 @@ public function testCreateUserSingleBackendNotExists() { $backend->expects($this->never()) ->method('loginName2UserName'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $user = $manager->createUser('foo', 'bar'); @@ -402,7 +398,7 @@ public function testCreateUserSingleBackendExists() { ->with($this->equalTo('foo')) ->willReturn(true); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $manager->createUser('foo', 'bar'); @@ -423,14 +419,14 @@ public function testCreateUserSingleBackendNotSupported() { $backend->expects($this->never()) ->method('userExists'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $this->assertFalse($manager->createUser('foo', 'bar')); } public function testCreateUserNoBackends() { - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $this->assertFalse($manager->createUser('foo', 'bar')); } @@ -450,7 +446,7 @@ public function testCreateUserFromBackendWithBackendError() { ->with('MyUid', 'MyPassword') ->willReturn(false); - $manager = new Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->createUserFromBackend('MyUid', 'MyPassword', $backend); } @@ -490,7 +486,7 @@ public function testCreateUserTwoBackendExists() { ->with($this->equalTo('foo')) ->willReturn(true); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -498,7 +494,7 @@ public function testCreateUserTwoBackendExists() { } public function testCountUsersNoBackend() { - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $result = $manager->countUsers(); $this->assertTrue(is_array($result)); @@ -523,7 +519,7 @@ public function testCountUsersOneBackend() { ->method('getBackendName') ->willReturn('Mock_Test_Util_User_Dummy'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $result = $manager->countUsers(); @@ -564,7 +560,7 @@ public function testCountUsersTwoBackends() { ->method('getBackendName') ->willReturn('Mock_Test_Util_User_Dummy'); - $manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend1); $manager->registerBackend($backend2); @@ -677,7 +673,7 @@ public function testDeleteUser() { ->method('getAppValue') ->willReturnArgument(2); - $manager = new \OC\User\Manager($config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($config, $this->cacheFactory, $this->eventDispatcher); $backend = new \Test\Util\User\Dummy(); $manager->registerBackend($backend); @@ -711,7 +707,7 @@ public function testGetByEmail() { true ); - $manager = new \OC\User\Manager($config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher); + $manager = new \OC\User\Manager($config, $this->cacheFactory, $this->eventDispatcher); $manager->registerBackend($backend); $users = $manager->getByEmail('test@example.com'); diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php index 2af48d8330c6f..598a8b6e061ea 100644 --- a/tests/lib/User/SessionTest.php +++ b/tests/lib/User/SessionTest.php @@ -34,7 +34,6 @@ use OCP\User\Events\PostLoginEvent; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use OC\Security\CSRF\CsrfTokenManager; /** @@ -122,7 +121,7 @@ public function testIsLoggedIn($isLoggedIn) { 'getUser' ]) ->getMock(); - $user = new User('sepp', null, $this->createMock(EventDispatcherInterface::class)); + $user = new User('sepp', null, $this->createMock(IEventDispatcher::class)); $userSession->expects($this->once()) ->method('getUser') ->willReturn($isLoggedIn ? $user : null); @@ -177,7 +176,6 @@ public function testLoginValidPasswordEnabled() { ->setMethods($mockedManagerMethods) ->setConstructorArgs([ $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), ]) @@ -244,7 +242,6 @@ public function testLoginValidPasswordDisabled() { ->setMethods($mockedManagerMethods) ->setConstructorArgs([ $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), ]) @@ -278,7 +275,6 @@ public function testLoginInvalidPassword() { ->setMethods($mockedManagerMethods) ->setConstructorArgs([ $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), ]) @@ -492,7 +488,6 @@ public function testRememberLoginValidToken() { ->setMethods($mockedManagerMethods) ->setConstructorArgs([ $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), ]) @@ -582,7 +577,6 @@ public function testRememberLoginInvalidSessionToken() { ->setMethods($mockedManagerMethods) ->setConstructorArgs([ $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), ]) @@ -647,7 +641,6 @@ public function testRememberLoginInvalidToken() { ->setMethods($mockedManagerMethods) ->setConstructorArgs([ $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), ]) @@ -700,7 +693,6 @@ public function testRememberLoginInvalidUser() { ->setMethods($mockedManagerMethods) ->setConstructorArgs([ $this->config, - $this->createMock(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), ]) @@ -739,8 +731,8 @@ public function testRememberLoginInvalidUser() { public function testActiveUserAfterSetSession() { $users = [ - 'foo' => new User('foo', null, $this->createMock(EventDispatcherInterface::class)), - 'bar' => new User('bar', null, $this->createMock(EventDispatcherInterface::class)) + 'foo' => new User('foo', null, $this->createMock(IEventDispatcher::class)), + 'bar' => new User('bar', null, $this->createMock(IEventDispatcher::class)) ]; $manager = $this->getMockBuilder(Manager::class) diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php index 7f4ca97e18bbc..806cb094066cf 100644 --- a/tests/lib/User/UserTest.php +++ b/tests/lib/User/UserTest.php @@ -14,15 +14,16 @@ use OC\Hooks\PublicEmitter; use OC\User\User; use OCP\Comments\ICommentsManager; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Storage\IStorageFactory; use OCP\IConfig; use OCP\IURLGenerator; use OCP\IUser; use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; +use OCP\Server; use OCP\UserInterface; use PHPUnit\Framework\MockObject\MockObject; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; /** @@ -33,12 +34,12 @@ * @package Test\User */ class UserTest extends TestCase { - /** @var EventDispatcherInterface|MockObject */ + /** @var IEventDispatcher|MockObject */ protected $dispatcher; protected function setUp(): void { parent::setUp(); - $this->dispatcher = $this->createMock(EventDispatcherInterface::class); + $this->dispatcher = Server::get(IEventDispatcher::class); } public function testDisplayName() { @@ -408,7 +409,7 @@ public function testSetDisplayNameSupported() { ->with('foo', 'Foo') ->willReturn(true); - $user = new User('foo', $backend, $this->dispatcher); + $user = new User('foo', $backend, $this->createMock(IEventDispatcher::class)); $this->assertTrue($user->setDisplayName('Foo')); $this->assertEquals('Foo', $user->getDisplayName()); } @@ -702,7 +703,8 @@ public function testSetEMailAddressNoChange() { $emitter->expects($this->never()) ->method('emit'); - $this->dispatcher->expects($this->never()) + $dispatcher = $this->createMock(IEventDispatcher::class); + $dispatcher->expects($this->never()) ->method('dispatch'); $config = $this->createMock(IConfig::class); @@ -712,7 +714,7 @@ public function testSetEMailAddressNoChange() { $config->expects($this->any()) ->method('setUserValue'); - $user = new User('foo', $backend, $this->dispatcher, $emitter, $config); + $user = new User('foo', $backend, $dispatcher, $emitter, $config); $user->setEMailAddress('foo@bar.com'); } From 80f79e170dfb0086f0edae0695ddce04ea199af8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 26 Jul 2023 21:56:07 +0200 Subject: [PATCH 2/3] fix!: Remove symfony EventDispatcherInterface from Group Signed-off-by: Joas Schilling --- lib/private/Group/Group.php | 46 +++++++++++----------------- lib/private/Group/Manager.php | 12 ++++---- lib/private/Server.php | 53 +-------------------------------- tests/lib/Group/GroupTest.php | 20 +++++-------- tests/lib/Group/ManagerTest.php | 10 ++----- 5 files changed, 36 insertions(+), 105 deletions(-) diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php index efc21ad7c0dc0..441ee64604d5d 100644 --- a/lib/private/Group/Group.php +++ b/lib/private/Group/Group.php @@ -34,6 +34,13 @@ use OC\Hooks\PublicEmitter; use OC\User\LazyUser; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Group\Events\BeforeGroupDeletedEvent; +use OCP\Group\Events\BeforeUserAddedEvent; +use OCP\Group\Events\BeforeUserRemovedEvent; +use OCP\Group\Events\GroupDeletedEvent; +use OCP\Group\Events\UserAddedEvent; +use OCP\Group\Events\UserRemovedEvent; use OCP\GroupInterface; use OCP\Group\Backend\ICountDisabledInGroup; use OCP\Group\Backend\IGetDisplayNameBackend; @@ -46,8 +53,6 @@ use OCP\IGroup; use OCP\IUser; use OCP\IUserManager; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; class Group implements IGroup { /** @var null|string */ @@ -64,23 +69,14 @@ class Group implements IGroup { /** @var Backend[] */ private $backends; - /** @var EventDispatcherInterface */ + /** @var IEventDispatcher */ private $dispatcher; /** @var \OC\User\Manager|IUserManager */ private $userManager; /** @var PublicEmitter */ private $emitter; - - /** - * @param string $gid - * @param Backend[] $backends - * @param EventDispatcherInterface $dispatcher - * @param IUserManager $userManager - * @param PublicEmitter $emitter - * @param string $displayName - */ - public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) { + public function __construct(string $gid, array $backends, IEventDispatcher $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) { $this->gid = $gid; $this->backends = $backends; $this->dispatcher = $dispatcher; @@ -112,12 +108,12 @@ public function getDisplayName() { public function setDisplayName(string $displayName): bool { $displayName = trim($displayName); if ($displayName !== '') { - $this->dispatcher->dispatch(new BeforeGroupChangedEvent($this, 'displayName', $displayName, $this->displayName)); + $this->dispatcher->dispatchTyped(new BeforeGroupChangedEvent($this, 'displayName', $displayName, $this->displayName)); foreach ($this->backends as $backend) { if (($backend instanceof ISetDisplayNameBackend) && $backend->setDisplayName($this->gid, $displayName)) { $this->displayName = $displayName; - $this->dispatcher->dispatch(new GroupChangedEvent($this, 'displayName', $displayName, '')); + $this->dispatcher->dispatchTyped(new GroupChangedEvent($this, 'displayName', $displayName, '')); return true; } } @@ -180,9 +176,7 @@ public function addUser(IUser $user) { return; } - $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [ - 'user' => $user, - ])); + $this->dispatcher->dispatchTyped(new BeforeUserAddedEvent($this, $user)); if ($this->emitter) { $this->emitter->emit('\OC\Group', 'preAddUser', [$this, $user]); @@ -194,9 +188,7 @@ public function addUser(IUser $user) { $this->users[$user->getUID()] = $user; } - $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [ - 'user' => $user, - ])); + $this->dispatcher->dispatchTyped(new UserAddedEvent($this, $user)); if ($this->emitter) { $this->emitter->emit('\OC\Group', 'postAddUser', [$this, $user]); @@ -213,9 +205,7 @@ public function addUser(IUser $user) { */ public function removeUser($user) { $result = false; - $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [ - 'user' => $user, - ])); + $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($this, $user)); if ($this->emitter) { $this->emitter->emit('\OC\Group', 'preRemoveUser', [$this, $user]); } @@ -226,9 +216,7 @@ public function removeUser($user) { } } if ($result) { - $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [ - 'user' => $user, - ])); + $this->dispatcher->dispatchTyped(new UserRemovedEvent($this, $user)); if ($this->emitter) { $this->emitter->emit('\OC\Group', 'postRemoveUser', [$this, $user]); } @@ -352,7 +340,7 @@ public function delete() { } $result = false; - $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this)); + $this->dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($this)); if ($this->emitter) { $this->emitter->emit('\OC\Group', 'preDelete', [$this]); } @@ -362,7 +350,7 @@ public function delete() { } } if ($result) { - $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this)); + $this->dispatcher->dispatchTyped(new GroupDeletedEvent($this)); if ($this->emitter) { $this->emitter->emit('\OC\Group', 'postDelete', [$this]); } diff --git a/lib/private/Group/Manager.php b/lib/private/Group/Manager.php index 0672e519e3668..c43b5165a791a 100644 --- a/lib/private/Group/Manager.php +++ b/lib/private/Group/Manager.php @@ -41,13 +41,14 @@ use OC\Hooks\PublicEmitter; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Group\Events\BeforeGroupCreatedEvent; +use OCP\Group\Events\GroupCreatedEvent; use OCP\GroupInterface; use OCP\ICacheFactory; use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Class Manager @@ -70,8 +71,7 @@ class Manager extends PublicEmitter implements IGroupManager { /** @var \OC\User\Manager */ private $userManager; - /** @var EventDispatcherInterface */ - private $dispatcher; + private IEventDispatcher $dispatcher; private LoggerInterface $logger; /** @var \OC\Group\Group[] */ @@ -86,7 +86,7 @@ class Manager extends PublicEmitter implements IGroupManager { private DisplayNameCache $displayNameCache; public function __construct(\OC\User\Manager $userManager, - EventDispatcherInterface $dispatcher, + IEventDispatcher $dispatcher, LoggerInterface $logger, ICacheFactory $cacheFactory) { $this->userManager = $userManager; @@ -220,11 +220,13 @@ public function createGroup($gid) { } elseif ($group = $this->get($gid)) { return $group; } else { + $this->dispatcher->dispatchTyped(new BeforeGroupCreatedEvent($gid)); $this->emit('\OC\Group', 'preCreate', [$gid]); foreach ($this->backends as $backend) { if ($backend->implementsActions(Backend::CREATE_GROUP)) { if ($backend->createGroup($gid)) { $group = $this->getGroupObject($gid); + $this->dispatcher->dispatchTyped(new GroupCreatedEvent($group)); $this->emit('\OC\Group', 'postCreate', [$group]); return $group; } @@ -424,7 +426,7 @@ public function getSubAdmin() { $this->userManager, $this, \OC::$server->getDatabaseConnection(), - \OC::$server->get(IEventDispatcher::class) + $this->dispatcher ); } diff --git a/lib/private/Server.php b/lib/private/Server.php index 6c7dcb730ebd0..8640fa3e38c1e 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -192,14 +192,6 @@ use OCP\Files\Template\ITemplateManager; use OCP\FullTextSearch\IFullTextSearchManager; use OCP\GlobalScale\IConfig; -use OCP\Group\Events\BeforeGroupCreatedEvent; -use OCP\Group\Events\BeforeGroupDeletedEvent; -use OCP\Group\Events\BeforeUserAddedEvent; -use OCP\Group\Events\BeforeUserRemovedEvent; -use OCP\Group\Events\GroupCreatedEvent; -use OCP\Group\Events\GroupDeletedEvent; -use OCP\Group\Events\UserAddedEvent; -use OCP\Group\Events\UserRemovedEvent; use OCP\Group\ISubAdmin; use OCP\Http\Client\IClientService; use OCP\IAppConfig; @@ -253,12 +245,10 @@ use OCP\SystemTag\ISystemTagObjectMapper; use OCP\Talk\IBroker; use OCP\Translation\ITranslationManager; -use OCP\User\Events\BeforePasswordUpdatedEvent; use OCP\User\Events\BeforeUserDeletedEvent; use OCP\User\Events\BeforeUserLoggedInEvent; use OCP\User\Events\BeforeUserLoggedInWithCookieEvent; use OCP\User\Events\BeforeUserLoggedOutEvent; -use OCP\User\Events\PasswordUpdatedEvent; use OCP\User\Events\PostLoginEvent; use OCP\User\Events\UserChangedEvent; use OCP\User\Events\UserLoggedInEvent; @@ -268,7 +258,6 @@ use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; use OCA\Files_External\Service\UserStoragesService; use OCA\Files_External\Service\UserGlobalStoragesService; use OCA\Files_External\Service\GlobalStoragesService; @@ -491,50 +480,10 @@ public function __construct($webRoot, \OC\Config $config) { $this->registerService(\OCP\IGroupManager::class, function (ContainerInterface $c) { $groupManager = new \OC\Group\Manager( $this->get(IUserManager::class), - $c->get(SymfonyAdapter::class), + $this->get(IEventDispatcher::class), $this->get(LoggerInterface::class), $this->get(ICacheFactory::class) ); - $groupManager->listen('\OC\Group', 'preCreate', function ($gid) { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new BeforeGroupCreatedEvent($gid)); - }); - $groupManager->listen('\OC\Group', 'postCreate', function (\OC\Group\Group $group) { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new GroupCreatedEvent($group)); - }); - $groupManager->listen('\OC\Group', 'preDelete', function (\OC\Group\Group $group) { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($group)); - }); - $groupManager->listen('\OC\Group', 'postDelete', function (\OC\Group\Group $group) { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new GroupDeletedEvent($group)); - }); - $groupManager->listen('\OC\Group', 'preAddUser', function (\OC\Group\Group $group, \OC\User\User $user) { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new BeforeUserAddedEvent($group, $user)); - }); - $groupManager->listen('\OC\Group', 'postAddUser', function (\OC\Group\Group $group, \OC\User\User $user) { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new UserAddedEvent($group, $user)); - }); - $groupManager->listen('\OC\Group', 'preRemoveUser', function (\OC\Group\Group $group, \OC\User\User $user) { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new BeforeUserRemovedEvent($group, $user)); - }); - $groupManager->listen('\OC\Group', 'postRemoveUser', function (\OC\Group\Group $group, \OC\User\User $user) { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $this->get(IEventDispatcher::class); - $dispatcher->dispatchTyped(new UserRemovedEvent($group, $user)); - }); return $groupManager; }); /** @deprecated 19.0.0 */ diff --git a/tests/lib/Group/GroupTest.php b/tests/lib/Group/GroupTest.php index 798b997e82a3f..58bb1c4133b6b 100644 --- a/tests/lib/Group/GroupTest.php +++ b/tests/lib/Group/GroupTest.php @@ -13,18 +13,14 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; class GroupTest extends \Test\TestCase { - /** @var EventDispatcherInterface|MockObject */ + /** @var IEventDispatcher|MockObject */ protected $dispatcher; - /** @var EventDispatcherInterface|MockObject */ - protected $eventDispatcher; protected function setUp(): void { parent::setUp(); - $this->dispatcher = $this->createMock(EventDispatcherInterface::class); - $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->dispatcher = $this->createMock(IEventDispatcher::class); } /** @@ -309,7 +305,7 @@ public function testSearchUsers() { $backend->expects($this->once()) ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $users = $group->searchUsers('2'); @@ -331,11 +327,11 @@ public function testSearchUsersMultipleBackends() { $backend1->expects($this->once()) ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $backend2->expects($this->once()) ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $users = $group->searchUsers('2'); @@ -354,7 +350,7 @@ public function testSearchUsersLimitAndOffset() { $backend->expects($this->once()) ->method('searchInGroup') ->with('group1', 'user', 1, 1) - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $users = $group->searchUsers('user', 1, 1); @@ -376,11 +372,11 @@ public function testSearchUsersMultipleBackendsLimitAndOffset() { $backend1->expects($this->once()) ->method('searchInGroup') ->with('group1', 'user', 2, 1) - ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $backend2->expects($this->once()) ->method('searchInGroup') ->with('group1', 'user', 2, 1) - ->willReturn(['user1' => new \OC\User\User('user1', null, $this->eventDispatcher)]); + ->willReturn(['user1' => new \OC\User\User('user1', null, $this->dispatcher)]); $users = $group->searchUsers('user', 2, 1); diff --git a/tests/lib/Group/ManagerTest.php b/tests/lib/Group/ManagerTest.php index 679223888bbb6..2887d14acaa17 100644 --- a/tests/lib/Group/ManagerTest.php +++ b/tests/lib/Group/ManagerTest.php @@ -33,7 +33,6 @@ use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; interface ISearchableGroupInterface extends ISearchableGroupBackend, GroupInterface { @@ -42,10 +41,8 @@ interface ISearchableGroupInterface extends ISearchableGroupBackend, GroupInterf class ManagerTest extends TestCase { /** @var Manager|MockObject */ protected $userManager; - /** @var EventDispatcherInterface|MockObject */ - protected $dispatcher; /** @var IEventDispatcher|MockObject */ - protected $eventDispatcher; + protected $dispatcher; /** @var LoggerInterface|MockObject */ protected $logger; /** @var ICacheFactory|MockObject */ @@ -55,8 +52,7 @@ protected function setUp(): void { parent::setUp(); $this->userManager = $this->createMock(Manager::class); - $this->dispatcher = $this->createMock(EventDispatcherInterface::class); - $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->logger = $this->createMock(LoggerInterface::class); $this->cache = $this->createMock(ICacheFactory::class); } @@ -773,7 +769,7 @@ public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitS $backend->expects($this->once()) ->method('searchInGroup') ->with('testgroup', '', 1, 0) - ->willReturn([new User('user2', null, $this->eventDispatcher)]); + ->willReturn([new User('user2', null, $this->dispatcher)]); $this->userManager->expects($this->never())->method('get'); From 030e8d891673d2a87878d5774df97a7d231a9738 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 26 Jul 2023 21:56:36 +0200 Subject: [PATCH 3/3] fix: Align doc type with creation Signed-off-by: Joas Schilling --- tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php | 3 +-- tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php b/tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php index 05d6ede640db7..24acd3cabc931 100644 --- a/tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php +++ b/tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php @@ -27,11 +27,10 @@ use OC\Security\CSP\ContentSecurityPolicyManager; use OCP\EventDispatcher\IEventDispatcher; use OCP\Security\CSP\AddContentSecurityPolicyEvent; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; class ContentSecurityPolicyManagerTest extends TestCase { - /** @var EventDispatcherInterface */ + /** @var IEventDispatcher */ private $dispatcher; /** @var ContentSecurityPolicyManager */ diff --git a/tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php b/tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php index b8f558719dc62..7202e45de73ab 100644 --- a/tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php +++ b/tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php @@ -29,11 +29,10 @@ use OCP\AppFramework\Http\FeaturePolicy; use OCP\EventDispatcher\IEventDispatcher; use OCP\Security\FeaturePolicy\AddFeaturePolicyEvent; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; class FeaturePolicyManagerTest extends TestCase { - /** @var EventDispatcherInterface */ + /** @var IEventDispatcher */ private $dispatcher; /** @var FeaturePolicyManager */