diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2025-06-02 13:19:03 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2025-06-02 13:20:53 +0200 |
commit | 9d8c2cd09645ac7c9fee7c01daf380d146553d19 (patch) | |
tree | 5985fc56309bee9a444bd84cc7747c5c422cbafd | |
parent | f4cb78b905fc7792af170901a327dacd70414cf7 (diff) | |
download | nextcloud-server-test/files-sharing-phpunit.tar.gz nextcloud-server-test/files-sharing-phpunit.zip |
test(files_sharing): split `testCanAccessShare` into separate tests and fix for PHPUnit 10test/files-sharing-phpunit
- split the test into individual test cases
- fix invalid call to `onConsecutiveCalls` (it was called more than
defined values and is deprecated in v10 anyways).
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
-rw-r--r-- | apps/files_sharing/tests/Controller/ShareAPIControllerTest.php | 117 |
1 files changed, 81 insertions, 36 deletions
diff --git a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php index 203e09e217e..f245f0a25c7 100644 --- a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php @@ -1542,84 +1542,129 @@ class ShareAPIControllerTest extends TestCase { $this->assertEquals($expected, $result->getData()); } - public function testCanAccessShare(): void { - $share = $this->getMockBuilder(IShare::class)->getMock(); + public function testCanAccessShareAsOwner(): void { + $share = $this->createMock(IShare::class); $share->method('getShareOwner')->willReturn($this->currentUser); $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } - $share = $this->getMockBuilder(IShare::class)->getMock(); + public function testCanAccessShareAsSharer(): void { + $share = $this->createMock(IShare::class); $share->method('getSharedBy')->willReturn($this->currentUser); $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } - $share = $this->getMockBuilder(IShare::class)->getMock(); + public function testCanAccessShareAsSharee(): void { + $share = $this->createMock(IShare::class); $share->method('getShareType')->willReturn(IShare::TYPE_USER); $share->method('getSharedWith')->willReturn($this->currentUser); $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } - $file = $this->getMockBuilder(File::class)->getMock(); + public function testCannotAccessLinkShare(): void { + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_LINK); + $share->method('getNodeId')->willReturn(42); - $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $userFolder = $this->createMock(Folder::class); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); + $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } + + /** + * @dataProvider dataCanAccessShareWithPermissions + */ + public function testCanAccessShareWithPermissions(int $permissions, bool $expected): void { + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_USER); + $share->method('getSharedWith')->willReturn($this->createMock(IUser::class)); + $share->method('getNodeId')->willReturn(42); + + $file = $this->createMock(File::class); + + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); $userFolder->method('getFirstNodeById') ->with($share->getNodeId()) ->willReturn($file); $userFolder->method('getById') ->with($share->getNodeId()) ->willReturn([$file]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); $file->method('getPermissions') - ->will($this->onConsecutiveCalls(Constants::PERMISSION_SHARE, Constants::PERMISSION_READ)); + ->willReturn($permissions); - // getPermissions -> share - $share = $this->getMockBuilder(IShare::class)->getMock(); - $share->method('getShareType')->willReturn(IShare::TYPE_USER); - $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock()); - $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + if ($expected) { + $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } else { + $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } + } - // getPermissions -> read - $share = $this->getMockBuilder(IShare::class)->getMock(); - $share->method('getShareType')->willReturn(IShare::TYPE_USER); - $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock()); - $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + public static function dataCanAccessShareWithPermissions(): array { + return [ + [Constants::PERMISSION_SHARE, true], + [Constants::PERMISSION_READ, false], + [Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, true], + ]; + } - $share = $this->getMockBuilder(IShare::class)->getMock(); + /** + * @dataProvider dataCanAccessShareAsGroupMember + */ + public function testCanAccessShareAsGroupMember(string $group, bool $expected): void { + $share = $this->createMock(IShare::class); $share->method('getShareType')->willReturn(IShare::TYPE_GROUP); - $share->method('getSharedWith')->willReturn('group'); + $share->method('getSharedWith')->willReturn($group); + $share->method('getNodeId')->willReturn(42); + + $file = $this->createMock(File::class); + + $userFolder = $this->createMock(Folder::class); + $userFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($file); + $userFolder->method('getById') + ->with($share->getNodeId()) + ->willReturn([$file]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); $user = $this->createMock(IUser::class); $this->userManager->method('get') ->with($this->currentUser) ->willReturn($user); - $group = $this->getMockBuilder(IGroup::class)->getMock(); + $group = $this->createMock(IGroup::class); $group->method('inGroup')->with($user)->willReturn(true); - $group2 = $this->getMockBuilder(IGroup::class)->getMock(); + $group2 = $this->createMock(IGroup::class); $group2->method('inGroup')->with($user)->willReturn(false); $this->groupManager->method('get')->willReturnMap([ ['group', $group], ['group2', $group2], - ['groupnull', null], + ['group-null', null], ]); - $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); - $share = $this->createMock(IShare::class); - $share->method('getShareType')->willReturn(IShare::TYPE_GROUP); - $share->method('getSharedWith')->willReturn('group2'); - $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); - - // null group - $share = $this->createMock(IShare::class); - $share->method('getShareType')->willReturn(IShare::TYPE_GROUP); - $share->method('getSharedWith')->willReturn('groupnull'); - $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + if ($expected) { + $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } else { + $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } + } - $share = $this->createMock(IShare::class); - $share->method('getShareType')->willReturn(IShare::TYPE_LINK); - $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + public static function dataCanAccessShareAsGroupMember(): array { + return [ + ['group', true], + ['group2', false], + ['group-null', false], + ]; } public function dataCanAccessRoomShare() { |