test: add some tests for SharesUpdatedListenerTest

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2026-02-19 18:01:29 +01:00
parent 1b84a85558
commit 8680d4446b
No known key found for this signature in database
GPG key ID: 42B69D8A64526EFB

View file

@ -0,0 +1,136 @@
<?php
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Files_Sharing\Tests;
use OCA\Files_Sharing\Config\ConfigLexicon;
use OCA\Files_Sharing\Event\UserShareAccessUpdatedEvent;
use OCA\Files_Sharing\Listener\SharesUpdatedListener;
use OCA\Files_Sharing\ShareRecipientUpdater;
use OCP\Config\IUserConfig;
use OCP\IAppConfig;
use OCP\IUser;
use OCP\Share\Events\BeforeShareDeletedEvent;
use OCP\Share\Events\ShareCreatedEvent;
use OCP\Share\IManager;
use OCP\Share\IShare;
use PHPUnit\Framework\MockObject\MockObject;
use Test\Mock\Config\MockAppConfig;
use Test\Mock\Config\MockUserConfig;
use Test\Traits\UserTrait;
class SharesUpdatedListenerTest extends \Test\TestCase {
use UserTrait;
private SharesUpdatedListener $sharesUpdatedListener;
private ShareRecipientUpdater&MockObject $shareRecipientUpdater;
private IManager&MockObject $manager;
private IUserConfig $userConfig;
private IAppConfig $appConfig;
protected function setUp(): void {
parent::setUp();
$this->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);
}
}