From 1985cdbd732cac10aafdc04164776b29ee41ea24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez=20V=C3=A1zquez?= Date: Fri, 31 May 2024 22:52:22 +0200 Subject: [PATCH 1/6] feat: migrate from deprecated PublicEmitter to IEventDispatcher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Borja Domínguez Vázquez --- lib/GroupManager.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/GroupManager.php b/lib/GroupManager.php index 3f9d3ff90..293179742 100644 --- a/lib/GroupManager.php +++ b/lib/GroupManager.php @@ -6,16 +6,20 @@ namespace OCA\User_SAML; -use OC\Hooks\PublicEmitter; use OCA\User_SAML\Exceptions\GroupNotFoundException; use OCA\User_SAML\Exceptions\NonMigratableGroupException; use OCA\User_SAML\Jobs\MigrateGroups; use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; +use OCP\Group\Events\BeforeGroupCreatedEvent; +use OCP\Group\Events\BeforeGroupDeletedEvent; +use OCP\Group\Events\GroupCreatedEvent; +use OCP\Group\Events\GroupDeletedEvent; class GroupManager { public const LOCAL_GROUPS_CHECK_FOR_MIGRATION = 'localGroupsCheckForMigration'; @@ -31,6 +35,8 @@ class GroupManager { private $ownGroupBackend; /** @var IConfig */ private $config; + /** @var IEventDispatcher */ + private $dispatcher; /** @var IJobList */ private $jobList; /** @var SAMLSettings */ @@ -42,6 +48,7 @@ public function __construct( IGroupManager $groupManager, GroupBackend $ownGroupBackend, IConfig $config, + IEventDispatcher $dispatcher, IJobList $jobList, SAMLSettings $settings ) { @@ -49,6 +56,7 @@ public function __construct( $this->groupManager = $groupManager; $this->ownGroupBackend = $ownGroupBackend; $this->config = $config; + $this->dispatcher = $dispatcher; $this->jobList = $jobList; $this->settings = $settings; } @@ -141,11 +149,9 @@ protected function unassignUserFromGroup(IUser $user, string $gid): void { if ($this->hasSamlBackend($group)) { $this->ownGroupBackend->removeFromGroup($user->getUID(), $group->getGID()); if ($this->ownGroupBackend->countUsersInGroup($gid) === 0) { - /** @psalm-suppress UndefinedInterfaceMethod */ - $this->groupManager->emit('\OC\Group', 'preDelete', [$group]); + $this->dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($group)); $this->ownGroupBackend->deleteGroup($group->getGID()); - /** @psalm-suppress UndefinedInterfaceMethod */ - $this->groupManager->emit('\OC\Group', 'postDelete', [$group]); + $this->dispatcher->dispatchTyped(new GroupDeletedEvent($group)); } } else { $group->removeUser($user); @@ -172,17 +178,13 @@ protected function assignUserToGroup(IUser $user, string $gid): void { } protected function createGroupInBackend(string $gid, ?string $originalGid = null): ?IGroup { - if ($this->groupManager instanceof PublicEmitter) { - $this->groupManager->emit('\OC\Group', 'preCreate', [$gid]); - } + $this->dispatcher->dispatchTyped(new BeforeGroupCreatedEvent($gid)); if (!$this->ownGroupBackend->createGroup($gid, $originalGid ?? $gid)) { return null; } $group = $this->groupManager->get($gid); - if ($this->groupManager instanceof PublicEmitter) { - $this->groupManager->emit('\OC\Group', 'postCreate', [$group]); - } + $this->dispatcher->dispatchTyped(new GroupCreatedEvent($group)); return $group; } From 49a3edb255b4aff48d26c4d1c4a2c5704af94a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez?= Date: Tue, 4 Jun 2024 12:38:10 +0200 Subject: [PATCH 2/6] Update stub.phpstub MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Borja Domínguez Signed-off-by: Borja Domínguez Vázquez --- tests/stub.phpstub | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/stub.phpstub b/tests/stub.phpstub index 4d8492f0e..12a826696 100644 --- a/tests/stub.phpstub +++ b/tests/stub.phpstub @@ -24,9 +24,3 @@ namespace OC\Group { \OCP\Group\Backend\INamedBackend { } } - -namespace OC\Hooks { - interface PublicEmitter { - public function emit($scope, $method, array $arguments = []); - } -} From 1d10effc6343cc9220e3b0938d63a3faee22ce3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez?= Date: Wed, 26 Jun 2024 15:01:09 +0200 Subject: [PATCH 3/6] Update GroupManager.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Borja Domínguez Signed-off-by: Borja Domínguez Vázquez --- lib/GroupManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/GroupManager.php b/lib/GroupManager.php index 293179742..989c1bb46 100644 --- a/lib/GroupManager.php +++ b/lib/GroupManager.php @@ -11,15 +11,15 @@ use OCA\User_SAML\Jobs\MigrateGroups; use OCP\BackgroundJob\IJobList; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Group\Events\BeforeGroupCreatedEvent; +use OCP\Group\Events\BeforeGroupDeletedEvent; +use OCP\Group\Events\GroupCreatedEvent; +use OCP\Group\Events\GroupDeletedEvent; use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; -use OCP\Group\Events\BeforeGroupCreatedEvent; -use OCP\Group\Events\BeforeGroupDeletedEvent; -use OCP\Group\Events\GroupCreatedEvent; -use OCP\Group\Events\GroupDeletedEvent; class GroupManager { public const LOCAL_GROUPS_CHECK_FOR_MIGRATION = 'localGroupsCheckForMigration'; From 77d21b6b55a49ec024bc5692d4c974cdefab72ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez=20V=C3=A1zquez?= Date: Thu, 27 Jun 2024 20:49:25 +0200 Subject: [PATCH 4/6] fix tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Borja Domínguez Vázquez --- tests/unit/GroupManagerTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/unit/GroupManagerTest.php b/tests/unit/GroupManagerTest.php index 676a49117..64af03612 100644 --- a/tests/unit/GroupManagerTest.php +++ b/tests/unit/GroupManagerTest.php @@ -11,6 +11,11 @@ use OCA\User_SAML\GroupBackend; use OCA\User_SAML\GroupManager; use OCA\User_SAML\SAMLSettings; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Group\Events\BeforeGroupCreatedEvent; +use OCP\Group\Events\BeforeGroupDeletedEvent; +use OCP\Group\Events\GroupCreatedEvent; +use OCP\Group\Events\GroupDeletedEvent; use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroup; @@ -42,6 +47,7 @@ protected function setUp(): void { $this->groupManager = $this->createMock(Manager::class); $this->ownGroupBackend = $this->createMock(GroupBackend::class); $this->config = $this->createMock(IConfig::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->jobList = $this->createMock(JobList::class); $this->settings = $this->createMock(SAMLSettings::class); $this->ownGroupManager = $this->createMock(GroupManager::class); @@ -55,6 +61,7 @@ public function getGroupManager(array $mockedFunctions = []) { $this->groupManager, $this->ownGroupBackend, $this->config, + $this->eventDispatcher, $this->jobList, $this->settings ]) @@ -66,6 +73,7 @@ public function getGroupManager(array $mockedFunctions = []) { $this->groupManager, $this->ownGroupBackend, $this->config, + $this->eventDispatcher, $this->jobList, $this->settings ); @@ -163,10 +171,16 @@ public function testUnassignUserFromGroups() { ->expects($this->once()) ->method('countUsersInGroup') ->willReturn(0); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforeGroupDeletedEvent($groupA)); // assert group is deleted $this->ownGroupBackend ->expects($this->once()) ->method('deleteGroup'); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new GroupDeletedEvent($groupA)); $this->invokePrivate($this->ownGroupManager, 'handleUserUnassignedFromGroups', [$user, ['groupA']]); } @@ -216,12 +230,18 @@ public function testAssignUserToNonExistingGroups() { $this->groupManager ->method('get') ->willReturnOnConsecutiveCalls(null, $groupB); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforeGroupCreatedEvent('groupB')); // assert group is created $this->ownGroupBackend ->expects($this->once()) ->method('createGroup') ->with('SAML_groupB', 'groupB') ->willReturn(true); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new GroupCreatedEvent($groupB)); // assert user gets added to group $groupB->expects($this->once()) ->method('addUser') @@ -264,12 +284,18 @@ public function testAssignUserToGroupsWithCollision() { // assert the default group prefix is configured $this->settings ->method('get'); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new BeforeGroupCreatedEvent('groupC')); // assert group is created with prefix + gid $this->ownGroupBackend ->expects($this->once()) ->method('createGroup') ->with('SAML_groupC', 'groupC') ->willReturn(true); + $this->eventDispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with(new GroupCreatedEvent($groupC)); // assert user gets added to group $groupC->expects($this->once()) ->method('addUser') From 54254b452fc5b4f348abba02a9ffde2e2d2e82b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez=20V=C3=A1zquez?= Date: Fri, 28 Jun 2024 22:33:43 +0200 Subject: [PATCH 5/6] fix tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Borja Domínguez Vázquez --- tests/unit/GroupManagerTest.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/unit/GroupManagerTest.php b/tests/unit/GroupManagerTest.php index 64af03612..48ea05aa2 100644 --- a/tests/unit/GroupManagerTest.php +++ b/tests/unit/GroupManagerTest.php @@ -171,16 +171,16 @@ public function testUnassignUserFromGroups() { ->expects($this->once()) ->method('countUsersInGroup') ->willReturn(0); - $this->eventDispatcher->expects($this->once()) - ->method('dispatchTyped') - ->with(new BeforeGroupDeletedEvent($groupA)); // assert group is deleted $this->ownGroupBackend ->expects($this->once()) ->method('deleteGroup'); - $this->eventDispatcher->expects($this->once()) + $this->eventDispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->with(new GroupDeletedEvent($groupA)); + ->withConsecutive( + [new BeforeGroupDeletedEvent($groupA)], + [new GroupDeletedEvent($groupA)] + ); $this->invokePrivate($this->ownGroupManager, 'handleUserUnassignedFromGroups', [$user, ['groupA']]); } @@ -230,18 +230,18 @@ public function testAssignUserToNonExistingGroups() { $this->groupManager ->method('get') ->willReturnOnConsecutiveCalls(null, $groupB); - $this->eventDispatcher->expects($this->once()) - ->method('dispatchTyped') - ->with(new BeforeGroupCreatedEvent('groupB')); // assert group is created $this->ownGroupBackend ->expects($this->once()) ->method('createGroup') ->with('SAML_groupB', 'groupB') ->willReturn(true); - $this->eventDispatcher->expects($this->once()) + $this->eventDispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->with(new GroupCreatedEvent($groupB)); + ->withConsecutive( + [new BeforeGroupCreatedEvent('SAML_groupB')], + [new GroupCreatedEvent($groupB)] + ); // assert user gets added to group $groupB->expects($this->once()) ->method('addUser') @@ -284,18 +284,18 @@ public function testAssignUserToGroupsWithCollision() { // assert the default group prefix is configured $this->settings ->method('get'); - $this->eventDispatcher->expects($this->once()) - ->method('dispatchTyped') - ->with(new BeforeGroupCreatedEvent('groupC')); // assert group is created with prefix + gid $this->ownGroupBackend ->expects($this->once()) ->method('createGroup') ->with('SAML_groupC', 'groupC') ->willReturn(true); - $this->eventDispatcher->expects($this->once()) + $this->eventDispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->with(new GroupCreatedEvent($groupC)); + ->withConsecutive( + [new BeforeGroupCreatedEvent('SAML_groupC')], + [new GroupCreatedEvent($groupC)] + ); // assert user gets added to group $groupC->expects($this->once()) ->method('addUser') From 65ad97bda5a4aa93dd99234d6cc9f85515fceda1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez=20V=C3=A1zquez?= Date: Sat, 29 Jun 2024 12:14:21 +0200 Subject: [PATCH 6/6] Fix warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Borja Domínguez Vázquez --- tests/unit/GroupManagerTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/GroupManagerTest.php b/tests/unit/GroupManagerTest.php index 48ea05aa2..0cbbb8f08 100644 --- a/tests/unit/GroupManagerTest.php +++ b/tests/unit/GroupManagerTest.php @@ -34,6 +34,8 @@ class GroupManagerTest extends TestCase { private $ownGroupBackend; /** @var IConfig|MockObject */ private $config; + /** @var IEventDispatcher|MockObject */ + private $eventDispatcher; /** @var JobList|MockObject */ private $jobList; /** @var SAMLSettings|MockObject */