1
0

UpdateGroupsServiceTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\user_ldap\tests\Service;
  27. use OCA\User_LDAP\Db\GroupMembership;
  28. use OCA\User_LDAP\Db\GroupMembershipMapper;
  29. use OCA\User_LDAP\Group_Proxy;
  30. use OCA\User_LDAP\Service\UpdateGroupsService;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\EventDispatcher\IEventDispatcher;
  33. use OCP\Group\Events\UserAddedEvent;
  34. use OCP\Group\Events\UserRemovedEvent;
  35. use OCP\IConfig;
  36. use OCP\IGroup;
  37. use OCP\IGroupManager;
  38. use OCP\IUser;
  39. use OCP\IUserManager;
  40. use PHPUnit\Framework\MockObject\MockObject;
  41. use Psr\Log\LoggerInterface;
  42. use Test\TestCase;
  43. class UpdateGroupsServiceTest extends TestCase {
  44. /** @var Group_Proxy|MockObject */
  45. protected $groupBackend;
  46. /** @var IEventDispatcher|MockObject */
  47. protected $dispatcher;
  48. /** @var IGroupManager|MockObject */
  49. protected $groupManager;
  50. /** @var IUserManager|MockObject */
  51. protected $userManager;
  52. /** @var LoggerInterface|MockObject */
  53. protected $logger;
  54. /** @var GroupMembershipMapper|MockObject */
  55. protected $groupMembershipMapper;
  56. /** @var IConfig|MockObject */
  57. protected $config;
  58. /** @var ITimeFactory|MockObject */
  59. protected $timeFactory;
  60. protected UpdateGroupsService $updateGroupsService;
  61. public function setUp(): void {
  62. $this->groupBackend = $this->createMock(Group_Proxy::class);
  63. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  64. $this->groupManager = $this->createMock(IGroupManager::class);
  65. $this->userManager = $this->createMock(IUserManager::class);
  66. $this->logger = $this->createMock(LoggerInterface::class);
  67. $this->groupMembershipMapper = $this->createMock(GroupMembershipMapper::class);
  68. $this->config = $this->createMock(IConfig::class);
  69. $this->timeFactory = $this->createMock(ITimeFactory::class);
  70. $this->updateGroupsService = new UpdateGroupsService(
  71. $this->groupBackend,
  72. $this->dispatcher,
  73. $this->groupManager,
  74. $this->userManager,
  75. $this->logger,
  76. $this->groupMembershipMapper,
  77. $this->config,
  78. $this->timeFactory
  79. );
  80. }
  81. public function testHandleKnownGroups(): void {
  82. $knownGroups = [
  83. 'emptyGroup' => [],
  84. 'stableGroup' => ['userA', 'userC', 'userE'],
  85. 'groupWithAdditions' => ['userA', 'userC', 'userE'],
  86. 'groupWithRemovals' => ['userA', 'userC', 'userDeleted', 'userE'],
  87. 'groupWithAdditionsAndRemovals' => ['userA', 'userC', 'userE'],
  88. 'vanishedGroup' => ['userB', 'userDeleted'],
  89. ];
  90. $knownGroupsDB = [];
  91. foreach ($knownGroups as $gid => $members) {
  92. $knownGroupsDB[] = [
  93. 'owncloudname' => $gid,
  94. 'owncloudusers' => $members
  95. ];
  96. }
  97. $actualGroups = [
  98. 'emptyGroup' => [],
  99. 'stableGroup' => ['userA', 'userC', 'userE'],
  100. 'groupWithAdditions' => ['userA', 'userC', 'userE', 'userF'],
  101. 'groupWithRemovals' => ['userA', 'userE'],
  102. 'groupWithAdditionsAndRemovals' => ['userC', 'userE', 'userF'],
  103. 'newGroup' => ['userB', 'userF'],
  104. ];
  105. $groups = array_intersect(array_keys($knownGroups), array_keys($actualGroups));
  106. $this->groupMembershipMapper->expects($this->never())
  107. ->method('getKnownGroups');
  108. $this->groupMembershipMapper->expects($this->exactly(5))
  109. ->method('findGroupMemberships')
  110. ->willReturnCallback(
  111. fn ($group) => array_map(
  112. fn ($userid) => GroupMembership::fromParams(['groupid' => $group,'userid' => $userid]),
  113. $knownGroups[$group]
  114. )
  115. );
  116. $this->groupMembershipMapper->expects($this->exactly(3))
  117. ->method('delete');
  118. $this->groupMembershipMapper->expects($this->exactly(2))
  119. ->method('insert');
  120. $this->groupBackend->expects($this->any())
  121. ->method('usersInGroup')
  122. ->willReturnCallback(function ($groupID) use ($actualGroups) {
  123. return $actualGroups[$groupID] ?? [];
  124. });
  125. $this->groupManager->expects($this->any())
  126. ->method('get')
  127. ->willReturnCallback(function (string $groupId): ?IGroup {
  128. if ($groupId === 'vanishedGroup') {
  129. return null;
  130. }
  131. return $this->createMock(IGroup::class);
  132. });
  133. $this->userManager->expects($this->exactly(5))
  134. ->method('get')
  135. ->willReturnCallback(function (string $userId) {
  136. if ($userId === 'userDeleted') {
  137. // user already deleted
  138. return null;
  139. }
  140. return $this->createMock(IUser::class);
  141. });
  142. $addedEvents = 0;
  143. $removedEvents = 0;
  144. $this->dispatcher->expects($this->exactly(4))
  145. ->method('dispatchTyped')
  146. ->willReturnCallback(function ($event) use (&$addedEvents, &$removedEvents) {
  147. if ($event instanceof UserRemovedEvent) {
  148. $removedEvents++;
  149. } elseif ($event instanceof UserAddedEvent) {
  150. $addedEvents++;
  151. }
  152. });
  153. $this->updateGroupsService->handleKnownGroups($groups);
  154. $this->assertSame(2, $removedEvents);
  155. $this->assertSame(2, $addedEvents);
  156. // and no event for the user that is already deleted, the DB is nevertheless updated, hence 5
  157. }
  158. }