UpdateGroupsServiceTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\user_ldap\tests\Service;
  8. use OCA\User_LDAP\Db\GroupMembership;
  9. use OCA\User_LDAP\Db\GroupMembershipMapper;
  10. use OCA\User_LDAP\Group_Proxy;
  11. use OCA\User_LDAP\Service\UpdateGroupsService;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\Group\Events\UserAddedEvent;
  15. use OCP\Group\Events\UserRemovedEvent;
  16. use OCP\IConfig;
  17. use OCP\IGroup;
  18. use OCP\IGroupManager;
  19. use OCP\IUser;
  20. use OCP\IUserManager;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use Psr\Log\LoggerInterface;
  23. use Test\TestCase;
  24. class UpdateGroupsServiceTest extends TestCase {
  25. /** @var Group_Proxy|MockObject */
  26. protected $groupBackend;
  27. /** @var IEventDispatcher|MockObject */
  28. protected $dispatcher;
  29. /** @var IGroupManager|MockObject */
  30. protected $groupManager;
  31. /** @var IUserManager|MockObject */
  32. protected $userManager;
  33. /** @var LoggerInterface|MockObject */
  34. protected $logger;
  35. /** @var GroupMembershipMapper|MockObject */
  36. protected $groupMembershipMapper;
  37. /** @var IConfig|MockObject */
  38. protected $config;
  39. /** @var ITimeFactory|MockObject */
  40. protected $timeFactory;
  41. protected UpdateGroupsService $updateGroupsService;
  42. public function setUp(): void {
  43. $this->groupBackend = $this->createMock(Group_Proxy::class);
  44. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  45. $this->groupManager = $this->createMock(IGroupManager::class);
  46. $this->userManager = $this->createMock(IUserManager::class);
  47. $this->logger = $this->createMock(LoggerInterface::class);
  48. $this->groupMembershipMapper = $this->createMock(GroupMembershipMapper::class);
  49. $this->config = $this->createMock(IConfig::class);
  50. $this->timeFactory = $this->createMock(ITimeFactory::class);
  51. $this->updateGroupsService = new UpdateGroupsService(
  52. $this->groupBackend,
  53. $this->dispatcher,
  54. $this->groupManager,
  55. $this->userManager,
  56. $this->logger,
  57. $this->groupMembershipMapper,
  58. $this->config,
  59. $this->timeFactory
  60. );
  61. }
  62. public function testHandleKnownGroups(): void {
  63. $knownGroups = [
  64. 'emptyGroup' => [],
  65. 'stableGroup' => ['userA', 'userC', 'userE'],
  66. 'groupWithAdditions' => ['userA', 'userC', 'userE'],
  67. 'groupWithRemovals' => ['userA', 'userC', 'userDeleted', 'userE'],
  68. 'groupWithAdditionsAndRemovals' => ['userA', 'userC', 'userE'],
  69. 'vanishedGroup' => ['userB', 'userDeleted'],
  70. ];
  71. $knownGroupsDB = [];
  72. foreach ($knownGroups as $gid => $members) {
  73. $knownGroupsDB[] = [
  74. 'owncloudname' => $gid,
  75. 'owncloudusers' => $members
  76. ];
  77. }
  78. $actualGroups = [
  79. 'emptyGroup' => [],
  80. 'stableGroup' => ['userA', 'userC', 'userE'],
  81. 'groupWithAdditions' => ['userA', 'userC', 'userE', 'userF'],
  82. 'groupWithRemovals' => ['userA', 'userE'],
  83. 'groupWithAdditionsAndRemovals' => ['userC', 'userE', 'userF'],
  84. 'newGroup' => ['userB', 'userF'],
  85. ];
  86. $groups = array_intersect(array_keys($knownGroups), array_keys($actualGroups));
  87. $this->groupMembershipMapper->expects($this->never())
  88. ->method('getKnownGroups');
  89. $this->groupMembershipMapper->expects($this->exactly(5))
  90. ->method('findGroupMemberships')
  91. ->willReturnCallback(
  92. fn ($group) => array_map(
  93. fn ($userid) => GroupMembership::fromParams(['groupid' => $group,'userid' => $userid]),
  94. $knownGroups[$group]
  95. )
  96. );
  97. $this->groupMembershipMapper->expects($this->exactly(3))
  98. ->method('delete');
  99. $this->groupMembershipMapper->expects($this->exactly(2))
  100. ->method('insert');
  101. $this->groupBackend->expects($this->any())
  102. ->method('usersInGroup')
  103. ->willReturnCallback(function ($groupID) use ($actualGroups) {
  104. return $actualGroups[$groupID] ?? [];
  105. });
  106. $this->groupManager->expects($this->any())
  107. ->method('get')
  108. ->willReturnCallback(function (string $groupId): ?IGroup {
  109. if ($groupId === 'vanishedGroup') {
  110. return null;
  111. }
  112. return $this->createMock(IGroup::class);
  113. });
  114. $this->userManager->expects($this->exactly(5))
  115. ->method('get')
  116. ->willReturnCallback(function (string $userId) {
  117. if ($userId === 'userDeleted') {
  118. // user already deleted
  119. return null;
  120. }
  121. return $this->createMock(IUser::class);
  122. });
  123. $addedEvents = 0;
  124. $removedEvents = 0;
  125. $this->dispatcher->expects($this->exactly(4))
  126. ->method('dispatchTyped')
  127. ->willReturnCallback(function ($event) use (&$addedEvents, &$removedEvents) {
  128. if ($event instanceof UserRemovedEvent) {
  129. $removedEvents++;
  130. } elseif ($event instanceof UserAddedEvent) {
  131. $addedEvents++;
  132. }
  133. });
  134. $this->updateGroupsService->handleKnownGroups($groups);
  135. $this->assertSame(2, $removedEvents);
  136. $this->assertSame(2, $addedEvents);
  137. // and no event for the user that is already deleted, the DB is nevertheless updated, hence 5
  138. }
  139. }