diff options
Diffstat (limited to 'apps/files_sharing/tests/Controller/ShareAPIControllerTest.php')
-rw-r--r-- | apps/files_sharing/tests/Controller/ShareAPIControllerTest.php | 922 |
1 files changed, 586 insertions, 336 deletions
diff --git a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php index f8c316cd2e9..e6be0342c26 100644 --- a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -6,6 +7,7 @@ */ namespace OCA\Files_Sharing\Tests\Controller; +use OCA\Federation\TrustedServers; use OCA\Files_Sharing\Controller\ShareAPIController; use OCP\App\IAppManager; use OCP\AppFramework\Http\DataResponse; @@ -21,6 +23,7 @@ use OCP\Files\Mount\IMountPoint; use OCP\Files\Mount\IShareOwnerlessMount; use OCP\Files\NotFoundException; use OCP\Files\Storage\IStorage; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IDateTimeZone; use OCP\IGroup; @@ -28,12 +31,15 @@ use OCP\IGroupManager; use OCP\IL10N; use OCP\IPreview; use OCP\IRequest; +use OCP\ITagManager; +use OCP\ITags; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserManager; use OCP\Lock\ILockingProvider; use OCP\Lock\LockedException; use OCP\Mail\IMailer; +use OCP\Server; use OCP\Share\Exceptions\GenericShareException; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IAttributes as IShareAttributes; @@ -67,6 +73,7 @@ class ShareAPIControllerTest extends TestCase { private IURLGenerator&MockObject $urlGenerator; private IL10N&MockObject $l; private IConfig&MockObject $config; + private IAppConfig&MockObject $appConfig; private IAppManager&MockObject $appManager; private ContainerInterface&MockObject $serverContainer; private IUserStatusManager&MockObject $userStatusManager; @@ -75,6 +82,8 @@ class ShareAPIControllerTest extends TestCase { private LoggerInterface&MockObject $logger; private IProviderFactory&MockObject $factory; private IMailer&MockObject $mailer; + private ITagManager&MockObject $tagManager; + private TrustedServers&MockObject $trustedServers; protected function setUp(): void { $this->shareManager = $this->createMock(IManager::class); @@ -98,6 +107,7 @@ class ShareAPIControllerTest extends TestCase { return vsprintf($text, $parameters); }); $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); $this->appManager = $this->createMock(IAppManager::class); $this->serverContainer = $this->createMock(ContainerInterface::class); $this->userStatusManager = $this->createMock(IUserStatusManager::class); @@ -110,6 +120,8 @@ class ShareAPIControllerTest extends TestCase { $this->logger = $this->createMock(LoggerInterface::class); $this->factory = $this->createMock(IProviderFactory::class); $this->mailer = $this->createMock(IMailer::class); + $this->tagManager = $this->createMock(ITagManager::class); + $this->trustedServers = $this->createMock(TrustedServers::class); $this->ocs = new ShareAPIController( $this->appName, @@ -121,6 +133,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -129,8 +142,11 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, - $this->currentUser, + $this->tagManager, + $this->trustedServers, + $this->currentUser ); + } /** @@ -148,6 +164,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -156,13 +173,15 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, + $this->tagManager, + $this->trustedServers, $this->currentUser, - ])->setMethods(['formatShare']) + ])->onlyMethods(['formatShare']) ->getMock(); } private function newShare() { - return \OCP\Server::get(IManager::class)->newShare(); + return Server::get(IManager::class)->newShare(); } @@ -246,9 +265,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with($share->getNodeId()) - ->willReturn($node); + ->willReturn([$node]); $this->shareManager ->expects($this->once()) @@ -264,7 +283,7 @@ class ShareAPIControllerTest extends TestCase { $node->expects($this->once()) ->method('lock') ->with(ILockingProvider::LOCK_SHARED) - ->will($this->throwException(new LockedException('mypath'))); + ->willThrowException(new LockedException('mypath')); $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share])); $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share])); @@ -411,9 +430,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with($share->getNodeId()) - ->willReturn($share->getNode()); + ->willReturn([$share->getNode()]); $this->shareManager->expects($this->once()) ->method('deleteFromSelf') @@ -474,9 +493,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with($share->getNodeId()) - ->willReturn($share->getNode()); + ->willReturn([$share->getNode()]); $this->shareManager->expects($this->never()) ->method('deleteFromSelf'); @@ -506,9 +525,10 @@ class ShareAPIControllerTest extends TestCase { ->willReturn($mount); $userFolder = $this->createMock(Folder::class); - $userFolder - ->expects($this->exactly(2)) - ->method('getFirstNodeById') + $userFolder->method('getById') + ->with(2) + ->willReturn([$file]); + $userFolder->method('getFirstNodeById') ->with(2) ->willReturn($file); @@ -584,9 +604,9 @@ class ShareAPIControllerTest extends TestCase { $share->method('getToken')->willReturn($token); $share->method('getPassword')->willReturn($password); - if ($shareType === IShare::TYPE_USER || - $shareType === IShare::TYPE_GROUP || - $shareType === IShare::TYPE_LINK) { + if ($shareType === IShare::TYPE_USER + || $shareType === IShare::TYPE_GROUP + || $shareType === IShare::TYPE_LINK) { $share->method('getFullId')->willReturn('ocinternal:' . $id); } @@ -815,9 +835,7 @@ class ShareAPIControllerTest extends TestCase { return $data; } - /** - * @dataProvider dataGetShare - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetShare')] public function testGetShare(IShare $share, array $result): void { /** @var ShareAPIController&MockObject $ocs */ $ocs = $this->getMockBuilder(ShareAPIController::class) @@ -831,6 +849,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -839,9 +858,11 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, + $this->tagManager, + $this->trustedServers, $this->currentUser, - - ])->setMethods(['canAccessShare']) + ]) + ->onlyMethods(['canAccessShare']) ->getMock(); $ocs->expects($this->any()) @@ -862,7 +883,6 @@ class ShareAPIControllerTest extends TestCase { $userFolder->method('getById') ->with($share->getNodeId()) ->willReturn([$share->getNode()]); - $userFolder->method('getFirstNodeById') ->with($share->getNodeId()) ->willReturn($share->getNode()); @@ -901,9 +921,8 @@ class ShareAPIControllerTest extends TestCase { ]); $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC')); - $d = $ocs->getShare($share->getId())->getData()[0]; - - $this->assertEquals($result, $ocs->getShare($share->getId())->getData()[0]); + $data = $ocs->getShare($share->getId())->getData()[0]; + $this->assertEquals($result, $data); } @@ -911,7 +930,7 @@ class ShareAPIControllerTest extends TestCase { $this->expectException(OCSNotFoundException::class); $this->expectExceptionMessage('Wrong share ID, share does not exist'); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setSharedBy('initiator') ->setSharedWith('recipient') ->setShareOwner('owner'); @@ -942,7 +961,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getDirectoryListing') ->willReturn([$file1, $file2]); - $file1UserShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file1UserShareOwner = Server::get(IManager::class)->newShare(); $file1UserShareOwner->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -956,7 +975,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_USER, ]; - $file1UserShareInitiator = \OCP\Server::get(IManager::class)->newShare(); + $file1UserShareInitiator = Server::get(IManager::class)->newShare(); $file1UserShareInitiator->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('currentUser') @@ -970,7 +989,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_USER, ]; - $file1UserShareRecipient = \OCP\Server::get(IManager::class)->newShare(); + $file1UserShareRecipient = Server::get(IManager::class)->newShare(); $file1UserShareRecipient->setShareType(IShare::TYPE_USER) ->setSharedWith('currentUser') ->setSharedBy('initiator') @@ -984,7 +1003,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_USER, ]; - $file1UserShareOther = \OCP\Server::get(IManager::class)->newShare(); + $file1UserShareOther = Server::get(IManager::class)->newShare(); $file1UserShareOther->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -998,7 +1017,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_USER, ]; - $file1GroupShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file1GroupShareOwner = Server::get(IManager::class)->newShare(); $file1GroupShareOwner->setShareType(IShare::TYPE_GROUP) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1012,7 +1031,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_GROUP, ]; - $file1GroupShareRecipient = \OCP\Server::get(IManager::class)->newShare(); + $file1GroupShareRecipient = Server::get(IManager::class)->newShare(); $file1GroupShareRecipient->setShareType(IShare::TYPE_GROUP) ->setSharedWith('currentUserGroup') ->setSharedBy('initiator') @@ -1026,7 +1045,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_GROUP, ]; - $file1GroupShareOther = \OCP\Server::get(IManager::class)->newShare(); + $file1GroupShareOther = Server::get(IManager::class)->newShare(); $file1GroupShareOther->setShareType(IShare::TYPE_GROUP) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1035,7 +1054,7 @@ class ShareAPIControllerTest extends TestCase { ->setNode($file1) ->setId(108); - $file1LinkShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file1LinkShareOwner = Server::get(IManager::class)->newShare(); $file1LinkShareOwner->setShareType(IShare::TYPE_LINK) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1049,7 +1068,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_LINK, ]; - $file1EmailShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file1EmailShareOwner = Server::get(IManager::class)->newShare(); $file1EmailShareOwner->setShareType(IShare::TYPE_EMAIL) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1063,7 +1082,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_EMAIL, ]; - $file1CircleShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file1CircleShareOwner = Server::get(IManager::class)->newShare(); $file1CircleShareOwner->setShareType(IShare::TYPE_CIRCLE) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1077,7 +1096,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_CIRCLE, ]; - $file1RoomShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file1RoomShareOwner = Server::get(IManager::class)->newShare(); $file1RoomShareOwner->setShareType(IShare::TYPE_ROOM) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1091,7 +1110,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_ROOM, ]; - $file1RemoteShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file1RemoteShareOwner = Server::get(IManager::class)->newShare(); $file1RemoteShareOwner->setShareType(IShare::TYPE_REMOTE) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1106,7 +1125,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_REMOTE, ]; - $file1RemoteGroupShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file1RemoteGroupShareOwner = Server::get(IManager::class)->newShare(); $file1RemoteGroupShareOwner->setShareType(IShare::TYPE_REMOTE_GROUP) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1121,7 +1140,7 @@ class ShareAPIControllerTest extends TestCase { 'share_type' => IShare::TYPE_REMOTE_GROUP, ]; - $file2UserShareOwner = \OCP\Server::get(IManager::class)->newShare(); + $file2UserShareOwner = Server::get(IManager::class)->newShare(); $file2UserShareOwner->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -1449,9 +1468,7 @@ class ShareAPIControllerTest extends TestCase { return $data; } - /** - * @dataProvider dataGetShares - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetShares')] public function testGetShares(array $getSharesParameters, array $shares, array $extraShareTypes, array $expected): void { /** @var ShareAPIController&MockObject $ocs */ $ocs = $this->getMockBuilder(ShareAPIController::class) @@ -1465,6 +1482,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -1473,6 +1491,8 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, + $this->tagManager, + $this->trustedServers, $this->currentUser, ]) ->onlyMethods(['formatShare']) @@ -1534,81 +1554,125 @@ 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])); + } + + #[\PHPUnit\Framework\Attributes\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(); + #[\PHPUnit\Framework\Attributes\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() { @@ -1638,22 +1702,22 @@ class ShareAPIControllerTest extends TestCase { } /** - * @dataProvider dataCanAccessRoomShare * * @param bool $expects * @param IShare $share * @param bool helperAvailable * @param bool canAccessShareByHelper */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataCanAccessRoomShare')] public function testCanAccessRoomShare(bool $expected, IShare $share, bool $helperAvailable, bool $canAccessShareByHelper): void { $userFolder = $this->getMockBuilder(Folder::class)->getMock(); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with($share->getNodeId()) - ->willReturn($share->getNode()); + ->willReturn([$share->getNode()]); if (!$helperAvailable) { $this->appManager->method('isEnabledForUser') @@ -1664,8 +1728,11 @@ class ShareAPIControllerTest extends TestCase { ->with('spreed') ->willReturn(true); - $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') - ->setMethods(['canAccessShare']) + // This is not possible anymore with PHPUnit 10+ + // as `setMethods` was removed and now real reflection is used, thus the class needs to exist. + // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + $helper = $this->getMockBuilder(\stdClass::class) + ->addMethods(['canAccessShare']) ->getMock(); $helper->method('canAccessShare') ->with($share, $this->currentUser) @@ -1701,7 +1768,7 @@ class ShareAPIControllerTest extends TestCase { $userFolder->expects($this->once()) ->method('get') ->with('invalid-path') - ->will($this->throwException(new NotFoundException())); + ->willThrowException(new NotFoundException()); $this->ocs->createShare('invalid-path'); } @@ -1741,8 +1808,7 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('newShare')->willReturn($share); [$userFolder, $path] = $this->getNonSharedUserFile(); - $this->rootFolder->expects($this->exactly(2)) - ->method('getUserFolder') + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); @@ -1769,8 +1835,7 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('newShare')->willReturn($share); [$userFolder, $path] = $this->getNonSharedUserFile(); - $this->rootFolder->expects($this->exactly(2)) - ->method('getUserFolder') + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); @@ -1806,6 +1871,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -1814,8 +1880,10 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, + $this->tagManager, + $this->trustedServers, $this->currentUser, - ])->setMethods(['formatShare']) + ])->onlyMethods(['formatShare']) ->getMock(); [$userFolder, $path] = $this->getNonSharedUserFile(); @@ -1839,15 +1907,15 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('createShare') ->with($this->callback(function (IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getPermissions() === ( - Constants::PERMISSION_ALL & - ~Constants::PERMISSION_DELETE & - ~Constants::PERMISSION_CREATE - ) && - $share->getShareType() === IShare::TYPE_USER && - $share->getSharedWith() === 'validUser' && - $share->getSharedBy() === 'currentUser'; + return $share->getNode() === $path + && $share->getPermissions() === ( + Constants::PERMISSION_ALL + & ~Constants::PERMISSION_DELETE + & ~Constants::PERMISSION_CREATE + ) + && $share->getShareType() === IShare::TYPE_USER + && $share->getSharedWith() === 'validUser' + && $share->getSharedBy() === 'currentUser'; })) ->willReturnArgument(0); @@ -1869,8 +1937,7 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('allowGroupSharing')->willReturn(true); [$userFolder, $path] = $this->getNonSharedUserFile(); - $this->rootFolder->expects($this->exactly(2)) - ->method('getUserFolder') + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); @@ -1904,6 +1971,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -1912,8 +1980,10 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, + $this->tagManager, + $this->trustedServers, $this->currentUser, - ])->setMethods(['formatShare']) + ])->onlyMethods(['formatShare']) ->getMock(); $this->request @@ -1950,11 +2020,11 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('createShare') ->with($this->callback(function (IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getPermissions() === Constants::PERMISSION_ALL && - $share->getShareType() === IShare::TYPE_GROUP && - $share->getSharedWith() === 'validGroup' && - $share->getSharedBy() === 'currentUser'; + return $share->getNode() === $path + && $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getShareType() === IShare::TYPE_GROUP + && $share->getSharedWith() === 'validGroup' + && $share->getSharedBy() === 'currentUser'; })) ->willReturnArgument(0); @@ -1974,8 +2044,7 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('newShare')->willReturn($share); [$userFolder, $path] = $this->getNonSharedUserFolder(); - $this->rootFolder->expects($this->exactly(2)) - ->method('getUserFolder') + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); @@ -2021,7 +2090,7 @@ class ShareAPIControllerTest extends TestCase { $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); $this->shareManager->method('shareApiAllowLinks')->willReturn(false); @@ -2047,7 +2116,7 @@ class ShareAPIControllerTest extends TestCase { $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true'); @@ -2068,13 +2137,13 @@ class ShareAPIControllerTest extends TestCase { $file = $this->createMock(File::class); $file->method('getId')->willReturn(42); $file->method('getStorage')->willReturn($storage); - + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); $this->rootFolder->method('get')->with('valid-path')->willReturn($file); $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); @@ -2098,18 +2167,18 @@ class ShareAPIControllerTest extends TestCase { $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); $this->shareManager->expects($this->once())->method('createShare')->with( $this->callback(function (IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getShareType() === IShare::TYPE_LINK && - $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) && - $share->getSharedBy() === 'currentUser' && - $share->getPassword() === null && - $share->getExpirationDate() === null; + return $share->getNode() === $path + && $share->getShareType() === IShare::TYPE_LINK + && $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getSharedBy() === 'currentUser' + && $share->getPassword() === null + && $share->getExpirationDate() === null; }) )->willReturnArgument(0); @@ -2137,7 +2206,7 @@ class ShareAPIControllerTest extends TestCase { $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); @@ -2176,7 +2245,7 @@ class ShareAPIControllerTest extends TestCase { $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); @@ -2184,13 +2253,13 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('createShare')->with( $this->callback(function (IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getShareType() === IShare::TYPE_LINK && - $share->getPermissions() === (Constants::PERMISSION_ALL & ~(Constants::PERMISSION_SHARE)) && - $share->getSharedBy() === 'currentUser' && - $share->getPassword() === 'password' && - $share->getSendPasswordByTalk() === true && - $share->getExpirationDate() === null; + return $share->getNode() === $path + && $share->getShareType() === IShare::TYPE_LINK + && $share->getPermissions() === (Constants::PERMISSION_ALL & ~(Constants::PERMISSION_SHARE)) + && $share->getSharedBy() === 'currentUser' + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === null; }) )->willReturnArgument(0); @@ -2223,7 +2292,7 @@ class ShareAPIControllerTest extends TestCase { $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); @@ -2261,7 +2330,7 @@ class ShareAPIControllerTest extends TestCase { $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); @@ -2270,12 +2339,12 @@ class ShareAPIControllerTest extends TestCase { $date = new \DateTime('2000-01-01'); $date->setTime(0, 0, 0); - return $share->getNode() === $path && - $share->getShareType() === IShare::TYPE_LINK && - $share->getPermissions() === Constants::PERMISSION_READ | Constants::PERMISSION_SHARE && - $share->getSharedBy() === 'currentUser' && - $share->getPassword() === null && - $share->getExpirationDate() == $date; + return $share->getNode() === $path + && $share->getShareType() === IShare::TYPE_LINK + && $share->getPermissions() === Constants::PERMISSION_READ | Constants::PERMISSION_SHARE + && $share->getSharedBy() === 'currentUser' + && $share->getPassword() === null + && $share->getExpirationDate() == $date; }) )->willReturnArgument(0); @@ -2307,7 +2376,7 @@ class ShareAPIControllerTest extends TestCase { $this->rootFolder->method('getById') ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OCP\Server::get(IManager::class)->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); @@ -2330,6 +2399,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -2338,8 +2408,10 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, + $this->tagManager, + $this->trustedServers, $this->currentUser, - ])->setMethods(['formatShare']) + ])->onlyMethods(['formatShare']) ->getMock(); [$userFolder, $path] = $this->getNonSharedUserFile(); @@ -2363,15 +2435,15 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('createShare') ->with($this->callback(function (IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getPermissions() === ( - Constants::PERMISSION_ALL & - ~Constants::PERMISSION_DELETE & - ~Constants::PERMISSION_CREATE - ) && - $share->getShareType() === IShare::TYPE_REMOTE && - $share->getSharedWith() === 'user@example.org' && - $share->getSharedBy() === 'currentUser'; + return $share->getNode() === $path + && $share->getPermissions() === ( + Constants::PERMISSION_ALL + & ~Constants::PERMISSION_DELETE + & ~Constants::PERMISSION_CREATE + ) + && $share->getShareType() === IShare::TYPE_REMOTE + && $share->getSharedWith() === 'user@example.org' + && $share->getSharedBy() === 'currentUser'; })) ->willReturnArgument(0); @@ -2400,6 +2472,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -2408,8 +2481,10 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, + $this->tagManager, + $this->trustedServers, $this->currentUser, - ])->setMethods(['formatShare']) + ])->onlyMethods(['formatShare']) ->getMock(); [$userFolder, $path] = $this->getNonSharedUserFile(); @@ -2433,15 +2508,15 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('createShare') ->with($this->callback(function (IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getPermissions() === ( - Constants::PERMISSION_ALL & - ~Constants::PERMISSION_DELETE & - ~Constants::PERMISSION_CREATE - ) && - $share->getShareType() === IShare::TYPE_REMOTE_GROUP && - $share->getSharedWith() === 'group@example.org' && - $share->getSharedBy() === 'currentUser'; + return $share->getNode() === $path + && $share->getPermissions() === ( + Constants::PERMISSION_ALL + & ~Constants::PERMISSION_DELETE + & ~Constants::PERMISSION_CREATE + ) + && $share->getShareType() === IShare::TYPE_REMOTE_GROUP + && $share->getSharedWith() === 'group@example.org' + && $share->getSharedBy() === 'currentUser'; })) ->willReturnArgument(0); @@ -2481,16 +2556,19 @@ class ShareAPIControllerTest extends TestCase { ->with('spreed') ->willReturn(true); - $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') - ->setMethods(['createShare']) + // This is not possible anymore with PHPUnit 10+ + // as `setMethods` was removed and now real reflection is used, thus the class needs to exist. + // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + $helper = $this->getMockBuilder(\stdClass::class) + ->addMethods(['createShare']) ->getMock(); $helper->method('createShare') ->with( $share, 'recipientRoom', - Constants::PERMISSION_ALL & - ~Constants::PERMISSION_DELETE & - ~Constants::PERMISSION_CREATE, + Constants::PERMISSION_ALL + & ~Constants::PERMISSION_DELETE + & ~Constants::PERMISSION_CREATE, '' )->willReturnCallback( function ($share): void { @@ -2531,8 +2609,7 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('newShare')->willReturn($share); [$userFolder, $path] = $this->getNonSharedUserFolder(); - $this->rootFolder->expects($this->exactly(2)) - ->method('getUserFolder') + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); @@ -2569,8 +2646,7 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('newShare')->willReturn($share); [$userFolder, $path] = $this->getNonSharedUserFile(); - $this->rootFolder->expects($this->exactly(2)) - ->method('getUserFolder') + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); @@ -2589,7 +2665,10 @@ class ShareAPIControllerTest extends TestCase { ->with('spreed') ->willReturn(true); - $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + // This is not possible anymore with PHPUnit 10+ + // as `setMethods` was removed and now real reflection is used, thus the class needs to exist. + // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + $helper = $this->getMockBuilder(\stdClass::class) ->addMethods(['createShare']) ->getMock(); $helper->method('createShare') @@ -2618,7 +2697,7 @@ class ShareAPIControllerTest extends TestCase { * TODO: Remove once proper solution is in place */ public function testCreateReshareOfFederatedMountNoDeletePermissions(): void { - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $this->shareManager->method('newShare')->willReturn($share); /** @var ShareAPIController&MockObject $ocs */ @@ -2633,6 +2712,7 @@ class ShareAPIControllerTest extends TestCase { $this->urlGenerator, $this->l, $this->config, + $this->appConfig, $this->appManager, $this->serverContainer, $this->userStatusManager, @@ -2641,8 +2721,10 @@ class ShareAPIControllerTest extends TestCase { $this->logger, $this->factory, $this->mailer, + $this->tagManager, + $this->trustedServers, $this->currentUser, - ])->setMethods(['formatShare']) + ])->onlyMethods(['formatShare']) ->getMock(); $userFolder = $this->getMockBuilder(Folder::class)->getMock(); @@ -2703,9 +2785,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with($share->getNodeId()) - ->willReturn($share->getNode()); + ->willReturn([$share->getNode()]); $this->ocs->updateShare(42); } @@ -2778,14 +2860,14 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('updateShare')->with( $this->callback(function (IShare $share) { - return $share->getPermissions() === Constants::PERMISSION_READ && - $share->getPassword() === null && - $share->getExpirationDate() === null && + return $share->getPermissions() === Constants::PERMISSION_READ + && $share->getPassword() === null + && $share->getExpirationDate() === null // Once set a note or a label are never back to null, only to an // empty string. - $share->getNote() === '' && - $share->getLabel() === '' && - $share->getHideDownload() === false; + && $share->getNote() === '' + && $share->getLabel() === '' + && $share->getHideDownload() === false; }) )->willReturnArgument(0); @@ -2796,6 +2878,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); + $userFolder->method('getById') + ->with(42) + ->willReturn([$node]); $userFolder->method('getFirstNodeById') ->with(42) ->willReturn($node); @@ -2820,7 +2905,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -2834,12 +2919,12 @@ class ShareAPIControllerTest extends TestCase { $date = new \DateTime('2000-01-01'); $date->setTime(0, 0, 0); - return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getExpirationDate() == $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getPassword() === 'password' + && $share->getExpirationDate() == $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -2850,9 +2935,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -2867,9 +2952,7 @@ class ShareAPIControllerTest extends TestCase { $this->assertEquals($expected->getData(), $result->getData()); } - /** - * @dataProvider publicUploadParamsProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('publicUploadParamsProvider')] public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password): void { $ocs = $this->mockFormatShare(); @@ -2877,7 +2960,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -2890,9 +2973,9 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('updateShare')->with( $this->callback(function (IShare $share) { - return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getExpirationDate() === null; + return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getPassword() === 'password' + && $share->getExpirationDate() === null; }) )->willReturnArgument(0); @@ -2900,9 +2983,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -2918,7 +3001,7 @@ class ShareAPIControllerTest extends TestCase { } - public function publicLinkValidPermissionsProvider() { + public static function publicLinkValidPermissionsProvider() { return [ [Constants::PERMISSION_CREATE], [Constants::PERMISSION_READ], @@ -2928,9 +3011,7 @@ class ShareAPIControllerTest extends TestCase { ]; } - /** - * @dataProvider publicLinkValidPermissionsProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('publicLinkValidPermissionsProvider')] public function testUpdateLinkShareSetCRUDPermissions($permissions): void { $ocs = $this->mockFormatShare(); @@ -2938,7 +3019,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -2958,9 +3039,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -2975,7 +3056,7 @@ class ShareAPIControllerTest extends TestCase { $this->assertEquals($expected->getData(), $result->getData()); } - public function publicLinkInvalidPermissionsProvider1() { + public static function publicLinkInvalidPermissionsProvider1() { return [ [Constants::PERMISSION_DELETE], [Constants::PERMISSION_UPDATE], @@ -2983,9 +3064,7 @@ class ShareAPIControllerTest extends TestCase { ]; } - /** - * @dataProvider publicLinkInvalidPermissionsProvider1 - */ + #[\PHPUnit\Framework\Attributes\DataProvider('publicLinkInvalidPermissionsProvider1')] public function testUpdateLinkShareSetInvalidCRUDPermissions1($permissions): void { $this->expectException(OCSBadRequestException::class); $this->expectExceptionMessage('Share must at least have READ or CREATE permissions'); @@ -2993,16 +3072,14 @@ class ShareAPIControllerTest extends TestCase { $this->testUpdateLinkShareSetCRUDPermissions($permissions, null); } - public function publicLinkInvalidPermissionsProvider2() { + public static function publicLinkInvalidPermissionsProvider2() { return [ [Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE], [Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE], ]; } - /** - * @dataProvider publicLinkInvalidPermissionsProvider2 - */ + #[\PHPUnit\Framework\Attributes\DataProvider('publicLinkInvalidPermissionsProvider2')] public function testUpdateLinkShareSetInvalidCRUDPermissions2($permissions): void { $this->expectException(OCSBadRequestException::class); $this->expectExceptionMessage('Share must have READ permission if UPDATE or DELETE permission is set'); @@ -3016,9 +3093,9 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); [$userFolder, $folder] = $this->getNonSharedUserFolder(); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3026,7 +3103,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -3038,7 +3115,7 @@ class ShareAPIControllerTest extends TestCase { $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-a'); } - public function publicUploadParamsProvider() { + public static function publicUploadParamsProvider() { return [ [null, 'true', null, 'password'], // legacy had no delete @@ -3054,25 +3131,23 @@ class ShareAPIControllerTest extends TestCase { ]; } - /** - * @dataProvider publicUploadParamsProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('publicUploadParamsProvider')] public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password): void { $this->expectException(OCSForbiddenException::class); $this->expectExceptionMessage('Public upload disabled by the administrator'); $ocs = $this->mockFormatShare(); [$userFolder, $folder] = $this->getNonSharedUserFolder(); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); $folder->method('getId')->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -3091,15 +3166,18 @@ class ShareAPIControllerTest extends TestCase { $ocs = $this->mockFormatShare(); - [$userFolder, $file] = $this->getNonSharedUserFile(); - $userFolder->method('getFirstNodeById') + $file = $this->getMockBuilder(File::class)->getMock(); + $file->method('getId') + ->willReturn(42); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $userFolder->method('getById') ->with(42) - ->willReturn($file); + ->willReturn([$folder]); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -3128,9 +3206,9 @@ class ShareAPIControllerTest extends TestCase { [$userFolder, $node] = $this->getNonSharedUserFolder(); $node->method('getId')->willReturn(42); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($node); + ->willReturn([$node]); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3155,13 +3233,13 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('updateShare')->with( $this->callback(function (IShare $share) use ($date) { - return $share->getPermissions() === Constants::PERMISSION_ALL && - $share->getPassword() === 'newpassword' && - $share->getSendPasswordByTalk() === true && - $share->getExpirationDate() === $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'newpassword' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -3179,9 +3257,9 @@ class ShareAPIControllerTest extends TestCase { $date->setTime(0, 0, 0); [$userFolder, $node] = $this->getNonSharedUserFolder(); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($node); + ->willReturn([$node]); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3209,13 +3287,13 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('updateShare')->with( $this->callback(function (IShare $share) use ($date) { - return $share->getPermissions() === Constants::PERMISSION_ALL && - $share->getPassword() === 'password' && - $share->getSendPasswordByTalk() === true && - $share->getExpirationDate() === $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -3237,9 +3315,9 @@ class ShareAPIControllerTest extends TestCase { $date->setTime(0, 0, 0); [$userFolder, $node] = $this->getNonSharedUserFolder(); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($node); + ->willReturn([$node]); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3277,9 +3355,9 @@ class ShareAPIControllerTest extends TestCase { $date->setTime(0, 0, 0); [$userFolder, $node] = $this->getNonSharedUserFolder(); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($node); + ->willReturn([$node]); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) ->willReturn($userFolder); @@ -3307,13 +3385,13 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('updateShare')->with( $this->callback(function (IShare $share) use ($date) { - return $share->getPermissions() === Constants::PERMISSION_ALL && - $share->getPassword() === 'password' && - $share->getSendPasswordByTalk() === false && - $share->getExpirationDate() === $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === false + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -3357,13 +3435,13 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('updateShare')->with( $this->callback(function (IShare $share) use ($date) { - return $share->getPermissions() === Constants::PERMISSION_ALL && - $share->getPassword() === 'password' && - $share->getSendPasswordByTalk() === false && - $share->getExpirationDate() === $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === false + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -3371,9 +3449,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($node); + ->willReturn([$node]); $mountPoint = $this->createMock(IMountPoint::class); $node->method('getMountPoint') @@ -3425,13 +3503,13 @@ class ShareAPIControllerTest extends TestCase { $date = new \DateTime('2010-12-23'); $date->setTime(0, 0, 0); - return $share->getPermissions() === Constants::PERMISSION_ALL && - $share->getPassword() === 'password' && - $share->getSendPasswordByTalk() === true && - $share->getExpirationDate() == $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() == $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -3439,9 +3517,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($node); + ->willReturn([$node]); $mountPoint = $this->createMock(IMountPoint::class); $node->method('getMountPoint') @@ -3465,7 +3543,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -3483,13 +3561,13 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('updateShare')->with( $this->callback(function (IShare $share) use ($date) { - return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getSendPasswordByTalk() === true && - $share->getExpirationDate() === $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -3500,9 +3578,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -3526,7 +3604,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -3544,13 +3622,13 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->expects($this->once())->method('updateShare')->with( $this->callback(function (IShare $share) use ($date): bool { - return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getSendPasswordByTalk() === true && - $share->getExpirationDate() === $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -3560,9 +3638,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -3586,7 +3664,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_LINK) @@ -3606,13 +3684,13 @@ class ShareAPIControllerTest extends TestCase { ->method('updateShare') ->with( $this->callback(function (IShare $share) use ($date) { - return $share->getPermissions() === Constants::PERMISSION_ALL && - $share->getPassword() === 'password' && - $share->getSendPasswordByTalk() === true && - $share->getExpirationDate() === $date && - $share->getNote() === 'note' && - $share->getLabel() === 'label' && - $share->getHideDownload() === true; + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) )->willReturnArgument(0); @@ -3620,9 +3698,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -3646,7 +3724,7 @@ class ShareAPIControllerTest extends TestCase { $file->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) ->setShareType(IShare::TYPE_USER) @@ -3668,9 +3746,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($file); + ->willReturn([$file]); $mountPoint = $this->createMock(IMountPoint::class); $file->method('getMountPoint') @@ -3692,7 +3770,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share ->setId(42) ->setSharedBy($this->currentUser) @@ -3704,7 +3782,7 @@ class ShareAPIControllerTest extends TestCase { // note: updateShare will modify the received instance but getSharedWith will reread from the database, // so their values will be different - $incomingShare = \OCP\Server::get(IManager::class)->newShare(); + $incomingShare = Server::get(IManager::class)->newShare(); $incomingShare ->setId(42) ->setSharedBy($this->currentUser) @@ -3734,6 +3812,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); $userFolder->method('getFirstNodeById') ->with(42) ->willReturn($folder); @@ -3764,7 +3845,7 @@ class ShareAPIControllerTest extends TestCase { $folder->method('getId') ->willReturn(42); - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share ->setId(42) ->setSharedBy($this->currentUser) @@ -3776,7 +3857,7 @@ class ShareAPIControllerTest extends TestCase { // note: updateShare will modify the received instance but getSharedWith will reread from the database, // so their values will be different - $incomingShare = \OCP\Server::get(IManager::class)->newShare(); + $incomingShare = Server::get(IManager::class)->newShare(); $incomingShare ->setId(42) ->setSharedBy($this->currentUser) @@ -3804,9 +3885,9 @@ class ShareAPIControllerTest extends TestCase { ->with($this->currentUser) ->willReturn($userFolder); - $userFolder->method('getFirstNodeById') + $userFolder->method('getById') ->with(42) - ->willReturn($folder); + ->willReturn([$folder]); $mountPoint = $this->createMock(IMountPoint::class); $folder->method('getMountPoint') @@ -3834,9 +3915,10 @@ class ShareAPIControllerTest extends TestCase { ->willReturn($mount); $userFolder = $this->createMock(Folder::class); - $userFolder - ->expects($this->exactly(2)) - ->method('getFirstNodeById') + $userFolder->method('getById') + ->with(2) + ->willReturn([$file]); + $userFolder->method('getFirstNodeById') ->with(2) ->willReturn($file); @@ -3938,7 +4020,7 @@ class ShareAPIControllerTest extends TestCase { $result = []; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -4040,7 +4122,7 @@ class ShareAPIControllerTest extends TestCase { ], false ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -4094,7 +4176,7 @@ class ShareAPIControllerTest extends TestCase { ], $share, [], false ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -4150,7 +4232,7 @@ class ShareAPIControllerTest extends TestCase { // with existing group - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_GROUP) ->setSharedWith('recipientGroup') ->setSharedBy('initiator') @@ -4204,7 +4286,7 @@ class ShareAPIControllerTest extends TestCase { ]; // with unknown group / no group backend - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_GROUP) ->setSharedWith('recipientGroup2') ->setSharedBy('initiator') @@ -4255,7 +4337,7 @@ class ShareAPIControllerTest extends TestCase { ], $share, [], false ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_LINK) ->setSharedBy('initiator') ->setShareOwner('owner') @@ -4314,7 +4396,7 @@ class ShareAPIControllerTest extends TestCase { ], $share, [], false ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_LINK) ->setSharedBy('initiator') ->setShareOwner('owner') @@ -4373,7 +4455,7 @@ class ShareAPIControllerTest extends TestCase { ], $share, [], false ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_REMOTE) ->setSharedBy('initiator') ->setSharedWith('user@server.com') @@ -4423,10 +4505,11 @@ class ShareAPIControllerTest extends TestCase { 'mount-type' => '', 'attributes' => null, 'item_permissions' => 1, + 'is_trusted_server' => false, ], $share, [], false ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_REMOTE_GROUP) ->setSharedBy('initiator') ->setSharedWith('user@server.com') @@ -4476,11 +4559,12 @@ class ShareAPIControllerTest extends TestCase { 'mount-type' => '', 'attributes' => null, 'item_permissions' => 1, + 'is_trusted_server' => false, ], $share, [], false ]; // Circle with id, display name and avatar set by the Circles app - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_CIRCLE) ->setSharedBy('initiator') ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]') @@ -4536,7 +4620,7 @@ class ShareAPIControllerTest extends TestCase { ]; // Circle with id set by the Circles app - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_CIRCLE) ->setSharedBy('initiator') ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]') @@ -4589,7 +4673,7 @@ class ShareAPIControllerTest extends TestCase { ]; // Circle with id not set by the Circles app - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_CIRCLE) ->setSharedBy('initiator') ->setSharedWith('Circle (Public circle, circleOwner)') @@ -4641,7 +4725,7 @@ class ShareAPIControllerTest extends TestCase { ], $share, [], false ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_USER) ->setSharedBy('initiator') ->setSharedWith('recipient') @@ -4656,7 +4740,7 @@ class ShareAPIControllerTest extends TestCase { [], $share, [], true ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_EMAIL) ->setSharedBy('initiator') ->setSharedWith('user@server.com') @@ -4711,7 +4795,7 @@ class ShareAPIControllerTest extends TestCase { ], $share, [], false ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_EMAIL) ->setSharedBy('initiator') ->setSharedWith('user@server.com') @@ -4768,7 +4852,7 @@ class ShareAPIControllerTest extends TestCase { ]; // Preview is available - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') @@ -4825,13 +4909,13 @@ class ShareAPIControllerTest extends TestCase { } /** - * @dataProvider dataFormatShare * * @param array $expects * @param IShare $share * @param array $users * @param $exception */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataFormatShare')] public function testFormatShare(array $expects, IShare $share, array $users, $exception): void { $this->userManager->method('get')->willReturnMap($users); @@ -4934,7 +5018,7 @@ class ShareAPIControllerTest extends TestCase { $result = []; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_ROOM) ->setSharedWith('recipientRoom') ->setSharedBy('initiator') @@ -4986,7 +5070,7 @@ class ShareAPIControllerTest extends TestCase { ], $share, false, [] ]; - $share = \OCP\Server::get(IManager::class)->newShare(); + $share = Server::get(IManager::class)->newShare(); $share->setShareType(IShare::TYPE_ROOM) ->setSharedWith('recipientRoom') ->setSharedBy('initiator') @@ -5044,13 +5128,13 @@ class ShareAPIControllerTest extends TestCase { } /** - * @dataProvider dataFormatRoomShare * * @param array $expects * @param IShare $share * @param bool $helperAvailable * @param array $formatShareByHelper */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataFormatRoomShare')] public function testFormatRoomShare(array $expects, IShare $share, bool $helperAvailable, array $formatShareByHelper): void { $this->rootFolder->method('getUserFolder') ->with($this->currentUser) @@ -5073,8 +5157,11 @@ class ShareAPIControllerTest extends TestCase { ->with('spreed') ->willReturn(true); - $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') - ->setMethods(['formatShare', 'canAccessShare']) + // This is not possible anymore with PHPUnit 10+ + // as `setMethods` was removed and now real reflection is used, thus the class needs to exist. + // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + $helper = $this->getMockBuilder(\stdClass::class) + ->addMethods(['formatShare', 'canAccessShare']) ->getMock(); $helper->method('formatShare') ->with($share) @@ -5107,6 +5194,9 @@ class ShareAPIControllerTest extends TestCase { $userFolder->method('getStorage')->willReturn($storage); $node->method('getStorage')->willReturn($storage); $node->method('getId')->willReturn(42); + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn($this->currentUser); + $node->method('getOwner')->willReturn($user); return [$userFolder, $node]; } @@ -5127,4 +5217,164 @@ class ShareAPIControllerTest extends TestCase { $node->method('getId')->willReturn(42); return [$userFolder, $node]; } + + public function testPopulateTags(): void { + $tagger = $this->createMock(ITags::class); + $this->tagManager->method('load') + ->with('files') + ->willReturn($tagger); + $data = [ + ['file_source' => 10], + ['file_source' => 22, 'foo' => 'bar'], + ['file_source' => 42, 'x' => 'y'], + ]; + $tags = [ + 10 => ['tag3'], + 42 => ['tag1', 'tag2'], + ]; + $tagger->method('getTagsForObjects') + ->with([10, 22, 42]) + ->willReturn($tags); + + $result = self::invokePrivate($this->ocs, 'populateTags', [$data]); + $this->assertSame([ + ['file_source' => 10, 'tags' => ['tag3']], + ['file_source' => 22, 'foo' => 'bar', 'tags' => []], + ['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']], + ], $result); + } + + public function trustedServerProvider(): array { + return [ + 'Trusted server' => [true, true], + 'Untrusted server' => [false, false], + ]; + } + + /** + * @dataProvider trustedServerProvider + */ + public function testFormatShareWithFederatedShare(bool $isKnownServer, bool $isTrusted): void { + $nodeId = 12; + $nodePath = '/test.txt'; + $share = $this->createShare( + 1, + IShare::TYPE_REMOTE, + 'recipient@remoteserver.com', // shared with + 'sender@testserver.com', // shared by + 'shareOwner', // share owner + $nodePath, // path + Constants::PERMISSION_READ, + time(), + null, + null, + $nodePath, + $nodeId + ); + + $node = $this->createMock(\OCP\Files\File::class); + $node->method('getId')->willReturn($nodeId); + $node->method('getPath')->willReturn($nodePath); + $node->method('getInternalPath')->willReturn(ltrim($nodePath, '/')); + $mountPoint = $this->createMock(\OCP\Files\Mount\IMountPoint::class); + $mountPoint->method('getMountType')->willReturn('local'); + $node->method('getMountPoint')->willReturn($mountPoint); + $node->method('getMimetype')->willReturn('text/plain'); + $storage = $this->createMock(\OCP\Files\Storage\IStorage::class); + $storageCache = $this->createMock(\OCP\Files\Cache\ICache::class); + $storageCache->method('getNumericStorageId')->willReturn(1); + $storage->method('getCache')->willReturn($storageCache); + $storage->method('getId')->willReturn('home::shareOwner'); + $node->method('getStorage')->willReturn($storage); + $parent = $this->createMock(\OCP\Files\Folder::class); + $parent->method('getId')->willReturn(2); + $node->method('getParent')->willReturn($parent); + $node->method('getSize')->willReturn(1234); + $node->method('getMTime')->willReturn(1234567890); + + $this->previewManager->method('isAvailable')->with($node)->willReturn(false); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturnSelf(); + + $this->rootFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($node); + + $this->rootFolder->method('getRelativePath') + ->with($node->getPath()) + ->willReturnArgument(0); + + $serverName = 'remoteserver.com'; + $this->trustedServers->method('isTrustedServer') + ->with($serverName) + ->willReturn($isKnownServer); + + $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]); + + $this->assertSame($isTrusted, $result['is_trusted_server']); + } + + public function testFormatShareWithFederatedShareWithAtInUsername(): void { + $nodeId = 12; + $nodePath = '/test.txt'; + $share = $this->createShare( + 1, + IShare::TYPE_REMOTE, + 'recipient@domain.com@remoteserver.com', + 'sender@testserver.com', + 'shareOwner', + $nodePath, + Constants::PERMISSION_READ, + time(), + null, + null, + $nodePath, + $nodeId + ); + + $node = $this->createMock(\OCP\Files\File::class); + $node->method('getId')->willReturn($nodeId); + $node->method('getPath')->willReturn($nodePath); + $node->method('getInternalPath')->willReturn(ltrim($nodePath, '/')); + $mountPoint = $this->createMock(\OCP\Files\Mount\IMountPoint::class); + $mountPoint->method('getMountType')->willReturn('local'); + $node->method('getMountPoint')->willReturn($mountPoint); + $node->method('getMimetype')->willReturn('text/plain'); + $storage = $this->createMock(\OCP\Files\Storage\IStorage::class); + $storageCache = $this->createMock(\OCP\Files\Cache\ICache::class); + $storageCache->method('getNumericStorageId')->willReturn(1); + $storage->method('getCache')->willReturn($storageCache); + $storage->method('getId')->willReturn('home::shareOwner'); + $node->method('getStorage')->willReturn($storage); + $parent = $this->createMock(\OCP\Files\Folder::class); + $parent->method('getId')->willReturn(2); + $node->method('getParent')->willReturn($parent); + $node->method('getSize')->willReturn(1234); + $node->method('getMTime')->willReturn(1234567890); + + $this->previewManager->method('isAvailable')->with($node)->willReturn(false); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturnSelf(); + + $this->rootFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($node); + + $this->rootFolder->method('getRelativePath') + ->with($node->getPath()) + ->willReturnArgument(0); + + $serverName = 'remoteserver.com'; + $this->trustedServers->method('isTrustedServer') + ->with($serverName) + ->willReturn(true); + + $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]); + + $this->assertTrue($result['is_trusted_server']); + } } |