diff --git a/apps/files_sharing/tests/SharesUpdatedListenerTest.php b/apps/files_sharing/tests/SharesUpdatedListenerTest.php new file mode 100644 index 00000000000..3e3211dae36 --- /dev/null +++ b/apps/files_sharing/tests/SharesUpdatedListenerTest.php @@ -0,0 +1,136 @@ +shareRecipientUpdater = $this->createMock(ShareRecipientUpdater::class); + $this->manager = $this->createMock(IManager::class); + $this->appConfig = new MockAppConfig([ + ConfigLexicon::UPDATE_ALL_CUTOFF => -1, + ConfigLexicon::UPDATE_SINGLE_CUTOFF => -1, + ]); + $this->userConfig = new MockUserConfig(); + $this->sharesUpdatedListener = new SharesUpdatedListener( + $this->manager, + $this->shareRecipientUpdater, + $this->userConfig, + $this->appConfig, + ); + } + + public function testShareAdded() { + $share = $this->createMock(IShare::class); + $user1 = $this->createUser('user1', ''); + $user2 = $this->createUser('user2', ''); + + $this->manager->method('getUsersForShare') + ->willReturn([$user1, $user2]); + + $event = new ShareCreatedEvent($share); + + $this->shareRecipientUpdater + ->expects($this->exactly(2)) + ->method('updateForShare') + ->willReturnCallback(function (IUser $user, IShare $eventShare) use ($user1, $user2, $share) { + $this->assertContains($user, [$user1, $user2]); + $this->assertEquals($share, $eventShare); + }); + + $this->sharesUpdatedListener->handle($event); + } + + public function testShareAddedFilterOwner() { + $share = $this->createMock(IShare::class); + $user1 = $this->createUser('user1', ''); + $user2 = $this->createUser('user2', ''); + $share->method('getSharedBy') + ->willReturn($user1->getUID()); + + $this->manager->method('getUsersForShare') + ->willReturn([$user1, $user2]); + + $event = new ShareCreatedEvent($share); + + $this->shareRecipientUpdater + ->expects($this->exactly(1)) + ->method('updateForShare') + ->willReturnCallback(function (IUser $user, IShare $eventShare) use ($user2, $share) { + $this->assertEquals($user, $user2); + $this->assertEquals($share, $eventShare); + }); + + $this->sharesUpdatedListener->handle($event); + } + + public function testShareAccessUpdated() { + $user1 = $this->createUser('user1', ''); + $user2 = $this->createUser('user2', ''); + + $event = new UserShareAccessUpdatedEvent([$user1, $user2]); + + $this->shareRecipientUpdater + ->expects($this->exactly(2)) + ->method('updateForUser') + ->willReturnCallback(function (IUser $user, bool $verifyMountPoints = true, array $ignoreShares = []) use ($user1, $user2) { + $this->assertContains($user, [$user1, $user2]); + $this->assertEquals(true, $verifyMountPoints); + $this->assertEquals([], $ignoreShares); + }); + + $this->sharesUpdatedListener->handle($event); + } + + public function testShareDeleted() { + $share = $this->createMock(IShare::class); + $user1 = $this->createUser('user1', ''); + $user2 = $this->createUser('user2', ''); + + $this->manager->method('getUsersForShare') + ->willReturn([$user1, $user2]); + + $event = new BeforeShareDeletedEvent($share); + + $this->shareRecipientUpdater + ->expects($this->exactly(2)) + ->method('updateForUser') + ->willReturnCallback(function (IUser $user, bool $verifyMountPoints = true, array $ignoreShares = []) use ($user1, $user2, $share) { + $this->assertContains($user, [$user1, $user2]); + $this->assertEquals(false, $verifyMountPoints); + $this->assertEquals([$share], $ignoreShares); + }); + + $this->sharesUpdatedListener->handle($event); + } +}