diff options
Diffstat (limited to 'tests/lib/Share20')
-rw-r--r-- | tests/lib/Share20/DefaultShareProviderTest.php | 528 | ||||
-rw-r--r-- | tests/lib/Share20/LegacyHooksTest.php | 80 | ||||
-rw-r--r-- | tests/lib/Share20/ManagerTest.php | 1393 | ||||
-rw-r--r-- | tests/lib/Share20/ShareByMailProviderTest.php | 31 | ||||
-rw-r--r-- | tests/lib/Share20/ShareHelperTest.php | 50 | ||||
-rw-r--r-- | tests/lib/Share20/ShareTest.php | 46 |
6 files changed, 1237 insertions, 891 deletions
diff --git a/tests/lib/Share20/DefaultShareProviderTest.php b/tests/lib/Share20/DefaultShareProviderTest.php index 315ee66bb31..bacf2b61ee3 100644 --- a/tests/lib/Share20/DefaultShareProviderTest.php +++ b/tests/lib/Share20/DefaultShareProviderTest.php @@ -1,35 +1,26 @@ <?php + /** - * @author Roeland Jago Douma <rullzer@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Share20; +use OC\Files\Node\Node; use OC\Share20\DefaultShareProvider; +use OC\Share20\Exception\ProviderException; +use OC\Share20\Share; use OC\Share20\ShareAttributes; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Constants; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Defaults; use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\IRootFolder; +use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroup; use OCP\IGroupManager; @@ -39,9 +30,12 @@ use OCP\IUser; use OCP\IUserManager; use OCP\L10N\IFactory; use OCP\Mail\IMailer; +use OCP\Server; +use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as IShareManager; use OCP\Share\IShare; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; /** * Class DefaultShareProviderTest @@ -53,38 +47,45 @@ class DefaultShareProviderTest extends \Test\TestCase { /** @var IDBConnection */ protected $dbConn; - /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */ + /** @var IUserManager | MockObject */ protected $userManager; - /** @var IGroupManager | \PHPUnit\Framework\MockObject\MockObject */ + /** @var IGroupManager | MockObject */ protected $groupManager; - /** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */ + /** @var IRootFolder | MockObject */ protected $rootFolder; /** @var DefaultShareProvider */ protected $provider; - /** @var \PHPUnit\Framework\MockObject\MockObject|IMailer */ + /** @var MockObject|IMailer */ protected $mailer; /** @var IFactory|MockObject */ protected $l10nFactory; - /** @var \PHPUnit\Framework\MockObject\MockObject|IL10N */ + /** @var MockObject|IL10N */ protected $l10n; - /** @var \PHPUnit\Framework\MockObject\MockObject|Defaults */ + /** @var MockObject|Defaults */ protected $defaults; - /** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */ + /** @var MockObject|IURLGenerator */ protected $urlGenerator; /** @var ITimeFactory|MockObject */ protected $timeFactory; + /** @var LoggerInterface|MockObject */ + protected $logger; + + protected IConfig&MockObject $config; + + protected IShareManager&MockObject $shareManager; + protected function setUp(): void { - $this->dbConn = \OC::$server->getDatabaseConnection(); + $this->dbConn = Server::get(IDBConnection::class); $this->userManager = $this->createMock(IUserManager::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->rootFolder = $this->createMock(IRootFolder::class); @@ -94,9 +95,12 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock(); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->shareManager = $this->createMock(IShareManager::class); + $this->config = $this->createMock(IConfig::class); $this->userManager->expects($this->any())->method('userExists')->willReturn(true); - $this->timeFactory->expects($this->any())->method('now')->willReturn(new \DateTimeImmutable("2023-05-04 00:00 Europe/Berlin")); + $this->timeFactory->expects($this->any())->method('now')->willReturn(new \DateTimeImmutable('2023-05-04 00:00 Europe/Berlin')); //Empty share table $this->dbConn->getQueryBuilder()->delete('share')->execute(); @@ -110,13 +114,16 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->defaults, $this->l10nFactory, $this->urlGenerator, - $this->timeFactory + $this->timeFactory, + $this->logger, + $this->shareManager, + $this->config, ); } protected function tearDown(): void { $this->dbConn->getQueryBuilder()->delete('share')->execute(); - $this->dbConn->getQueryBuilder()->delete('filecache')->execute(); + $this->dbConn->getQueryBuilder()->delete('filecache')->runAcrossAllShards()->execute(); $this->dbConn->getQueryBuilder()->delete('storages')->execute(); } @@ -167,7 +174,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $qb->setValue('token', $qb->expr()->literal($token)); } if ($expiration) { - $qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATE)); + $qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATETIME_MUTABLE)); } if ($parent) { $qb->setValue('parent', $qb->expr()->literal($parent)); @@ -180,13 +187,13 @@ class DefaultShareProviderTest extends \Test\TestCase { - public function testGetShareByIdNotExist() { - $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class); + public function testGetShareByIdNotExist(): void { + $this->expectException(ShareNotFound::class); $this->provider->getShareById(1); } - public function testGetShareByIdUserShare() { + public function testGetShareByIdUserShare(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') @@ -233,7 +240,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('myTarget', $share->getTarget()); } - public function testGetShareByIdLazy() { + public function testGetShareByIdLazy(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') @@ -268,7 +275,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('myTarget', $share->getTarget()); } - public function testGetShareByIdLazy2() { + public function testGetShareByIdLazy2(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') @@ -312,7 +319,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('myTarget', $share->getTarget()); } - public function testGetShareByIdGroupShare() { + public function testGetShareByIdGroupShare(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') @@ -336,10 +343,10 @@ class DefaultShareProviderTest extends \Test\TestCase { $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath); $this->rootFolder - ->method('getUserFolder') - ->willReturnMap([ - ['shareOwner', $shareOwnerFolder], - ]); + ->method('getUserFolder') + ->willReturnMap([ + ['shareOwner', $shareOwnerFolder], + ]); $share = $this->provider->getShareById($id); @@ -355,7 +362,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('myTarget', $share->getTarget()); } - public function testGetShareByIdUserGroupShare() { + public function testGetShareByIdUserGroupShare(): void { $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user0', 'user0', 'file', 42, 'myTarget', 31, null, null); $this->addShareToDB(2, 'user1', 'user0', 'user0', 'file', 42, 'userTarget', 0, null, null, $id); @@ -370,6 +377,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $node = $this->createMock(Folder::class); $node->method('getId')->willReturn(42); + $node->method('getName')->willReturn('myTarget'); $this->rootFolder->method('getUserFolder')->with('user0')->willReturnSelf(); $this->rootFolder->method('getFirstNodeById')->willReturn($node); @@ -394,7 +402,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('userTarget', $share->getTarget()); } - public function testGetShareByIdLinkShare() { + public function testGetShareByIdLinkShare(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') @@ -420,10 +428,10 @@ class DefaultShareProviderTest extends \Test\TestCase { $shareOwnerFolder->method('getFirstNodeById')->with(42)->willReturn($ownerPath); $this->rootFolder - ->method('getUserFolder') - ->willReturnMap([ - ['shareOwner', $shareOwnerFolder], - ]); + ->method('getUserFolder') + ->willReturnMap([ + ['shareOwner', $shareOwnerFolder], + ]); $share = $this->provider->getShareById($id); @@ -441,7 +449,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('myTarget', $share->getTarget()); } - public function testDeleteSingleShare() { + public function testDeleteSingleShare(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->values([ @@ -471,9 +479,12 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->defaults, $this->l10nFactory, $this->urlGenerator, - $this->timeFactory + $this->timeFactory, + $this->logger, + $this->shareManager, + $this->config, ]) - ->setMethods(['getShareById']) + ->onlyMethods(['getShareById']) ->getMock(); $provider->delete($share); @@ -489,7 +500,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEmpty($result); } - public function testDeleteSingleShareLazy() { + public function testDeleteSingleShareLazy(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->values([ @@ -522,7 +533,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEmpty($result); } - public function testDeleteGroupShareWithUserGroupShares() { + public function testDeleteGroupShareWithUserGroupShares(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->values([ @@ -566,9 +577,12 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->defaults, $this->l10nFactory, $this->urlGenerator, - $this->timeFactory + $this->timeFactory, + $this->logger, + $this->shareManager, + $this->config, ]) - ->setMethods(['getShareById']) + ->onlyMethods(['getShareById']) ->getMock(); $provider->delete($share); @@ -584,7 +598,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEmpty($result); } - public function testGetChildren() { + public function testGetChildren(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->values([ @@ -672,8 +686,8 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('myTarget2', $children[1]->getTarget()); } - public function testCreateUserShare() { - $share = new \OC\Share20\Share($this->rootFolder, $this->userManager); + public function testCreateUserShare(): void { + $share = new Share($this->rootFolder, $this->userManager); $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); @@ -716,7 +730,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $share2 = $this->provider->create($share); $this->assertNotNull($share2->getId()); - $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId()); + $this->assertSame('ocinternal:' . $share2->getId(), $share2->getFullId()); $this->assertSame(IShare::TYPE_USER, $share2->getShareType()); $this->assertSame('sharedWith', $share2->getSharedWith()); $this->assertSame('sharedBy', $share2->getSharedBy()); @@ -737,15 +751,15 @@ class DefaultShareProviderTest extends \Test\TestCase { [ 'scope' => 'permissions', 'key' => 'download', - 'enabled' => true + 'value' => true ] ], $share->getAttributes()->toArray() ); } - public function testCreateGroupShare() { - $share = new \OC\Share20\Share($this->rootFolder, $this->userManager); + public function testCreateGroupShare(): void { + $share = new Share($this->rootFolder, $this->userManager); $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); @@ -786,7 +800,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $share2 = $this->provider->create($share); $this->assertNotNull($share2->getId()); - $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId()); + $this->assertSame('ocinternal:' . $share2->getId(), $share2->getFullId()); $this->assertSame(IShare::TYPE_GROUP, $share2->getShareType()); $this->assertSame('sharedWith', $share2->getSharedWith()); $this->assertSame('sharedBy', $share2->getSharedBy()); @@ -807,15 +821,15 @@ class DefaultShareProviderTest extends \Test\TestCase { [ 'scope' => 'permissions', 'key' => 'download', - 'enabled' => true + 'value' => true ] ], $share->getAttributes()->toArray() ); } - public function testCreateLinkShare() { - $share = new \OC\Share20\Share($this->rootFolder, $this->userManager); + public function testCreateLinkShare(): void { + $share = new Share($this->rootFolder, $this->userManager); $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); @@ -827,18 +841,18 @@ class DefaultShareProviderTest extends \Test\TestCase { $ownerFolder = $this->createMock(Folder::class); $userFolder = $this->createMock(Folder::class); $this->rootFolder - ->method('getUserFolder') - ->willReturnMap([ - ['sharedBy', $userFolder], - ['shareOwner', $ownerFolder], - ]); + ->method('getUserFolder') + ->willReturnMap([ + ['sharedBy', $userFolder], + ['shareOwner', $ownerFolder], + ]); $userFolder->method('getFirstNodeById') - ->with(100) - ->willReturn($path); + ->with(100) + ->willReturn($path); $ownerFolder->method('getFirstNodeById') - ->with(100) - ->willReturn($path); + ->with(100) + ->willReturn($path); $share->setShareType(IShare::TYPE_LINK); $share->setSharedBy('sharedBy'); @@ -855,7 +869,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $share2 = $this->provider->create($share); $this->assertNotNull($share2->getId()); - $this->assertSame('ocinternal:'.$share2->getId(), $share2->getFullId()); + $this->assertSame('ocinternal:' . $share2->getId(), $share2->getFullId()); $this->assertSame(IShare::TYPE_LINK, $share2->getShareType()); $this->assertSame('sharedBy', $share2->getSharedBy()); $this->assertSame('shareOwner', $share2->getShareOwner()); @@ -869,7 +883,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals($expireDate->getTimestamp(), $share2->getExpirationDate()->getTimestamp()); } - public function testGetShareByToken() { + public function testGetShareByToken(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') @@ -884,6 +898,7 @@ class DefaultShareProviderTest extends \Test\TestCase { 'file_target' => $qb->expr()->literal('myTarget'), 'permissions' => $qb->expr()->literal(13), 'token' => $qb->expr()->literal('secrettoken'), + 'label' => $qb->expr()->literal('the label'), ]); $qb->execute(); $id = $qb->getLastInsertId(); @@ -899,13 +914,46 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertSame('sharedBy', $share->getSharedBy()); $this->assertSame('secrettoken', $share->getToken()); $this->assertSame('password', $share->getPassword()); + $this->assertSame('the label', $share->getLabel()); $this->assertSame(true, $share->getSendPasswordByTalk()); $this->assertSame(null, $share->getSharedWith()); } + /** + * Assert that if no label is provided the label is correctly, + * as types on IShare, a string and not null + */ + public function testGetShareByTokenNullLabel(): void { + $qb = $this->dbConn->getQueryBuilder(); - public function testGetShareByTokenNotFound() { - $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK), + 'password' => $qb->expr()->literal('password'), + 'password_by_talk' => $qb->expr()->literal(true), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('myTarget'), + 'permissions' => $qb->expr()->literal(13), + 'token' => $qb->expr()->literal('secrettoken'), + ]); + $qb->executeStatement(); + $id = $qb->getLastInsertId(); + + $file = $this->createMock(File::class); + + $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); + $this->rootFolder->method('getFirstNodeById')->with(42)->willReturn($file); + + $share = $this->provider->getShareByToken('secrettoken'); + $this->assertEquals($id, $share->getId()); + $this->assertSame('', $share->getLabel()); + } + + public function testGetShareByTokenNotFound(): void { + $this->expectException(ShareNotFound::class); $this->provider->getShareByToken('invalidtoken'); } @@ -924,16 +972,16 @@ class DefaultShareProviderTest extends \Test\TestCase { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('filecache') ->values([ - 'storage' => $qb->expr()->literal($storage), - 'path' => $qb->expr()->literal($path), - 'path_hash' => $qb->expr()->literal(md5($path)), - 'name' => $qb->expr()->literal(basename($path)), + 'storage' => $qb->createNamedParameter($storage, IQueryBuilder::PARAM_INT), + 'path' => $qb->createNamedParameter($path), + 'path_hash' => $qb->createNamedParameter(md5($path)), + 'name' => $qb->createNamedParameter(basename($path)), ]); $this->assertEquals(1, $qb->execute()); return $qb->getLastInsertId(); } - public function storageAndFileNameProvider() { + public static function storageAndFileNameProvider(): array { return [ // regular file on regular storage ['home::shareOwner', 'files/test.txt', 'files/test2.txt'], @@ -944,10 +992,8 @@ class DefaultShareProviderTest extends \Test\TestCase { ]; } - /** - * @dataProvider storageAndFileNameProvider - */ - public function testGetSharedWithUser($storageStringId, $fileName1, $fileName2) { + #[\PHPUnit\Framework\Attributes\DataProvider('storageAndFileNameProvider')] + public function testGetSharedWithUser($storageStringId, $fileName1, $fileName2): void { $storageId = $this->createTestStorageEntry($storageStringId); $fileId = $this->createTestFileEntry($fileName1, $storageId); $fileId2 = $this->createTestFileEntry($fileName2, $storageId); @@ -995,10 +1041,8 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals(IShare::TYPE_USER, $share->getShareType()); } - /** - * @dataProvider storageAndFileNameProvider - */ - public function testGetSharedWithGroup($storageStringId, $fileName1, $fileName2) { + #[\PHPUnit\Framework\Attributes\DataProvider('storageAndFileNameProvider')] + public function testGetSharedWithGroup($storageStringId, $fileName1, $fileName2): void { $storageId = $this->createTestStorageEntry($storageStringId); $fileId = $this->createTestFileEntry($fileName1, $storageId); $fileId2 = $this->createTestFileEntry($fileName2, $storageId); @@ -1033,7 +1077,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $groups = []; foreach (range(0, 100) as $i) { - $groups[] = 'group'.$i; + $groups[] = 'group' . $i; } $groups[] = 'sharedWith'; @@ -1050,7 +1094,9 @@ class DefaultShareProviderTest extends \Test\TestCase { ['shareOwner', $owner], ['sharedBy', $initiator], ]); - $this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups); + $this->groupManager + ->method('getUserGroupIds') + ->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'sharedWith' ? $groups : [])); $file = $this->createMock(File::class); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); @@ -1067,10 +1113,8 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals(IShare::TYPE_GROUP, $share->getShareType()); } - /** - * @dataProvider storageAndFileNameProvider - */ - public function testGetSharedWithGroupUserModified($storageStringId, $fileName1, $fileName2) { + #[\PHPUnit\Framework\Attributes\DataProvider('storageAndFileNameProvider')] + public function testGetSharedWithGroupUserModified($storageStringId, $fileName1, $fileName2): void { $storageId = $this->createTestStorageEntry($storageStringId); $fileId = $this->createTestFileEntry($fileName1, $storageId); $qb = $this->dbConn->getQueryBuilder(); @@ -1138,7 +1182,9 @@ class DefaultShareProviderTest extends \Test\TestCase { ['shareOwner', $owner], ['sharedBy', $initiator], ]); - $this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups); + $this->groupManager + ->method('getUserGroupIds') + ->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'user' ? $groups : [])); $file = $this->createMock(File::class); $this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf(); @@ -1157,10 +1203,8 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertSame('userTarget', $share->getTarget()); } - /** - * @dataProvider storageAndFileNameProvider - */ - public function testGetSharedWithUserWithNode($storageStringId, $fileName1, $fileName2) { + #[\PHPUnit\Framework\Attributes\DataProvider('storageAndFileNameProvider')] + public function testGetSharedWithUserWithNode($storageStringId, $fileName1, $fileName2): void { $storageId = $this->createTestStorageEntry($storageStringId); $fileId = $this->createTestFileEntry($fileName1, $storageId); $fileId2 = $this->createTestFileEntry($fileName2, $storageId); @@ -1199,10 +1243,8 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals(IShare::TYPE_USER, $share->getShareType()); } - /** - * @dataProvider storageAndFileNameProvider - */ - public function testGetSharedWithGroupWithNode($storageStringId, $fileName1, $fileName2) { + #[\PHPUnit\Framework\Attributes\DataProvider('storageAndFileNameProvider')] + public function testGetSharedWithGroupWithNode($storageStringId, $fileName1, $fileName2): void { $storageId = $this->createTestStorageEntry($storageStringId); $fileId = $this->createTestFileEntry($fileName1, $storageId); $fileId2 = $this->createTestFileEntry($fileName2, $storageId); @@ -1221,7 +1263,9 @@ class DefaultShareProviderTest extends \Test\TestCase { ['user1', $user1], ]); - $this->groupManager->method('getUserGroupIds')->with($user0)->willReturn(['group0']); + $this->groupManager + ->method('getUserGroupIds') + ->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'user0' ? ['group0'] : [])); $node = $this->createMock(Folder::class); $node->method('getId')->willReturn($fileId2); @@ -1240,7 +1284,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals(IShare::TYPE_GROUP, $share->getShareType()); } - public function shareTypesProvider() { + public static function shareTypesProvider(): array { return [ [IShare::TYPE_USER, false], [IShare::TYPE_GROUP, false], @@ -1249,10 +1293,8 @@ class DefaultShareProviderTest extends \Test\TestCase { ]; } - /** - * @dataProvider shareTypesProvider - */ - public function testGetSharedWithWithDeletedFile($shareType, $trashed) { + #[\PHPUnit\Framework\Attributes\DataProvider('shareTypesProvider')] + public function testGetSharedWithWithDeletedFile($shareType, $trashed): void { if ($trashed) { // exists in database but is in trash $storageId = $this->createTestStorageEntry('home::shareOwner'); @@ -1281,7 +1323,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $groups = []; foreach (range(0, 100) as $i) { - $groups[] = 'group'.$i; + $groups[] = 'group' . $i; } $groups[] = 'sharedWith'; @@ -1298,13 +1340,15 @@ class DefaultShareProviderTest extends \Test\TestCase { ['shareOwner', $owner], ['sharedBy', $initiator], ]); - $this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups); + $this->groupManager + ->method('getUserGroupIds') + ->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'sharedWith' ? $groups : [])); $share = $this->provider->getSharedWith('sharedWith', $shareType, null, 1, 0); $this->assertCount(0, $share); } - public function testGetSharesBy() { + public function testGetSharesBy(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->values([ @@ -1353,7 +1397,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('myTarget', $share->getTarget()); } - public function testGetSharesNode() { + public function testGetSharesNode(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->values([ @@ -1403,7 +1447,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('myTarget', $share->getTarget()); } - public function testGetSharesReshare() { + public function testGetSharesReshare(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->values([ @@ -1462,7 +1506,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('userTarget', $share->getTarget()); } - public function testDeleteFromSelfGroupNoCustomShare() { + public function testDeleteFromSelfGroupNoCustomShare(): void { $qb = $this->dbConn->getQueryBuilder(); $stmt = $qb->insert('share') ->values([ @@ -1519,7 +1563,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('user2', $share2['share_with']); } - public function testDeleteFromSelfGroupAlreadyCustomShare() { + public function testDeleteFromSelfGroupAlreadyCustomShare(): void { $qb = $this->dbConn->getQueryBuilder(); $stmt = $qb->insert('share') ->values([ @@ -1592,7 +1636,7 @@ class DefaultShareProviderTest extends \Test\TestCase { } - public function testDeleteFromSelfGroupUserNotInGroup() { + public function testDeleteFromSelfGroupUserNotInGroup(): void { $qb = $this->dbConn->getQueryBuilder(); $stmt = $qb->insert('share') ->values([ @@ -1635,8 +1679,8 @@ class DefaultShareProviderTest extends \Test\TestCase { } - public function testDeleteFromSelfGroupDoesNotExist() { - $this->expectException(\OC\Share20\Exception\ProviderException::class); + public function testDeleteFromSelfGroupDoesNotExist(): void { + $this->expectException(ProviderException::class); $this->expectExceptionMessage('Group "group" does not exist'); $qb = $this->dbConn->getQueryBuilder(); @@ -1676,7 +1720,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->provider->deleteFromSelf($share, 'user2'); } - public function testDeleteFromSelfUser() { + public function testDeleteFromSelfUser(): void { $qb = $this->dbConn->getQueryBuilder(); $stmt = $qb->insert('share') ->values([ @@ -1726,8 +1770,8 @@ class DefaultShareProviderTest extends \Test\TestCase { } - public function testDeleteFromSelfUserNotRecipient() { - $this->expectException(\OC\Share20\Exception\ProviderException::class); + public function testDeleteFromSelfUserNotRecipient(): void { + $this->expectException(ProviderException::class); $this->expectExceptionMessage('Recipient does not match'); $qb = $this->dbConn->getQueryBuilder(); @@ -1769,8 +1813,8 @@ class DefaultShareProviderTest extends \Test\TestCase { } - public function testDeleteFromSelfLink() { - $this->expectException(\OC\Share20\Exception\ProviderException::class); + public function testDeleteFromSelfLink(): void { + $this->expectException(ProviderException::class); $this->expectExceptionMessage('Invalid shareType'); $qb = $this->dbConn->getQueryBuilder(); @@ -1805,16 +1849,16 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->provider->deleteFromSelf($share, $user1); } - public function testUpdateUser() { + public function testUpdateUser(): void { $id = $this->addShareToDB(IShare::TYPE_USER, 'user0', 'user1', 'user2', 'file', 42, 'target', 31, null, null); $users = []; for ($i = 0; $i < 6; $i++) { $user = $this->createMock(IUser::class); - $user->method('getUID')->willReturn('user'.$i); + $user->method('getUID')->willReturn('user' . $i); $user->method('getDisplayName')->willReturn('user' . $i); - $users['user'.$i] = $user; + $users['user' . $i] = $user; } $this->userManager->method('get')->willReturnCallback( @@ -1863,15 +1907,15 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertSame(1, $share2->getPermissions()); } - public function testUpdateLink() { + public function testUpdateLink(): void { $id = $this->addShareToDB(IShare::TYPE_LINK, null, 'user1', 'user2', 'file', 42, 'target', 31, null, null); $users = []; for ($i = 0; $i < 6; $i++) { $user = $this->createMock(IUser::class); - $user->method('getUID')->willReturn('user'.$i); - $users['user'.$i] = $user; + $user->method('getUID')->willReturn('user' . $i); + $users['user' . $i] = $user; } $this->userManager->method('get')->willReturnCallback( @@ -1923,7 +1967,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertSame(1, $share2->getPermissions()); } - public function testUpdateLinkRemovePassword() { + public function testUpdateLinkRemovePassword(): void { $id = $this->addShareToDB(IShare::TYPE_LINK, 'foo', 'user1', 'user2', 'file', 42, 'target', 31, null, null); @@ -1936,8 +1980,8 @@ class DefaultShareProviderTest extends \Test\TestCase { $users = []; for ($i = 0; $i < 6; $i++) { $user = $this->createMock(IUser::class); - $user->method('getUID')->willReturn('user'.$i); - $users['user'.$i] = $user; + $user->method('getUID')->willReturn('user' . $i); + $users['user' . $i] = $user; } $this->userManager->method('get')->willReturnCallback( @@ -1986,15 +2030,15 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertSame(1, $share2->getPermissions()); } - public function testUpdateGroupNoSub() { + public function testUpdateGroupNoSub(): void { $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user1', 'user2', 'file', 42, 'target', 31, null, null); $users = []; for ($i = 0; $i < 6; $i++) { $user = $this->createMock(IUser::class); - $user->method('getUID')->willReturn('user'.$i); - $users['user'.$i] = $user; + $user->method('getUID')->willReturn('user' . $i); + $users['user' . $i] = $user; } $this->userManager->method('get')->willReturnCallback( @@ -2006,9 +2050,9 @@ class DefaultShareProviderTest extends \Test\TestCase { $groups = []; for ($i = 0; $i < 2; $i++) { $group = $this->createMock(IGroup::class); - $group->method('getGID')->willReturn('group'.$i); + $group->method('getGID')->willReturn('group' . $i); $group->method('getDisplayName')->willReturn('group-displayname' . $i); - $groups['group'.$i] = $group; + $groups['group' . $i] = $group; } $this->groupManager->method('get')->willReturnCallback( @@ -2059,7 +2103,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertSame(1, $share2->getPermissions()); } - public function testUpdateGroupSubShares() { + public function testUpdateGroupSubShares(): void { $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user1', 'user2', 'file', 42, 'target', 31, null, null); @@ -2072,8 +2116,8 @@ class DefaultShareProviderTest extends \Test\TestCase { $users = []; for ($i = 0; $i < 6; $i++) { $user = $this->createMock(IUser::class); - $user->method('getUID')->willReturn('user'.$i); - $users['user'.$i] = $user; + $user->method('getUID')->willReturn('user' . $i); + $users['user' . $i] = $user; } $this->userManager->method('get')->willReturnCallback( @@ -2085,9 +2129,9 @@ class DefaultShareProviderTest extends \Test\TestCase { $groups = []; for ($i = 0; $i < 2; $i++) { $group = $this->createMock(IGroup::class); - $group->method('getGID')->willReturn('group'.$i); - $group->method('getDisplayName')->willReturn('group-displayname'.$i); - $groups['group'.$i] = $group; + $group->method('getGID')->willReturn('group' . $i); + $group->method('getDisplayName')->willReturn('group-displayname' . $i); + $groups['group' . $i] = $group; } $this->groupManager->method('get')->willReturnCallback( @@ -2160,7 +2204,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $stmt->closeCursor(); } - public function testMoveUserShare() { + public function testMoveUserShare(): void { $id = $this->addShareToDB(IShare::TYPE_USER, 'user0', 'user1', 'user1', 'file', 42, 'mytaret', 31, null, null); @@ -2191,7 +2235,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertSame('/newTarget', $share->getTarget()); } - public function testMoveGroupShare() { + public function testMoveGroupShare(): void { $id = $this->addShareToDB(IShare::TYPE_GROUP, 'group0', 'user1', 'user1', 'file', 42, 'mytaret', 31, null, null); @@ -2233,7 +2277,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertSame('/ultraNewTarget', $share->getTarget()); } - public function dataDeleteUser() { + public static function dataDeleteUser(): array { return [ [IShare::TYPE_USER, 'a', 'b', 'c', 'a', true], [IShare::TYPE_USER, 'a', 'b', 'c', 'b', false], @@ -2253,7 +2297,6 @@ class DefaultShareProviderTest extends \Test\TestCase { } /** - * @dataProvider dataDeleteUser * * @param int $type The shareType (user/group/link) * @param string $owner The owner of the share (uid) @@ -2262,7 +2305,8 @@ class DefaultShareProviderTest extends \Test\TestCase { * @param string $deletedUser The user that is deleted * @param bool $rowDeleted Is the row deleted in this setup */ - public function testDeleteUser($type, $owner, $initiator, $recipient, $deletedUser, $rowDeleted) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteUser')] + public function testDeleteUser($type, $owner, $initiator, $recipient, $deletedUser, $rowDeleted): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->setValue('share_type', $qb->createNamedParameter($type)) @@ -2291,7 +2335,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertCount($rowDeleted ? 0 : 1, $data); } - public function dataDeleteUserGroup() { + public static function dataDeleteUserGroup(): array { return [ ['a', 'b', 'c', 'a', true, true], ['a', 'b', 'c', 'b', false, false], @@ -2301,7 +2345,6 @@ class DefaultShareProviderTest extends \Test\TestCase { } /** - * @dataProvider dataDeleteUserGroup * * @param string $owner The owner of the share (uid) * @param string $initiator The initiator of the share (uid) @@ -2310,7 +2353,8 @@ class DefaultShareProviderTest extends \Test\TestCase { * @param bool $groupShareDeleted * @param bool $userGroupShareDeleted */ - public function testDeleteUserGroup($owner, $initiator, $recipient, $deletedUser, $groupShareDeleted, $userGroupShareDeleted) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteUserGroup')] + public function testDeleteUserGroup($owner, $initiator, $recipient, $deletedUser, $groupShareDeleted, $userGroupShareDeleted): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->setValue('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)) @@ -2360,7 +2404,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertCount($groupShareDeleted ? 0 : 1, $data); } - public function dataGroupDeleted() { + public static function dataGroupDeleted(): array { return [ [ [ @@ -2407,13 +2451,13 @@ class DefaultShareProviderTest extends \Test\TestCase { } /** - * @dataProvider dataGroupDeleted * * @param $shares * @param $groupToDelete * @param $shouldBeDeleted */ - public function testGroupDeleted($shares, $groupToDelete, $shouldBeDeleted) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGroupDeleted')] + public function testGroupDeleted($shares, $groupToDelete, $shouldBeDeleted): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->setValue('share_type', $qb->createNamedParameter($shares['type'])) @@ -2454,7 +2498,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertCount($shouldBeDeleted ? 0 : count($ids), $data); } - public function dataUserDeletedFromGroup() { + public static function dataUserDeletedFromGroup(): array { return [ ['group1', 'user1', true], ['group1', 'user2', false], @@ -2467,13 +2511,13 @@ class DefaultShareProviderTest extends \Test\TestCase { * And a user specific group share with 'user1'. * User $user is deleted from group $gid. * - * @dataProvider dataUserDeletedFromGroup * * @param string $group * @param string $user * @param bool $toDelete */ - public function testUserDeletedFromGroup($group, $user, $toDelete) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataUserDeletedFromGroup')] + public function testUserDeletedFromGroup($group, $user, $toDelete): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') ->setValue('share_type', $qb->createNamedParameter(IShare::TYPE_GROUP)) @@ -2512,10 +2556,10 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertCount($toDelete ? 0 : 1, $data); } - public function testGetSharesInFolder() { - $userManager = \OC::$server->getUserManager(); - $groupManager = \OC::$server->getGroupManager(); - $rootFolder = \OC::$server->get(IRootFolder::class); + public function testGetSharesInFolder(): void { + $userManager = Server::get(IUserManager::class); + $groupManager = Server::get(IGroupManager::class); + $rootFolder = Server::get(IRootFolder::class); $provider = new DefaultShareProvider( $this->dbConn, @@ -2526,7 +2570,10 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->defaults, $this->l10nFactory, $this->urlGenerator, - $this->timeFactory + $this->timeFactory, + $this->logger, + $this->shareManager, + $this->config, ); $password = md5(time()); @@ -2542,14 +2589,14 @@ class DefaultShareProviderTest extends \Test\TestCase { $file1 = $folder1->newFile('bar'); $folder2 = $folder1->newFolder('baz'); - $shareManager = \OC::$server->get(IShareManager::class); + $shareManager = Server::get(IShareManager::class); $share1 = $shareManager->newShare(); $share1->setNode($folder1) ->setSharedBy($u1->getUID()) ->setSharedWith($u2->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_USER) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $share1 = $this->provider->create($share1); $share2 = $shareManager->newShare(); @@ -2558,7 +2605,7 @@ class DefaultShareProviderTest extends \Test\TestCase { ->setSharedWith($u3->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_USER) - ->setPermissions(\OCP\Constants::PERMISSION_READ); + ->setPermissions(Constants::PERMISSION_READ); $share2 = $this->provider->create($share2); $share3 = $shareManager->newShare(); @@ -2566,7 +2613,7 @@ class DefaultShareProviderTest extends \Test\TestCase { ->setSharedBy($u2->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_LINK) - ->setPermissions(\OCP\Constants::PERMISSION_READ); + ->setPermissions(Constants::PERMISSION_READ); $share3 = $this->provider->create($share3); $share4 = $shareManager->newShare(); @@ -2575,7 +2622,7 @@ class DefaultShareProviderTest extends \Test\TestCase { ->setSharedWith($g1->getGID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_GROUP) - ->setPermissions(\OCP\Constants::PERMISSION_READ); + ->setPermissions(Constants::PERMISSION_READ); $share4 = $this->provider->create($share4); $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false); @@ -2610,10 +2657,10 @@ class DefaultShareProviderTest extends \Test\TestCase { $g1->delete(); } - public function testGetAccessListNoCurrentAccessRequired() { - $userManager = \OC::$server->getUserManager(); - $groupManager = \OC::$server->getGroupManager(); - $rootFolder = \OC::$server->get(IRootFolder::class); + public function testGetAccessListNoCurrentAccessRequired(): void { + $userManager = Server::get(IUserManager::class); + $groupManager = Server::get(IGroupManager::class); + $rootFolder = Server::get(IRootFolder::class); $provider = new DefaultShareProvider( $this->dbConn, @@ -2624,7 +2671,10 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->defaults, $this->l10nFactory, $this->urlGenerator, - $this->timeFactory + $this->timeFactory, + $this->logger, + $this->shareManager, + $this->config, ); $u1 = $userManager->createUser('testShare1', 'test'); @@ -2646,15 +2696,16 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertCount(0, $result['users']); $this->assertFalse($result['public']); - $shareManager = \OC::$server->get(IShareManager::class); + $shareManager = Server::get(IShareManager::class); $share1 = $shareManager->newShare(); $share1->setNode($folder1) ->setSharedBy($u1->getUID()) ->setSharedWith($u2->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_USER) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $share1 = $this->provider->create($share1); + $share1 = $provider->acceptShare($share1, $u2->getUid()); $share2 = $shareManager->newShare(); $share2->setNode($folder2) @@ -2662,17 +2713,20 @@ class DefaultShareProviderTest extends \Test\TestCase { ->setSharedWith($g1->getGID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_GROUP) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $share2 = $this->provider->create($share2); $shareManager->deleteFromSelf($share2, $u4->getUID()); + $share2 = $provider->acceptShare($share2, $u3->getUid()); + $share2 = $provider->acceptShare($share2, $u4->getUid()); + $share3 = $shareManager->newShare(); $share3->setNode($file1) ->setSharedBy($u3->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_LINK) - ->setPermissions(\OCP\Constants::PERMISSION_READ); + ->setPermissions(Constants::PERMISSION_READ); $share3 = $this->provider->create($share3); $share4 = $shareManager->newShare(); @@ -2681,8 +2735,9 @@ class DefaultShareProviderTest extends \Test\TestCase { ->setSharedWith($u5->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_USER) - ->setPermissions(\OCP\Constants::PERMISSION_READ); + ->setPermissions(Constants::PERMISSION_READ); $share4 = $this->provider->create($share4); + $share4 = $provider->acceptShare($share4, $u5->getUid()); $result = $provider->getAccessList([$folder1, $folder2, $file1], false); @@ -2706,10 +2761,10 @@ class DefaultShareProviderTest extends \Test\TestCase { $g1->delete(); } - public function testGetAccessListCurrentAccessRequired() { - $userManager = \OC::$server->getUserManager(); - $groupManager = \OC::$server->getGroupManager(); - $rootFolder = \OC::$server->get(IRootFolder::class); + public function testGetAccessListCurrentAccessRequired(): void { + $userManager = Server::get(IUserManager::class); + $groupManager = Server::get(IGroupManager::class); + $rootFolder = Server::get(IRootFolder::class); $provider = new DefaultShareProvider( $this->dbConn, @@ -2720,7 +2775,10 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->defaults, $this->l10nFactory, $this->urlGenerator, - $this->timeFactory + $this->timeFactory, + $this->logger, + $this->shareManager, + $this->config, ); $u1 = $userManager->createUser('testShare1', 'test'); @@ -2742,15 +2800,16 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertCount(0, $result['users']); $this->assertFalse($result['public']); - $shareManager = \OC::$server->get(IShareManager::class); + $shareManager = Server::get(IShareManager::class); $share1 = $shareManager->newShare(); $share1->setNode($folder1) ->setSharedBy($u1->getUID()) ->setSharedWith($u2->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_USER) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $share1 = $this->provider->create($share1); + $share1 = $provider->acceptShare($share1, $u2->getUid()); $share2 = $shareManager->newShare(); $share2->setNode($folder2) @@ -2758,8 +2817,10 @@ class DefaultShareProviderTest extends \Test\TestCase { ->setSharedWith($g1->getGID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_GROUP) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $share2 = $this->provider->create($share2); + $share2 = $provider->acceptShare($share2, $u3->getUid()); + $share2 = $provider->acceptShare($share2, $u4->getUid()); $shareManager->deleteFromSelf($share2, $u4->getUID()); @@ -2768,7 +2829,7 @@ class DefaultShareProviderTest extends \Test\TestCase { ->setSharedBy($u3->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_LINK) - ->setPermissions(\OCP\Constants::PERMISSION_READ); + ->setPermissions(Constants::PERMISSION_READ); $share3 = $this->provider->create($share3); $share4 = $shareManager->newShare(); @@ -2777,8 +2838,9 @@ class DefaultShareProviderTest extends \Test\TestCase { ->setSharedWith($u5->getUID()) ->setShareOwner($u1->getUID()) ->setShareType(IShare::TYPE_USER) - ->setPermissions(\OCP\Constants::PERMISSION_READ); + ->setPermissions(Constants::PERMISSION_READ); $share4 = $this->provider->create($share4); + $share4 = $provider->acceptShare($share4, $u5->getUid()); $result = $provider->getAccessList([$folder1, $folder2, $file1], true); @@ -2801,7 +2863,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $g1->delete(); } - public function testGetAllShares() { + public function testGetAllShares(): void { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share') @@ -2965,4 +3027,88 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals('token5', $share->getToken()); $this->assertEquals('myTarget5', $share->getTarget()); } + + + public function testGetSharesByPath(): void { + $qb = $this->dbConn->getQueryBuilder(); + + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(IShare::TYPE_USER), + 'uid_owner' => $qb->expr()->literal('user1'), + 'uid_initiator' => $qb->expr()->literal('user1'), + 'share_with' => $qb->expr()->literal('user2'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(1), + ]); + $qb->execute(); + + $id1 = $qb->getLastInsertId(); + + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP), + 'uid_owner' => $qb->expr()->literal('user1'), + 'uid_initiator' => $qb->expr()->literal('user1'), + 'share_with' => $qb->expr()->literal('user2'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(1), + ]); + $qb->execute(); + + $id2 = $qb->getLastInsertId(); + + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK), + 'uid_owner' => $qb->expr()->literal('user1'), + 'uid_initiator' => $qb->expr()->literal('user1'), + 'share_with' => $qb->expr()->literal('user2'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(1), + ]); + $qb->execute(); + + $id3 = $qb->getLastInsertId(); + + $ownerPath1 = $this->createMock(File::class); + $shareOwner1Folder = $this->createMock(Folder::class); + $shareOwner1Folder->method('getFirstNodeById')->willReturn($ownerPath1); + + $ownerPath2 = $this->createMock(File::class); + $shareOwner2Folder = $this->createMock(Folder::class); + $shareOwner2Folder->method('getFirstNodeById')->willReturn($ownerPath2); + + $ownerPath3 = $this->createMock(File::class); + $shareOwner3Folder = $this->createMock(Folder::class); + $shareOwner3Folder->method('getFirstNodeById')->willReturn($ownerPath3); + + $this->rootFolder + ->method('getUserFolder') + ->willReturnMap( + [ + ['shareOwner1', $shareOwner1Folder], + ['shareOwner2', $shareOwner2Folder], + ['shareOwner3', $shareOwner3Folder], + ] + ); + + $node = $this->createMock(Node::class); + $node + ->expects($this->once()) + ->method('getId') + ->willReturn(1); + + $shares = $this->provider->getSharesByPath($node); + $this->assertCount(3, $shares); + + $this->assertEquals($id1, $shares[0]->getId()); + $this->assertEquals(IShare::TYPE_USER, $shares[0]->getShareType()); + + $this->assertEquals($id2, $shares[1]->getId()); + $this->assertEquals(IShare::TYPE_GROUP, $shares[1]->getShareType()); + + $this->assertEquals($id3, $shares[2]->getId()); + $this->assertEquals(IShare::TYPE_LINK, $shares[2]->getShareType()); + } } diff --git a/tests/lib/Share20/LegacyHooksTest.php b/tests/lib/Share20/LegacyHooksTest.php index 239b3c91ca1..2ce72b3fc1c 100644 --- a/tests/lib/Share20/LegacyHooksTest.php +++ b/tests/lib/Share20/LegacyHooksTest.php @@ -1,28 +1,13 @@ <?php + /** - * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Share20; +use OC\EventDispatcher\EventDispatcher; use OC\Share20\LegacyHooks; use OC\Share20\Manager; use OCP\Constants; @@ -30,6 +15,7 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\ICacheEntry; use OCP\Files\File; use OCP\IServerContainer; +use OCP\Server; use OCP\Share\Events\BeforeShareCreatedEvent; use OCP\Share\Events\BeforeShareDeletedEvent; use OCP\Share\Events\ShareCreatedEvent; @@ -37,9 +23,23 @@ use OCP\Share\Events\ShareDeletedEvent; use OCP\Share\Events\ShareDeletedFromSelfEvent; use OCP\Share\IManager as IShareManager; use OCP\Share\IShare; +use OCP\Util; use Psr\Log\LoggerInterface; use Test\TestCase; +class Dummy { + public function postShare() { + } + public function preShare() { + } + public function postFromSelf() { + } + public function post() { + } + public function pre() { + } +} + class LegacyHooksTest extends TestCase { /** @var LegacyHooks */ private $hooks; @@ -55,12 +55,12 @@ class LegacyHooksTest extends TestCase { $symfonyDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); $logger = $this->createMock(LoggerInterface::class); - $this->eventDispatcher = new \OC\EventDispatcher\EventDispatcher($symfonyDispatcher, \OC::$server->get(IServerContainer::class), $logger); + $this->eventDispatcher = new EventDispatcher($symfonyDispatcher, Server::get(IServerContainer::class), $logger); $this->hooks = new LegacyHooks($this->eventDispatcher); - $this->manager = \OC::$server->get(IShareManager::class); + $this->manager = Server::get(IShareManager::class); } - public function testPreUnshare() { + public function testPreUnshare(): void { $path = $this->createMock(File::class); $path->method('getId')->willReturn(1); @@ -77,8 +77,8 @@ class LegacyHooksTest extends TestCase { ->setTarget('myTarget') ->setNodeCacheEntry($info); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre'); + $hookListner = $this->getMockBuilder(Dummy::class)->onlyMethods(['pre'])->getMock(); + Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre'); $hookListnerExpectsPre = [ 'id' => 42, @@ -101,7 +101,7 @@ class LegacyHooksTest extends TestCase { $this->eventDispatcher->dispatchTyped($event); } - public function testPostUnshare() { + public function testPostUnshare(): void { $path = $this->createMock(File::class); $path->method('getId')->willReturn(1); @@ -118,8 +118,8 @@ class LegacyHooksTest extends TestCase { ->setTarget('myTarget') ->setNodeCacheEntry($info); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post'); + $hookListner = $this->getMockBuilder(Dummy::class)->onlyMethods(['post'])->getMock(); + Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post'); $hookListnerExpectsPost = [ 'id' => 42, @@ -155,7 +155,7 @@ class LegacyHooksTest extends TestCase { $this->eventDispatcher->dispatchTyped($event); } - public function testPostUnshareFromSelf() { + public function testPostUnshareFromSelf(): void { $path = $this->createMock(File::class); $path->method('getId')->willReturn(1); @@ -172,8 +172,8 @@ class LegacyHooksTest extends TestCase { ->setTarget('myTarget') ->setNodeCacheEntry($info); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['postFromSelf'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_unshareFromSelf', $hookListner, 'postFromSelf'); + $hookListner = $this->getMockBuilder(Dummy::class)->onlyMethods(['postFromSelf'])->getMock(); + Util::connectHook('OCP\Share', 'post_unshareFromSelf', $hookListner, 'postFromSelf'); $hookListnerExpectsPostFromSelf = [ 'id' => 42, @@ -211,7 +211,7 @@ class LegacyHooksTest extends TestCase { $this->eventDispatcher->dispatchTyped($event); } - public function testPreShare() { + public function testPreShare(): void { $path = $this->createMock(File::class); $path->method('getId')->willReturn(1); @@ -229,8 +229,8 @@ class LegacyHooksTest extends TestCase { ->setToken('token'); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['preShare'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare'); + $hookListner = $this->getMockBuilder(Dummy::class)->onlyMethods(['preShare'])->getMock(); + Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare'); $run = true; $error = ''; @@ -259,7 +259,7 @@ class LegacyHooksTest extends TestCase { $this->eventDispatcher->dispatchTyped($event); } - public function testPreShareError() { + public function testPreShareError(): void { $path = $this->createMock(File::class); $path->method('getId')->willReturn(1); @@ -277,8 +277,8 @@ class LegacyHooksTest extends TestCase { ->setToken('token'); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['preShare'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare'); + $hookListner = $this->getMockBuilder(Dummy::class)->onlyMethods(['preShare'])->getMock(); + Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare'); $run = true; $error = ''; @@ -302,7 +302,7 @@ class LegacyHooksTest extends TestCase { ->expects($this->exactly(1)) ->method('preShare') ->with($expected) - ->willReturnCallback(function ($data) { + ->willReturnCallback(function ($data): void { $data['run'] = false; $data['error'] = 'I error'; }); @@ -314,7 +314,7 @@ class LegacyHooksTest extends TestCase { $this->assertSame('I error', $event->getError()); } - public function testPostShare() { + public function testPostShare(): void { $path = $this->createMock(File::class); $path->method('getId')->willReturn(1); @@ -333,8 +333,8 @@ class LegacyHooksTest extends TestCase { ->setToken('token'); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['postShare'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'postShare'); + $hookListner = $this->getMockBuilder(Dummy::class)->onlyMethods(['postShare'])->getMock(); + Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'postShare'); $expected = [ 'id' => 42, diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index da173dc2e1f..0e37934786a 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -1,34 +1,23 @@ <?php + /** - * @author Roeland Jago Douma <rullzer@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Share20; use DateTimeZone; use OC\Files\Mount\MoveableMount; +use OC\Files\Utils\PathHelper; use OC\KnownUser\KnownUserService; use OC\Share20\DefaultShareProvider; -use OC\Share20\Exception; +use OC\Share20\Exception\ProviderException; use OC\Share20\Manager; use OC\Share20\Share; use OC\Share20\ShareDisableChecker; +use OCP\Constants; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; @@ -36,9 +25,11 @@ use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\Mount\IMountManager; use OCP\Files\Mount\IMountPoint; +use OCP\Files\Mount\IShareOwnerlessMount; use OCP\Files\Node; -use OCP\Files\Storage; +use OCP\Files\Storage\IStorage; use OCP\HintException; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IDateTimeZone; use OCP\IGroup; @@ -60,15 +51,25 @@ use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\Events\ShareDeletedEvent; use OCP\Share\Events\ShareDeletedFromSelfEvent; use OCP\Share\Exceptions\AlreadySharedException; +use OCP\Share\Exceptions\GenericShareException; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager; use OCP\Share\IProviderFactory; use OCP\Share\IShare; use OCP\Share\IShareProvider; +use OCP\Share\IShareProviderSupportsAllSharesInFolder; +use OCP\Util; use PHPUnit\Framework\MockObject\MockBuilder; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; +class DummyShareManagerListener { + public function post() { + } + public function listener() { + } +} + /** * Class ManagerTest * @@ -88,9 +89,9 @@ class ManagerTest extends \Test\TestCase { protected $hasher; /** @var IShareProvider|MockObject */ protected $defaultProvider; - /** @var IMountManager|MockObject */ + /** @var IMountManager|MockObject */ protected $mountManager; - /** @var IGroupManager|MockObject */ + /** @var IGroupManager|MockObject */ protected $groupManager; /** @var IL10N|MockObject */ protected $l; @@ -104,21 +105,23 @@ class ManagerTest extends \Test\TestCase { protected $rootFolder; /** @var IEventDispatcher|MockObject */ protected $dispatcher; - /** @var IMailer|MockObject */ + /** @var IMailer|MockObject */ protected $mailer; - /** @var IURLGenerator|MockObject */ + /** @var IURLGenerator|MockObject */ protected $urlGenerator; - /** @var \OC_Defaults|MockObject */ + /** @var \OC_Defaults|MockObject */ protected $defaults; - /** @var IUserSession|MockObject */ + /** @var IUserSession|MockObject */ protected $userSession; - /** @var KnownUserService|MockObject */ + /** @var KnownUserService|MockObject */ protected $knownUserService; - /** @var ShareDisableChecker|MockObject */ + /** @var ShareDisableChecker|MockObject */ protected $shareDisabledChecker; private DateTimeZone $timezone; - /** @var IDateTimeZone|MockObject */ + /** @var IDateTimeZone|MockObject */ protected $dateTimeZone; + /** @var IAppConfig|MockObject */ + protected $appConfig; protected function setUp(): void { $this->logger = $this->createMock(LoggerInterface::class); @@ -141,6 +144,8 @@ class ManagerTest extends \Test\TestCase { $this->timezone = new \DateTimeZone('Pacific/Auckland'); $this->dateTimeZone->method('getTimeZone')->willReturnCallback(fn () => $this->timezone); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->l10nFactory = $this->createMock(IFactory::class); $this->l = $this->createMock(IL10N::class); $this->l->method('t') @@ -182,6 +187,7 @@ class ManagerTest extends \Test\TestCase { $this->knownUserService, $this->shareDisabledChecker, $this->dateTimeZone, + $this->appConfig, ); } @@ -209,11 +215,20 @@ class ManagerTest extends \Test\TestCase { $this->knownUserService, $this->shareDisabledChecker, $this->dateTimeZone, + $this->appConfig, ]); } + private function createFolderMock(string $folderPath): MockObject&Folder { + $folder = $this->createMock(Folder::class); + $folder->method('getPath')->willReturn($folderPath); + $folder->method('getRelativePath')->willReturnCallback( + fn (string $path): ?string => PathHelper::getRelativePath($folderPath, $path) + ); + return $folder; + } - public function testDeleteNoShareId() { + public function testDeleteNoShareId(): void { $this->expectException(\InvalidArgumentException::class); $share = $this->manager->newShare(); @@ -221,13 +236,7 @@ class ManagerTest extends \Test\TestCase { $this->manager->deleteShare($share); } - public function dataTestDelete() { - $user = $this->createMock(IUser::class); - $user->method('getUID')->willReturn('sharedWithUser'); - - $group = $this->createMock(IGroup::class); - $group->method('getGID')->willReturn('sharedWithGroup'); - + public static function dataTestDelete(): array { return [ [IShare::TYPE_USER, 'sharedWithUser'], [IShare::TYPE_GROUP, 'sharedWithGroup'], @@ -236,12 +245,10 @@ class ManagerTest extends \Test\TestCase { ]; } - /** - * @dataProvider dataTestDelete - */ - public function testDelete($shareType, $sharedWith) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestDelete')] + public function testDelete($shareType, $sharedWith): void { $manager = $this->createManagerMock() - ->setMethods(['getShareById', 'deleteChildren']) + ->onlyMethods(['getShareById', 'deleteChildren', 'promoteReshares']) ->getMock(); $manager->method('deleteChildren')->willReturn([]); @@ -259,31 +266,31 @@ class ManagerTest extends \Test\TestCase { ->setTarget('myTarget'); $manager->expects($this->once())->method('deleteChildren')->with($share); + $manager->expects($this->once())->method('promoteReshares')->with($share); $this->defaultProvider ->expects($this->once()) ->method('delete') ->with($share); + $calls = [ + BeforeShareDeletedEvent::class, + ShareDeletedEvent::class, + ]; $this->dispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->withConsecutive( - [ - $this->callBack(function (BeforeShareDeletedEvent $e) use ($share) { - return $e->getShare() === $share; - })], - [ - $this->callBack(function (ShareDeletedEvent $e) use ($share) { - return $e->getShare() === $share; - })] - ); + ->willReturnCallback(function ($event) use (&$calls, $share): void { + $expected = array_shift($calls); + $this->assertInstanceOf($expected, $event); + $this->assertEquals($share, $event->getShare()); + }); $manager->deleteShare($share); } - public function testDeleteLazyShare() { + public function testDeleteLazyShare(): void { $manager = $this->createManagerMock() - ->setMethods(['getShareById', 'deleteChildren']) + ->onlyMethods(['getShareById', 'deleteChildren', 'promoteReshares']) ->getMock(); $manager->method('deleteChildren')->willReturn([]); @@ -302,31 +309,31 @@ class ManagerTest extends \Test\TestCase { $this->rootFolder->expects($this->never())->method($this->anything()); $manager->expects($this->once())->method('deleteChildren')->with($share); + $manager->expects($this->once())->method('promoteReshares')->with($share); $this->defaultProvider ->expects($this->once()) ->method('delete') ->with($share); + $calls = [ + BeforeShareDeletedEvent::class, + ShareDeletedEvent::class, + ]; $this->dispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->withConsecutive( - [ - $this->callBack(function (BeforeShareDeletedEvent $e) use ($share) { - return $e->getShare() === $share; - })], - [ - $this->callBack(function (ShareDeletedEvent $e) use ($share) { - return $e->getShare() === $share; - })] - ); + ->willReturnCallback(function ($event) use (&$calls, $share): void { + $expected = array_shift($calls); + $this->assertInstanceOf($expected, $event); + $this->assertEquals($share, $event->getShare()); + }); $manager->deleteShare($share); } - public function testDeleteNested() { + public function testDeleteNested(): void { $manager = $this->createManagerMock() - ->setMethods(['getShareById']) + ->onlyMethods(['getShareById', 'promoteReshares']) ->getMock(); $path = $this->createMock(File::class); @@ -368,51 +375,40 @@ class ManagerTest extends \Test\TestCase { [$share3, []], ]); - $this->defaultProvider + $deleteCalls = [ + $share3, + $share2, + $share1, + ]; + $this->defaultProvider->expects($this->exactly(3)) ->method('delete') - ->withConsecutive([$share3], [$share2], [$share1]); + ->willReturnCallback(function ($share) use (&$deleteCalls): void { + $expected = array_shift($deleteCalls); + $this->assertEquals($expected, $share); + }); + $dispatchCalls = [ + [BeforeShareDeletedEvent::class, $share1], + [BeforeShareDeletedEvent::class, $share2], + [BeforeShareDeletedEvent::class, $share3], + [ShareDeletedEvent::class, $share3], + [ShareDeletedEvent::class, $share2], + [ShareDeletedEvent::class, $share1], + ]; $this->dispatcher->expects($this->exactly(6)) ->method('dispatchTyped') - ->withConsecutive( - [ - $this->callBack(function (BeforeShareDeletedEvent $e) use ($share1) { - return $e->getShare()->getId() === $share1->getId(); - }) - ], - [ - $this->callBack(function (BeforeShareDeletedEvent $e) use ($share2) { - return $e->getShare()->getId() === $share2->getId(); - }) - ], - [ - $this->callBack(function (BeforeShareDeletedEvent $e) use ($share3) { - return $e->getShare()->getId() === $share3->getId(); - }) - ], - [ - $this->callBack(function (ShareDeletedEvent $e) use ($share3) { - return $e->getShare()->getId() === $share3->getId(); - }) - ], - [ - $this->callBack(function (ShareDeletedEvent $e) use ($share2) { - return $e->getShare()->getId() === $share2->getId(); - }) - ], - [ - $this->callBack(function (ShareDeletedEvent $e) use ($share1) { - return $e->getShare()->getId() === $share1->getId(); - }) - ], - ); + ->willReturnCallback(function ($event) use (&$dispatchCalls): void { + $expected = array_shift($dispatchCalls); + $this->assertInstanceOf($expected[0], $event); + $this->assertEquals($expected[1]->getId(), $event->getShare()->getId()); + }); $manager->deleteShare($share1); } - public function testDeleteFromSelf() { + public function testDeleteFromSelf(): void { $manager = $this->createManagerMock() - ->setMethods(['getShareById']) + ->onlyMethods(['getShareById']) ->getMock(); $recipientId = 'unshareFrom'; @@ -443,9 +439,9 @@ class ManagerTest extends \Test\TestCase { $manager->deleteFromSelf($share, $recipientId); } - public function testDeleteChildren() { + public function testDeleteChildren(): void { $manager = $this->createManagerMock() - ->setMethods(['deleteShare']) + ->onlyMethods(['deleteShare']) ->getMock(); $share = $this->createMock(IShare::class); @@ -474,16 +470,213 @@ class ManagerTest extends \Test\TestCase { return []; }); - $this->defaultProvider - ->expects($this->exactly(3)) + $calls = [ + $child1, + $child2, + $child3, + ]; + $this->defaultProvider->expects($this->exactly(3)) ->method('delete') - ->withConsecutive([$child1], [$child2], [$child3]); + ->willReturnCallback(function ($share) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, $share); + }); $result = self::invokePrivate($manager, 'deleteChildren', [$share]); $this->assertSame($shares, $result); } - public function testGetShareById() { + public function testPromoteReshareFile(): void { + $manager = $this->createManagerMock() + ->onlyMethods(['updateShare', 'getSharesInFolder', 'generalCreateChecks']) + ->getMock(); + + $file = $this->createMock(File::class); + + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_USER); + $share->method('getNodeType')->willReturn('folder'); + $share->method('getSharedWith')->willReturn('userB'); + $share->method('getNode')->willReturn($file); + + $reShare = $this->createMock(IShare::class); + $reShare->method('getShareType')->willReturn(IShare::TYPE_USER); + $reShare->method('getSharedBy')->willReturn('userB'); + $reShare->method('getSharedWith')->willReturn('userC'); + $reShare->method('getNode')->willReturn($file); + + $this->defaultProvider->method('getSharesBy') + ->willReturnCallback(function ($userId, $shareType, $node, $reshares, $limit, $offset) use ($reShare, $file) { + $this->assertEquals($file, $node); + if ($shareType === IShare::TYPE_USER) { + return match($userId) { + 'userB' => [$reShare], + }; + } else { + return []; + } + }); + $manager->method('generalCreateChecks')->willThrowException(new GenericShareException()); + + $manager->expects($this->exactly(1))->method('updateShare')->with($reShare); + + self::invokePrivate($manager, 'promoteReshares', [$share]); + } + + public function testPromoteReshare(): void { + $manager = $this->createManagerMock() + ->onlyMethods(['updateShare', 'getSharesInFolder', 'generalCreateChecks']) + ->getMock(); + + $folder = $this->createFolderMock('/path/to/folder'); + + $subFolder = $this->createFolderMock('/path/to/folder/sub'); + + $otherFolder = $this->createFolderMock('/path/to/otherfolder/'); + + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_USER); + $share->method('getNodeType')->willReturn('folder'); + $share->method('getSharedWith')->willReturn('userB'); + $share->method('getNode')->willReturn($folder); + + $reShare = $this->createMock(IShare::class); + $reShare->method('getShareType')->willReturn(IShare::TYPE_USER); + $reShare->method('getSharedBy')->willReturn('userB'); + $reShare->method('getSharedWith')->willReturn('userC'); + $reShare->method('getNode')->willReturn($folder); + + $reShareInSubFolder = $this->createMock(IShare::class); + $reShareInSubFolder->method('getShareType')->willReturn(IShare::TYPE_USER); + $reShareInSubFolder->method('getSharedBy')->willReturn('userB'); + $reShareInSubFolder->method('getNode')->willReturn($subFolder); + + $reShareInOtherFolder = $this->createMock(IShare::class); + $reShareInOtherFolder->method('getShareType')->willReturn(IShare::TYPE_USER); + $reShareInOtherFolder->method('getSharedBy')->willReturn('userB'); + $reShareInOtherFolder->method('getNode')->willReturn($otherFolder); + + $this->defaultProvider->method('getSharesBy') + ->willReturnCallback(function ($userId, $shareType, $node, $reshares, $limit, $offset) use ($reShare, $reShareInSubFolder, $reShareInOtherFolder) { + if ($shareType === IShare::TYPE_USER) { + return match($userId) { + 'userB' => [$reShare,$reShareInSubFolder,$reShareInOtherFolder], + }; + } else { + return []; + } + }); + $manager->method('generalCreateChecks')->willThrowException(new GenericShareException()); + + $calls = [ + $reShare, + $reShareInSubFolder, + ]; + $manager->expects($this->exactly(2)) + ->method('updateShare') + ->willReturnCallback(function ($share) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, $share); + }); + + self::invokePrivate($manager, 'promoteReshares', [$share]); + } + + public function testPromoteReshareWhenUserHasAnotherShare(): void { + $manager = $this->createManagerMock() + ->onlyMethods(['updateShare', 'getSharesInFolder', 'getSharedWith', 'generalCreateChecks']) + ->getMock(); + + $folder = $this->createFolderMock('/path/to/folder'); + + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_USER); + $share->method('getNodeType')->willReturn('folder'); + $share->method('getSharedWith')->willReturn('userB'); + $share->method('getNode')->willReturn($folder); + + $reShare = $this->createMock(IShare::class); + $reShare->method('getShareType')->willReturn(IShare::TYPE_USER); + $reShare->method('getNodeType')->willReturn('folder'); + $reShare->method('getSharedBy')->willReturn('userB'); + $reShare->method('getNode')->willReturn($folder); + + $this->defaultProvider->method('getSharesBy')->willReturn([$reShare]); + $manager->method('generalCreateChecks')->willReturn(true); + + /* No share is promoted because generalCreateChecks does not throw */ + $manager->expects($this->never())->method('updateShare'); + + self::invokePrivate($manager, 'promoteReshares', [$share]); + } + + public function testPromoteReshareOfUsersInGroupShare(): void { + $manager = $this->createManagerMock() + ->onlyMethods(['updateShare', 'getSharesInFolder', 'getSharedWith', 'generalCreateChecks']) + ->getMock(); + + $folder = $this->createFolderMock('/path/to/folder'); + + $userA = $this->createMock(IUser::class); + $userA->method('getUID')->willReturn('userA'); + + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_GROUP); + $share->method('getNodeType')->willReturn('folder'); + $share->method('getSharedWith')->willReturn('Group'); + $share->method('getNode')->willReturn($folder); + $share->method('getShareOwner')->willReturn($userA); + + $reShare1 = $this->createMock(IShare::class); + $reShare1->method('getShareType')->willReturn(IShare::TYPE_USER); + $reShare1->method('getNodeType')->willReturn('folder'); + $reShare1->method('getSharedBy')->willReturn('userB'); + $reShare1->method('getNode')->willReturn($folder); + + $reShare2 = $this->createMock(IShare::class); + $reShare2->method('getShareType')->willReturn(IShare::TYPE_USER); + $reShare2->method('getNodeType')->willReturn('folder'); + $reShare2->method('getSharedBy')->willReturn('userC'); + $reShare2->method('getNode')->willReturn($folder); + + $userB = $this->createMock(IUser::class); + $userB->method('getUID')->willReturn('userB'); + $userC = $this->createMock(IUser::class); + $userC->method('getUID')->willReturn('userC'); + $group = $this->createMock(IGroup::class); + $group->method('getUsers')->willReturn([$userB, $userC]); + $this->groupManager->method('get')->with('Group')->willReturn($group); + + $this->defaultProvider->method('getSharesBy') + ->willReturnCallback(function ($userId, $shareType, $node, $reshares, $limit, $offset) use ($reShare1, $reShare2) { + if ($shareType === IShare::TYPE_USER) { + return match($userId) { + 'userB' => [$reShare1], + 'userC' => [$reShare2], + }; + } else { + return []; + } + }); + $manager->method('generalCreateChecks')->willThrowException(new GenericShareException()); + + $manager->method('getSharedWith')->willReturn([]); + + $calls = [ + $reShare1, + $reShare2, + ]; + $manager->expects($this->exactly(2)) + ->method('updateShare') + ->willReturnCallback(function ($share) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, $share); + }); + + self::invokePrivate($manager, 'promoteReshares', [$share]); + } + + public function testGetShareById(): void { $share = $this->createMock(IShare::class); $this->defaultProvider @@ -496,11 +689,11 @@ class ManagerTest extends \Test\TestCase { } - public function testGetExpiredShareById() { - $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class); + public function testGetExpiredShareById(): void { + $this->expectException(ShareNotFound::class); $manager = $this->createManagerMock() - ->setMethods(['deleteShare']) + ->onlyMethods(['deleteShare']) ->getMock(); $date = new \DateTime(); @@ -523,19 +716,22 @@ class ManagerTest extends \Test\TestCase { } - public function testVerifyPasswordNullButEnforced() { + public function testVerifyPasswordNullButEnforced(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Passwords are enforced for link and mail shares'); $this->config->method('getAppValue')->willReturnMap([ ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], - ['core', 'shareapi_enforce_links_password', 'no', 'yes'], + ]); + + $this->appConfig->method('getValueBool')->willReturnMap([ + ['core', 'shareapi_enforce_links_password', true], ]); self::invokePrivate($this->manager, 'verifyPassword', [null]); } - public function testVerifyPasswordNotEnforcedGroup() { + public function testVerifyPasswordNotEnforcedGroup(): void { $this->config->method('getAppValue')->willReturnMap([ ['core', 'shareapi_enforce_links_password_excluded_groups', '', '["admin"]'], ['core', 'shareapi_enforce_links_password', 'no', 'yes'], @@ -550,7 +746,7 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($result); } - public function testVerifyPasswordNotEnforcedMultipleGroups() { + public function testVerifyPasswordNotEnforcedMultipleGroups(): void { $this->config->method('getAppValue')->willReturnMap([ ['core', 'shareapi_enforce_links_password_excluded_groups', '', '["admin", "special"]'], ['core', 'shareapi_enforce_links_password', 'no', 'yes'], @@ -565,7 +761,7 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($result); } - public function testVerifyPasswordNull() { + public function testVerifyPasswordNull(): void { $this->config->method('getAppValue')->willReturnMap([ ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ['core', 'shareapi_enforce_links_password', 'no', 'no'], @@ -575,14 +771,14 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($result); } - public function testVerifyPasswordHook() { + public function testVerifyPasswordHook(): void { $this->config->method('getAppValue')->willReturnMap([ ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ['core', 'shareapi_enforce_links_password', 'no', 'no'], ]); $this->dispatcher->expects($this->once())->method('dispatchTyped') - ->willReturnCallback(function (Event $event) { + ->willReturnCallback(function (Event $event): void { $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event); /** @var ValidatePasswordPolicyEvent $event */ $this->assertSame('password', $event->getPassword()); @@ -594,7 +790,7 @@ class ManagerTest extends \Test\TestCase { } - public function testVerifyPasswordHookFails() { + public function testVerifyPasswordHookFails(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('password not accepted'); @@ -604,18 +800,18 @@ class ManagerTest extends \Test\TestCase { ]); $this->dispatcher->expects($this->once())->method('dispatchTyped') - ->willReturnCallback(function (Event $event) { + ->willReturnCallback(function (Event $event): void { $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event); /** @var ValidatePasswordPolicyEvent $event */ $this->assertSame('password', $event->getPassword()); - throw new HintException('message', 'password not accepted'); + throw new HintException('password not accepted'); } ); self::invokePrivate($this->manager, 'verifyPassword', ['password']); } - public function createShare($id, $type, $path, $sharedWith, $sharedBy, $shareOwner, + public function createShare($id, $type, $node, $sharedWith, $sharedBy, $shareOwner, $permissions, $expireDate = null, $password = null, $attributes = null) { $share = $this->createMock(IShare::class); @@ -623,7 +819,10 @@ class ManagerTest extends \Test\TestCase { $share->method('getSharedWith')->willReturn($sharedWith); $share->method('getSharedBy')->willReturn($sharedBy); $share->method('getShareOwner')->willReturn($shareOwner); - $share->method('getNode')->willReturn($path); + $share->method('getNode')->willReturn($node); + if ($node && $node->getId()) { + $share->method('getNodeId')->willReturn($node->getId()); + } $share->method('getPermissions')->willReturn($permissions); $share->method('getAttributes')->willReturn($attributes); $share->method('getExpirationDate')->willReturn($expireDate); @@ -642,43 +841,46 @@ class ManagerTest extends \Test\TestCase { $file = $this->createMock(File::class); $node = $this->createMock(Node::class); - $storage = $this->createMock(Storage\IStorage::class); + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') ->with('\OCA\Files_Sharing\External\Storage') ->willReturn(false); $file->method('getStorage') ->willReturn($storage); + $file->method('getId')->willReturn(108); $node->method('getStorage') ->willReturn($storage); + $node->method('getId')->willReturn(108); $data = [ - [$this->createShare(null, IShare::TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true], - [$this->createShare(null, IShare::TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true], - [$this->createShare(null, IShare::TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid user', true], - [$this->createShare(null, IShare::TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true], - [$this->createShare(null, IShare::TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true], - [$this->createShare(null, IShare::TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith is not a valid group', true], - [$this->createShare(null, IShare::TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true], - [$this->createShare(null, IShare::TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true], - [$this->createShare(null, IShare::TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true], - [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unknown share type', true], - - [$this->createShare(null, IShare::TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true], - [$this->createShare(null, IShare::TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true], - [$this->createShare(null, IShare::TYPE_LINK, $file, null, null, $user0, 31, null, null), 'SharedBy should be set', true], + [$this->createShare(null, IShare::TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'Share recipient is not a valid user', true], + [$this->createShare(null, IShare::TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'Share recipient is not a valid user', true], + [$this->createShare(null, IShare::TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'Share recipient is not a valid user', true], + [$this->createShare(null, IShare::TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'Share recipient is not a valid group', true], + [$this->createShare(null, IShare::TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'Share recipient is not a valid group', true], + [$this->createShare(null, IShare::TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'Share recipient is not a valid group', true], + [$this->createShare(null, IShare::TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'Share recipient should be empty', true], + [$this->createShare(null, IShare::TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'Share recipient should be empty', true], + [$this->createShare(null, IShare::TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'Share recipient should be empty', true], + [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'Unknown share type', true], + + [$this->createShare(null, IShare::TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'Share initiator must be set', true], + [$this->createShare(null, IShare::TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'Share initiator must be set', true], + [$this->createShare(null, IShare::TYPE_LINK, $file, null, null, $user0, 31, null, null), 'Share initiator must be set', true], [$this->createShare(null, IShare::TYPE_USER, $file, $user0, $user0, $user0, 31, null, null), 'Cannot share with yourself', true], - [$this->createShare(null, IShare::TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Path should be set', true], - [$this->createShare(null, IShare::TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Path should be set', true], - [$this->createShare(null, IShare::TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Path should be set', true], + [$this->createShare(null, IShare::TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Shared path must be set', true], + [$this->createShare(null, IShare::TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Shared path must be set', true], + [$this->createShare(null, IShare::TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Shared path must be set', true], - [$this->createShare(null, IShare::TYPE_USER, $node, $user2, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true], - [$this->createShare(null, IShare::TYPE_GROUP, $node, $group0, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true], - [$this->createShare(null, IShare::TYPE_LINK, $node, null, $user0, $user0, 31, null, null), 'Path should be either a file or a folder', true], + [$this->createShare(null, IShare::TYPE_USER, $node, $user2, $user0, $user0, 31, null, null), 'Shared path must be either a file or a folder', true], + [$this->createShare(null, IShare::TYPE_GROUP, $node, $group0, $user0, $user0, 31, null, null), 'Shared path must be either a file or a folder', true], + [$this->createShare(null, IShare::TYPE_LINK, $node, null, $user0, $user0, 31, null, null), 'Shared path must be either a file or a folder', true], ]; $nonShareAble = $this->createMock(Folder::class); + $nonShareAble->method('getId')->willReturn(108); $nonShareAble->method('isShareable')->willReturn(false); $nonShareAble->method('getPath')->willReturn('path'); $nonShareAble->method('getName')->willReturn('name'); @@ -693,7 +895,7 @@ class ManagerTest extends \Test\TestCase { $limitedPermssions = $this->createMock(File::class); $limitedPermssions->method('isShareable')->willReturn(true); - $limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ); + $limitedPermssions->method('getPermissions')->willReturn(Constants::PERMISSION_READ); $limitedPermssions->method('getId')->willReturn(108); $limitedPermssions->method('getPath')->willReturn('path'); $limitedPermssions->method('getName')->willReturn('name'); @@ -702,44 +904,63 @@ class ManagerTest extends \Test\TestCase { $limitedPermssions->method('getStorage') ->willReturn($storage); - $data[] = [$this->createShare(null, IShare::TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'A share requires permissions', true]; - $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'A share requires permissions', true]; - $data[] = [$this->createShare(null, IShare::TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'A share requires permissions', true]; + $data[] = [$this->createShare(null, IShare::TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'Valid permissions are required for sharing', true]; + $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'Valid permissions are required for sharing', true]; + $data[] = [$this->createShare(null, IShare::TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'Valid permissions are required for sharing', true]; $mount = $this->createMock(MoveableMount::class); $limitedPermssions->method('getMountPoint')->willReturn($mount); - - $data[] = [$this->createShare(null, IShare::TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Cannot increase permissions of path', true]; + // increase permissions of a re-share $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Cannot increase permissions of path', true]; - $data[] = [$this->createShare(null, IShare::TYPE_LINK, $limitedPermssions, null, $user0, $user0, 3, null, null), 'Cannot increase permissions of path', true]; + $data[] = [$this->createShare(null, IShare::TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 3, null, null), 'Cannot increase permissions of path', true]; + $nonMovableStorage = $this->createMock(IStorage::class); + $nonMovableStorage->method('instanceOfStorage') + ->with('\OCA\Files_Sharing\External\Storage') + ->willReturn(false); + $nonMovableStorage->method('getPermissions')->willReturn(Constants::PERMISSION_ALL); $nonMoveableMountPermssions = $this->createMock(Folder::class); $nonMoveableMountPermssions->method('isShareable')->willReturn(true); - $nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ); + $nonMoveableMountPermssions->method('getPermissions')->willReturn(Constants::PERMISSION_READ); $nonMoveableMountPermssions->method('getId')->willReturn(108); $nonMoveableMountPermssions->method('getPath')->willReturn('path'); $nonMoveableMountPermssions->method('getName')->willReturn('name'); + $nonMoveableMountPermssions->method('getInternalPath')->willReturn(''); $nonMoveableMountPermssions->method('getOwner') ->willReturn($owner); $nonMoveableMountPermssions->method('getStorage') - ->willReturn($storage); + ->willReturn($nonMovableStorage); $data[] = [$this->createShare(null, IShare::TYPE_USER, $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false]; $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false]; $rootFolder = $this->createMock(Folder::class); $rootFolder->method('isShareable')->willReturn(true); - $rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL); + $rootFolder->method('getPermissions')->willReturn(Constants::PERMISSION_ALL); $rootFolder->method('getId')->willReturn(42); $data[] = [$this->createShare(null, IShare::TYPE_USER, $rootFolder, $user2, $user0, $user0, 30, null, null), 'You cannot share your root folder', true]; $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You cannot share your root folder', true]; $data[] = [$this->createShare(null, IShare::TYPE_LINK, $rootFolder, null, $user0, $user0, 16, null, null), 'You cannot share your root folder', true]; + $allPermssionsFiles = $this->createMock(File::class); + $allPermssionsFiles->method('isShareable')->willReturn(true); + $allPermssionsFiles->method('getPermissions')->willReturn(Constants::PERMISSION_ALL); + $allPermssionsFiles->method('getId')->willReturn(187); + $allPermssionsFiles->method('getOwner') + ->willReturn($owner); + $allPermssionsFiles->method('getStorage') + ->willReturn($storage); + + // test invalid CREATE or DELETE permissions + $data[] = [$this->createShare(null, IShare::TYPE_USER, $allPermssionsFiles, $user2, $user0, $user0, Constants::PERMISSION_ALL, null, null), 'File shares cannot have create or delete permissions', true]; + $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $allPermssionsFiles, $group0, $user0, $user0, Constants::PERMISSION_READ | Constants::PERMISSION_CREATE, null, null), 'File shares cannot have create or delete permissions', true]; + $data[] = [$this->createShare(null, IShare::TYPE_LINK, $allPermssionsFiles, null, $user0, $user0, Constants::PERMISSION_READ | Constants::PERMISSION_DELETE, null, null), 'File shares cannot have create or delete permissions', true]; + $allPermssions = $this->createMock(Folder::class); $allPermssions->method('isShareable')->willReturn(true); - $allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL); + $allPermssions->method('getPermissions')->willReturn(Constants::PERMISSION_ALL); $allPermssions->method('getId')->willReturn(108); $allPermssions->method('getOwner') ->willReturn($owner); @@ -749,18 +970,24 @@ class ManagerTest extends \Test\TestCase { $data[] = [$this->createShare(null, IShare::TYPE_USER, $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true]; $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true]; + // test invalid permissions + $data[] = [$this->createShare(null, IShare::TYPE_USER, $allPermssions, $user2, $user0, $user0, 32, null, null), 'Valid permissions are required for sharing', true]; + $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 63, null, null), 'Valid permissions are required for sharing', true]; + $data[] = [$this->createShare(null, IShare::TYPE_LINK, $allPermssions, null, $user0, $user0, -1, null, null), 'Valid permissions are required for sharing', true]; + + // working shares $data[] = [$this->createShare(null, IShare::TYPE_USER, $allPermssions, $user2, $user0, $user0, 31, null, null), null, false]; $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false]; $data[] = [$this->createShare(null, IShare::TYPE_LINK, $allPermssions, null, $user0, $user0, 17, null, null), null, false]; - $remoteStorage = $this->createMock(Storage\IStorage::class); + $remoteStorage = $this->createMock(IStorage::class); $remoteStorage->method('instanceOfStorage') ->with('\OCA\Files_Sharing\External\Storage') ->willReturn(true); $remoteFile = $this->createMock(Folder::class); $remoteFile->method('isShareable')->willReturn(true); - $remoteFile->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ ^ \OCP\Constants::PERMISSION_UPDATE); + $remoteFile->method('getPermissions')->willReturn(Constants::PERMISSION_READ ^ Constants::PERMISSION_UPDATE); $remoteFile->method('getId')->willReturn(108); $remoteFile->method('getOwner') ->willReturn($owner); @@ -774,13 +1001,13 @@ class ManagerTest extends \Test\TestCase { } /** - * @dataProvider dataGeneralChecks * * @param $share * @param $exceptionMessage * @param $exception */ - public function testGeneralChecks($share, $exceptionMessage, $exception) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGeneralChecks')] + public function testGeneralChecks($share, $exceptionMessage, $exception): void { $thrown = null; $this->userManager->method('userExists')->willReturnMap([ @@ -797,10 +1024,9 @@ class ManagerTest extends \Test\TestCase { ->method('getId') ->willReturn(42); // Id 108 is used in the data to refer to the node of the share. - $userFolder->expects($this->any()) - ->method('getFirstNodeById') + $userFolder->method('getById') ->with(108) - ->willReturn($share->getNode()); + ->willReturn([$share->getNode()]); $userFolder->expects($this->any()) ->method('getRelativePath') ->willReturnArgument(0); @@ -810,7 +1036,7 @@ class ManagerTest extends \Test\TestCase { try { self::invokePrivate($this->manager, 'generalCreateChecks', [$share]); $thrown = false; - } catch (\OCP\Share\Exceptions\GenericShareException $e) { + } catch (GenericShareException $e) { $this->assertEquals($exceptionMessage, $e->getHint()); $thrown = true; } catch (\InvalidArgumentException $e) { @@ -822,7 +1048,7 @@ class ManagerTest extends \Test\TestCase { } - public function testGeneralCheckShareRoot() { + public function testGeneralCheckShareRoot(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('You cannot share your root folder'); @@ -847,15 +1073,13 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'generalCreateChecks', [$share]); } - public function validateExpirationDateInternalProvider() { + public static function validateExpirationDateInternalProvider() { return [[IShare::TYPE_USER], [IShare::TYPE_REMOTE], [IShare::TYPE_REMOTE_GROUP]]; } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalInPast($shareType) { - $this->expectException(\OCP\Share\Exceptions\GenericShareException::class); + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalInPast($shareType): void { + $this->expectException(GenericShareException::class); $this->expectExceptionMessage('Expiration date is in the past'); // Expire date in the past @@ -869,10 +1093,8 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalEnforceButNotSet($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalEnforceButNotSet($shareType): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Expiration date is enforced'); @@ -896,10 +1118,8 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalEnforceButNotEnabledAndNotSet($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalEnforceButNotEnabledAndNotSet($shareType): void { $share = $this->manager->newShare(); $share->setProviderId('foo')->setId('bar'); $share->setShareType($shareType); @@ -921,10 +1141,8 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalEnforceButNotSetNewShare($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalEnforceButNotSetNewShare($shareType): void { $share = $this->manager->newShare(); $share->setShareType($shareType); @@ -956,10 +1174,8 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalEnforceRelaxedDefaultButNotSetNewShare($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalEnforceRelaxedDefaultButNotSetNewShare($shareType): void { $share = $this->manager->newShare(); $share->setShareType($shareType); @@ -991,11 +1207,9 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalEnforceTooFarIntoFuture($shareType) { - $this->expectException(\OCP\Share\Exceptions\GenericShareException::class); + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalEnforceTooFarIntoFuture($shareType): void { + $this->expectException(GenericShareException::class); $this->expectExceptionMessage('Cannot set expiration date more than 3 days in the future'); $future = new \DateTime(); @@ -1024,10 +1238,8 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalEnforceValid($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalEnforceValid($shareType): void { $future = new \DateTime('now', $this->dateTimeZone->getTimeZone()); $future->add(new \DateInterval('P2D')); $future->setTime(1, 2, 3); @@ -1055,8 +1267,8 @@ class ManagerTest extends \Test\TestCase { ]); } - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) { return $data['expirationDate'] == $future; })); @@ -1066,10 +1278,8 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalNoDefault($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalNoDefault($shareType): void { $date = new \DateTime('now', $this->dateTimeZone->getTimeZone()); $date->add(new \DateInterval('P5D')); $date->setTime(1, 2, 3); @@ -1081,8 +1291,8 @@ class ManagerTest extends \Test\TestCase { $share->setShareType($shareType); $share->setExpirationDate($date); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) { return $data['expirationDate'] == $expected && $data['passwordSet'] === false; })); @@ -1092,12 +1302,10 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalNoDateNoDefault($shareType) { - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalNoDateNoDefault($shareType): void { + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) { return $data['expirationDate'] === null && $data['passwordSet'] === true; })); @@ -1111,10 +1319,8 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalNoDateDefault($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalNoDateDefault($shareType): void { $share = $this->manager->newShare(); $share->setShareType($shareType); @@ -1139,8 +1345,8 @@ class ManagerTest extends \Test\TestCase { ]); } - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) { return $data['expirationDate'] == $expected; })); @@ -1150,10 +1356,8 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalDefault($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalDefault($shareType): void { $future = new \DateTime('now', $this->timezone); $future->add(new \DateInterval('P5D')); $future->setTime(1, 2, 3); @@ -1181,8 +1385,8 @@ class ManagerTest extends \Test\TestCase { ]); } - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) { return $data['expirationDate'] == $expected; })); @@ -1192,19 +1396,17 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalHookModification($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalHookModification($shareType): void { $nextWeek = new \DateTime('now', $this->timezone); $nextWeek->add(new \DateInterval('P7D')); $nextWeek->setTime(0, 0, 0); $save = clone $nextWeek; - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); - $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) { + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data): void { $data['expirationDate']->sub(new \DateInterval('P2D')); }); @@ -1218,10 +1420,8 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($save, $share->getExpirationDate()); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalHookException($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalHookException($shareType): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Invalid date!'); @@ -1233,9 +1433,9 @@ class ManagerTest extends \Test\TestCase { $share->setShareType($shareType); $share->setExpirationDate($nextWeek); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); - $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) { + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data): void { $data['accepted'] = false; $data['message'] = 'Invalid date!'; }); @@ -1243,10 +1443,8 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]); } - /** - * @dataProvider validateExpirationDateInternalProvider - */ - public function testValidateExpirationDateInternalExistingShareNoDefault($shareType) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateExpirationDateInternalProvider')] + public function testValidateExpirationDateInternalExistingShareNoDefault($shareType): void { $share = $this->manager->newShare(); $share->setShareType($shareType); $share->setId('42')->setProviderId('foo'); @@ -1270,8 +1468,8 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals(null, $share->getExpirationDate()); } - public function testValidateExpirationDateInPast() { - $this->expectException(\OCP\Share\Exceptions\GenericShareException::class); + public function testValidateExpirationDateInPast(): void { + $this->expectException(GenericShareException::class); $this->expectExceptionMessage('Expiration date is in the past'); // Expire date in the past @@ -1284,7 +1482,7 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]); } - public function testValidateExpirationDateEnforceButNotSet() { + public function testValidateExpirationDateEnforceButNotSet(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Expiration date is enforced'); @@ -1300,7 +1498,7 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]); } - public function testValidateExpirationDateEnforceButNotEnabledAndNotSet() { + public function testValidateExpirationDateEnforceButNotEnabledAndNotSet(): void { $share = $this->manager->newShare(); $share->setProviderId('foo')->setId('bar'); @@ -1314,7 +1512,7 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($share->getExpirationDate()); } - public function testValidateExpirationDateEnforceButNotSetNewShare() { + public function testValidateExpirationDateEnforceButNotSetNewShare(): void { $share = $this->manager->newShare(); $this->config->method('getAppValue') @@ -1335,7 +1533,7 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - public function testValidateExpirationDateEnforceRelaxedDefaultButNotSetNewShare() { + public function testValidateExpirationDateEnforceRelaxedDefaultButNotSetNewShare(): void { $share = $this->manager->newShare(); $this->config->method('getAppValue') @@ -1356,8 +1554,8 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - public function testValidateExpirationDateEnforceTooFarIntoFuture() { - $this->expectException(\OCP\Share\Exceptions\GenericShareException::class); + public function testValidateExpirationDateEnforceTooFarIntoFuture(): void { + $this->expectException(GenericShareException::class); $this->expectExceptionMessage('Cannot set expiration date more than 3 days in the future'); $future = new \DateTime(); @@ -1376,7 +1574,7 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]); } - public function testValidateExpirationDateEnforceValid() { + public function testValidateExpirationDateEnforceValid(): void { $future = new \DateTime('now', $this->timezone); $future->add(new \DateInterval('P2D')); $future->setTime(1, 2, 3); @@ -1394,8 +1592,8 @@ class ManagerTest extends \Test\TestCase { ['core', 'shareapi_default_expire_date', 'no', 'yes'], ]); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) { return $data['expirationDate'] == $future; })); @@ -1405,7 +1603,7 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - public function testValidateExpirationDateNoDefault() { + public function testValidateExpirationDateNoDefault(): void { $date = new \DateTime('now', $this->timezone); $date->add(new \DateInterval('P5D')); $date->setTime(1, 2, 3); @@ -1417,8 +1615,8 @@ class ManagerTest extends \Test\TestCase { $share = $this->manager->newShare(); $share->setExpirationDate($date); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) { return $data['expirationDate'] == $expected && $data['passwordSet'] === false; })); @@ -1428,9 +1626,9 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - public function testValidateExpirationDateNoDateNoDefault() { - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + public function testValidateExpirationDateNoDateNoDefault(): void { + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) { return $data['expirationDate'] === null && $data['passwordSet'] === true; })); @@ -1443,7 +1641,7 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($share->getExpirationDate()); } - public function testValidateExpirationDateNoDateDefault() { + public function testValidateExpirationDateNoDateDefault(): void { $share = $this->manager->newShare(); $expected = new \DateTime('now', $this->timezone); @@ -1458,8 +1656,8 @@ class ManagerTest extends \Test\TestCase { ['core', 'link_defaultExpDays', '3', '3'], ]); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) { return $data['expirationDate'] == $expected; })); @@ -1469,7 +1667,7 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - public function testValidateExpirationDateDefault() { + public function testValidateExpirationDateDefault(): void { $future = new \DateTime('now', $this->timezone); $future->add(new \DateInterval('P5D')); $future->setTime(1, 2, 3); @@ -1488,8 +1686,8 @@ class ManagerTest extends \Test\TestCase { ['core', 'link_defaultExpDays', '3', '1'], ]); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) { return $data['expirationDate'] == $expected; })); @@ -1499,7 +1697,7 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - public function testValidateExpirationNegativeOffsetTimezone() { + public function testValidateExpirationNegativeOffsetTimezone(): void { $this->timezone = new \DateTimeZone('Pacific/Tahiti'); $future = new \DateTime(); $future->add(new \DateInterval('P5D')); @@ -1519,8 +1717,8 @@ class ManagerTest extends \Test\TestCase { ['core', 'link_defaultExpDays', '3', '1'], ]); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) { return $data['expirationDate'] == $expected; })); @@ -1530,7 +1728,7 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $share->getExpirationDate()); } - public function testValidateExpirationDateHookModification() { + public function testValidateExpirationDateHookModification(): void { $nextWeek = new \DateTime('now', $this->timezone); $nextWeek->add(new \DateInterval('P7D')); @@ -1539,9 +1737,9 @@ class ManagerTest extends \Test\TestCase { $save->sub(new \DateInterval('P2D')); $save->setTimezone(new \DateTimeZone(date_default_timezone_get())); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); - $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) { + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data): void { $data['expirationDate']->sub(new \DateInterval('P2D')); }); @@ -1553,7 +1751,7 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($save, $share->getExpirationDate()); } - public function testValidateExpirationDateHookException() { + public function testValidateExpirationDateHookException(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Invalid date!'); @@ -1564,9 +1762,9 @@ class ManagerTest extends \Test\TestCase { $share = $this->manager->newShare(); $share->setExpirationDate($nextWeek); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock(); - \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); - $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) { + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener'); + $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data): void { $data['accepted'] = false; $data['message'] = 'Invalid date!'; }); @@ -1574,7 +1772,7 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]); } - public function testValidateExpirationDateExistingShareNoDefault() { + public function testValidateExpirationDateExistingShareNoDefault(): void { $share = $this->manager->newShare(); $share->setId('42')->setProviderId('foo'); @@ -1590,7 +1788,7 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals(null, $share->getExpirationDate()); } - public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups() { + public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Sharing is only allowed with group members'); @@ -1624,7 +1822,7 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'userCreateChecks', [$share]); } - public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup() { + public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup(): void { $share = $this->manager->newShare(); $sharedBy = $this->createMock(IUser::class); @@ -1665,7 +1863,7 @@ class ManagerTest extends \Test\TestCase { } - public function testUserCreateChecksIdenticalShareExists() { + public function testUserCreateChecksIdenticalShareExists(): void { $this->expectException(AlreadySharedException::class); $this->expectExceptionMessage('Sharing name.txt failed, because this item is already shared with the account user'); @@ -1694,7 +1892,7 @@ class ManagerTest extends \Test\TestCase { } - public function testUserCreateChecksIdenticalPathSharedViaGroup() { + public function testUserCreateChecksIdenticalPathSharedViaGroup(): void { $this->expectException(AlreadySharedException::class); $this->expectExceptionMessage('Sharing name2.txt failed, because this item is already shared with the account userName'); @@ -1739,7 +1937,7 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'userCreateChecks', [$share]); } - public function testUserCreateChecksIdenticalPathSharedViaDeletedGroup() { + public function testUserCreateChecksIdenticalPathSharedViaDeletedGroup(): void { $share = $this->manager->newShare(); $sharedWith = $this->createMock(IUser::class); @@ -1772,7 +1970,7 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($this->invokePrivate($this->manager, 'userCreateChecks', [$share])); } - public function testUserCreateChecksIdenticalPathNotSharedWithUser() { + public function testUserCreateChecksIdenticalPathNotSharedWithUser(): void { $share = $this->manager->newShare(); $sharedWith = $this->createMock(IUser::class); $path = $this->createMock(Node::class); @@ -1809,7 +2007,7 @@ class ManagerTest extends \Test\TestCase { } - public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed() { + public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Group sharing is now allowed'); @@ -1825,7 +2023,7 @@ class ManagerTest extends \Test\TestCase { } - public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup() { + public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Sharing is only allowed within your own groups'); @@ -1852,7 +2050,7 @@ class ManagerTest extends \Test\TestCase { } - public function testGroupCreateChecksShareWithGroupMembersOnlyNullGroup() { + public function testGroupCreateChecksShareWithGroupMembersOnlyNullGroup(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Sharing is only allowed within your own groups'); @@ -1875,7 +2073,7 @@ class ManagerTest extends \Test\TestCase { $this->assertNull($this->invokePrivate($this->manager, 'groupCreateChecks', [$share])); } - public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup() { + public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup(): void { $share = $this->manager->newShare(); $user = $this->createMock(IUser::class); @@ -1907,7 +2105,7 @@ class ManagerTest extends \Test\TestCase { } - public function testGroupCreateChecksPathAlreadySharedWithSameGroup() { + public function testGroupCreateChecksPathAlreadySharedWithSameGroup(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Path is already shared with this group'); @@ -1937,7 +2135,7 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'groupCreateChecks', [$share]); } - public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup() { + public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup(): void { $share = $this->manager->newShare(); $share->setSharedWith('sharedWith'); @@ -1963,7 +2161,7 @@ class ManagerTest extends \Test\TestCase { } - public function testLinkCreateChecksNoLinkSharesAllowed() { + public function testLinkCreateChecksNoLinkSharesAllowed(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Link sharing is not allowed'); @@ -1979,10 +2177,10 @@ class ManagerTest extends \Test\TestCase { } - public function testFileLinkCreateChecksNoPublicUpload() { + public function testFileLinkCreateChecksNoPublicUpload(): void { $share = $this->manager->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE); + $share->setPermissions(Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE); $share->setNodeType('file'); $this->config @@ -1996,13 +2194,13 @@ class ManagerTest extends \Test\TestCase { $this->addToAssertionCount(1); } - public function testFolderLinkCreateChecksNoPublicUpload() { + public function testFolderLinkCreateChecksNoPublicUpload(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Public upload is not allowed'); $share = $this->manager->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE); + $share->setPermissions(Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE); $share->setNodeType('folder'); $this->config @@ -2015,10 +2213,10 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'linkCreateChecks', [$share]); } - public function testLinkCreateChecksPublicUpload() { + public function testLinkCreateChecksPublicUpload(): void { $share = $this->manager->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE); + $share->setPermissions(Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE); $share->setSharedWith('sharedWith'); $folder = $this->createMock(\OC\Files\Node\Folder::class); $share->setNode($folder); @@ -2034,10 +2232,10 @@ class ManagerTest extends \Test\TestCase { $this->addToAssertionCount(1); } - public function testLinkCreateChecksReadOnly() { + public function testLinkCreateChecksReadOnly(): void { $share = $this->manager->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_READ); + $share->setPermissions(Constants::PERMISSION_READ); $share->setSharedWith('sharedWith'); $folder = $this->createMock(\OC\Files\Node\Folder::class); $share->setNode($folder); @@ -2054,15 +2252,15 @@ class ManagerTest extends \Test\TestCase { } - public function testPathCreateChecksContainsSharedMount() { + public function testPathCreateChecksContainsSharedMount(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Path contains files shared with you'); + $this->expectExceptionMessage('You cannot share a folder that contains other shares'); $path = $this->createMock(Folder::class); $path->method('getPath')->willReturn('path'); $mount = $this->createMock(IMountPoint::class); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $mount->method('getStorage')->willReturn($storage); $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(true); @@ -2071,12 +2269,12 @@ class ManagerTest extends \Test\TestCase { self::invokePrivate($this->manager, 'pathCreateChecks', [$path]); } - public function testPathCreateChecksContainsNoSharedMount() { + public function testPathCreateChecksContainsNoSharedMount(): void { $path = $this->createMock(Folder::class); $path->method('getPath')->willReturn('path'); $mount = $this->createMock(IMountPoint::class); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $mount->method('getStorage')->willReturn($storage); $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(false); @@ -2086,14 +2284,14 @@ class ManagerTest extends \Test\TestCase { $this->addToAssertionCount(1); } - public function testPathCreateChecksContainsNoFolder() { + public function testPathCreateChecksContainsNoFolder(): void { $path = $this->createMock(File::class); self::invokePrivate($this->manager, 'pathCreateChecks', [$path]); $this->addToAssertionCount(1); } - public function dataIsSharingDisabledForUser() { + public static function dataIsSharingDisabledForUser() { $data = []; // No exclude groups @@ -2131,7 +2329,6 @@ class ManagerTest extends \Test\TestCase { } /** - * @dataProvider dataIsSharingDisabledForUser * * @param string $excludeGroups * @param string $groupList @@ -2139,7 +2336,8 @@ class ManagerTest extends \Test\TestCase { * @param string[] $groupIds * @param bool $expected */ - public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataIsSharingDisabledForUser')] + public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected): void { $user = $this->createMock(IUser::class); $this->config->method('getAppValue') @@ -2167,7 +2365,7 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, $res); } - public function dataCanShare() { + public static function dataCanShare() { $data = []; /* @@ -2183,20 +2381,20 @@ class ManagerTest extends \Test\TestCase { } /** - * @dataProvider dataCanShare * * @param bool $expected * @param string $sharingEnabled * @param bool $disabledForUser */ - public function testCanShare($expected, $sharingEnabled, $disabledForUser) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataCanShare')] + public function testCanShare($expected, $sharingEnabled, $disabledForUser): void { $this->config->method('getAppValue') ->willReturnMap([ ['core', 'shareapi_enabled', 'yes', $sharingEnabled], ]); $manager = $this->createManagerMock() - ->setMethods(['sharingDisabledForUser']) + ->onlyMethods(['sharingDisabledForUser']) ->getMock(); $manager->method('sharingDisabledForUser') @@ -2216,15 +2414,16 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals($expected, !$exception); } - public function testCreateShareUser() { + public function testCreateShareUser(): void { + /** @var Manager&MockObject $manager */ $manager = $this->createManagerMock() - ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks']) + ->onlyMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks']) ->getMock(); $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $path = $this->createMock(File::class); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); @@ -2237,7 +2436,7 @@ class ManagerTest extends \Test\TestCase { 'sharedWith', 'sharedBy', null, - \OCP\Constants::PERMISSION_ALL); + Constants::PERMISSION_ALL); $manager->expects($this->once()) ->method('canShare') @@ -2271,15 +2470,15 @@ class ManagerTest extends \Test\TestCase { $manager->createShare($share); } - public function testCreateShareGroup() { + public function testCreateShareGroup(): void { $manager = $this->createManagerMock() - ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks']) + ->onlyMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks']) ->getMock(); $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $path = $this->createMock(File::class); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); @@ -2292,7 +2491,7 @@ class ManagerTest extends \Test\TestCase { 'sharedWith', 'sharedBy', null, - \OCP\Constants::PERMISSION_ALL); + Constants::PERMISSION_ALL); $manager->expects($this->once()) ->method('canShare') @@ -2326,9 +2525,9 @@ class ManagerTest extends \Test\TestCase { $manager->createShare($share); } - public function testCreateShareLink() { + public function testCreateShareLink(): void { $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'generalCreateChecks', 'linkCreateChecks', @@ -2342,7 +2541,7 @@ class ManagerTest extends \Test\TestCase { $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $path = $this->createMock(File::class); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); @@ -2355,7 +2554,7 @@ class ManagerTest extends \Test\TestCase { $share->setShareType(IShare::TYPE_LINK) ->setNode($path) ->setSharedBy('sharedBy') - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setExpirationDate($date) ->setPassword('password'); @@ -2401,40 +2600,30 @@ class ManagerTest extends \Test\TestCase { return $share->setId(42); }); + $calls = [ + BeforeShareCreatedEvent::class, + ShareCreatedEvent::class, + ]; $this->dispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->withConsecutive( - // Pre share - [ - $this->callback(function (BeforeShareCreatedEvent $e) use ($path, $date) { - $share = $e->getShare(); - - return $share->getShareType() === IShare::TYPE_LINK && - $share->getNode() === $path && - $share->getSharedBy() === 'sharedBy' && - $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && - $share->getExpirationDate() === $date && - $share->getPassword() === 'hashed' && - $share->getToken() === 'token'; - }) - ], - // Post share - [ - $this->callback(function (ShareCreatedEvent $e) use ($path, $date) { - $share = $e->getShare(); - - return $share->getShareType() === IShare::TYPE_LINK && - $share->getNode() === $path && - $share->getSharedBy() === 'sharedBy' && - $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && - $share->getExpirationDate() === $date && - $share->getPassword() === 'hashed' && - $share->getToken() === 'token' && - $share->getId() === '42' && - $share->getTarget() === '/target'; - }) - ] - ); + ->willReturnCallback(function ($event) use (&$calls, $date, $path): void { + $expected = array_shift($calls); + $this->assertInstanceOf($expected, $event); + $share = $event->getShare(); + + $this->assertEquals(IShare::TYPE_LINK, $share->getShareType(), 'getShareType'); + $this->assertEquals($path, $share->getNode(), 'getNode'); + $this->assertEquals('sharedBy', $share->getSharedBy(), 'getSharedBy'); + $this->assertEquals(Constants::PERMISSION_ALL, $share->getPermissions(), 'getPermissions'); + $this->assertEquals($date, $share->getExpirationDate(), 'getExpirationDate'); + $this->assertEquals('hashed', $share->getPassword(), 'getPassword'); + $this->assertEquals('token', $share->getToken(), 'getToken'); + + if ($expected === ShareCreatedEvent::class) { + $this->assertEquals('42', $share->getId(), 'getId'); + $this->assertEquals('/target', $share->getTarget(), 'getTarget'); + } + }); /** @var IShare $share */ $share = $manager->createShare($share); @@ -2446,9 +2635,9 @@ class ManagerTest extends \Test\TestCase { $this->assertEquals('hashed', $share->getPassword()); } - public function testCreateShareMail() { + public function testCreateShareMail(): void { $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'generalCreateChecks', 'linkCreateChecks', @@ -2462,7 +2651,7 @@ class ManagerTest extends \Test\TestCase { $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $path = $this->createMock(File::class); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); @@ -2473,7 +2662,7 @@ class ManagerTest extends \Test\TestCase { $share->setShareType(IShare::TYPE_EMAIL) ->setNode($path) ->setSharedBy('sharedBy') - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once()) ->method('canShare') @@ -2508,38 +2697,30 @@ class ManagerTest extends \Test\TestCase { return $share->setId(42); }); + $calls = [ + BeforeShareCreatedEvent::class, + ShareCreatedEvent::class, + ]; $this->dispatcher->expects($this->exactly(2)) ->method('dispatchTyped') - ->withConsecutive( - [ - $this->callback(function (BeforeShareCreatedEvent $e) use ($path) { - $share = $e->getShare(); - - return $share->getShareType() === IShare::TYPE_EMAIL && - $share->getNode() === $path && - $share->getSharedBy() === 'sharedBy' && - $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && - $share->getExpirationDate() === null && - $share->getPassword() === null && - $share->getToken() === 'token'; - }) - ], - [ - $this->callback(function (ShareCreatedEvent $e) use ($path) { - $share = $e->getShare(); - - return $share->getShareType() === IShare::TYPE_EMAIL && - $share->getNode() === $path && - $share->getSharedBy() === 'sharedBy' && - $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && - $share->getExpirationDate() === null && - $share->getPassword() === null && - $share->getToken() === 'token' && - $share->getId() === '42' && - $share->getTarget() === '/target'; - }) - ], - ); + ->willReturnCallback(function ($event) use (&$calls, $path): void { + $expected = array_shift($calls); + $this->assertInstanceOf($expected, $event); + $share = $event->getShare(); + + $this->assertEquals(IShare::TYPE_EMAIL, $share->getShareType(), 'getShareType'); + $this->assertEquals($path, $share->getNode(), 'getNode'); + $this->assertEquals('sharedBy', $share->getSharedBy(), 'getSharedBy'); + $this->assertEquals(Constants::PERMISSION_ALL, $share->getPermissions(), 'getPermissions'); + $this->assertNull($share->getExpirationDate(), 'getExpirationDate'); + $this->assertNull($share->getPassword(), 'getPassword'); + $this->assertEquals('token', $share->getToken(), 'getToken'); + + if ($expected === ShareCreatedEvent::class) { + $this->assertEquals('42', $share->getId(), 'getId'); + $this->assertEquals('/target', $share->getTarget(), 'getTarget'); + } + }); /** @var IShare $share */ $share = $manager->createShare($share); @@ -2550,12 +2731,12 @@ class ManagerTest extends \Test\TestCase { } - public function testCreateShareHookError() { + public function testCreateShareHookError(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('I won\'t let you share'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'generalCreateChecks', 'userCreateChecks', @@ -2566,7 +2747,7 @@ class ManagerTest extends \Test\TestCase { $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $path = $this->createMock(File::class); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); @@ -2579,7 +2760,7 @@ class ManagerTest extends \Test\TestCase { 'sharedWith', 'sharedBy', null, - \OCP\Constants::PERMISSION_ALL); + Constants::PERMISSION_ALL); $manager->expects($this->once()) ->method('canShare') @@ -2609,7 +2790,7 @@ class ManagerTest extends \Test\TestCase { ->method('dispatchTyped') ->with( $this->isInstanceOf(BeforeShareCreatedEvent::class) - )->willReturnCallback(function (BeforeShareCreatedEvent $e) { + )->willReturnCallback(function (BeforeShareCreatedEvent $e): void { $e->setError('I won\'t let you share!'); $e->stopPropagation(); } @@ -2618,20 +2799,20 @@ class ManagerTest extends \Test\TestCase { $manager->createShare($share); } - public function testCreateShareOfIncomingFederatedShare() { + public function testCreateShareOfIncomingFederatedShare(): void { $manager = $this->createManagerMock() - ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks']) + ->onlyMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks']) ->getMock(); $shareOwner = $this->createMock(IUser::class); $shareOwner->method('getUID')->willReturn('shareOwner'); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') ->with('OCA\Files_Sharing\External\Storage') ->willReturn(true); - $storage2 = $this->createMock(Storage::class); + $storage2 = $this->createMock(IStorage::class); $storage2->method('instanceOfStorage') ->with('OCA\Files_Sharing\External\Storage') ->willReturn(false); @@ -2658,7 +2839,7 @@ class ManagerTest extends \Test\TestCase { 'sharedWith', 'sharedBy', null, - \OCP\Constants::PERMISSION_ALL); + Constants::PERMISSION_ALL); $manager->expects($this->once()) ->method('canShare') @@ -2692,7 +2873,7 @@ class ManagerTest extends \Test\TestCase { $manager->createShare($share); } - public function testGetSharesBy() { + public function testGetSharesBy(): void { $share = $this->manager->newShare(); $node = $this->createMock(Folder::class); @@ -2714,6 +2895,31 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($share, $shares[0]); } + public function testGetSharesByOwnerless(): void { + $mount = $this->createMock(IShareOwnerlessMount::class); + + $node = $this->createMock(Folder::class); + $node + ->expects($this->once()) + ->method('getMountPoint') + ->willReturn($mount); + + $share = $this->manager->newShare(); + $share->setNode($node); + $share->setShareType(IShare::TYPE_USER); + + $this->defaultProvider + ->expects($this->once()) + ->method('getSharesByPath') + ->with($this->equalTo($node)) + ->willReturn([$share]); + + $shares = $this->manager->getSharesBy('user', IShare::TYPE_USER, $node, true, 1, 1); + + $this->assertCount(1, $shares); + $this->assertSame($share, $shares[0]); + } + /** * Test to ensure we correctly remove expired link shares * @@ -2722,12 +2928,12 @@ class ManagerTest extends \Test\TestCase { * have received share 1,2 and 7. And from the manager. Share 3-6 should be * deleted (as they are evaluated). but share 8 should still be there. */ - public function testGetSharesByExpiredLinkShares() { + public function testGetSharesByExpiredLinkShares(): void { $manager = $this->createManagerMock() - ->setMethods(['deleteShare']) + ->onlyMethods(['deleteShare']) ->getMock(); - /** @var \OCP\Share\IShare[] $shares */ + /** @var IShare[] $shares */ $shares = []; /* @@ -2750,7 +2956,7 @@ class ManagerTest extends \Test\TestCase { $shares[4]->setExpirationDate($today); $shares[5]->setExpirationDate($today); - /** @var \OCP\Share\IShare[] $i */ + /** @var IShare[] $i */ $shares2 = []; for ($i = 0; $i < 8; $i++) { $shares2[] = clone $shares[$i]; @@ -2771,7 +2977,7 @@ class ManagerTest extends \Test\TestCase { * Simulate the deleteShare call. */ $manager->method('deleteShare') - ->willReturnCallback(function ($share) use (&$shares2) { + ->willReturnCallback(function ($share) use (&$shares2): void { for ($i = 0; $i < count($shares2); $i++) { if ($shares2[$i]->getId() === $share->getId()) { array_splice($shares2, $i, 1); @@ -2795,7 +3001,7 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($today, $shares[3]->getExpirationDate()); } - public function testGetShareByToken() { + public function testGetShareByToken(): void { $this->config ->expects($this->exactly(2)) ->method('getAppValue') @@ -2824,7 +3030,7 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($share, $ret); } - public function testGetShareByTokenRoom() { + public function testGetShareByTokenRoom(): void { $this->config ->expects($this->exactly(2)) ->method('getAppValue') @@ -2845,7 +3051,7 @@ class ManagerTest extends \Test\TestCase { ->method('getProviderForType') ->willReturnCallback(function ($shareType) use ($roomShareProvider) { if ($shareType !== IShare::TYPE_ROOM) { - throw new Exception\ProviderException(); + throw new ProviderException(); } return $roomShareProvider; @@ -2860,7 +3066,7 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($share, $ret); } - public function testGetShareByTokenWithException() { + public function testGetShareByTokenWithException(): void { $this->config ->expects($this->exactly(2)) ->method('getAppValue') @@ -2875,13 +3081,17 @@ class ManagerTest extends \Test\TestCase { $share = $this->createMock(IShare::class); + $calls = [ + [IShare::TYPE_LINK], + [IShare::TYPE_REMOTE], + ]; $factory->expects($this->exactly(2)) ->method('getProviderForType') - ->withConsecutive( - [IShare::TYPE_LINK], - [IShare::TYPE_REMOTE] - ) - ->willReturn($this->defaultProvider); + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + return $this->defaultProvider; + }); $this->defaultProvider->expects($this->exactly(2)) ->method('getShareByToken') @@ -2896,8 +3106,8 @@ class ManagerTest extends \Test\TestCase { } - public function testGetShareByTokenHideDisabledUser() { - $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class); + public function testGetShareByTokenHideDisabledUser(): void { + $this->expectException(ShareNotFound::class); $this->expectExceptionMessage('The requested share comes from a disabled user'); $this->config @@ -2913,7 +3123,7 @@ class ManagerTest extends \Test\TestCase { ->willReturnArgument(0); $manager = $this->createManagerMock() - ->setMethods(['deleteShare']) + ->onlyMethods(['deleteShare']) ->getMock(); $date = new \DateTime(); @@ -2951,8 +3161,8 @@ class ManagerTest extends \Test\TestCase { } - public function testGetShareByTokenExpired() { - $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class); + public function testGetShareByTokenExpired(): void { + $this->expectException(ShareNotFound::class); $this->expectExceptionMessage('The requested share does not exist anymore'); $this->config @@ -2966,7 +3176,7 @@ class ManagerTest extends \Test\TestCase { ->willReturnArgument(0); $manager = $this->createManagerMock() - ->setMethods(['deleteShare']) + ->onlyMethods(['deleteShare']) ->getMock(); $date = new \DateTime(); @@ -2986,7 +3196,7 @@ class ManagerTest extends \Test\TestCase { $manager->getShareByToken('expiredToken'); } - public function testGetShareByTokenNotExpired() { + public function testGetShareByTokenNotExpired(): void { $this->config ->expects($this->exactly(2)) ->method('getAppValue') @@ -3012,8 +3222,8 @@ class ManagerTest extends \Test\TestCase { } - public function testGetShareByTokenWithPublicLinksDisabled() { - $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class); + public function testGetShareByTokenWithPublicLinksDisabled(): void { + $this->expectException(ShareNotFound::class); $this->config ->expects($this->once()) @@ -3023,7 +3233,7 @@ class ManagerTest extends \Test\TestCase { $this->manager->getShareByToken('validToken'); } - public function testGetShareByTokenPublicUploadDisabled() { + public function testGetShareByTokenPublicUploadDisabled(): void { $this->config ->expects($this->exactly(3)) ->method('getAppValue') @@ -3035,7 +3245,7 @@ class ManagerTest extends \Test\TestCase { $share = $this->manager->newShare(); $share->setShareType(IShare::TYPE_LINK) - ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE); + ->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE); $share->setSharedWith('sharedWith'); $folder = $this->createMock(\OC\Files\Node\Folder::class); $share->setNode($folder); @@ -3047,16 +3257,16 @@ class ManagerTest extends \Test\TestCase { $res = $this->manager->getShareByToken('validToken'); - $this->assertSame(\OCP\Constants::PERMISSION_READ, $res->getPermissions()); + $this->assertSame(Constants::PERMISSION_READ, $res->getPermissions()); } - public function testCheckPasswordNoLinkShare() { + public function testCheckPasswordNoLinkShare(): void { $share = $this->createMock(IShare::class); $share->method('getShareType')->willReturn(IShare::TYPE_USER); $this->assertFalse($this->manager->checkPassword($share, 'password')); } - public function testCheckPasswordNoPassword() { + public function testCheckPasswordNoPassword(): void { $share = $this->createMock(IShare::class); $share->method('getShareType')->willReturn(IShare::TYPE_LINK); $this->assertFalse($this->manager->checkPassword($share, 'password')); @@ -3065,7 +3275,7 @@ class ManagerTest extends \Test\TestCase { $this->assertFalse($this->manager->checkPassword($share, null)); } - public function testCheckPasswordInvalidPassword() { + public function testCheckPasswordInvalidPassword(): void { $share = $this->createMock(IShare::class); $share->method('getShareType')->willReturn(IShare::TYPE_LINK); $share->method('getPassword')->willReturn('password'); @@ -3075,7 +3285,7 @@ class ManagerTest extends \Test\TestCase { $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword')); } - public function testCheckPasswordValidPassword() { + public function testCheckPasswordValidPassword(): void { $share = $this->createMock(IShare::class); $share->method('getShareType')->willReturn(IShare::TYPE_LINK); $share->method('getPassword')->willReturn('passwordHash'); @@ -3085,7 +3295,7 @@ class ManagerTest extends \Test\TestCase { $this->assertTrue($this->manager->checkPassword($share, 'password')); } - public function testCheckPasswordUpdateShare() { + public function testCheckPasswordUpdateShare(): void { $share = $this->manager->newShare(); $share->setShareType(IShare::TYPE_LINK) ->setPassword('passwordHash'); @@ -3099,7 +3309,7 @@ class ManagerTest extends \Test\TestCase { $this->defaultProvider->expects($this->once()) ->method('update') - ->with($this->callback(function (\OCP\Share\IShare $share) { + ->with($this->callback(function (IShare $share) { return $share->getPassword() === 'newHash'; })); @@ -3107,12 +3317,12 @@ class ManagerTest extends \Test\TestCase { } - public function testUpdateShareCantChangeShareType() { + public function testUpdateShareCantChangeShareType(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Cannot change share type'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById' ]) @@ -3135,12 +3345,12 @@ class ManagerTest extends \Test\TestCase { } - public function testUpdateShareCantChangeRecipientForGroupShare() { + public function testUpdateShareCantChangeRecipientForGroupShare(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Can only update recipient on user shares'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById' ]) @@ -3163,12 +3373,12 @@ class ManagerTest extends \Test\TestCase { } - public function testUpdateShareCantShareWithOwner() { + public function testUpdateShareCantShareWithOwner(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('Cannot share with the share owner'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById' ]) @@ -3191,11 +3401,11 @@ class ManagerTest extends \Test\TestCase { $manager->updateShare($share); } - public function testUpdateShareUser() { + public function testUpdateShareUser(): void { $this->userManager->expects($this->any())->method('userExists')->willReturn(true); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3234,15 +3444,15 @@ class ManagerTest extends \Test\TestCase { ->with($share) ->willReturn($share); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); $this->rootFolder->method('getUserFolder')->with('newUser')->willReturnSelf(); $this->rootFolder->method('getRelativePath')->with('/newUser/files/myPath')->willReturn('/myPath'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener2, 'post'); $hookListener2->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3257,9 +3467,9 @@ class ManagerTest extends \Test\TestCase { $manager->updateShare($share); } - public function testUpdateShareGroup() { + public function testUpdateShareGroup(): void { $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3292,20 +3502,20 @@ class ManagerTest extends \Test\TestCase { ->with($share) ->willReturn($share); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener2, 'post'); $hookListener2->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareLink() { + public function testUpdateShareLink(): void { $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3354,8 +3564,8 @@ class ManagerTest extends \Test\TestCase { ->with($share) ->willReturn($share); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3363,8 +3573,8 @@ class ManagerTest extends \Test\TestCase { 'uidOwner' => 'owner', ]); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3373,20 +3583,20 @@ class ManagerTest extends \Test\TestCase { 'disabled' => false, ]); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareLinkEnableSendPasswordByTalkWithNoPassword() { + public function testUpdateShareLinkEnableSendPasswordByTalkWithNoPassword(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3435,24 +3645,24 @@ class ManagerTest extends \Test\TestCase { $this->defaultProvider->expects($this->never()) ->method('update'); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->never())->method('post'); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMail() { + public function testUpdateShareMail(): void { $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3465,7 +3675,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $tomorrow = new \DateTime(); $tomorrow->setTime(0, 0, 0); @@ -3484,7 +3694,7 @@ class ManagerTest extends \Test\TestCase { ->setPassword('password') ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -3504,8 +3714,8 @@ class ManagerTest extends \Test\TestCase { ->with($share, 'password') ->willReturn($share); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3513,8 +3723,8 @@ class ManagerTest extends \Test\TestCase { 'uidOwner' => 'owner', ]); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3523,16 +3733,16 @@ class ManagerTest extends \Test\TestCase { 'disabled' => false, ]); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMailEnableSendPasswordByTalk() { + public function testUpdateShareMailEnableSendPasswordByTalk(): void { $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3545,7 +3755,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setPassword(null) ->setSendPasswordByTalk(false); @@ -3567,7 +3777,7 @@ class ManagerTest extends \Test\TestCase { ->setSendPasswordByTalk(true) ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -3587,8 +3797,8 @@ class ManagerTest extends \Test\TestCase { ->with($share, 'password') ->willReturn($share); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3596,8 +3806,8 @@ class ManagerTest extends \Test\TestCase { 'uidOwner' => 'owner', ]); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3606,16 +3816,16 @@ class ManagerTest extends \Test\TestCase { 'disabled' => false, ]); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMailEnableSendPasswordByTalkWithDifferentPassword() { + public function testUpdateShareMailEnableSendPasswordByTalkWithDifferentPassword(): void { $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3628,7 +3838,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setPassword('anotherPasswordHash') ->setSendPasswordByTalk(false); @@ -3650,7 +3860,7 @@ class ManagerTest extends \Test\TestCase { ->setSendPasswordByTalk(true) ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -3675,8 +3885,8 @@ class ManagerTest extends \Test\TestCase { ->with($share, 'password') ->willReturn($share); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3684,8 +3894,8 @@ class ManagerTest extends \Test\TestCase { 'uidOwner' => 'owner', ]); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->once())->method('post')->with([ 'itemType' => 'file', 'itemSource' => 100, @@ -3694,19 +3904,19 @@ class ManagerTest extends \Test\TestCase { 'disabled' => false, ]); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMailEnableSendPasswordByTalkWithNoPassword() { + public function testUpdateShareMailEnableSendPasswordByTalkWithNoPassword(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3719,7 +3929,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setPassword(null) ->setSendPasswordByTalk(false); @@ -3741,7 +3951,7 @@ class ManagerTest extends \Test\TestCase { ->setSendPasswordByTalk(true) ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -3758,28 +3968,28 @@ class ManagerTest extends \Test\TestCase { $this->defaultProvider->expects($this->never()) ->method('update'); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->never())->method('post'); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMailEnableSendPasswordByTalkRemovingPassword() { + public function testUpdateShareMailEnableSendPasswordByTalkRemovingPassword(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3792,7 +4002,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setPassword('passwordHash') ->setSendPasswordByTalk(false); @@ -3814,7 +4024,7 @@ class ManagerTest extends \Test\TestCase { ->setSendPasswordByTalk(true) ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -3831,28 +4041,28 @@ class ManagerTest extends \Test\TestCase { $this->defaultProvider->expects($this->never()) ->method('update'); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->never())->method('post'); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMailEnableSendPasswordByTalkRemovingPasswordWithEmptyString() { + public function testUpdateShareMailEnableSendPasswordByTalkRemovingPasswordWithEmptyString(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3865,7 +4075,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setPassword('passwordHash') ->setSendPasswordByTalk(false); @@ -3887,7 +4097,7 @@ class ManagerTest extends \Test\TestCase { ->setSendPasswordByTalk(true) ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -3904,28 +4114,28 @@ class ManagerTest extends \Test\TestCase { $this->defaultProvider->expects($this->never()) ->method('update'); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->never())->method('post'); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMailEnableSendPasswordByTalkWithPreviousPassword() { + public function testUpdateShareMailEnableSendPasswordByTalkWithPreviousPassword(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot enable sending the password by Talk without setting a new password'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -3938,7 +4148,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setPassword('password') ->setSendPasswordByTalk(false); @@ -3960,7 +4170,7 @@ class ManagerTest extends \Test\TestCase { ->setSendPasswordByTalk(true) ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -3979,27 +4189,27 @@ class ManagerTest extends \Test\TestCase { $this->defaultProvider->expects($this->never()) ->method('update'); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->never())->method('post'); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMailDisableSendPasswordByTalkWithPreviousPassword() { + public function testUpdateShareMailDisableSendPasswordByTalkWithPreviousPassword(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot disable sending the password by Talk without setting a new password'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -4012,7 +4222,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setPassword('passwordHash') ->setSendPasswordByTalk(true); @@ -4034,7 +4244,7 @@ class ManagerTest extends \Test\TestCase { ->setSendPasswordByTalk(false) ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -4053,27 +4263,27 @@ class ManagerTest extends \Test\TestCase { $this->defaultProvider->expects($this->never()) ->method('update'); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->never())->method('post'); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testUpdateShareMailDisableSendPasswordByTalkWithoutChangingPassword() { + public function testUpdateShareMailDisableSendPasswordByTalkWithoutChangingPassword(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot disable sending the password by Talk without setting a new password'); $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'canShare', 'getShareById', 'generalCreateChecks', @@ -4086,7 +4296,7 @@ class ManagerTest extends \Test\TestCase { $originalShare = $this->manager->newShare(); $originalShare->setShareType(IShare::TYPE_EMAIL) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setPermissions(Constants::PERMISSION_ALL) ->setPassword('passwordHash') ->setSendPasswordByTalk(true); @@ -4108,7 +4318,7 @@ class ManagerTest extends \Test\TestCase { ->setSendPasswordByTalk(false) ->setExpirationDate($tomorrow) ->setNode($file) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $manager->expects($this->once())->method('canShare')->willReturn(true); $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare); @@ -4127,22 +4337,22 @@ class ManagerTest extends \Test\TestCase { $this->defaultProvider->expects($this->never()) ->method('update'); - $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); + $hookListener = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post'); $hookListener->expects($this->never())->method('post'); - $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); + $hookListener2 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post'); $hookListener2->expects($this->never())->method('post'); - $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); + $hookListener3 = $this->createMock(DummyShareManagerListener::class); + Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post'); $hookListener3->expects($this->never())->method('post'); $manager->updateShare($share); } - public function testMoveShareLink() { + public function testMoveShareLink(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Cannot change target of link share'); @@ -4155,9 +4365,9 @@ class ManagerTest extends \Test\TestCase { } - public function testMoveShareUserNotRecipient() { + public function testMoveShareUserNotRecipient(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Invalid recipient'); + $this->expectExceptionMessage('Invalid share recipient'); $share = $this->manager->newShare(); $share->setShareType(IShare::TYPE_USER); @@ -4167,7 +4377,7 @@ class ManagerTest extends \Test\TestCase { $this->manager->moveShare($share, 'recipient'); } - public function testMoveShareUser() { + public function testMoveShareUser(): void { $share = $this->manager->newShare(); $share->setShareType(IShare::TYPE_USER) ->setId('42') @@ -4182,9 +4392,9 @@ class ManagerTest extends \Test\TestCase { } - public function testMoveShareGroupNotRecipient() { + public function testMoveShareGroupNotRecipient(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Invalid recipient'); + $this->expectExceptionMessage('Invalid share recipient'); $share = $this->manager->newShare(); $share->setShareType(IShare::TYPE_GROUP); @@ -4202,7 +4412,7 @@ class ManagerTest extends \Test\TestCase { } - public function testMoveShareGroupNull() { + public function testMoveShareGroupNull(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Group "shareWith" does not exist'); @@ -4218,7 +4428,7 @@ class ManagerTest extends \Test\TestCase { $this->manager->moveShare($share, 'recipient'); } - public function testMoveShareGroup() { + public function testMoveShareGroup(): void { $share = $this->manager->newShare(); $share->setShareType(IShare::TYPE_GROUP) ->setId('42') @@ -4239,17 +4449,15 @@ class ManagerTest extends \Test\TestCase { $this->addToAssertionCount(1); } - /** - * @dataProvider dataTestShareProviderExists - */ - public function testShareProviderExists($shareType, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShareProviderExists')] + public function testShareProviderExists($shareType, $expected): void { $factory = $this->getMockBuilder('OCP\Share\IProviderFactory')->getMock(); $factory->expects($this->any())->method('getProviderForType') ->willReturnCallback(function ($id) { if ($id === IShare::TYPE_USER) { return true; } - throw new Exception\ProviderException(); + throw new ProviderException(); }); $manager = $this->createManager($factory); @@ -4258,14 +4466,14 @@ class ManagerTest extends \Test\TestCase { ); } - public function dataTestShareProviderExists() { + public static function dataTestShareProviderExists() { return [ [IShare::TYPE_USER, true], [42, false], ]; } - public function testGetSharesInFolder() { + public function testGetSharesInFolder(): void { $factory = new DummyFactory2($this->createMock(IServerContainer::class)); $manager = $this->createManager($factory); @@ -4312,7 +4520,42 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($expects, $result); } - public function testGetAccessList() { + public function testGetSharesInFolderOwnerless(): void { + $factory = new DummyFactory2($this->createMock(IServerContainer::class)); + + $manager = $this->createManager($factory); + + $factory->setProvider($this->defaultProvider); + $extraProvider = $this->createMock(IShareProviderSupportsAllSharesInFolder::class); + $factory->setSecondProvider($extraProvider); + + $share1 = $this->createMock(IShare::class); + $share2 = $this->createMock(IShare::class); + + $mount = $this->createMock(IShareOwnerlessMount::class); + + $folder = $this->createMock(Folder::class); + $folder + ->method('getMountPoint') + ->willReturn($mount); + + $this->defaultProvider + ->method('getAllSharesInFolder') + ->with($folder) + ->willReturn([1 => [$share1]]); + + $extraProvider + ->method('getAllSharesInFolder') + ->with($folder) + ->willReturn([1 => [$share2]]); + + $this->assertSame([ + 1 => [$share1, $share2], + ], $manager->getSharesInFolder('user', $folder)); + } + + + public function testGetAccessList(): void { $factory = new DummyFactory2($this->createMock(IServerContainer::class)); $manager = $this->createManager($factory); @@ -4411,7 +4654,7 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($expected['users'], $result['users']); } - public function testGetAccessListWithCurrentAccess() { + public function testGetAccessListWithCurrentAccess(): void { $factory = new DummyFactory2($this->createMock(IServerContainer::class)); $manager = $this->createManager($factory); @@ -4519,7 +4762,7 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($expected['users'], $result['users']); } - public function testGetAllShares() { + public function testGetAllShares(): void { $factory = new DummyFactory2($this->createMock(IServerContainer::class)); $manager = $this->createManager($factory); @@ -4554,7 +4797,7 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($expects, $result); } - public function dataCurrentUserCanEnumerateTargetUser(): array { + public static function dataCurrentUserCanEnumerateTargetUser(): array { return [ 'Full match guest' => [true, true, false, false, false, false, false, true], 'Full match user' => [false, true, false, false, false, false, false, true], @@ -4577,19 +4820,19 @@ class ManagerTest extends \Test\TestCase { } /** - * @dataProvider dataCurrentUserCanEnumerateTargetUser * @param bool $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataCurrentUserCanEnumerateTargetUser')] public function testCurrentUserCanEnumerateTargetUser(bool $currentUserIsGuest, bool $allowEnumerationFullMatch, bool $allowEnumeration, bool $limitEnumerationToPhone, bool $limitEnumerationToGroups, bool $isKnownToUser, bool $haveCommonGroup, bool $expected): void { /** @var IManager|MockObject $manager */ $manager = $this->createManagerMock() - ->setMethods([ + ->onlyMethods([ 'allowEnumerationFullMatch', 'allowEnumeration', 'limitEnumerationToPhone', 'limitEnumerationToGroups', ]) - ->getMock(); + ->getMock(); $manager->method('allowEnumerationFullMatch') ->willReturn($allowEnumerationFullMatch); @@ -4636,7 +4879,7 @@ class DummyFactory implements IProviderFactory { /** @var IShareProvider */ protected $provider; - public function __construct(\OCP\IServerContainer $serverContainer) { + public function __construct(IServerContainer $serverContainer) { } /** diff --git a/tests/lib/Share20/ShareByMailProviderTest.php b/tests/lib/Share20/ShareByMailProviderTest.php index 0cdd13fe17c..de4dc1a820c 100644 --- a/tests/lib/Share20/ShareByMailProviderTest.php +++ b/tests/lib/Share20/ShareByMailProviderTest.php @@ -3,24 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2022 Richard Steinmetz <richard@steinmetz.cloud> - * - * @author Richard Steinmetz <richard@steinmetz.cloud> - * - * @license AGPL-3.0-or-later - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Share20; @@ -40,6 +24,7 @@ use OCP\IUserManager; use OCP\Mail\IMailer; use OCP\Security\IHasher; use OCP\Security\ISecureRandom; +use OCP\Server; use OCP\Share\IShare; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; @@ -101,7 +86,7 @@ class ShareByMailProviderTest extends TestCase { private $settingsManager; protected function setUp(): void { - $this->dbConn = \OC::$server->getDatabaseConnection(); + $this->dbConn = Server::get(IDBConnection::class); $this->userManager = $this->createMock(IUserManager::class); $this->rootFolder = $this->createMock(IRootFolder::class); $this->mailer = $this->createMock(IMailer::class); @@ -141,7 +126,7 @@ class ShareByMailProviderTest extends TestCase { protected function tearDown(): void { $this->dbConn->getQueryBuilder()->delete('share')->execute(); - $this->dbConn->getQueryBuilder()->delete('filecache')->execute(); + $this->dbConn->getQueryBuilder()->delete('filecache')->runAcrossAllShards()->execute(); $this->dbConn->getQueryBuilder()->delete('storages')->execute(); } @@ -195,7 +180,7 @@ class ShareByMailProviderTest extends TestCase { $qb->setValue('token', $qb->expr()->literal($token)); } if ($expiration) { - $qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATE)); + $qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATETIME_MUTABLE)); } if ($parent) { $qb->setValue('parent', $qb->expr()->literal($parent)); @@ -205,7 +190,7 @@ class ShareByMailProviderTest extends TestCase { return $qb->getLastInsertId(); } - public function testGetSharesByWithResharesAndNoNode() { + public function testGetSharesByWithResharesAndNoNode(): void { $this->addShareToDB( IShare::TYPE_EMAIL, 'external.one@domain.tld', @@ -252,7 +237,7 @@ class ShareByMailProviderTest extends TestCase { $this->assertEquals('external.one@domain.tld', $actual[0]->getSharedWith()); } - public function testGetSharesByWithResharesAndNode() { + public function testGetSharesByWithResharesAndNode(): void { $this->addShareToDB( IShare::TYPE_EMAIL, 'external.one@domain.tld', diff --git a/tests/lib/Share20/ShareHelperTest.php b/tests/lib/Share20/ShareHelperTest.php index 857810e7881..3928843cf7d 100644 --- a/tests/lib/Share20/ShareHelperTest.php +++ b/tests/lib/Share20/ShareHelperTest.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Share20; @@ -44,7 +28,7 @@ class ShareHelperTest extends TestCase { $this->helper = new ShareHelper($this->manager); } - public function dataGetPathsForAccessList() { + public static function dataGetPathsForAccessList(): array { return [ [[], [], false, [], [], false, [ 'users' => [], @@ -65,10 +49,8 @@ class ShareHelperTest extends TestCase { ]; } - /** - * @dataProvider dataGetPathsForAccessList - */ - public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetPathsForAccessList')] + public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected): void { $this->manager->expects($this->once()) ->method('getAccessList') ->willReturn([ @@ -81,7 +63,7 @@ class ShareHelperTest extends TestCase { /** @var ShareHelper|\PHPUnit\Framework\MockObject\MockObject $helper */ $helper = $this->getMockBuilder(ShareHelper::class) ->setConstructorArgs([$this->manager]) - ->setMethods(['getPathsForUsers', 'getPathsForRemotes']) + ->onlyMethods(['getPathsForUsers', 'getPathsForRemotes']) ->getMock(); $helper->expects($resolveUsers ? $this->once() : $this->never()) @@ -97,7 +79,7 @@ class ShareHelperTest extends TestCase { $this->assertSame($expected, $helper->getPathsForAccessList($node)); } - public function dataGetPathsForUsers() { + public static function dataGetPathsForUsers(): array { return [ [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []], [ @@ -118,13 +100,13 @@ class ShareHelperTest extends TestCase { } /** - * @dataProvider dataGetPathsForUsers * * @param array $users * @param array $nodes * @param array $expected */ - public function testGetPathsForUsers(array $users, array $nodes, array $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetPathsForUsers')] + public function testGetPathsForUsers(array $users, array $nodes, array $expected): void { $lastNode = null; foreach ($nodes as $nodeId => $nodeName) { /** @var Node|\PHPUnit\Framework\MockObject\MockObject $node */ @@ -150,7 +132,7 @@ class ShareHelperTest extends TestCase { $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForUsers', [$lastNode, $users])); } - public function dataGetPathsForRemotes() { + public static function dataGetPathsForRemotes(): array { return [ [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []], [ @@ -175,13 +157,13 @@ class ShareHelperTest extends TestCase { } /** - * @dataProvider dataGetPathsForRemotes * * @param array $remotes * @param array $nodes * @param array $expected */ - public function testGetPathsForRemotes(array $remotes, array $nodes, array $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetPathsForRemotes')] + public function testGetPathsForRemotes(array $remotes, array $nodes, array $expected): void { $lastNode = null; foreach ($nodes as $nodeId => $nodePath) { /** @var Node|\PHPUnit\Framework\MockObject\MockObject $node */ @@ -207,7 +189,7 @@ class ShareHelperTest extends TestCase { $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForRemotes', [$lastNode, $remotes])); } - public function dataGetMountedPath() { + public static function dataGetMountedPath(): array { return [ ['/admin/files/foobar', '/foobar'], ['/admin/files/foo/bar', '/foo/bar'], @@ -215,11 +197,11 @@ class ShareHelperTest extends TestCase { } /** - * @dataProvider dataGetMountedPath * @param string $path * @param string $expected */ - public function testGetMountedPath($path, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetMountedPath')] + public function testGetMountedPath($path, $expected): void { /** @var Node|\PHPUnit\Framework\MockObject\MockObject $node */ $node = $this->createMock(Node::class); $node->expects($this->once()) diff --git a/tests/lib/Share20/ShareTest.php b/tests/lib/Share20/ShareTest.php index b2419e07683..f15fbb860db 100644 --- a/tests/lib/Share20/ShareTest.php +++ b/tests/lib/Share20/ShareTest.php @@ -1,28 +1,18 @@ <?php + /** - * @author Roeland Jago Douma <rullzer@owncloud.com> - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Share20; +use OC\Share20\Share; use OCP\Files\IRootFolder; use OCP\IUserManager; +use OCP\Share\Exceptions\IllegalIDChangeException; +use OCP\Share\IShare; use PHPUnit\Framework\MockObject\MockObject; /** @@ -35,37 +25,37 @@ class ShareTest extends \Test\TestCase { protected $rootFolder; /** @var IUserManager|MockObject */ protected $userManager; - /** @var \OCP\Share\IShare */ + /** @var IShare */ protected $share; protected function setUp(): void { $this->rootFolder = $this->createMock(IRootFolder::class); $this->userManager = $this->createMock(IUserManager::class); - $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager); + $this->share = new Share($this->rootFolder, $this->userManager); } - public function testSetIdInvalid() { + public function testSetIdInvalid(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('String expected.'); $this->share->setId(1.2); } - public function testSetIdInt() { + public function testSetIdInt(): void { $this->share->setId(42); $this->assertEquals('42', $this->share->getId()); } - public function testSetIdString() { + public function testSetIdString(): void { $this->share->setId('foo'); $this->assertEquals('foo', $this->share->getId()); } - public function testSetIdOnce() { - $this->expectException(\OCP\Share\Exceptions\IllegalIDChangeException::class); + public function testSetIdOnce(): void { + $this->expectException(IllegalIDChangeException::class); $this->expectExceptionMessage('Not allowed to assign a new internal id to a share'); $this->share->setId('foo'); @@ -73,7 +63,7 @@ class ShareTest extends \Test\TestCase { } - public function testSetProviderIdInt() { + public function testSetProviderIdInt(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('String expected.'); @@ -81,15 +71,15 @@ class ShareTest extends \Test\TestCase { } - public function testSetProviderIdString() { + public function testSetProviderIdString(): void { $this->share->setProviderId('foo'); $this->share->setId('bar'); $this->assertEquals('foo:bar', $this->share->getFullId()); } - public function testSetProviderIdOnce() { - $this->expectException(\OCP\Share\Exceptions\IllegalIDChangeException::class); + public function testSetProviderIdOnce(): void { + $this->expectException(IllegalIDChangeException::class); $this->expectExceptionMessage('Not allowed to assign a new provider id to a share'); $this->share->setProviderId('foo'); |