diff --git a/lib/Provider/TextContextProvider.php b/lib/Provider/TextContextProvider.php index 089cdacb3..2fe3707c6 100644 --- a/lib/Provider/TextContextProvider.php +++ b/lib/Provider/TextContextProvider.php @@ -66,9 +66,8 @@ public function buildDocument(): Document { #[\Override] public function prepareSession(): SessionInfo { - $readonly = !$this->permissionService->checkPermission($this->cardMapper, $this->cardId, Acl::PERMISSION_EDIT, $this->userId); return new SessionInfo( - readOnly: $readonly, + readOnly: $this->isReadOnly(), lock: null, canAttachFiles: false, ); @@ -76,7 +75,11 @@ public function prepareSession(): SessionInfo { #[\Override] public function isReadOnly(): bool { - return !$this->permissionService->checkPermission($this->cardMapper, $this->cardId, Acl::PERMISSION_EDIT, $this->userId); + try { + return !$this->permissionService->checkPermission($this->cardMapper, $this->cardId, Acl::PERMISSION_EDIT, $this->userId); + } catch (NoPermissionException|DoesNotExistException) { + return true; + } } #[\Override] diff --git a/tests/unit/Provider/TextContextProviderTest.php b/tests/unit/Provider/TextContextProviderTest.php index 72f0408fd..a73d62a0d 100644 --- a/tests/unit/Provider/TextContextProviderTest.php +++ b/tests/unit/Provider/TextContextProviderTest.php @@ -8,6 +8,7 @@ namespace OCA\Deck\Provider; use OC\Security\SecureRandom; +use OCA\Deck\Db\Acl; use OCA\Deck\Db\Card; use OCA\Deck\Db\CardMapper; use OCA\Deck\NoPermissionException; @@ -22,6 +23,7 @@ class TextContextProviderTest extends TestCase { private CardService&MockObject $cardService; + private PermissionService&MockObject $permissionService; private TextContextProvider $provider; protected function setUp(): void { @@ -30,11 +32,12 @@ protected function setUp(): void { $this->markTestSkipped('Text app not available'); } $this->cardService = $this->createMock(CardService::class); + $this->permissionService = $this->createMock(PermissionService::class); $this->provider = new TextContextProvider( $this->createMock(SecureRandom::class), $this->cardService, $this->createMock(CardMapper::class), - $this->createMock(PermissionService::class), + $this->permissionService, 'admin', 5, ); @@ -75,4 +78,24 @@ public function testBuildDocumentNoPermission(): void { $this->expectException(NotPermittedException::class); $this->provider->buildDocument(); } + + public function testPrepareSessionWithEditPermission(): void { + $this->permissionService->expects($this->once()) + ->method('checkPermission') + ->with($this->anything(), 5, Acl::PERMISSION_EDIT, 'admin') + ->willReturn(true); + + $sessionInfo = $this->provider->prepareSession(); + + $this->assertFalse($sessionInfo->readOnly); + $this->assertFalse($sessionInfo->canAttachFiles); + $this->assertNull($sessionInfo->lock); + } + + public function testPrepareSessionWithoutEditPermission(): void { + $this->permissionService->method('checkPermission') + ->willThrowException(new NoPermissionException('denied')); + + $this->assertTrue($this->provider->prepareSession()->readOnly); + } }