diff options
Diffstat (limited to 'apps/files_sharing/tests')
48 files changed, 8469 insertions, 6031 deletions
diff --git a/apps/files_sharing/tests/ApiTest.php b/apps/files_sharing/tests/ApiTest.php index 8e2b5942d14..960f29224bb 100644 --- a/apps/files_sharing/tests/ApiTest.php +++ b/apps/files_sharing/tests/ApiTest.php @@ -1,72 +1,73 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Jan-Christoph Borchardt <hey@jancborchardt.net> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; +use OC\Core\AppInfo\ConfigLexicon; use OC\Files\Cache\Scanner; +use OC\Files\FileInfo; +use OC\Files\Filesystem; +use OC\Files\Storage\Temporary; +use OC\Files\View; +use OCA\Federation\TrustedServers; use OCA\Files_Sharing\Controller\ShareAPIController; +use OCP\App\IAppManager; use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\Constants; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\IAppConfig; +use OCP\IConfig; +use OCP\IDateTimeZone; +use OCP\IGroupManager; use OCP\IL10N; +use OCP\IPreview; use OCP\IRequest; +use OCP\ITagManager; +use OCP\IURLGenerator; +use OCP\IUserManager; +use OCP\Mail\IMailer; +use OCP\Server; +use OCP\Share\IProviderFactory; +use OCP\Share\IShare; +use OCP\UserStatus\IManager as IUserStatusManager; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; /** * Class ApiTest * * @group DB - * TODO: convert to real intergration tests + * TODO: convert to real integration tests */ class ApiTest extends TestCase { - - const TEST_FOLDER_NAME = '/folder_share_api_test'; - const APP_NAME = 'files_sharing'; + public const TEST_FOLDER_NAME = '/folder_share_api_test'; + public const APP_NAME = 'files_sharing'; private static $tempStorage; - /** @var \OCP\Files\Folder */ - private $userFolder; + private Folder $userFolder; + private string $subsubfolder; + protected IAppConfig&MockObject $appConfig; - /** @var string */ - private $subsubfolder; - - protected function setUp() { + protected function setUp(): void { parent::setUp(); - \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no'); - \OC::$server->getConfig()->setAppValue('core', 'shareapi_expire_after_n_days', '7'); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_exclude_groups', 'no'); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_expire_after_n_days', '7'); + + Filesystem::getLoader()->removeStorageWrapper('sharing_mask'); $this->folder = self::TEST_FOLDER_NAME; - $this->subfolder = '/subfolder_share_api_test'; + $this->subfolder = '/subfolder_share_api_test'; $this->subsubfolder = '/subsubfolder_share_api_test'; $this->filename = '/share-api-test.txt'; @@ -76,16 +77,18 @@ class ApiTest extends TestCase { $this->view->mkdir($this->folder); $this->view->mkdir($this->folder . $this->subfolder); $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder); - $this->view->file_put_contents($this->folder.$this->filename, $this->data); + $this->view->file_put_contents($this->folder . $this->filename, $this->data); $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data); $mount = $this->view->getMount($this->filename); $mount->getStorage()->getScanner()->scan('', Scanner::SCAN_RECURSIVE); $this->userFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); + + $this->appConfig = $this->createMock(IAppConfig::class); } - protected function tearDown() { - if($this->view instanceof \OC\Files\View) { + protected function tearDown(): void { + if ($this->view instanceof View) { $this->view->unlink($this->filename); $this->view->deleteAll($this->folder); } @@ -97,39 +100,63 @@ class ApiTest extends TestCase { /** * @param string $userId The userId of the caller - * @return \OCA\Files_Sharing\Controller\ShareAPIController + * @return ShareAPIController */ private function createOCS($userId) { $l = $this->getMockBuilder(IL10N::class)->getMock(); $l->method('t') - ->will($this->returnCallback(function($text, $parameters = []) { + ->willReturnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); - })); + }); + $config = $this->createMock(IConfig::class); + $appManager = $this->createMock(IAppManager::class); + $serverContainer = $this->createMock(ContainerInterface::class); + $userStatusManager = $this->createMock(IUserStatusManager::class); + $previewManager = $this->createMock(IPreview::class); + $dateTimeZone = $this->createMock(IDateTimeZone::class); + $logger = $this->createMock(LoggerInterface::class); + $providerFactory = $this->createMock(IProviderFactory::class); + $mailer = $this->createMock(IMailer::class); + $tagManager = $this->createMock(ITagManager::class); + $trustedServers = $this->createMock(TrustedServers::class); + $dateTimeZone->method('getTimeZone')->willReturn(new \DateTimeZone(date_default_timezone_get())); return new ShareAPIController( self::APP_NAME, $this->getMockBuilder(IRequest::class)->getMock(), $this->shareManager, - \OC::$server->getGroupManager(), - \OC::$server->getUserManager(), - \OC::$server->getRootFolder(), - \OC::$server->getURLGenerator(), + Server::get(IGroupManager::class), + Server::get(IUserManager::class), + Server::get(IRootFolder::class), + Server::get(IURLGenerator::class), + $l, + $config, + $this->appConfig, + $appManager, + $serverContainer, + $userStatusManager, + $previewManager, + $dateTimeZone, + $logger, + $providerFactory, + $mailer, + $tagManager, + $trustedServers, $userId, - $l ); } - function testCreateShareUserFile() { + public function testCreateShareUserFile(): void { $this->setUp(); // for some reasons phpunit refuses to do this for us only for this test $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2); + $result = $ocs->createShare($this->filename, Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2); $ocs->cleanup(); $data = $result->getData(); $this->assertEquals(19, $data['permissions']); $this->assertEmpty($data['expiration']); - $this->shareManager->getShareById('ocinternal:'.$data['id']); + $this->shareManager->getShareById('ocinternal:' . $data['id']); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); $ocs->deleteShare($data['id']); @@ -137,132 +164,139 @@ class ApiTest extends TestCase { $ocs->cleanup(); } - function testCreateShareUserFolder() { + public function testCreateShareUserFolder(): void { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2); $ocs->cleanup(); $data = $result->getData(); $this->assertEquals(31, $data['permissions']); $this->assertEmpty($data['expiration']); - $this->shareManager->getShareById('ocinternal:'.$data['id']); + $this->shareManager->getShareById('ocinternal:' . $data['id']); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); $ocs->deleteShare($data['id']); $ocs->cleanup(); - } - function testCreateShareGroupFile() { + public function testCreateShareGroupFile(): void { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1); + $result = $ocs->createShare($this->filename, Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1); $ocs->cleanup(); $data = $result->getData(); $this->assertEquals(19, $data['permissions']); $this->assertEmpty($data['expiration']); - $this->shareManager->getShareById('ocinternal:'.$data['id']); + $this->shareManager->getShareById('ocinternal:' . $data['id']); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); $ocs->deleteShare($data['id']); $ocs->cleanup(); } - function testCreateShareGroupFolder() { + public function testCreateShareGroupFolder(): void { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1); $ocs->cleanup(); $data = $result->getData(); $this->assertEquals(31, $data['permissions']); $this->assertEmpty($data['expiration']); - $this->shareManager->getShareById('ocinternal:'.$data['id']); + $this->shareManager->getShareById('ocinternal:' . $data['id']); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); $ocs->deleteShare($data['id']); $ocs->cleanup(); - } - public function testCreateShareLink() { + /** + * @group RoutingWeirdness + */ + public function testCreateShareLink(): void { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK); $ocs->cleanup(); $data = $result->getData(); - $this->assertEquals(1, $data['permissions']); + $this->assertEquals(Constants::PERMISSION_ALL, + $data['permissions']); $this->assertEmpty($data['expiration']); $this->assertTrue(is_string($data['token'])); // check for correct link - $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']); + $url = Server::get(IURLGenerator::class)->getAbsoluteURL('/index.php/s/' . $data['token']); $this->assertEquals($url, $data['url']); - $this->shareManager->getShareById('ocinternal:'.$data['id']); + $this->shareManager->getShareById('ocinternal:' . $data['id']); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); $ocs->deleteShare($data['id']); $ocs->cleanup(); } - public function testCreateShareLinkPublicUpload() { + /** + * @group RoutingWeirdness + * @dataProvider dataAllowFederationOnPublicShares + */ + public function testCreateShareLinkPublicUpload(array $appConfig, int $permissions): void { + $this->appConfig->method('getValueBool') + ->willReturnMap([$appConfig]); + $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true'); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true'); $ocs->cleanup(); $data = $result->getData(); $this->assertEquals( - \OCP\Constants::PERMISSION_READ | - \OCP\Constants::PERMISSION_CREATE | - \OCP\Constants::PERMISSION_UPDATE | - \OCP\Constants::PERMISSION_DELETE, + Constants::PERMISSION_READ + | Constants::PERMISSION_CREATE + | Constants::PERMISSION_UPDATE + | Constants::PERMISSION_DELETE + | $permissions, $data['permissions'] ); $this->assertEmpty($data['expiration']); $this->assertTrue(is_string($data['token'])); // check for correct link - $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']); + $url = Server::get(IURLGenerator::class)->getAbsoluteURL('/index.php/s/' . $data['token']); $this->assertEquals($url, $data['url']); - $this->shareManager->getShareById('ocinternal:'.$data['id']); + $this->shareManager->getShareById('ocinternal:' . $data['id']); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); $ocs->deleteShare($data['id']); $ocs->cleanup(); } - function testEnfoceLinkPassword() { - + public function testEnforceLinkPassword(): void { $password = md5(time()); - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); $config->setAppValue('core', 'shareapi_enforce_links_password', 'yes'); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); try { - $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK); + $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK); $this->fail(); } catch (OCSForbiddenException $e) { - } $ocs->cleanup(); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); try { - $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', ''); + $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', ''); $this->fail(); } catch (OCSForbiddenException $e) { - } $ocs->cleanup(); // share with password should succeed $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', $password); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', $password); $ocs->cleanup(); $data = $result->getData(); @@ -278,7 +312,6 @@ class ApiTest extends TestCase { $ocs->updateShare($data['id']); $this->fail(); } catch (OCSBadRequestException $e) { - } $ocs->cleanup(); @@ -288,34 +321,35 @@ class ApiTest extends TestCase { $ocs->cleanup(); $config->setAppValue('core', 'shareapi_enforce_links_password', 'no'); + $this->addToAssertionCount(1); } /** * @medium - */ - function testSharePermissions() { + */ + public function testSharePermissions(): void { // sharing file to a user should work if shareapi_exclude_groups is set // to no - \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no'); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_exclude_groups', 'no'); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2); + $result = $ocs->createShare($this->filename, Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2); $ocs->cleanup(); $data = $result->getData(); - $this->shareManager->getShareById('ocinternal:'.$data['id']); + $this->shareManager->getShareById('ocinternal:' . $data['id']); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); $ocs->deleteShare($data['id']); $ocs->cleanup(); // exclude groups, but not the group the user belongs to. Sharing should still work - \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'yes'); - \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2'); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_exclude_groups', 'yes'); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2'); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2); + $result = $ocs->createShare($this->filename, Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2); $ocs->cleanup(); $data = $result->getData(); @@ -327,29 +361,31 @@ class ApiTest extends TestCase { $ocs->cleanup(); // now we exclude the group the user belongs to ('group'), sharing should fail now - \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group'); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_exclude_groups_list', 'admin,group'); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2); + $ocs->createShare($this->filename, Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2); $ocs->cleanup(); // cleanup - \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups', 'no'); - \OC::$server->getConfig()->setAppValue('core', 'shareapi_exclude_groups_list', ''); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_exclude_groups', 'no'); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_exclude_groups_list', ''); + + $this->addToAssertionCount(1); } /** * @medium */ - function testGetAllShares() { + public function testGetAllShares(): void { $node = $this->userFolder->get($this->filename); $share = $this->shareManager->newShare(); $share->setNode($node) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share = $this->shareManager->createShare($share); @@ -363,24 +399,31 @@ class ApiTest extends TestCase { $this->shareManager->deleteShare($share); } - function testGetAllSharesWithMe() { + public function testGetAllSharesWithMe(): void { + $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); + $this->logout(); + $node1 = $this->userFolder->get($this->filename); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share1 = $this->shareManager->createShare($share1); + $share1->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share1); $node2 = $this->userFolder->get($this->folder); $share2 = $this->shareManager->newShare(); $share2->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share2 = $this->shareManager->createShare($share2); + $share2->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share2); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); $result = $ocs->getShares('true'); @@ -394,10 +437,11 @@ class ApiTest extends TestCase { /** * @medium + * @group RoutingWeirdness */ - function testPublicLinkUrl() { + public function testPublicLinkUrl(): void { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK); $ocs->cleanup(); $data = $result->getData(); @@ -407,7 +451,7 @@ class ApiTest extends TestCase { $id = $data['id']; // check for correct link - $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']); + $url = Server::get(IURLGenerator::class)->getAbsoluteURL('/index.php/s/' . $data['token']); $this->assertEquals($url, $data['url']); // check for link in getall shares @@ -444,20 +488,20 @@ class ApiTest extends TestCase { * @depends testCreateShareUserFile * @depends testCreateShareLink */ - function testGetShareFromSource() { + public function testGetShareFromSource(): void { $node = $this->userFolder->get($this->filename); $share = $this->shareManager->newShare(); $share->setNode($node) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share1 = $this->shareManager->createShare($share); $share = $this->shareManager->newShare(); $share->setNode($node) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share2 = $this->shareManager->createShare($share); @@ -477,13 +521,13 @@ class ApiTest extends TestCase { * @depends testCreateShareUserFile * @depends testCreateShareLink */ - function testGetShareFromSourceWithReshares() { + public function testGetShareFromSourceWithReshares(): void { $node = $this->userFolder->get($this->filename); $share1 = $this->shareManager->newShare(); $share1->setNode($node) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share1 = $this->shareManager->createShare($share1); @@ -491,7 +535,7 @@ class ApiTest extends TestCase { $share2->setNode($node) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) ->setSharedWith(self::TEST_FILES_SHARING_API_USER3) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share2 = $this->shareManager->createShare($share2); @@ -518,13 +562,13 @@ class ApiTest extends TestCase { * @medium * @depends testCreateShareUserFile */ - function testGetShareFromId() { + public function testGetShareFromId(): void { $node = $this->userFolder->get($this->filename); $share1 = $this->shareManager->newShare(); $share1->setNode($node) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share1 = $this->shareManager->createShare($share1); @@ -542,21 +586,21 @@ class ApiTest extends TestCase { /** * @medium */ - function testGetShareFromFolder() { + public function testGetShareFromFolder(): void { $node1 = $this->userFolder->get($this->filename); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share1 = $this->shareManager->createShare($share1); - $node2 = $this->userFolder->get($this->folder.'/'.$this->filename); + $node2 = $this->userFolder->get($this->folder . '/' . $this->filename); $share2 = $this->shareManager->newShare(); $share2->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share2 = $this->shareManager->createShare($share2); @@ -572,13 +616,13 @@ class ApiTest extends TestCase { $this->shareManager->deleteShare($share2); } - function testGetShareFromFolderWithFile() { + public function testGetShareFromFolderWithFile(): void { $node1 = $this->userFolder->get($this->filename); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share1 = $this->shareManager->createShare($share1); @@ -598,40 +642,46 @@ class ApiTest extends TestCase { * share a folder, than reshare a file within the shared folder and check if we construct the correct path * @medium */ - function testGetShareFromFolderReshares() { + public function testGetShareFromFolderReshares(): void { $node1 = $this->userFolder->get($this->folder); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share1 = $this->shareManager->createShare($share1); + $share1->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share1); - $node2 = $this->userFolder->get($this->folder.'/'.$this->filename); + $node2 = $this->userFolder->get($this->folder . '/' . $this->filename); $share2 = $this->shareManager->newShare(); $share2->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share2 = $this->shareManager->createShare($share2); + $share2->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share2); - $node3 = $this->userFolder->get($this->folder.'/'.$this->subfolder.'/'.$this->filename); + $node3 = $this->userFolder->get($this->folder . '/' . $this->subfolder . '/' . $this->filename); $share3 = $this->shareManager->newShare(); $share3->setNode($node3) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share3 = $this->shareManager->createShare($share3); + $share3->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share3); + + $testValues = [ + ['query' => $this->folder, + 'expectedResult' => $this->folder . $this->filename], + ['query' => $this->folder . $this->subfolder, + 'expectedResult' => $this->folder . $this->subfolder . $this->filename], + ]; - $testValues=array( - array('query' => $this->folder, - 'expectedResult' => $this->folder . $this->filename), - array('query' => $this->folder . $this->subfolder, - 'expectedResult' => $this->folder . $this->subfolder . $this->filename), - ); foreach ($testValues as $value) { - $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); $result = $ocs->getShares('false', 'false', 'true', $value['query']); $ocs->cleanup(); @@ -652,23 +702,27 @@ class ApiTest extends TestCase { * reshare a sub folder and check if we get the correct path * @medium */ - function testGetShareFromSubFolderReShares() { + public function testGetShareFromSubFolderReShares(): void { $node1 = $this->userFolder->get($this->folder . $this->subfolder); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share1 = $this->shareManager->createShare($share1); + $share1->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share1); - $node2 = \OC::$server->getRootFolder()->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->get($this->subfolder); + $node2 = Server::get(IRootFolder::class)->getUserFolder(self::TEST_FILES_SHARING_API_USER2)->get($this->subfolder); $share2 = $this->shareManager->newShare(); $share2->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share2 = $this->shareManager->createShare($share2); + $share2->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share2); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); $result = $ocs->getShares(); @@ -690,13 +744,13 @@ class ApiTest extends TestCase { * test re-re-share of folder if the path gets constructed correctly * @medium */ - function XtestGetShareFromFolderReReShares() { + public function XtestGetShareFromFolderReReShares() { $node1 = $this->userFolder->get($this->folder . $this->subfolder); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share1 = $this->shareManager->createShare($share1); @@ -705,14 +759,14 @@ class ApiTest extends TestCase { $share2->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) ->setSharedWith(self::TEST_FILES_SHARING_API_USER3) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share2 = $this->shareManager->createShare($share2); $share3 = $this->shareManager->newShare(); $share3->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER3) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share3 = $this->shareManager->createShare($share3); @@ -767,36 +821,42 @@ class ApiTest extends TestCase { * test multiple shared folder if the path gets constructed correctly * @medium */ - function testGetShareMultipleSharedFolder() { + public function testGetShareMultipleSharedFolder(): void { $this->setUp(); $node1 = $this->userFolder->get($this->folder . $this->subfolder); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share1 = $this->shareManager->createShare($share1); + $share1->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share1); $node2 = $this->userFolder->get($this->folder); $share2 = $this->shareManager->newShare(); $share2->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share2 = $this->shareManager->createShare($share2); + $share2->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share2); $share3 = $this->shareManager->newShare(); $share3->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share3 = $this->shareManager->createShare($share3); + $share3->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share3); // $request = $this->createRequest(['path' => $this->subfolder]); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); - $result1 = $ocs->getShares('false','false','false', $this->subfolder); + $result1 = $ocs->getShares('false', 'false', 'false', $this->subfolder); $ocs->cleanup(); // test should return one share within $this->folder @@ -815,7 +875,7 @@ class ApiTest extends TestCase { $s2 = reset($data2); $this->assertEquals($this->subfolder, $s1['path']); - $this->assertEquals($this->folder.$this->subfolder, $s2['path']); + $this->assertEquals($this->folder . $this->subfolder, $s2['path']); $this->shareManager->deleteShare($share1); $this->shareManager->deleteShare($share2); @@ -826,15 +886,17 @@ class ApiTest extends TestCase { * test re-re-share of folder if the path gets constructed correctly * @medium */ - function testGetShareFromFileReReShares() { + public function testGetShareFromFileReReShares(): void { $node1 = $this->userFolder->get($this->folder . $this->subfolder); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share1 = $this->shareManager->createShare($share1); + $share1->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share1); $user2Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2); $node2 = $user2Folder->get($this->subfolder . $this->filename); @@ -842,18 +904,22 @@ class ApiTest extends TestCase { $share2->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) ->setSharedWith(self::TEST_FILES_SHARING_API_USER3) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share2 = $this->shareManager->createShare($share2); + $share2->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share2); $user3Folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER3); $node3 = $user3Folder->get($this->filename); $share3 = $this->shareManager->newShare(); $share3->setNode($node3) ->setSharedBy(self::TEST_FILES_SHARING_API_USER3) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share3 = $this->shareManager->createShare($share3); + $share3->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share3); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3); $result = $ocs->getShares(); @@ -875,13 +941,13 @@ class ApiTest extends TestCase { /** * @medium */ - function testGetShareFromUnknownId() { + public function testGetShareFromUnknownId(): void { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER3); try { $ocs->getShare(0); $this->fail(); } catch (OCSNotFoundException $e) { - $this->assertEquals('Wrong share ID, share doesn\'t exist', $e->getMessage()); + $this->assertEquals('Wrong share ID, share does not exist', $e->getMessage()); } $ocs->cleanup(); } @@ -891,8 +957,7 @@ class ApiTest extends TestCase { * @depends testCreateShareUserFile * @depends testCreateShareLink */ - function testUpdateShare() { - + public function testUpdateShare(): void { $password = md5(time()); $node1 = $this->userFolder->get($this->filename); @@ -900,24 +965,33 @@ class ApiTest extends TestCase { $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) - ->setPermissions(19); + ->setShareType(IShare::TYPE_USER) + ->setPermissions(19) + ->setAttributes($this->shareManager->newShare()->newAttributes()); + + $this->assertNotNull($share1->getAttributes()); $share1 = $this->shareManager->createShare($share1); + $this->assertEquals(19, $share1->getPermissions()); $share2 = $this->shareManager->newShare(); $share2->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share2 = $this->shareManager->createShare($share2); + $this->assertEquals(1, $share2->getPermissions()); // update permissions $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $ocs->updateShare($share1->getId(), 1); + $ocs->updateShare( + $share1->getId(), 1, null, null, null, null, null, null, null, + '[{"scope": "app1", "key": "attr1", "value": true}]' + ); $ocs->cleanup(); $share1 = $this->shareManager->getShareById('ocinternal:' . $share1->getId()); $this->assertEquals(1, $share1->getPermissions()); + $this->assertEquals(true, $share1->getAttributes()->getAttribute('app1', 'attr1')); // update password for link share $this->assertNull($share2->getPassword()); @@ -942,27 +1016,33 @@ class ApiTest extends TestCase { /** * @medium + * @dataProvider dataAllowFederationOnPublicShares */ - function testUpdateShareUpload() { + public function testUpdateShareUpload(array $appConfig, int $permissions): void { + $this->appConfig->method('getValueBool')->willReturnMap([ + $appConfig, + ]); + $node1 = $this->userFolder->get($this->folder); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share1 = $this->shareManager->createShare($share1); // update public upload $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $ocs->updateShare($share1->getId(), null, null, 'true'); + $ocs->updateShare($share1->getId(), null, null, null, 'true'); $ocs->cleanup(); $share1 = $this->shareManager->getShareById($share1->getFullId()); $this->assertEquals( - \OCP\Constants::PERMISSION_READ | - \OCP\Constants::PERMISSION_CREATE | - \OCP\Constants::PERMISSION_UPDATE | - \OCP\Constants::PERMISSION_DELETE, + Constants::PERMISSION_READ + | Constants::PERMISSION_CREATE + | Constants::PERMISSION_UPDATE + | Constants::PERMISSION_DELETE + | $permissions, $share1->getPermissions() ); @@ -970,39 +1050,49 @@ class ApiTest extends TestCase { $this->shareManager->deleteShare($share1); } + public static function dataAllowFederationOnPublicShares(): array { + return [ + [['core', ConfigLexicon::SHAREAPI_ALLOW_FEDERATION_ON_PUBLIC_SHARES, false, false], 0], + [['core', ConfigLexicon::SHAREAPI_ALLOW_FEDERATION_ON_PUBLIC_SHARES, false, true], Constants::PERMISSION_SHARE], + ]; + } + /** * @medium */ - function testUpdateShareExpireDate() { + public function testUpdateShareExpireDate(): void { $node1 = $this->userFolder->get($this->folder); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share1 = $this->shareManager->createShare($share1); + $share1->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share1); - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); // enforce expire date, by default 7 days after the file was shared $config->setAppValue('core', 'shareapi_default_expire_date', 'yes'); $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes'); $dateWithinRange = new \DateTime(); - $dateWithinRange->setTime(0,0,0); - $dateWithinRange->add(new \DateInterval('P5D')); + $dateWithinRange->add(new \DateInterval('P6D')); + $dateOutOfRange = new \DateTime(); - $dateOutOfRange->setTime(0,0,0); $dateOutOfRange->add(new \DateInterval('P8D')); // update expire date to a valid value $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $ocs->updateShare($share1->getId(), null, null, null, $dateWithinRange->format('Y-m-d')); + $ocs->updateShare($share1->getId(), null, null, null, null, $dateWithinRange->format('Y-m-d')); $ocs->cleanup(); $share1 = $this->shareManager->getShareById($share1->getFullId()); // date should be changed + $dateWithinRange->setTime(0, 0, 0); + $dateWithinRange->setTimezone(new \DateTimeZone(date_default_timezone_get())); $this->assertEquals($dateWithinRange, $share1->getExpirationDate()); // update expire date to a value out of range @@ -1011,7 +1101,6 @@ class ApiTest extends TestCase { $ocs->updateShare($share1->getId()); $this->fail(); } catch (OCSBadRequestException $e) { - } $ocs->cleanup(); @@ -1026,7 +1115,6 @@ class ApiTest extends TestCase { $ocs->updateShare($share1->getId()); $this->fail(); } catch (OCSBadRequestException $e) { - } $ocs->cleanup(); @@ -1045,22 +1133,22 @@ class ApiTest extends TestCase { * @medium * @depends testCreateShareUserFile */ - function testDeleteShare() { + public function testDeleteShare(): void { $node1 = $this->userFolder->get($this->filename); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(19); $share1 = $this->shareManager->createShare($share1); $share2 = $this->shareManager->newShare(); $share2->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); - $share2 = $this->shareManager->createShare($share1); + $share2 = $this->shareManager->createShare($share2); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); $ocs->deleteShare($share1->getId()); @@ -1070,31 +1158,35 @@ class ApiTest extends TestCase { $ocs->deleteShare($share2->getId()); $ocs->cleanup(); - $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_USER)); - $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_LINK)); + $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_USER)); + $this->assertEmpty($this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_LINK)); } /** * test unshare of a reshared file */ - function testDeleteReshare() { + public function testDeleteReshare(): void { $node1 = $this->userFolder->get($this->folder); $share1 = $this->shareManager->newShare(); $share1->setNode($node1) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setPermissions(31); $share1 = $this->shareManager->createShare($share1); + $share1->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share1); $user2folder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER2); - $node2 = $user2folder->get($this->folder.'/'.$this->filename); + $node2 = $user2folder->get($this->folder . '/' . $this->filename); $share2 = $this->shareManager->newShare(); $share2->setNode($node2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPermissions(1); $share2 = $this->shareManager->createShare($share2); + $share2->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share2); // test if we can unshare the link again $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); @@ -1102,46 +1194,49 @@ class ApiTest extends TestCase { $ocs->cleanup(); $this->shareManager->deleteShare($share1); + $this->addToAssertionCount(1); } /** * share a folder which contains a share mount point, should be forbidden */ - public function testShareFolderWithAMountPoint() { + public function testShareFolderWithAMountPoint(): void { // user 1 shares a folder with user2 self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); // user2 shares a file from the folder as link self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); - $view->mkdir("localDir"); + $view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $view->mkdir('localDir'); // move mount point to the folder "localDir" - $result = $view->rename($this->folder, 'localDir/'.$this->folder); + $result = $view->rename($this->folder, 'localDir/' . $this->folder); $this->assertTrue($result !== false); // try to share "localDir" $fileInfo2 = $view->getFileInfo('localDir'); - $this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo); + $this->assertTrue($fileInfo2 instanceof FileInfo); $pass = true; try { $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, 'localDir', self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER3, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); } catch (\Exception $e) { $pass = false; @@ -1165,15 +1260,15 @@ class ApiTest extends TestCase { */ public static function initTestMountPointsHook($data) { if ($data['user'] === self::TEST_FILES_SHARING_API_USER1) { - \OC\Files\Filesystem::mount(self::$tempStorage, array(), '/' . self::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME); + Filesystem::mount(self::$tempStorage, [], '/' . self::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME); } } /** * Tests mounting a folder that is an external storage mount point. */ - public function testShareStorageMountPoint() { - $tempStorage = new \OC\Files\Storage\Temporary(array()); + public function testShareStorageMountPoint(): void { + $tempStorage = new Temporary([]); $tempStorage->file_put_contents('test.txt', 'abcdef'); $tempStorage->getScanner()->scan(''); @@ -1184,17 +1279,19 @@ class ApiTest extends TestCase { // user 1 shares the mount point folder with user2 $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); // user2: check that mount point name appears correctly self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($view->file_exists($this->folder)); $this->assertTrue($view->file_exists($this->folder . '/test.txt')); @@ -1206,54 +1303,35 @@ class ApiTest extends TestCase { \OC_Hook::clear('OC_Filesystem', 'post_initMountPoints'); \OC_Hook::clear('\OCA\Files_Sharing\Tests\ApiTest', 'initTestMountPointsHook'); } - /** - * @expectedException \Exception - */ - public function XtestShareNonExisting() { - self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - - $id = PHP_INT_MAX - 1; - \OC\Share\Share::shareItem('file', $id, \OCP\Share::SHARE_TYPE_LINK, self::TEST_FILES_SHARING_API_USER2, 31); - } - /** - * @expectedException \Exception - */ - public function testShareNotOwner() { - self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - \OC\Files\Filesystem::file_put_contents('foo.txt', 'bar'); - $info = \OC\Files\Filesystem::getFileInfo('foo.txt'); - - self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - - \OC\Share\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, self::TEST_FILES_SHARING_API_USER2, 31); - } - - public function datesProvider() { + public static function datesProvider() { $date = new \DateTime(); + $date->setTime(0, 0); $date->add(new \DateInterval('P5D')); + $date->setTimezone(new \DateTimeZone(date_default_timezone_get())); return [ - [$date->format('Y-m-d'), true], + [$date->format('Y-m-d H:i:s'), true], ['abc', false], - [$date->format('Y-m-d') . 'xyz', false], + [$date->format('Y-m-d H:i:s') . 'xyz', false], ]; } /** * Make sure only ISO 8601 dates are accepted * - * @dataProvider datesProvider + * @group RoutingWeirdness */ - public function testPublicLinkExpireDate($date, $valid) { + #[\PHPUnit\Framework\Attributes\DataProvider('datesProvider')] + public function testPublicLinkExpireDate($date, $valid): void { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); try { - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', $date); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date); $this->assertTrue($valid); } catch (OCSNotFoundException $e) { $this->assertFalse($valid); - $this->assertEquals('Invalid date, date format must be YYYY-MM-DD', $e->getMessage()); + $this->assertEquals('Invalid date. Format must be YYYY-MM-DD', $e->getMessage()); $ocs->cleanup(); return; } @@ -1261,21 +1339,24 @@ class ApiTest extends TestCase { $data = $result->getData(); $this->assertTrue(is_string($data['token'])); - $this->assertEquals($date, substr($data['expiration'], 0, 10)); + $this->assertEquals(substr($date, 0, 10), substr($data['expiration'], 0, 10)); // check for correct link - $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']); + $url = Server::get(IURLGenerator::class)->getAbsoluteURL('/index.php/s/' . $data['token']); $this->assertEquals($url, $data['url']); - $share = $this->shareManager->getShareById('ocinternal:'.$data['id']); + $share = $this->shareManager->getShareById('ocinternal:' . $data['id']); - $this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d')); + $this->assertEquals($date, $share->getExpirationDate()->format('Y-m-d H:i:s')); $this->shareManager->deleteShare($share); } - public function testCreatePublicLinkExpireDateValid() { - $config = \OC::$server->getConfig(); + /** + * @group RoutingWeirdness + */ + public function testCreatePublicLinkExpireDateValid(): void { + $config = Server::get(IConfig::class); // enforce expire date, by default 7 days after the file was shared $config->setAppValue('core', 'shareapi_default_expire_date', 'yes'); @@ -1285,19 +1366,19 @@ class ApiTest extends TestCase { $date->add(new \DateInterval('P5D')); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', $date->format('Y-m-d')); + $result = $ocs->createShare($this->filename, Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d')); $ocs->cleanup(); $data = $result->getData(); $this->assertTrue(is_string($data['token'])); - $this->assertEquals($date->format('Y-m-d') . ' 00:00:00', $data['expiration']); + $this->assertEquals($date->format('Y-m-d 00:00:00'), $data['expiration']); // check for correct link - $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']); + $url = Server::get(IURLGenerator::class)->getAbsoluteURL('/index.php/s/' . $data['token']); $this->assertEquals($url, $data['url']); - $share = $this->shareManager->getShareById('ocinternal:'.$data['id']); - $date->setTime(0,0,0); + $share = $this->shareManager->getShareById('ocinternal:' . $data['id']); + $date->setTime(0, 0, 0); $this->assertEquals($date, $share->getExpirationDate()); $this->shareManager->deleteShare($share); @@ -1306,8 +1387,8 @@ class ApiTest extends TestCase { $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no'); } - public function testCreatePublicLinkExpireDateInvalidFuture() { - $config = \OC::$server->getConfig(); + public function testCreatePublicLinkExpireDateInvalidFuture(): void { + $config = Server::get(IConfig::class); // enforce expire date, by default 7 days after the file was shared $config->setAppValue('core', 'shareapi_default_expire_date', 'yes'); @@ -1319,11 +1400,11 @@ class ApiTest extends TestCase { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); try { - $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', $date->format('Y-m-d')); + $ocs->createShare($this->filename, Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d')); $this->fail(); } catch (OCSException $e) { $this->assertEquals(404, $e->getCode()); - $this->assertEquals('Can’t set expiration date more than 7 days in the future', $e->getMessage()); + $this->assertEquals('Cannot set expiration date more than 7 days in the future', $e->getMessage()); } $ocs->cleanup(); @@ -1332,7 +1413,7 @@ class ApiTest extends TestCase { } public function XtestCreatePublicLinkExpireDateInvalidPast() { - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); $date = new \DateTime(); $date->sub(new \DateInterval('P8D')); @@ -1340,9 +1421,9 @@ class ApiTest extends TestCase { $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); try { - $ocs->createShare($this->filename, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', $date->format('Y-m-d')); + $ocs->createShare($this->filename, Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, $date->format('Y-m-d')); $this->fail(); - } catch(OCSException $e) { + } catch (OCSException $e) { $this->assertEquals(404, $e->getCode()); $this->assertEquals('Expiration date is in the past', $e->getMessage()); } @@ -1356,17 +1437,21 @@ class ApiTest extends TestCase { * test for no invisible shares * See: https://github.com/owncloud/core/issues/22295 */ - public function testInvisibleSharesUser() { + public function testInvisibleSharesUser(): void { // simulate a post request $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_USER, self::TEST_FILES_SHARING_API_USER2); $ocs->cleanup(); $data = $result->getData(); $topId = $data['id']; $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); - $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK); + $ocs->acceptShare($topId); + $ocs->cleanup(); + + $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); + $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK); $ocs->cleanup(); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); @@ -1384,17 +1469,23 @@ class ApiTest extends TestCase { * test for no invisible shares * See: https://github.com/owncloud/core/issues/22295 */ - public function testInvisibleSharesGroup() { + public function testInvisibleSharesGroup(): void { // simulate a post request $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); - $result = $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1); + $result = $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1); $ocs->cleanup(); $data = $result->getData(); $topId = $data['id']; $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); - $ocs->createShare($this->folder, \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK); + $ocs->acceptShare($topId); + $ocs->cleanup(); + + \OC_Util::tearDownFS(); + + $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2); + $ocs->createShare($this->folder, Constants::PERMISSION_ALL, IShare::TYPE_LINK); $ocs->cleanup(); $ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1); diff --git a/apps/files_sharing/tests/ApplicationTest.php b/apps/files_sharing/tests/ApplicationTest.php new file mode 100644 index 00000000000..84a3f4b372b --- /dev/null +++ b/apps/files_sharing/tests/ApplicationTest.php @@ -0,0 +1,206 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\Files_Sharing\Tests; + +use OCA\Files_Sharing\AppInfo\Application; +use OCA\Files_Sharing\Listener\BeforeDirectFileDownloadListener; +use OCA\Files_Sharing\Listener\BeforeZipCreatedListener; +use OCA\Files_Sharing\SharedStorage; +use OCP\Files\Events\BeforeDirectFileDownloadEvent; +use OCP\Files\Events\BeforeZipCreatedEvent; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\Storage\IStorage; +use OCP\IUser; +use OCP\IUserSession; +use OCP\Share\IAttributes; +use OCP\Share\IShare; +use Test\TestCase; + +class ApplicationTest extends TestCase { + private Application $application; + + /** @var IUserSession */ + private $userSession; + + /** @var IRootFolder */ + private $rootFolder; + + + protected function setUp(): void { + parent::setUp(); + + $this->application = new Application([]); + + $this->userSession = $this->createMock(IUserSession::class); + $this->rootFolder = $this->createMock(IRootFolder::class); + } + + public function providesDataForCanGet(): array { + // normal file (sender) - can download directly + $senderFileStorage = $this->createMock(IStorage::class); + $senderFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false); + $senderFile = $this->createMock(File::class); + $senderFile->method('getStorage')->willReturn($senderFileStorage); + $senderUserFolder = $this->createMock(Folder::class); + $senderUserFolder->method('get')->willReturn($senderFile); + + $result[] = [ '/bar.txt', $senderUserFolder, true ]; + + // shared file (receiver) with attribute secure-view-enabled set false - + // can download directly + $receiverFileShareAttributes = $this->createMock(IAttributes::class); + $receiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(true); + $receiverFileShare = $this->createMock(IShare::class); + $receiverFileShare->method('getAttributes')->willReturn($receiverFileShareAttributes); + $receiverFileStorage = $this->createMock(SharedStorage::class); + $receiverFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); + $receiverFileStorage->method('getShare')->willReturn($receiverFileShare); + $receiverFile = $this->createMock(File::class); + $receiverFile->method('getStorage')->willReturn($receiverFileStorage); + $receiverUserFolder = $this->createMock(Folder::class); + $receiverUserFolder->method('get')->willReturn($receiverFile); + + $result[] = [ '/share-bar.txt', $receiverUserFolder, true ]; + + // shared file (receiver) with attribute secure-view-enabled set true - + // cannot download directly + $secureReceiverFileShareAttributes = $this->createMock(IAttributes::class); + $secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false); + $secureReceiverFileShare = $this->createMock(IShare::class); + $secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes); + $secureReceiverFileStorage = $this->createMock(SharedStorage::class); + $secureReceiverFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); + $secureReceiverFileStorage->method('getShare')->willReturn($secureReceiverFileShare); + $secureReceiverFile = $this->createMock(File::class); + $secureReceiverFile->method('getStorage')->willReturn($secureReceiverFileStorage); + $secureReceiverUserFolder = $this->createMock(Folder::class); + $secureReceiverUserFolder->method('get')->willReturn($secureReceiverFile); + + $result[] = [ '/secure-share-bar.txt', $secureReceiverUserFolder, false ]; + + return $result; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('providesDataForCanGet')] + public function testCheckDirectCanBeDownloaded(string $path, Folder $userFolder, bool $run): void { + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn('test'); + $this->userSession->method('getUser')->willReturn($user); + $this->userSession->method('isLoggedIn')->willReturn(true); + $this->rootFolder->method('getUserFolder')->willReturn($userFolder); + + // Simulate direct download of file + $event = new BeforeDirectFileDownloadEvent($path); + $listener = new BeforeDirectFileDownloadListener( + $this->userSession, + $this->rootFolder + ); + $listener->handle($event); + + $this->assertEquals($run, $event->isSuccessful()); + } + + public function providesDataForCanZip(): array { + // Mock: Normal file/folder storage + $nonSharedStorage = $this->createMock(IStorage::class); + $nonSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false); + + // Mock: Secure-view file/folder shared storage + $secureReceiverFileShareAttributes = $this->createMock(IAttributes::class); + $secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false); + $secureReceiverFileShare = $this->createMock(IShare::class); + $secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes); + $secureSharedStorage = $this->createMock(SharedStorage::class); + $secureSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true); + $secureSharedStorage->method('getShare')->willReturn($secureReceiverFileShare); + + // 1. can download zipped 2 non-shared files inside non-shared folder + // 2. can download zipped non-shared folder + $sender1File = $this->createMock(File::class); + $sender1File->method('getStorage')->willReturn($nonSharedStorage); + $sender1Folder = $this->createMock(Folder::class); + $sender1Folder->method('getStorage')->willReturn($nonSharedStorage); + $sender1Folder->method('getDirectoryListing')->willReturn([$sender1File, $sender1File]); + $sender1RootFolder = $this->createMock(Folder::class); + $sender1RootFolder->method('getStorage')->willReturn($nonSharedStorage); + $sender1RootFolder->method('getDirectoryListing')->willReturn([$sender1Folder]); + $sender1UserFolder = $this->createMock(Folder::class); + $sender1UserFolder->method('get')->willReturn($sender1RootFolder); + + $return[] = [ '/folder', ['bar1.txt', 'bar2.txt'], $sender1UserFolder, true ]; + $return[] = [ '/', ['folder'], $sender1UserFolder, true ]; + + // 3. cannot download zipped 1 non-shared file and 1 secure-shared inside non-shared folder + $receiver1File = $this->createMock(File::class); + $receiver1File->method('getStorage')->willReturn($nonSharedStorage); + $receiver1SecureFile = $this->createMock(File::class); + $receiver1SecureFile->method('getStorage')->willReturn($secureSharedStorage); + $receiver1Folder = $this->createMock(Folder::class); + $receiver1Folder->method('getStorage')->willReturn($nonSharedStorage); + $receiver1Folder->method('getDirectoryListing')->willReturn([$receiver1File, $receiver1SecureFile]); + $receiver1RootFolder = $this->createMock(Folder::class); + $receiver1RootFolder->method('getStorage')->willReturn($nonSharedStorage); + $receiver1RootFolder->method('getDirectoryListing')->willReturn([$receiver1Folder]); + $receiver1UserFolder = $this->createMock(Folder::class); + $receiver1UserFolder->method('get')->willReturn($receiver1RootFolder); + + $return[] = [ '/folder', ['secured-bar1.txt', 'bar2.txt'], $receiver1UserFolder, false ]; + + // 4. cannot download zipped secure-shared folder + $receiver2Folder = $this->createMock(Folder::class); + $receiver2Folder->method('getStorage')->willReturn($secureSharedStorage); + $receiver2RootFolder = $this->createMock(Folder::class); + $receiver2RootFolder->method('getStorage')->willReturn($nonSharedStorage); + $receiver2RootFolder->method('getDirectoryListing')->willReturn([$receiver2Folder]); + $receiver2UserFolder = $this->createMock(Folder::class); + $receiver2UserFolder->method('get')->willReturn($receiver2RootFolder); + + $return[] = [ '/', ['secured-folder'], $receiver2UserFolder, false ]; + + return $return; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('providesDataForCanZip')] + public function testCheckZipCanBeDownloaded(string $dir, array $files, Folder $userFolder, bool $run): void { + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn('test'); + $this->userSession->method('getUser')->willReturn($user); + $this->userSession->method('isLoggedIn')->willReturn(true); + + $this->rootFolder->method('getUserFolder')->with('test')->willReturn($userFolder); + + // Simulate zip download of folder folder + $event = new BeforeZipCreatedEvent($dir, $files); + $listener = new BeforeZipCreatedListener( + $this->userSession, + $this->rootFolder + ); + $listener->handle($event); + + + $this->assertEquals($run, $event->isSuccessful()); + $this->assertEquals($run, $event->getErrorMessage() === null); + } + + public function testCheckFileUserNotFound(): void { + $this->userSession->method('isLoggedIn')->willReturn(false); + + // Simulate zip download of folder folder + $event = new BeforeZipCreatedEvent('/test', ['test.txt']); + $listener = new BeforeZipCreatedListener( + $this->userSession, + $this->rootFolder + ); + $listener->handle($event); + + // It should run as this would restrict e.g. share links otherwise + $this->assertTrue($event->isSuccessful()); + $this->assertEquals(null, $event->getErrorMessage()); + } +} diff --git a/apps/files_sharing/tests/BackendTest.php b/apps/files_sharing/tests/BackendTest.php deleted file mode 100644 index 897cf7c2de2..00000000000 --- a/apps/files_sharing/tests/BackendTest.php +++ /dev/null @@ -1,113 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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/> - * - */ - -namespace OCA\Files_Sharing\Tests; - - -/** - * Class BackendTest - * - * @group DB - */ -class BackendTest extends TestCase { - - const TEST_FOLDER_NAME = '/folder_share_api_test'; - - public $folder; - public $subfolder; - public $subsubfolder; - - protected function setUp() { - parent::setUp(); - - $this->folder = self::TEST_FOLDER_NAME; - $this->subfolder = '/subfolder_share_backend_test'; - $this->subsubfolder = '/subsubfolder_share_backend_test'; - - $this->filename = '/share-backend-test.txt'; - - // save file with content - $this->view->file_put_contents($this->filename, $this->data); - $this->view->mkdir($this->folder); - $this->view->mkdir($this->folder . $this->subfolder); - $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder); - $this->view->file_put_contents($this->folder.$this->filename, $this->data); - $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data); - $this->view->file_put_contents($this->folder . $this->subfolder . $this->subsubfolder . $this->filename, $this->data); - } - - protected function tearDown() { - if ($this->view) { - $this->view->unlink($this->filename); - $this->view->deleteAll($this->folder); - } - - parent::tearDown(); - } - - public function testGetParents() { - - $fileinfo1 = $this->view->getFileInfo($this->folder); - $fileinfo2 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder); - $fileinfo3 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder . $this->filename); - - $this->assertTrue(\OC\Share\Share::shareItem('folder', $fileinfo1['fileid'], \OCP\Share::SHARE_TYPE_USER, - self::TEST_FILES_SHARING_API_USER2, 31)); - $this->assertTrue(\OC\Share\Share::shareItem('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER, - self::TEST_FILES_SHARING_API_USER3, 31)); - - $backend = new \OCA\Files_Sharing\ShareBackend\Folder(); - - $result = $backend->getParents($fileinfo3['fileid']); - $this->assertSame(2, count($result)); - - $count1 = 0; - $count2 = 0; - foreach($result as $r) { - if ($r['path'] === 'files' . $this->folder) { - $this->assertSame(ltrim($this->folder, '/'), $r['collection']['path']); - $count1++; - } elseif ($r['path'] === 'files' . $this->folder . $this->subfolder . $this->subsubfolder) { - $this->assertSame(ltrim($this->subsubfolder, '/'), $r['collection']['path']); - $count2++; - } else { - $this->assertTrue(false, 'unexpected result'); - } - } - - $this->assertSame(1, $count1); - $this->assertSame(1, $count2); - - $result1 = $backend->getParents($fileinfo3['fileid'], self::TEST_FILES_SHARING_API_USER3); - $this->assertSame(1, count($result1)); - $elemet = reset($result1); - $this->assertSame('files' . $this->folder . $this->subfolder . $this->subsubfolder ,$elemet['path']); - $this->assertSame(ltrim($this->subsubfolder, '/') ,$elemet['collection']['path']); - - } - -} diff --git a/apps/files_sharing/tests/CacheTest.php b/apps/files_sharing/tests/CacheTest.php index c891f56f3d3..e95d3d4f91a 100644 --- a/apps/files_sharing/tests/CacheTest.php +++ b/apps/files_sharing/tests/CacheTest.php @@ -1,38 +1,24 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Stefan Weil <sw@weilnetz.de> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; +use OC\Files\Cache\Cache; +use OC\Files\Filesystem; +use OC\Files\Storage\Storage; use OC\Files\Storage\Temporary; use OC\Files\Storage\Wrapper\Jail; +use OC\Files\View; use OCA\Files_Sharing\SharedStorage; +use OCP\Constants; +use OCP\Files\Cache\IWatcher; +use OCP\IUserManager; +use OCP\Server; +use OCP\Share\IShare; /** * Class CacheTest @@ -42,38 +28,38 @@ use OCA\Files_Sharing\SharedStorage; class CacheTest extends TestCase { /** - * @var \OC\Files\View + * @var View */ public $user2View; - /** @var \OC\Files\Cache\Cache */ + /** @var Cache */ protected $ownerCache; - /** @var \OC\Files\Cache\Cache */ + /** @var Cache */ protected $sharedCache; - /** @var \OC\Files\Storage\Storage */ + /** @var Storage */ protected $ownerStorage; - /** @var \OC\Files\Storage\Storage */ + /** @var Storage */ protected $sharedStorage; /** @var \OCP\Share\IManager */ protected $shareManager; - protected function setUp() { + protected function setUp(): void { parent::setUp(); - $this->shareManager = \OC::$server->getShareManager(); + $this->shareManager = Server::get(\OCP\Share\IManager::class); - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); $userManager->get(self::TEST_FILES_SHARING_API_USER1)->setDisplayName('User One'); $userManager->get(self::TEST_FILES_SHARING_API_USER2)->setDisplayName('User Two'); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - $this->user2View = new \OC\Files\View('/'. self::TEST_FILES_SHARING_API_USER2 . '/files'); + $this->user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); // prepare user1's dir structure $this->view->mkdir('container'); @@ -88,8 +74,9 @@ class CacheTest extends TestCase { $this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData); $this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData); $this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>'); + $this->view->file_put_contents('simplefile.txt', $textData); - list($this->ownerStorage,) = $this->view->resolvePath(''); + [$this->ownerStorage,] = $this->view->resolvePath(''); $this->ownerCache = $this->ownerStorage->getCache(); $this->ownerStorage->getScanner()->scan(''); @@ -99,38 +86,42 @@ class CacheTest extends TestCase { $node = $rootFolder->get('container/shareddir'); $share = $this->shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $this->shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); $node = $rootFolder->get('container/shared single file.txt'); $share = $this->shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE)); - $this->shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL & ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE)); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); // login as user2 self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // retrieve the shared storage - $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2); - list($this->sharedStorage,) = $secondView->resolvePath('files/shareddir'); + $secondView = new View('/' . self::TEST_FILES_SHARING_API_USER2); + [$this->sharedStorage,] = $secondView->resolvePath('files/shareddir'); $this->sharedCache = $this->sharedStorage->getCache(); } - protected function tearDown() { - if($this->sharedCache) { + protected function tearDown(): void { + if ($this->sharedCache) { $this->sharedCache->clear(); } self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER); + $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER); foreach ($shares as $share) { $this->shareManager->deleteShare($share); } @@ -142,297 +133,242 @@ class CacheTest extends TestCase { parent::tearDown(); } - function searchDataProvider() { - return array( - array('%another%', - array( - array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'), - array('name' => 'another.txt', 'path' => 'subdir/another.txt'), - ) - ), - array('%Another%', - array( - array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'), - array('name' => 'another.txt', 'path' => 'subdir/another.txt'), - ) - ), - array('%dir%', - array( - array('name' => 'emptydir', 'path' => 'emptydir'), - array('name' => 'subdir', 'path' => 'subdir'), - array('name' => 'shareddir', 'path' => ''), - ) - ), - array('%Dir%', - array( - array('name' => 'emptydir', 'path' => 'emptydir'), - array('name' => 'subdir', 'path' => 'subdir'), - array('name' => 'shareddir', 'path' => ''), - ) - ), - array('%txt%', - array( - array('name' => 'bar.txt', 'path' => 'bar.txt'), - array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'), - array('name' => 'another.txt', 'path' => 'subdir/another.txt'), - ) - ), - array('%Txt%', - array( - array('name' => 'bar.txt', 'path' => 'bar.txt'), - array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'), - array('name' => 'another.txt', 'path' => 'subdir/another.txt'), - ) - ), - array('%', - array( - array('name' => 'bar.txt', 'path' => 'bar.txt'), - array('name' => 'emptydir', 'path' => 'emptydir'), - array('name' => 'subdir', 'path' => 'subdir'), - array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'), - array('name' => 'another.txt', 'path' => 'subdir/another.txt'), - array('name' => 'not a text file.xml', 'path' => 'subdir/not a text file.xml'), - array('name' => 'shareddir', 'path' => ''), - ) - ), - array('%nonexistent%', - array( - ) - ), - ); + public function searchDataProvider() { + return [ + ['%another%', + [ + ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'], + ['name' => 'another.txt', 'path' => 'subdir/another.txt'], + ] + ], + ['%Another%', + [ + ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'], + ['name' => 'another.txt', 'path' => 'subdir/another.txt'], + ] + ], + ['%dir%', + [ + ['name' => 'emptydir', 'path' => 'emptydir'], + ['name' => 'subdir', 'path' => 'subdir'], + ['name' => 'shareddir', 'path' => ''], + ] + ], + ['%Dir%', + [ + ['name' => 'emptydir', 'path' => 'emptydir'], + ['name' => 'subdir', 'path' => 'subdir'], + ['name' => 'shareddir', 'path' => ''], + ] + ], + ['%txt%', + [ + ['name' => 'bar.txt', 'path' => 'bar.txt'], + ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'], + ['name' => 'another.txt', 'path' => 'subdir/another.txt'], + ] + ], + ['%Txt%', + [ + ['name' => 'bar.txt', 'path' => 'bar.txt'], + ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'], + ['name' => 'another.txt', 'path' => 'subdir/another.txt'], + ] + ], + ['%', + [ + ['name' => 'bar.txt', 'path' => 'bar.txt'], + ['name' => 'emptydir', 'path' => 'emptydir'], + ['name' => 'subdir', 'path' => 'subdir'], + ['name' => 'another too.txt', 'path' => 'subdir/another too.txt'], + ['name' => 'another.txt', 'path' => 'subdir/another.txt'], + ['name' => 'not a text file.xml', 'path' => 'subdir/not a text file.xml'], + ['name' => 'shareddir', 'path' => ''], + ] + ], + ['%nonexistent%', + [ + ] + ], + ]; } /** * we cannot use a dataProvider because that would cause the stray hook detection to remove the hooks * that were added in setUpBeforeClass. */ - function testSearch() { + public function testSearch(): void { foreach ($this->searchDataProvider() as $data) { - list($pattern, $expectedFiles) = $data; + [$pattern, $expectedFiles] = $data; $results = $this->sharedStorage->getCache()->search($pattern); $this->verifyFiles($expectedFiles, $results); } - } /** * Test searching by mime type */ - function testSearchByMime() { + public function testSearchByMime(): void { $results = $this->sharedStorage->getCache()->searchByMime('text'); - $check = array( - array( - 'name' => 'bar.txt', - 'path' => 'bar.txt' - ), - array( - 'name' => 'another too.txt', - 'path' => 'subdir/another too.txt' - ), - array( - 'name' => 'another.txt', - 'path' => 'subdir/another.txt' - ), - ); - $this->verifyFiles($check, $results); - } - - /** - * Test searching by tag - */ - function testSearchByTag() { - $userId = \OC::$server->getUserSession()->getUser()->getUId(); - $id1 = $this->sharedCache->get('bar.txt')['fileid']; - $id2 = $this->sharedCache->get('subdir/another too.txt')['fileid']; - $id3 = $this->sharedCache->get('subdir/not a text file.xml')['fileid']; - $id4 = $this->sharedCache->get('subdir/another.txt')['fileid']; - $tagManager = \OC::$server->getTagManager()->load('files', [], false, $userId); - $tagManager->tagAs($id1, 'tag1'); - $tagManager->tagAs($id1, 'tag2'); - $tagManager->tagAs($id2, 'tag1'); - $tagManager->tagAs($id3, 'tag1'); - $tagManager->tagAs($id4, 'tag2'); - $results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId); - $check = array( - array( - 'name' => 'bar.txt', - 'path' => 'bar.txt' - ), - array( - 'name' => 'another too.txt', - 'path' => 'subdir/another too.txt' - ), - array( - 'name' => 'not a text file.xml', - 'path' => 'subdir/not a text file.xml' - ), - ); + $check = [ + [ + 'name' => 'bar.txt', + 'path' => 'bar.txt' + ], + [ + 'name' => 'another too.txt', + 'path' => 'subdir/another too.txt' + ], + [ + 'name' => 'another.txt', + 'path' => 'subdir/another.txt' + ], + ]; $this->verifyFiles($check, $results); - $tagManager->delete(array('tag1', 'tag2')); } - /** - * Test searching by tag for multiple sections of the tree - */ - function testSearchByTagTree() { - $userId = \OC::$server->getUserSession()->getUser()->getUId(); - $this->sharedStorage->mkdir('subdir/emptydir'); - $this->sharedStorage->mkdir('subdir/emptydir2'); - $this->ownerStorage->getScanner()->scan(''); - $allIds = array( - $this->sharedCache->get('')['fileid'], - $this->sharedCache->get('bar.txt')['fileid'], - $this->sharedCache->get('subdir/another too.txt')['fileid'], - $this->sharedCache->get('subdir/not a text file.xml')['fileid'], - $this->sharedCache->get('subdir/another.txt')['fileid'], - $this->sharedCache->get('subdir/emptydir')['fileid'], - $this->sharedCache->get('subdir/emptydir2')['fileid'], - ); - $tagManager = \OC::$server->getTagManager()->load('files', [], false, $userId); - foreach ($allIds as $id) { - $tagManager->tagAs($id, 'tag1'); - } - $results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId); - $check = array( - array( - 'name' => 'shareddir', - 'path' => '' - ), - array( - 'name' => 'bar.txt', - 'path' => 'bar.txt' - ), - array( - 'name' => 'another.txt', - 'path' => 'subdir/another.txt' - ), - array( - 'name' => 'another too.txt', - 'path' => 'subdir/another too.txt' - ), - array( - 'name' => 'emptydir', - 'path' => 'subdir/emptydir' - ), - array( - 'name' => 'emptydir2', - 'path' => 'subdir/emptydir2' - ), - array( - 'name' => 'not a text file.xml', - 'path' => 'subdir/not a text file.xml' - ), - ); - $this->verifyFiles($check, $results); - $tagManager->delete(array('tag1')); - } - - function testGetFolderContentsInRoot() { + public function testGetFolderContentsInRoot(): void { $results = $this->user2View->getDirectoryContent('/'); + $results = (array_filter($results, function ($file) { + return $file->getName() !== 'welcome.txt'; + })); // we should get the shared items "shareddir" and "shared single file.txt" // additional root will always contain the example file "welcome.txt", // so this will be part of the result $this->verifyFiles( - array( - array( - 'name' => 'welcome.txt', - 'path' => 'files/welcome.txt', - 'mimetype' => 'text/plain', - ), - array( + [ + [ 'name' => 'shareddir', 'path' => 'files/shareddir', 'mimetype' => 'httpd/unix-directory', 'uid_owner' => self::TEST_FILES_SHARING_API_USER1, 'displayname_owner' => 'User One', - ), - array( + ], + [ 'name' => 'shared single file.txt', 'path' => 'files/shared single file.txt', 'mimetype' => 'text/plain', 'uid_owner' => self::TEST_FILES_SHARING_API_USER1, 'displayname_owner' => 'User One', - ), - ), + ], + ], $results ); } - function testGetFolderContentsInSubdir() { + public function testGetFolderContentsInSubdir(): void { $results = $this->user2View->getDirectoryContent('/shareddir'); $this->verifyFiles( - array( - array( + [ + [ 'name' => 'bar.txt', 'path' => 'bar.txt', 'mimetype' => 'text/plain', 'uid_owner' => self::TEST_FILES_SHARING_API_USER1, 'displayname_owner' => 'User One', - ), - array( + ], + [ 'name' => 'emptydir', 'path' => 'emptydir', 'mimetype' => 'httpd/unix-directory', 'uid_owner' => self::TEST_FILES_SHARING_API_USER1, 'displayname_owner' => 'User One', - ), - array( + ], + [ 'name' => 'subdir', 'path' => 'subdir', 'mimetype' => 'httpd/unix-directory', 'uid_owner' => self::TEST_FILES_SHARING_API_USER1, 'displayname_owner' => 'User One', - ), - ), + ], + ], $results ); } - function testGetFolderContentsWhenSubSubdirShared() { + /** + * This covers a bug where the share owners name was propagated + * to the recipient in the recent files API response where the + * share recipient has a different target set + * + * https://github.com/nextcloud/server/issues/39879 + */ + public function testShareRenameOriginalFileInRecentResults(): void { + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + + $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); + $node = $rootFolder->get('simplefile.txt'); + $share = $this->shareManager->newShare(); + $share->setNode($node) + ->setShareType(IShare::TYPE_USER) + ->setSharedWith(self::TEST_FILES_SHARING_API_USER3) + ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) + ->setPermissions(Constants::PERMISSION_READ); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + $node->move(self::TEST_FILES_SHARING_API_USER1 . '/files/simplefile2.txt'); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER3); + $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER3); + $recents = $rootFolder->getRecent(10); + self::assertEquals([ + 'welcome.txt', + 'simplefile.txt' + ], array_map(function ($node) { + return $node->getFileInfo()['name']; + }, $recents)); + } + + public function testGetFolderContentsWhenSubSubdirShared(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); $node = $rootFolder->get('container/shareddir/subdir'); $share = $this->shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER3) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); + ->setPermissions(Constants::PERMISSION_ALL); $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); - $thirdView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files'); + $thirdView = new View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files'); $results = $thirdView->getDirectoryContent('/subdir'); $this->verifyFiles( - array( - array( + [ + [ 'name' => 'another too.txt', 'path' => 'another too.txt', 'mimetype' => 'text/plain', 'uid_owner' => self::TEST_FILES_SHARING_API_USER1, 'displayname_owner' => 'User One', - ), - array( + ], + [ 'name' => 'another.txt', 'path' => 'another.txt', 'mimetype' => 'text/plain', 'uid_owner' => self::TEST_FILES_SHARING_API_USER1, 'displayname_owner' => 'User One', - ), - array( + ], + [ 'name' => 'not a text file.xml', 'path' => 'not a text file.xml', 'mimetype' => 'application/xml', 'uid_owner' => self::TEST_FILES_SHARING_API_USER1, 'displayname_owner' => 'User One', - ), - ), + ], + ], $results ); @@ -459,7 +395,7 @@ class CacheTest extends TestCase { } } } - $this->assertEquals(array(), $results); + $this->assertEquals([], $results); } /** @@ -473,91 +409,95 @@ class CacheTest extends TestCase { } } - public function testGetPathByIdDirectShare() { + public function testGetPathByIdDirectShare(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - \OC\Files\Filesystem::file_put_contents('test.txt', 'foo'); - $info = \OC\Files\Filesystem::getFileInfo('test.txt'); + Filesystem::file_put_contents('test.txt', 'foo'); + $info = Filesystem::getFileInfo('test.txt'); $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); $node = $rootFolder->get('test.txt'); $share = $this->shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE); - $this->shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); \OC_Util::tearDownFS(); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists('/test.txt')); - list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt'); + $this->assertTrue(Filesystem::file_exists('/test.txt')); + [$sharedStorage] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt'); /** - * @var \OCA\Files_Sharing\SharedStorage $sharedStorage + * @var SharedStorage $sharedStorage */ - $sharedCache = $sharedStorage->getCache(); $this->assertEquals('', $sharedCache->getPathById($info->getId())); } - public function testGetPathByIdShareSubFolder() { + public function testGetPathByIdShareSubFolder(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - \OC\Files\Filesystem::mkdir('foo'); - \OC\Files\Filesystem::mkdir('foo/bar'); - \OC\Files\Filesystem::touch('foo/bar/test.txt'); - $folderInfo = \OC\Files\Filesystem::getFileInfo('foo'); - $fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt'); + Filesystem::mkdir('foo'); + Filesystem::mkdir('foo/bar'); + Filesystem::touch('foo/bar/test.txt'); + $folderInfo = Filesystem::getFileInfo('foo'); + $fileInfo = Filesystem::getFileInfo('foo/bar/test.txt'); $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); $node = $rootFolder->get('foo'); $share = $this->shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $this->shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); \OC_Util::tearDownFS(); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo')); - list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo'); + $this->assertTrue(Filesystem::file_exists('/foo')); + [$sharedStorage] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo'); /** - * @var \OCA\Files_Sharing\SharedStorage $sharedStorage + * @var SharedStorage $sharedStorage */ - $sharedCache = $sharedStorage->getCache(); $this->assertEquals('', $sharedCache->getPathById($folderInfo->getId())); $this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId())); } - public function testNumericStorageId() { + public function testNumericStorageId(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - \OC\Files\Filesystem::mkdir('foo'); + Filesystem::mkdir('foo'); $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); $node = $rootFolder->get('foo'); $share = $this->shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $this->shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); \OC_Util::tearDownFS(); - list($sourceStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo'); + [$sourceStorage] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo'); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo')); + $this->assertTrue(Filesystem::file_exists('/foo')); /** @var SharedStorage $sharedStorage */ - list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo'); + [$sharedStorage] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo'); $this->assertEquals($sourceStorage->getCache()->getNumericStorageId(), $sharedStorage->getCache()->getNumericStorageId()); } - public function testShareJailedStorage() { + public function testShareJailedStorage(): void { $sourceStorage = new Temporary(); $sourceStorage->mkdir('jail'); $sourceStorage->mkdir('jail/sub'); @@ -575,22 +515,94 @@ class CacheTest extends TestCase { $node = $rootFolder->get('foo/sub'); $share = $this->shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $this->shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); \OC_Util::tearDownFS(); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertEquals('foo', \OC\Files\Filesystem::file_get_contents('/sub/foo.txt')); + $this->assertEquals('foo', Filesystem::file_get_contents('/sub/foo.txt')); - \OC\Files\Filesystem::file_put_contents('/sub/bar.txt', 'bar'); + Filesystem::file_put_contents('/sub/bar.txt', 'bar'); /** @var SharedStorage $sharedStorage */ - list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub'); + [$sharedStorage] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub'); $this->assertTrue($sharedStorage->getCache()->inCache('bar.txt')); $this->assertTrue($sourceStorage->getCache()->inCache('jail/sub/bar.txt')); } + + public function testSearchShareJailedStorage(): void { + $sourceStorage = new Temporary(); + $sourceStorage->mkdir('jail'); + $sourceStorage->mkdir('jail/sub'); + $sourceStorage->file_put_contents('jail/sub/foo.txt', 'foo'); + $jailedSource = new Jail([ + 'storage' => $sourceStorage, + 'root' => 'jail' + ]); + $sourceStorage->getScanner()->scan(''); + $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $jailedSource, '/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo'); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + + $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); + $node = $rootFolder->get('foo/sub'); + $share = $this->shareManager->newShare(); + $share->setNode($node) + ->setShareType(IShare::TYPE_USER) + ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) + ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) + ->setPermissions(Constants::PERMISSION_ALL); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); + \OC_Util::tearDownFS(); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + + /** @var SharedStorage $sharedStorage */ + [$sharedStorage] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub'); + + $results = $sharedStorage->getCache()->search('foo.txt'); + $this->assertCount(1, $results); + } + + public function testWatcherRootChange() { + $sourceStorage = new Temporary(); + $sourceStorage->mkdir('shared'); + $sourceStorage->file_put_contents('shared/foo.txt', 'foo'); + $sourceStorage->getScanner()->scan(''); + $sourceStorage->getWatcher()->setPolicy(IWatcher::CHECK_ALWAYS); + $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $sourceStorage, '/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo'); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + + $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); + $node = $rootFolder->get('foo/shared'); + $this->assertEquals(3, $node->getSize()); + + $share = $this->shareManager->newShare(); + $share->setNode($node) + ->setShareType(IShare::TYPE_USER) + ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) + ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) + ->setPermissions(Constants::PERMISSION_ALL); + $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($share); + \OC_Util::tearDownFS(); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + + $view = Filesystem::getView(); + + $sourceStorage->rmdir('shared'); + + $this->assertFalse($view->getFileInfo('shared')); + } } diff --git a/apps/files_sharing/tests/CapabilitiesTest.php b/apps/files_sharing/tests/CapabilitiesTest.php index 721dd0fbf5e..9a076d7a171 100644 --- a/apps/files_sharing/tests/CapabilitiesTest.php +++ b/apps/files_sharing/tests/CapabilitiesTest.php @@ -1,33 +1,33 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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 OCA\Files_Sharing\Tests; +use OC\KnownUser\KnownUserService; +use OC\Share20\Manager; +use OC\Share20\ShareDisableChecker; use OCA\Files_Sharing\Capabilities; +use OCP\App\IAppManager; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\IRootFolder; +use OCP\Files\Mount\IMountManager; +use OCP\IAppConfig; use OCP\IConfig; - +use OCP\IDateTimeZone; +use OCP\IGroupManager; +use OCP\IURLGenerator; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\L10N\IFactory; +use OCP\Mail\IMailer; +use OCP\Security\IHasher; +use OCP\Security\ISecureRandom; +use OCP\Share\IProviderFactory; +use Psr\Log\LoggerInterface; /** * Class CapabilitiesTest @@ -56,72 +56,120 @@ class CapabilitiesTest extends \Test\TestCase { * @param (string[])[] $map Map of arguments to return types for the getAppValue function in the mock * @return string[] */ - private function getResults(array $map) { + private function getResults(array $map, array $typedMap = [], bool $federationEnabled = true) { $config = $this->getMockBuilder(IConfig::class)->disableOriginalConstructor()->getMock(); - $config->method('getAppValue')->will($this->returnValueMap($map)); - $cap = new Capabilities($config); + $appManager = $this->getMockBuilder(IAppManager::class)->disableOriginalConstructor()->getMock(); + $config->method('getAppValue')->willReturnMap($map); + $appManager->method('isEnabledForAnyone')->with('federation')->willReturn($federationEnabled); + + if (empty($typedMap)) { + $appConfig = $this->createMock(IAppConfig::class); + } else { + // hack to help transition from old IConfig to new IAppConfig + $appConfig = $this->getMockBuilder(IAppConfig::class)->disableOriginalConstructor()->getMock(); + $appConfig->expects($this->any())->method('getValueBool')->willReturnCallback(function (...$args) use ($typedMap): bool { + foreach ($typedMap as $entry) { + if ($entry[0] !== $args[0] || $entry[1] !== $args[1]) { + continue; + } + + return $entry[2]; + } + + return false; + }); + } + + $shareManager = new Manager( + $this->createMock(LoggerInterface::class), + $config, + $this->createMock(ISecureRandom::class), + $this->createMock(IHasher::class), + $this->createMock(IMountManager::class), + $this->createMock(IGroupManager::class), + $this->createMock(IFactory::class), + $this->createMock(IProviderFactory::class), + $this->createMock(IUserManager::class), + $this->createMock(IRootFolder::class), + $this->createMock(IMailer::class), + $this->createMock(IURLGenerator::class), + $this->createMock(\OC_Defaults::class), + $this->createMock(IEventDispatcher::class), + $this->createMock(IUserSession::class), + $this->createMock(KnownUserService::class), + $this->createMock(ShareDisableChecker::class), + $this->createMock(IDateTimeZone::class), + $appConfig, + ); + + $cap = new Capabilities($config, $appConfig, $shareManager, $appManager); $result = $this->getFilesSharingPart($cap->getCapabilities()); return $result; } - public function testEnabledSharingAPI() { + public function testEnabledSharingAPI(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ]; $result = $this->getResults($map); $this->assertTrue($result['api_enabled']); - $this->assertContains('public', $result); - $this->assertContains('user', $result); - $this->assertContains('resharing', $result); + $this->assertArrayHasKey('public', $result); + $this->assertArrayHasKey('user', $result); + $this->assertArrayHasKey('resharing', $result); } - public function testDisabledSharingAPI() { + public function testDisabledSharingAPI(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'no'], ]; $result = $this->getResults($map); $this->assertFalse($result['api_enabled']); - $this->assertNotContains('public', $result); - $this->assertNotContains('user', $result); - $this->assertNotContains('resharing', $result); + $this->assertFalse($result['public']['enabled']); + $this->assertFalse($result['user']['send_mail']); + $this->assertFalse($result['resharing']); } - public function testNoLinkSharing() { + public function testNoLinkSharing(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'no'], ]; $result = $this->getResults($map); - $this->assertInternalType('array', $result['public']); + $this->assertIsArray($result['public']); $this->assertFalse($result['public']['enabled']); } - public function testOnlyLinkSharing() { + public function testOnlyLinkSharing(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); - $this->assertInternalType('array', $result['public']); + $this->assertIsArray($result['public']); $this->assertTrue($result['public']['enabled']); } - public function testLinkPassword() { + public function testLinkPassword(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], - ['core', 'shareapi_enforce_links_password', 'no', 'yes'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; - $result = $this->getResults($map); + $typedMap = [ + ['core', 'shareapi_enforce_links_password', true], + ]; + $result = $this->getResults($map, $typedMap); $this->assertArrayHasKey('password', $result['public']); $this->assertArrayHasKey('enforced', $result['public']['password']); $this->assertTrue($result['public']['password']['enforced']); } - public function testLinkNoPassword() { + public function testLinkNoPassword(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ['core', 'shareapi_enforce_links_password', 'no', 'no'], ]; $result = $this->getResults($map); @@ -130,108 +178,117 @@ class CapabilitiesTest extends \Test\TestCase { $this->assertFalse($result['public']['password']['enforced']); } - public function testLinkNoExpireDate() { + public function testLinkNoExpireDate(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], ['core', 'shareapi_default_expire_date', 'no', 'no'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertArrayHasKey('expire_date', $result['public']); - $this->assertInternalType('array', $result['public']['expire_date']); + $this->assertIsArray($result['public']['expire_date']); $this->assertFalse($result['public']['expire_date']['enabled']); } - public function testLinkExpireDate() { + public function testLinkExpireDate(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], ['core', 'shareapi_default_expire_date', 'no', 'yes'], ['core', 'shareapi_expire_after_n_days', '7', '7'], ['core', 'shareapi_enforce_expire_date', 'no', 'no'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertArrayHasKey('expire_date', $result['public']); - $this->assertInternalType('array', $result['public']['expire_date']); + $this->assertIsArray($result['public']['expire_date']); $this->assertTrue($result['public']['expire_date']['enabled']); $this->assertArrayHasKey('days', $result['public']['expire_date']); $this->assertFalse($result['public']['expire_date']['enforced']); } - public function testLinkExpireDateEnforced() { + public function testLinkExpireDateEnforced(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], ['core', 'shareapi_default_expire_date', 'no', 'yes'], ['core', 'shareapi_enforce_expire_date', 'no', 'yes'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertArrayHasKey('expire_date', $result['public']); - $this->assertInternalType('array', $result['public']['expire_date']); + $this->assertIsArray($result['public']['expire_date']); $this->assertTrue($result['public']['expire_date']['enforced']); } - public function testLinkSendMail() { + public function testLinkSendMail(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], ['core', 'shareapi_allow_public_notification', 'no', 'yes'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertTrue($result['public']['send_mail']); } - public function testLinkNoSendMail() { + public function testLinkNoSendMail(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], ['core', 'shareapi_allow_public_notification', 'no', 'no'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertFalse($result['public']['send_mail']); } - public function testResharing() { + public function testResharing(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_resharing', 'yes', 'yes'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertTrue($result['resharing']); } - public function testNoResharing() { + public function testNoResharing(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_resharing', 'yes', 'no'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertFalse($result['resharing']); } - public function testLinkPublicUpload() { + public function testLinkPublicUpload(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], ['core', 'shareapi_allow_public_upload', 'yes', 'yes'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertTrue($result['public']['upload']); $this->assertTrue($result['public']['upload_files_drop']); } - public function testLinkNoPublicUpload() { + public function testLinkNoPublicUpload(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_links', 'yes', 'yes'], ['core', 'shareapi_allow_public_upload', 'yes', 'no'], + ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''], ]; $result = $this->getResults($map); $this->assertFalse($result['public']['upload']); $this->assertFalse($result['public']['upload_files_drop']); } - public function testNoGroupSharing() { + public function testNoGroupSharing(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_group_sharing', 'yes', 'no'], @@ -240,7 +297,7 @@ class CapabilitiesTest extends \Test\TestCase { $this->assertFalse($result['group_sharing']); } - public function testGroupSharing() { + public function testGroupSharing(): void { $map = [ ['core', 'shareapi_enabled', 'yes', 'yes'], ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'], @@ -249,7 +306,7 @@ class CapabilitiesTest extends \Test\TestCase { $this->assertTrue($result['group_sharing']); } - public function testFederatedSharingIncomming() { + public function testFederatedSharingIncoming(): void { $map = [ ['files_sharing', 'incoming_server2server_share_enabled', 'yes', 'yes'], ]; @@ -258,7 +315,7 @@ class CapabilitiesTest extends \Test\TestCase { $this->assertTrue($result['federation']['incoming']); } - public function testFederatedSharingNoIncomming() { + public function testFederatedSharingNoIncoming(): void { $map = [ ['files_sharing', 'incoming_server2server_share_enabled', 'yes', 'no'], ]; @@ -267,7 +324,7 @@ class CapabilitiesTest extends \Test\TestCase { $this->assertFalse($result['federation']['incoming']); } - public function testFederatedSharingOutgoing() { + public function testFederatedSharingOutgoing(): void { $map = [ ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', 'yes'], ]; @@ -276,7 +333,7 @@ class CapabilitiesTest extends \Test\TestCase { $this->assertTrue($result['federation']['outgoing']); } - public function testFederatedSharingNoOutgoing() { + public function testFederatedSharingNoOutgoing(): void { $map = [ ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', 'no'], ]; @@ -285,4 +342,19 @@ class CapabilitiesTest extends \Test\TestCase { $this->assertFalse($result['federation']['outgoing']); } + public function testFederatedSharingExpirationDate(): void { + $result = $this->getResults([]); + $this->assertArrayHasKey('federation', $result); + $this->assertEquals(['enabled' => true], $result['federation']['expire_date']); + $this->assertEquals(['enabled' => true], $result['federation']['expire_date_supported']); + } + + public function testFederatedSharingDisabled(): void { + $result = $this->getResults([], federationEnabled: false); + $this->assertArrayHasKey('federation', $result); + $this->assertFalse($result['federation']['incoming']); + $this->assertFalse($result['federation']['outgoing']); + $this->assertEquals(['enabled' => false], $result['federation']['expire_date']); + $this->assertEquals(['enabled' => false], $result['federation']['expire_date_supported']); + } } diff --git a/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php b/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php index 8f516788761..572463a9ebc 100644 --- a/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php +++ b/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php @@ -1,29 +1,11 @@ <?php + /** - * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @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 OCA\Files_Sharing\Tests\Collaboration; - use OCA\Files_Sharing\Collaboration\ShareRecipientSorter; use OCP\Files\Folder; use OCP\Files\IRootFolder; @@ -34,16 +16,16 @@ use OCP\Share\IManager; use Test\TestCase; class ShareRecipientSorterTest extends TestCase { - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ protected $shareManager; - /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */ protected $rootFolder; - /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ protected $userSession; - /** @var ShareRecipientSorter */ + /** @var ShareRecipientSorter */ protected $sorter; - public function setUp() { + protected function setUp(): void { parent::setUp(); $this->shareManager = $this->createMock(IManager::class); @@ -54,13 +36,13 @@ class ShareRecipientSorterTest extends TestCase { } /** - * @dataProvider sortDataProvider * @param $data */ - public function testSort($data) { + #[\PHPUnit\Framework\Attributes\DataProvider('sortDataProvider')] + public function testSort($data): void { $node = $this->createMock(Node::class); - /** @var Folder|\PHPUnit_Framework_MockObject_MockObject $folder */ + /** @var Folder|\PHPUnit\Framework\MockObject\MockObject $folder */ $folder = $this->createMock(Folder::class); $this->rootFolder->expects($this->any()) ->method('getUserFolder') @@ -77,9 +59,9 @@ class ShareRecipientSorterTest extends TestCase { if ($data['context']['itemType'] === 'files') { $folder->expects($this->once()) - ->method('getById') + ->method('getFirstNodeById') ->with($data['context']['itemId']) - ->willReturn([$node]); + ->willReturn($node); $this->shareManager->expects($this->once()) ->method('getAccessList') @@ -87,7 +69,7 @@ class ShareRecipientSorterTest extends TestCase { ->willReturn($data['accessList']); } else { $folder->expects($this->never()) - ->method('getById'); + ->method('getFirstNodeById'); $this->shareManager->expects($this->never()) ->method('getAccessList'); } @@ -98,16 +80,16 @@ class ShareRecipientSorterTest extends TestCase { $this->assertEquals($data['expected'], $workArray); } - public function testSortNoNodes() { - /** @var Folder|\PHPUnit_Framework_MockObject_MockObject $folder */ + public function testSortNoNodes(): void { + /** @var Folder|\PHPUnit\Framework\MockObject\MockObject $folder */ $folder = $this->createMock(Folder::class); $this->rootFolder->expects($this->any()) ->method('getUserFolder') ->willReturn($folder); $folder->expects($this->once()) - ->method('getById') - ->willReturn([]); + ->method('getFirstNodeById') + ->willReturn(null); $user = $this->createMock(IUser::class); $user->expects($this->any()) @@ -128,44 +110,42 @@ class ShareRecipientSorterTest extends TestCase { ] ]; $workArray = $originalArray; - $this->sorter->sort($workArray, ['itemType' => 'files', 'itemId' => 404]); + $this->sorter->sort($workArray, ['itemType' => 'files', 'itemId' => '404']); $this->assertEquals($originalArray, $workArray); } - public function sortDataProvider() { + public static function sortDataProvider() { return [[ [ #0 – sort properly and otherwise keep existing order - 'context' => ['itemType' => 'files', 'itemId' => 42], + 'context' => ['itemType' => 'files', 'itemId' => '42'], 'accessList' => ['users' => ['celia', 'darius', 'faruk', 'gail'], 'bots' => ['r2-d2']], 'input' => [ - 'users' => - [ - ['value' => ['shareWith' => 'alice']], - ['value' => ['shareWith' => 'bob']], - ['value' => ['shareWith' => 'celia']], - ['value' => ['shareWith' => 'darius']], - ['value' => ['shareWith' => 'elena']], - ['value' => ['shareWith' => 'faruk']], - ['value' => ['shareWith' => 'gail']], - ], + 'users' => [ + ['value' => ['shareWith' => 'alice']], + ['value' => ['shareWith' => 'bob']], + ['value' => ['shareWith' => 'celia']], + ['value' => ['shareWith' => 'darius']], + ['value' => ['shareWith' => 'elena']], + ['value' => ['shareWith' => 'faruk']], + ['value' => ['shareWith' => 'gail']], + ], 'bots' => [ ['value' => ['shareWith' => 'c-3po']], ['value' => ['shareWith' => 'r2-d2']], ] ], 'expected' => [ - 'users' => - [ - ['value' => ['shareWith' => 'celia']], - ['value' => ['shareWith' => 'darius']], - ['value' => ['shareWith' => 'faruk']], - ['value' => ['shareWith' => 'gail']], - ['value' => ['shareWith' => 'alice']], - ['value' => ['shareWith' => 'bob']], - ['value' => ['shareWith' => 'elena']], - ], + 'users' => [ + ['value' => ['shareWith' => 'celia']], + ['value' => ['shareWith' => 'darius']], + ['value' => ['shareWith' => 'faruk']], + ['value' => ['shareWith' => 'gail']], + ['value' => ['shareWith' => 'alice']], + ['value' => ['shareWith' => 'bob']], + ['value' => ['shareWith' => 'elena']], + ], 'bots' => [ ['value' => ['shareWith' => 'r2-d2']], ['value' => ['shareWith' => 'c-3po']], @@ -174,35 +154,33 @@ class ShareRecipientSorterTest extends TestCase { ], [ #1 – no recipients - 'context' => ['itemType' => 'files', 'itemId' => 42], + 'context' => ['itemType' => 'files', 'itemId' => '42'], 'accessList' => ['users' => false], 'input' => [ - 'users' => - [ - ['value' => ['shareWith' => 'alice']], - ['value' => ['shareWith' => 'bob']], - ['value' => ['shareWith' => 'celia']], - ['value' => ['shareWith' => 'darius']], - ['value' => ['shareWith' => 'elena']], - ['value' => ['shareWith' => 'faruk']], - ['value' => ['shareWith' => 'gail']], - ], + 'users' => [ + ['value' => ['shareWith' => 'alice']], + ['value' => ['shareWith' => 'bob']], + ['value' => ['shareWith' => 'celia']], + ['value' => ['shareWith' => 'darius']], + ['value' => ['shareWith' => 'elena']], + ['value' => ['shareWith' => 'faruk']], + ['value' => ['shareWith' => 'gail']], + ], 'bots' => [ ['value' => ['shareWith' => 'c-3po']], ['value' => ['shareWith' => 'r2-d2']], ] ], 'expected' => [ - 'users' => - [ - ['value' => ['shareWith' => 'alice']], - ['value' => ['shareWith' => 'bob']], - ['value' => ['shareWith' => 'celia']], - ['value' => ['shareWith' => 'darius']], - ['value' => ['shareWith' => 'elena']], - ['value' => ['shareWith' => 'faruk']], - ['value' => ['shareWith' => 'gail']], - ], + 'users' => [ + ['value' => ['shareWith' => 'alice']], + ['value' => ['shareWith' => 'bob']], + ['value' => ['shareWith' => 'celia']], + ['value' => ['shareWith' => 'darius']], + ['value' => ['shareWith' => 'elena']], + ['value' => ['shareWith' => 'faruk']], + ['value' => ['shareWith' => 'gail']], + ], 'bots' => [ ['value' => ['shareWith' => 'c-3po']], ['value' => ['shareWith' => 'r2-d2']], @@ -211,35 +189,33 @@ class ShareRecipientSorterTest extends TestCase { ], [ #2 – unsupported item type - 'context' => ['itemType' => 'announcements', 'itemId' => 42], + 'context' => ['itemType' => 'announcements', 'itemId' => '42'], 'accessList' => null, // not needed 'input' => [ - 'users' => - [ - ['value' => ['shareWith' => 'alice']], - ['value' => ['shareWith' => 'bob']], - ['value' => ['shareWith' => 'celia']], - ['value' => ['shareWith' => 'darius']], - ['value' => ['shareWith' => 'elena']], - ['value' => ['shareWith' => 'faruk']], - ['value' => ['shareWith' => 'gail']], - ], + 'users' => [ + ['value' => ['shareWith' => 'alice']], + ['value' => ['shareWith' => 'bob']], + ['value' => ['shareWith' => 'celia']], + ['value' => ['shareWith' => 'darius']], + ['value' => ['shareWith' => 'elena']], + ['value' => ['shareWith' => 'faruk']], + ['value' => ['shareWith' => 'gail']], + ], 'bots' => [ ['value' => ['shareWith' => 'c-3po']], ['value' => ['shareWith' => 'r2-d2']], ] ], 'expected' => [ - 'users' => - [ - ['value' => ['shareWith' => 'alice']], - ['value' => ['shareWith' => 'bob']], - ['value' => ['shareWith' => 'celia']], - ['value' => ['shareWith' => 'darius']], - ['value' => ['shareWith' => 'elena']], - ['value' => ['shareWith' => 'faruk']], - ['value' => ['shareWith' => 'gail']], - ], + 'users' => [ + ['value' => ['shareWith' => 'alice']], + ['value' => ['shareWith' => 'bob']], + ['value' => ['shareWith' => 'celia']], + ['value' => ['shareWith' => 'darius']], + ['value' => ['shareWith' => 'elena']], + ['value' => ['shareWith' => 'faruk']], + ['value' => ['shareWith' => 'gail']], + ], 'bots' => [ ['value' => ['shareWith' => 'c-3po']], ['value' => ['shareWith' => 'r2-d2']], @@ -248,7 +224,7 @@ class ShareRecipientSorterTest extends TestCase { ], [ #3 – no nothing - 'context' => ['itemType' => 'files', 'itemId' => 42], + 'context' => ['itemType' => 'files', 'itemId' => '42'], 'accessList' => [], 'input' => [], 'expected' => [], diff --git a/apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php b/apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php index 7380f39dfba..6f0960bf46c 100644 --- a/apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php +++ b/apps/files_sharing/tests/Command/CleanupRemoteStoragesTest.php @@ -1,29 +1,18 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud GmbH. - * - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud GmbH. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests\Command; use OCA\Files_Sharing\Command\CleanupRemoteStorages; +use OCP\Federation\ICloudId; +use OCP\Federation\ICloudIdManager; +use OCP\IDBConnection; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; @@ -37,15 +26,9 @@ use Test\TestCase; */ class CleanupRemoteStoragesTest extends TestCase { - /** - * @var CleanupRemoteStorages - */ - private $command; - - /** - * @var \OCP\IDBConnection - */ - private $connection; + protected IDBConnection $connection; + protected CleanupRemoteStorages $command; + private ICloudIdManager&MockObject $cloudIdManager; private $storages = [ ['id' => 'shared::7b4a322b22f9d0047c38d77d471ce3cf', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e1', 'remote' => 'https://hostname.tld/owncloud1', 'user' => 'user1'], @@ -57,65 +40,71 @@ class CleanupRemoteStoragesTest extends TestCase { ['notExistingId' => 'shared::c34568c143cdac7d2f06e0800b5280f9', 'share_token' => 'f2c69dad1dc0649f26976fd210fc62e7', 'remote' => 'https://hostname.tld/owncloud7', 'user' => 'user7'], ]; - protected function setup() { + protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); - $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $storageQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $storageQuery->insert('storages') - ->setValue('id', '?'); + ->setValue('id', $storageQuery->createParameter('id')); - $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $shareExternalQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $shareExternalQuery->insert('share_external') - ->setValue('share_token', '?') - ->setValue('remote', '?') - ->setValue('name', '?')->setParameter(2, 'irrelevant') - ->setValue('owner', '?')->setParameter(3, 'irrelevant') - ->setValue('user', '?') - ->setValue('mountpoint', '?')->setParameter(5, 'irrelevant') - ->setValue('mountpoint_hash', '?')->setParameter(6, 'irrelevant'); - - $filesQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + ->setValue('share_token', $shareExternalQuery->createParameter('share_token')) + ->setValue('remote', $shareExternalQuery->createParameter('remote')) + ->setValue('name', $shareExternalQuery->createParameter('name')) + ->setValue('owner', $shareExternalQuery->createParameter('owner')) + ->setValue('user', $shareExternalQuery->createParameter('user')) + ->setValue('mountpoint', $shareExternalQuery->createParameter('mountpoint')) + ->setValue('mountpoint_hash', $shareExternalQuery->createParameter('mountpoint_hash')); + + $filesQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $filesQuery->insert('filecache') - ->setValue('storage', '?') - ->setValue('path', '?') - ->setValue('path_hash', '?'); + ->setValue('storage', $filesQuery->createParameter('storage')) + ->setValue('path', $filesQuery->createParameter('path')) + ->setValue('path_hash', $filesQuery->createParameter('path_hash')); foreach ($this->storages as &$storage) { if (isset($storage['id'])) { - $storageQuery->setParameter(0, $storage['id']); - $storageQuery->execute(); - $storage['numeric_id'] = $this->connection->lastInsertId('*PREFIX*storages'); + $storageQuery->setParameter('id', $storage['id']); + $storageQuery->executeStatement(); + $storage['numeric_id'] = $storageQuery->getLastInsertId(); } if (isset($storage['share_token'])) { $shareExternalQuery - ->setParameter(0, $storage['share_token']) - ->setParameter(1, $storage['remote']) - ->setParameter(4, $storage['user']); - $shareExternalQuery->execute(); + ->setParameter('share_token', $storage['share_token']) + ->setParameter('remote', $storage['remote']) + ->setParameter('name', 'irrelevant') + ->setParameter('owner', 'irrelevant') + ->setParameter('user', $storage['user']) + ->setParameter('mountpoint', 'irrelevant') + ->setParameter('mountpoint_hash', 'irrelevant'); + $shareExternalQuery->executeStatement(); } if (isset($storage['files_count'])) { for ($i = 0; $i < $storage['files_count']; $i++) { - $filesQuery->setParameter(0, $storage['numeric_id']); - $filesQuery->setParameter(1, 'file' . $i); - $filesQuery->setParameter(2, md5('file' . $i)); - $filesQuery->execute(); + $filesQuery->setParameter('storage', $storage['numeric_id']); + $filesQuery->setParameter('path', 'file' . $i); + $filesQuery->setParameter('path_hash', md5('file' . $i)); + $filesQuery->executeStatement(); } } } - $this->command = new CleanupRemoteStorages($this->connection); + $this->cloudIdManager = $this->createMock(ICloudIdManager::class); + + $this->command = new CleanupRemoteStorages($this->connection, $this->cloudIdManager); } - public function tearDown() { - $storageQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + protected function tearDown(): void { + $storageQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $storageQuery->delete('storages') ->where($storageQuery->expr()->eq('id', $storageQuery->createParameter('id'))); - $shareExternalQuery = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $shareExternalQuery = Server::get(IDBConnection::class)->getQueryBuilder(); $shareExternalQuery->delete('share_external') ->where($shareExternalQuery->expr()->eq('share_token', $shareExternalQuery->createParameter('share_token'))) ->andWhere($shareExternalQuery->expr()->eq('remote', $shareExternalQuery->createParameter('remote'))); @@ -123,34 +112,40 @@ class CleanupRemoteStoragesTest extends TestCase { foreach ($this->storages as $storage) { if (isset($storage['id'])) { $storageQuery->setParameter('id', $storage['id']); - $storageQuery->execute(); + $storageQuery->executeStatement(); } if (isset($storage['share_token'])) { $shareExternalQuery->setParameter('share_token', $storage['share_token']); $shareExternalQuery->setParameter('remote', $storage['remote']); - $shareExternalQuery->execute(); + $shareExternalQuery->executeStatement(); } } - return parent::tearDown(); + parent::tearDown(); } private function doesStorageExist($numericId) { - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); $qb->select('*') ->from('storages') ->where($qb->expr()->eq('numeric_id', $qb->createNamedParameter($numericId))); - $result = $qb->execute()->fetchAll(); + + $qResult = $qb->executeQuery(); + $result = $qResult->fetch(); + $qResult->closeCursor(); if (!empty($result)) { return true; } - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); $qb->select('*') ->from('filecache') ->where($qb->expr()->eq('storage', $qb->createNamedParameter($numericId))); - $result = $qb->execute()->fetchAll(); + + $qResult = $qb->executeQuery(); + $result = $qResult->fetch(); + $qResult->closeCursor(); if (!empty($result)) { return true; } @@ -161,7 +156,7 @@ class CleanupRemoteStoragesTest extends TestCase { /** * Test cleanup of orphaned storages */ - public function testCleanup() { + public function testCleanup(): void { $input = $this->getMockBuilder(InputInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -169,19 +164,30 @@ class CleanupRemoteStoragesTest extends TestCase { ->disableOriginalConstructor() ->getMock(); - // - // parent folder, `files`, ´test` and `welcome.txt` => 4 elements - - $at = 0; + $outputCalls = []; $output - ->expects($this->at($at++)) + ->expects($this->any()) ->method('writeln') - ->with('5 remote storage(s) need(s) to be checked'); - $output - ->expects($this->at($at++)) - ->method('writeln') - ->with('5 remote share(s) exist'); + ->willReturnCallback(function (string $text) use (&$outputCalls): void { + $outputCalls[] = $text; + }); + + $this->cloudIdManager + ->expects($this->any()) + ->method('getCloudId') + ->willReturnCallback(function (string $user, string $remote) { + $cloudIdMock = $this->createMock(ICloudId::class); + + // The remotes are already sanitized in the original data, so + // they can be directly returned. + $cloudIdMock + ->expects($this->any()) + ->method('getRemote') + ->willReturn($remote); + + return $cloudIdMock; + }); $this->command->execute($input, $output); @@ -191,6 +197,9 @@ class CleanupRemoteStoragesTest extends TestCase { $this->assertTrue($this->doesStorageExist($this->storages[4]['numeric_id'])); $this->assertFalse($this->doesStorageExist($this->storages[5]['numeric_id'])); + $this->assertEquals([ + '5 remote storage(s) need(s) to be checked', + '5 remote share(s) exist', + ], array_slice($outputCalls, 0, 2)); } } - diff --git a/apps/files_sharing/tests/Command/FixShareOwnersTest.php b/apps/files_sharing/tests/Command/FixShareOwnersTest.php new file mode 100644 index 00000000000..0fde61895b1 --- /dev/null +++ b/apps/files_sharing/tests/Command/FixShareOwnersTest.php @@ -0,0 +1,117 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OCA\Files_Sharing\Tests\Command; + +use OCA\Files_Sharing\Command\FixShareOwners; +use OCA\Files_Sharing\OrphanHelper; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +/** + * Class FixShareOwnersTest + * + * @package OCA\Files_Sharing\Tests\Command + */ +class FixShareOwnersTest extends TestCase { + /** + * @var FixShareOwners + */ + private $command; + + /** + * @var OrphanHelper|\PHPUnit\Framework\MockObject\MockObject + */ + private $orphanHelper; + + protected function setUp(): void { + parent::setUp(); + + $this->orphanHelper = $this->createMock(OrphanHelper::class); + $this->command = new FixShareOwners($this->orphanHelper); + } + + public function testExecuteNoSharesDetected() { + $this->orphanHelper->expects($this->once()) + ->method('getAllShares') + ->willReturn([ + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], + ]); + $this->orphanHelper->expects($this->exactly(2)) + ->method('isShareValid') + ->willReturn(true); + + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $output->expects($this->once()) + ->method('writeln') + ->with('No broken shares detected'); + $this->command->execute($input, $output); + } + + public function testExecuteSharesDetected() { + $this->orphanHelper->expects($this->once()) + ->method('getAllShares') + ->willReturn([ + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], + ]); + $this->orphanHelper->expects($this->exactly(2)) + ->method('isShareValid') + ->willReturnOnConsecutiveCalls(true, false); + $this->orphanHelper->expects($this->once()) + ->method('fileExists') + ->willReturn(true); + $this->orphanHelper->expects($this->once()) + ->method('findOwner') + ->willReturn('newOwner'); + $this->orphanHelper->expects($this->once()) + ->method('updateShareOwner'); + + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $output->expects($this->once()) + ->method('writeln') + ->with('Share with id <info>2</info> (target: <info>target2</info>) updated to owner <info>newOwner</info>'); + $this->command->execute($input, $output); + } + + public function testExecuteSharesDetectedDryRun() { + $this->orphanHelper->expects($this->once()) + ->method('getAllShares') + ->willReturn([ + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], + ]); + $this->orphanHelper->expects($this->exactly(2)) + ->method('isShareValid') + ->willReturnOnConsecutiveCalls(true, false); + $this->orphanHelper->expects($this->once()) + ->method('fileExists') + ->willReturn(true); + $this->orphanHelper->expects($this->once()) + ->method('findOwner') + ->willReturn('newOwner'); + $this->orphanHelper->expects($this->never()) + ->method('updateShareOwner'); + + $input = $this->createMock(InputInterface::class); + $output = $this->createMock(OutputInterface::class); + + $output->expects($this->once()) + ->method('writeln') + ->with('Share with id <info>2</info> (target: <info>target2</info>) can be updated to owner <info>newOwner</info>'); + $input->expects($this->once()) + ->method('getOption') + ->with('dry-run') + ->willReturn(true); + $this->command->execute($input, $output); + } +} diff --git a/apps/files_sharing/tests/Controller/ExternalShareControllerTest.php b/apps/files_sharing/tests/Controller/ExternalShareControllerTest.php index 71a4f4adeba..7e054d9a6dc 100644 --- a/apps/files_sharing/tests/Controller/ExternalShareControllerTest.php +++ b/apps/files_sharing/tests/Controller/ExternalShareControllerTest.php @@ -1,34 +1,19 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author 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: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests\Controllers; use OCA\Files_Sharing\Controller\ExternalSharesController; -use OCP\AppFramework\Http\DataResponse; +use OCA\Files_Sharing\External\Manager; use OCP\AppFramework\Http\JSONResponse; use OCP\Http\Client\IClientService; +use OCP\IConfig; use OCP\IRequest; +use PHPUnit\Framework\MockObject\MockObject; /** * Class ExternalShareControllerTest @@ -40,17 +25,17 @@ class ExternalShareControllerTest extends \Test\TestCase { private $request; /** @var \OCA\Files_Sharing\External\Manager */ private $externalManager; + /** @var IConfig|MockObject */ + private $config; /** @var IClientService */ private $clientService; - public function setUp() { + protected function setUp(): void { parent::setUp(); - $this->request = $this->getMockBuilder('\\OCP\\IRequest') - ->disableOriginalConstructor()->getMock(); - $this->externalManager = $this->getMockBuilder('\\OCA\\Files_Sharing\\External\\Manager') - ->disableOriginalConstructor()->getMock(); - $this->clientService = $this->getMockBuilder('\\OCP\Http\\Client\\IClientService') - ->disableOriginalConstructor()->getMock(); + $this->request = $this->createMock(IRequest::class); + $this->externalManager = $this->createMock(Manager::class); + $this->clientService = $this->createMock(IClientService::class); + $this->config = $this->createMock(IConfig::class); } /** @@ -61,20 +46,21 @@ class ExternalShareControllerTest extends \Test\TestCase { 'files_sharing', $this->request, $this->externalManager, - $this->clientService + $this->clientService, + $this->config, ); } - public function testIndex() { + public function testIndex(): void { $this->externalManager ->expects($this->once()) ->method('getOpenShares') - ->will($this->returnValue(['MyDummyArray'])); + ->willReturn(['MyDummyArray']); $this->assertEquals(new JSONResponse(['MyDummyArray']), $this->getExternalShareController()->index()); } - public function testCreate() { + public function testCreate(): void { $this->externalManager ->expects($this->once()) ->method('acceptShare') @@ -83,7 +69,7 @@ class ExternalShareControllerTest extends \Test\TestCase { $this->assertEquals(new JSONResponse(), $this->getExternalShareController()->create(4)); } - public function testDestroy() { + public function testDestroy(): void { $this->externalManager ->expects($this->once()) ->method('declineShare') @@ -91,66 +77,4 @@ class ExternalShareControllerTest extends \Test\TestCase { $this->assertEquals(new JSONResponse(), $this->getExternalShareController()->destroy(4)); } - - public function testRemoteWithValidHttps() { - $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient') - ->disableOriginalConstructor()->getMock(); - $response = $this->getMockBuilder('\\OCP\\Http\\Client\\IResponse') - ->disableOriginalConstructor()->getMock(); - $response - ->expects($this->exactly(2)) - ->method('getBody') - ->will($this->onConsecutiveCalls('Certainly not a JSON string', '{"installed":true,"maintenance":false,"version":"8.1.0.8","versionstring":"8.1.0","edition":""}')); - $client - ->expects($this->any()) - ->method('get') - ->will($this->returnValue($response)); - - $this->clientService - ->expects($this->exactly(2)) - ->method('newClient') - ->will($this->returnValue($client)); - - $this->assertEquals(new DataResponse('https'), $this->getExternalShareController()->testRemote('owncloud.org')); - } - - public function testRemoteWithWorkingHttp() { - $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient') - ->disableOriginalConstructor()->getMock(); - $response = $this->getMockBuilder('\\OCP\\Http\\Client\\IResponse') - ->disableOriginalConstructor()->getMock(); - $client - ->method('get') - ->will($this->returnValue($response)); - $response - ->expects($this->exactly(5)) - ->method('getBody') - ->will($this->onConsecutiveCalls('Certainly not a JSON string', 'Certainly not a JSON string', 'Certainly not a JSON string', 'Certainly not a JSON string', '{"installed":true,"maintenance":false,"version":"8.1.0.8","versionstring":"8.1.0","edition":""}')); - $this->clientService - ->expects($this->exactly(5)) - ->method('newClient') - ->will($this->returnValue($client)); - - $this->assertEquals(new DataResponse('http'), $this->getExternalShareController()->testRemote('owncloud.org')); - } - - public function testRemoteWithInvalidRemote() { - $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient') - ->disableOriginalConstructor()->getMock(); - $response = $this->getMockBuilder('\\OCP\\Http\\Client\\IResponse') - ->disableOriginalConstructor()->getMock(); - $client - ->method('get') - ->will($this->returnValue($response)); - $response - ->expects($this->exactly(6)) - ->method('getBody') - ->will($this->returnValue('Certainly not a JSON string')); - $this->clientService - ->expects($this->exactly(6)) - ->method('newClient') - ->will($this->returnValue($client)); - - $this->assertEquals(new DataResponse(false), $this->getExternalShareController()->testRemote('owncloud.org')); - } } diff --git a/apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php b/apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php index 174abbb6f60..f49d839e8d4 100644 --- a/apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php +++ b/apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2016, 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: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Sharing\Tests\Controller; @@ -26,6 +10,7 @@ use OCA\Files_Sharing\Controller\PublicPreviewController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\Constants; use OCP\Files\File; use OCP\Files\Folder; @@ -33,70 +18,79 @@ use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\IPreview; use OCP\IRequest; +use OCP\ISession; +use OCP\Preview\IMimeIconProvider; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager; use OCP\Share\IShare; -use Punic\Data; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class PublicPreviewControllerTest extends TestCase { - /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ - private $previewManager; - - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ - private $shareManager; + private IPreview&MockObject $previewManager; + private IManager&MockObject $shareManager; + private ITimeFactory&MockObject $timeFactory; + private IRequest&MockObject $request; - /** @var PublicPreviewController */ - private $controller; + private PublicPreviewController $controller; - public function setUp() { + protected function setUp(): void { parent::setUp(); $this->previewManager = $this->createMock(IPreview::class); $this->shareManager = $this->createMock(IManager::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->request = $this->createMock(IRequest::class); + + $this->timeFactory->method('getTime') + ->willReturn(1337); + + $this->overwriteService(ITimeFactory::class, $this->timeFactory); $this->controller = new PublicPreviewController( 'files_sharing', - $this->createMock(IRequest::class), + $this->request, $this->shareManager, - $this->previewManager + $this->createMock(ISession::class), + $this->previewManager, + $this->createMock(IMimeIconProvider::class), ); } - public function testInvalidToken() { - $res = $this->controller->getPreview('file', 10, 10, ''); + public function testInvalidToken(): void { + $res = $this->controller->getPreview('', 'file', 10, 10, ''); $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); $this->assertEquals($expected, $res); } - public function testInvalidWidth() { - $res = $this->controller->getPreview('file', 0); + public function testInvalidWidth(): void { + $res = $this->controller->getPreview('token', 'file', 0); $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); $this->assertEquals($expected, $res); } - public function testInvalidHeight() { - $res = $this->controller->getPreview('file', 10, 0); + public function testInvalidHeight(): void { + $res = $this->controller->getPreview('token', 'file', 10, 0); $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); $this->assertEquals($expected, $res); } - public function testInvalidShare() { + public function testInvalidShare(): void { $this->shareManager->method('getShareByToken') ->with($this->equalTo('token')) ->willThrowException(new ShareNotFound()); - $res = $this->controller->getPreview('file', 10, 10, 'token'); + $res = $this->controller->getPreview('token', 'file', 10, 10); $expected = new DataResponse([], Http::STATUS_NOT_FOUND); $this->assertEquals($expected, $res); } - public function testShareNotAccessable() { + public function testShareNotAccessable(): void { $share = $this->createMock(IShare::class); $this->shareManager->method('getShareByToken') ->with($this->equalTo('token')) @@ -105,12 +99,102 @@ class PublicPreviewControllerTest extends TestCase { $share->method('getPermissions') ->willReturn(0); - $res = $this->controller->getPreview('file', 10, 10, 'token'); + $res = $this->controller->getPreview('token', 'file', 10, 10); $expected = new DataResponse([], Http::STATUS_FORBIDDEN); $this->assertEquals($expected, $res); } + public function testShareNoDownload() { + $share = $this->createMock(IShare::class); + $this->shareManager->method('getShareByToken') + ->with($this->equalTo('token')) + ->willReturn($share); + + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); + + $share->method('canSeeContent') + ->willReturn(false); + + $res = $this->controller->getPreview('token', 'file', 10, 10); + $expected = new DataResponse([], Http::STATUS_FORBIDDEN); + + $this->assertEquals($expected, $res); + } + + public function testShareNoDownloadButPreviewHeader() { + $share = $this->createMock(IShare::class); + $this->shareManager->method('getShareByToken') + ->with($this->equalTo('token')) + ->willReturn($share); + + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); + + $share->method('canSeeContent') + ->willReturn(false); + + $this->request->method('getHeader') + ->with('x-nc-preview') + ->willReturn('true'); + + $file = $this->createMock(File::class); + $share->method('getNode') + ->willReturn($file); + + $preview = $this->createMock(ISimpleFile::class); + $preview->method('getName')->willReturn('name'); + $preview->method('getMTime')->willReturn(42); + $this->previewManager->method('getPreview') + ->with($this->equalTo($file), 10, 10, false) + ->willReturn($preview); + + $preview->method('getMimeType') + ->willReturn('myMime'); + + $res = $this->controller->getPreview('token', 'file', 10, 10, true); + $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); + $expected->cacheFor(15 * 60); + $this->assertEquals($expected, $res); + } + + public function testShareWithAttributes() { + $share = $this->createMock(IShare::class); + $this->shareManager->method('getShareByToken') + ->with($this->equalTo('token')) + ->willReturn($share); + + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); + + $share->method('canSeeContent') + ->willReturn(true); + + $this->request->method('getHeader') + ->with('x-nc-preview') + ->willReturn('true'); + + $file = $this->createMock(File::class); + $share->method('getNode') + ->willReturn($file); + + $preview = $this->createMock(ISimpleFile::class); + $preview->method('getName')->willReturn('name'); + $preview->method('getMTime')->willReturn(42); + $this->previewManager->method('getPreview') + ->with($this->equalTo($file), 10, 10, false) + ->willReturn($preview); + + $preview->method('getMimeType') + ->willReturn('myMime'); + + $res = $this->controller->getPreview('token', 'file', 10, 10, true); + $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); + $expected->cacheFor(3600 * 24); + $this->assertEquals($expected, $res); + } + public function testPreviewFile() { $share = $this->createMock(IShare::class); $this->shareManager->method('getShareByToken') @@ -124,7 +208,12 @@ class PublicPreviewControllerTest extends TestCase { $share->method('getNode') ->willReturn($file); + $share->method('canSeeContent') + ->willReturn(true); + $preview = $this->createMock(ISimpleFile::class); + $preview->method('getName')->willReturn('name'); + $preview->method('getMTime')->willReturn(42); $this->previewManager->method('getPreview') ->with($this->equalTo($file), 10, 10, false) ->willReturn($preview); @@ -132,12 +221,13 @@ class PublicPreviewControllerTest extends TestCase { $preview->method('getMimeType') ->willReturn('myMime'); - $res = $this->controller->getPreview('file', 10, 10, 'token', true); + $res = $this->controller->getPreview('token', 'file', 10, 10, true); $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); + $expected->cacheFor(3600 * 24); $this->assertEquals($expected, $res); } - public function testPreviewFolderInvalidFile() { + public function testPreviewFolderInvalidFile(): void { $share = $this->createMock(IShare::class); $this->shareManager->method('getShareByToken') ->with($this->equalTo('token')) @@ -150,17 +240,20 @@ class PublicPreviewControllerTest extends TestCase { $share->method('getNode') ->willReturn($folder); + $share->method('canSeeContent') + ->willReturn(true); + $folder->method('get') ->with($this->equalTo('file')) ->willThrowException(new NotFoundException()); - $res = $this->controller->getPreview('file', 10, 10, 'token', true); + $res = $this->controller->getPreview('token', 'file', 10, 10, true); $expected = new DataResponse([], Http::STATUS_NOT_FOUND); $this->assertEquals($expected, $res); } - public function testPreviewFolderValidFile() { + public function testPreviewFolderValidFile(): void { $share = $this->createMock(IShare::class); $this->shareManager->method('getShareByToken') ->with($this->equalTo('token')) @@ -173,12 +266,17 @@ class PublicPreviewControllerTest extends TestCase { $share->method('getNode') ->willReturn($folder); + $share->method('canSeeContent') + ->willReturn(true); + $file = $this->createMock(File::class); $folder->method('get') ->with($this->equalTo('file')) ->willReturn($file); $preview = $this->createMock(ISimpleFile::class); + $preview->method('getName')->willReturn('name'); + $preview->method('getMTime')->willReturn(42); $this->previewManager->method('getPreview') ->with($this->equalTo($file), 10, 10, false) ->willReturn($preview); @@ -186,8 +284,9 @@ class PublicPreviewControllerTest extends TestCase { $preview->method('getMimeType') ->willReturn('myMime'); - $res = $this->controller->getPreview('file', 10, 10, 'token', true); + $res = $this->controller->getPreview('token', 'file', 10, 10, true); $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); + $expected->cacheFor(3600 * 24); $this->assertEquals($expected, $res); } } diff --git a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php index c438dac2521..e6be0342c26 100644 --- a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php @@ -1,50 +1,56 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests\Controller; +use OCA\Federation\TrustedServers; +use OCA\Files_Sharing\Controller\ShareAPIController; +use OCP\App\IAppManager; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSBadRequestException; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\Constants; use OCP\Files\File; use OCP\Files\Folder; -use OCP\Files\Storage; -use OCP\IL10N; -use OCA\Files_Sharing\Controller\ShareAPIController; +use OCP\Files\IRootFolder; +use OCP\Files\Mount\IMountPoint; +use OCP\Files\Mount\IShareOwnerlessMount; use OCP\Files\NotFoundException; +use OCP\Files\Storage\IStorage; +use OCP\IAppConfig; +use OCP\IConfig; +use OCP\IDateTimeZone; +use OCP\IGroup; use OCP\IGroupManager; -use OCP\IUserManager; +use OCP\IL10N; +use OCP\IPreview; use OCP\IRequest; +use OCP\ITagManager; +use OCP\ITags; use OCP\IURLGenerator; use OCP\IUser; -use OCP\Files\IRootFolder; +use OCP\IUserManager; +use OCP\Lock\ILockingProvider; use OCP\Lock\LockedException; +use OCP\Mail\IMailer; +use OCP\Server; +use OCP\Share\Exceptions\GenericShareException; +use OCP\Share\Exceptions\ShareNotFound; +use OCP\Share\IAttributes as IShareAttributes; use OCP\Share\IManager; -use OCP\Share; -use Test\TestCase; +use OCP\Share\IProviderFactory; use OCP\Share\IShare; +use OCP\UserStatus\IManager as IUserStatusManager; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; +use Test\TestCase; /** * Class ShareAPIControllerTest @@ -54,42 +60,40 @@ use OCP\Share\IShare; */ class ShareAPIControllerTest extends TestCase { - /** @var string */ - private $appName = 'files_sharing'; - - /** @var \OC\Share20\Manager|\PHPUnit_Framework_MockObject_MockObject */ - private $shareManager; - - /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ - private $groupManager; - - /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - private $userManager; - - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ - private $request; - - /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ - private $rootFolder; - - /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ - private $urlGenerator; - - /** @var string|\PHPUnit_Framework_MockObject_MockObject */ - private $currentUser; - - /** @var ShareAPIController */ - private $ocs; - - /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ - private $l; - - protected function setUp() { + private string $appName = 'files_sharing'; + private string $currentUser; + + private ShareAPIController $ocs; + + private IManager&MockObject $shareManager; + private IGroupManager&MockObject $groupManager; + private IUserManager&MockObject $userManager; + private IRequest&MockObject $request; + private IRootFolder&MockObject $rootFolder; + private IURLGenerator&MockObject $urlGenerator; + private IL10N&MockObject $l; + private IConfig&MockObject $config; + private IAppConfig&MockObject $appConfig; + private IAppManager&MockObject $appManager; + private ContainerInterface&MockObject $serverContainer; + private IUserStatusManager&MockObject $userStatusManager; + private IPreview&MockObject $previewManager; + private IDateTimeZone&MockObject $dateTimeZone; + private LoggerInterface&MockObject $logger; + private IProviderFactory&MockObject $factory; + private IMailer&MockObject $mailer; + private ITagManager&MockObject $tagManager; + private TrustedServers&MockObject $trustedServers; + + protected function setUp(): void { $this->shareManager = $this->createMock(IManager::class); $this->shareManager ->expects($this->any()) ->method('shareApiEnabled') ->willReturn(true); + $this->shareManager + ->expects($this->any()) + ->method('shareProviderExists')->willReturn(true); $this->groupManager = $this->createMock(IGroupManager::class); $this->userManager = $this->createMock(IUserManager::class); $this->request = $this->createMock(IRequest::class); @@ -99,9 +103,25 @@ class ShareAPIControllerTest extends TestCase { $this->l = $this->createMock(IL10N::class); $this->l->method('t') - ->will($this->returnCallback(function($text, $parameters = []) { + ->willReturnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); - })); + }); + $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->appManager = $this->createMock(IAppManager::class); + $this->serverContainer = $this->createMock(ContainerInterface::class); + $this->userStatusManager = $this->createMock(IUserStatusManager::class); + $this->previewManager = $this->createMock(IPreview::class); + $this->previewManager->method('isAvailable') + ->willReturnCallback(function ($fileInfo) { + return $fileInfo->getMimeType() === 'mimeWithPreview'; + }); + $this->dateTimeZone = $this->createMock(IDateTimeZone::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->factory = $this->createMock(IProviderFactory::class); + $this->mailer = $this->createMock(IMailer::class); + $this->tagManager = $this->createMock(ITagManager::class); + $this->trustedServers = $this->createMock(TrustedServers::class); $this->ocs = new ShareAPIController( $this->appName, @@ -111,13 +131,26 @@ class ShareAPIControllerTest extends TestCase { $this->userManager, $this->rootFolder, $this->urlGenerator, - $this->currentUser, - $this->l + $this->l, + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser ); + } /** - * @return ShareAPIController|\PHPUnit_Framework_MockObject_MockObject + * @return ShareAPIController&MockObject */ private function mockFormatShare() { return $this->getMockBuilder(ShareAPIController::class) @@ -129,38 +162,67 @@ class ShareAPIControllerTest extends TestCase { $this->userManager, $this->rootFolder, $this->urlGenerator, - $this->currentUser, $this->l, - ])->setMethods(['formatShare']) + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser, + ])->onlyMethods(['formatShare']) ->getMock(); } private function newShare() { - return \OC::$server->getShareManager()->newShare(); + return Server::get(IManager::class)->newShare(); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Wrong share ID, share doesn't exist - */ - public function testDeleteShareShareNotFound() { + + private function mockShareAttributes() { + $formattedShareAttributes = [ + [ + 'scope' => 'permissions', + 'key' => 'download', + 'value' => true + ] + ]; + + $shareAttributes = $this->createMock(IShareAttributes::class); + $shareAttributes->method('toArray')->willReturn($formattedShareAttributes); + $shareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(true); + + // send both IShare attributes class and expected json string + return [$shareAttributes, \json_encode($formattedShareAttributes)]; + } + + public function testDeleteShareShareNotFound(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Wrong share ID, share does not exist'); + $this->shareManager - ->expects($this->exactly(2)) + ->expects($this->exactly(7)) ->method('getShareById') - ->will($this->returnCallback(function($id) { - if ($id === 'ocinternal:42' || $id === 'ocFederatedSharing:42') { - throw new \OCP\Share\Exceptions\ShareNotFound(); + ->willReturnCallback(function ($id): void { + if ($id === 'ocinternal:42' || $id === 'ocRoomShare:42' || $id === 'ocFederatedSharing:42' || $id === 'ocCircleShare:42' || $id === 'ocMailShare:42' || $id === 'deck:42' || $id === 'sciencemesh:42') { + throw new ShareNotFound(); } else { throw new \Exception(); } - })); + }); $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true); $this->ocs->deleteShare(42); } - public function testDeleteShare() { + public function testDeleteShare(): void { $node = $this->getMockBuilder(File::class)->getMock(); $share = $this->newShare(); @@ -178,7 +240,7 @@ class ShareAPIControllerTest extends TestCase { $node->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $expected = new DataResponse(); $result = $this->ocs->deleteShare(42); @@ -187,34 +249,323 @@ class ShareAPIControllerTest extends TestCase { $this->assertEquals($expected->getData(), $result->getData()); } + + public function testDeleteShareLocked(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Could not delete share'); + + $node = $this->getMockBuilder(File::class)->getMock(); + $node->method('getId')->willReturn(1); + + $share = $this->newShare(); + $share->setNode($node); + + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with($share->getNodeId()) + ->willReturn([$node]); + + $this->shareManager + ->expects($this->once()) + ->method('getShareById') + ->with('ocinternal:42') + ->willReturn($share); + + $this->shareManager + ->expects($this->never()) + ->method('deleteShare') + ->with($share); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED) + ->willThrowException(new LockedException('mypath')); + + $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share])); + $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share])); + + $this->ocs->deleteShare(42); + } + + /** + * You can always remove a share that was shared with you + */ + public function testDeleteShareWithMe(): void { + $node = $this->getMockBuilder(File::class)->getMock(); + + $share = $this->newShare(); + $share->setSharedWith($this->currentUser) + ->setShareType(IShare::TYPE_USER) + ->setNode($node); + + $this->shareManager + ->expects($this->once()) + ->method('getShareById') + ->with('ocinternal:42') + ->willReturn($share); + + $this->shareManager + ->expects($this->once()) + ->method('deleteShare') + ->with($share); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share])); + $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share])); + + $this->ocs->deleteShare(42); + } + /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage could not delete share + * You can always delete a share you own */ - public function testDeleteShareLocked() { + public function testDeleteShareOwner(): void { $node = $this->getMockBuilder(File::class)->getMock(); $share = $this->newShare(); $share->setSharedBy($this->currentUser) ->setNode($node); + $this->shareManager ->expects($this->once()) ->method('getShareById') ->with('ocinternal:42') ->willReturn($share); + $this->shareManager - ->expects($this->never()) + ->expects($this->once()) ->method('deleteShare') ->with($share); $node->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED) - ->will($this->throwException(new LockedException('mypath'))); + ->with(ILockingProvider::LOCK_SHARED); + + $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share])); + $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share])); + + $this->ocs->deleteShare(42); + } + + /** + * You can always delete a share when you own + * the file path it belong to + */ + public function testDeleteShareFileOwner(): void { + $node = $this->getMockBuilder(File::class)->getMock(); + $node->method('getId')->willReturn(1); + + $share = $this->newShare(); + $share->setShareOwner($this->currentUser) + ->setNode($node); + + $this->shareManager + ->expects($this->once()) + ->method('getShareById') + ->with('ocinternal:42') + ->willReturn($share); + + $this->shareManager + ->expects($this->once()) + ->method('deleteShare') + ->with($share); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteFromSelf', [$share])); + $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShare', [$share])); + + $this->ocs->deleteShare(42); + } + + /** + * You can remove (the mountpoint, not the share) + * a share if you're in the group the share is shared with + */ + public function testDeleteSharedWithMyGroup(): void { + $node = $this->getMockBuilder(File::class)->getMock(); + $node->method('getId')->willReturn(1); + + $share = $this->newShare(); + $share->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('group') + ->setNode($node); + + $this->shareManager + ->expects($this->once()) + ->method('getShareById') + ->with('ocinternal:42') + ->willReturn($share); + + // canDeleteShareFromSelf + $user = $this->createMock(IUser::class); + $group = $this->getMockBuilder(IGroup::class)->getMock(); + $this->groupManager + ->method('get') + ->with('group') + ->willReturn($group); + $this->userManager + ->method('get') + ->with($this->currentUser) + ->willReturn($user); + $group->method('inGroup') + ->with($user) + ->willReturn(true); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with($share->getNodeId()) + ->willReturn([$share->getNode()]); + + $this->shareManager->expects($this->once()) + ->method('deleteFromSelf') + ->with($share, $this->currentUser); + + $this->shareManager->expects($this->never()) + ->method('deleteShare'); + + $this->assertTrue($this->invokePrivate($this->ocs, 'canDeleteShareFromSelf', [$share])); + $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share])); + + $this->ocs->deleteShare(42); + } + + /** + * You cannot remove a share if you're not + * in the group the share is shared with + */ + public function testDeleteSharedWithGroupIDontBelongTo(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Wrong share ID, share does not exist'); + + $node = $this->getMockBuilder(File::class)->getMock(); + $node->method('getId')->willReturn(42); + + $share = $this->newShare(); + $share->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('group') + ->setNode($node); + + $this->shareManager + ->expects($this->once()) + ->method('getShareById') + ->with('ocinternal:42') + ->willReturn($share); + + // canDeleteShareFromSelf + $user = $this->createMock(IUser::class); + $group = $this->getMockBuilder(IGroup::class)->getMock(); + $this->groupManager + ->method('get') + ->with('group') + ->willReturn($group); + $this->userManager + ->method('get') + ->with($this->currentUser) + ->willReturn($user); + $group->method('inGroup') + ->with($user) + ->willReturn(false); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with($share->getNodeId()) + ->willReturn([$share->getNode()]); + + $this->shareManager->expects($this->never()) + ->method('deleteFromSelf'); + + $this->shareManager->expects($this->never()) + ->method('deleteShare'); + + $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShareFromSelf', [$share])); + $this->assertFalse($this->invokePrivate($this->ocs, 'canDeleteShare', [$share])); $this->ocs->deleteShare(42); } + public function testDeleteShareOwnerless(): void { + $ocs = $this->mockFormatShare(); + + $mount = $this->createMock(IShareOwnerlessMount::class); + + $file = $this->createMock(File::class); + $file + ->expects($this->exactly(2)) + ->method('getPermissions') + ->willReturn(Constants::PERMISSION_SHARE); + $file + ->expects($this->once()) + ->method('getMountPoint') + ->willReturn($mount); + + $userFolder = $this->createMock(Folder::class); + $userFolder->method('getById') + ->with(2) + ->willReturn([$file]); + $userFolder->method('getFirstNodeById') + ->with(2) + ->willReturn($file); + + $this->rootFolder + ->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $share = $this->createMock(IShare::class); + $share + ->expects($this->once()) + ->method('getNode') + ->willReturn($file); + $share + ->expects($this->exactly(2)) + ->method('getNodeId') + ->willReturn(2); + $share + ->expects($this->exactly(2)) + ->method('getPermissions') + ->willReturn(Constants::PERMISSION_SHARE); + + $this->shareManager + ->expects($this->once()) + ->method('getShareById') + ->with('ocinternal:1', $this->currentUser) + ->willReturn($share); + + $this->shareManager + ->expects($this->once()) + ->method('deleteShare') + ->with($share); + + $result = $ocs->deleteShare(1); + $this->assertInstanceOf(DataResponse::class, $result); + } + /* * FIXME: Enable once we have a federated Share Provider @@ -222,17 +573,17 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager ->expects($this->once()) ->method('getShareById') - ->with('ocinternal:42') + ->with('ocinternal:42', 'currentUser') ->will($this->throwException(new \OC\Share20\Exception\ShareNotFound())); - $expected = new \OC\OCS\Result(null, 404, 'wrong share ID, share doesn\'t exist.'); + $expected = new \OC\OCS\Result(null, 404, 'wrong share ID, share does not exist.'); $this->assertEquals($expected, $this->ocs->getShare(42)); } */ public function createShare($id, $shareType, $sharedWith, $sharedBy, $shareOwner, $path, $permissions, - $shareTime, $expiration, $parent, $target, $mail_send, $token=null, - $password=null) { + $shareTime, $expiration, $parent, $target, $mail_send, $note = '', $token = null, + $password = null, $label = '', $attributes = null) { $share = $this->getMockBuilder(IShare::class)->getMock(); $share->method('getId')->willReturn($id); $share->method('getShareType')->willReturn($shareType); @@ -241,6 +592,9 @@ class ShareAPIControllerTest extends TestCase { $share->method('getShareOwner')->willReturn($shareOwner); $share->method('getNode')->willReturn($path); $share->method('getPermissions')->willReturn($permissions); + $share->method('getNote')->willReturn($note); + $share->method('getLabel')->willReturn($label); + $share->method('getAttributes')->willReturn($attributes); $time = new \DateTime(); $time->setTimestamp($shareTime); $share->method('getShareTime')->willReturn($time); @@ -250,10 +604,10 @@ class ShareAPIControllerTest extends TestCase { $share->method('getToken')->willReturn($token); $share->method('getPassword')->willReturn($password); - if ($shareType === \OCP\Share::SHARE_TYPE_USER || - $shareType === \OCP\Share::SHARE_TYPE_GROUP || - $shareType === \OCP\Share::SHARE_TYPE_LINK) { - $share->method('getFullId')->willReturn('ocinternal:'.$id); + if ($shareType === IShare::TYPE_USER + || $shareType === IShare::TYPE_GROUP + || $shareType === IShare::TYPE_LINK) { + $share->method('getFullId')->willReturn('ocinternal:' . $id); } return $share; @@ -267,33 +621,43 @@ class ShareAPIControllerTest extends TestCase { ->getMock(); $cache->method('getNumericStorageId')->willReturn(101); - $storage = $this->getMockBuilder(Storage::class) + $storage = $this->getMockBuilder(IStorage::class) ->disableOriginalConstructor() ->getMock(); $storage->method('getId')->willReturn('STORAGE'); $storage->method('getCache')->willReturn($cache); - $parentFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); + $parentFolder = $this->getMockBuilder(Folder::class)->getMock(); $parentFolder->method('getId')->willReturn(3); + $mountPoint = $this->createMock(IMountPoint::class); + $mountPoint->method('getMountType')->willReturn(''); $file = $this->getMockBuilder('OCP\Files\File')->getMock(); $file->method('getId')->willReturn(1); $file->method('getPath')->willReturn('file'); $file->method('getStorage')->willReturn($storage); $file->method('getParent')->willReturn($parentFolder); + $file->method('getSize')->willReturn(123465); + $file->method('getMTime')->willReturn(1234567890); $file->method('getMimeType')->willReturn('myMimeType'); + $file->method('getMountPoint')->willReturn($mountPoint); - $folder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); + $folder = $this->getMockBuilder(Folder::class)->getMock(); $folder->method('getId')->willReturn(2); $folder->method('getPath')->willReturn('folder'); $folder->method('getStorage')->willReturn($storage); $folder->method('getParent')->willReturn($parentFolder); + $folder->method('getSize')->willReturn(123465); + $folder->method('getMTime')->willReturn(1234567890); $folder->method('getMimeType')->willReturn('myFolderMimeType'); + $folder->method('getMountPoint')->willReturn($mountPoint); + + [$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes(); // File shared with user $share = $this->createShare( 100, - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, 'userId', 'initiatorId', 'ownerId', @@ -303,13 +667,16 @@ class ShareAPIControllerTest extends TestCase { null, 6, 'target', - 0 + 0, + 'personal note', + $shareAttributes, ); $expected = [ 'id' => 100, - 'share_type' => \OCP\Share::SHARE_TYPE_USER, + 'share_type' => IShare::TYPE_USER, 'share_with' => 'userId', 'share_with_displayname' => 'userDisplay', + 'share_with_displayname_unique' => 'userId@example.com', 'uid_owner' => 'initiatorId', 'displayname_owner' => 'initiatorDisplay', 'item_type' => 'file', @@ -320,6 +687,7 @@ class ShareAPIControllerTest extends TestCase { 'token' => null, 'expiration' => null, 'permissions' => 4, + 'attributes' => $shareAttributesReturnJson, 'stime' => 5, 'parent' => null, 'storage_id' => 'STORAGE', @@ -327,15 +695,27 @@ class ShareAPIControllerTest extends TestCase { 'storage' => 101, 'mail_send' => 0, 'uid_file_owner' => 'ownerId', + 'note' => 'personal note', + 'label' => '', 'displayname_file_owner' => 'ownerDisplay', 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123465, + 'item_mtime' => 1234567890, + 'attributes' => null, + 'item_permissions' => 4, + 'is-mount-root' => false, + 'mount-type' => '', ]; $data[] = [$share, $expected]; // Folder shared with group $share = $this->createShare( 101, - \OCP\Share::SHARE_TYPE_GROUP, + IShare::TYPE_GROUP, 'groupId', 'initiatorId', 'ownerId', @@ -345,11 +725,13 @@ class ShareAPIControllerTest extends TestCase { null, 6, 'target', - 0 + 0, + 'personal note', + $shareAttributes, ); $expected = [ 'id' => 101, - 'share_type' => \OCP\Share::SHARE_TYPE_GROUP, + 'share_type' => IShare::TYPE_GROUP, 'share_with' => 'groupId', 'share_with_displayname' => 'groupId', 'uid_owner' => 'initiatorId', @@ -362,6 +744,7 @@ class ShareAPIControllerTest extends TestCase { 'token' => null, 'expiration' => null, 'permissions' => 4, + 'attributes' => $shareAttributesReturnJson, 'stime' => 5, 'parent' => null, 'storage_id' => 'STORAGE', @@ -369,8 +752,20 @@ class ShareAPIControllerTest extends TestCase { 'storage' => 101, 'mail_send' => 0, 'uid_file_owner' => 'ownerId', + 'note' => 'personal note', + 'label' => '', 'displayname_file_owner' => 'ownerDisplay', 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123465, + 'item_mtime' => 1234567890, + 'attributes' => null, + 'item_permissions' => 4, + 'is-mount-root' => false, + 'mount-type' => '', ]; $data[] = [$share, $expected]; @@ -378,7 +773,7 @@ class ShareAPIControllerTest extends TestCase { $expire = \DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-02 01:02:03'); $share = $this->createShare( 101, - \OCP\Share::SHARE_TYPE_LINK, + IShare::TYPE_LINK, null, 'initiatorId', 'ownerId', @@ -389,14 +784,18 @@ class ShareAPIControllerTest extends TestCase { 6, 'target', 0, + 'personal note', 'token', - 'password' + 'password', + 'first link share' ); $expected = [ 'id' => 101, - 'share_type' => \OCP\Share::SHARE_TYPE_LINK, + 'share_type' => IShare::TYPE_LINK, + 'password' => 'password', 'share_with' => 'password', - 'share_with_displayname' => 'password', + 'share_with_displayname' => '(Shared link)', + 'send_password_by_talk' => false, 'uid_owner' => 'initiatorId', 'displayname_owner' => 'initiatorDisplay', 'item_type' => 'folder', @@ -407,6 +806,7 @@ class ShareAPIControllerTest extends TestCase { 'token' => 'token', 'expiration' => '2000-01-02 00:00:00', 'permissions' => 4, + 'attributes' => null, 'stime' => 5, 'parent' => null, 'storage_id' => 'STORAGE', @@ -415,51 +815,77 @@ class ShareAPIControllerTest extends TestCase { 'mail_send' => 0, 'url' => 'url', 'uid_file_owner' => 'ownerId', + 'note' => 'personal note', + 'label' => 'first link share', 'displayname_file_owner' => 'ownerDisplay', 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123465, + 'item_mtime' => 1234567890, + 'attributes' => null, + 'item_permissions' => 4, + 'is-mount-root' => false, + 'mount-type' => '', ]; $data[] = [$share, $expected]; return $data; } - /** - * @dataProvider dataGetShare - */ - public function testGetShare(\OCP\Share\IShare $share, array $result) { - /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetShare')] + public function testGetShare(IShare $share, array $result): void { + /** @var ShareAPIController&MockObject $ocs */ $ocs = $this->getMockBuilder(ShareAPIController::class) - ->setConstructorArgs([ - $this->appName, - $this->request, - $this->shareManager, - $this->groupManager, - $this->userManager, - $this->rootFolder, - $this->urlGenerator, - $this->currentUser, - $this->l, - ])->setMethods(['canAccessShare']) - ->getMock(); + ->setConstructorArgs([ + $this->appName, + $this->request, + $this->shareManager, + $this->groupManager, + $this->userManager, + $this->rootFolder, + $this->urlGenerator, + $this->l, + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser, + ]) + ->onlyMethods(['canAccessShare']) + ->getMock(); $ocs->expects($this->any()) ->method('canAccessShare') ->willReturn(true); $this->shareManager - ->expects($this->once()) + ->expects($this->any()) ->method('getShareById') - ->with($share->getFullId()) + ->with($share->getFullId(), 'currentUser') ->willReturn($share); - $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); $userFolder ->method('getRelativePath') - ->will($this->returnArgument(0)); + ->willReturnArgument(0); $userFolder->method('getById') ->with($share->getNodeId()) ->willReturn([$share->getNode()]); + $userFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($share->getNode()); $this->rootFolder->method('getUserFolder') ->with($this->currentUser) @@ -480,28 +906,31 @@ class ShareAPIControllerTest extends TestCase { $user = $this->getMockBuilder(IUser::class)->getMock(); $user->method('getUID')->willReturn('userId'); $user->method('getDisplayName')->willReturn('userDisplay'); + $user->method('getSystemEMailAddress')->willReturn('userId@example.com'); - $group = $this->getMockBuilder('OCP\IGroup')->getMock(); + $group = $this->getMockBuilder(IGroup::class)->getMock(); $group->method('getGID')->willReturn('groupId'); - $this->userManager->method('get')->will($this->returnValueMap([ + $this->userManager->method('get')->willReturnMap([ ['userId', $user], ['initiatorId', $initiator], ['ownerId', $owner], - ])); - $this->groupManager->method('get')->will($this->returnValueMap([ + ]); + $this->groupManager->method('get')->willReturnMap([ ['group', $group], - ])); + ]); + $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC')); - $this->assertEquals($result, $ocs->getShare($share->getId())->getData()[0]); + $data = $ocs->getShare($share->getId())->getData()[0]; + $this->assertEquals($result, $data); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Wrong share ID, share doesn't exist - */ - public function testGetShareInvalidNode() { - $share = \OC::$server->getShareManager()->newShare(); + + public function testGetShareInvalidNode(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Wrong share ID, share does not exist'); + + $share = Server::get(IManager::class)->newShare(); $share->setSharedBy('initiator') ->setSharedWith('recipient') ->setShareOwner('owner'); @@ -509,81 +938,827 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager ->expects($this->once()) ->method('getShareById') - ->with('ocinternal:42') + ->with('ocinternal:42', 'currentUser') ->willReturn($share); + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + $this->ocs->getShare(42); } - public function testCanAccessShare() { - $share = $this->getMockBuilder(IShare::class)->getMock(); + public function dataGetShares() { + $folder = $this->getMockBuilder(Folder::class)->getMock(); + $file1 = $this->getMockBuilder(File::class)->getMock(); + $file1->method('getName') + ->willReturn('file1'); + $file2 = $this->getMockBuilder(File::class)->getMock(); + $file2->method('getName') + ->willReturn('file2'); + + $folder->method('getDirectoryListing') + ->willReturn([$file1, $file2]); + + $file1UserShareOwner = Server::get(IManager::class)->newShare(); + $file1UserShareOwner->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(4); + + $file1UserShareOwnerExpected = [ + 'id' => 4, + 'share_type' => IShare::TYPE_USER, + ]; + + $file1UserShareInitiator = Server::get(IManager::class)->newShare(); + $file1UserShareInitiator->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('currentUser') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(8); + + $file1UserShareInitiatorExpected = [ + 'id' => 8, + 'share_type' => IShare::TYPE_USER, + ]; + + $file1UserShareRecipient = Server::get(IManager::class)->newShare(); + $file1UserShareRecipient->setShareType(IShare::TYPE_USER) + ->setSharedWith('currentUser') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(15); + + $file1UserShareRecipientExpected = [ + 'id' => 15, + 'share_type' => IShare::TYPE_USER, + ]; + + $file1UserShareOther = Server::get(IManager::class)->newShare(); + $file1UserShareOther->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(16); + + $file1UserShareOtherExpected = [ + 'id' => 16, + 'share_type' => IShare::TYPE_USER, + ]; + + $file1GroupShareOwner = Server::get(IManager::class)->newShare(); + $file1GroupShareOwner->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(23); + + $file1GroupShareOwnerExpected = [ + 'id' => 23, + 'share_type' => IShare::TYPE_GROUP, + ]; + + $file1GroupShareRecipient = Server::get(IManager::class)->newShare(); + $file1GroupShareRecipient->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('currentUserGroup') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(42); + + $file1GroupShareRecipientExpected = [ + 'id' => 42, + 'share_type' => IShare::TYPE_GROUP, + ]; + + $file1GroupShareOther = Server::get(IManager::class)->newShare(); + $file1GroupShareOther->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(108); + + $file1LinkShareOwner = Server::get(IManager::class)->newShare(); + $file1LinkShareOwner->setShareType(IShare::TYPE_LINK) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(415); + + $file1LinkShareOwnerExpected = [ + 'id' => 415, + 'share_type' => IShare::TYPE_LINK, + ]; + + $file1EmailShareOwner = Server::get(IManager::class)->newShare(); + $file1EmailShareOwner->setShareType(IShare::TYPE_EMAIL) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(416); + + $file1EmailShareOwnerExpected = [ + 'id' => 416, + 'share_type' => IShare::TYPE_EMAIL, + ]; + + $file1CircleShareOwner = Server::get(IManager::class)->newShare(); + $file1CircleShareOwner->setShareType(IShare::TYPE_CIRCLE) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(423); + + $file1CircleShareOwnerExpected = [ + 'id' => 423, + 'share_type' => IShare::TYPE_CIRCLE, + ]; + + $file1RoomShareOwner = Server::get(IManager::class)->newShare(); + $file1RoomShareOwner->setShareType(IShare::TYPE_ROOM) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(442); + + $file1RoomShareOwnerExpected = [ + 'id' => 442, + 'share_type' => IShare::TYPE_ROOM, + ]; + + $file1RemoteShareOwner = Server::get(IManager::class)->newShare(); + $file1RemoteShareOwner->setShareType(IShare::TYPE_REMOTE) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setExpirationDate(new \DateTime('2000-01-01T01:02:03')) + ->setNode($file1) + ->setId(815); + + $file1RemoteShareOwnerExpected = [ + 'id' => 815, + 'share_type' => IShare::TYPE_REMOTE, + ]; + + $file1RemoteGroupShareOwner = Server::get(IManager::class)->newShare(); + $file1RemoteGroupShareOwner->setShareType(IShare::TYPE_REMOTE_GROUP) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setExpirationDate(new \DateTime('2000-01-02T01:02:03')) + ->setNode($file1) + ->setId(816); + + $file1RemoteGroupShareOwnerExpected = [ + 'id' => 816, + 'share_type' => IShare::TYPE_REMOTE_GROUP, + ]; + + $file2UserShareOwner = Server::get(IManager::class)->newShare(); + $file2UserShareOwner->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file2) + ->setId(823); + + $file2UserShareOwnerExpected = [ + 'id' => 823, + 'share_type' => IShare::TYPE_USER, + ]; + + $data = [ + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1UserShareInitiatorExpected, + $file1UserShareOtherExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther], + ], + ], + [ + ], + [ + $file1UserShareInitiatorExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareRecipient], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareRecipientExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareOwner], + IShare::TYPE_LINK => [$file1LinkShareOwner], + IShare::TYPE_EMAIL => [$file1EmailShareOwner], + IShare::TYPE_CIRCLE => [$file1CircleShareOwner], + IShare::TYPE_ROOM => [$file1RoomShareOwner], + IShare::TYPE_REMOTE => [$file1RemoteShareOwner], + IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareOwnerExpected, + $file1LinkShareOwnerExpected, + $file1EmailShareOwnerExpected, + $file1CircleShareOwnerExpected, + $file1RoomShareOwnerExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareOwner], + IShare::TYPE_LINK => [$file1LinkShareOwner], + IShare::TYPE_EMAIL => [$file1EmailShareOwner], + IShare::TYPE_CIRCLE => [$file1CircleShareOwner], + IShare::TYPE_ROOM => [$file1RoomShareOwner], + IShare::TYPE_REMOTE => [$file1RemoteShareOwner], + IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner], + ], + ], + [ + IShare::TYPE_REMOTE => true, + IShare::TYPE_REMOTE_GROUP => true, + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareOwnerExpected, + $file1LinkShareOwnerExpected, + $file1EmailShareOwnerExpected, + $file1CircleShareOwnerExpected, + $file1RoomShareOwnerExpected, + $file1RemoteShareOwnerExpected, + $file1RemoteGroupShareOwnerExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + ], + 'file2' => [ + IShare::TYPE_USER => [$file2UserShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file2UserShareOwnerExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther], + ], + 'file2' => [ + IShare::TYPE_USER => [$file2UserShareOwner], + ], + ], + [ + ], + [ + $file1UserShareInitiatorExpected, + $file1UserShareOtherExpected, + $file2UserShareOwnerExpected, + ] + ], + // This might not happen in a real environment, as the combination + // of shares does not seem to be possible on a folder without + // resharing rights; if the folder has resharing rights then the + // share with others would be included too in the results. + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther], + ], + ], + [ + ], + [ + $file1UserShareInitiatorExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareRecipient], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareRecipientExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareOwner], + IShare::TYPE_LINK => [$file1LinkShareOwner], + IShare::TYPE_EMAIL => [$file1EmailShareOwner], + IShare::TYPE_CIRCLE => [$file1CircleShareOwner], + IShare::TYPE_ROOM => [$file1RoomShareOwner], + IShare::TYPE_REMOTE => [$file1RemoteShareOwner], + IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareOwnerExpected, + $file1LinkShareOwnerExpected, + $file1EmailShareOwnerExpected, + $file1CircleShareOwnerExpected, + $file1RoomShareOwnerExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareOwner], + IShare::TYPE_LINK => [$file1LinkShareOwner], + IShare::TYPE_EMAIL => [$file1EmailShareOwner], + IShare::TYPE_CIRCLE => [$file1CircleShareOwner], + IShare::TYPE_ROOM => [$file1RoomShareOwner], + IShare::TYPE_REMOTE => [$file1RemoteShareOwner], + IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner], + ], + ], + [ + IShare::TYPE_REMOTE => true, + IShare::TYPE_REMOTE_GROUP => true, + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareOwnerExpected, + $file1LinkShareOwnerExpected, + $file1EmailShareOwnerExpected, + $file1CircleShareOwnerExpected, + $file1RoomShareOwnerExpected, + $file1RemoteShareOwnerExpected, + $file1RemoteGroupShareOwnerExpected, + ] + ], + ]; + + return $data; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetShares')] + public function testGetShares(array $getSharesParameters, array $shares, array $extraShareTypes, array $expected): void { + /** @var ShareAPIController&MockObject $ocs */ + $ocs = $this->getMockBuilder(ShareAPIController::class) + ->setConstructorArgs([ + $this->appName, + $this->request, + $this->shareManager, + $this->groupManager, + $this->userManager, + $this->rootFolder, + $this->urlGenerator, + $this->l, + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser, + ]) + ->onlyMethods(['formatShare']) + ->getMock(); + + $ocs->method('formatShare') + ->willReturnCallback( + function ($share) { + return [ + 'id' => $share->getId(), + 'share_type' => $share->getShareType() + ]; + } + ); + + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $userFolder->method('get') + ->with('path') + ->willReturn($getSharesParameters['path']); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $this->shareManager + ->method('getSharesBy') + ->willReturnCallback( + function ($user, $shareType, $node) use ($shares) { + if (!isset($shares[$node->getName()]) || !isset($shares[$node->getName()][$shareType])) { + return []; + } + return $shares[$node->getName()][$shareType]; + } + ); + + $this->shareManager + ->method('outgoingServer2ServerSharesAllowed') + ->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE] ?? false); + + $this->shareManager + ->method('outgoingServer2ServerGroupSharesAllowed') + ->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE_GROUP] ?? false); + + $this->groupManager + ->method('isInGroup') + ->willReturnCallback( + function ($user, $group) { + return $group === 'currentUserGroup'; + } + ); + + $result = $ocs->getShares( + $getSharesParameters['sharedWithMe'] ?? 'false', + $getSharesParameters['reshares'] ?? 'false', + $getSharesParameters['subfiles'] ?? 'false', + 'path' + ); + + $this->assertEquals($expected, $result->getData()); + } + + public function testCanAccessShareAsOwner(): void { + $share = $this->createMock(IShare::class); $share->method('getShareOwner')->willReturn($this->currentUser); $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } - $share = $this->getMockBuilder(IShare::class)->getMock(); + public function testCanAccessShareAsSharer(): void { + $share = $this->createMock(IShare::class); $share->method('getSharedBy')->willReturn($this->currentUser); $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } - $share = $this->getMockBuilder(IShare::class)->getMock(); - $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER); + public function testCanAccessShareAsSharee(): void { + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_USER); $share->method('getSharedWith')->willReturn($this->currentUser); $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } + + public function testCannotAccessLinkShare(): void { + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_LINK); + $share->method('getNodeId')->willReturn(42); + + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); - $share = $this->getMockBuilder(IShare::class)->getMock(); - $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_USER); - $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock()); $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } - $share = $this->getMockBuilder(IShare::class)->getMock(); - $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP); - $share->method('getSharedWith')->willReturn('group'); + #[\PHPUnit\Framework\Attributes\DataProvider('dataCanAccessShareWithPermissions')] + public function testCanAccessShareWithPermissions(int $permissions, bool $expected): void { + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_USER); + $share->method('getSharedWith')->willReturn($this->createMock(IUser::class)); + $share->method('getNodeId')->willReturn(42); + + $file = $this->createMock(File::class); + + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $userFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($file); + $userFolder->method('getById') + ->with($share->getNodeId()) + ->willReturn([$file]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $file->method('getPermissions') + ->willReturn($permissions); + + if ($expected) { + $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } else { + $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } + } + + public static function dataCanAccessShareWithPermissions(): array { + return [ + [Constants::PERMISSION_SHARE, true], + [Constants::PERMISSION_READ, false], + [Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, true], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataCanAccessShareAsGroupMember')] + public function testCanAccessShareAsGroupMember(string $group, bool $expected): void { + $share = $this->createMock(IShare::class); + $share->method('getShareType')->willReturn(IShare::TYPE_GROUP); + $share->method('getSharedWith')->willReturn($group); + $share->method('getNodeId')->willReturn(42); + + $file = $this->createMock(File::class); + + $userFolder = $this->createMock(Folder::class); + $userFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($file); + $userFolder->method('getById') + ->with($share->getNodeId()) + ->willReturn([$file]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); $user = $this->createMock(IUser::class); $this->userManager->method('get') ->with($this->currentUser) ->willReturn($user); - $group = $this->getMockBuilder('OCP\IGroup')->getMock(); + $group = $this->createMock(IGroup::class); $group->method('inGroup')->with($user)->willReturn(true); - $group2 = $this->getMockBuilder('OCP\IGroup')->getMock(); + $group2 = $this->createMock(IGroup::class); $group2->method('inGroup')->with($user)->willReturn(false); - $this->groupManager->method('get')->will($this->returnValueMap([ + $this->groupManager->method('get')->willReturnMap([ ['group', $group], ['group2', $group2], - ['groupnull', null], - ])); - $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + ['group-null', null], + ]); - $share = $this->createMock(IShare::class); - $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP); - $share->method('getSharedWith')->willReturn('group2'); - $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + if ($expected) { + $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } else { + $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } + } - // null group - $share = $this->createMock(IShare::class); - $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP); - $share->method('getSharedWith')->willReturn('groupnull'); - $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + public static function dataCanAccessShareAsGroupMember(): array { + return [ + ['group', true], + ['group2', false], + ['group-null', false], + ]; + } + + public function dataCanAccessRoomShare() { + $result = []; $share = $this->createMock(IShare::class); - $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK); - $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + $share->method('getShareType')->willReturn(IShare::TYPE_ROOM); + $share->method('getSharedWith')->willReturn('recipientRoom'); + + $result[] = [ + false, $share, false, false + ]; + + $result[] = [ + false, $share, false, true + ]; + + $result[] = [ + true, $share, true, true + ]; + + $result[] = [ + false, $share, true, false + ]; + + return $result; } /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Please specify a file or folder path + * + * @param bool $expects + * @param IShare $share + * @param bool helperAvailable + * @param bool canAccessShareByHelper */ - public function testCreateShareNoPath() { + #[\PHPUnit\Framework\Attributes\DataProvider('dataCanAccessRoomShare')] + public function testCanAccessRoomShare(bool $expected, IShare $share, bool $helperAvailable, bool $canAccessShareByHelper): void { + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with($share->getNodeId()) + ->willReturn([$share->getNode()]); + + if (!$helperAvailable) { + $this->appManager->method('isEnabledForUser') + ->with('spreed') + ->willReturn(false); + } else { + $this->appManager->method('isEnabledForUser') + ->with('spreed') + ->willReturn(true); + + // This is not possible anymore with PHPUnit 10+ + // as `setMethods` was removed and now real reflection is used, thus the class needs to exist. + // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + $helper = $this->getMockBuilder(\stdClass::class) + ->addMethods(['canAccessShare']) + ->getMock(); + $helper->method('canAccessShare') + ->with($share, $this->currentUser) + ->willReturn($canAccessShareByHelper); + + $this->serverContainer->method('get') + ->with('\OCA\Talk\Share\Helper\ShareAPIController') + ->willReturn($helper); + } + + $this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share])); + } + + + public function testCreateShareNoPath(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Please specify a file or folder path'); + $this->ocs->createShare(); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Wrong path, file/folder doesn't exist - */ - public function testCreateShareInvalidPath() { + + public function testCreateShareInvalidPath(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Wrong path, file/folder does not exist'); + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); $this->rootFolder->expects($this->once()) ->method('getUserFolder') @@ -593,109 +1768,98 @@ class ShareAPIControllerTest extends TestCase { $userFolder->expects($this->once()) ->method('get') ->with('invalid-path') - ->will($this->throwException(new \OCP\Files\NotFoundException())); + ->willThrowException(new NotFoundException()); $this->ocs->createShare('invalid-path'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage invalid permissions - */ - public function testCreateShareInvalidPermissions() { + public function testCreateShareInvalidShareType(): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Unknown share type'); + $share = $this->newShare(); $this->shareManager->method('newShare')->willReturn($share); - $userFolder = $this->getMockBuilder(Folder::class)->getMock(); - $this->rootFolder->expects($this->once()) - ->method('getUserFolder') - ->with('currentUser') - ->willReturn($userFolder); + [$userFolder, $file] = $this->getNonSharedUserFile(); + $this->rootFolder->expects($this->atLeastOnce()) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); - $path = $this->getMockBuilder(File::class)->getMock(); - $userFolder->expects($this->once()) - ->method('get') - ->with('valid-path') - ->willReturn($path); + $userFolder->expects($this->atLeastOnce()) + ->method('get') + ->with('valid-path') + ->willReturn($file); + $userFolder->method('getById') + ->willReturn([]); - $path->expects($this->once()) + $file->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); - $this->ocs->createShare('valid-path', 32); + $this->ocs->createShare('valid-path', 31); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Please specify a valid user - */ - public function testCreateShareUserNoShareWith() { + public function testCreateShareUserNoShareWith(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Please specify a valid account to share with'); + $share = $this->newShare(); $this->shareManager->method('newShare')->willReturn($share); - $userFolder = $this->getMockBuilder(Folder::class)->getMock(); - $this->rootFolder->expects($this->once()) - ->method('getUserFolder') + [$userFolder, $path] = $this->getNonSharedUserFile(); + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); - $path = $this->getMockBuilder(File::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); - $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); - $path->method('getStorage')->willReturn($storage); $userFolder->expects($this->once()) ->method('get') ->with('valid-path') ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); $path->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); - $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER); + $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Please specify a valid user - */ - public function testCreateShareUserNoValidShareWith() { + + public function testCreateShareUserNoValidShareWith(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Please specify a valid account to share with'); + $share = $this->newShare(); $this->shareManager->method('newShare')->willReturn($share); - $userFolder = $this->getMockBuilder(Folder::class)->getMock(); - $this->rootFolder->expects($this->once()) - ->method('getUserFolder') + [$userFolder, $path] = $this->getNonSharedUserFile(); + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); - $path = $this->getMockBuilder(File::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); - $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); - $path->method('getStorage')->willReturn($storage); $userFolder->expects($this->once()) ->method('get') ->with('valid-path') ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); $path->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->userManager->method('userExists') ->with('invalidUser') ->willReturn(false); - $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'invalidUser'); + $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'invalidUser'); } - public function testCreateShareUser() { + public function testCreateShareUser(): void { $share = $this->newShare(); $this->shareManager->method('newShare')->willReturn($share); - /** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */ + /** @var ShareAPIController $ocs */ $ocs = $this->getMockBuilder(ShareAPIController::class) ->setConstructorArgs([ $this->appName, @@ -705,94 +1869,97 @@ class ShareAPIControllerTest extends TestCase { $this->userManager, $this->rootFolder, $this->urlGenerator, - $this->currentUser, $this->l, - ])->setMethods(['formatShare']) + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser, + ])->onlyMethods(['formatShare']) ->getMock(); - $userFolder = $this->getMockBuilder(Folder::class)->getMock(); - $this->rootFolder->expects($this->once()) - ->method('getUserFolder') - ->with('currentUser') - ->willReturn($userFolder); + [$userFolder, $path] = $this->getNonSharedUserFile(); + $this->rootFolder->expects($this->exactly(2)) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); - $path = $this->getMockBuilder(File::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); - $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); - $path->method('getStorage')->willReturn($storage); $userFolder->expects($this->once()) - ->method('get') - ->with('valid-path') - ->willReturn($path); + ->method('get') + ->with('valid-path') + ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); $this->userManager->method('userExists')->with('validUser')->willReturn(true); $path->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->shareManager->method('createShare') - ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getPermissions() === ( - \OCP\Constants::PERMISSION_ALL & - ~\OCP\Constants::PERMISSION_DELETE & - ~\OCP\Constants::PERMISSION_CREATE - ) && - $share->getShareType() === \OCP\Share::SHARE_TYPE_USER && - $share->getSharedWith() === 'validUser' && - $share->getSharedBy() === 'currentUser'; + ->with($this->callback(function (IShare $share) use ($path) { + return $share->getNode() === $path + && $share->getPermissions() === ( + Constants::PERMISSION_ALL + & ~Constants::PERMISSION_DELETE + & ~Constants::PERMISSION_CREATE + ) + && $share->getShareType() === IShare::TYPE_USER + && $share->getSharedWith() === 'validUser' + && $share->getSharedBy() === 'currentUser'; })) - ->will($this->returnArgument(0)); + ->willReturnArgument(0); - $expected = new DataResponse(null); - $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'validUser'); + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser'); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Please specify a valid group - */ - public function testCreateShareGroupNoValidShareWith() { + + public function testCreateShareGroupNoValidShareWith(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Please specify a valid group'); + $share = $this->newShare(); $this->shareManager->method('newShare')->willReturn($share); - $this->shareManager->method('createShare')->will($this->returnArgument(0)); + $this->shareManager->method('createShare')->willReturnArgument(0); $this->shareManager->method('allowGroupSharing')->willReturn(true); - $userFolder = $this->getMockBuilder(Folder::class)->getMock(); - $this->rootFolder->expects($this->once()) - ->method('getUserFolder') - ->with('currentUser') - ->willReturn($userFolder); + [$userFolder, $path] = $this->getNonSharedUserFile(); + $this->rootFolder->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); - $path = $this->getMockBuilder(File::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); - $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); - $path->method('getStorage')->willReturn($storage); $userFolder->expects($this->once()) - ->method('get') - ->with('valid-path') - ->willReturn($path); + ->method('get') + ->with('valid-path') + ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); $path->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); - $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'invalidGroup'); + $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup'); } - public function testCreateShareGroup() { + public function testCreateShareGroup(): void { $share = $this->newShare(); $this->shareManager->method('newShare')->willReturn($share); - /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */ + /** @var ShareAPIController&MockObject $ocs */ $ocs = $this->getMockBuilder(ShareAPIController::class) ->setConstructorArgs([ $this->appName, @@ -802,36 +1969,44 @@ class ShareAPIControllerTest extends TestCase { $this->userManager, $this->rootFolder, $this->urlGenerator, - $this->currentUser, $this->l, - ])->setMethods(['formatShare']) + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser, + ])->onlyMethods(['formatShare']) ->getMock(); $this->request ->method('getParam') - ->will($this->returnValueMap([ + ->willReturnMap([ ['path', null, 'valid-path'], - ['permissions', null, \OCP\Constants::PERMISSION_ALL], - ['shareType', '-1', \OCP\Share::SHARE_TYPE_GROUP], + ['permissions', null, Constants::PERMISSION_ALL], + ['shareType', '-1', IShare::TYPE_GROUP], ['shareWith', null, 'validGroup'], - ])); + ]); - $userFolder = $this->getMockBuilder(Folder::class)->getMock(); - $this->rootFolder->expects($this->once()) + [$userFolder, $path] = $this->getNonSharedUserFolder(); + $this->rootFolder->expects($this->exactly(2)) ->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); - $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); - $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); - $path->method('getStorage')->willReturn($storage); $userFolder->expects($this->once()) ->method('get') ->with('valid-path') ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true); @@ -841,49 +2016,44 @@ class ShareAPIControllerTest extends TestCase { $path->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->shareManager->method('createShare') - ->with($this->callback(function (\OCP\Share\IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && - $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP && - $share->getSharedWith() === 'validGroup' && - $share->getSharedBy() === 'currentUser'; + ->with($this->callback(function (IShare $share) use ($path) { + return $share->getNode() === $path + && $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getShareType() === IShare::TYPE_GROUP + && $share->getSharedWith() === 'validGroup' + && $share->getSharedBy() === 'currentUser'; })) - ->will($this->returnArgument(0)); + ->willReturnArgument(0); - $expected = new DataResponse(null); - $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'validGroup'); + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'validGroup'); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Group sharing is disabled by the administrator - */ - public function testCreateShareGroupNotAllowed() { + + public function testCreateShareGroupNotAllowed(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Group sharing is disabled by the administrator'); + $share = $this->newShare(); $this->shareManager->method('newShare')->willReturn($share); - $userFolder = $this->getMockBuilder(Folder::class)->getMock(); - $this->rootFolder->expects($this->once()) - ->method('getUserFolder') + [$userFolder, $path] = $this->getNonSharedUserFolder(); + $this->rootFolder->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); - $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); - $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); - $path->method('getStorage')->willReturn($storage); $userFolder->expects($this->once()) ->method('get') ->with('valid-path') ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); $this->groupManager->method('groupExists')->with('validGroup')->willReturn(true); @@ -891,223 +2061,646 @@ class ShareAPIControllerTest extends TestCase { ->method('allowGroupSharing') ->willReturn(false); - $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_GROUP, 'invalidGroup'); + $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_GROUP, 'invalidGroup'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Public link sharing is disabled by the administrator - */ - public function testCreateShareLinkNoLinksAllowed() { + + public function testCreateShareLinkNoLinksAllowed(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Public link sharing is disabled by the administrator'); + $this->request ->method('getParam') - ->will($this->returnValueMap([ + ->willReturnMap([ ['path', null, 'valid-path'], - ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK], - ])); + ['shareType', '-1', IShare::TYPE_LINK], + ]); $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + $path->method('getId')->willReturn(42); + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); $path->method('getStorage')->willReturn($storage); - $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf()); + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); $this->rootFolder->method('get')->with('valid-path')->willReturn($path); + $this->rootFolder->method('getById') + ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); + $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); + $this->shareManager->method('shareApiAllowLinks')->willReturn(false); - $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK); + $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException - * @expectedExceptionMessage Public upload disabled by the administrator - */ - public function testCreateShareLinkNoPublicUpload() { + + public function testCreateShareLinkNoPublicUpload(): void { + $this->expectException(OCSForbiddenException::class); + $this->expectExceptionMessage('Public upload disabled by the administrator'); + $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + $path->method('getId')->willReturn(42); + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); $path->method('getStorage')->willReturn($storage); - $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf()); + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); $this->rootFolder->method('get')->with('valid-path')->willReturn($path); + $this->rootFolder->method('getById') + ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); - $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true'); + $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Public upload is only possible for publicly shared folders - */ - public function testCreateShareLinkPublicUploadFile() { - $path = $this->getMockBuilder(File::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + + public function testCreateShareLinkPublicUploadFile(): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Public upload is only possible for publicly shared folders'); + + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); + + $file = $this->createMock(File::class); + $file->method('getId')->willReturn(42); + $file->method('getStorage')->willReturn($storage); + + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); + $this->rootFolder->method('get')->with('valid-path')->willReturn($file); + $this->rootFolder->method('getById') + ->willReturn([]); + + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); + $this->shareManager->method('shareApiAllowLinks')->willReturn(true); + $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); + + $this->ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true'); + } + + public function testCreateShareLinkPublicUploadFolder(): void { + $ocs = $this->mockFormatShare(); + + $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(1); + $storage = $this->createMock(IStorage::class); + $storage->method('instanceOfStorage') + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); $path->method('getStorage')->willReturn($storage); - $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf()); + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); $this->rootFolder->method('get')->with('valid-path')->willReturn($path); + $this->rootFolder->method('getById') + ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $this->ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true'); + $this->shareManager->expects($this->once())->method('createShare')->with( + $this->callback(function (IShare $share) use ($path) { + return $share->getNode() === $path + && $share->getShareType() === IShare::TYPE_LINK + && $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getSharedBy() === 'currentUser' + && $share->getPassword() === null + && $share->getExpirationDate() === null; + }) + )->willReturnArgument(0); + + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', '', null, ''); + + $this->assertInstanceOf(get_class($expected), $result); + $this->assertEquals($expected->getData(), $result->getData()); } - public function testCreateShareLinkPublicUploadFolder() { + public function testCreateShareLinkPassword(): void { $ocs = $this->mockFormatShare(); $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + $path->method('getId')->willReturn(42); + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); $path->method('getStorage')->willReturn($storage); - $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf()); + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); $this->rootFolder->method('get')->with('valid-path')->willReturn($path); + $this->rootFolder->method('getById') + ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); $this->shareManager->expects($this->once())->method('createShare')->with( - $this->callback(function (\OCP\Share\IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && - $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) && - $share->getSharedBy() === 'currentUser' && - $share->getPassword() === null && - $share->getExpirationDate() === null; + $this->callback(function (IShare $share) use ($path) { + return $share->getNode() === $path + && $share->getShareType() === IShare::TYPE_LINK + && $share->getPermissions() === Constants::PERMISSION_READ // publicUpload was set to false + && $share->getSharedBy() === 'currentUser' + && $share->getPassword() === 'password' + && $share->getExpirationDate() === null; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); - $expected = new DataResponse(null); - $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'true', '', ''); + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', Constants::PERMISSION_READ, IShare::TYPE_LINK, null, 'false', 'password', null, ''); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testCreateShareLinkPassword() { + public function testCreateShareLinkSendPasswordByTalk(): void { $ocs = $this->mockFormatShare(); $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + $path->method('getId')->willReturn(42); + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); $path->method('getStorage')->willReturn($storage); - $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf()); + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); $this->rootFolder->method('get')->with('valid-path')->willReturn($path); + $this->rootFolder->method('getById') + ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); + $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true); + $this->shareManager->expects($this->once())->method('createShare')->with( - $this->callback(function (\OCP\Share\IShare $share) use ($path) { - return $share->getNode() === $path && - $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && - $share->getPermissions() === \OCP\Constants::PERMISSION_READ && - $share->getSharedBy() === 'currentUser' && - $share->getPassword() === 'password' && - $share->getExpirationDate() === null; + $this->callback(function (IShare $share) use ($path) { + return $share->getNode() === $path + && $share->getShareType() === IShare::TYPE_LINK + && $share->getPermissions() === (Constants::PERMISSION_ALL & ~(Constants::PERMISSION_SHARE)) + && $share->getSharedBy() === 'currentUser' + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === null; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); - $expected = new DataResponse(null); - $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', 'password', ''); + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'true', 'password', 'true', ''); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testCreateShareValidExpireDate() { + + public function testCreateShareLinkSendPasswordByTalkWithTalkDisabled(): void { + $this->expectException(OCSForbiddenException::class); + $this->expectExceptionMessage('Sharing valid-path sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled'); + + $ocs = $this->mockFormatShare(); + + $path = $this->getMockBuilder(Folder::class)->getMock(); + $path->method('getId')->willReturn(42); + $storage = $this->createMock(IStorage::class); + $storage->method('instanceOfStorage') + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); + $path->method('getStorage')->willReturn($storage); + $path->method('getPath')->willReturn('valid-path'); + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); + $this->rootFolder->method('get')->with('valid-path')->willReturn($path); + $this->rootFolder->method('getById') + ->willReturn([]); + + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); + $this->shareManager->method('shareApiAllowLinks')->willReturn(true); + $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); + + $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false); + + $this->shareManager->expects($this->never())->method('createShare'); + + $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', 'password', 'true', ''); + } + + public function testCreateShareValidExpireDate(): void { $ocs = $this->mockFormatShare(); $this->request ->method('getParam') - ->will($this->returnValueMap([ + ->willReturnMap([ ['path', null, 'valid-path'], - ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK], + ['shareType', '-1', IShare::TYPE_LINK], ['publicUpload', null, 'false'], ['expireDate', '', '2000-01-01'], ['password', '', ''], - ])); + ]); $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + $path->method('getId')->willReturn(42); + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); $path->method('getStorage')->willReturn($storage); - $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf()); + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); $this->rootFolder->method('get')->with('valid-path')->willReturn($path); + $this->rootFolder->method('getById') + ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); $this->shareManager->expects($this->once())->method('createShare')->with( - $this->callback(function (\OCP\Share\IShare $share) use ($path) { + $this->callback(function (IShare $share) use ($path) { $date = new \DateTime('2000-01-01'); - $date->setTime(0,0,0); - - return $share->getNode() === $path && - $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && - $share->getPermissions() === \OCP\Constants::PERMISSION_READ && - $share->getSharedBy() === 'currentUser' && - $share->getPassword() === null && - $share->getExpirationDate() == $date; + $date->setTime(0, 0, 0); + + return $share->getNode() === $path + && $share->getShareType() === IShare::TYPE_LINK + && $share->getPermissions() === Constants::PERMISSION_READ | Constants::PERMISSION_SHARE + && $share->getSharedBy() === 'currentUser' + && $share->getPassword() === null + && $share->getExpirationDate() == $date; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); - $expected = new DataResponse(null); - $result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', '2000-01-01'); + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', null, IShare::TYPE_LINK, null, 'false', '', null, '2000-01-01'); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Invalid date, date format must be YYYY-MM-DD - */ - public function testCreateShareInvalidExpireDate() { + + public function testCreateShareInvalidExpireDate(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD'); + $ocs = $this->mockFormatShare(); $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + $path->method('getId')->willReturn(42); + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(false); + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); $path->method('getStorage')->willReturn($storage); - $this->rootFolder->method('getUserFolder')->with($this->currentUser)->will($this->returnSelf()); + $this->rootFolder->method('getUserFolder')->with($this->currentUser)->willReturnSelf(); $this->rootFolder->method('get')->with('valid-path')->willReturn($path); + $this->rootFolder->method('getById') + ->willReturn([]); - $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); + $this->shareManager->method('newShare')->willReturn(Server::get(IManager::class)->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_LINK, null, 'false', '', 'a1b2d3'); + $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, 'a1b2d3'); + } + + public function testCreateShareRemote(): void { + $share = $this->newShare(); + $this->shareManager->method('newShare')->willReturn($share); + + /** @var ShareAPIController $ocs */ + $ocs = $this->getMockBuilder(ShareAPIController::class) + ->setConstructorArgs([ + $this->appName, + $this->request, + $this->shareManager, + $this->groupManager, + $this->userManager, + $this->rootFolder, + $this->urlGenerator, + $this->l, + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser, + ])->onlyMethods(['formatShare']) + ->getMock(); + + [$userFolder, $path] = $this->getNonSharedUserFile(); + $this->rootFolder->expects($this->exactly(2)) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); + + $this->userManager->method('userExists')->with('validUser')->willReturn(true); + + $path->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->shareManager->method('createShare') + ->with($this->callback(function (IShare $share) use ($path) { + return $share->getNode() === $path + && $share->getPermissions() === ( + Constants::PERMISSION_ALL + & ~Constants::PERMISSION_DELETE + & ~Constants::PERMISSION_CREATE + ) + && $share->getShareType() === IShare::TYPE_REMOTE + && $share->getSharedWith() === 'user@example.org' + && $share->getSharedBy() === 'currentUser'; + })) + ->willReturnArgument(0); + + $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true); + + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE, 'user@example.org'); + + $this->assertInstanceOf(get_class($expected), $result); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareRemoteGroup(): void { + $share = $this->newShare(); + $this->shareManager->method('newShare')->willReturn($share); + + /** @var ShareAPIController $ocs */ + $ocs = $this->getMockBuilder(ShareAPIController::class) + ->setConstructorArgs([ + $this->appName, + $this->request, + $this->shareManager, + $this->groupManager, + $this->userManager, + $this->rootFolder, + $this->urlGenerator, + $this->l, + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser, + ])->onlyMethods(['formatShare']) + ->getMock(); + + [$userFolder, $path] = $this->getNonSharedUserFile(); + $this->rootFolder->expects($this->exactly(2)) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); + + $this->userManager->method('userExists')->with('validUser')->willReturn(true); + + $path->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->shareManager->method('createShare') + ->with($this->callback(function (IShare $share) use ($path) { + return $share->getNode() === $path + && $share->getPermissions() === ( + Constants::PERMISSION_ALL + & ~Constants::PERMISSION_DELETE + & ~Constants::PERMISSION_CREATE + ) + && $share->getShareType() === IShare::TYPE_REMOTE_GROUP + && $share->getSharedWith() === 'group@example.org' + && $share->getSharedBy() === 'currentUser'; + })) + ->willReturnArgument(0); + + $this->shareManager->method('outgoingServer2ServerGroupSharesAllowed')->willReturn(true); + + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_REMOTE_GROUP, 'group@example.org'); + + $this->assertInstanceOf(get_class($expected), $result); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testCreateShareRoom(): void { + $ocs = $this->mockFormatShare(); + + $share = $this->newShare(); + $this->shareManager->method('newShare')->willReturn($share); + + [$userFolder, $path] = $this->getNonSharedUserFile(); + $this->rootFolder->expects($this->exactly(2)) + ->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); + + $path->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->appManager->method('isEnabledForUser') + ->with('spreed') + ->willReturn(true); + + // This is not possible anymore with PHPUnit 10+ + // as `setMethods` was removed and now real reflection is used, thus the class needs to exist. + // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + $helper = $this->getMockBuilder(\stdClass::class) + ->addMethods(['createShare']) + ->getMock(); + $helper->method('createShare') + ->with( + $share, + 'recipientRoom', + Constants::PERMISSION_ALL + & ~Constants::PERMISSION_DELETE + & ~Constants::PERMISSION_CREATE, + '' + )->willReturnCallback( + function ($share): void { + $share->setSharedWith('recipientRoom'); + $share->setPermissions(Constants::PERMISSION_ALL); + } + ); + + $this->serverContainer->method('get') + ->with('\OCA\Talk\Share\Helper\ShareAPIController') + ->willReturn($helper); + + $this->shareManager->method('createShare') + ->with($this->callback(function (IShare $share) use ($path) { + return $share->getNode() === $path + && $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getShareType() === IShare::TYPE_ROOM + && $share->getSharedWith() === 'recipientRoom' + && $share->getSharedBy() === 'currentUser'; + })) + ->willReturnArgument(0); + + $expected = new DataResponse([]); + $result = $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom'); + + $this->assertInstanceOf(get_class($expected), $result); + $this->assertEquals($expected->getData(), $result->getData()); + } + + + public function testCreateShareRoomHelperNotAvailable(): void { + $this->expectException(OCSForbiddenException::class); + $this->expectExceptionMessage('Sharing valid-path failed because the back end does not support room shares'); + + $ocs = $this->mockFormatShare(); + + $share = $this->newShare(); + $this->shareManager->method('newShare')->willReturn($share); + + [$userFolder, $path] = $this->getNonSharedUserFolder(); + $this->rootFolder->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $path->method('getPath')->willReturn('valid-path'); + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); + + $path->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->appManager->method('isEnabledForUser') + ->with('spreed') + ->willReturn(false); + + $this->shareManager->expects($this->never())->method('createShare'); + + $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom'); + } + + + public function testCreateShareRoomHelperThrowException(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Exception thrown by the helper'); + + $ocs = $this->mockFormatShare(); + + $share = $this->newShare(); + $share->setSharedBy('currentUser'); + $this->shareManager->method('newShare')->willReturn($share); + + [$userFolder, $path] = $this->getNonSharedUserFile(); + $this->rootFolder->method('getUserFolder') + ->with('currentUser') + ->willReturn($userFolder); + + $userFolder->expects($this->once()) + ->method('get') + ->with('valid-path') + ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); + + $path->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->appManager->method('isEnabledForUser') + ->with('spreed') + ->willReturn(true); + + // This is not possible anymore with PHPUnit 10+ + // as `setMethods` was removed and now real reflection is used, thus the class needs to exist. + // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + $helper = $this->getMockBuilder(\stdClass::class) + ->addMethods(['createShare']) + ->getMock(); + $helper->method('createShare') + ->with( + $share, + 'recipientRoom', + Constants::PERMISSION_ALL & ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE), + '' + )->willReturnCallback( + function ($share): void { + throw new OCSNotFoundException('Exception thrown by the helper'); + } + ); + + $this->serverContainer->method('get') + ->with('\OCA\Talk\Share\Helper\ShareAPIController') + ->willReturn($helper); + + $this->shareManager->expects($this->never())->method('createShare'); + + $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom'); } /** * Test for https://github.com/owncloud/core/issues/22587 * TODO: Remove once proper solution is in place */ - public function testCreateReshareOfFederatedMountNoDeletePermissions() { - $share = \OC::$server->getShareManager()->newShare(); + public function testCreateReshareOfFederatedMountNoDeletePermissions(): void { + $share = Server::get(IManager::class)->newShare(); $this->shareManager->method('newShare')->willReturn($share); - /** @var ShareAPIController|\PHPUnit_Framework_MockObject_MockObject $ocs */ + /** @var ShareAPIController&MockObject $ocs */ $ocs = $this->getMockBuilder(ShareAPIController::class) ->setConstructorArgs([ $this->appName, @@ -1117,186 +2710,260 @@ class ShareAPIControllerTest extends TestCase { $this->userManager, $this->rootFolder, $this->urlGenerator, - $this->currentUser, $this->l, - ])->setMethods(['formatShare']) + $this->config, + $this->appConfig, + $this->appManager, + $this->serverContainer, + $this->userStatusManager, + $this->previewManager, + $this->dateTimeZone, + $this->logger, + $this->factory, + $this->mailer, + $this->tagManager, + $this->trustedServers, + $this->currentUser, + ])->onlyMethods(['formatShare']) ->getMock(); $userFolder = $this->getMockBuilder(Folder::class)->getMock(); - $this->rootFolder->expects($this->once()) + $this->rootFolder->expects($this->exactly(2)) ->method('getUserFolder') ->with('currentUser') ->willReturn($userFolder); $path = $this->getMockBuilder(Folder::class)->getMock(); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + $path->method('getId')->willReturn(42); + + $storage = $this->createMock(IStorage::class); $storage->method('instanceOfStorage') - ->with('OCA\Files_Sharing\External\Storage') - ->willReturn(true); + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', true], + ['OCA\Files_Sharing\SharedStorage', false], + ]); + $userFolder->method('getStorage')->willReturn($storage); $path->method('getStorage')->willReturn($storage); - $path->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ); + + $path->method('getPermissions')->willReturn(Constants::PERMISSION_READ); $userFolder->expects($this->once()) ->method('get') ->with('valid-path') ->willReturn($path); + $userFolder->method('getById') + ->willReturn([]); $this->userManager->method('userExists')->with('validUser')->willReturn(true); $this->shareManager ->expects($this->once()) ->method('createShare') - ->with($this->callback(function (\OCP\Share\IShare $share) { - return $share->getPermissions() === \OCP\Constants::PERMISSION_READ; + ->with($this->callback(function (IShare $share) { + return $share->getPermissions() === Constants::PERMISSION_READ; })) - ->will($this->returnArgument(0)); + ->willReturnArgument(0); - $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, \OCP\Share::SHARE_TYPE_USER, 'validUser'); + $ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_USER, 'validUser'); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSNotFoundException - * @expectedExceptionMessage Wrong share ID, share doesn't exist - */ - public function testUpdateShareCantAccess() { - $node = $this->getMockBuilder(Folder::class)->getMock(); + + public function testUpdateShareCantAccess(): void { + $this->expectException(OCSNotFoundException::class); + $this->expectExceptionMessage('Wrong share ID, share does not exist'); + + [$userFolder, $node] = $this->getNonSharedUserFolder(); $share = $this->newShare(); $share->setNode($node); $node->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with($share->getNodeId()) + ->willReturn([$share->getNode()]); + $this->ocs->updateShare(42); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException - * @expectedExceptionMessage Wrong or no update parameter given - */ - public function testUpdateNoParametersLink() { + + public function testUpdateNoParametersLink(): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Wrong or no update parameter given'); + $node = $this->getMockBuilder(Folder::class)->getMock(); $share = $this->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setNode($node); $node->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->ocs->updateShare(42); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException - * @expectedExceptionMessage Wrong or no update parameter given - */ - public function testUpdateNoParametersOther() { + + public function testUpdateNoParametersOther(): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Wrong or no update parameter given'); + $node = $this->getMockBuilder(Folder::class)->getMock(); $share = $this->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_GROUP) + ->setShareType(IShare::TYPE_GROUP) ->setNode($node); $node->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->ocs->updateShare(42); } - public function testUpdateLinkShareClear() { + public function testUpdateLinkShareClear(): void { $ocs = $this->mockFormatShare(); - $node = $this->getMockBuilder(Folder::class)->getMock(); + [$userFolder, $node] = $this->getNonSharedUserFolder(); + $node->method('getId') + ->willReturn(42); $share = $this->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPassword('password') ->setExpirationDate(new \DateTime()) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) ->setNode($node); $node->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) { - return $share->getPermissions() === \OCP\Constants::PERMISSION_READ && - $share->getPassword() === null && - $share->getExpirationDate() === null; + $this->callback(function (IShare $share) { + return $share->getPermissions() === Constants::PERMISSION_READ + && $share->getPassword() === null + && $share->getExpirationDate() === null + // Once set a note or a label are never back to null, only to an + // empty string. + && $share->getNote() === '' + && $share->getLabel() === '' + && $share->getHideDownload() === false; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); $this->shareManager->method('getSharedWith') ->willReturn([]); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, null, '', 'false', ''); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$node]); + $userFolder->method('getFirstNodeById') + ->with(42) + ->willReturn($node); + + $mountPoint = $this->createMock(IMountPoint::class); + $node->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, null, '', null, 'false', '', '', '', 'false'); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testUpdateLinkShareSet() { + public function testUpdateLinkShareSet(): void { $ocs = $this->mockFormatShare(); - $folder = $this->getMockBuilder(Folder::class)->getMock(); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $folder->method('getId') + ->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setNode($folder); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) { + $this->callback(function (IShare $share) { $date = new \DateTime('2000-01-01'); - $date->setTime(0,0,0); - - return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getExpirationDate() == $date; + $date->setTime(0, 0, 0); + + return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getPassword() === 'password' + && $share->getExpirationDate() == $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); $this->shareManager->method('getSharedWith') ->willReturn([]); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, null, 'password', 'true', '2000-01-01'); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + + $mountPoint = $this->createMock(IMountPoint::class); + $folder->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-01', 'note', 'label', 'true'); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - /** - * @dataProvider publicUploadParamsProvider - */ - public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password) { + #[\PHPUnit\Framework\Attributes\DataProvider('publicUploadParamsProvider')] + public function testUpdateLinkShareEnablePublicUpload($permissions, $publicUpload, $expireDate, $password): void { $ocs = $this->mockFormatShare(); - $folder = $this->getMockBuilder(Folder::class)->getMock(); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $folder->method('getId') + ->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPassword('password') ->setNode($folder); @@ -1305,452 +2972,988 @@ class ShareAPIControllerTest extends TestCase { $this->shareManager->method('getSharedWith')->willReturn([]); $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) { - return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getExpirationDate() === null; + $this->callback(function (IShare $share) { + return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getPassword() === 'password' + && $share->getExpirationDate() === null; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, $permissions, $password, $publicUpload, $expireDate); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + + $mountPoint = $this->createMock(IMountPoint::class); + $folder->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException - * @expectedExceptionMessage Invalid date. Format must be YYYY-MM-DD - */ - public function testUpdateLinkShareInvalidDate() { + + public static function publicLinkValidPermissionsProvider() { + return [ + [Constants::PERMISSION_CREATE], + [Constants::PERMISSION_READ], + [Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE], + [Constants::PERMISSION_READ | Constants::PERMISSION_DELETE], + [Constants::PERMISSION_READ | Constants::PERMISSION_CREATE], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('publicLinkValidPermissionsProvider')] + public function testUpdateLinkShareSetCRUDPermissions($permissions): void { $ocs = $this->mockFormatShare(); - $folder = $this->getMockBuilder(Folder::class)->getMock(); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $folder->method('getId') + ->willReturn(42); + + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) + ->setSharedBy($this->currentUser) + ->setShareType(IShare::TYPE_LINK) + ->setPassword('password') + ->setNode($folder); + + $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); + $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); + $this->shareManager->method('getSharedWith')->willReturn([]); + + $this->shareManager + ->expects($this->any()) + ->method('updateShare') + ->willReturnArgument(0); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + + $mountPoint = $this->createMock(IMountPoint::class); + $folder->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, $permissions, 'password', null, null, null); + + $this->assertInstanceOf(get_class($expected), $result); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public static function publicLinkInvalidPermissionsProvider1() { + return [ + [Constants::PERMISSION_DELETE], + [Constants::PERMISSION_UPDATE], + [Constants::PERMISSION_SHARE], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('publicLinkInvalidPermissionsProvider1')] + public function testUpdateLinkShareSetInvalidCRUDPermissions1($permissions): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Share must at least have READ or CREATE permissions'); + + $this->testUpdateLinkShareSetCRUDPermissions($permissions, null); + } + + public static function publicLinkInvalidPermissionsProvider2() { + return [ + [Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE], + [Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE], + ]; + } - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + #[\PHPUnit\Framework\Attributes\DataProvider('publicLinkInvalidPermissionsProvider2')] + public function testUpdateLinkShareSetInvalidCRUDPermissions2($permissions): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Share must have READ permission if UPDATE or DELETE permission is set'); + + $this->testUpdateLinkShareSetCRUDPermissions($permissions); + } + + public function testUpdateLinkShareInvalidDate(): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Invalid date. Format must be YYYY-MM-DD'); + + $ocs = $this->mockFormatShare(); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $folder->method('getId') + ->willReturn(42); + + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setNode($folder); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $ocs->updateShare(42, null, 'password', 'true', '2000-01-a'); + $ocs->updateShare(42, null, 'password', null, 'true', '2000-01-a'); } - public function publicUploadParamsProvider() { + public static function publicUploadParamsProvider() { return [ [null, 'true', null, 'password'], // legacy had no delete [ - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE, - null, null, 'password' + Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE, + 'true', null, 'password' ], // correct [ - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE, + Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE, null, null, 'password' ], ]; } - /** - * @dataProvider publicUploadParamsProvider - * @expectedException \OCP\AppFramework\OCS\OCSForbiddenException - * @expectedExceptionMessage Public upload disabled by the administrator - */ - public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password) { + #[\PHPUnit\Framework\Attributes\DataProvider('publicUploadParamsProvider')] + public function testUpdateLinkSharePublicUploadNotAllowed($permissions, $publicUpload, $expireDate, $password): void { + $this->expectException(OCSForbiddenException::class); + $this->expectExceptionMessage('Public upload disabled by the administrator'); + $ocs = $this->mockFormatShare(); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); - $folder = $this->getMockBuilder(Folder::class)->getMock(); + $folder->method('getId')->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setNode($folder); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false); - $ocs->updateShare(42, $permissions, $password, $publicUpload, $expireDate); + $ocs->updateShare(42, $permissions, $password, null, $publicUpload, $expireDate); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException - * @expectedExceptionMessage Public upload is only possible for publicly shared folders - */ - public function testUpdateLinkSharePublicUploadOnFile() { + + public function testUpdateLinkSharePublicUploadOnFile(): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Public upload is only possible for publicly shared folders'); + $ocs = $this->mockFormatShare(); $file = $this->getMockBuilder(File::class)->getMock(); + $file->method('getId') + ->willReturn(42); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setNode($file); + $this->shareManager + ->method('getShareById') + ->with('ocinternal:42') + ->willReturn($share); + $this->shareManager + ->method('shareApiLinkAllowPublicUpload') + ->willReturn(true); + $this->shareManager + ->method('updateShare') + ->with($share) + ->willThrowException(new \InvalidArgumentException('File shares cannot have create or delete permissions')); + + $ocs->updateShare(42, null, 'password', null, 'true', ''); + } + + public function testUpdateLinkSharePasswordDoesNotChangeOther(): void { + $ocs = $this->mockFormatShare(); + + $date = new \DateTime('2000-01-01'); + $date->setTime(0, 0, 0); + + [$userFolder, $node] = $this->getNonSharedUserFolder(); + $node->method('getId')->willReturn(42); + $userFolder->method('getById') + ->with(42) + ->willReturn([$node]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + $share = $this->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) + ->setSharedBy($this->currentUser) + ->setShareType(IShare::TYPE_LINK) + ->setPassword('password') + ->setSendPasswordByTalk(true) + ->setExpirationDate($date) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) + ->setNode($node); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); - $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $ocs->updateShare(42, null, 'password', 'true', ''); + $this->shareManager->expects($this->once())->method('updateShare')->with( + $this->callback(function (IShare $share) use ($date) { + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'newpassword' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; + }) + )->willReturnArgument(0); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, null, 'newpassword', null, null, null, null, null, null); + + $this->assertInstanceOf(get_class($expected), $result); + $this->assertEquals($expected->getData(), $result->getData()); } - public function testUpdateLinkSharePasswordDoesNotChangeOther() { + public function testUpdateLinkShareSendPasswordByTalkDoesNotChangeOther(): void { $ocs = $this->mockFormatShare(); $date = new \DateTime('2000-01-01'); - $date->setTime(0,0,0); + $date->setTime(0, 0, 0); - $node = $this->getMockBuilder(File::class)->getMock(); + [$userFolder, $node] = $this->getNonSharedUserFolder(); + $userFolder->method('getById') + ->with(42) + ->willReturn([$node]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + $node->method('getId')->willReturn(42); $share = $this->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPassword('password') + ->setSendPasswordByTalk(false) ->setExpirationDate($date) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) ->setNode($node); $node->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); + $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true); + $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) use ($date) { - return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && - $share->getPassword() === 'newpassword' && - $share->getExpirationDate() === $date; + $this->callback(function (IShare $share) use ($date) { + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, null, 'newpassword', null, null); + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, null, null, 'true', null, null, null, null, null); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testUpdateLinkShareExpireDateDoesNotChangeOther() { + + public function testUpdateLinkShareSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void { + $this->expectException(OCSForbiddenException::class); + $this->expectExceptionMessage('"Sending the password by Nextcloud Talk" for sharing a file or folder failed because Nextcloud Talk is not enabled.'); + $ocs = $this->mockFormatShare(); - $node = $this->getMockBuilder(File::class)->getMock(); + $date = new \DateTime('2000-01-01'); + $date->setTime(0, 0, 0); + + [$userFolder, $node] = $this->getNonSharedUserFolder(); + $userFolder->method('getById') + ->with(42) + ->willReturn([$node]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + $node->method('getId')->willReturn(42); + $share = $this->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) + ->setSharedBy($this->currentUser) + ->setShareType(IShare::TYPE_LINK) + ->setPassword('password') + ->setSendPasswordByTalk(false) + ->setExpirationDate($date) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) + ->setNode($node); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); + + $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false); + + $this->shareManager->expects($this->never())->method('updateShare'); + + $ocs->updateShare(42, null, null, 'true', null, null, null, null, null); + } + + public function testUpdateLinkShareDoNotSendPasswordByTalkDoesNotChangeOther(): void { + $ocs = $this->mockFormatShare(); + + $date = new \DateTime('2000-01-01'); + $date->setTime(0, 0, 0); + + [$userFolder, $node] = $this->getNonSharedUserFolder(); + $userFolder->method('getById') + ->with(42) + ->willReturn([$node]); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + $node->method('getId')->willReturn(42); + $share = $this->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) + ->setSharedBy($this->currentUser) + ->setShareType(IShare::TYPE_LINK) + ->setPassword('password') + ->setSendPasswordByTalk(true) + ->setExpirationDate($date) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) + ->setNode($node); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); + + $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(true); + + $this->shareManager->expects($this->once())->method('updateShare')->with( + $this->callback(function (IShare $share) use ($date) { + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === false + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; + }) + )->willReturnArgument(0); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null); + + $this->assertInstanceOf(get_class($expected), $result); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testUpdateLinkShareDoNotSendPasswordByTalkWithTalkDisabledDoesNotChangeOther(): void { + $ocs = $this->mockFormatShare(); + + $date = new \DateTime('2000-01-01'); + $date->setTime(0, 0, 0); + + [$userFolder, $node] = $this->getNonSharedUserFolder(); + $node->method('getId') + ->willReturn(42); + + $share = $this->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) + ->setSharedBy($this->currentUser) + ->setShareType(IShare::TYPE_LINK) + ->setPassword('password') + ->setSendPasswordByTalk(true) + ->setExpirationDate($date) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) + ->setNode($node); + + $node->expects($this->once()) + ->method('lock') + ->with(ILockingProvider::LOCK_SHARED); + + $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); + + $this->appManager->method('isEnabledForUser')->with('spreed')->willReturn(false); + + $this->shareManager->expects($this->once())->method('updateShare')->with( + $this->callback(function (IShare $share) use ($date) { + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === false + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; + }) + )->willReturnArgument(0); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$node]); + + $mountPoint = $this->createMock(IMountPoint::class); + $node->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $mountPoint = $this->createMock(IMountPoint::class); + $node->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, null, null, 'false', null, null, null, null, null); + + $this->assertInstanceOf(get_class($expected), $result); + $this->assertEquals($expected->getData(), $result->getData()); + } + + public function testUpdateLinkShareExpireDateDoesNotChangeOther(): void { + $ocs = $this->mockFormatShare(); + + [$userFolder, $node] = $this->getNonSharedUserFolder(); + $node->method('getId') + ->willReturn(42); + $share = $this->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPassword('password') + ->setSendPasswordByTalk(true) ->setExpirationDate(new \DateTime()) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) ->setNode($node); $node->expects($this->once()) ->method('lock') - ->with(\OCP\Lock\ILockingProvider::LOCK_SHARED); + ->with(ILockingProvider::LOCK_SHARED); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) { + $this->callback(function (IShare $share) { $date = new \DateTime('2010-12-23'); - $date->setTime(0,0,0); - - return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && - $share->getPassword() === 'password' && - $share->getExpirationDate() == $date; + $date->setTime(0, 0, 0); + + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() == $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, null, null, null, '2010-12-23'); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$node]); + + $mountPoint = $this->createMock(IMountPoint::class); + $node->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, null, null, null, null, '2010-12-23', null, null, null); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testUpdateLinkSharePublicUploadDoesNotChangeOther() { + public function testUpdateLinkSharePublicUploadDoesNotChangeOther(): void { $ocs = $this->mockFormatShare(); $date = new \DateTime('2000-01-01'); - $folder = $this->getMockBuilder(Folder::class)->getMock(); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $folder->method('getId') + ->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPassword('password') + ->setSendPasswordByTalk(true) ->setExpirationDate($date) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) ->setNode($folder); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) use ($date) { - return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getExpirationDate() === $date; + $this->callback(function (IShare $share) use ($date) { + return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); $this->shareManager->method('getSharedWith') ->willReturn([]); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, null, null, 'true', null); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + + $mountPoint = $this->createMock(IMountPoint::class); + $folder->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, null, null, null, 'true', null, null, null, null); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testUpdateLinkSharePermissions() { + public function testUpdateLinkSharePermissions(): void { $ocs = $this->mockFormatShare(); $date = new \DateTime('2000-01-01'); - $folder = $this->getMockBuilder(Folder::class)->getMock(); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $folder->method('getId') + ->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPassword('password') + ->setSendPasswordByTalk(true) ->setExpirationDate($date) - ->setPermissions(\OCP\Constants::PERMISSION_ALL) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_ALL) ->setNode($folder); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) use ($date) { - return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getExpirationDate() === $date; + $this->callback(function (IShare $share) use ($date): bool { + return $share->getPermissions() === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); $this->shareManager->method('getSharedWith')->willReturn([]); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, 7, null, null, null); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + + $mountPoint = $this->createMock(IMountPoint::class); + $folder->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, 7, null, null, 'true', null, null, null, null); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testUpdateLinkSharePermissionsShare() { + public function testUpdateLinkSharePermissionsShare(): void { $ocs = $this->mockFormatShare(); $date = new \DateTime('2000-01-01'); - $folder = $this->getMockBuilder(Folder::class)->getMock(); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $folder->method('getId') + ->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setShareType(IShare::TYPE_LINK) ->setPassword('password') + ->setSendPasswordByTalk(true) ->setExpirationDate($date) - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNote('note') + ->setLabel('label') + ->setHideDownload(true) + ->setPermissions(Constants::PERMISSION_READ) ->setNode($folder); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) use ($date) { - return $share->getPermissions() === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE) && - $share->getPassword() === 'password' && - $share->getExpirationDate() === $date; - }) - )->will($this->returnArgument(0)); + $this->shareManager->expects($this->once()) + ->method('updateShare') + ->with( + $this->callback(function (IShare $share) use ($date) { + return $share->getPermissions() === Constants::PERMISSION_ALL + && $share->getPassword() === 'password' + && $share->getSendPasswordByTalk() === true + && $share->getExpirationDate() === $date + && $share->getNote() === 'note' + && $share->getLabel() === 'label' + && $share->getHideDownload() === true; + }) + )->willReturnArgument(0); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + + $mountPoint = $this->createMock(IMountPoint::class); + $folder->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); $this->shareManager->method('getSharedWith')->willReturn([]); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, 31, null, null, null); + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, Constants::PERMISSION_ALL, null, null, null, null, null, null, null); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testUpdateOtherPermissions() { + public function testUpdateOtherPermissions(): void { $ocs = $this->mockFormatShare(); - $file = $this->getMockBuilder(File::class)->getMock(); + [$userFolder, $file] = $this->getNonSharedUserFolder(); + $file->method('getId') + ->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL) + $share = Server::get(IManager::class)->newShare(); + $share->setPermissions(Constants::PERMISSION_ALL) ->setSharedBy($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setNode($file); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); $this->shareManager->expects($this->once())->method('updateShare')->with( - $this->callback(function (\OCP\Share\IShare $share) { - return $share->getPermissions() === \OCP\Constants::PERMISSION_ALL; + $this->callback(function (IShare $share) { + return $share->getPermissions() === Constants::PERMISSION_ALL; }) - )->will($this->returnArgument(0)); + )->willReturnArgument(0); $this->shareManager->method('getSharedWith')->willReturn([]); - $expected = new DataResponse(null); - $result = $ocs->updateShare(42, 31, null, null, null); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$file]); + + $mountPoint = $this->createMock(IMountPoint::class); + $file->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $expected = new DataResponse([]); + $result = $ocs->updateShare(42, 31, null, null, null, null); $this->assertInstanceOf(get_class($expected), $result); $this->assertEquals($expected->getData(), $result->getData()); } - public function testUpdateShareCannotIncreasePermissions() { + public function testUpdateShareCannotIncreasePermissions(): void { $ocs = $this->mockFormatShare(); - $folder = $this->createMock(Folder::class); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $folder->method('getId') + ->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); + $share = Server::get(IManager::class)->newShare(); $share ->setId(42) ->setSharedBy($this->currentUser) ->setShareOwner('anotheruser') - ->setShareType(\OCP\Share::SHARE_TYPE_GROUP) + ->setShareType(IShare::TYPE_GROUP) ->setSharedWith('group1') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) ->setNode($folder); // note: updateShare will modify the received instance but getSharedWith will reread from the database, // so their values will be different - $incomingShare = \OC::$server->getShareManager()->newShare(); + $incomingShare = Server::get(IManager::class)->newShare(); $incomingShare ->setId(42) ->setSharedBy($this->currentUser) ->setShareOwner('anotheruser') - ->setShareType(\OCP\Share::SHARE_TYPE_GROUP) + ->setShareType(IShare::TYPE_GROUP) ->setSharedWith('group1') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) ->setNode($folder); $this->request ->method('getParam') - ->will($this->returnValueMap([ + ->willReturnMap([ ['permissions', null, '31'], - ])); + ]); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->expects($this->any()) ->method('getSharedWith') - ->will($this->returnValueMap([ - ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, []], - ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]] - ])); + ->willReturnMap([ + ['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []], + ['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]], + ['currentUser', IShare::TYPE_ROOM, $share->getNode(), -1, 0, []] + ]); - $this->shareManager->expects($this->never())->method('updateShare'); + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + $userFolder->method('getFirstNodeById') + ->with(42) + ->willReturn($folder); + + $mountPoint = $this->createMock(IMountPoint::class); + $folder->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $this->shareManager->expects($this->once()) + ->method('updateShare') + ->with($share) + ->willThrowException(new GenericShareException('Cannot increase permissions of path/file', 'Cannot increase permissions of path/file', 404)); try { $ocs->updateShare(42, 31); $this->fail(); - } catch (OCSNotFoundException $e) { - $this->assertEquals('Cannot increase permissions', $e->getMessage()); + } catch (OCSException $e) { + $this->assertEquals('Cannot increase permissions of path/file', $e->getMessage()); } } - public function testUpdateShareCannotIncreasePermissionsLinkShare() { + public function testUpdateShareCanIncreasePermissionsIfOwner(): void { $ocs = $this->mockFormatShare(); - $folder = $this->createMock(Folder::class); + [$userFolder, $folder] = $this->getNonSharedUserFolder(); + $folder->method('getId') + ->willReturn(42); - $share = \OC::$server->getShareManager()->newShare(); + $share = Server::get(IManager::class)->newShare(); $share ->setId(42) ->setSharedBy($this->currentUser) - ->setShareOwner('anotheruser') - ->setShareType(\OCP\Share::SHARE_TYPE_LINK) - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setShareOwner($this->currentUser) + ->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('group1') + ->setPermissions(Constants::PERMISSION_READ) ->setNode($folder); // note: updateShare will modify the received instance but getSharedWith will reread from the database, // so their values will be different - $incomingShare = \OC::$server->getShareManager()->newShare(); + $incomingShare = Server::get(IManager::class)->newShare(); $incomingShare ->setId(42) ->setSharedBy($this->currentUser) - ->setShareOwner('anotheruser') - ->setShareType(\OCP\Share::SHARE_TYPE_USER) - ->setSharedWith('currentUser') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setShareOwner($this->currentUser) + ->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('group1') + ->setPermissions(Constants::PERMISSION_READ) ->setNode($folder); $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->expects($this->any()) ->method('getSharedWith') - ->will($this->returnValueMap([ - ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, [$incomingShare]], - ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, []] - ])); + ->willReturnMap([ + ['currentUser', IShare::TYPE_USER, $share->getNode(), -1, 0, []], + ['currentUser', IShare::TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]] + ]); - $this->shareManager->expects($this->never())->method('updateShare'); - $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); + $this->shareManager->expects($this->once()) + ->method('updateShare') + ->with($share) + ->willReturn($share); - try { - $ocs->updateShare(42, null, null, 'true'); - $this->fail(); - } catch (OCSNotFoundException $e) { - $this->assertEquals('Cannot increase permissions', $e->getMessage()); - } + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(42) + ->willReturn([$folder]); + + $mountPoint = $this->createMock(IMountPoint::class); + $folder->method('getMountPoint') + ->willReturn($mountPoint); + $mountPoint->method('getStorageRootId') + ->willReturn(42); + + $result = $ocs->updateShare(42, 31); + $this->assertInstanceOf(DataResponse::class, $result); } - public function testUpdateShareCanIncreasePermissionsIfOwner() { + public function testUpdateShareOwnerless(): void { $ocs = $this->mockFormatShare(); - $folder = $this->createMock(Folder::class); + $mount = $this->createMock(IShareOwnerlessMount::class); - $share = \OC::$server->getShareManager()->newShare(); - $share - ->setId(42) - ->setSharedBy($this->currentUser) - ->setShareOwner($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_GROUP) - ->setSharedWith('group1') - ->setPermissions(\OCP\Constants::PERMISSION_READ) - ->setNode($folder); + $file = $this->createMock(File::class); + $file + ->expects($this->exactly(2)) + ->method('getPermissions') + ->willReturn(Constants::PERMISSION_SHARE); + $file + ->expects($this->once()) + ->method('getMountPoint') + ->willReturn($mount); - // note: updateShare will modify the received instance but getSharedWith will reread from the database, - // so their values will be different - $incomingShare = \OC::$server->getShareManager()->newShare(); - $incomingShare - ->setId(42) - ->setSharedBy($this->currentUser) - ->setShareOwner($this->currentUser) - ->setShareType(\OCP\Share::SHARE_TYPE_GROUP) - ->setSharedWith('group1') - ->setPermissions(\OCP\Constants::PERMISSION_READ) - ->setNode($folder); + $userFolder = $this->createMock(Folder::class); + $userFolder->method('getById') + ->with(2) + ->willReturn([$file]); + $userFolder->method('getFirstNodeById') + ->with(2) + ->willReturn($file); - $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); + $this->rootFolder + ->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); - $this->shareManager->expects($this->any()) - ->method('getSharedWith') - ->will($this->returnValueMap([ - ['currentUser', \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0, []], - ['currentUser', \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0, [$incomingShare]] - ])); + $share = $this->createMock(IShare::class); + $share + ->expects($this->once()) + ->method('getNode') + ->willReturn($file); + $share + ->expects($this->exactly(2)) + ->method('getNodeId') + ->willReturn(2); + $share + ->expects($this->exactly(2)) + ->method('getPermissions') + ->willReturn(Constants::PERMISSION_SHARE); - $this->shareManager->expects($this->once()) + $this->shareManager + ->expects($this->once()) + ->method('getShareById') + ->with('ocinternal:1', $this->currentUser) + ->willReturn($share); + + $this->shareManager + ->expects($this->once()) ->method('updateShare') ->with($share) ->willReturn($share); - $result = $ocs->updateShare(42, 31); + $result = $ocs->updateShare(1, Constants::PERMISSION_ALL); $this->assertInstanceOf(DataResponse::class, $result); } @@ -1758,28 +3961,53 @@ class ShareAPIControllerTest extends TestCase { $file = $this->getMockBuilder(File::class)->getMock(); $folder = $this->getMockBuilder(Folder::class)->getMock(); $parent = $this->getMockBuilder(Folder::class)->getMock(); + $fileWithPreview = $this->getMockBuilder(File::class)->getMock(); $file->method('getMimeType')->willReturn('myMimeType'); $folder->method('getMimeType')->willReturn('myFolderMimeType'); + $fileWithPreview->method('getMimeType')->willReturn('mimeWithPreview'); + + $mountPoint = $this->createMock(IMountPoint::class); + $mountPoint->method('getMountType')->willReturn(''); + $file->method('getMountPoint')->willReturn($mountPoint); + $folder->method('getMountPoint')->willReturn($mountPoint); + $fileWithPreview->method('getMountPoint')->willReturn($mountPoint); $file->method('getPath')->willReturn('file'); $folder->method('getPath')->willReturn('folder'); + $fileWithPreview->method('getPath')->willReturn('fileWithPreview'); $parent->method('getId')->willReturn(1); $folder->method('getId')->willReturn(2); $file->method('getId')->willReturn(3); + $fileWithPreview->method('getId')->willReturn(4); $file->method('getParent')->willReturn($parent); $folder->method('getParent')->willReturn($parent); + $fileWithPreview->method('getParent')->willReturn($parent); + + $file->method('getSize')->willReturn(123456); + $folder->method('getSize')->willReturn(123456); + $fileWithPreview->method('getSize')->willReturn(123456); + $file->method('getMTime')->willReturn(1234567890); + $folder->method('getMTime')->willReturn(1234567890); + $fileWithPreview->method('getMTime')->willReturn(1234567890); $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock(); $cache->method('getNumericStorageId')->willReturn(100); - $storage = $this->getMockBuilder(Storage::class)->getMock(); + $storage = $this->createMock(IStorage::class); $storage->method('getId')->willReturn('storageId'); $storage->method('getCache')->willReturn($cache); $file->method('getStorage')->willReturn($storage); $folder->method('getStorage')->willReturn($storage); + $fileWithPreview->method('getStorage')->willReturn($storage); + + + $mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock(); + $mountPoint->method('getMountType')->willReturn(''); + $file->method('getMountPoint')->willReturn($mountPoint); + $folder->method('getMountPoint')->willReturn($mountPoint); $owner = $this->getMockBuilder(IUser::class)->getMock(); $owner->method('getDisplayName')->willReturn('ownerDN'); @@ -1787,28 +4015,33 @@ class ShareAPIControllerTest extends TestCase { $initiator->method('getDisplayName')->willReturn('initiatorDN'); $recipient = $this->getMockBuilder(IUser::class)->getMock(); $recipient->method('getDisplayName')->willReturn('recipientDN'); + $recipient->method('getSystemEMailAddress')->willReturn('recipient'); + [$shareAttributes, $shareAttributesReturnJson] = $this->mockShareAttributes(); $result = []; - $share = \OC::$server->getShareManager()->newShare(); - $share->setShareType(\OCP\Share::SHARE_TYPE_USER) + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') ->setShareOwner('owner') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) + ->setAttributes($shareAttributes) ->setNode($file) ->setShareTime(new \DateTime('2000-01-01T00:01:02')) ->setTarget('myTarget') + ->setNote('personal note') ->setId(42); - /* User backend down */ + // User backend down $result[] = [ [ - 'id' => 42, - 'share_type' => \OCP\Share::SHARE_TYPE_USER, + 'id' => '42', + 'share_type' => IShare::TYPE_USER, 'uid_owner' => 'initiator', 'displayname_owner' => 'initiator', 'permissions' => 1, + 'attributes' => $shareAttributesReturnJson, 'stime' => 946684862, 'parent' => null, 'expiration' => null, @@ -1825,25 +4058,40 @@ class ShareAPIControllerTest extends TestCase { 'file_target' => 'myTarget', 'share_with' => 'recipient', 'share_with_displayname' => 'recipient', + 'share_with_displayname_unique' => 'recipient', + 'note' => 'personal note', + 'label' => '', 'mail_send' => 0, 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => '[{"scope":"permissions","key":"download","value":true}]', + 'item_permissions' => 1, ], $share, [], false ]; - - /* User backend up */ + // User backend up $result[] = [ [ - 'id' => 42, - 'share_type' => \OCP\Share::SHARE_TYPE_USER, + 'id' => '42', + 'share_type' => IShare::TYPE_USER, 'uid_owner' => 'initiator', 'displayname_owner' => 'initiatorDN', 'permissions' => 1, + 'attributes' => $shareAttributesReturnJson, 'stime' => 946684862, 'parent' => null, 'expiration' => null, 'token' => null, 'uid_file_owner' => 'owner', 'displayname_file_owner' => 'ownerDN', + 'note' => 'personal note', + 'label' => '', 'path' => 'file', 'item_type' => 'file', 'storage_id' => 'storageId', @@ -1854,8 +4102,19 @@ class ShareAPIControllerTest extends TestCase { 'file_target' => 'myTarget', 'share_with' => 'recipient', 'share_with_displayname' => 'recipientDN', + 'share_with_displayname_unique' => 'recipient', 'mail_send' => 0, 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => '[{"scope":"permissions","key":"download","value":true}]', + 'item_permissions' => 1, ], $share, [ ['owner', $owner], ['initiator', $initiator], @@ -1863,31 +4122,88 @@ class ShareAPIControllerTest extends TestCase { ], false ]; - $share = \OC::$server->getShareManager()->newShare(); - $share->setShareType(\OCP\Share::SHARE_TYPE_USER) + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_USER) ->setSharedWith('recipient') ->setSharedBy('initiator') ->setShareOwner('owner') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) ->setNode($file) ->setShareTime(new \DateTime('2000-01-01T00:01:02')) ->setTarget('myTarget') + ->setNote('personal note') ->setId(42); - - /* User backend down */ + // User backend down $result[] = [ [ - 'id' => 42, - 'share_type' => \OCP\Share::SHARE_TYPE_USER, + 'id' => '42', + 'share_type' => IShare::TYPE_USER, 'uid_owner' => 'initiator', 'displayname_owner' => 'initiator', 'permissions' => 1, + 'attributes' => null, 'stime' => 946684862, 'parent' => null, 'expiration' => null, 'token' => null, 'uid_file_owner' => 'owner', 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'label' => '', + 'path' => 'file', + 'item_type' => 'file', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 3, + 'file_source' => 3, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => 'recipient', + 'share_with_displayname' => 'recipient', + 'share_with_displayname_unique' => 'recipient', + 'mail_send' => 0, + 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + ], $share, [], false + ]; + + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setNote('personal note') + ->setId(42); + // User backend down + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_USER, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'attributes' => null, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'currentUser', + 'displayname_file_owner' => 'currentUser', + 'note' => 'personal note', + 'label' => '', 'path' => 'file', 'item_type' => 'file', 'storage_id' => 'storageId', @@ -1898,36 +4214,52 @@ class ShareAPIControllerTest extends TestCase { 'file_target' => 'myTarget', 'share_with' => 'recipient', 'share_with_displayname' => 'recipient', + 'share_with_displayname_unique' => 'recipient', 'mail_send' => 0, 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => true, + 'can_delete' => true, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 11, ], $share, [], false ]; // with existing group - $share = \OC::$server->getShareManager()->newShare(); - $share->setShareType(\OCP\Share::SHARE_TYPE_GROUP) + + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_GROUP) ->setSharedWith('recipientGroup') ->setSharedBy('initiator') ->setShareOwner('owner') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) ->setNode($file) ->setShareTime(new \DateTime('2000-01-01T00:01:02')) ->setTarget('myTarget') + ->setNote('personal note') ->setId(42); $result[] = [ [ - 'id' => 42, - 'share_type' => \OCP\Share::SHARE_TYPE_GROUP, + 'id' => '42', + 'share_type' => IShare::TYPE_GROUP, 'uid_owner' => 'initiator', 'displayname_owner' => 'initiator', 'permissions' => 1, + 'attributes' => null, 'stime' => 946684862, 'parent' => null, 'expiration' => null, 'token' => null, 'uid_file_owner' => 'owner', 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'label' => '', 'path' => 'file', 'item_type' => 'file', 'storage_id' => 'storageId', @@ -1940,24 +4272,35 @@ class ShareAPIControllerTest extends TestCase { 'share_with_displayname' => 'recipientGroupDisplayName', 'mail_send' => 0, 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, ], $share, [], false ]; // with unknown group / no group backend - $share = \OC::$server->getShareManager()->newShare(); - $share->setShareType(Share::SHARE_TYPE_GROUP) + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_GROUP) ->setSharedWith('recipientGroup2') ->setSharedBy('initiator') ->setShareOwner('owner') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) ->setNode($file) ->setShareTime(new \DateTime('2000-01-01T00:01:02')) ->setTarget('myTarget') + ->setNote('personal note') ->setId(42); $result[] = [ [ - 'id' => 42, - 'share_type' => Share::SHARE_TYPE_GROUP, + 'id' => '42', + 'share_type' => IShare::TYPE_GROUP, 'uid_owner' => 'initiator', 'displayname_owner' => 'initiator', 'permissions' => 1, @@ -1967,6 +4310,8 @@ class ShareAPIControllerTest extends TestCase { 'token' => null, 'uid_file_owner' => 'owner', 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'label' => '', 'path' => 'file', 'item_type' => 'file', 'storage_id' => 'storageId', @@ -1979,26 +4324,98 @@ class ShareAPIControllerTest extends TestCase { 'share_with_displayname' => 'recipientGroup2', 'mail_send' => 0, 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + ], $share, [], false + ]; + + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_LINK) + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setPassword('mypassword') + ->setExpirationDate(new \DateTime('2001-01-02T00:00:00')) + ->setToken('myToken') + ->setNote('personal note') + ->setLabel('new link share') + ->setId(42); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_LINK, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'attributes' => null, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => '2001-01-02 00:00:00', + 'token' => 'myToken', + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'label' => 'new link share', + 'path' => 'file', + 'item_type' => 'file', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 3, + 'file_source' => 3, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'password' => 'mypassword', + 'share_with' => 'mypassword', + 'share_with_displayname' => '(Shared link)', + 'send_password_by_talk' => false, + 'mail_send' => 0, + 'url' => 'myLink', + 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, ], $share, [], false ]; - $share = \OC::$server->getShareManager()->newShare(); - $share->setShareType(\OCP\Share::SHARE_TYPE_LINK) + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_LINK) ->setSharedBy('initiator') ->setShareOwner('owner') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) ->setNode($file) ->setShareTime(new \DateTime('2000-01-01T00:01:02')) ->setTarget('myTarget') ->setPassword('mypassword') + ->setSendPasswordByTalk(true) ->setExpirationDate(new \DateTime('2001-01-02T00:00:00')) ->setToken('myToken') + ->setNote('personal note') + ->setLabel('new link share') ->setId(42); $result[] = [ [ - 'id' => 42, - 'share_type' => \OCP\Share::SHARE_TYPE_LINK, + 'id' => '42', + 'share_type' => IShare::TYPE_LINK, 'uid_owner' => 'initiator', 'displayname_owner' => 'initiator', 'permissions' => 1, @@ -2008,6 +4425,8 @@ class ShareAPIControllerTest extends TestCase { 'token' => 'myToken', 'uid_file_owner' => 'owner', 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'label' => 'new link share', 'path' => 'file', 'item_type' => 'file', 'storage_id' => 'storageId', @@ -2016,38 +4435,108 @@ class ShareAPIControllerTest extends TestCase { 'file_source' => 3, 'file_parent' => 1, 'file_target' => 'myTarget', + 'password' => 'mypassword', 'share_with' => 'mypassword', - 'share_with_displayname' => 'mypassword', + 'share_with_displayname' => '(Shared link)', + 'send_password_by_talk' => true, 'mail_send' => 0, 'url' => 'myLink', 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, ], $share, [], false ]; - $share = \OC::$server->getShareManager()->newShare(); - $share->setShareType(\OCP\Share::SHARE_TYPE_REMOTE) + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_REMOTE) ->setSharedBy('initiator') ->setSharedWith('user@server.com') ->setShareOwner('owner') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) ->setNode($folder) ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setExpirationDate(new \DateTime('2001-02-03T04:05:06')) ->setTarget('myTarget') + ->setNote('personal note') ->setId(42); $result[] = [ [ - 'id' => 42, - 'share_type' => \OCP\Share::SHARE_TYPE_REMOTE, + 'id' => '42', + 'share_type' => IShare::TYPE_REMOTE, 'uid_owner' => 'initiator', 'displayname_owner' => 'initiator', 'permissions' => 1, 'stime' => 946684862, 'parent' => null, - 'expiration' => null, + 'expiration' => '2001-02-03 00:00:00', + 'token' => null, + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'label' => '', + 'path' => 'folder', + 'item_type' => 'folder', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 2, + 'file_source' => 2, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => 'user@server.com', + 'share_with_displayname' => 'foobar', + 'mail_send' => 0, + 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + 'is_trusted_server' => false, + ], $share, [], false + ]; + + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_REMOTE_GROUP) + ->setSharedBy('initiator') + ->setSharedWith('user@server.com') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($folder) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setExpirationDate(new \DateTime('2001-02-03T04:05:06')) + ->setTarget('myTarget') + ->setNote('personal note') + ->setId(42); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_REMOTE_GROUP, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => '2001-02-03 00:00:00', 'token' => null, 'uid_file_owner' => 'owner', 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'label' => '', 'path' => 'folder', 'item_type' => 'folder', 'storage_id' => 'storageId', @@ -2057,65 +4546,439 @@ class ShareAPIControllerTest extends TestCase { 'file_parent' => 1, 'file_target' => 'myTarget', 'share_with' => 'user@server.com', - 'share_with_displayname' => 'user@server.com', + 'share_with_displayname' => 'foobar', + 'mail_send' => 0, + 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + 'is_trusted_server' => false, + ], $share, [], false + ]; + + // Circle with id, display name and avatar set by the Circles app + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_CIRCLE) + ->setSharedBy('initiator') + ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]') + ->setSharedWithDisplayName('The display name') + ->setSharedWithAvatar('path/to/the/avatar') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($folder) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setId(42); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_CIRCLE, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'attributes' => null, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => '', + 'label' => '', + 'path' => 'folder', + 'item_type' => 'folder', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 2, + 'file_source' => 2, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => '4815162342', + 'share_with_displayname' => 'The display name', + 'share_with_avatar' => 'path/to/the/avatar', + 'mail_send' => 0, + 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + ], $share, [], false + ]; + + // Circle with id set by the Circles app + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_CIRCLE) + ->setSharedBy('initiator') + ->setSharedWith('Circle (Public circle, circleOwner) [4815162342]') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($folder) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setId(42); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_CIRCLE, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => '', + 'label' => '', + 'path' => 'folder', + 'item_type' => 'folder', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 2, + 'file_source' => 2, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => '4815162342', + 'share_with_displayname' => 'Circle (Public circle, circleOwner)', + 'share_with_avatar' => '', + 'mail_send' => 0, + 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + ], $share, [], false + ]; + + // Circle with id not set by the Circles app + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_CIRCLE) + ->setSharedBy('initiator') + ->setSharedWith('Circle (Public circle, circleOwner)') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($folder) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setId(42); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_CIRCLE, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => '', + 'label' => '', + 'path' => 'folder', + 'item_type' => 'folder', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 2, + 'file_source' => 2, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => 'Circle', + 'share_with_displayname' => 'Circle (Public circle, circleOwner)', + 'share_with_avatar' => '', 'mail_send' => 0, 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, ], $share, [], false ]; - $share = \OC::$server->getShareManager()->newShare(); - $share->setShareType(\OCP\Share::SHARE_TYPE_USER) + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_USER) ->setSharedBy('initiator') ->setSharedWith('recipient') ->setShareOwner('owner') - ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setPermissions(Constants::PERMISSION_READ) ->setShareTime(new \DateTime('2000-01-01T00:01:02')) ->setTarget('myTarget') + ->setNote('personal note') ->setId(42); $result[] = [ [], $share, [], true ]; + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_EMAIL) + ->setSharedBy('initiator') + ->setSharedWith('user@server.com') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($folder) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setId(42) + ->setPassword('password'); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_EMAIL, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => '', + 'label' => '', + 'path' => 'folder', + 'item_type' => 'folder', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 2, + 'file_source' => 2, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => 'user@server.com', + 'share_with_displayname' => 'mail display name', + 'mail_send' => 0, + 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'password' => 'password', + 'send_password_by_talk' => false, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'password_expiration_time' => null, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + ], $share, [], false + ]; + + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_EMAIL) + ->setSharedBy('initiator') + ->setSharedWith('user@server.com') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($folder) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setId(42) + ->setPassword('password') + ->setSendPasswordByTalk(true); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_EMAIL, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => '', + 'label' => '', + 'path' => 'folder', + 'item_type' => 'folder', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 2, + 'file_source' => 2, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => 'user@server.com', + 'share_with_displayname' => 'mail display name', + 'mail_send' => 0, + 'mimetype' => 'myFolderMimeType', + 'has_preview' => false, + 'password' => 'password', + 'send_password_by_talk' => true, + 'hide_download' => 0, + 'can_edit' => false, + 'can_delete' => false, + 'password_expiration_time' => null, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + ], $share, [], false + ]; + + // Preview is available + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($fileWithPreview) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setNote('personal note') + ->setId(42); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_USER, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'currentUser', + 'displayname_file_owner' => 'currentUser', + 'note' => 'personal note', + 'label' => '', + 'path' => 'fileWithPreview', + 'item_type' => 'file', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 4, + 'file_source' => 4, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => 'recipient', + 'share_with_displayname' => 'recipient', + 'share_with_displayname_unique' => 'recipient', + 'mail_send' => 0, + 'mimetype' => 'mimeWithPreview', + 'has_preview' => true, + 'hide_download' => 0, + 'can_edit' => true, + 'can_delete' => true, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 11, + ], $share, [], false + ]; + return $result; } /** - * @dataProvider dataFormatShare * * @param array $expects - * @param \OCP\Share\IShare $share + * @param IShare $share * @param array $users * @param $exception */ - public function testFormatShare(array $expects, \OCP\Share\IShare $share, array $users, $exception) { - $this->userManager->method('get')->will($this->returnValueMap($users)); + #[\PHPUnit\Framework\Attributes\DataProvider('dataFormatShare')] + public function testFormatShare(array $expects, IShare $share, array $users, $exception): void { + $this->userManager->method('get')->willReturnMap($users); - $recipientGroup = $this->createMock('\OCP\IGroup'); + $recipientGroup = $this->createMock(IGroup::class); $recipientGroup->method('getDisplayName')->willReturn('recipientGroupDisplayName'); - $this->groupManager->method('get')->will($this->returnValueMap([ - ['recipientGroup', $recipientGroup], - ])); + $this->groupManager->method('get')->willReturnMap([ + ['recipientGroup', $recipientGroup], + ]); $this->urlGenerator->method('linkToRouteAbsolute') ->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken']) ->willReturn('myLink'); - $this->rootFolder->method('getUserFolder') ->with($this->currentUser) - ->will($this->returnSelf()); + ->willReturnSelf(); + $this->dateTimeZone->method('getTimezone')->willReturn(new \DateTimeZone('UTC')); if (!$exception) { - $this->rootFolder->method('getById') + $this->rootFolder->method('getFirstNodeById') ->with($share->getNodeId()) - ->willReturn([$share->getNode()]); + ->willReturn($share->getNode()); $this->rootFolder->method('getRelativePath') ->with($share->getNode()->getPath()) - ->will($this->returnArgument(0)); + ->willReturnArgument(0); } + $cm = $this->createMock(\OCP\Contacts\IManager::class); + $this->overwriteService(\OCP\Contacts\IManager::class, $cm); + + $cm->method('search') + ->willReturnMap([ + ['user@server.com', ['CLOUD'], [ + 'limit' => 1, + 'enumeration' => false, + 'strict_search' => true, + ], + [ + [ + 'CLOUD' => [ + 'user@server.com', + ], + 'FN' => 'foobar', + ], + ], + ], + ['user@server.com', ['EMAIL'], [ + 'limit' => 1, + 'enumeration' => false, + 'strict_search' => true, + ], + [ + [ + 'EMAIL' => [ + 'user@server.com', + ], + 'FN' => 'mail display name', + ], + ], + ], + ]); + try { $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]); $this->assertFalse($exception); @@ -2124,4 +4987,394 @@ class ShareAPIControllerTest extends TestCase { $this->assertTrue($exception); } } + + public function dataFormatRoomShare() { + $file = $this->getMockBuilder(File::class)->getMock(); + $parent = $this->getMockBuilder(Folder::class)->getMock(); + + $file->method('getMimeType')->willReturn('myMimeType'); + + $file->method('getPath')->willReturn('file'); + + $parent->method('getId')->willReturn(1); + $file->method('getId')->willReturn(3); + + $file->method('getParent')->willReturn($parent); + + $file->method('getSize')->willReturn(123456); + $file->method('getMTime')->willReturn(1234567890); + + $mountPoint = $this->getMockBuilder(IMountPoint::class)->getMock(); + $mountPoint->method('getMountType')->willReturn(''); + $file->method('getMountPoint')->willReturn($mountPoint); + + $cache = $this->getMockBuilder('OCP\Files\Cache\ICache')->getMock(); + $cache->method('getNumericStorageId')->willReturn(100); + $storage = $this->createMock(IStorage::class); + $storage->method('getId')->willReturn('storageId'); + $storage->method('getCache')->willReturn($cache); + + $file->method('getStorage')->willReturn($storage); + + $result = []; + + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_ROOM) + ->setSharedWith('recipientRoom') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setNote('personal note') + ->setId(42); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_ROOM, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'path' => 'file', + 'item_type' => 'file', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 3, + 'file_source' => 3, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => 'recipientRoom', + 'share_with_displayname' => '', + 'mail_send' => 0, + 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'label' => '', + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 1, + ], $share, false, [] + ]; + + $share = Server::get(IManager::class)->newShare(); + $share->setShareType(IShare::TYPE_ROOM) + ->setSharedWith('recipientRoom') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(Constants::PERMISSION_READ) + ->setNode($file) + ->setShareTime(new \DateTime('2000-01-01T00:01:02')) + ->setTarget('myTarget') + ->setNote('personal note') + ->setId(42); + + $result[] = [ + [ + 'id' => '42', + 'share_type' => IShare::TYPE_ROOM, + 'uid_owner' => 'initiator', + 'displayname_owner' => 'initiator', + 'permissions' => 1, + 'stime' => 946684862, + 'parent' => null, + 'expiration' => null, + 'token' => null, + 'uid_file_owner' => 'owner', + 'displayname_file_owner' => 'owner', + 'note' => 'personal note', + 'path' => 'file', + 'item_type' => 'file', + 'storage_id' => 'storageId', + 'storage' => 100, + 'item_source' => 3, + 'file_source' => 3, + 'file_parent' => 1, + 'file_target' => 'myTarget', + 'share_with' => 'recipientRoom', + 'share_with_displayname' => 'recipientRoomName', + 'mail_send' => 0, + 'mimetype' => 'myMimeType', + 'has_preview' => false, + 'hide_download' => 0, + 'label' => '', + 'can_edit' => false, + 'can_delete' => false, + 'item_size' => 123456, + 'item_mtime' => 1234567890, + 'is-mount-root' => false, + 'mount-type' => '', + 'attributes' => null, + 'item_permissions' => 9, + ], $share, true, [ + 'share_with_displayname' => 'recipientRoomName' + ] + ]; + + return $result; + } + + /** + * + * @param array $expects + * @param IShare $share + * @param bool $helperAvailable + * @param array $formatShareByHelper + */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataFormatRoomShare')] + public function testFormatRoomShare(array $expects, IShare $share, bool $helperAvailable, array $formatShareByHelper): void { + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturnSelf(); + + $this->rootFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($share->getNode()); + + $this->rootFolder->method('getRelativePath') + ->with($share->getNode()->getPath()) + ->willReturnArgument(0); + + if (!$helperAvailable) { + $this->appManager->method('isEnabledForUser') + ->with('spreed') + ->willReturn(false); + } else { + $this->appManager->method('isEnabledForUser') + ->with('spreed') + ->willReturn(true); + + // This is not possible anymore with PHPUnit 10+ + // as `setMethods` was removed and now real reflection is used, thus the class needs to exist. + // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController') + $helper = $this->getMockBuilder(\stdClass::class) + ->addMethods(['formatShare', 'canAccessShare']) + ->getMock(); + $helper->method('formatShare') + ->with($share) + ->willReturn($formatShareByHelper); + $helper->method('canAccessShare') + ->with($share) + ->willReturn(true); + + $this->serverContainer->method('get') + ->with('\OCA\Talk\Share\Helper\ShareAPIController') + ->willReturn($helper); + } + + $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]); + $this->assertEquals($expects, $result); + } + + /** + * @return list{Folder, Folder} + */ + private function getNonSharedUserFolder(): array { + $node = $this->getMockBuilder(Folder::class)->getMock(); + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $storage = $this->createMock(IStorage::class); + $storage->method('instanceOfStorage') + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); + $userFolder->method('getStorage')->willReturn($storage); + $node->method('getStorage')->willReturn($storage); + $node->method('getId')->willReturn(42); + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn($this->currentUser); + $node->method('getOwner')->willReturn($user); + return [$userFolder, $node]; + } + + /** + * @return list{Folder, File} + */ + private function getNonSharedUserFile(): array { + $node = $this->getMockBuilder(File::class)->getMock(); + $userFolder = $this->getMockBuilder(Folder::class)->getMock(); + $storage = $this->createMock(IStorage::class); + $storage->method('instanceOfStorage') + ->willReturnMap([ + ['OCA\Files_Sharing\External\Storage', false], + ['OCA\Files_Sharing\SharedStorage', false], + ]); + $userFolder->method('getStorage')->willReturn($storage); + $node->method('getStorage')->willReturn($storage); + $node->method('getId')->willReturn(42); + return [$userFolder, $node]; + } + + public function testPopulateTags(): void { + $tagger = $this->createMock(ITags::class); + $this->tagManager->method('load') + ->with('files') + ->willReturn($tagger); + $data = [ + ['file_source' => 10], + ['file_source' => 22, 'foo' => 'bar'], + ['file_source' => 42, 'x' => 'y'], + ]; + $tags = [ + 10 => ['tag3'], + 42 => ['tag1', 'tag2'], + ]; + $tagger->method('getTagsForObjects') + ->with([10, 22, 42]) + ->willReturn($tags); + + $result = self::invokePrivate($this->ocs, 'populateTags', [$data]); + $this->assertSame([ + ['file_source' => 10, 'tags' => ['tag3']], + ['file_source' => 22, 'foo' => 'bar', 'tags' => []], + ['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']], + ], $result); + } + + public function trustedServerProvider(): array { + return [ + 'Trusted server' => [true, true], + 'Untrusted server' => [false, false], + ]; + } + + /** + * @dataProvider trustedServerProvider + */ + public function testFormatShareWithFederatedShare(bool $isKnownServer, bool $isTrusted): void { + $nodeId = 12; + $nodePath = '/test.txt'; + $share = $this->createShare( + 1, + IShare::TYPE_REMOTE, + 'recipient@remoteserver.com', // shared with + 'sender@testserver.com', // shared by + 'shareOwner', // share owner + $nodePath, // path + Constants::PERMISSION_READ, + time(), + null, + null, + $nodePath, + $nodeId + ); + + $node = $this->createMock(\OCP\Files\File::class); + $node->method('getId')->willReturn($nodeId); + $node->method('getPath')->willReturn($nodePath); + $node->method('getInternalPath')->willReturn(ltrim($nodePath, '/')); + $mountPoint = $this->createMock(\OCP\Files\Mount\IMountPoint::class); + $mountPoint->method('getMountType')->willReturn('local'); + $node->method('getMountPoint')->willReturn($mountPoint); + $node->method('getMimetype')->willReturn('text/plain'); + $storage = $this->createMock(\OCP\Files\Storage\IStorage::class); + $storageCache = $this->createMock(\OCP\Files\Cache\ICache::class); + $storageCache->method('getNumericStorageId')->willReturn(1); + $storage->method('getCache')->willReturn($storageCache); + $storage->method('getId')->willReturn('home::shareOwner'); + $node->method('getStorage')->willReturn($storage); + $parent = $this->createMock(\OCP\Files\Folder::class); + $parent->method('getId')->willReturn(2); + $node->method('getParent')->willReturn($parent); + $node->method('getSize')->willReturn(1234); + $node->method('getMTime')->willReturn(1234567890); + + $this->previewManager->method('isAvailable')->with($node)->willReturn(false); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturnSelf(); + + $this->rootFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($node); + + $this->rootFolder->method('getRelativePath') + ->with($node->getPath()) + ->willReturnArgument(0); + + $serverName = 'remoteserver.com'; + $this->trustedServers->method('isTrustedServer') + ->with($serverName) + ->willReturn($isKnownServer); + + $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]); + + $this->assertSame($isTrusted, $result['is_trusted_server']); + } + + public function testFormatShareWithFederatedShareWithAtInUsername(): void { + $nodeId = 12; + $nodePath = '/test.txt'; + $share = $this->createShare( + 1, + IShare::TYPE_REMOTE, + 'recipient@domain.com@remoteserver.com', + 'sender@testserver.com', + 'shareOwner', + $nodePath, + Constants::PERMISSION_READ, + time(), + null, + null, + $nodePath, + $nodeId + ); + + $node = $this->createMock(\OCP\Files\File::class); + $node->method('getId')->willReturn($nodeId); + $node->method('getPath')->willReturn($nodePath); + $node->method('getInternalPath')->willReturn(ltrim($nodePath, '/')); + $mountPoint = $this->createMock(\OCP\Files\Mount\IMountPoint::class); + $mountPoint->method('getMountType')->willReturn('local'); + $node->method('getMountPoint')->willReturn($mountPoint); + $node->method('getMimetype')->willReturn('text/plain'); + $storage = $this->createMock(\OCP\Files\Storage\IStorage::class); + $storageCache = $this->createMock(\OCP\Files\Cache\ICache::class); + $storageCache->method('getNumericStorageId')->willReturn(1); + $storage->method('getCache')->willReturn($storageCache); + $storage->method('getId')->willReturn('home::shareOwner'); + $node->method('getStorage')->willReturn($storage); + $parent = $this->createMock(\OCP\Files\Folder::class); + $parent->method('getId')->willReturn(2); + $node->method('getParent')->willReturn($parent); + $node->method('getSize')->willReturn(1234); + $node->method('getMTime')->willReturn(1234567890); + + $this->previewManager->method('isAvailable')->with($node)->willReturn(false); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturnSelf(); + + $this->rootFolder->method('getFirstNodeById') + ->with($share->getNodeId()) + ->willReturn($node); + + $this->rootFolder->method('getRelativePath') + ->with($node->getPath()) + ->willReturnArgument(0); + + $serverName = 'remoteserver.com'; + $this->trustedServers->method('isTrustedServer') + ->with($serverName) + ->willReturn(true); + + $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]); + + $this->assertTrue($result['is_trusted_server']); + } } diff --git a/apps/files_sharing/tests/Controller/ShareControllerTest.php b/apps/files_sharing/tests/Controller/ShareControllerTest.php index 6062ff89065..011210aff42 100644 --- a/apps/files_sharing/tests/Controller/ShareControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareControllerTest.php @@ -1,56 +1,52 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Cloutier <vincent1cloutier@gmail.com> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests\Controllers; use OC\Files\Filesystem; +use OC\Files\Node\Folder; +use OC\Share20\Manager; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\Files_Sharing\Controller\ShareController; +use OCA\Files_Sharing\DefaultPublicShareTemplateProvider; +use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; +use OCP\Accounts\IAccount; +use OCP\Accounts\IAccountManager; +use OCP\Accounts\IAccountProperty; +use OCP\Activity\IManager; +use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\Template\ExternalShareMenuAction; +use OCP\AppFramework\Http\Template\LinkMenuAction; +use OCP\AppFramework\Http\Template\PublicTemplateResponse; +use OCP\AppFramework\Http\Template\SimpleMenuAction; +use OCP\AppFramework\Services\IInitialState; +use OCP\Constants; +use OCP\Defaults; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\File; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IL10N; -use OCP\ILogger; use OCP\IPreview; use OCP\IRequest; -use OCP\IUser; -use OCP\Share\Exceptions\ShareNotFound; -use OCP\AppFramework\Http\NotFoundResponse; -use OCP\AppFramework\Http\RedirectResponse; -use OCP\AppFramework\Http\TemplateResponse; use OCP\ISession; +use OCP\IURLGenerator; +use OCP\IUser; use OCP\IUserManager; use OCP\Security\ISecureRandom; -use OCP\IURLGenerator; +use OCP\Server; +use OCP\Share\Exceptions\ShareNotFound; +use OCP\Share\IAttributes; +use OCP\Share\IPublicShareTemplateFactory; use OCP\Share\IShare; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use PHPUnit\Framework\MockObject\MockObject; /** * @group DB @@ -59,66 +55,88 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; */ class ShareControllerTest extends \Test\TestCase { - /** @var string */ - private $user; - /** @var string */ - private $oldUser; - - /** @var string */ - private $appName = 'files_sharing'; - /** @var ShareController */ - private $shareController; - /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */ - private $urlGenerator; - /** @var ISession | \PHPUnit_Framework_MockObject_MockObject */ - private $session; - /** @var \OCP\IPreview | \PHPUnit_Framework_MockObject_MockObject */ - private $previewManager; - /** @var \OCP\IConfig | \PHPUnit_Framework_MockObject_MockObject */ - private $config; - /** @var \OC\Share20\Manager | \PHPUnit_Framework_MockObject_MockObject */ - private $shareManager; - /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */ - private $userManager; - /** @var FederatedShareProvider | \PHPUnit_Framework_MockObject_MockObject */ - private $federatedShareProvider; - /** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */ - private $eventDispatcher; - - protected function setUp() { + private string $user; + private string $oldUser; + private string $appName = 'files_sharing'; + private ShareController $shareController; + + private IL10N&MockObject $l10n; + private IConfig&MockObject $config; + private ISession&MockObject $session; + private Defaults&MockObject $defaults; + private IAppConfig&MockObject $appConfig; + private Manager&MockObject $shareManager; + private IPreview&MockObject $previewManager; + private IUserManager&MockObject $userManager; + private IInitialState&MockObject $initialState; + private IURLGenerator&MockObject $urlGenerator; + private ISecureRandom&MockObject $secureRandom; + private IAccountManager&MockObject $accountManager; + private IEventDispatcher&MockObject $eventDispatcher; + private FederatedShareProvider&MockObject $federatedShareProvider; + private IPublicShareTemplateFactory&MockObject $publicShareTemplateFactory; + + protected function setUp(): void { parent::setUp(); $this->appName = 'files_sharing'; - $this->shareManager = $this->getMockBuilder('\OC\Share20\Manager')->disableOriginalConstructor()->getMock(); - $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); - $this->session = $this->getMockBuilder(ISession::class)->getMock(); - $this->previewManager = $this->getMockBuilder(IPreview::class)->getMock(); - $this->config = $this->getMockBuilder(IConfig::class)->getMock(); - $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock(); - $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider') - ->disableOriginalConstructor()->getMock(); + $this->shareManager = $this->createMock(Manager::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->session = $this->createMock(ISession::class); + $this->previewManager = $this->createMock(IPreview::class); + $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->initialState = $this->createMock(IInitialState::class); + $this->federatedShareProvider = $this->createMock(FederatedShareProvider::class); $this->federatedShareProvider->expects($this->any()) ->method('isOutgoingServer2serverShareEnabled')->willReturn(true); $this->federatedShareProvider->expects($this->any()) ->method('isIncomingServer2serverShareEnabled')->willReturn(true); - $this->eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); + $this->accountManager = $this->createMock(IAccountManager::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->l10n = $this->createMock(IL10N::class); + $this->secureRandom = $this->createMock(ISecureRandom::class); + $this->defaults = $this->createMock(Defaults::class); + $this->publicShareTemplateFactory = $this->createMock(IPublicShareTemplateFactory::class); + $this->publicShareTemplateFactory + ->expects($this->any()) + ->method('getProvider') + ->willReturn( + new DefaultPublicShareTemplateProvider( + $this->userManager, + $this->accountManager, + $this->previewManager, + $this->federatedShareProvider, + $this->urlGenerator, + $this->eventDispatcher, + $this->l10n, + $this->defaults, + $this->config, + $this->createMock(IRequest::class), + $this->initialState, + $this->appConfig, + ) + ); - $this->shareController = new \OCA\Files_Sharing\Controller\ShareController( + $this->shareController = new ShareController( $this->appName, - $this->getMockBuilder(IRequest::class)->getMock(), + $this->createMock(IRequest::class), $this->config, $this->urlGenerator, $this->userManager, - $this->getMockBuilder(ILogger::class)->getMock(), - $this->getMockBuilder('\OCP\Activity\IManager')->getMock(), + $this->createMock(IManager::class), $this->shareManager, $this->session, $this->previewManager, - $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock(), + $this->createMock(IRootFolder::class), $this->federatedShareProvider, + $this->accountManager, $this->eventDispatcher, - $this->getMockBuilder(IL10N::class)->getMock(), - $this->getMockBuilder('\OCP\Defaults')->getMock() + $this->l10n, + $this->secureRandom, + $this->defaults, + $this->publicShareTemplateFactory, ); @@ -126,22 +144,24 @@ class ShareControllerTest extends \Test\TestCase { $this->oldUser = \OC_User::getUser(); // Create a dummy user - $this->user = \OC::$server->getSecureRandom()->generate(12, ISecureRandom::CHAR_LOWER); + $this->user = Server::get(ISecureRandom::class)->generate(12, ISecureRandom::CHAR_LOWER); - \OC::$server->getUserManager()->createUser($this->user, $this->user); + Server::get(IUserManager::class)->createUser($this->user, $this->user); \OC_Util::tearDownFS(); $this->loginAsUser($this->user); } - protected function tearDown() { + protected function tearDown(): void { \OC_Util::tearDownFS(); \OC_User::setUserId(''); Filesystem::tearDown(); - $user = \OC::$server->getUserManager()->get($this->user); - if ($user !== null) { $user->delete(); } + $user = Server::get(IUserManager::class)->get($this->user); + if ($user !== null) { + $user->delete(); + } \OC_User::setIncognitoMode(false); - \OC::$server->getSession()->set('public_link_authenticated', ''); + Server::get(ISession::class)->set('public_link_authenticated', ''); // Set old user \OC_User::setUserId($this->oldUser); @@ -149,76 +169,118 @@ class ShareControllerTest extends \Test\TestCase { parent::tearDown(); } - public function testShowAuthenticateNotAuthenticated() { - $share = \OC::$server->getShareManager()->newShare(); + public function testShowShareInvalidToken(): void { + $this->shareController->setToken('invalidtoken'); $this->shareManager ->expects($this->once()) ->method('getShareByToken') - ->with('token') - ->willReturn($share); + ->with('invalidtoken') + ->willThrowException(new ShareNotFound()); - $response = $this->shareController->showAuthenticate('token'); - $expectedResponse = new TemplateResponse($this->appName, 'authenticate', [], 'guest'); - $this->assertEquals($expectedResponse, $response); + $this->expectException(NotFoundException::class); + + // Test without a not existing token + $this->shareController->showShare(); } - public function testShowAuthenticateAuthenticatedForDifferentShare() { - $share = \OC::$server->getShareManager()->newShare(); - $share->setId(1); + public function testShowShareNotAuthenticated(): void { + $this->shareController->setToken('validtoken'); + + $share = Server::get(\OCP\Share\IManager::class)->newShare(); + $share->setPassword('password'); $this->shareManager ->expects($this->once()) ->method('getShareByToken') - ->with('token') + ->with('validtoken') ->willReturn($share); - $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); - $this->session->method('get')->with('public_link_authenticated')->willReturn('2'); + $this->expectException(NotFoundException::class); - $response = $this->shareController->showAuthenticate('token'); - $expectedResponse = new TemplateResponse($this->appName, 'authenticate', [], 'guest'); - $this->assertEquals($expectedResponse, $response); + // Test without a not existing token + $this->shareController->showShare(); } - public function testShowAuthenticateCorrectShare() { - $share = \OC::$server->getShareManager()->newShare(); - $share->setId(1); - $this->shareManager - ->expects($this->once()) - ->method('getShareByToken') - ->with('token') - ->willReturn($share); + public function testShowShare(): void { + $note = 'personal note'; + $filename = 'file1.txt'; - $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); - $this->session->method('get')->with('public_link_authenticated')->willReturn('1'); + $this->shareController->setToken('token'); - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']) - ->willReturn('redirect'); + $owner = $this->createMock(IUser::class); + $owner->method('getDisplayName')->willReturn('ownerDisplay'); + $owner->method('getUID')->willReturn('ownerUID'); + $owner->method('isEnabled')->willReturn(true); - $response = $this->shareController->showAuthenticate('token'); - $expectedResponse = new RedirectResponse('redirect'); - $this->assertEquals($expectedResponse, $response); - } + $initiator = $this->createMock(IUser::class); + $initiator->method('getDisplayName')->willReturn('initiatorDisplay'); + $initiator->method('getUID')->willReturn('initiatorUID'); + $initiator->method('isEnabled')->willReturn(true); - public function testAuthenticateInvalidToken() { - $this->shareManager - ->expects($this->once()) - ->method('getShareByToken') - ->with('token') - ->will($this->throwException(new \OCP\Share\Exceptions\ShareNotFound())); + $file = $this->createMock(File::class); + $file->method('getName')->willReturn($filename); + $file->method('getMimetype')->willReturn('text/plain'); + $file->method('getSize')->willReturn(33); + $file->method('isReadable')->willReturn(true); + $file->method('isShareable')->willReturn(true); + $file->method('getId')->willReturn(111); + + $accountName = $this->createMock(IAccountProperty::class); + $accountName->method('getScope') + ->willReturn(IAccountManager::SCOPE_PUBLISHED); + $account = $this->createMock(IAccount::class); + $account->method('getProperty') + ->with(IAccountManager::PROPERTY_DISPLAYNAME) + ->willReturn($accountName); + $this->accountManager->expects($this->once()) + ->method('getAccount') + ->with($owner) + ->willReturn($account); + + /** @var Manager */ + $manager = Server::get(Manager::class); + $share = $manager->newShare(); + $share->setId(42) + ->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE) + ->setPassword('password') + ->setShareOwner('ownerUID') + ->setSharedBy('initiatorUID') + ->setNode($file) + ->setNote($note) + ->setTarget("/$filename") + ->setToken('token'); - $response = $this->shareController->authenticate('token'); - $expectedResponse = new NotFoundResponse(); - $this->assertEquals($expectedResponse, $response); - } + $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); + $this->session->method('get')->with('public_link_authenticated')->willReturn('42'); - public function testAuthenticateValidPassword() { - $share = \OC::$server->getShareManager()->newShare(); - $share->setId(42); + $this->urlGenerator->expects(self::atLeast(2)) + ->method('linkToRouteAbsolute') + ->willReturnMap([ + // every file has the show show share url in the opengraph url prop + ['files_sharing.sharecontroller.showShare', ['token' => 'token'], 'shareUrl'], + // this share is not an image to the default preview is used + ['files_sharing.PublicPreview.getPreview', ['x' => 256, 'y' => 256, 'file' => $share->getTarget(), 'token' => 'token'], 'previewUrl'], + ]); + + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->willReturnMap([ + ['/public.php/dav/files/token/?accept=zip', 'downloadUrl'], + ]); + + $this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true); + + $this->config->method('getSystemValue') + ->willReturnMap( + [ + ['max_filesize_animated_gifs_public_sharing', 10, 10], + ['enable_previews', true, true], + ['preview_max_x', 1024, 1024], + ['preview_max_y', 1024, 1024], + ] + ); $this->shareManager ->expects($this->once()) @@ -226,128 +288,289 @@ class ShareControllerTest extends \Test\TestCase { ->with('token') ->willReturn($share); - $this->shareManager - ->expects($this->once()) - ->method('checkPassword') - ->with($share, 'validpassword') - ->willReturn(true); + $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) { + if ($uid === 'ownerUID') { + return $owner; + } + if ($uid === 'initiatorUID') { + return $initiator; + } + return null; + }); + + $this->eventDispatcher->method('dispatchTyped')->with( + $this->callback(function ($event) use ($share) { + if ($event instanceof BeforeTemplateRenderedEvent) { + return $event->getShare() === $share; + } else { + return true; + } + }) + ); - $this->session - ->expects($this->once()) - ->method('set') - ->with('public_link_authenticated', '42'); + $this->l10n->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($text, $parameters) { + return vsprintf($text, $parameters); + }); + + $this->defaults->expects(self::any()) + ->method('getProductName') + ->willReturn('Nextcloud'); + + // Ensure the correct initial state is setup + // Shared node is a file so this is a single file share: + $view = 'public-file-share'; + // Set up initial state + $initialState = []; + $this->initialState->expects(self::any()) + ->method('provideInitialState') + ->willReturnCallback(function ($key, $value) use (&$initialState): void { + $initialState[$key] = $value; + }); + $expectedInitialState = [ + 'isPublic' => true, + 'sharingToken' => 'token', + 'sharePermissions' => (Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE), + 'filename' => $filename, + 'view' => $view, + 'fileId' => 111, + 'owner' => 'ownerUID', + 'ownerDisplayName' => 'ownerDisplay', + 'isFileRequest' => false, + ]; - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('files_sharing.sharecontroller.showShare', ['token'=>'token']) - ->willReturn('redirect'); + $response = $this->shareController->showShare(); + + $this->assertEquals($expectedInitialState, $initialState); + + $csp = new ContentSecurityPolicy(); + $csp->addAllowedFrameDomain('\'self\''); + $expectedResponse = new PublicTemplateResponse('files', 'index'); + $expectedResponse->setParams(['pageTitle' => $filename]); + $expectedResponse->setContentSecurityPolicy($csp); + $expectedResponse->setHeaderTitle($filename); + $expectedResponse->setHeaderDetails('shared by ownerDisplay'); + $expectedResponse->setHeaderActions([ + new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', 'downloadUrl', 0, '33'), + new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', 'owner', 'ownerDisplay', $filename), + new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', 'downloadUrl'), + ]); - $response = $this->shareController->authenticate('token', 'validpassword'); - $expectedResponse = new RedirectResponse('redirect'); $this->assertEquals($expectedResponse, $response); } - public function testAuthenticateInvalidPassword() { - $share = \OC::$server->getShareManager()->newShare(); - $share->setNodeId(100) - ->setNodeType('file') - ->setToken('token') - ->setSharedBy('initiator') - ->setId(42); + public function testShowFileDropShare(): void { + $filename = 'folder1'; - $this->shareManager - ->expects($this->once()) - ->method('getShareByToken') - ->with('token') - ->willReturn($share); + $this->shareController->setToken('token'); - $this->shareManager - ->expects($this->once()) - ->method('checkPassword') - ->with($share, 'invalidpassword') - ->willReturn(false); + $owner = $this->createMock(IUser::class); + $owner->method('getDisplayName')->willReturn('ownerDisplay'); + $owner->method('getUID')->willReturn('ownerUID'); + $owner->method('isEnabled')->willReturn(true); - $this->session - ->expects($this->never()) - ->method('set'); - - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['access'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'share_link_access', $hookListner, 'access'); - - $hookListner->expects($this->once()) - ->method('access') - ->with($this->callback(function(array $data) { - return $data['itemType'] === 'file' && - $data['itemSource'] === 100 && - $data['uidOwner'] === 'initiator' && - $data['token'] === 'token' && - $data['errorCode'] === 403 && - $data['errorMessage'] === 'Wrong password'; - })); - - $response = $this->shareController->authenticate('token', 'invalidpassword'); - $expectedResponse = new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest'); - $expectedResponse->throttle(); - $this->assertEquals($expectedResponse, $response); - } + $initiator = $this->createMock(IUser::class); + $initiator->method('getDisplayName')->willReturn('initiatorDisplay'); + $initiator->method('getUID')->willReturn('initiatorUID'); + $initiator->method('isEnabled')->willReturn(true); - public function testShowShareInvalidToken() { - $this->shareManager + $file = $this->createMock(Folder::class); + $file->method('isReadable')->willReturn(true); + $file->method('isShareable')->willReturn(true); + $file->method('getId')->willReturn(1234); + $file->method('getName')->willReturn($filename); + + $accountName = $this->createMock(IAccountProperty::class); + $accountName->method('getScope') + ->willReturn(IAccountManager::SCOPE_PUBLISHED); + $account = $this->createMock(IAccount::class); + $account->method('getProperty') + ->with(IAccountManager::PROPERTY_DISPLAYNAME) + ->willReturn($accountName); + $this->accountManager->expects($this->once()) + ->method('getAccount') + ->with($owner) + ->willReturn($account); + + /** @var Manager */ + $manager = Server::get(Manager::class); + $share = $manager->newShare(); + $share->setId(42) + ->setPermissions(Constants::PERMISSION_CREATE) + ->setPassword('password') + ->setShareOwner('ownerUID') + ->setSharedBy('initiatorUID') + ->setNote('The note') + ->setLabel('A label') + ->setNode($file) + ->setTarget("/$filename") + ->setToken('token'); + + $this->appConfig ->expects($this->once()) - ->method('getShareByToken') - ->with('invalidtoken') - ->will($this->throwException(new ShareNotFound())); + ->method('getValueString') + ->with('core', 'shareapi_public_link_disclaimertext', '') + ->willReturn('My disclaimer text'); - // Test without a not existing token - $response = $this->shareController->showShare('invalidtoken'); - $expectedResponse = new NotFoundResponse(); - $this->assertEquals($expectedResponse, $response); - } + $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); + $this->session->method('get')->with('public_link_authenticated')->willReturn('42'); - public function testShowShareNotAuthenticated() { - $share = \OC::$server->getShareManager()->newShare(); - $share->setPassword('password'); + $this->urlGenerator->expects(self::atLeastOnce()) + ->method('linkToRouteAbsolute') + ->willReturnMap([ + // every file has the show show share url in the opengraph url prop + ['files_sharing.sharecontroller.showShare', ['token' => 'token'], 'shareUrl'], + // there is no preview or folders so no other link for opengraph + ]); + + $this->config->method('getSystemValue') + ->willReturnMap( + [ + ['max_filesize_animated_gifs_public_sharing', 10, 10], + ['enable_previews', true, true], + ['preview_max_x', 1024, 1024], + ['preview_max_y', 1024, 1024], + ] + ); $this->shareManager ->expects($this->once()) ->method('getShareByToken') - ->with('validtoken') + ->with('token') ->willReturn($share); - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('files_sharing.sharecontroller.authenticate', ['token' => 'validtoken']) - ->willReturn('redirect'); + $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) { + if ($uid === 'ownerUID') { + return $owner; + } + if ($uid === 'initiatorUID') { + return $initiator; + } + return null; + }); + + $this->eventDispatcher->method('dispatchTyped')->with( + $this->callback(function ($event) use ($share) { + if ($event instanceof BeforeTemplateRenderedEvent) { + return $event->getShare() === $share; + } else { + return true; + } + }) + ); + + $this->l10n->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($text, $parameters) { + return vsprintf($text, $parameters); + }); + + // Set up initial state + $initialState = []; + $this->initialState->expects(self::any()) + ->method('provideInitialState') + ->willReturnCallback(function ($key, $value) use (&$initialState): void { + $initialState[$key] = $value; + }); + $expectedInitialState = [ + 'isPublic' => true, + 'sharingToken' => 'token', + 'sharePermissions' => Constants::PERMISSION_CREATE, + 'filename' => $filename, + 'view' => 'public-file-drop', + 'disclaimer' => 'My disclaimer text', + 'owner' => 'ownerUID', + 'ownerDisplayName' => 'ownerDisplay', + 'isFileRequest' => false, + 'note' => 'The note', + 'label' => 'A label', + ]; + + $response = $this->shareController->showShare(); + + $this->assertEquals($expectedInitialState, $initialState); + + $csp = new ContentSecurityPolicy(); + $csp->addAllowedFrameDomain('\'self\''); + $expectedResponse = new PublicTemplateResponse('files', 'index'); + $expectedResponse->setParams(['pageTitle' => 'A label']); + $expectedResponse->setContentSecurityPolicy($csp); + $expectedResponse->setHeaderTitle('A label'); + $expectedResponse->setHeaderDetails('shared by ownerDisplay'); + $expectedResponse->setHeaderActions([ + new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', 'shareUrl'), + ]); - // Test without a not existing token - $response = $this->shareController->showShare('validtoken'); - $expectedResponse = new RedirectResponse('redirect'); $this->assertEquals($expectedResponse, $response); } + public function testShowShareWithPrivateName(): void { + $note = 'personal note'; + $filename = 'file1.txt'; - public function testShowShare() { - $owner = $this->getMockBuilder(IUser::class)->getMock(); + $this->shareController->setToken('token'); + + $owner = $this->createMock(IUser::class); $owner->method('getDisplayName')->willReturn('ownerDisplay'); $owner->method('getUID')->willReturn('ownerUID'); + $owner->method('isEnabled')->willReturn(true); - $file = $this->getMockBuilder('OCP\Files\File')->getMock(); - $file->method('getName')->willReturn('file1.txt'); + $initiator = $this->createMock(IUser::class); + $initiator->method('getDisplayName')->willReturn('initiatorDisplay'); + $initiator->method('getUID')->willReturn('initiatorUID'); + $initiator->method('isEnabled')->willReturn(true); + + $file = $this->createMock(File::class); + $file->method('getName')->willReturn($filename); $file->method('getMimetype')->willReturn('text/plain'); $file->method('getSize')->willReturn(33); $file->method('isReadable')->willReturn(true); $file->method('isShareable')->willReturn(true); - - $share = \OC::$server->getShareManager()->newShare(); + $file->method('getId')->willReturn(111); + + $accountName = $this->createMock(IAccountProperty::class); + $accountName->method('getScope') + ->willReturn(IAccountManager::SCOPE_LOCAL); + $account = $this->createMock(IAccount::class); + $account->method('getProperty') + ->with(IAccountManager::PROPERTY_DISPLAYNAME) + ->willReturn($accountName); + $this->accountManager->expects($this->once()) + ->method('getAccount') + ->with($owner) + ->willReturn($account); + + /** @var IShare */ + $share = Server::get(Manager::class)->newShare(); $share->setId(42); $share->setPassword('password') ->setShareOwner('ownerUID') + ->setSharedBy('initiatorUID') ->setNode($file) - ->setTarget('/file1.txt'); + ->setNote($note) + ->setToken('token') + ->setPermissions(Constants::PERMISSION_ALL & ~Constants::PERMISSION_SHARE) + ->setTarget("/$filename"); $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); $this->session->method('get')->with('public_link_authenticated')->willReturn('42'); + $this->urlGenerator->expects(self::atLeast(2)) + ->method('linkToRouteAbsolute') + ->willReturnMap([ + // every file has the show show share url in the opengraph url prop + ['files_sharing.sharecontroller.showShare', ['token' => 'token'], 'shareUrl'], + // this share is not an image to the default preview is used + ['files_sharing.PublicPreview.getPreview', ['x' => 256, 'y' => 256, 'file' => $share->getTarget(), 'token' => 'token'], 'previewUrl'], + ]); + + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->willReturnMap([ + ['/public.php/dav/files/token/?accept=zip', 'downloadUrl'], + ]); + $this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true); $this->config->method('getSystemValue') @@ -367,75 +590,79 @@ class ShareControllerTest extends \Test\TestCase { ->method('getShareByToken') ->with('token') ->willReturn($share); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('core', 'shareapi_public_link_disclaimertext', null) - ->willReturn('My disclaimer text'); - $this->userManager->method('get')->with('ownerUID')->willReturn($owner); + $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) { + if ($uid === 'ownerUID') { + return $owner; + } + if ($uid === 'initiatorUID') { + return $initiator; + } + return null; + }); + + $this->eventDispatcher->method('dispatchTyped')->with( + $this->callback(function ($event) use ($share) { + if ($event instanceof BeforeTemplateRenderedEvent) { + return $event->getShare() === $share; + } else { + return true; + } + }) + ); - $this->eventDispatcher->expects($this->once()) - ->method('dispatch') - ->with('OCA\Files_Sharing::loadAdditionalScripts'); + $this->l10n->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($text, $parameters) { + return vsprintf($text, $parameters); + }); - $response = $this->shareController->showShare('token'); - $sharedTmplParams = array( - 'displayName' => 'ownerDisplay', - 'owner' => 'ownerUID', - 'filename' => 'file1.txt', - 'directory_path' => '/file1.txt', - 'mimetype' => 'text/plain', - 'dirToken' => 'token', - 'sharingToken' => 'token', - 'server2serversharing' => true, - 'protected' => 'true', - 'dir' => '', - 'downloadURL' => null, - 'fileSize' => '33 B', - 'nonHumanFileSize' => 33, - 'maxSizeAnimateGif' => 10, - 'previewSupported' => true, - 'previewEnabled' => true, - 'previewMaxX' => 1024, - 'previewMaxY' => 1024, - 'hideFileList' => false, - 'shareOwner' => 'ownerDisplay', - 'disclaimer' => 'My disclaimer text', - 'shareUrl' => null, - 'previewImage' => null, - 'previewURL' => null, - ); + $this->defaults->expects(self::any()) + ->method('getProductName') + ->willReturn('Nextcloud'); + + $response = $this->shareController->showShare(); - $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); + $csp = new ContentSecurityPolicy(); $csp->addAllowedFrameDomain('\'self\''); - $expectedResponse = new TemplateResponse($this->appName, 'public', $sharedTmplParams, 'base'); + $expectedResponse = new PublicTemplateResponse('files', 'index'); + $expectedResponse->setParams(['pageTitle' => $filename]); $expectedResponse->setContentSecurityPolicy($csp); + $expectedResponse->setHeaderTitle($filename); + $expectedResponse->setHeaderDetails(''); + $expectedResponse->setHeaderActions([ + new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', 'downloadUrl', 0, '33'), + new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', 'owner', 'ownerDisplay', $filename), + new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', 'downloadUrl'), + ]); $this->assertEquals($expectedResponse, $response); } - /** - * @expectedException \OCP\Files\NotFoundException - */ - public function testShowShareInvalid() { + + public function testShowShareInvalid(): void { + $this->expectException(NotFoundException::class); + + $filename = 'file1.txt'; + $this->shareController->setToken('token'); + $owner = $this->getMockBuilder(IUser::class)->getMock(); $owner->method('getDisplayName')->willReturn('ownerDisplay'); $owner->method('getUID')->willReturn('ownerUID'); $file = $this->getMockBuilder('OCP\Files\File')->getMock(); - $file->method('getName')->willReturn('file1.txt'); + $file->method('getName')->willReturn($filename); $file->method('getMimetype')->willReturn('text/plain'); $file->method('getSize')->willReturn(33); $file->method('isShareable')->willReturn(false); $file->method('isReadable')->willReturn(true); - $share = \OC::$server->getShareManager()->newShare(); + $share = Server::get(\OCP\Share\IManager::class)->newShare(); $share->setId(42); $share->setPassword('password') ->setShareOwner('ownerUID') ->setNode($file) - ->setTarget('/file1.txt'); + ->setTarget("/$filename"); $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); $this->session->method('get')->with('public_link_authenticated')->willReturn('42'); @@ -460,16 +687,16 @@ class ShareControllerTest extends \Test\TestCase { $this->userManager->method('get')->with('ownerUID')->willReturn($owner); - $this->shareController->showShare('token'); + $this->shareController->showShare(); } - public function testDownloadShare() { + public function testDownloadShareWithCreateOnlyShare(): void { $share = $this->getMockBuilder(IShare::class)->getMock(); $share->method('getPassword')->willReturn('password'); $share ->expects($this->once()) ->method('getPermissions') - ->willReturn(\OCP\Constants::PERMISSION_READ); + ->willReturn(Constants::PERMISSION_CREATE); $this->shareManager ->expects($this->once()) @@ -477,35 +704,119 @@ class ShareControllerTest extends \Test\TestCase { ->with('validtoken') ->willReturn($share); - $this->urlGenerator->expects($this->once()) - ->method('linkToRoute') - ->with('files_sharing.sharecontroller.authenticate', ['token' => 'validtoken']) - ->willReturn('redirect'); - // Test with a password protected share and no authentication $response = $this->shareController->downloadShare('validtoken'); - $expectedResponse = new RedirectResponse('redirect'); + $expectedResponse = new DataResponse('Share has no read permission'); $this->assertEquals($expectedResponse, $response); } - public function testDownloadShareWithCreateOnlyShare() { - $share = $this->getMockBuilder(IShare::class)->getMock(); + public function testDownloadShareWithoutDownloadPermission(): void { + $attributes = $this->createMock(IAttributes::class); + $attributes->expects(self::once()) + ->method('getAttribute') + ->with('permissions', 'download') + ->willReturn(false); + + $share = $this->createMock(IShare::class); $share->method('getPassword')->willReturn('password'); - $share - ->expects($this->once()) + $share->expects(self::once()) ->method('getPermissions') - ->willReturn(\OCP\Constants::PERMISSION_CREATE); + ->willReturn(Constants::PERMISSION_READ); + $share->expects(self::once()) + ->method('getAttributes') + ->willReturn($attributes); $this->shareManager - ->expects($this->once()) + ->expects(self::once()) ->method('getShareByToken') ->with('validtoken') ->willReturn($share); // Test with a password protected share and no authentication $response = $this->shareController->downloadShare('validtoken'); - $expectedResponse = new DataResponse('Share is read-only'); + $expectedResponse = new DataResponse('Share has no download permission'); $this->assertEquals($expectedResponse, $response); } + public function testDisabledOwner(): void { + $this->shareController->setToken('token'); + + $owner = $this->getMockBuilder(IUser::class)->getMock(); + $owner->method('isEnabled')->willReturn(false); + + $initiator = $this->createMock(IUser::class); + $initiator->method('isEnabled')->willReturn(false); + + /* @var MockObject|Folder $folder */ + $folder = $this->createMock(Folder::class); + + $share = Server::get(\OCP\Share\IManager::class)->newShare(); + $share->setId(42); + $share->setPermissions(Constants::PERMISSION_CREATE) + ->setShareOwner('ownerUID') + ->setSharedBy('initiatorUID') + ->setNode($folder) + ->setTarget('/share'); + + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->willReturn($share); + + $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) { + if ($uid === 'ownerUID') { + return $owner; + } + if ($uid === 'initiatorUID') { + return $initiator; + } + return null; + }); + + $this->expectException(NotFoundException::class); + + $this->shareController->showShare(); + } + + public function testDisabledInitiator(): void { + $this->shareController->setToken('token'); + + $owner = $this->getMockBuilder(IUser::class)->getMock(); + $owner->method('isEnabled')->willReturn(false); + + $initiator = $this->createMock(IUser::class); + $initiator->method('isEnabled')->willReturn(true); + + /* @var MockObject|Folder $folder */ + $folder = $this->createMock(Folder::class); + + $share = Server::get(\OCP\Share\IManager::class)->newShare(); + $share->setId(42); + $share->setPermissions(Constants::PERMISSION_CREATE) + ->setShareOwner('ownerUID') + ->setSharedBy('initiatorUID') + ->setNode($folder) + ->setTarget('/share'); + + $this->shareManager + ->expects($this->once()) + ->method('getShareByToken') + ->with('token') + ->willReturn($share); + + $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) { + if ($uid === 'ownerUID') { + return $owner; + } + if ($uid === 'initiatorUID') { + return $initiator; + } + return null; + }); + + $this->expectException(NotFoundException::class); + + $this->shareController->showShare(); + } } diff --git a/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php b/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php index 882e858217e..1a678610805 100644 --- a/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php @@ -1,25 +1,8 @@ <?php + /** - * - * - * @author Morris Jobke <hey@morrisjobke.de> - * @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: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Sharing\Tests\Controller; @@ -33,42 +16,38 @@ use OCP\IRequest; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as ShareManager; use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class ShareInfoControllerTest extends TestCase { - /** @var ShareInfoController */ - private $controller; - - /** @var ShareManager|\PHPUnit_Framework_MockObject_MockObject */ - private $shareManager; + protected ShareInfoController $controller; + protected ShareManager&MockObject $shareManager; - public function setUp() { + protected function setUp(): void { parent::setUp(); $this->shareManager = $this->createMock(ShareManager::class); - $this->controller = $this->getMockBuilder(ShareInfoController::class) - ->setConstructorArgs([ - 'files_sharing', - $this->createMock(IRequest::class), - $this->shareManager - ]) - ->setMethods(['addROWrapper']) - ->getMock(); + $this->controller = new ShareInfoController( + 'files_sharing', + $this->createMock(IRequest::class), + $this->shareManager + ); } - public function testNoShare() { + public function testNoShare(): void { $this->shareManager->method('getShareByToken') ->with('token') ->willThrowException(new ShareNotFound()); $expected = new JSONResponse([], Http::STATUS_NOT_FOUND); + $expected->throttle(['token' => 'token']); $this->assertEquals($expected, $this->controller->info('token')); } - public function testWrongPassword() { + public function testWrongPassword(): void { $share = $this->createMock(IShare::class); $share->method('getPassword') ->willReturn('sharePass'); @@ -81,10 +60,11 @@ class ShareInfoControllerTest extends TestCase { ->willReturn(false); $expected = new JSONResponse([], Http::STATUS_FORBIDDEN); + $expected->throttle(['token' => 'token']); $this->assertEquals($expected, $this->controller->info('token', 'pass')); } - public function testNoReadPermissions() { + public function testNoReadPermissions(): void { $share = $this->createMock(IShare::class); $share->method('getPassword') ->willReturn('sharePass'); @@ -99,6 +79,7 @@ class ShareInfoControllerTest extends TestCase { ->willReturn(true); $expected = new JSONResponse([], Http::STATUS_FORBIDDEN); + $expected->throttle(['token' => 'token']); $this->assertEquals($expected, $this->controller->info('token', 'pass')); } @@ -122,7 +103,7 @@ class ShareInfoControllerTest extends TestCase { return $file; } - public function testInfoFile() { + public function testInfoFile(): void { $file = $this->prepareFile(); $share = $this->createMock(IShare::class); @@ -154,7 +135,7 @@ class ShareInfoControllerTest extends TestCase { $this->assertEquals($expected, $this->controller->info('token', 'pass')); } - public function testInfoFileRO() { + public function testInfoFileRO(): void { $file = $this->prepareFile(); $share = $this->createMock(IShare::class); @@ -172,9 +153,6 @@ class ShareInfoControllerTest extends TestCase { ->with($share, 'pass') ->willReturn(true); - $this->controller->expects($this->once()) - ->method('addROWrapper'); - $expected = new JSONResponse([ 'id' => 42, 'parentId' => 41, @@ -239,7 +217,7 @@ class ShareInfoControllerTest extends TestCase { return $root; } - public function testInfoFolder() { + public function testInfoFolder(): void { $file = $this->prepareFolder(); $share = $this->createMock(IShare::class); @@ -284,7 +262,7 @@ class ShareInfoControllerTest extends TestCase { 'parentId' => 43, 'mtime' => 1339, 'name' => 'file', - 'permissions' => 9, + 'permissions' => 1, 'mimetype' => 'mime/type', 'size' => 3, 'type' => 'file', diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index 98ae953a318..18e1bf0347b 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -1,43 +1,24 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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 OCA\Files_Sharing\Tests\Controller; use OCA\Files_Sharing\Controller\ShareesAPIController; use OCA\Files_Sharing\Tests\TestCase; -use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\Collaboration\Collaborators\ISearch; +use OCP\GlobalScale\IConfig as GlobalScaleIConfig; use OCP\IConfig; use OCP\IRequest; use OCP\IURLGenerator; -use OCP\Share; use OCP\Share\IManager; +use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; /** * Class ShareesTest @@ -50,25 +31,30 @@ class ShareesAPIControllerTest extends TestCase { /** @var ShareesAPIController */ protected $sharees; - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ + /** @var string */ + protected $uid; + + /** @var IRequest|MockObject */ protected $request; - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IManager|MockObject */ protected $shareManager; - /** @var ISearch|\PHPUnit_Framework_MockObject_MockObject */ + /** @var ISearch|MockObject */ protected $collaboratorSearch; - protected function setUp() { + /** @var IConfig|MockObject */ + protected $config; + + protected function setUp(): void { parent::setUp(); + $this->uid = 'test123'; $this->request = $this->createMock(IRequest::class); $this->shareManager = $this->createMock(IManager::class); + $this->config = $this->createMock(IConfig::class); - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $configMock */ - $configMock = $this->createMock(IConfig::class); - - /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject $urlGeneratorMock */ + /** @var IURLGenerator|MockObject $urlGeneratorMock */ $urlGeneratorMock = $this->createMock(IURLGenerator::class); $this->collaboratorSearch = $this->createMock(ISearch::class); @@ -76,187 +62,215 @@ class ShareesAPIControllerTest extends TestCase { $this->sharees = new ShareesAPIController( 'files_sharing', $this->request, - $configMock, + $this->uid, + $this->config, $urlGeneratorMock, $this->shareManager, $this->collaboratorSearch ); } - public function dataSearch() { - $noRemote = [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_EMAIL]; - $allTypes = [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE, Share::SHARE_TYPE_EMAIL]; + public static function dataSearch(): array { + $noRemote = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_EMAIL]; + $allTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_REMOTE_GROUP, IShare::TYPE_EMAIL]; return [ - [[], '', 'yes', true, true, $noRemote, false, true, true], + [[], '', 'yes', false, true, true, true, $noRemote, false, true, true], // Test itemType [[ 'search' => '', - ], '', 'yes', true, true, $noRemote, false, true, true], + ], '', 'yes', false, true, true, true, $noRemote, false, true, true], [[ 'search' => 'foobar', - ], '', 'yes', true, true, $noRemote, false, true, true], + ], '', 'yes', false, true, true, true, $noRemote, false, true, true], [[ 'search' => 0, - ], '', 'yes', true, true, $noRemote, false, true, true], + ], '', 'yes', false, true, true, true, $noRemote, false, true, true], // Test itemType [[ 'itemType' => '', - ], '', 'yes', true, true, $noRemote, false, true, true], + ], '', 'yes', false, true, true, true, $noRemote, false, true, true], [[ 'itemType' => 'folder', - ], '', 'yes', true, true, $allTypes, false, true, true], + ], '', 'yes', false, true, true, true, $allTypes, false, true, true], [[ 'itemType' => 0, - ], '', 'yes', true, true, $noRemote, false, true, true], - + ], '', 'yes', false, true, true , true, $noRemote, false, true, true], // Test shareType [[ 'itemType' => 'call', - ], '', 'yes', true, true, $noRemote, false, true, true], + ], '', 'yes', false, true, true, true, $noRemote, false, true, true], + [[ + 'itemType' => 'call', + ], '', 'yes', false, true, true, true, [0, 4], false, true, false], [[ 'itemType' => 'folder', - ], '', 'yes', true, true, $allTypes, false, true, true], + ], '', 'yes', false, true, true, true, $allTypes, false, true, true], [[ 'itemType' => 'folder', 'shareType' => 0, - ], '', 'yes', true, false, [0], false, true, true], + ], '', 'yes', false, true, true, false, [0], false, true, true], [[ 'itemType' => 'folder', 'shareType' => '0', - ], '', 'yes', true, false, [0], false, true, true], + ], '', 'yes', false, true, true, false, [0], false, true, true], [[ 'itemType' => 'folder', 'shareType' => 1, - ], '', 'yes', true, false, [1], false, true, true], + ], '', 'yes', false, true, true, false, [1], false, true, true], [[ 'itemType' => 'folder', 'shareType' => 12, - ], '', 'yes', true, false, [], false, true, true], + ], '', 'yes', false, true, true, false, [], false, true, true], [[ 'itemType' => 'folder', 'shareType' => 'foobar', - ], '', 'yes', true, true, $allTypes, false, true, true], + ], '', 'yes', false, true, true, true, $allTypes, false, true, true], + [[ 'itemType' => 'folder', 'shareType' => [0, 1, 2], - ], '', 'yes', false, false, [0, 1], false, true, true], + ], '', 'yes', false, false, false, false, [0, 1], false, true, true], [[ 'itemType' => 'folder', 'shareType' => [0, 1], - ], '', 'yes', false, false, [0, 1], false, true, true], + ], '', 'yes', false, false, false, false, [0, 1], false, true, true], [[ 'itemType' => 'folder', 'shareType' => $allTypes, - ], '', 'yes', true, true, $allTypes, false, true, true], + ], '', 'yes', false, true, true, true, $allTypes, false, true, true], [[ 'itemType' => 'folder', 'shareType' => $allTypes, - ], '', 'yes', false, false, [0, 1], false, true, true], + ], '', 'yes', false, false, false, false, [0, 1], false, true, true], [[ 'itemType' => 'folder', 'shareType' => $allTypes, - ], '', 'yes', true, false, [0, 6], false, true, false], + ], '', 'yes', false, true, false, false, [0, 6], false, true, false], [[ 'itemType' => 'folder', 'shareType' => $allTypes, - ], '', 'yes', false, true, [0, 4], false, true, false], + ], '', 'yes', false, false, false, true, [0, 4], false, true, false], + [[ + 'itemType' => 'folder', + 'shareType' => $allTypes, + ], '', 'yes', false, true, true, false, [0, 6, 9], false, true, false], // Test pagination [[ 'itemType' => 'folder', 'page' => 1, - ], '', 'yes', true, true, $allTypes, false, true, true], + ], '', 'yes', false, true, true, true, $allTypes, false, true, true], [[ 'itemType' => 'folder', 'page' => 10, - ], '', 'yes', true, true, $allTypes, false, true, true], + ], '', 'yes', false, true, true, true, $allTypes, false, true, true], // Test perPage [[ 'itemType' => 'folder', 'perPage' => 1, - ], '', 'yes', true, true, $allTypes, false, true, true], + ], '', 'yes', false, true, true, true, $allTypes, false, true, true], [[ 'itemType' => 'folder', 'perPage' => 10, - ], '', 'yes', true, true, $allTypes, false, true, true], + ], '', 'yes', false, true, true, true, $allTypes, false, true, true], // Test $shareWithGroupOnly setting [[ 'itemType' => 'folder', - ], 'no', 'yes', true, true, $allTypes, false, true, true], + ], 'no', 'yes', false, true, true, true, $allTypes, false, true, true], [[ 'itemType' => 'folder', - ], 'yes', 'yes', true, true, $allTypes, true, true, true], + ], 'yes', 'yes', false, true, true, true, $allTypes, true, true, true], // Test $shareeEnumeration setting [[ 'itemType' => 'folder', - ], 'no', 'yes', true, true, $allTypes, false, true, true], + ], 'no', 'yes', false, true, true, true, $allTypes, false, true, true], [[ 'itemType' => 'folder', - ], 'no', 'no', true, true, $allTypes, false, false, true], + ], 'no', 'no', false, true, true, true, $allTypes, false, false, true], + ]; } /** - * @dataProvider dataSearch * * @param array $getData * @param string $apiSetting * @param string $enumSetting * @param bool $remoteSharingEnabled + * @param bool $isRemoteGroupSharingEnabled * @param bool $emailSharingEnabled * @param array $shareTypes * @param bool $shareWithGroupOnly * @param bool $shareeEnumeration * @param bool $allowGroupSharing + * @throws OCSBadRequestException */ - public function testSearch($getData, $apiSetting, $enumSetting, $remoteSharingEnabled, $emailSharingEnabled, $shareTypes, $shareWithGroupOnly, $shareeEnumeration, $allowGroupSharing) { - $search = isset($getData['search']) ? $getData['search'] : ''; - $itemType = isset($getData['itemType']) ? $getData['itemType'] : 'irrelevant'; - $page = isset($getData['page']) ? $getData['page'] : 1; - $perPage = isset($getData['perPage']) ? $getData['perPage'] : 200; - $shareType = isset($getData['shareType']) ? $getData['shareType'] : null; - - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataSearch')] + public function testSearch( + array $getData, + string $apiSetting, + string $enumSetting, + bool $sharingDisabledForUser, + bool $remoteSharingEnabled, + bool $isRemoteGroupSharingEnabled, + bool $emailSharingEnabled, + array $shareTypes, + bool $shareWithGroupOnly, + bool $shareeEnumeration, + bool $allowGroupSharing, + ): void { + $search = $getData['search'] ?? ''; + $itemType = $getData['itemType'] ?? 'irrelevant'; + $page = $getData['page'] ?? 1; + $perPage = $getData['perPage'] ?? 200; + $shareType = $getData['shareType'] ?? null; + + $globalConfig = $this->createMock(GlobalScaleIConfig::class); + $globalConfig->expects(self::once()) + ->method('isGlobalScaleEnabled') + ->willReturn(true); + $this->overwriteService(GlobalScaleIConfig::class, $globalConfig); + + /** @var IConfig|MockObject $config */ $config = $this->createMock(IConfig::class); - $config->expects($this->exactly(2)) - ->method('getAppValue') - ->with('core', $this->anything(), $this->anything()) - ->willReturnMap([ - ['core', 'shareapi_only_share_with_group_members', 'no', $apiSetting], - ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', $enumSetting], - ]); - - $this->shareManager->expects($itemType === 'file' || $itemType === 'folder' ? $this->once() : $this->never()) + + $this->shareManager->expects($this->once()) ->method('allowGroupSharing') ->willReturn($allowGroupSharing); - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */ + /** @var string */ + $uid = 'test123'; + /** @var IRequest|MockObject $request */ $request = $this->createMock(IRequest::class); - /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject $urlGenerator */ + /** @var IURLGenerator|MockObject $urlGenerator */ $urlGenerator = $this->createMock(IURLGenerator::class); - /** @var \PHPUnit_Framework_MockObject_MockObject|\OCA\Files_Sharing\Controller\ShareesAPIController $sharees */ - $sharees = $this->getMockBuilder('\OCA\Files_Sharing\Controller\ShareesAPIController') + /** @var MockObject|ShareesAPIController $sharees */ + $sharees = $this->getMockBuilder(ShareesAPIController::class) ->setConstructorArgs([ 'files_sharing', $request, + $uid, $config, $urlGenerator, $this->shareManager, $this->collaboratorSearch ]) - ->setMethods(['isRemoteSharingAllowed', 'shareProviderExists']) + ->onlyMethods(['isRemoteSharingAllowed', 'isRemoteGroupSharingAllowed']) ->getMock(); + $expectedShareTypes = $shareTypes; + sort($expectedShareTypes); + $this->collaboratorSearch->expects($this->once()) ->method('search') - ->with($search, $shareTypes, $this->anything(), $perPage, $perPage * ($page -1)) + ->with($search, $expectedShareTypes, $this->anything(), $perPage, $perPage * ($page - 1)) ->willReturn([[], false]); $sharees->expects($this->any()) @@ -264,18 +278,31 @@ class ShareesAPIControllerTest extends TestCase { ->with($itemType) ->willReturn($remoteSharingEnabled); - $this->shareManager->expects($this->any()) - ->method('shareProviderExists') - ->with(\OCP\Share::SHARE_TYPE_EMAIL) - ->willReturn($emailSharingEnabled); - $this->assertInstanceOf(Http\DataResponse::class, $sharees->search($search, $itemType, $page, $perPage, $shareType)); + $sharees->expects($this->any()) + ->method('isRemoteGroupSharingAllowed') + ->with($itemType) + ->willReturn($isRemoteGroupSharingEnabled); - $this->assertSame($shareWithGroupOnly, $this->invokePrivate($sharees, 'shareWithGroupOnly')); - $this->assertSame($shareeEnumeration, $this->invokePrivate($sharees, 'shareeEnumeration')); + $this->shareManager->expects($this->any()) + ->method('sharingDisabledForUser') + ->with($uid) + ->willReturn($sharingDisabledForUser); + + $this->shareManager->expects($this->any()) + ->method('shareProviderExists') + ->willReturnCallback(function ($shareType) use ($emailSharingEnabled) { + if ($shareType === IShare::TYPE_EMAIL) { + return $emailSharingEnabled; + } else { + return false; + } + }); + + $this->assertInstanceOf(DataResponse::class, $sharees->search($search, $itemType, $page, $perPage, $shareType)); } - public function dataSearchInvalid() { + public static function dataSearchInvalid(): array { return [ // Test invalid pagination [[ @@ -302,36 +329,39 @@ class ShareesAPIControllerTest extends TestCase { } /** - * @dataProvider dataSearchInvalid * * @param array $getData * @param string $message */ - public function testSearchInvalid($getData, $message) { - $page = isset($getData['page']) ? $getData['page'] : 1; - $perPage = isset($getData['perPage']) ? $getData['perPage'] : 200; + #[\PHPUnit\Framework\Attributes\DataProvider('dataSearchInvalid')] + public function testSearchInvalid($getData, $message): void { + $page = $getData['page'] ?? 1; + $perPage = $getData['perPage'] ?? 200; - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */ + /** @var IConfig|MockObject $config */ $config = $this->createMock(IConfig::class); $config->expects($this->never()) ->method('getAppValue'); - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */ + /** @var string */ + $uid = 'test123'; + /** @var IRequest|MockObject $request */ $request = $this->createMock(IRequest::class); - /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject $urlGenerator */ + /** @var IURLGenerator|MockObject $urlGenerator */ $urlGenerator = $this->createMock(IURLGenerator::class); - /** @var \PHPUnit_Framework_MockObject_MockObject|\OCA\Files_Sharing\Controller\ShareesAPIController $sharees */ + /** @var MockObject|ShareesAPIController $sharees */ $sharees = $this->getMockBuilder('\OCA\Files_Sharing\Controller\ShareesAPIController') ->setConstructorArgs([ 'files_sharing', $request, + $uid, $config, $urlGenerator, $this->shareManager, $this->collaboratorSearch ]) - ->setMethods(['isRemoteSharingAllowed']) + ->onlyMethods(['isRemoteSharingAllowed']) ->getMock(); $sharees->expects($this->never()) ->method('isRemoteSharingAllowed'); @@ -347,7 +377,7 @@ class ShareesAPIControllerTest extends TestCase { } } - public function dataIsRemoteSharingAllowed() { + public static function dataIsRemoteSharingAllowed() { return [ ['file', true], ['folder', true], @@ -357,24 +387,40 @@ class ShareesAPIControllerTest extends TestCase { } /** - * @dataProvider dataIsRemoteSharingAllowed * * @param string $itemType * @param bool $expected */ - public function testIsRemoteSharingAllowed($itemType, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataIsRemoteSharingAllowed')] + public function testIsRemoteSharingAllowed($itemType, $expected): void { $this->assertSame($expected, $this->invokePrivate($this->sharees, 'isRemoteSharingAllowed', [$itemType])); } - /** - * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException - * @expectedExceptionMessage Missing itemType - */ - public function testSearchNoItemType() { + public function testSearchSharingDisabled(): void { + $this->shareManager->expects($this->once()) + ->method('sharingDisabledForUser') + ->with($this->uid) + ->willReturn(true); + + $this->config->expects($this->once()) + ->method('getSystemValueInt') + ->with('sharing.minSearchStringLength', 0) + ->willReturn(0); + + $this->shareManager->expects($this->never()) + ->method('allowGroupSharing'); + + $this->assertInstanceOf(DataResponse::class, $this->sharees->search('', null, 1, 10, [], false)); + } + + public function testSearchNoItemType(): void { + $this->expectException(OCSBadRequestException::class); + $this->expectExceptionMessage('Missing itemType'); + $this->sharees->search('', null, 1, 10, [], false); } - public function dataGetPaginationLink() { + public static function dataGetPaginationLink() { return [ [1, '/ocs/v1.php', ['perPage' => 2], '<?perPage=2&page=2>; rel="next"'], [10, '/ocs/v2.php', ['perPage' => 2], '<?perPage=2&page=11>; rel="next"'], @@ -382,14 +428,14 @@ class ShareesAPIControllerTest extends TestCase { } /** - * @dataProvider dataGetPaginationLink * * @param int $page * @param string $scriptName * @param array $params * @param array $expected */ - public function testGetPaginationLink($page, $scriptName, $params, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetPaginationLink')] + public function testGetPaginationLink($page, $scriptName, $params, $expected): void { $this->request->expects($this->once()) ->method('getScriptName') ->willReturn($scriptName); @@ -397,7 +443,7 @@ class ShareesAPIControllerTest extends TestCase { $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'getPaginationLink', [$page, $params])); } - public function dataIsV2() { + public static function dataIsV2() { return [ ['/ocs/v1.php', false], ['/ocs/v2.php', true], @@ -405,12 +451,12 @@ class ShareesAPIControllerTest extends TestCase { } /** - * @dataProvider dataIsV2 * * @param string $scriptName * @param bool $expected */ - public function testIsV2($scriptName, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataIsV2')] + public function testIsV2($scriptName, $expected): void { $this->request->expects($this->once()) ->method('getScriptName') ->willReturn($scriptName); diff --git a/apps/files_sharing/tests/DeleteOrphanedSharesJobTest.php b/apps/files_sharing/tests/DeleteOrphanedSharesJobTest.php index 246fe816e54..c245d157151 100644 --- a/apps/files_sharing/tests/DeleteOrphanedSharesJobTest.php +++ b/apps/files_sharing/tests/DeleteOrphanedSharesJobTest.php @@ -1,31 +1,21 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; +use OC\Files\Filesystem; +use OC\SystemConfig; use OCA\Files_Sharing\DeleteOrphanedSharesJob; +use OCP\App\IAppManager; +use OCP\Constants; +use OCP\IDBConnection; +use OCP\IUserManager; +use OCP\Server; +use OCP\Share\IShare; /** * Class DeleteOrphanedSharesJobTest @@ -35,7 +25,6 @@ use OCA\Files_Sharing\DeleteOrphanedSharesJob; * @package OCA\Files_Sharing\Tests */ class DeleteOrphanedSharesJobTest extends \Test\TestCase { - /** * @var bool */ @@ -47,7 +36,7 @@ class DeleteOrphanedSharesJobTest extends \Test\TestCase { private $job; /** - * @var \OCP\IDBConnection + * @var IDBConnection */ private $connection; @@ -61,50 +50,50 @@ class DeleteOrphanedSharesJobTest extends \Test\TestCase { */ private $user2; - public static function setUpBeforeClass() { - $appManager = \OC::$server->getAppManager(); + public static function setUpBeforeClass(): void { + $appManager = Server::get(IAppManager::class); self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin'); $appManager->disableApp('files_trashbin'); // just in case... - \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); + Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); } - public static function tearDownAfterClass() { + public static function tearDownAfterClass(): void { if (self::$trashBinStatus) { - \OC::$server->getAppManager()->enableApp('files_trashbin'); + Server::get(IAppManager::class)->enableApp('files_trashbin'); } } - protected function setup() { + protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); // clear occasional leftover shares from other tests $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`'); $this->user1 = $this->getUniqueID('user1_'); $this->user2 = $this->getUniqueID('user2_'); - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); $userManager->createUser($this->user1, 'pass'); $userManager->createUser($this->user2, 'pass'); - \OC::registerShareHooks(); + \OC::registerShareHooks(Server::get(SystemConfig::class)); - $this->job = new DeleteOrphanedSharesJob(); + $this->job = Server::get(DeleteOrphanedSharesJob::class); } - protected function tearDown() { + protected function tearDown(): void { $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`'); - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); $user1 = $userManager->get($this->user1); - if($user1) { + if ($user1) { $user1->delete(); } $user2 = $userManager->get($this->user2); - if($user2) { + if ($user2) { $user2->delete(); } @@ -126,20 +115,23 @@ class DeleteOrphanedSharesJobTest extends \Test\TestCase { /** * Test clearing orphaned shares */ - public function testClearShares() { + public function testClearShares(): void { $this->loginAsUser($this->user1); - $view = new \OC\Files\View('/' . $this->user1 . '/'); - $view->mkdir('files/test'); - $view->mkdir('files/test/sub'); + $user1Folder = \OC::$server->getUserFolder($this->user1); + $testFolder = $user1Folder->newFolder('test'); + $testSubFolder = $testFolder->newFolder('sub'); - $fileInfo = $view->getFileInfo('files/test/sub'); - $fileId = $fileInfo->getId(); + $shareManager = Server::get(\OCP\Share\IManager::class); + $share = $shareManager->newShare(); - $this->assertTrue( - \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ), - 'Failed asserting that user 1 successfully shared "test/sub" with user 2.' - ); + $share->setNode($testSubFolder) + ->setShareType(IShare::TYPE_USER) + ->setPermissions(Constants::PERMISSION_READ) + ->setSharedWith($this->user2) + ->setSharedBy($this->user1); + + $shareManager->createShare($share); $this->assertCount(1, $this->getShares()); @@ -147,28 +139,10 @@ class DeleteOrphanedSharesJobTest extends \Test\TestCase { $this->assertCount(1, $this->getShares(), 'Linked shares not deleted'); - $view->unlink('files/test'); + $testFolder->delete(); $this->job->run([]); $this->assertCount(0, $this->getShares(), 'Orphaned shares deleted'); } - - public function testKeepNonFileShares() { - $this->loginAsUser($this->user1); - - \OC\Share\Share::registerBackend('test', 'Test\Share\Backend'); - - $this->assertTrue( - \OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ), - 'Failed asserting that user 1 successfully shared something with user 2.' - ); - - $this->assertCount(1, $this->getShares()); - - $this->job->run([]); - - $this->assertCount(1, $this->getShares(), 'Non-file shares kept'); - } } - diff --git a/apps/files_sharing/tests/EncryptedSizePropagationTest.php b/apps/files_sharing/tests/EncryptedSizePropagationTest.php index ea2d7e786a9..1be17df3957 100644 --- a/apps/files_sharing/tests/EncryptedSizePropagationTest.php +++ b/apps/files_sharing/tests/EncryptedSizePropagationTest.php @@ -1,30 +1,15 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Robin Appelman <robin@icewind.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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; use OC\Files\View; +use OCP\ITempManager; +use OCP\Server; use Test\Traits\EncryptionTrait; /** @@ -33,14 +18,27 @@ use Test\Traits\EncryptionTrait; class EncryptedSizePropagationTest extends SizePropagationTest { use EncryptionTrait; + protected function setUp(): void { + parent::setUp(); + $this->config->setAppValue('encryption', 'useMasterKey', '0'); + } + protected function setupUser($name, $password = '') { $this->createUser($name, $password); - $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder(); - $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]); - $this->config->setAppValue('encryption', 'useMasterKey', '0'); + $this->registerMountForUser($name); $this->setupForUser($name, $password); $this->loginWithEncryption($name); return new View('/' . $name . '/files'); } + private function registerMountForUser($user): void { + $tmpFolder = Server::get(ITempManager::class)->getTemporaryFolder(); + $this->registerMount($user, '\OC\Files\Storage\Local', '/' . $user, ['datadir' => $tmpFolder]); + } + + protected function loginHelper($user, $create = false, $password = false) { + $this->registerMountForUser($user); + $this->setupForUser($user, $password); + parent::loginHelper($user, $create, $password); + } } diff --git a/apps/files_sharing/tests/EtagPropagationTest.php b/apps/files_sharing/tests/EtagPropagationTest.php index 4aba9e29113..d8580ea92d5 100644 --- a/apps/files_sharing/tests/EtagPropagationTest.php +++ b/apps/files_sharing/tests/EtagPropagationTest.php @@ -1,35 +1,18 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; use OC\Files\Filesystem; use OC\Files\View; +use OCP\Constants; +use OCP\Files\IRootFolder; +use OCP\Server; +use OCP\Share\IShare; /** * Class EtagPropagationTest @@ -53,8 +36,8 @@ class EtagPropagationTest extends PropagationTestCase { $this->fileIds[self::TEST_FILES_SHARING_API_USER3] = []; $this->fileIds[self::TEST_FILES_SHARING_API_USER4] = []; - $rootFolder = \OC::$server->getRootFolder(); - $shareManager = \OC::$server->getShareManager(); + $rootFolder = Server::get(IRootFolder::class); + $shareManager = Server::get(\OCP\Share\IManager::class); $this->rootView = new View(''); $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); @@ -74,27 +57,32 @@ class EtagPropagationTest extends PropagationTestCase { ->get('/foo.txt'); $share = $shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE); - $shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE); + $share = $shareManager->createShare($share); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); $node = $rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER1) ->get('/sub1/sub2/folder'); + $share = $shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $shareManager->createShare($share); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); + $share = $shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER3) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $shareManager->createShare($share); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER3); $folderInfo = $view1->getFileInfo('/directReshare'); $this->assertInstanceOf('\OC\Files\FileInfo', $folderInfo); @@ -103,11 +91,12 @@ class EtagPropagationTest extends PropagationTestCase { ->get('/directReshare'); $share = $shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $shareManager->createShare($share); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); $this->fileIds[self::TEST_FILES_SHARING_API_USER1][''] = $view1->getFileInfo('')->getId(); $this->fileIds[self::TEST_FILES_SHARING_API_USER1]['sub1'] = $view1->getFileInfo('sub1')->getId(); @@ -120,6 +109,8 @@ class EtagPropagationTest extends PropagationTestCase { $view2 = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $view2->mkdir('/sub1/sub2'); $view2->rename('/folder', '/sub1/sub2/folder'); + $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); + $insideInfo = $view2->getFileInfo('/sub1/sub2/folder/inside'); $this->assertInstanceOf('\OC\Files\FileInfo', $insideInfo); @@ -127,11 +118,12 @@ class EtagPropagationTest extends PropagationTestCase { ->get('/sub1/sub2/folder/inside'); $share = $shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER4) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $shareManager->createShare($share); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER4); $folderInfo = $view2->getFileInfo('/directReshare'); $this->assertInstanceOf('\OC\Files\FileInfo', $folderInfo); @@ -140,11 +132,12 @@ class EtagPropagationTest extends PropagationTestCase { ->get('/directReshare'); $share = $shareManager->newShare(); $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) + ->setShareType(IShare::TYPE_USER) ->setSharedWith(self::TEST_FILES_SHARING_API_USER4) ->setSharedBy(self::TEST_FILES_SHARING_API_USER2) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $shareManager->createShare($share); + ->setPermissions(Constants::PERMISSION_ALL); + $share = $shareManager->createShare($share); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER4); $this->fileIds[self::TEST_FILES_SHARING_API_USER2][''] = $view2->getFileInfo('')->getId(); $this->fileIds[self::TEST_FILES_SHARING_API_USER2]['sub1'] = $view2->getFileInfo('sub1')->getId(); @@ -182,7 +175,7 @@ class EtagPropagationTest extends PropagationTestCase { } } - public function testOwnerWritesToShare() { + public function testOwnerWritesToShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); Filesystem::file_put_contents('/sub1/sub2/folder/asd.txt', 'bar'); $this->assertEtagsNotChanged([self::TEST_FILES_SHARING_API_USER4]); @@ -192,7 +185,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerWritesToSingleFileShare() { + public function testOwnerWritesToSingleFileShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); Filesystem::file_put_contents('/foo.txt', 'longer_bar'); $t = (int)Filesystem::filemtime('/foo.txt') - 1; @@ -203,7 +196,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerWritesToShareWithReshare() { + public function testOwnerWritesToShareWithReshare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); Filesystem::file_put_contents('/sub1/sub2/folder/inside/bar.txt', 'bar'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -212,7 +205,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerRenameInShare() { + public function testOwnerRenameInShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); $this->assertEtagsNotChanged([self::TEST_FILES_SHARING_API_USER4]); Filesystem::rename('/sub1/sub2/folder/file.txt', '/sub1/sub2/folder/renamed.txt'); @@ -222,7 +215,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerRenameInReShare() { + public function testOwnerRenameInReShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); Filesystem::rename('/sub1/sub2/folder/inside/file.txt', '/sub1/sub2/folder/inside/renamed.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -231,7 +224,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerRenameIntoReShare() { + public function testOwnerRenameIntoReShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); Filesystem::rename('/sub1/sub2/folder/file.txt', '/sub1/sub2/folder/inside/renamed.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -240,7 +233,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerRenameOutOfReShare() { + public function testOwnerRenameOutOfReShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); Filesystem::rename('/sub1/sub2/folder/inside/file.txt', '/sub1/sub2/folder/renamed.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -249,7 +242,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerDeleteInShare() { + public function testOwnerDeleteInShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); Filesystem::unlink('/sub1/sub2/folder/file.txt'); $this->assertEtagsNotChanged([self::TEST_FILES_SHARING_API_USER4]); @@ -259,7 +252,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerDeleteInReShare() { + public function testOwnerDeleteInReShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); Filesystem::unlink('/sub1/sub2/folder/inside/file.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -268,14 +261,14 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerUnshares() { + public function testOwnerUnshares(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); $folderInfo = $this->rootView->getFileInfo('/' . self::TEST_FILES_SHARING_API_USER1 . '/files/sub1/sub2/folder'); $this->assertInstanceOf('\OC\Files\FileInfo', $folderInfo); $node = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1)->get('/sub1/sub2/folder'); - $shareManager = \OC::$server->getShareManager(); - $shares = $shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER, $node, true); + $shareManager = Server::get(\OCP\Share\IManager::class); + $shares = $shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER, $node, true); foreach ($shares as $share) { if ($share->getSharedWith() === self::TEST_FILES_SHARING_API_USER2) { @@ -291,14 +284,14 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testOwnerUnsharesFlatReshares() { + public function testOwnerUnsharesFlatReshares(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); $folderInfo = $this->rootView->getFileInfo('/' . self::TEST_FILES_SHARING_API_USER1 . '/files/sub1/sub2/folder/inside'); $this->assertInstanceOf('\OC\Files\FileInfo', $folderInfo); $node = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1)->get('/sub1/sub2/folder/inside'); - $shareManager = \OC::$server->getShareManager(); - $shares = $shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER, $node, true); + $shareManager = Server::get(\OCP\Share\IManager::class); + $shares = $shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER, $node, true); foreach ($shares as $share) { $shareManager->deleteShare($share); @@ -312,7 +305,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientUnsharesFromSelf() { + public function testRecipientUnsharesFromSelf(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); $ls = $this->rootView->getDirectoryContent('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub1/sub2/'); $this->assertTrue( @@ -326,7 +319,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientWritesToShare() { + public function testRecipientWritesToShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); Filesystem::file_put_contents('/sub1/sub2/folder/asd.txt', 'bar'); $this->assertEtagsNotChanged([self::TEST_FILES_SHARING_API_USER4]); @@ -339,7 +332,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientWritesToReshare() { + public function testRecipientWritesToReshare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); Filesystem::file_put_contents('/sub1/sub2/folder/inside/asd.txt', 'bar'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -348,7 +341,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientWritesToOtherRecipientsReshare() { + public function testRecipientWritesToOtherRecipientsReshare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER3); Filesystem::file_put_contents('/sub1/sub2/folder/inside/asd.txt', 'bar'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -357,7 +350,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientRenameInShare() { + public function testRecipientRenameInShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); Filesystem::rename('/sub1/sub2/folder/file.txt', '/sub1/sub2/folder/renamed.txt'); $this->assertEtagsNotChanged([self::TEST_FILES_SHARING_API_USER4]); @@ -367,7 +360,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientRenameInReShare() { + public function testRecipientRenameInReShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); Filesystem::rename('/sub1/sub2/folder/inside/file.txt', '/sub1/sub2/folder/inside/renamed.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -376,7 +369,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientRenameResharedFolder() { + public function testRecipientRenameResharedFolder(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); Filesystem::rename('/directReshare', '/sub1/directReshare'); $this->assertEtagsNotChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_USER4]); @@ -387,7 +380,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientDeleteInShare() { + public function testRecipientDeleteInShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); Filesystem::unlink('/sub1/sub2/folder/file.txt'); $this->assertEtagsNotChanged([self::TEST_FILES_SHARING_API_USER4]); @@ -397,7 +390,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientDeleteInReShare() { + public function testRecipientDeleteInReShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); Filesystem::unlink('/sub1/sub2/folder/inside/file.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -406,7 +399,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testReshareRecipientWritesToReshare() { + public function testReshareRecipientWritesToReshare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER4); Filesystem::file_put_contents('/sub1/sub2/inside/asd.txt', 'bar'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -415,7 +408,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testReshareRecipientRenameInReShare() { + public function testReshareRecipientRenameInReShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER4); Filesystem::rename('/sub1/sub2/inside/file.txt', '/sub1/sub2/inside/renamed.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -424,7 +417,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testReshareRecipientDeleteInReShare() { + public function testReshareRecipientDeleteInReShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER4); Filesystem::unlink('/sub1/sub2/inside/file.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -433,7 +426,7 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientUploadInDirectReshare() { + public function testRecipientUploadInDirectReshare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); Filesystem::file_put_contents('/directReshare/test.txt', 'sad'); $this->assertEtagsNotChanged([self::TEST_FILES_SHARING_API_USER3]); @@ -442,19 +435,19 @@ class EtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testEtagChangeOnPermissionsChange() { + public function testEtagChangeOnPermissionsChange(): void { $userFolder = $this->rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER1); $node = $userFolder->get('/sub1/sub2/folder'); - $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER, $node); - /** @var \OCP\Share\IShare[] $shares */ - $shares = array_filter($shares, function(\OCP\Share\IShare $share) { + $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER, $node); + /** @var IShare[] $shares */ + $shares = array_filter($shares, function (IShare $share) { return $share->getSharedWith() === self::TEST_FILES_SHARING_API_USER2; }); $this->assertCount(1, $shares); $share = $shares[0]; - $share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE); + $share->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE); $this->shareManager->updateShare($share); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER2]); diff --git a/apps/files_sharing/tests/ExpireSharesJobTest.php b/apps/files_sharing/tests/ExpireSharesJobTest.php index 46b50c5fac4..42bc5a4b659 100644 --- a/apps/files_sharing/tests/ExpireSharesJobTest.php +++ b/apps/files_sharing/tests/ExpireSharesJobTest.php @@ -1,30 +1,21 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; +use OC\SystemConfig; use OCA\Files_Sharing\ExpireSharesJob; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Constants; +use OCP\IDBConnection; +use OCP\IUserManager; +use OCP\Server; +use OCP\Share\IManager; +use OCP\Share\IShare; /** * Class ExpireSharesJobTest @@ -35,55 +26,47 @@ use OCA\Files_Sharing\ExpireSharesJob; */ class ExpireSharesJobTest extends \Test\TestCase { - /** - * @var ExpireSharesJob - */ + /** @var ExpireSharesJob */ private $job; - /** - * @var \OCP\IDBConnection - */ + /** @var IDBConnection */ private $connection; - /** - * @var string - */ + /** @var string */ private $user1; - /** - * @var string - */ + /** @var string */ private $user2; - protected function setup() { + protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); // clear occasional leftover shares from other tests $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`'); $this->user1 = $this->getUniqueID('user1_'); $this->user2 = $this->getUniqueID('user2_'); - $userManager = \OC::$server->getUserManager(); - $userManager->createUser($this->user1, 'pass'); - $userManager->createUser($this->user2, 'pass'); + $userManager = Server::get(IUserManager::class); + $userManager->createUser($this->user1, 'longrandompassword'); + $userManager->createUser($this->user2, 'longrandompassword'); - \OC::registerShareHooks(); + \OC::registerShareHooks(Server::get(SystemConfig::class)); - $this->job = new ExpireSharesJob(); + $this->job = new ExpireSharesJob(Server::get(ITimeFactory::class), Server::get(IManager::class), $this->connection); } - protected function tearDown() { + protected function tearDown(): void { $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`'); - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); $user1 = $userManager->get($this->user1); - if($user1) { + if ($user1) { $user1->delete(); } $user2 = $userManager->get($this->user2); - if($user2) { + if ($user2) { $user2->delete(); } @@ -107,7 +90,7 @@ class ExpireSharesJobTest extends \Test\TestCase { return $shares; } - public function dataExpireLinkShare() { + public static function dataExpireLinkShare() { return [ [false, '', false, false], [false, '', true, false], @@ -123,25 +106,28 @@ class ExpireSharesJobTest extends \Test\TestCase { } /** - * @dataProvider dataExpireLinkShare * * @param bool addExpiration Should we add an expire date * @param string $interval The dateInterval * @param bool $addInterval If true add to the current time if false subtract * @param bool $shouldExpire Should this share be expired */ - public function testExpireLinkShare($addExpiration, $interval, $addInterval, $shouldExpire) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataExpireLinkShare')] + public function testExpireLinkShare($addExpiration, $interval, $addInterval, $shouldExpire): void { $this->loginAsUser($this->user1); - $view = new \OC\Files\View('/' . $this->user1 . '/'); - $view->mkdir('files/test'); + $user1Folder = \OC::$server->getUserFolder($this->user1); + $testFolder = $user1Folder->newFolder('test'); + + $shareManager = Server::get(\OCP\Share\IManager::class); + $share = $shareManager->newShare(); - $fileInfo = $view->getFileInfo('files/test'); + $share->setNode($testFolder) + ->setShareType(IShare::TYPE_LINK) + ->setPermissions(Constants::PERMISSION_READ) + ->setSharedBy($this->user1); - $this->assertNotNull( - \OC\Share\Share::shareItem('folder', $fileInfo->getId(), \OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ), - 'Failed asserting that user 1 successfully shared "test" by link.' - ); + $shareManager->createShare($share); $shares = $this->getShares(); $this->assertCount(1, $shares); @@ -184,23 +170,25 @@ class ExpireSharesJobTest extends \Test\TestCase { } } - public function testDoNotExpireOtherShares() { + public function testDoNotExpireOtherShares(): void { $this->loginAsUser($this->user1); - $view = new \OC\Files\View('/' . $this->user1 . '/'); - $view->mkdir('files/test'); + $user1Folder = \OC::$server->getUserFolder($this->user1); + $testFolder = $user1Folder->newFolder('test'); - $fileInfo = $view->getFileInfo('files/test'); + $shareManager = Server::get(\OCP\Share\IManager::class); + $share = $shareManager->newShare(); - $this->assertNotNull( - \OC\Share\Share::shareItem('folder', $fileInfo->getId(), \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ), - 'Failed asserting that user 1 successfully shared "test" by link with user2.' - ); + $share->setNode($testFolder) + ->setShareType(IShare::TYPE_USER) + ->setPermissions(Constants::PERMISSION_READ) + ->setSharedBy($this->user1) + ->setSharedWith($this->user2); + + $shareManager->createShare($share); $shares = $this->getShares(); $this->assertCount(1, $shares); - reset($shares); - $share = current($shares); $this->logout(); @@ -209,6 +197,4 @@ class ExpireSharesJobTest extends \Test\TestCase { $shares = $this->getShares(); $this->assertCount(1, $shares); } - } - diff --git a/apps/files_sharing/tests/External/CacheTest.php b/apps/files_sharing/tests/External/CacheTest.php index 0129e760648..39e2057a24c 100644 --- a/apps/files_sharing/tests/External/CacheTest.php +++ b/apps/files_sharing/tests/External/CacheTest.php @@ -1,32 +1,23 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Robin Appelman <robin@icewind.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_Sharing\Tests\External; use OC\Federation\CloudIdManager; +use OC\Files\Storage\Storage; +use OCA\Files_Sharing\External\Cache; use OCA\Files_Sharing\Tests\TestCase; +use OCP\Contacts\IManager; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudIdManager; +use OCP\Files\Cache\ICacheEntry; +use OCP\ICacheFactory; +use OCP\IURLGenerator; +use OCP\IUserManager; /** * Class Cache @@ -36,14 +27,16 @@ use OCP\Federation\ICloudIdManager; * @package OCA\Files_Sharing\Tests\External */ class CacheTest extends TestCase { + /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ + protected $contactsManager; /** - * @var \OC\Files\Storage\Storage + * @var Storage **/ private $storage; /** - * @var \OCA\Files_Sharing\External\Cache + * @var Cache */ private $cache; @@ -52,13 +45,21 @@ class CacheTest extends TestCase { */ private $remoteUser; - /** @var ICloudIdManager */ + /** @var ICloudIdManager */ private $cloudIdManager; - protected function setUp() { + protected function setUp(): void { parent::setUp(); - $this->cloudIdManager = new CloudIdManager(); + $this->contactsManager = $this->createMock(IManager::class); + + $this->cloudIdManager = new CloudIdManager( + $this->createMock(ICacheFactory::class), + $this->createMock(IEventDispatcher::class), + $this->contactsManager, + $this->createMock(IURLGenerator::class), + $this->createMock(IUserManager::class), + ); $this->remoteUser = $this->getUniqueID('remoteuser'); $this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage') @@ -67,29 +68,35 @@ class CacheTest extends TestCase { $this->storage ->expects($this->any()) ->method('getId') - ->will($this->returnValue('dummystorage::')); - $this->cache = new \OCA\Files_Sharing\External\Cache( + ->willReturn('dummystorage::'); + + $this->contactsManager->expects($this->any()) + ->method('search') + ->willReturn([]); + + $this->cache = new Cache( $this->storage, $this->cloudIdManager->getCloudId($this->remoteUser, 'http://example.com/owncloud') ); + $this->cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $this->cache->put( 'test.txt', - array( + [ 'mimetype' => 'text/plain', 'size' => 5, 'mtime' => 123, - ) + ] ); } - protected function tearDown() { + protected function tearDown(): void { if ($this->cache) { $this->cache->clear(); } parent::tearDown(); } - public function testGetInjectsOwnerDisplayName() { + public function testGetInjectsOwnerDisplayName(): void { $info = $this->cache->get('test.txt'); $this->assertEquals( $this->remoteUser . '@example.com/owncloud', @@ -97,27 +104,27 @@ class CacheTest extends TestCase { ); } - public function testGetReturnsFalseIfNotFound() { + public function testGetReturnsFalseIfNotFound(): void { $info = $this->cache->get('unexisting-entry.txt'); $this->assertFalse($info); } - public function testGetFolderPopulatesOwner() { + public function testGetFolderPopulatesOwner(): void { $dirId = $this->cache->put( 'subdir', - array( + [ 'mimetype' => 'httpd/unix-directory', 'size' => 5, 'mtime' => 123, - ) + ] ); $this->cache->put( 'subdir/contents.txt', - array( + [ 'mimetype' => 'text/plain', 'size' => 5, 'mtime' => 123, - ) + ] ); $results = $this->cache->getFolderContentsById($dirId); @@ -127,5 +134,4 @@ class CacheTest extends TestCase { $results[0]['displayname_owner'] ); } - } diff --git a/apps/files_sharing/tests/External/ManagerTest.php b/apps/files_sharing/tests/External/ManagerTest.php index 6756f91fb70..14c6afec4d8 100644 --- a/apps/files_sharing/tests/External/ManagerTest.php +++ b/apps/files_sharing/tests/External/ManagerTest.php @@ -1,38 +1,41 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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 OCA\Files_Sharing\Tests\External; use OC\Federation\CloudIdManager; +use OC\Files\Mount\MountPoint; +use OC\Files\SetupManagerFactory; use OC\Files\Storage\StorageFactory; +use OC\Files\Storage\Temporary; use OCA\Files_Sharing\External\Manager; use OCA\Files_Sharing\External\MountProvider; use OCA\Files_Sharing\Tests\TestCase; +use OCP\Contacts\IManager; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Federation\ICloudFederationFactory; +use OCP\Federation\ICloudFederationProviderManager; +use OCP\Files\NotFoundException; +use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; +use OCP\Http\Client\IResponse; +use OCP\ICacheFactory; +use OCP\IDBConnection; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IURLGenerator; +use OCP\IUser; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\OCS\IDiscoveryService; +use OCP\Server; +use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\Traits\UserTrait; /** @@ -45,75 +48,172 @@ use Test\Traits\UserTrait; class ManagerTest extends TestCase { use UserTrait; - /** @var Manager **/ - private $manager; - - /** @var \OC\Files\Mount\Manager */ - private $mountManager; - - /** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */ - private $clientService; - - private $uid; - - /** - * @var \OCP\IUser - */ - private $user; - private $testMountProvider; - - protected function setUp() { + protected string $uid; + protected IUser $user; + protected MountProvider $testMountProvider; + protected IEventDispatcher&MockObject $eventDispatcher; + protected LoggerInterface&MockObject $logger; + protected \OC\Files\Mount\Manager $mountManager; + protected IManager&MockObject $contactsManager; + protected Manager&MockObject $manager; + protected IClientService&MockObject $clientService; + protected ICloudFederationProviderManager&MockObject $cloudFederationProviderManager; + protected ICloudFederationFactory&MockObject $cloudFederationFactory; + protected IGroupManager&MockObject $groupManager; + protected IUserManager&MockObject $userManager; + + protected function setUp(): void { parent::setUp(); $this->uid = $this->getUniqueID('user'); - $this->createUser($this->uid, ''); - $this->user = \OC::$server->getUserManager()->get($this->uid); - $this->mountManager = new \OC\Files\Mount\Manager(); + $this->user = $this->createUser($this->uid, ''); + $this->mountManager = new \OC\Files\Mount\Manager($this->createMock(SetupManagerFactory::class)); $this->clientService = $this->getMockBuilder(IClientService::class) ->disableOriginalConstructor()->getMock(); + $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class); + $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + + $this->contactsManager = $this->createMock(IManager::class); + // needed for MountProvider() initialization + $this->contactsManager->expects($this->any()) + ->method('search') + ->willReturn([]); + + $this->logger = $this->createMock(LoggerInterface::class); + $this->logger->expects($this->never())->method('emergency'); + + $this->manager = $this->createManagerForUser($this->uid); - $this->manager = new Manager( - \OC::$server->getDatabaseConnection(), - $this->mountManager, - new StorageFactory(), - $this->clientService, - \OC::$server->getNotificationManager(), - \OC::$server->query(\OCP\OCS\IDiscoveryService::class), - $this->uid - ); - $this->testMountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function() { + $this->testMountProvider = new MountProvider(Server::get(IDBConnection::class), function () { return $this->manager; - }, new CloudIdManager()); + }, new CloudIdManager( + $this->createMock(ICacheFactory::class), + $this->createMock(IEventDispatcher::class), + $this->contactsManager, + $this->createMock(IURLGenerator::class), + $this->userManager, + )); + + $group1 = $this->createMock(IGroup::class); + $group1->expects($this->any())->method('getGID')->willReturn('group1'); + $group1->expects($this->any())->method('inGroup')->with($this->user)->willReturn(true); + + $group2 = $this->createMock(IGroup::class); + $group2->expects($this->any())->method('getGID')->willReturn('group2'); + $group2->expects($this->any())->method('inGroup')->with($this->user)->willReturn(true); + + $this->userManager->expects($this->any())->method('get')->willReturn($this->user); + $this->groupManager->expects($this->any())->method(('getUserGroups'))->willReturn([$group1, $group2]); + $this->groupManager->expects($this->any())->method(('get')) + ->willReturnMap([ + ['group1', $group1], + ['group2', $group2], + ]); + } + + protected function tearDown(): void { + // clear the share external table to avoid side effects + $query = Server::get(IDBConnection::class)->prepare('DELETE FROM `*PREFIX*share_external`'); + $result = $query->execute(); + $result->closeCursor(); + + parent::tearDown(); + } + + private function createManagerForUser($userId) { + $user = $this->createMock(IUser::class); + $user->method('getUID') + ->willReturn($userId); + $userSession = $this->createMock(IUserSession::class); + $userSession->method('getUser') + ->willReturn($user); + + return $this->getMockBuilder(Manager::class) + ->setConstructorArgs( + [ + Server::get(IDBConnection::class), + $this->mountManager, + new StorageFactory(), + $this->clientService, + Server::get(\OCP\Notification\IManager::class), + Server::get(IDiscoveryService::class), + $this->cloudFederationProviderManager, + $this->cloudFederationFactory, + $this->groupManager, + $this->userManager, + $userSession, + $this->eventDispatcher, + $this->logger, + ] + )->onlyMethods(['tryOCMEndPoint'])->getMock(); } private function setupMounts() { + $this->clearMounts(); $mounts = $this->testMountProvider->getMountsForUser($this->user, new StorageFactory()); foreach ($mounts as $mount) { $this->mountManager->addMount($mount); } } - public function testAddShare() { + private function clearMounts() { + $this->mountManager->clear(); + $this->mountManager->addMount(new MountPoint(Temporary::class, '', [])); + } - $shareData1 = [ + public function testAddUserShare(): void { + $this->doTestAddShare([ 'remote' => 'http://localhost', 'token' => 'token1', 'password' => '', 'name' => '/SharedFolder', 'owner' => 'foobar', + 'shareType' => IShare::TYPE_USER, 'accepted' => false, 'user' => $this->uid, - ]; + 'remoteId' => '2342' + ], false); + } + + public function testAddGroupShare(): void { + $this->doTestAddShare([ + 'remote' => 'http://localhost', + 'token' => 'token1', + 'password' => '', + 'name' => '/SharedFolder', + 'owner' => 'foobar', + 'shareType' => IShare::TYPE_GROUP, + 'accepted' => false, + 'user' => 'group1', + 'remoteId' => '2342' + ], true); + } + + public function doTestAddShare($shareData1, $isGroup = false) { $shareData2 = $shareData1; $shareData2['token'] = 'token2'; $shareData3 = $shareData1; $shareData3['token'] = 'token3'; + if ($isGroup) { + $this->manager->expects($this->never())->method('tryOCMEndPoint'); + } else { + $this->manager->expects(self::atLeast(2)) + ->method('tryOCMEndPoint') + ->willReturnMap([ + ['http://localhost', 'token1', '2342', 'accept', false], + ['http://localhost', 'token3', '2342', 'decline', false], + ]); + } + // Add a share for "user" $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1)); $openShares = $this->manager->getOpenShares(); $this->assertCount(1, $openShares); - $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); + $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}', $shareData1['user']); $this->setupMounts(); $this->assertNotMount('SharedFolder'); @@ -123,39 +223,54 @@ class ManagerTest extends TestCase { $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2)); $openShares = $this->manager->getOpenShares(); $this->assertCount(2, $openShares); - $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); + $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}', $shareData1['user']); // New share falls back to "-1" appendix, because the name is already taken - $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1'); + $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']); $this->setupMounts(); $this->assertNotMount('SharedFolder'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1'); - $client = $this->getMockBuilder('OCP\Http\Client\IClient') - ->disableOriginalConstructor()->getMock(); - $this->clientService->expects($this->at(0)) + $newClientCalls = []; + $this->clientService ->method('newClient') - ->willReturn($client); - $response = $this->getMockBuilder('OCP\Http\Client\IResponse') - ->disableOriginalConstructor()->getMock(); - $client->expects($this->once()) - ->method('post') - ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything()) - ->willReturn($response); + ->willReturnCallback(function () use (&$newClientCalls): IClient { + if (!empty($newClientCalls)) { + return array_shift($newClientCalls); + } + return $this->createMock(IClient::class); + }); + if (!$isGroup) { + $client = $this->createMock(IClient::class); + $newClientCalls[] = $client; + $response = $this->createMock(IResponse::class); + $response->method('getBody') + ->willReturn(json_encode([ + 'ocs' => [ + 'meta' => [ + 'statuscode' => 200, + ] + ] + ])); + $client->expects($this->once()) + ->method('post') + ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything()) + ->willReturn($response); + } // Accept the first share - $this->manager->acceptShare($openShares[0]['id']); + $this->assertTrue($this->manager->acceptShare($openShares[0]['id'])); // Check remaining shares - Accepted $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]); $this->assertCount(1, $acceptedShares); $shareData1['accepted'] = true; - $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']); + $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name'], $this->uid); // Check remaining shares - Open $openShares = $this->manager->getOpenShares(); $this->assertCount(1, $openShares); - $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1'); + $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']); $this->setupMounts(); $this->assertMount($shareData1['name']); @@ -166,29 +281,39 @@ class ManagerTest extends TestCase { $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3)); $openShares = $this->manager->getOpenShares(); $this->assertCount(2, $openShares); - $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1'); - // New share falls back to the original name (no "-\d", because the name is not taken) - $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}'); + $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']); + if (!$isGroup) { + // New share falls back to the original name (no "-\d", because the name is not taken) + $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}', $shareData3['user']); + } else { + $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}-2', $shareData3['user']); + } $this->setupMounts(); $this->assertMount($shareData1['name']); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1'); - $client = $this->getMockBuilder('OCP\Http\Client\IClient') - ->disableOriginalConstructor()->getMock(); - $this->clientService->expects($this->at(0)) - ->method('newClient') - ->willReturn($client); - $response = $this->getMockBuilder('OCP\Http\Client\IResponse') - ->disableOriginalConstructor()->getMock(); - $client->expects($this->once()) - ->method('post') - ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything()) - ->willReturn($response); + if (!$isGroup) { + $client = $this->createMock(IClient::class); + $newClientCalls[] = $client; + $response = $this->createMock(IResponse::class); + $response->method('getBody') + ->willReturn(json_encode([ + 'ocs' => [ + 'meta' => [ + 'statuscode' => 200, + ] + ] + ])); + $client->expects($this->once()) + ->method('post') + ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything()) + ->willReturn($response); + } // Decline the third share - $this->manager->declineShare($openShares[1]['id']); + $this->assertTrue($this->manager->declineShare($openShares[1]['id'])); $this->setupMounts(); $this->assertMount($shareData1['name']); @@ -199,61 +324,382 @@ class ManagerTest extends TestCase { $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]); $this->assertCount(1, $acceptedShares); $shareData1['accepted'] = true; - $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']); + $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name'], $this->uid); // Check remaining shares - Open $openShares = $this->manager->getOpenShares(); - $this->assertCount(1, $openShares); - $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1'); + if ($isGroup) { + // declining a group share adds it back to pending instead of deleting it + $this->assertCount(2, $openShares); + // this is a group share that is still open + $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $shareData2['user']); + // this is the user share sub-entry matching the group share which got declined + $this->assertExternalShareEntry($shareData3, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}-2', $this->uid); + } else { + $this->assertCount(1, $openShares); + $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1', $this->uid); + } $this->setupMounts(); $this->assertMount($shareData1['name']); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1'); - $client1 = $this->getMockBuilder('OCP\Http\Client\IClient') - ->disableOriginalConstructor()->getMock(); - $client2 = $this->getMockBuilder('OCP\Http\Client\IClient') - ->disableOriginalConstructor()->getMock(); - $this->clientService->expects($this->at(0)) - ->method('newClient') - ->willReturn($client1); - $this->clientService->expects($this->at(1)) - ->method('newClient') - ->willReturn($client2); - $response = $this->getMockBuilder('OCP\Http\Client\IResponse') - ->disableOriginalConstructor()->getMock(); - $client1->expects($this->once()) - ->method('post') - ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything()) - ->willReturn($response); - $client2->expects($this->once()) - ->method('post') - ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything()) - ->willReturn($response); + if ($isGroup) { + // no http requests here + $this->manager->removeGroupShares('group1'); + } else { + $client1 = $this->createMock(IClient::class); + $client2 = $this->createMock(IClient::class); + $newClientCalls[] = $client1; + $newClientCalls[] = $client2; + $response = $this->createMock(IResponse::class); + $response->method('getBody') + ->willReturn(json_encode([ + 'ocs' => [ + 'meta' => [ + 'statuscode' => 200, + ] + ] + ])); + + $client1->expects($this->once()) + ->method('post') + ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything()) + ->willReturn($response); + $client2->expects($this->once()) + ->method('post') + ->with($this->stringStartsWith('http://localhost/ocs/v2.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything()) + ->willReturn($response); + + $this->manager->removeUserShares($this->uid); + } - $this->manager->removeUserShares($this->uid); $this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted'); - $this->mountManager->clear(); + $this->clearMounts(); self::invokePrivate($this->manager, 'setupMounts'); $this->assertNotMount($shareData1['name']); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1'); } + private function verifyAcceptedGroupShare($shareData) { + $openShares = $this->manager->getOpenShares(); + $this->assertCount(0, $openShares); + $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]); + $this->assertCount(1, $acceptedShares); + $shareData['accepted'] = true; + $this->assertExternalShareEntry($shareData, $acceptedShares[0], 0, $shareData['name'], $this->uid); + $this->setupMounts(); + $this->assertMount($shareData['name']); + } + + private function verifyDeclinedGroupShare($shareData, $tempMount = null) { + if ($tempMount === null) { + $tempMount = '{{TemporaryMountPointName#/SharedFolder}}'; + } + $openShares = $this->manager->getOpenShares(); + $this->assertCount(1, $openShares); + $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]); + $this->assertCount(0, $acceptedShares); + $this->assertExternalShareEntry($shareData, $openShares[0], 0, $tempMount, $this->uid); + $this->setupMounts(); + $this->assertNotMount($shareData['name']); + $this->assertNotMount($tempMount); + } + + private function createTestUserShare($userId = 'user1') { + $shareData = [ + 'remote' => 'http://localhost', + 'token' => 'token1', + 'password' => '', + 'name' => '/SharedFolder', + 'owner' => 'foobar', + 'shareType' => IShare::TYPE_USER, + 'accepted' => false, + 'user' => $userId, + 'remoteId' => '2342' + ]; + + $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData)); + + return $shareData; + } + private function createTestGroupShare($groupId = 'group1') { + $shareData = [ + 'remote' => 'http://localhost', + 'token' => 'token1', + 'password' => '', + 'name' => '/SharedFolder', + 'owner' => 'foobar', + 'shareType' => IShare::TYPE_GROUP, + 'accepted' => false, + 'user' => $groupId, + 'remoteId' => '2342' + ]; + + $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData)); + + $allShares = self::invokePrivate($this->manager, 'getShares', [null]); + foreach ($allShares as $share) { + if ($share['user'] === $groupId) { + // this will hold the main group entry + $groupShare = $share; + break; + } + } + + return [$shareData, $groupShare]; + } + + public function testAcceptOriginalGroupShare(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData); + + // a second time + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData); + } + + public function testAcceptGroupShareAgainThroughGroupShare(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData); + + // decline again, this keeps the sub-share + $this->assertTrue($this->manager->declineShare($groupShare['id'])); + $this->verifyDeclinedGroupShare($shareData, '/SharedFolder'); + + // this will return sub-entries + $openShares = $this->manager->getOpenShares(); + $this->assertCount(1, $openShares); + + // accept through group share + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData, '/SharedFolder'); + + // accept a second time + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData, '/SharedFolder'); + } + + public function testAcceptGroupShareAgainThroughSubShare(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData); + + // decline again, this keeps the sub-share + $this->assertTrue($this->manager->declineShare($groupShare['id'])); + $this->verifyDeclinedGroupShare($shareData, '/SharedFolder'); + + // this will return sub-entries + $openShares = $this->manager->getOpenShares(); + $this->assertCount(1, $openShares); + + // accept through sub-share + $this->assertTrue($this->manager->acceptShare($openShares[0]['id'])); + $this->verifyAcceptedGroupShare($shareData); + + // accept a second time + $this->assertTrue($this->manager->acceptShare($openShares[0]['id'])); + $this->verifyAcceptedGroupShare($shareData); + } + + public function testDeclineOriginalGroupShare(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + $this->assertTrue($this->manager->declineShare($groupShare['id'])); + $this->verifyDeclinedGroupShare($shareData); + + // a second time + $this->assertTrue($this->manager->declineShare($groupShare['id'])); + $this->verifyDeclinedGroupShare($shareData); + } + + public function testDeclineGroupShareAgainThroughGroupShare(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData); + + // decline again, this keeps the sub-share + $this->assertTrue($this->manager->declineShare($groupShare['id'])); + $this->verifyDeclinedGroupShare($shareData, '/SharedFolder'); + + // a second time + $this->assertTrue($this->manager->declineShare($groupShare['id'])); + $this->verifyDeclinedGroupShare($shareData, '/SharedFolder'); + } + + public function testDeclineGroupShareAgainThroughSubshare(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData); + + // this will return sub-entries + $allShares = self::invokePrivate($this->manager, 'getShares', [null]); + $this->assertCount(1, $allShares); + + // decline again through sub-share + $this->assertTrue($this->manager->declineShare($allShares[0]['id'])); + $this->verifyDeclinedGroupShare($shareData, '/SharedFolder'); + + // a second time + $this->assertTrue($this->manager->declineShare($allShares[0]['id'])); + $this->verifyDeclinedGroupShare($shareData, '/SharedFolder'); + } + + public function testDeclineGroupShareAgainThroughMountPoint(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData); + + // decline through mount point name + $this->assertTrue($this->manager->removeShare($this->uid . '/files/' . $shareData['name'])); + $this->verifyDeclinedGroupShare($shareData, '/SharedFolder'); + + // second time must fail as the mount point is gone + $this->assertFalse($this->manager->removeShare($this->uid . '/files/' . $shareData['name'])); + } + + public function testDeclineThenAcceptGroupShareAgainThroughGroupShare(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + // decline, this creates a declined sub-share + $this->assertTrue($this->manager->declineShare($groupShare['id'])); + $this->verifyDeclinedGroupShare($shareData); + + // this will return sub-entries + $openShares = $this->manager->getOpenShares(); + + // accept through sub-share + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData, '/SharedFolder'); + + // accept a second time + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + $this->verifyAcceptedGroupShare($shareData, '/SharedFolder'); + } + + public function testDeclineThenAcceptGroupShareAgainThroughSubShare(): void { + [$shareData, $groupShare] = $this->createTestGroupShare(); + // decline, this creates a declined sub-share + $this->assertTrue($this->manager->declineShare($groupShare['id'])); + $this->verifyDeclinedGroupShare($shareData); + + // this will return sub-entries + $openShares = $this->manager->getOpenShares(); + + // accept through sub-share + $this->assertTrue($this->manager->acceptShare($openShares[0]['id'])); + $this->verifyAcceptedGroupShare($shareData); + + // accept a second time + $this->assertTrue($this->manager->acceptShare($openShares[0]['id'])); + $this->verifyAcceptedGroupShare($shareData); + } + + public function testDeleteUserShares(): void { + // user 1 shares + + $shareData = $this->createTestUserShare($this->uid); + + [$shareData, $groupShare] = $this->createTestGroupShare(); + + $shares = $this->manager->getOpenShares(); + $this->assertCount(2, $shares); + + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + + // user 2 shares + $manager2 = $this->createManagerForUser('user2'); + $shareData2 = [ + 'remote' => 'http://localhost', + 'token' => 'token1', + 'password' => '', + 'name' => '/SharedFolder', + 'owner' => 'foobar', + 'shareType' => IShare::TYPE_USER, + 'accepted' => false, + 'user' => 'user2', + 'remoteId' => '2342' + ]; + + $this->assertCount(1, $manager2->getOpenShares()); + $this->assertSame(null, call_user_func_array([$manager2, 'addShare'], $shareData2)); + $this->assertCount(2, $manager2->getOpenShares()); + + $this->manager->expects($this->once())->method('tryOCMEndPoint')->with('http://localhost', 'token1', '2342', 'decline')->willReturn([]); + $this->manager->removeUserShares($this->uid); + + $user1Shares = $this->manager->getOpenShares(); + // user share is gone, group is still there + $this->assertCount(1, $user1Shares); + $this->assertEquals($user1Shares[0]['share_type'], IShare::TYPE_GROUP); + + // user 2 shares untouched + $user2Shares = $manager2->getOpenShares(); + $this->assertCount(2, $user2Shares); + $this->assertEquals($user2Shares[0]['share_type'], IShare::TYPE_GROUP); + $this->assertEquals($user2Shares[0]['user'], 'group1'); + $this->assertEquals($user2Shares[1]['share_type'], IShare::TYPE_USER); + $this->assertEquals($user2Shares[1]['user'], 'user2'); + } + + public function testDeleteGroupShares(): void { + $shareData = $this->createTestUserShare($this->uid); + + [$shareData, $groupShare] = $this->createTestGroupShare(); + + $shares = $this->manager->getOpenShares(); + $this->assertCount(2, $shares); + + $this->assertTrue($this->manager->acceptShare($groupShare['id'])); + + // user 2 shares + $manager2 = $this->createManagerForUser('user2'); + $shareData2 = [ + 'remote' => 'http://localhost', + 'token' => 'token1', + 'password' => '', + 'name' => '/SharedFolder', + 'owner' => 'foobar', + 'shareType' => IShare::TYPE_USER, + 'accepted' => false, + 'user' => 'user2', + 'remoteId' => '2342' + ]; + + $this->assertCount(1, $manager2->getOpenShares()); + $this->assertSame(null, call_user_func_array([$manager2, 'addShare'], $shareData2)); + $this->assertCount(2, $manager2->getOpenShares()); + + $this->manager->expects($this->never())->method('tryOCMEndPoint'); + $this->manager->removeGroupShares('group1'); + + $user1Shares = $this->manager->getOpenShares(); + // user share is gone, group is still there + $this->assertCount(1, $user1Shares); + $this->assertEquals($user1Shares[0]['share_type'], IShare::TYPE_USER); + + // user 2 shares untouched + $user2Shares = $manager2->getOpenShares(); + $this->assertCount(1, $user2Shares); + $this->assertEquals($user2Shares[0]['share_type'], IShare::TYPE_USER); + $this->assertEquals($user2Shares[0]['user'], 'user2'); + } + /** * @param array $expected * @param array $actual * @param int $share * @param string $mountPoint */ - protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint) { + protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint, $targetEntity) { $this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share); $this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share); $this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share); $this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share); - $this->assertEquals($expected['accepted'], (int) $actual['accepted'], 'Asserting accept of a share #' . $share); - $this->assertEquals($expected['user'], $actual['user'], 'Asserting user of a share #' . $share); + $this->assertEquals($expected['accepted'], (int)$actual['accepted'], 'Asserting accept of a share #' . $share); + $this->assertEquals($targetEntity, $actual['user'], 'Asserting user of a share #' . $share); $this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share); } @@ -269,12 +715,12 @@ class ManagerTest extends TestCase { private function assertNotMount($mountPoint) { $mountPoint = rtrim($mountPoint, '/'); - $mount = $this->mountManager->find($this->getFullPath($mountPoint)); - if ($mount) { + try { + $mount = $this->mountManager->find($this->getFullPath($mountPoint)); $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount); $this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/')); - } else { - $this->assertNull($mount); + } catch (NotFoundException $e) { + } } diff --git a/apps/files_sharing/tests/External/ScannerTest.php b/apps/files_sharing/tests/External/ScannerTest.php index 1ad19d5c80f..8b44d47f2b1 100644 --- a/apps/files_sharing/tests/External/ScannerTest.php +++ b/apps/files_sharing/tests/External/ScannerTest.php @@ -1,39 +1,28 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * - * @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: 2018-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests\External; +use OC\Files\Cache\Cache; use OCA\Files_Sharing\External\Scanner; +use OCA\Files_Sharing\External\Storage; use Test\TestCase; +/** + * @group DB + */ class ScannerTest extends TestCase { - /** @var \OCA\Files_Sharing\External\Scanner */ - protected $scanner; - /** @var \OCA\Files_Sharing\External\Storage|\PHPUnit_Framework_MockObject_MockObject */ + protected Scanner $scanner; + /** @var Storage|\PHPUnit\Framework\MockObject\MockObject */ protected $storage; - /** @var \OC\Files\Cache\Cache|\PHPUnit_Framework_MockObject_MockObject */ + /** @var Cache|\PHPUnit\Framework\MockObject\MockObject */ protected $cache; - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage') @@ -49,19 +38,7 @@ class ScannerTest extends TestCase { $this->scanner = new Scanner($this->storage); } - public function testScanAll() { - $this->storage->expects($this->any()) - ->method('getShareInfo') - ->willReturn(['status' => 'success', 'data' => []]); - - // FIXME add real tests, we are currently only checking for - // Declaration of OCA\Files_Sharing\External\Scanner::*() should be - // compatible with OC\Files\Cache\Scanner::*() - $this->scanner->scanAll(); - $this->assertTrue(true); - } - - public function testScan() { + public function testScan(): void { $this->storage->expects($this->any()) ->method('getShareInfo') ->willReturn(['status' => 'success', 'data' => []]); @@ -70,14 +47,14 @@ class ScannerTest extends TestCase { // Declaration of OCA\Files_Sharing\External\Scanner::*() should be // compatible with OC\Files\Cache\Scanner::*() $this->scanner->scan('test', Scanner::SCAN_RECURSIVE); - $this->assertTrue(true); + $this->addToAssertionCount(1); } - public function testScanFile() { + public function testScanFile(): void { // FIXME add real tests, we are currently only checking for // Declaration of OCA\Files_Sharing\External\Scanner::*() should be // compatible with OC\Files\Cache\Scanner::*() $this->scanner->scanFile('test', Scanner::SCAN_RECURSIVE); - $this->assertTrue(true); + $this->addToAssertionCount(1); } } diff --git a/apps/files_sharing/tests/ExternalStorageTest.php b/apps/files_sharing/tests/ExternalStorageTest.php index f7345d25e08..1d9d2eed7bd 100644 --- a/apps/files_sharing/tests/ExternalStorageTest.php +++ b/apps/files_sharing/tests/ExternalStorageTest.php @@ -1,32 +1,15 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; + use OC\Federation\CloudId; +use OCA\Files_Sharing\External\Manager as ExternalShareManager; +use OCA\Files_Sharing\External\Storage; use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; use OCP\Http\Client\IResponse; @@ -37,44 +20,44 @@ use OCP\Http\Client\IResponse; * @group DB */ class ExternalStorageTest extends \Test\TestCase { - - function optionsProvider() { - return array( - array( + public static function optionsProvider() { + return [ + [ 'http://remoteserver:8080/owncloud', 'http://remoteserver:8080/owncloud/public.php/webdav/', - ), + ], // extra slash - array( + [ 'http://remoteserver:8080/owncloud/', 'http://remoteserver:8080/owncloud/public.php/webdav/', - ), + ], // extra path - array( + [ 'http://remoteserver:8080/myservices/owncloud/', 'http://remoteserver:8080/myservices/owncloud/public.php/webdav/', - ), + ], // root path - array( + [ 'http://remoteserver:8080/', 'http://remoteserver:8080/public.php/webdav/', - ), + ], // without port - array( + [ 'http://remoteserver/oc.test', 'http://remoteserver/oc.test/public.php/webdav/', - ), + ], // https - array( + [ 'https://remoteserver/', 'https://remoteserver/public.php/webdav/', - ), - ); + ], + ]; } private function getTestStorage($uri) { $certificateManager = \OC::$server->getCertificateManager(); $httpClientService = $this->createMock(IClientService::class); + $manager = $this->createMock(ExternalShareManager::class); $client = $this->createMock(IClient::class); $response = $this->createMock(IResponse::class); $client @@ -91,30 +74,29 @@ class ExternalStorageTest extends \Test\TestCase { ->willReturn($client); return new TestSharingExternalStorage( - array( + [ 'cloudId' => new CloudId('testOwner@' . $uri, 'testOwner', $uri), 'remote' => $uri, 'owner' => 'testOwner', 'mountpoint' => 'remoteshare', 'token' => 'abcdef', 'password' => '', - 'manager' => null, + 'manager' => $manager, 'certificateManager' => $certificateManager, 'HttpClientService' => $httpClientService, - ) + ] ); } - /** - * @dataProvider optionsProvider - */ - public function testStorageMountOptions($inputUri, $baseUri) { + #[\PHPUnit\Framework\Attributes\DataProvider('optionsProvider')] + public function testStorageMountOptions($inputUri, $baseUri): void { $storage = $this->getTestStorage($inputUri); $this->assertEquals($baseUri, $storage->getBaseUri()); } - public function testIfTestReturnsTheValue() { - $result = $this->getTestStorage('https://remoteserver')->test(); + public function testIfTestReturnsTheValue(): void { + $storage = $this->getTestStorage('https://remoteserver'); + $result = $storage->test(); $this->assertSame(true, $result); } } @@ -122,15 +104,14 @@ class ExternalStorageTest extends \Test\TestCase { /** * Dummy subclass to make it possible to access private members */ -class TestSharingExternalStorage extends \OCA\Files_Sharing\External\Storage { - +class TestSharingExternalStorage extends Storage { public function getBaseUri() { return $this->createBaseUri(); } - public function stat($path) { + public function stat(string $path): array|false { if ($path === '') { - return true; + return ['key' => 'value']; } return parent::stat($path); } diff --git a/apps/files_sharing/tests/GroupEtagPropagationTest.php b/apps/files_sharing/tests/GroupEtagPropagationTest.php index 206980659c7..da9c7c6bd07 100644 --- a/apps/files_sharing/tests/GroupEtagPropagationTest.php +++ b/apps/files_sharing/tests/GroupEtagPropagationTest.php @@ -1,33 +1,16 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; use OC\Files\Filesystem; use OC\Files\View; +use OCP\Constants; +use OCP\Share\IShare; /** * @group SLOWDB @@ -52,13 +35,14 @@ class GroupEtagPropagationTest extends PropagationTestCase { $view1 = new View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); $view1->mkdir('/test/sub'); - $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + $share = $this->share( + IShare::TYPE_GROUP, '/test', self::TEST_FILES_SHARING_API_USER1, 'group1', - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); $this->fileIds[self::TEST_FILES_SHARING_API_USER1][''] = $view1->getFileInfo('')->getId(); $this->fileIds[self::TEST_FILES_SHARING_API_USER1]['test'] = $view1->getFileInfo('test')->getId(); $this->fileIds[self::TEST_FILES_SHARING_API_USER1]['test/sub'] = $view1->getFileInfo('test/sub')->getId(); @@ -66,20 +50,22 @@ class GroupEtagPropagationTest extends PropagationTestCase { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); $view2 = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); - $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + $share = $this->share( + IShare::TYPE_GROUP, '/test', self::TEST_FILES_SHARING_API_USER2, 'group2', - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); - $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER3); + $share = $this->share( + IShare::TYPE_GROUP, '/test/sub', self::TEST_FILES_SHARING_API_USER2, 'group3', - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER4); $this->fileIds[self::TEST_FILES_SHARING_API_USER2][''] = $view2->getFileInfo('')->getId(); $this->fileIds[self::TEST_FILES_SHARING_API_USER2]['test'] = $view2->getFileInfo('test')->getId(); @@ -105,7 +91,7 @@ class GroupEtagPropagationTest extends PropagationTestCase { } } - public function testGroupReShareRecipientWrites() { + public function testGroupReShareRecipientWrites(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER3); Filesystem::file_put_contents('/test/sub/file.txt', 'asd'); @@ -115,7 +101,7 @@ class GroupEtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testGroupReShareSubFolderRecipientWrites() { + public function testGroupReShareSubFolderRecipientWrites(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER4); Filesystem::file_put_contents('/sub/file.txt', 'asd'); @@ -125,7 +111,7 @@ class GroupEtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientUnsharesFromSelf() { + public function testRecipientUnsharesFromSelf(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); $this->assertTrue( $this->rootView->unlink('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test') @@ -135,7 +121,7 @@ class GroupEtagPropagationTest extends PropagationTestCase { $this->assertAllUnchanged(); } - public function testRecipientUnsharesFromSelfUniqueGroupShare() { + public function testRecipientUnsharesFromSelfUniqueGroupShare(): void { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); // rename to create an extra entry in the share table $this->rootView->rename('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test', '/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test_renamed'); diff --git a/apps/files_sharing/tests/HelperTest.php b/apps/files_sharing/tests/HelperTest.php index 4585b7e7ab8..4d0d747b3e4 100644 --- a/apps/files_sharing/tests/HelperTest.php +++ b/apps/files_sharing/tests/HelperTest.php @@ -1,31 +1,17 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; +use OC\Files\Filesystem; +use OCA\Files_Sharing\Helper; +use OCP\IConfig; +use OCP\Server; + /** * Class HelperTest * @@ -36,18 +22,16 @@ class HelperTest extends TestCase { /** * test set and get share folder */ - public function testSetGetShareFolder() { - $this->assertSame('/', \OCA\Files_Sharing\Helper::getShareFolder()); + public function testSetGetShareFolder(): void { + $this->assertSame('/', Helper::getShareFolder()); - \OCA\Files_Sharing\Helper::setShareFolder('/Shared/Folder'); + Helper::setShareFolder('/Shared/Folder'); - $sharedFolder = \OCA\Files_Sharing\Helper::getShareFolder(); - $this->assertSame('/Shared/Folder', \OCA\Files_Sharing\Helper::getShareFolder()); - $this->assertTrue(\OC\Files\Filesystem::is_dir($sharedFolder)); + $sharedFolder = Helper::getShareFolder(); + $this->assertSame('/Shared/Folder', Helper::getShareFolder()); + $this->assertTrue(Filesystem::is_dir($sharedFolder)); // cleanup - \OC::$server->getConfig()->deleteSystemValue('share_folder'); - + Server::get(IConfig::class)->deleteSystemValue('share_folder'); } - } diff --git a/apps/files_sharing/tests/Listener/LoadAdditionalListenerTest.php b/apps/files_sharing/tests/Listener/LoadAdditionalListenerTest.php new file mode 100644 index 00000000000..75bee35d58a --- /dev/null +++ b/apps/files_sharing/tests/Listener/LoadAdditionalListenerTest.php @@ -0,0 +1,120 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\Files_Sharing\Tests\Listener; + +use OC\InitialStateService; +use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCA\Files_Sharing\Listener\LoadAdditionalListener; +use OCP\EventDispatcher\Event; +use OCP\IConfig; +use OCP\L10N\IFactory; +use OCP\Share\IManager; +use OCP\Util; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use Test\TestCase; + +class LoadAdditionalListenerTest extends TestCase { + protected LoggerInterface&MockObject $logger; + protected LoadAdditionalScriptsEvent&MockObject $event; + protected IManager&MockObject $shareManager; + protected IFactory&MockObject $factory; + protected InitialStateService&MockObject $initialStateService; + protected IConfig&MockObject $config; + + protected function setUp(): void { + parent::setUp(); + + $this->logger = $this->createMock(LoggerInterface::class); + $this->event = $this->createMock(LoadAdditionalScriptsEvent::class); + $this->shareManager = $this->createMock(IManager::class); + $this->factory = $this->createMock(IFactory::class); + $this->initialStateService = $this->createMock(InitialStateService::class); + $this->config = $this->createMock(IConfig::class); + + /* Empty static array to avoid inter-test conflicts */ + \OC_Util::$styles = []; + self::invokePrivate(Util::class, 'scripts', [[]]); + self::invokePrivate(Util::class, 'scriptDeps', [[]]); + self::invokePrivate(Util::class, 'scriptsInit', [[]]); + } + + protected function tearDown(): void { + parent::tearDown(); + + \OC_Util::$styles = []; + self::invokePrivate(Util::class, 'scripts', [[]]); + self::invokePrivate(Util::class, 'scriptDeps', [[]]); + self::invokePrivate(Util::class, 'scriptsInit', [[]]); + } + + public function testHandleIgnoresNonMatchingEvent(): void { + $listener = new LoadAdditionalListener(); + $event = $this->createMock(Event::class); + + // Should not throw or call anything + $listener->handle($event); + + $this->assertTrue(true); // No exception means pass + } + + public function testHandleWithLoadAdditionalScriptsEvent(): void { + $listener = new LoadAdditionalListener(); + + $this->shareManager->method('shareApiEnabled')->willReturn(false); + $this->factory->method('findLanguage')->willReturn('language_mock'); + $this->config->method('getSystemValueBool')->willReturn(true); + + $this->overwriteService(IManager::class, $this->shareManager); + $this->overwriteService(IFactory::class, $this->factory); + $this->overwriteService(InitialStateService::class, $this->initialStateService); + $this->overwriteService(IConfig::class, $this->config); + + $scriptsBefore = Util::getScripts(); + $this->assertNotContains('files_sharing/l10n/language_mock', $scriptsBefore); + $this->assertNotContains('files_sharing/js/additionalScripts', $scriptsBefore); + $this->assertNotContains('files_sharing/js/init', $scriptsBefore); + $this->assertNotContains('files_sharing/css/icons', \OC_Util::$styles); + + // Util static methods can't be easily mocked, so just ensure no exceptions + $listener->handle($this->event); + + // assert array $scripts contains the expected scripts + $scriptsAfter = Util::getScripts(); + $this->assertContains('files_sharing/l10n/language_mock', $scriptsAfter); + $this->assertContains('files_sharing/js/additionalScripts', $scriptsAfter); + $this->assertNotContains('files_sharing/js/init', $scriptsAfter); + + $this->assertContains('files_sharing/css/icons', \OC_Util::$styles); + } + + public function testHandleWithLoadAdditionalScriptsEventWithShareApiEnabled(): void { + $listener = new LoadAdditionalListener(); + + $this->shareManager->method('shareApiEnabled')->willReturn(true); + $this->config->method('getSystemValueBool')->willReturn(true); + + $this->overwriteService(IManager::class, $this->shareManager); + $this->overwriteService(InitialStateService::class, $this->initialStateService); + $this->overwriteService(IConfig::class, $this->config); + $this->overwriteService(IFactory::class, $this->factory); + + $scriptsBefore = Util::getScripts(); + $this->assertNotContains('files_sharing/js/init', $scriptsBefore); + + // Util static methods can't be easily mocked, so just ensure no exceptions + $listener->handle($this->event); + + $scriptsAfter = Util::getScripts(); + + // assert array $scripts contains the expected scripts + $this->assertContains('files_sharing/js/init', $scriptsAfter); + } +} diff --git a/apps/files_sharing/tests/LockingTest.php b/apps/files_sharing/tests/LockingTest.php index 4af5e3e2cee..280c364a136 100644 --- a/apps/files_sharing/tests/LockingTest.php +++ b/apps/files_sharing/tests/LockingTest.php @@ -1,34 +1,20 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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: 2018-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; use OC\Files\Filesystem; use OC\Files\View; +use OCP\Constants; +use OCP\IUserManager; use OCP\Lock\ILockingProvider; +use OCP\Lock\LockedException; +use OCP\Server; +use OCP\Share\IShare; /** * Class LockingTest @@ -46,11 +32,11 @@ class LockingTest extends TestCase { private $ownerUid; private $recipientUid; - public function setUp() { + protected function setUp(): void { parent::setUp(); $this->userBackend = new \Test\Util\User\Dummy(); - \OC::$server->getUserManager()->registerBackend($this->userBackend); + Server::get(IUserManager::class)->registerBackend($this->userBackend); $this->ownerUid = $this->getUniqueID('owner_'); $this->recipientUid = $this->getUniqueID('recipient_'); @@ -63,26 +49,26 @@ class LockingTest extends TestCase { $fileId = Filesystem::getFileInfo('/foo/bar.txt')->getId(); $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, '/foo/bar.txt', $this->ownerUid, $this->recipientUid, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); $this->loginAsUser($this->recipientUid); $this->assertTrue(Filesystem::file_exists('bar.txt')); } - public function tearDown() { - \OC::$server->getUserManager()->removeBackend($this->userBackend); + protected function tearDown(): void { + Server::get(IUserManager::class)->removeBackend($this->userBackend); parent::tearDown(); } - /** - * @expectedException \OCP\Lock\LockedException - */ - public function testLockAsRecipient() { + + public function testLockAsRecipient(): void { + $this->expectException(LockedException::class); + $this->loginAsUser($this->ownerUid); Filesystem::initMountPoints($this->recipientUid); @@ -92,7 +78,7 @@ class LockingTest extends TestCase { Filesystem::rename('/foo', '/asd'); } - public function testUnLockAsRecipient() { + public function testUnLockAsRecipient(): void { $this->loginAsUser($this->ownerUid); Filesystem::initMountPoints($this->recipientUid); @@ -103,14 +89,13 @@ class LockingTest extends TestCase { $this->assertTrue(Filesystem::rename('/foo', '/asd')); } - public function testChangeLock() { - + public function testChangeLock(): void { Filesystem::initMountPoints($this->recipientUid); $recipientView = new View('/' . $this->recipientUid . '/files'); $recipientView->lockFile('bar.txt', ILockingProvider::LOCK_SHARED); $recipientView->changeLock('bar.txt', ILockingProvider::LOCK_EXCLUSIVE); $recipientView->unlockFile('bar.txt', ILockingProvider::LOCK_EXCLUSIVE); - $this->assertTrue(true); + $this->addToAssertionCount(1); } } diff --git a/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php b/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php index c260a65725d..efc6b3f7f7f 100644 --- a/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php +++ b/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php @@ -1,25 +1,8 @@ <?php + /** - * - * - * @author Morris Jobke <hey@morrisjobke.de> - * @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: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Sharing\Tests\Middleware; @@ -36,20 +19,20 @@ use OCP\Share\IManager; */ class OCSShareAPIMiddlewareTest extends \Test\TestCase { - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ private $shareManager; /** @var IL10N */ private $l; /** @var OCSShareAPIMiddleware */ private $middleware; - public function setUp() { + protected function setUp(): void { parent::setUp(); $this->shareManager = $this->createMock(IManager::class); $this->l = $this->createMock(IL10N::class); - $this->l->method('t')->will($this->returnArgument(0)); + $this->l->method('t')->willReturnArgument(0); $this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l); } @@ -90,13 +73,13 @@ class OCSShareAPIMiddlewareTest extends \Test\TestCase { } /** - * @dataProvider dataBeforeController * * @param Controller $controller * @param bool $enabled * @param bool $exception */ - public function testBeforeController(Controller $controller, $enabled, $exception) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataBeforeController')] + public function testBeforeController(Controller $controller, $enabled, $exception): void { $this->shareManager->method('shareApiEnabled')->willReturn($enabled); try { @@ -122,12 +105,12 @@ class OCSShareAPIMiddlewareTest extends \Test\TestCase { } /** - * @dataProvider dataAfterController * * @param Controller $controller * @param bool $called */ - public function testAfterController(Controller $controller) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataAfterController')] + public function testAfterController(Controller $controller): void { if ($controller instanceof ShareAPIController) { $controller->expects($this->once())->method('cleanup'); } @@ -136,5 +119,6 @@ class OCSShareAPIMiddlewareTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); $this->middleware->afterController($controller, 'foo', $response); + $this->addToAssertionCount(1); } } diff --git a/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php b/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php index cbac5c7bef5..631b6a0f51c 100644 --- a/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php +++ b/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php @@ -1,25 +1,8 @@ <?php + /** - * - * - * @author Morris Jobke <hey@morrisjobke.de> - * @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: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Sharing\Tests\Middleware; @@ -29,32 +12,33 @@ use OCA\Files_Sharing\Middleware\ShareInfoMiddleware; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\Response; use OCP\Share\IManager as ShareManager; use Test\TestCase; class ShareInfoMiddlewareTest extends TestCase { - /** @var ShareManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var ShareManager|\PHPUnit\Framework\MockObject\MockObject */ private $shareManager; /** @var ShareInfoMiddleware */ private $middleware; - public function setUp() { + protected function setUp(): void { parent::setUp(); $this->shareManager = $this->createMock(ShareManager::class); $this->middleware = new ShareInfoMiddleware($this->shareManager); } - public function testBeforeControllerNoShareInfo() { + public function testBeforeControllerNoShareInfo(): void { $this->shareManager->expects($this->never()) ->method($this->anything()); $this->middleware->beforeController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo'); } - public function testBeforeControllerShareInfoNoS2s() { + public function testBeforeControllerShareInfoNoS2s(): void { $this->shareManager->expects($this->once()) ->method('outgoingServer2ServerSharesAllowed') ->willReturn(false); @@ -63,7 +47,7 @@ class ShareInfoMiddlewareTest extends TestCase { $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo'); } - public function testBeforeControllerShareInfo() { + public function testBeforeControllerShareInfo(): void { $this->shareManager->expects($this->once()) ->method('outgoingServer2ServerSharesAllowed') ->willReturn(true); @@ -71,7 +55,7 @@ class ShareInfoMiddlewareTest extends TestCase { $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo'); } - public function testAfterExceptionNoShareInfo() { + public function testAfterExceptionNoShareInfo(): void { $exeption = new \Exception(); try { @@ -83,7 +67,7 @@ class ShareInfoMiddlewareTest extends TestCase { } - public function testAfterExceptionNoS2S() { + public function testAfterExceptionNoS2S(): void { $exeption = new \Exception(); try { @@ -94,7 +78,7 @@ class ShareInfoMiddlewareTest extends TestCase { } } - public function testAfterExceptionS2S() { + public function testAfterExceptionS2S(): void { $expected = new JSONResponse([], Http::STATUS_NOT_FOUND); $this->assertEquals( @@ -103,8 +87,8 @@ class ShareInfoMiddlewareTest extends TestCase { ); } - public function testAfterControllerNoShareInfo() { - $response = $this->createMock(Http\Response::class); + public function testAfterControllerNoShareInfo(): void { + $response = $this->createMock(Response::class); $this->assertEquals( $response, @@ -112,8 +96,8 @@ class ShareInfoMiddlewareTest extends TestCase { ); } - public function testAfterControllerNoJSON() { - $response = $this->createMock(Http\Response::class); + public function testAfterControllerNoJSON(): void { + $response = $this->createMock(Response::class); $this->assertEquals( $response, @@ -121,7 +105,7 @@ class ShareInfoMiddlewareTest extends TestCase { ); } - public function testAfterControllerJSONok() { + public function testAfterControllerJSONok(): void { $data = ['foo' => 'bar']; $response = new JSONResponse($data); @@ -136,7 +120,7 @@ class ShareInfoMiddlewareTest extends TestCase { ); } - public function testAfterControllerJSONerror() { + public function testAfterControllerJSONerror(): void { $data = ['foo' => 'bar']; $response = new JSONResponse($data, Http::STATUS_FORBIDDEN); @@ -153,5 +137,4 @@ class ShareInfoMiddlewareTest extends TestCase { } class ShareInfoMiddlewareTestController extends Controller { - } diff --git a/apps/files_sharing/tests/Middleware/SharingCheckMiddlewareTest.php b/apps/files_sharing/tests/Middleware/SharingCheckMiddlewareTest.php index d8676547a76..3d86007a54c 100644 --- a/apps/files_sharing/tests/Middleware/SharingCheckMiddlewareTest.php +++ b/apps/files_sharing/tests/Middleware/SharingCheckMiddlewareTest.php @@ -1,39 +1,21 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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 OCA\Files_Sharing\Middleware; + use OCA\Files_Sharing\Controller\ExternalSharesController; use OCA\Files_Sharing\Controller\ShareController; +use OCA\Files_Sharing\Exceptions\S2SException; use OCP\App\IAppManager; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\NotFoundResponse; -use OCP\Files\NotFoundException; use OCP\AppFramework\Utility\IControllerMethodReflector; -use OCA\Files_Sharing\Exceptions\S2SException; -use OCP\AppFramework\Http\JSONResponse; +use OCP\Files\NotFoundException; use OCP\IConfig; use OCP\IRequest; use OCP\Share\IManager; @@ -44,22 +26,22 @@ use OCP\Share\IShare; */ class SharingCheckMiddlewareTest extends \Test\TestCase { - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ private $config; - /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */ private $appManager; /** @var SharingCheckMiddleware */ private $sharingCheckMiddleware; - /** @var Controller|\PHPUnit_Framework_MockObject_MockObject */ + /** @var Controller|\PHPUnit\Framework\MockObject\MockObject */ private $controllerMock; - /** @var IControllerMethodReflector|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */ private $reflector; - /** @var IManager | \PHPUnit_Framework_MockObject_MockObject */ + /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */ private $shareManager; - /** @var IRequest | \PHPUnit_Framework_MockObject_MockObject */ + /** @var IRequest | \PHPUnit\Framework\MockObject\MockObject */ private $request; - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->config = $this->createMock(IConfig::class); @@ -78,78 +60,33 @@ class SharingCheckMiddlewareTest extends \Test\TestCase { $this->request); } - public function testIsSharingEnabledWithAppEnabled() { + public function testIsSharingEnabledWithAppEnabled(): void { $this->appManager ->expects($this->once()) ->method('isEnabledForUser') ->with('files_sharing') - ->will($this->returnValue(true)); + ->willReturn(true); $this->assertTrue(self::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled')); } - public function testIsSharingEnabledWithAppDisabled() { + public function testIsSharingEnabledWithAppDisabled(): void { $this->appManager ->expects($this->once()) ->method('isEnabledForUser') ->with('files_sharing') - ->will($this->returnValue(false)); + ->willReturn(false); $this->assertFalse(self::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled')); } - public function testIsLinkSharingEnabledWithEverythinEnabled() { - $this->config - ->expects($this->at(0)) - ->method('getAppValue') - ->with('core', 'shareapi_enabled', 'yes') - ->will($this->returnValue('yes')); - - $this->config - ->expects($this->at(1)) - ->method('getAppValue') - ->with('core', 'shareapi_allow_links', 'yes') - ->will($this->returnValue('yes')); - - $this->assertTrue(self::invokePrivate($this->sharingCheckMiddleware, 'isLinkSharingEnabled')); - } - - - public function testIsLinkSharingEnabledWithLinkSharingDisabled() { - $this->config - ->expects($this->at(0)) - ->method('getAppValue') - ->with('core', 'shareapi_enabled', 'yes') - ->will($this->returnValue('yes')); - - $this->config - ->expects($this->at(1)) - ->method('getAppValue') - ->with('core', 'shareapi_allow_links', 'yes') - ->will($this->returnValue('no')); - - $this->assertFalse(self::invokePrivate($this->sharingCheckMiddleware, 'isLinkSharingEnabled')); - } - - public function testIsLinkSharingEnabledWithSharingAPIDisabled() { - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('core', 'shareapi_enabled', 'yes') - ->will($this->returnValue('no')); - - $this->assertFalse(self::invokePrivate($this->sharingCheckMiddleware, 'isLinkSharingEnabled')); - } - - public function externalSharesChecksDataProvider() { - + public static function externalSharesChecksDataProvider() { $data = []; foreach ([false, true] as $annIn) { foreach ([false, true] as $annOut) { foreach ([false, true] as $confIn) { foreach ([false, true] as $confOut) { - $res = true; if (!$annIn && !$confIn) { $res = false; @@ -178,40 +115,36 @@ class SharingCheckMiddlewareTest extends \Test\TestCase { return $data; } - /** - * @dataProvider externalSharesChecksDataProvider - */ - public function testExternalSharesChecks($annotations, $config, $expectedResult) { + #[\PHPUnit\Framework\Attributes\DataProvider('externalSharesChecksDataProvider')] + public function testExternalSharesChecks($annotations, $config, $expectedResult): void { $this->reflector ->expects($this->atLeastOnce()) ->method('hasAnnotation') - ->will($this->returnValueMap($annotations)); + ->willReturnMap($annotations); $this->config ->method('getAppValue') - ->will($this->returnValueMap($config)); + ->willReturnMap($config); $this->assertEquals($expectedResult, self::invokePrivate($this->sharingCheckMiddleware, 'externalSharesChecks')); } - /** - * @dataProvider externalSharesChecksDataProvider - */ - public function testBeforeControllerWithExternalShareControllerWithSharingEnabled($annotations, $config, $noException) { + #[\PHPUnit\Framework\Attributes\DataProvider('externalSharesChecksDataProvider')] + public function testBeforeControllerWithExternalShareControllerWithSharingEnabled($annotations, $config, $noException): void { $this->appManager ->expects($this->once()) ->method('isEnabledForUser') ->with('files_sharing') - ->will($this->returnValue(true)); + ->willReturn(true); $this->reflector ->expects($this->atLeastOnce()) ->method('hasAnnotation') - ->will($this->returnValueMap($annotations)); + ->willReturnMap($annotations); $this->config ->method('getAppValue') - ->will($this->returnValueMap($config)); + ->willReturnMap($config); $controller = $this->createMock(ExternalSharesController::class); @@ -226,96 +159,47 @@ class SharingCheckMiddlewareTest extends \Test\TestCase { $this->assertNotEquals($noException, $exceptionThrown); } - public function testBeforeControllerWithShareControllerWithSharingEnabled() { - + public function testBeforeControllerWithShareControllerWithSharingEnabled(): void { $share = $this->createMock(IShare::class); $this->appManager ->expects($this->once()) ->method('isEnabledForUser') ->with('files_sharing') - ->will($this->returnValue(true)); - - $this->config - ->expects($this->at(0)) - ->method('getAppValue') - ->with('core', 'shareapi_enabled', 'yes') - ->will($this->returnValue('yes')); - - $this->config - ->expects($this->at(1)) - ->method('getAppValue') - ->with('core', 'shareapi_allow_links', 'yes') - ->will($this->returnValue('yes')); - - $this->request->expects($this->once())->method('getParam')->with('token') - ->willReturn('token'); - $this->shareManager->expects($this->once())->method('getShareByToken') - ->with('token')->willReturn($share); - - $share->expects($this->once())->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK); + ->willReturn(true); $controller = $this->createMock(ShareController::class); $this->sharingCheckMiddleware->beforeController($controller, 'myMethod'); } - /** - * @expectedException \OCP\Files\NotFoundException - * @expectedExceptionMessage Link sharing is disabled - */ - public function testBeforeControllerWithShareControllerWithSharingEnabledAPIDisabled() { - $share = $this->createMock(IShare::class); + public function testBeforeControllerWithSharingDisabled(): void { + $this->expectException(NotFoundException::class); + $this->expectExceptionMessage('Sharing is disabled.'); $this->appManager ->expects($this->once()) ->method('isEnabledForUser') ->with('files_sharing') - ->will($this->returnValue(true)); + ->willReturn(false); - $controller = $this->createMock(ShareController::class); - - $this->request->expects($this->once())->method('getParam')->with('token') - ->willReturn('token'); - $this->shareManager->expects($this->once())->method('getShareByToken') - ->with('token')->willReturn($share); - - $share->expects($this->once())->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK); - - - $this->sharingCheckMiddleware->beforeController($controller, 'myMethod'); + $this->sharingCheckMiddleware->beforeController($this->controllerMock, 'myMethod'); } - /** - * @expectedException \OCP\Files\NotFoundException - * @expectedExceptionMessage Sharing is disabled. - */ - public function testBeforeControllerWithSharingDisabled() { - $this->appManager - ->expects($this->once()) - ->method('isEnabledForUser') - ->with('files_sharing') - ->will($this->returnValue(false)); - $this->sharingCheckMiddleware->beforeController($this->controllerMock, 'myMethod'); - } + public function testAfterExceptionWithRegularException(): void { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('My Exception message'); - /** - * @expectedException \Exception - * @expectedExceptionMessage My Exception message - */ - public function testAfterExceptionWithRegularException() { $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new \Exception('My Exception message')); } - public function testAfterExceptionWithNotFoundException() { + public function testAfterExceptionWithNotFoundException(): void { $this->assertEquals(new NotFoundResponse(), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new NotFoundException('My Exception message'))); } - public function testAfterExceptionWithS2SException() { + public function testAfterExceptionWithS2SException(): void { $this->assertEquals(new JSONResponse('My Exception message', 405), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new S2SException('My Exception message'))); } - - } diff --git a/apps/files_sharing/tests/Migration/SetPasswordColumnTest.php b/apps/files_sharing/tests/Migration/SetPasswordColumnTest.php index c4c78b04a23..3cbbad0f8bc 100644 --- a/apps/files_sharing/tests/Migration/SetPasswordColumnTest.php +++ b/apps/files_sharing/tests/Migration/SetPasswordColumnTest.php @@ -1,33 +1,19 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2017 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests\Migration; - use OCA\Files_Sharing\Migration\SetPasswordColumn; use OCA\Files_Sharing\Tests\TestCase; use OCP\IConfig; +use OCP\IDBConnection; use OCP\Migration\IOutput; -use OCP\Share; +use OCP\Server; +use OCP\Share\IShare; /** * Class SetPasswordColumnTest @@ -36,10 +22,10 @@ use OCP\Share; */ class SetPasswordColumnTest extends TestCase { - /** @var \OCP\IDBConnection */ + /** @var IDBConnection */ private $connection; - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ private $config; /** @var SetPasswordColumn */ @@ -47,17 +33,17 @@ class SetPasswordColumnTest extends TestCase { private $table = 'share'; - public function setUp() { + protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); $this->config = $this->createMock(IConfig::class); $this->migration = new SetPasswordColumn($this->connection, $this->config); $this->cleanDB(); } - public function tearDown() { + protected function tearDown(): void { parent::tearDown(); $this->cleanDB(); } @@ -67,13 +53,13 @@ class SetPasswordColumnTest extends TestCase { $query->delete($this->table)->execute(); } - public function testAddPasswordColumn() { + public function testAddPasswordColumn(): void { $this->config->expects($this->once()) ->method('getAppValue') ->with('files_sharing', 'installed_version', '0.0.0') ->willReturn('1.3.0'); - $shareTypes = [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE, Share::SHARE_TYPE_EMAIL, Share::SHARE_TYPE_LINK]; + $shareTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_EMAIL, IShare::TYPE_LINK]; foreach ($shareTypes as $shareType) { for ($i = 0; $i < 5; $i++) { @@ -105,11 +91,13 @@ class SetPasswordColumnTest extends TestCase { $query = $this->connection->getQueryBuilder(); $query->select('*') ->from('share'); - $allShares = $query->execute()->fetchAll(); + $result = $query->execute(); + $allShares = $result->fetchAll(); + $result->closeCursor(); foreach ($allShares as $share) { - if ((int)$share['share_type'] === Share::SHARE_TYPE_LINK) { - $this->assertNull( $share['share_with']); + if ((int)$share['share_type'] === IShare::TYPE_LINK) { + $this->assertNull($share['share_with']); $this->assertSame('shareWith', $share['password']); } else { $this->assertSame('shareWith', $share['share_with']); diff --git a/apps/files_sharing/tests/MountProviderTest.php b/apps/files_sharing/tests/MountProviderTest.php index b521e109cf9..e043a1cb1ef 100644 --- a/apps/files_sharing/tests/MountProviderTest.php +++ b/apps/files_sharing/tests/MountProviderTest.php @@ -1,99 +1,107 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Maxence Lange <maxence@nextcloud.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; +use OC\Memcache\NullCache; +use OC\Share20\Share; use OCA\Files_Sharing\MountProvider; +use OCA\Files_Sharing\SharedMount; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\IRootFolder; +use OCP\Files\Mount\IMountManager; use OCP\Files\Storage\IStorageFactory; +use OCP\ICacheFactory; use OCP\IConfig; -use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; -use OCP\Share\IShare; +use OCP\Share\IAttributes as IShareAttributes; use OCP\Share\IManager; +use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; /** * @group DB */ class MountProviderTest extends \Test\TestCase { - /** @var MountProvider */ - private $provider; - - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ - private $config; - - /** @var IUser|\PHPUnit_Framework_MockObject_MockObject */ - private $user; + protected MountProvider $provider; - /** @var IStorageFactory|\PHPUnit_Framework_MockObject_MockObject */ - private $loader; + protected IUser&MockObject $user; + protected IConfig&MockObject $config; + protected IManager&MockObject $shareManager; + protected IStorageFactory&MockObject $loader; + protected LoggerInterface&MockObject $logger; - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ - private $shareManager; - - /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */ - private $logger; - - public function setUp() { + protected function setUp(): void { parent::setUp(); $this->config = $this->getMockBuilder(IConfig::class)->getMock(); $this->user = $this->getMockBuilder(IUser::class)->getMock(); $this->loader = $this->getMockBuilder('OCP\Files\Storage\IStorageFactory')->getMock(); $this->shareManager = $this->getMockBuilder(IManager::class)->getMock(); - $this->logger = $this->getMockBuilder(ILogger::class)->getMock(); + $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); + $eventDispatcher = $this->createMock(IEventDispatcher::class); + $cacheFactory = $this->createMock(ICacheFactory::class); + $cacheFactory->method('createLocal') + ->willReturn(new NullCache()); + $mountManager = $this->createMock(IMountManager::class); + + $this->provider = new MountProvider($this->config, $this->shareManager, $this->logger, $eventDispatcher, $cacheFactory, $mountManager); + } + + private function makeMockShareAttributes($attrs) { + if ($attrs === null) { + return null; + } - $this->provider = new MountProvider($this->config, $this->shareManager, $this->logger); + $shareAttributes = $this->createMock(IShareAttributes::class); + $shareAttributes->method('toArray')->willReturn($attrs); + $shareAttributes->method('getAttribute')->willReturnCallback( + function ($scope, $key) use ($attrs) { + $result = null; + foreach ($attrs as $attr) { + if ($attr['key'] === $key && $attr['scope'] === $scope) { + $result = $attr['value']; + } + } + return $result; + } + ); + return $shareAttributes; } - private function makeMockShare($id, $nodeId, $owner = 'user2', $target = null, $permissions = 31) { + private function makeMockShare($id, $nodeId, $owner = 'user2', $target = null, $permissions = 31, $attributes = null) { $share = $this->createMock(IShare::class); $share->expects($this->any()) ->method('getPermissions') - ->will($this->returnValue($permissions)); + ->willReturn($permissions); + $share->expects($this->any()) + ->method('getAttributes') + ->willReturn($this->makeMockShareAttributes($attributes)); $share->expects($this->any()) ->method('getShareOwner') - ->will($this->returnValue($owner)); + ->willReturn($owner); $share->expects($this->any()) ->method('getTarget') - ->will($this->returnValue($target)); + ->willReturn($target); $share->expects($this->any()) ->method('getId') - ->will($this->returnValue($id)); + ->willReturn($id); $share->expects($this->any()) ->method('getNodeId') - ->will($this->returnValue($nodeId)); + ->willReturn($nodeId); $share->expects($this->any()) ->method('getShareTime') - ->will($this->returnValue( + ->willReturn( // compute share time based on id, simulating share order new \DateTime('@' . (1469193980 + 1000 * $id)) - )); + ); return $share; } @@ -102,86 +110,118 @@ class MountProviderTest extends \Test\TestCase { * - shares that were opted out of (permissions === 0) * - shares with a group in which the owner is already in */ - public function testExcludeShares() { + public function testExcludeShares(): void { $rootFolder = $this->createMock(IRootFolder::class); $userManager = $this->createMock(IUserManager::class); + $attr1 = []; + $attr2 = [['scope' => 'permission', 'key' => 'download', 'value' => true]]; $userShares = [ - $this->makeMockShare(1, 100, 'user2', '/share2', 0), - $this->makeMockShare(2, 100, 'user2', '/share2', 31), + $this->makeMockShare(1, 100, 'user2', '/share2', 0, $attr1), + $this->makeMockShare(2, 100, 'user2', '/share2', 31, $attr2), ]; $groupShares = [ - $this->makeMockShare(3, 100, 'user2', '/share2', 0), - $this->makeMockShare(4, 101, 'user2', '/share4', 31), - $this->makeMockShare(5, 100, 'user1', '/share4', 31), + $this->makeMockShare(3, 100, 'user2', '/share2', 0, $attr1), + $this->makeMockShare(4, 101, 'user2', '/share4', 31, $attr2), + $this->makeMockShare(5, 100, 'user1', '/share4', 31, $attr2), ]; - // tests regarding circles are made in the app itself. + $roomShares = [ + $this->makeMockShare(6, 102, 'user2', '/share6', 0), + $this->makeMockShare(7, 102, 'user1', '/share6', 31), + $this->makeMockShare(8, 102, 'user2', '/share6', 31), + $this->makeMockShare(9, 102, 'user2', '/share6', 31), + ]; + $deckShares = [ + $this->makeMockShare(10, 103, 'user2', '/share7', 0), + $this->makeMockShare(11, 103, 'user1', '/share7', 31), + $this->makeMockShare(12, 103, 'user2', '/share7', 31), + $this->makeMockShare(13, 103, 'user2', '/share7', 31), + ]; + // tests regarding circles and sciencemesh are made in the apps themselves. $circleShares = []; + $scienceMeshShares = []; $this->user->expects($this->any()) ->method('getUID') - ->will($this->returnValue('user1')); - $this->shareManager->expects($this->at(0)) - ->method('getSharedWith') - ->with('user1', \OCP\Share::SHARE_TYPE_USER) - ->will($this->returnValue($userShares)); - $this->shareManager->expects($this->at(1)) - ->method('getSharedWith') - ->with('user1', \OCP\Share::SHARE_TYPE_GROUP, null, -1) - ->will($this->returnValue($groupShares)); - $this->shareManager->expects($this->at(2)) + ->willReturn('user1'); + $this->shareManager->expects($this->exactly(6)) ->method('getSharedWith') - ->with('user1', \OCP\Share::SHARE_TYPE_CIRCLE, null, -1) - ->will($this->returnValue($circleShares)); + ->willReturnMap([ + ['user1', IShare::TYPE_USER, null, -1, 0, $userShares], + ['user1', IShare::TYPE_GROUP, null, -1, 0, $groupShares], + ['user1', IShare::TYPE_CIRCLE, null, -1, 0, $circleShares], + ['user1', IShare::TYPE_ROOM, null, -1, 0, $roomShares], + ['user1', IShare::TYPE_DECK, null, -1, 0, $deckShares], + ['user1', IShare::TYPE_SCIENCEMESH, null, -1, 0, $scienceMeshShares], + ]); + $this->shareManager->expects($this->any()) ->method('newShare') - ->will($this->returnCallback(function() use ($rootFolder, $userManager) { - return new \OC\Share20\Share($rootFolder, $userManager); - })); + ->willReturnCallback(function () use ($rootFolder, $userManager) { + return new Share($rootFolder, $userManager); + }); + $mounts = $this->provider->getMountsForUser($this->user, $this->loader); - $this->assertCount(2, $mounts); + $this->assertCount(4, $mounts); $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[0]); $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[1]); + $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[2]); + $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mounts[3]); + /** @var SharedMount[] $mounts */ $mountedShare1 = $mounts[0]->getShare(); $this->assertEquals('2', $mountedShare1->getId()); $this->assertEquals('user2', $mountedShare1->getShareOwner()); $this->assertEquals(100, $mountedShare1->getNodeId()); $this->assertEquals('/share2', $mountedShare1->getTarget()); $this->assertEquals(31, $mountedShare1->getPermissions()); + $this->assertEquals(true, $mountedShare1->getAttributes()->getAttribute('permission', 'download')); $mountedShare2 = $mounts[1]->getShare(); $this->assertEquals('4', $mountedShare2->getId()); $this->assertEquals('user2', $mountedShare2->getShareOwner()); $this->assertEquals(101, $mountedShare2->getNodeId()); $this->assertEquals('/share4', $mountedShare2->getTarget()); $this->assertEquals(31, $mountedShare2->getPermissions()); + $this->assertEquals(true, $mountedShare2->getAttributes()->getAttribute('permission', 'download')); + $mountedShare3 = $mounts[2]->getShare(); + $this->assertEquals('8', $mountedShare3->getId()); + $this->assertEquals('user2', $mountedShare3->getShareOwner()); + $this->assertEquals(102, $mountedShare3->getNodeId()); + $this->assertEquals('/share6', $mountedShare3->getTarget()); + $this->assertEquals(31, $mountedShare3->getPermissions()); + $mountedShare4 = $mounts[3]->getShare(); + $this->assertEquals('12', $mountedShare4->getId()); + $this->assertEquals('user2', $mountedShare4->getShareOwner()); + $this->assertEquals(103, $mountedShare4->getNodeId()); + $this->assertEquals('/share7', $mountedShare4->getTarget()); + $this->assertEquals(31, $mountedShare4->getPermissions()); } - public function mergeSharesDataProvider() { + public static function mergeSharesDataProvider(): array { // note: the user in the specs here is the shareOwner not recipient // the recipient is always "user1" return [ // #0: share as outsider with "group1" and "user1" with same permissions [ [ - [1, 100, 'user2', '/share2', 31], + [1, 100, 'user2', '/share2', 31, null], ], [ - [2, 100, 'user2', '/share2', 31], + [2, 100, 'user2', '/share2', 31, null], ], [ // combined, user share has higher priority - ['1', 100, 'user2', '/share2', 31], + ['1', 100, 'user2', '/share2', 31, []], ], ], // #1: share as outsider with "group1" and "user1" with different permissions [ [ - [1, 100, 'user2', '/share', 31], + [1, 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true], ['scope' => 'app', 'key' => 'attribute1', 'value' => true]]], ], [ - [2, 100, 'user2', '/share', 15], + [2, 100, 'user2', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => false], ['scope' => 'app', 'key' => 'attribute2', 'value' => false]]], ], [ // use highest permissions - ['1', 100, 'user2', '/share', 31], + ['1', 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true], ['scope' => 'app', 'key' => 'attribute1', 'value' => true], ['scope' => 'app', 'key' => 'attribute2', 'value' => false]]], ], ], // #2: share as outsider with "group1" and "group2" with same permissions @@ -189,12 +229,12 @@ class MountProviderTest extends \Test\TestCase { [ ], [ - [1, 100, 'user2', '/share', 31], - [2, 100, 'user2', '/share', 31], + [1, 100, 'user2', '/share', 31, null], + [2, 100, 'user2', '/share', 31, []], ], [ // combined, first group share has higher priority - ['1', 100, 'user2', '/share', 31], + ['1', 100, 'user2', '/share', 31, []], ], ], // #3: share as outsider with "group1" and "group2" with different permissions @@ -202,12 +242,12 @@ class MountProviderTest extends \Test\TestCase { [ ], [ - [1, 100, 'user2', '/share', 31], - [2, 100, 'user2', '/share', 15], + [1, 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => false]]], + [2, 100, 'user2', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => true]]], ], [ - // use higher permissions - ['1', 100, 'user2', '/share', 31], + // use higher permissions (most permissive) + ['1', 100, 'user2', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true]]], ], ], // #4: share as insider with "group1" @@ -215,7 +255,7 @@ class MountProviderTest extends \Test\TestCase { [ ], [ - [1, 100, 'user1', '/share', 31], + [1, 100, 'user1', '/share', 31, []], ], [ // no received share since "user1" is the sharer/owner @@ -226,8 +266,8 @@ class MountProviderTest extends \Test\TestCase { [ ], [ - [1, 100, 'user1', '/share', 31], - [2, 100, 'user1', '/share', 15], + [1, 100, 'user1', '/share', 31, [['scope' => 'permission', 'key' => 'download', 'value' => true]]], + [2, 100, 'user1', '/share', 15, [['scope' => 'permission', 'key' => 'download', 'value' => false]]], ], [ // no received share since "user1" is the sharer/owner @@ -238,7 +278,7 @@ class MountProviderTest extends \Test\TestCase { [ ], [ - [1, 100, 'user2', '/share', 0], + [1, 100, 'user2', '/share', 0, []], ], [ // no received share since "user1" opted out @@ -247,40 +287,40 @@ class MountProviderTest extends \Test\TestCase { // #7: share as outsider with "group1" and "user1" where recipient renamed in between [ [ - [1, 100, 'user2', '/share2-renamed', 31], + [1, 100, 'user2', '/share2-renamed', 31, []], ], [ - [2, 100, 'user2', '/share2', 31], + [2, 100, 'user2', '/share2', 31, []], ], [ // use target of least recent share - ['1', 100, 'user2', '/share2-renamed', 31], + ['1', 100, 'user2', '/share2-renamed', 31, []], ], ], // #8: share as outsider with "group1" and "user1" where recipient renamed in between [ [ - [2, 100, 'user2', '/share2', 31], + [2, 100, 'user2', '/share2', 31, []], ], [ - [1, 100, 'user2', '/share2-renamed', 31], + [1, 100, 'user2', '/share2-renamed', 31, []], ], [ // use target of least recent share - ['1', 100, 'user2', '/share2-renamed', 31], + ['1', 100, 'user2', '/share2-renamed', 31, []], ], ], // #9: share as outsider with "nullgroup" and "user1" where recipient renamed in between [ [ - [2, 100, 'user2', '/share2', 31], + [2, 100, 'user2', '/share2', 31, []], ], [ - [1, 100, 'nullgroup', '/share2-renamed', 31], + [1, 100, 'nullgroup', '/share2-renamed', 31, []], ], [ // use target of least recent share - ['1', 100, 'nullgroup', '/share2-renamed', 31], + ['1', 100, 'nullgroup', '/share2-renamed', 31, []], ], true ], @@ -293,51 +333,53 @@ class MountProviderTest extends \Test\TestCase { * Happens when sharing the same entry to a user through multiple ways, * like several groups and also direct shares at the same time. * - * @dataProvider mergeSharesDataProvider * * @param array $userShares array of user share specs * @param array $groupShares array of group share specs * @param array $expectedShares array of expected supershare specs */ - public function testMergeShares($userShares, $groupShares, $expectedShares, $moveFails = false) { + #[\PHPUnit\Framework\Attributes\DataProvider('mergeSharesDataProvider')] + public function testMergeShares($userShares, $groupShares, $expectedShares, $moveFails = false): void { $rootFolder = $this->createMock(IRootFolder::class); $userManager = $this->createMock(IUserManager::class); - $userShares = array_map(function($shareSpec) { - return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4]); + $userShares = array_map(function ($shareSpec) { + return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]); }, $userShares); - $groupShares = array_map(function($shareSpec) { - return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4]); + $groupShares = array_map(function ($shareSpec) { + return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4], $shareSpec[5]); }, $groupShares); $this->user->expects($this->any()) ->method('getUID') - ->will($this->returnValue('user1')); + ->willReturn('user1'); // tests regarding circles are made in the app itself. $circleShares = []; - $this->shareManager->expects($this->at(0)) - ->method('getSharedWith') - ->with('user1', \OCP\Share::SHARE_TYPE_USER) - ->will($this->returnValue($userShares)); - $this->shareManager->expects($this->at(1)) + $roomShares = []; + $deckShares = []; + $scienceMeshShares = []; + $this->shareManager->expects($this->exactly(6)) ->method('getSharedWith') - ->with('user1', \OCP\Share::SHARE_TYPE_GROUP, null, -1) - ->will($this->returnValue($groupShares)); - $this->shareManager->expects($this->at(2)) - ->method('getSharedWith') - ->with('user1', \OCP\Share::SHARE_TYPE_CIRCLE, null, -1) - ->will($this->returnValue($circleShares)); + ->willReturnMap([ + ['user1', IShare::TYPE_USER, null, -1, 0, $userShares], + ['user1', IShare::TYPE_GROUP, null, -1, 0, $groupShares], + ['user1', IShare::TYPE_CIRCLE, null, -1, 0, $circleShares], + ['user1', IShare::TYPE_ROOM, null, -1, 0, $roomShares], + ['user1', IShare::TYPE_DECK, null, -1, 0, $deckShares], + ['user1', IShare::TYPE_SCIENCEMESH, null, -1, 0, $scienceMeshShares], + ]); + $this->shareManager->expects($this->any()) ->method('newShare') - ->will($this->returnCallback(function() use ($rootFolder, $userManager) { - return new \OC\Share20\Share($rootFolder, $userManager); - })); + ->willReturnCallback(function () use ($rootFolder, $userManager) { + return new Share($rootFolder, $userManager); + }); if ($moveFails) { $this->shareManager->expects($this->any()) ->method('moveShare') - ->will($this->throwException(new \InvalidArgumentException())); + ->willThrowException(new \InvalidArgumentException()); } $mounts = $this->provider->getMountsForUser($this->user, $this->loader); @@ -349,6 +391,7 @@ class MountProviderTest extends \Test\TestCase { $this->assertInstanceOf('OCA\Files_Sharing\SharedMount', $mount); // supershare + /** @var SharedMount $mount */ $share = $mount->getShare(); $this->assertEquals($expectedShare[0], $share->getId()); @@ -356,7 +399,11 @@ class MountProviderTest extends \Test\TestCase { $this->assertEquals($expectedShare[2], $share->getShareOwner()); $this->assertEquals($expectedShare[3], $share->getTarget()); $this->assertEquals($expectedShare[4], $share->getPermissions()); + if ($expectedShare[5] === null) { + $this->assertNull($share->getAttributes()); + } else { + $this->assertEquals($expectedShare[5], $share->getAttributes()->toArray()); + } } } } - diff --git a/apps/files_sharing/tests/PermissionsTest.php b/apps/files_sharing/tests/PermissionsTest.php deleted file mode 100644 index 2eca474e966..00000000000 --- a/apps/files_sharing/tests/PermissionsTest.php +++ /dev/null @@ -1,164 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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/> - * - */ - - -namespace OCA\Files_Sharing\Tests; - -use OC\Files\Cache\Cache; -use OC\Files\Storage\Storage; -use OC\Files\View; - -/** - * Class PermissionsTest - * - * @group DB - */ -class PermissionsTest extends TestCase { - - /** @var Storage */ - private $sharedStorageRestrictedShare; - - /** @var Storage */ - private $sharedCacheRestrictedShare; - - /** @var View */ - private $secondView; - - /** @var Storage */ - private $ownerStorage; - - /** @var Storage */ - private $sharedStorage; - - /** @var Cache */ - private $sharedCache; - - /** @var Cache */ - private $ownerCache; - - protected function setUp() { - parent::setUp(); - - self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - - // prepare user1's dir structure - $textData = "dummy file data\n"; - $this->view->mkdir('container'); - $this->view->mkdir('container/shareddir'); - $this->view->mkdir('container/shareddir/subdir'); - $this->view->mkdir('container/shareddirrestricted'); - $this->view->mkdir('container/shareddirrestricted/subdir'); - $this->view->file_put_contents('container/shareddir/textfile.txt', $textData); - $this->view->file_put_contents('container/shareddirrestricted/textfile1.txt', $textData); - - list($this->ownerStorage, $internalPath) = $this->view->resolvePath(''); - $this->ownerCache = $this->ownerStorage->getCache(); - $this->ownerStorage->getScanner()->scan(''); - - // share "shareddir" with user2 - $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1); - - $node = $rootFolder->get('container/shareddir'); - $share = $this->shareManager->newShare(); - $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) - ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_ALL); - $this->shareManager->createShare($share); - - $node = $rootFolder->get('container/shareddirrestricted'); - $share = $this->shareManager->newShare(); - $share->setNode($node) - ->setShareType(\OCP\Share::SHARE_TYPE_USER) - ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) - ->setSharedBy(self::TEST_FILES_SHARING_API_USER1) - ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE); - $this->shareManager->createShare($share); - - // login as user2 - self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - - // retrieve the shared storage - $this->secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2); - list($this->sharedStorage, $internalPath) = $this->secondView->resolvePath('files/shareddir'); - list($this->sharedStorageRestrictedShare, $internalPath) = $this->secondView->resolvePath('files/shareddirrestricted'); - $this->sharedCache = $this->sharedStorage->getCache(); - $this->sharedCacheRestrictedShare = $this->sharedStorageRestrictedShare->getCache(); - } - - protected function tearDown() { - if ($this->sharedCache) { - $this->sharedCache->clear(); - } - - self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - - $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER); - foreach ($shares as $share) { - $this->shareManager->deleteShare($share); - } - - $this->view->deleteAll('container'); - - $this->ownerCache->clear(); - - parent::tearDown(); - } - - /** - * Test that the permissions of shared directory are returned correctly - */ - function testGetPermissions() { - $sharedDirPerms = $this->sharedStorage->getPermissions('shareddir'); - $this->assertEquals(31, $sharedDirPerms); - $sharedDirPerms = $this->sharedStorage->getPermissions('shareddir/textfile.txt'); - $this->assertEquals(31, $sharedDirPerms); - $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('shareddirrestricted'); - $this->assertEquals(7, $sharedDirRestrictedPerms); - $sharedDirRestrictedPerms = $this->sharedStorageRestrictedShare->getPermissions('shareddirrestricted/textfile.txt'); - $this->assertEquals(7, $sharedDirRestrictedPerms); - } - - /** - * Test that the permissions of shared directory are returned correctly - */ - function testGetDirectoryPermissions() { - $contents = $this->secondView->getDirectoryContent('files/shareddir'); - $this->assertEquals('subdir', $contents[0]['name']); - $this->assertEquals(31, $contents[0]['permissions']); - $this->assertEquals('textfile.txt', $contents[1]['name']); - // 27 is correct because create is reserved to folders only - requires more unit tests overall to ensure this - $this->assertEquals(27, $contents[1]['permissions']); - $contents = $this->secondView->getDirectoryContent('files/shareddirrestricted'); - $this->assertEquals('subdir', $contents[0]['name']); - $this->assertEquals(7, $contents[0]['permissions']); - $this->assertEquals('textfile1.txt', $contents[1]['name']); - // 3 is correct because create is reserved to folders only - $this->assertEquals(3, $contents[1]['permissions']); - } -} diff --git a/apps/files_sharing/tests/PropagationTestCase.php b/apps/files_sharing/tests/PropagationTestCase.php index 21cea3033af..98bf5ad92fd 100644 --- a/apps/files_sharing/tests/PropagationTestCase.php +++ b/apps/files_sharing/tests/PropagationTestCase.php @@ -1,47 +1,36 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Robin Appelman <robin@icewind.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: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; +use OC\Files\View; +use OCA\Files_Sharing\Helper; +use OCP\IUserSession; +use OCP\Server; + abstract class PropagationTestCase extends TestCase { /** - * @var \OC\Files\View + * @var View */ protected $rootView; protected $fileIds = []; // [$user=>[$path=>$id]] protected $fileEtags = []; // [$id=>$etag] - public static function setUpBeforeClass() { + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - \OCA\Files_Sharing\Helper::registerHooks(); + Helper::registerHooks(); } - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->setUpShares(); } - protected function tearDown() { + protected function tearDown(): void { \OC_Hook::clear('OC_Filesystem', 'post_write'); \OC_Hook::clear('OC_Filesystem', 'post_delete'); \OC_Hook::clear('OC_Filesystem', 'post_rename'); @@ -56,7 +45,7 @@ abstract class PropagationTestCase extends TestCase { * @param string $subPath */ protected function assertEtagsChanged($users, $subPath = '') { - $oldUser = \OC::$server->getUserSession()->getUser(); + $oldUser = Server::get(IUserSession::class)->getUser(); foreach ($users as $user) { $this->loginAsUser($user); $id = $this->fileIds[$user][$subPath]; @@ -73,7 +62,7 @@ abstract class PropagationTestCase extends TestCase { * @param string $subPath */ protected function assertEtagsNotChanged($users, $subPath = '') { - $oldUser = \OC::$server->getUserSession()->getUser(); + $oldUser = Server::get(IUserSession::class)->getUser(); foreach ($users as $user) { $this->loginAsUser($user); $id = $this->fileIds[$user][$subPath]; diff --git a/apps/files_sharing/tests/ShareTest.php b/apps/files_sharing/tests/ShareTest.php index 9b60255ed97..737ad6dcb4e 100644 --- a/apps/files_sharing/tests/ShareTest.php +++ b/apps/files_sharing/tests/ShareTest.php @@ -1,48 +1,39 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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 OCA\Files_Sharing\Tests; +use OC\Files\FileInfo; +use OC\Files\Filesystem; +use OCA\Files_Sharing\Helper; +use OCP\Constants; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IUserManager; +use OCP\Server; +use OCP\Share\IShare; + /** * Class ShareTest * * @group DB */ class ShareTest extends TestCase { - - const TEST_FOLDER_NAME = '/folder_share_api_test'; + public const TEST_FOLDER_NAME = '/folder_share_api_test'; private static $tempStorage; - protected function setUp() { + private string $subsubfolder = ''; + + protected function setUp(): void { parent::setUp(); $this->folder = self::TEST_FOLDER_NAME; - $this->subfolder = '/subfolder_share_api_test'; + $this->subfolder = '/subfolder_share_api_test'; $this->subsubfolder = '/subsubfolder_share_api_test'; $this->filename = '/share-api-test.txt'; @@ -52,27 +43,23 @@ class ShareTest extends TestCase { $this->view->mkdir($this->folder); $this->view->mkdir($this->folder . $this->subfolder); $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder); - $this->view->file_put_contents($this->folder.$this->filename, $this->data); + $this->view->file_put_contents($this->folder . $this->filename, $this->data); $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data); } - protected function tearDown() { + protected function tearDown(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $this->view->unlink($this->filename); $this->view->deleteAll($this->folder); self::$tempStorage = null; - // clear database table - $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share`'); - $query->execute(); - parent::tearDown(); } - public function testUnshareFromSelf() { - $groupManager = \OC::$server->getGroupManager(); - $userManager = \OC::$server->getUserManager(); + public function testUnshareFromSelf(): void { + $groupManager = Server::get(IGroupManager::class); + $userManager = Server::get(IUserManager::class); $testGroup = $groupManager->createGroup('testGroup'); $user1 = $userManager->get(self::TEST_FILES_SHARING_API_USER2); @@ -81,132 +68,134 @@ class ShareTest extends TestCase { $testGroup->addUser($user2); $share1 = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->filename, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); $share2 = $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + IShare::TYPE_GROUP, $this->filename, self::TEST_FILES_SHARING_API_USER1, 'testGroup', - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); + $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2); + $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER3); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertTrue(Filesystem::file_exists($this->filename)); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertTrue(Filesystem::file_exists($this->filename)); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - \OC\Files\Filesystem::unlink($this->filename); + Filesystem::unlink($this->filename); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // both group share and user share should be gone - $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertFalse(Filesystem::file_exists($this->filename)); // for user3 nothing should change self::loginHelper(self::TEST_FILES_SHARING_API_USER3); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertTrue(Filesystem::file_exists($this->filename)); $this->shareManager->deleteShare($share1); $this->shareManager->deleteShare($share2); } /** - * @param \OC\Files\FileInfo[] $content + * @param FileInfo[] $content * @param string[] $expected */ public function verifyDirContent($content, $expected) { foreach ($content as $c) { if (!in_array($c['name'], $expected)) { - $this->assertTrue(false, "folder should only contain '" . implode(',', $expected) . "', found: " .$c['name']); + $this->assertTrue(false, "folder should only contain '" . implode(',', $expected) . "', found: " . $c['name']); } } } - public function testShareWithDifferentShareFolder() { - + public function testShareWithDifferentShareFolder(): void { $fileinfo = $this->view->getFileInfo($this->filename); $folderinfo = $this->view->getFileInfo($this->folder); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->filename, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); - \OCA\Files_Sharing\Helper::setShareFolder('/Shared/subfolder'); + Helper::setShareFolder('/Shared/subfolder'); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); - $this->assertTrue(\OC\Files\Filesystem::file_exists('/Shared/subfolder/' . $this->folder)); + $this->assertTrue(Filesystem::file_exists($this->filename)); + $this->assertTrue(Filesystem::file_exists('/Shared/subfolder/' . $this->folder)); //cleanup - \OC::$server->getConfig()->deleteSystemValue('share_folder'); + Server::get(IConfig::class)->deleteSystemValue('share_folder'); } - public function testShareWithGroupUniqueName() { + public function testShareWithGroupUniqueName(): void { + $this->markTestSkipped('TODO: Disable because fails on drone'); + $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); - \OC\Files\Filesystem::file_put_contents('test.txt', 'test'); + Filesystem::file_put_contents('test.txt', 'test'); $share = $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + IShare::TYPE_GROUP, 'test.txt', self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_GROUP1, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); - $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP); + $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_GROUP); $share = $shares[0]; - $this->assertSame('/test.txt' ,$share->getTarget()); + $this->assertSame('/test.txt', $share->getTarget()); $this->assertSame(19, $share->getPermissions()); - - \OC\Files\Filesystem::rename('test.txt', 'new test.txt'); - $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP); + Filesystem::rename('test.txt', 'new test.txt'); + + $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_GROUP); $share = $shares[0]; - $this->assertSame('/new test.txt' ,$share->getTarget()); + $this->assertSame('/new test.txt', $share->getTarget()); $this->assertSame(19, $share->getPermissions()); - - $share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE); + + $share->setPermissions(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE); $this->shareManager->updateShare($share); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); - $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP); + $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_GROUP); $share = $shares[0]; - $this->assertSame('/new test.txt' ,$share->getTarget()); + $this->assertSame('/new test.txt', $share->getTarget()); $this->assertSame(3, $share->getPermissions()); } /** * shared files should never have delete permissions - * @dataProvider dataProviderTestFileSharePermissions */ - public function testFileSharePermissions($permission, $expectedvalid) { - + #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderTestFileSharePermissions')] + public function testFileSharePermissions($permission, $expectedvalid): void { $pass = true; try { $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->filename, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, @@ -219,35 +208,34 @@ class ShareTest extends TestCase { $this->assertEquals($expectedvalid, $pass); } - public function dataProviderTestFileSharePermissions() { - $permission1 = \OCP\Constants::PERMISSION_ALL; - $permission3 = \OCP\Constants::PERMISSION_READ; - $permission4 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE; - $permission5 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE; - $permission6 = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; - - return array( - array($permission1, false), - array($permission3, true), - array($permission4, true), - array($permission5, false), - array($permission6, false), - ); + public static function dataProviderTestFileSharePermissions() { + $permission1 = Constants::PERMISSION_ALL; + $permission3 = Constants::PERMISSION_READ; + $permission4 = Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE; + $permission5 = Constants::PERMISSION_READ | Constants::PERMISSION_DELETE; + $permission6 = Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE; + + return [ + [$permission1, false], + [$permission3, true], + [$permission4, true], + [$permission5, false], + [$permission6, false], + ]; } - public function testFileOwner() { - + public function testFileOwner(): void { $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->filename, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ + Constants::PERMISSION_READ ); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); - $info = \OC\Files\Filesystem::getFileInfo($this->filename); + $info = Filesystem::getFileInfo($this->filename); $this->assertSame(self::TEST_FILES_SHARING_API_USER1, $info->getOwner()->getUID()); } diff --git a/apps/files_sharing/tests/SharedMountTest.php b/apps/files_sharing/tests/SharedMountTest.php index 1eddbcb64f7..cc9c70a241f 100644 --- a/apps/files_sharing/tests/SharedMountTest.php +++ b/apps/files_sharing/tests/SharedMountTest.php @@ -1,35 +1,24 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; + +use OC\Files\Filesystem; +use OC\Files\View; +use OC\Memcache\ArrayCache; +use OCA\Files_Sharing\MountProvider; +use OCA\Files_Sharing\SharedMount; +use OCP\Constants; +use OCP\ICacheFactory; +use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IUserManager; +use OCP\Server; +use OCP\Share\IShare; /** * Class SharedMountTest @@ -44,25 +33,30 @@ class SharedMountTest extends TestCase { /** @var IUserManager */ private $userManager; - protected function setUp() { + private $folder2; + + protected function setUp(): void { parent::setUp(); $this->folder = '/folder_share_storage_test'; + $this->folder2 = '/folder_share_storage_test2'; $this->filename = '/share-api-storage.txt'; $this->view->mkdir($this->folder); + $this->view->mkdir($this->folder2); // save file with content $this->view->file_put_contents($this->filename, 'root file'); $this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder'); + $this->view->file_put_contents($this->folder2 . $this->filename, 'file in subfolder2'); - $this->groupManager = \OC::$server->getGroupManager(); - $this->userManager = \OC::$server->getUserManager(); + $this->groupManager = Server::get(IGroupManager::class); + $this->userManager = Server::get(IUserManager::class); } - protected function tearDown() { + protected function tearDown(): void { if ($this->view) { if ($this->view->file_exists($this->folder)) { $this->view->unlink($this->folder); @@ -78,15 +72,16 @@ class SharedMountTest extends TestCase { /** * test if the mount point moves up if the parent folder no longer exists */ - public function testShareMountLoseParentFolder() { + public function testShareMountLoseParentFolder(): void { // share to user $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL); + Constants::PERMISSION_ALL); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); $share->setTarget('/foo/bar' . $this->folder); $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2); @@ -109,18 +104,18 @@ class SharedMountTest extends TestCase { /** * @medium */ - public function testDeleteParentOfMountPoint() { + public function testDeleteParentOfMountPoint(): void { // share to user $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($user2View->file_exists($this->folder)); // create a local folder @@ -149,31 +144,31 @@ class SharedMountTest extends TestCase { $this->view->unlink($this->folder); } - public function testMoveSharedFile() { + public function testMoveSharedFile(): void { $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->filename, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - \OC\Files\Filesystem::rename($this->filename, $this->filename . '_renamed'); + Filesystem::rename($this->filename, $this->filename . '_renamed'); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename . '_renamed')); - $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertTrue(Filesystem::file_exists($this->filename . '_renamed')); + $this->assertFalse(Filesystem::file_exists($this->filename)); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); - $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed')); + $this->assertTrue(Filesystem::file_exists($this->filename)); + $this->assertFalse(Filesystem::file_exists($this->filename . '_renamed')); // rename back to original name self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - \OC\Files\Filesystem::rename($this->filename . '_renamed', $this->filename); - $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed')); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + Filesystem::rename($this->filename . '_renamed', $this->filename); + $this->assertFalse(Filesystem::file_exists($this->filename . '_renamed')); + $this->assertTrue(Filesystem::file_exists($this->filename)); //cleanup $this->shareManager->deleteShare($share); @@ -183,7 +178,7 @@ class SharedMountTest extends TestCase { * share file with a group if a user renames the file the filename should not change * for the other users */ - public function testMoveGroupShare () { + public function testMoveGroupShare(): void { $testGroup = $this->groupManager->createGroup('testGroup'); $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1); $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2); @@ -194,29 +189,32 @@ class SharedMountTest extends TestCase { $fileinfo = $this->view->getFileInfo($this->filename); $share = $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + IShare::TYPE_GROUP, $this->filename, self::TEST_FILES_SHARING_API_USER1, 'testGroup', - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); + $this->shareManager->acceptShare($share, $user1->getUID()); + $this->shareManager->acceptShare($share, $user2->getUID()); + $this->shareManager->acceptShare($share, $user3->getUID()); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertTrue(Filesystem::file_exists($this->filename)); - \OC\Files\Filesystem::rename($this->filename, 'newFileName'); + Filesystem::rename($this->filename, 'newFileName'); - $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName')); - $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertTrue(Filesystem::file_exists('newFileName')); + $this->assertFalse(Filesystem::file_exists($this->filename)); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); - $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName')); + $this->assertTrue(Filesystem::file_exists($this->filename)); + $this->assertFalse(Filesystem::file_exists('newFileName')); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); - $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName')); + $this->assertTrue(Filesystem::file_exists($this->filename)); + $this->assertFalse(Filesystem::file_exists('newFileName')); //cleanup self::loginHelper(self::TEST_FILES_SHARING_API_USER1); @@ -227,12 +225,12 @@ class SharedMountTest extends TestCase { } /** - * @dataProvider dataProviderTestStripUserFilesPath * @param string $path * @param string $expectedResult * @param bool $exception if a exception is expected */ - public function testStripUserFilesPath($path, $expectedResult, $exception) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderTestStripUserFilesPath')] + public function testStripUserFilesPath($path, $expectedResult, $exception): void { $testClass = new DummyTestClassSharedMount(null, null); try { $result = $testClass->stripUserFilesPathDummy($path); @@ -241,97 +239,27 @@ class SharedMountTest extends TestCase { if ($exception) { $this->assertSame(10, $e->getCode()); } else { - $this->assertTrue(false, 'Exception catched, but expected: ' . $expectedResult); + $this->assertTrue(false, 'Exception caught, but expected: ' . $expectedResult); } } } - public function dataProviderTestStripUserFilesPath() { - return array( - array('/user/files/foo.txt', '/foo.txt', false), - array('/user/files/folder/foo.txt', '/folder/foo.txt', false), - array('/data/user/files/foo.txt', null, true), - array('/data/user/files/', null, true), - array('/files/foo.txt', null, true), - array('/foo.txt', null, true), - ); - } - - public function dataPermissionMovedGroupShare() { - $data = []; - - $powerset = function($permissions) { - $results = [\OCP\Constants::PERMISSION_READ]; - - foreach ($permissions as $permission) { - foreach ($results as $combination) { - $results[] = $permission | $combination; - } - } - return $results; - }; - - //Generate file permissions - $permissions = [ - \OCP\Constants::PERMISSION_UPDATE, - \OCP\Constants::PERMISSION_SHARE, + public static function dataProviderTestStripUserFilesPath() { + return [ + ['/user/files/foo.txt', '/foo.txt', false], + ['/user/files/folder/foo.txt', '/folder/foo.txt', false], + ['/data/user/files/foo.txt', null, true], + ['/data/user/files/', null, true], + ['/files/foo.txt', null, true], + ['/foo.txt', null, true], ]; - - $allPermissions = $powerset($permissions); - - foreach ($allPermissions as $before) { - foreach ($allPermissions as $after) { - if ($before === $after) { continue; } - - $data[] = [ - 'file', - $before, - $after, - ]; - } - } - - //Generate folder permissions - $permissions = [ - \OCP\Constants::PERMISSION_UPDATE, - \OCP\Constants::PERMISSION_CREATE, - \OCP\Constants::PERMISSION_SHARE, - \OCP\Constants::PERMISSION_DELETE, - ]; - - $allPermissions = $powerset($permissions); - - foreach ($allPermissions as $before) { - foreach ($allPermissions as $after) { - if ($before === $after) { continue; } - - $data[] = [ - 'folder', - $before, - $after, - ]; - } - } - - return $data; } - - /** - * moved mountpoints of a group share should keep the same permission as their parent group share. - * See #15253 - * - * @dataProvider dataPermissionMovedGroupShare + * If the permissions on a group share are upgraded be sure to still respect + * removed shares by a member of that group */ - public function testPermissionMovedGroupShare($type, $beforePerm, $afterPerm) { - - if ($type === 'file') { - $path = $this->filename; - } else if ($type === 'folder') { - $path = $this->folder; - } - + public function testPermissionUpgradeOnUserDeletedGroupShare(): void { $testGroup = $this->groupManager->createGroup('testGroup'); $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1); $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2); @@ -340,115 +268,165 @@ class SharedMountTest extends TestCase { $testGroup->addUser($user2); $testGroup->addUser($user3); + $connection = Server::get(IDBConnection::class); + // Share item with group + $fileinfo = $this->view->getFileInfo($this->folder); $share = $this->share( - \OCP\Share::SHARE_TYPE_GROUP, - $path, + IShare::TYPE_GROUP, + $this->folder, self::TEST_FILES_SHARING_API_USER1, 'testGroup', - $beforePerm + Constants::PERMISSION_READ ); + $this->shareManager->acceptShare($share, $user1->getUID()); + $this->shareManager->acceptShare($share, $user2->getUID()); + $this->shareManager->acceptShare($share, $user3->getUID()); // Login as user 2 and verify the item exists self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists($path)); + $this->assertTrue(Filesystem::file_exists($this->folder)); $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2); - $this->assertEquals($beforePerm, $result->getPermissions()); + $this->assertNotEmpty($result); + $this->assertEquals(Constants::PERMISSION_READ, $result->getPermissions()); - // Now move the item forcing a new entry in the share table - \OC\Files\Filesystem::rename($path, 'newPath'); - $this->assertTrue(\OC\Files\Filesystem::file_exists('newPath')); - $this->assertFalse(\OC\Files\Filesystem::file_exists($path)); + // Delete the share + $this->assertTrue(Filesystem::rmdir($this->folder)); + $this->assertFalse(Filesystem::file_exists($this->folder)); - // change permissions - $share->setPermissions($afterPerm); - $this->shareManager->updateShare($share); + // Verify we do not get a share + $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2); + $this->assertEquals(0, $result->getPermissions()); - // Login as user 3 and verify that the permissions are changed - self::loginHelper(self::TEST_FILES_SHARING_API_USER3); - $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER3); - $this->assertNotEmpty($result); - $this->assertEquals($afterPerm, $result->getPermissions()); + // Login as user 1 again and change permissions + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + $share->setPermissions(Constants::PERMISSION_ALL); + $share = $this->shareManager->updateShare($share); - // Login as user 2 and verify that the permissions are changed + // Login as user 2 and verify self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + $this->assertFalse(Filesystem::file_exists($this->folder)); $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2); - $this->assertNotEmpty($result); - $this->assertEquals($afterPerm, $result->getPermissions()); - $this->assertEquals('/newPath', $result->getTarget()); + $this->assertEquals(0, $result->getPermissions()); + + $this->shareManager->deleteShare($share); //cleanup self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - $this->shareManager->deleteShare($share); $testGroup->removeUser($user1); $testGroup->removeUser($user2); $testGroup->removeUser($user3); } /** - * If the permissions on a group share are upgraded be sure to still respect - * removed shares by a member of that group + * test if the mount point gets renamed if a folder exists at the target */ - public function testPermissionUpgradeOnUserDeletedGroupShare() { - $testGroup = $this->groupManager->createGroup('testGroup'); - $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1); - $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2); - $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3); - $testGroup->addUser($user1); - $testGroup->addUser($user2); - $testGroup->addUser($user3); + public function testShareMountOverFolder(): void { + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + $this->view2->mkdir('bar'); - $connection = \OC::$server->getDatabaseConnection(); + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - // Share item with group - $fileinfo = $this->view->getFileInfo($this->folder); + // share to user $share = $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, - 'testGroup', - \OCP\Constants::PERMISSION_READ - ); + self::TEST_FILES_SHARING_API_USER2, + Constants::PERMISSION_ALL); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); - // Login as user 2 and verify the item exists - self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder)); - $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2); - $this->assertNotEmpty($result); - $this->assertEquals(\OCP\Constants::PERMISSION_READ, $result->getPermissions()); + $share->setTarget('/bar'); + $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2); - // Delete the share - $this->assertTrue(\OC\Files\Filesystem::rmdir($this->folder)); - $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder)); + $share = $this->shareManager->getShareById($share->getFullId()); - // Verify we do not get a share - $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2); - $this->assertEquals(0, $result->getPermissions()); + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + // share should have been moved - // Login as user 1 again and change permissions + $share = $this->shareManager->getShareById($share->getFullId()); + $this->assertSame('/bar (2)', $share->getTarget()); + + //cleanup self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL); - $share = $this->shareManager->updateShare($share); + $this->shareManager->deleteShare($share); + $this->view->unlink($this->folder); + } + + /** + * test if the mount point gets renamed if another share exists at the target + */ + public function testShareMountOverShare(): void { + // create a shared cache + $caches = []; + $cacheFactory = $this->createMock(ICacheFactory::class); + $cacheFactory->method('createLocal') + ->willReturnCallback(function (string $prefix) use (&$caches) { + if (!isset($caches[$prefix])) { + $caches[$prefix] = new ArrayCache($prefix); + } + return $caches[$prefix]; + }); + $cacheFactory->method('createDistributed') + ->willReturnCallback(function (string $prefix) use (&$caches) { + if (!isset($caches[$prefix])) { + $caches[$prefix] = new ArrayCache($prefix); + } + return $caches[$prefix]; + }); + + // hack to overwrite the cache factory, we can't use the proper "overwriteService" since the mount provider is created before this test is called + $mountProvider = Server::get(MountProvider::class); + $reflectionClass = new \ReflectionClass($mountProvider); + $reflectionCacheFactory = $reflectionClass->getProperty('cacheFactory'); + $reflectionCacheFactory->setAccessible(true); + $reflectionCacheFactory->setValue($mountProvider, $cacheFactory); + + // share to user + $share = $this->share( + IShare::TYPE_USER, + $this->folder, + self::TEST_FILES_SHARING_API_USER1, + self::TEST_FILES_SHARING_API_USER2, + Constants::PERMISSION_ALL); + $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2); + + $share->setTarget('/foobar'); + $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2); + + + // share to user + $share2 = $this->share( + IShare::TYPE_USER, + $this->folder2, + self::TEST_FILES_SHARING_API_USER1, + self::TEST_FILES_SHARING_API_USER2, + Constants::PERMISSION_ALL); + $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2); + + $share2->setTarget('/foobar'); + $this->shareManager->moveShare($share2, self::TEST_FILES_SHARING_API_USER2); - // Login as user 2 and verify self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder)); - $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2); - $this->assertEquals(0, $result->getPermissions()); + // one of the shares should have been moved - $this->shareManager->deleteShare($share); + $share = $this->shareManager->getShareById($share->getFullId()); + $share2 = $this->shareManager->getShareById($share2->getFullId()); + + // we don't know or care which share got the "(2)" just that one of them did + $this->assertNotEquals($share->getTarget(), $share2->getTarget()); + $this->assertSame('/foobar', min($share->getTarget(), $share2->getTarget())); + $this->assertSame('/foobar (2)', max($share->getTarget(), $share2->getTarget())); //cleanup self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - $testGroup->removeUser($user1); - $testGroup->removeUser($user2); - $testGroup->removeUser($user3); + $this->shareManager->deleteShare($share); + $this->view->unlink($this->folder); } - } -class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount { - public function __construct($storage, $mountpoint, $arguments = null, $loader = null){ +class DummyTestClassSharedMount extends SharedMount { + public function __construct($storage, $mountpoint, $arguments = null, $loader = null) { // noop } diff --git a/apps/files_sharing/tests/SharedStorageTest.php b/apps/files_sharing/tests/SharedStorageTest.php index c443aa9c45f..1c1f0a7b71d 100644 --- a/apps/files_sharing/tests/SharedStorageTest.php +++ b/apps/files_sharing/tests/SharedStorageTest.php @@ -1,37 +1,27 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; -use OCA\Files_Sharing\SharedStorage; -use OCP\Share\IShare; +use OC\Files\Cache\FailedCache; +use OC\Files\Filesystem; +use OC\Files\Storage\FailedStorage; +use OC\Files\Storage\Storage; +use OC\Files\Storage\Temporary; use OC\Files\View; +use OCA\Files_Sharing\SharedStorage; +use OCA\Files_Trashbin\AppInfo\Application; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\Constants; +use OCP\Files\Config\IMountProviderCollection; use OCP\Files\NotFoundException; +use OCP\IUserManager; +use OCP\Server; +use OCP\Share\IShare; /** * Class SharedStorageTest @@ -39,10 +29,11 @@ use OCP\Files\NotFoundException; * @group DB */ class SharedStorageTest extends TestCase { - - protected function setUp() { + protected function setUp(): void { parent::setUp(); - \OCA\Files_Trashbin\Trashbin::registerHooks(); + // register trashbin hooks + $trashbinApp = new Application(); + $trashbinApp->boot($this->createMock(IBootContext::class)); $this->folder = '/folder_share_storage_test'; $this->filename = '/share-api-storage.txt'; @@ -51,11 +42,11 @@ class SharedStorageTest extends TestCase { $this->view->mkdir($this->folder); // save file with content - $this->view->file_put_contents($this->filename, "root file"); - $this->view->file_put_contents($this->folder . $this->filename, "file in subfolder"); + $this->view->file_put_contents($this->filename, 'root file'); + $this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder'); } - protected function tearDown() { + protected function tearDown(): void { if ($this->view) { if ($this->view->file_exists($this->folder)) { $this->view->unlink($this->folder); @@ -65,7 +56,7 @@ class SharedStorageTest extends TestCase { } } - \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); + Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); parent::tearDown(); } @@ -75,19 +66,19 @@ class SharedStorageTest extends TestCase { * * @medium */ - public function testParentOfMountPointIsGone() { + public function testParentOfMountPointIsGone(): void { // share to user $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($user2View->file_exists($this->folder)); // create a local folder @@ -102,8 +93,8 @@ class SharedStorageTest extends TestCase { $this->assertFalse($user2View->is_dir($this->folder)); // delete the local folder - /** @var \OC\Files\Storage\Storage $storage */ - list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/localfolder'); + /** @var Storage $storage */ + [$storage, $internalPath] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/localfolder'); $storage->rmdir($internalPath); //enforce reload of the mount points @@ -120,20 +111,20 @@ class SharedStorageTest extends TestCase { /** * @medium */ - public function testRenamePartFile() { + public function testRenamePartFile(): void { // share to user $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($user2View->file_exists($this->folder)); @@ -158,63 +149,63 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share); } - public function testFilesize() { + public function testFilesize(): void { $folderSize = $this->view->filesize($this->folder); $file1Size = $this->view->filesize($this->folder . $this->filename); $file2Size = $this->view->filesize($this->filename); $share1 = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); $share2 = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->filename, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // compare file size between user1 and user2, should always be the same - $this->assertSame($folderSize, \OC\Files\Filesystem::filesize($this->folder)); - $this->assertSame($file1Size, \OC\Files\Filesystem::filesize($this->folder . $this->filename)); - $this->assertSame($file2Size, \OC\Files\Filesystem::filesize($this->filename)); + $this->assertSame($folderSize, Filesystem::filesize($this->folder)); + $this->assertSame($file1Size, Filesystem::filesize($this->folder . $this->filename)); + $this->assertSame($file2Size, Filesystem::filesize($this->filename)); //cleanup $this->shareManager->deleteShare($share1); $this->shareManager->deleteShare($share2); } - public function testGetPermissions() { + public function testGetPermissions(): void { $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ + Constants::PERMISSION_READ ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::is_dir($this->folder)); + $this->assertTrue(Filesystem::is_dir($this->folder)); // for the share root we expect: // the read permissions (1) // the delete permission (8), to enable unshare - $rootInfo = \OC\Files\Filesystem::getFileInfo($this->folder); + $rootInfo = Filesystem::getFileInfo($this->folder); $this->assertSame(9, $rootInfo->getPermissions()); // for the file within the shared folder we expect: // the read permissions (1) - $subfileInfo = \OC\Files\Filesystem::getFileInfo($this->folder . $this->filename); + $subfileInfo = Filesystem::getFileInfo($this->folder . $this->filename); $this->assertSame(1, $subfileInfo->getPermissions()); @@ -222,19 +213,19 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share); } - public function testFopenWithReadOnlyPermission() { + public function testFopenWithReadOnlyPermission(): void { $this->view->file_put_contents($this->folder . '/existing.txt', 'foo'); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ + Constants::PERMISSION_READ ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); // part file should be forbidden $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w'); @@ -254,20 +245,20 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share); } - public function testFopenWithCreateOnlyPermission() { + public function testFopenWithCreateOnlyPermission(): void { $this->view->file_put_contents($this->folder . '/existing.txt', 'foo'); $fileinfoFolder = $this->view->getFileInfo($this->folder); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE + Constants::PERMISSION_READ | Constants::PERMISSION_CREATE ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); // create part file allowed $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w'); @@ -283,7 +274,7 @@ class SharedStorageTest extends TestCase { $this->assertFalse($user2View->rename($this->folder . '/test-create.txt', $this->folder . '/newtarget.txt')); $this->assertFalse($user2View->file_exists($this->folder . '/newtarget.txt')); - // rename file not allowed if target exists + // rename file not allowed if target exists $this->assertFalse($user2View->rename($this->folder . '/newtarget.txt', $this->folder . '/existing.txt')); // overwriting file not allowed @@ -298,24 +289,22 @@ class SharedStorageTest extends TestCase { //cleanup self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - $result = \OC\Share\Share::unshare('folder', $fileinfoFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, - self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue($result); + $this->shareManager->deleteShare($share); } - public function testFopenWithUpdateOnlyPermission() { + public function testFopenWithUpdateOnlyPermission(): void { $this->view->file_put_contents($this->folder . '/existing.txt', 'foo'); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); // create part file allowed $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w'); @@ -349,19 +338,19 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share); } - public function testFopenWithDeleteOnlyPermission() { + public function testFopenWithDeleteOnlyPermission(): void { $this->view->file_put_contents($this->folder . '/existing.txt', 'foo'); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE + Constants::PERMISSION_READ | Constants::PERMISSION_DELETE ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $user2View = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); // part file should be forbidden $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w'); @@ -381,32 +370,32 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share); } - public function testMountSharesOtherUser() { - $rootView = new \OC\Files\View(''); + public function testMountSharesOtherUser(): void { + $rootView = new View(''); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); // share 2 different files with 2 different users $share1 = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); $share2 = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->filename, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER3, - \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder)); - $mountConfigManager = \OC::$server->getMountProviderCollection(); - $mounts = $mountConfigManager->getMountsForUser(\OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3)); - array_walk($mounts, array(\OC\Files\Filesystem::getMountManager(), 'addMount')); + $mountConfigManager = Server::get(IMountProviderCollection::class); + $mounts = $mountConfigManager->getMountsForUser(Server::get(IUserManager::class)->get(self::TEST_FILES_SHARING_API_USER3)); + array_walk($mounts, [Filesystem::getMountManager(), 'addMount']); $this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER3 . '/files/' . $this->filename)); @@ -422,28 +411,25 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share2); } - public function testCopyFromStorage() { + public function testCopyFromStorage(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($view->file_exists($this->folder)); - /** - * @var \OCP\Files\Storage $sharedStorage - */ - list($sharedStorage,) = $view->resolvePath($this->folder); + [$sharedStorage,] = $view->resolvePath($this->folder); $this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')); - $sourceStorage = new \OC\Files\Storage\Temporary(array()); + $sourceStorage = new Temporary([]); $sourceStorage->file_put_contents('foo.txt', 'asd'); $sharedStorage->copyFromStorage($sourceStorage, 'foo.txt', 'bar.txt'); @@ -455,29 +441,27 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share); } - public function testMoveFromStorage() { + public function testMoveFromStorage(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($view->file_exists($this->folder)); - /** - * @var \OCP\Files\Storage $sharedStorage - */ - list($sharedStorage,) = $view->resolvePath($this->folder); + [$sharedStorage,] = $view->resolvePath($this->folder); $this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')); - $sourceStorage = new \OC\Files\Storage\Temporary(array()); + $sourceStorage = new Temporary([]); $sourceStorage->file_put_contents('foo.txt', 'asd'); + $sourceStorage->getScanner()->scan(''); $sharedStorage->moveFromStorage($sourceStorage, 'foo.txt', 'bar.txt'); $this->assertTrue($sharedStorage->file_exists('bar.txt')); @@ -488,47 +472,49 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share); } - public function testNameConflict() { + public function testNameConflict(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); - $view1 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); + $view1 = new View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); $view1->mkdir('foo'); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); - $view3 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files'); + $view3 = new View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files'); $view3->mkdir('foo'); // share a folder with the same name from two different users to the same user self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $share1 = $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + IShare::TYPE_GROUP, 'foo', self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_GROUP1, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); + $this->shareManager->acceptShare($share1, self::TEST_FILES_SHARING_API_USER2); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); $share2 = $this->share( - \OCP\Share::SHARE_TYPE_GROUP, + IShare::TYPE_GROUP, 'foo', self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_GROUP1, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); + $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $view2 = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($view2->file_exists('/foo')); $this->assertTrue($view2->file_exists('/foo (2)')); $mount = $view2->getMount('/foo'); $this->assertInstanceOf('\OCA\Files_Sharing\SharedMount', $mount); - /** @var \OCA\Files_Sharing\SharedStorage $storage */ + /** @var SharedStorage $storage */ $storage = $mount->getStorage(); $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $storage->getOwner('')); @@ -537,19 +523,19 @@ class SharedStorageTest extends TestCase { $this->shareManager->deleteShare($share2); } - public function testOwnerPermissions() { + public function testOwnerPermissions(): void { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_DELETE + Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); - $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); $this->assertTrue($view->file_exists($this->folder)); $view->file_put_contents($this->folder . '/newfile.txt', 'asd'); @@ -557,15 +543,14 @@ class SharedStorageTest extends TestCase { self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $this->assertTrue($this->view->file_exists($this->folder . '/newfile.txt')); - $this->assertEquals(\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, + $this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $this->view->getFileInfo($this->folder . '/newfile.txt')->getPermissions()); $this->view->unlink($this->folder); $this->shareManager->deleteShare($share); - } - public function testInitWithNonExistingUser() { + public function testInitWithNonExistingUser(): void { $share = $this->createMock(IShare::class); $share->method('getShareOwner')->willReturn('unexist'); $ownerView = $this->createMock(View::class); @@ -577,15 +562,16 @@ class SharedStorageTest extends TestCase { ]); // trigger init - $this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage()); - $this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache()); + $this->assertInstanceOf(FailedStorage::class, $storage->getSourceStorage()); + $this->assertInstanceOf(FailedCache::class, $storage->getCache()); } - public function testInitWithNotFoundSource() { + public function testInitWithNotFoundSource(): void { $share = $this->createMock(IShare::class); $share->method('getShareOwner')->willReturn(self::TEST_FILES_SHARING_API_USER1); + $share->method('getNodeId')->willReturn(1); $ownerView = $this->createMock(View::class); - $ownerView->method('getPath')->will($this->throwException(new NotFoundException())); + $ownerView->method('getPath')->willThrowException(new NotFoundException()); $storage = new SharedStorage([ 'ownerView' => $ownerView, 'superShare' => $share, @@ -594,7 +580,33 @@ class SharedStorageTest extends TestCase { ]); // trigger init - $this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage()); - $this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache()); + $this->assertInstanceOf(FailedStorage::class, $storage->getSourceStorage()); + $this->assertInstanceOf(FailedCache::class, $storage->getCache()); + } + + public function testCopyPermissions(): void { + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + + $share = $this->share( + IShare::TYPE_USER, + $this->filename, + self::TEST_FILES_SHARING_API_USER1, + self::TEST_FILES_SHARING_API_USER2, + Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE - Constants::PERMISSION_DELETE + ); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + $view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $this->assertTrue($view->file_exists($this->filename)); + + $this->assertTrue($view->copy($this->filename, '/target.txt')); + + $this->assertTrue($view->file_exists('/target.txt')); + + $info = $view->getFileInfo('/target.txt'); + $this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $info->getPermissions()); + + $this->view->unlink($this->filename); + $this->shareManager->deleteShare($share); } } diff --git a/apps/files_sharing/tests/SharesReminderJobTest.php b/apps/files_sharing/tests/SharesReminderJobTest.php new file mode 100644 index 00000000000..ce468e279ec --- /dev/null +++ b/apps/files_sharing/tests/SharesReminderJobTest.php @@ -0,0 +1,193 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\Files_Sharing\Tests; + +use OC\SystemConfig; +use OCA\Files_Sharing\SharesReminderJob; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Constants; +use OCP\Defaults; +use OCP\Files\IMimeTypeLoader; +use OCP\Files\IRootFolder; +use OCP\IDBConnection; +use OCP\IURLGenerator; +use OCP\IUserManager; +use OCP\L10N\IFactory; +use OCP\Mail\IMailer; +use OCP\Mail\IMessage; +use OCP\Server; +use OCP\Share\IManager; +use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; + +/** + * Class SharesReminderJobTest + * + * @group DB + * + * @package OCA\Files_Sharing\Tests + */ +class SharesReminderJobTest extends \Test\TestCase { + private SharesReminderJob $job; + private IDBConnection $db; + private IManager $shareManager; + private IUserManager $userManager; + private IMailer|MockObject $mailer; + private string $user1; + private string $user2; + + protected function setUp(): void { + parent::setUp(); + + $this->db = Server::get(IDBConnection::class); + $this->shareManager = Server::get(IManager::class); + $this->userManager = Server::get(IUserManager::class); + $this->mailer = $this->createMock(IMailer::class); + + // Clear occasional leftover shares from other tests + $this->db->executeUpdate('DELETE FROM `*PREFIX*share`'); + + $this->user1 = $this->getUniqueID('user1_'); + $this->user2 = $this->getUniqueID('user2_'); + + $user1 = $this->userManager->createUser($this->user1, 'longrandompassword'); + $user2 = $this->userManager->createUser($this->user2, 'longrandompassword'); + $user1->setSystemEMailAddress('user1@test.com'); + $user2->setSystemEMailAddress('user2@test.com'); + + \OC::registerShareHooks(Server::get(SystemConfig::class)); + + $this->job = new SharesReminderJob( + Server::get(ITimeFactory::class), + $this->db, + Server::get(IManager::class), + $this->userManager, + Server::get(LoggerInterface::class), + Server::get(IURLGenerator::class), + Server::get(IFactory::class), + $this->mailer, + Server::get(Defaults::class), + Server::get(IMimeTypeLoader::class), + ); + } + + protected function tearDown(): void { + $this->db->executeUpdate('DELETE FROM `*PREFIX*share`'); + + $userManager = Server::get(IUserManager::class); + $user1 = $userManager->get($this->user1); + if ($user1) { + $user1->delete(); + } + $user2 = $userManager->get($this->user2); + if ($user2) { + $user2->delete(); + } + + $this->logout(); + + parent::tearDown(); + } + + public static function dataSharesReminder() { + $someMail = 'test@test.com'; + $noExpirationDate = null; + $today = new \DateTime(); + // For expiration dates, the time is always automatically set to zero by ShareAPIController + $today->setTime(0, 0); + $nearFuture = new \DateTime(); + $nearFuture->setTimestamp($today->getTimestamp() + 86400 * 1); + $farFuture = new \DateTime(); + $farFuture->setTimestamp($today->getTimestamp() + 86400 * 2); + $permissionRead = Constants::PERMISSION_READ; + $permissionCreate = $permissionRead | Constants::PERMISSION_CREATE; + $permissionUpdate = $permissionRead | Constants::PERMISSION_UPDATE; + $permissionDelete = $permissionRead | Constants::PERMISSION_DELETE; + $permissionAll = Constants::PERMISSION_ALL; + + return [ + // No reminders for folders without expiration date + [$noExpirationDate, '', false, $permissionRead, false], + [$noExpirationDate, '', false, $permissionCreate, false], + [$noExpirationDate, '', true, $permissionDelete, false], + [$noExpirationDate, '', true, $permissionCreate, false], + [$noExpirationDate, $someMail, false, $permissionUpdate, false], + [$noExpirationDate, $someMail, false, $permissionCreate, false], + [$noExpirationDate, $someMail, true, $permissionRead, false], + [$noExpirationDate, $someMail, true, $permissionAll, false], + // No reminders for folders with expiration date in the far future + [$farFuture, '', false, $permissionRead, false], + [$farFuture, '', false, $permissionCreate, false], + [$farFuture, '', true, $permissionDelete, false], + [$farFuture, '', true, $permissionCreate, false], + [$farFuture, $someMail, false, $permissionUpdate, false], + [$farFuture, $someMail, false, $permissionCreate, false], + [$farFuture, $someMail, true, $permissionRead, false], + [$farFuture, $someMail, true, $permissionAll, false], + /* Should send reminders for folders with expiration date in the near future + if the folder is empty and the user has write permission */ + [$nearFuture, '', false, $permissionRead, false], + [$nearFuture, '', false, $permissionCreate, false], + [$nearFuture, '', true, $permissionDelete, false], + [$nearFuture, '', true, $permissionCreate, true], + [$nearFuture, $someMail, false, $permissionUpdate, false], + [$nearFuture, $someMail, false, $permissionCreate, false], + [$nearFuture, $someMail, true, $permissionRead, false], + [$nearFuture, $someMail, true, $permissionAll, true], + ]; + } + + /** + * + * @param \DateTime|null $expirationDate Share expiration date + * @param string $email Share with this email. If empty, the share is of type TYPE_USER and the sharee is user2 + * @param bool $isEmpty Is share folder empty? + * @param int $permissions + * @param bool $shouldBeReminded + */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataSharesReminder')] + public function testSharesReminder( + ?\DateTime $expirationDate, string $email, bool $isEmpty, int $permissions, bool $shouldBeReminded, + ): void { + $this->loginAsUser($this->user1); + + $user1Folder = Server::get(IRootFolder::class)->getUserFolder($this->user1); + $testFolder = $user1Folder->newFolder('test'); + + if (!$isEmpty) { + $testFolder->newFile('some_file.txt', 'content'); + } + + $share = $this->shareManager->newShare(); + + $share->setNode($testFolder) + ->setShareType(($email ? IShare::TYPE_EMAIL : IShare::TYPE_USER)) + ->setPermissions($permissions) + ->setSharedBy($this->user1) + ->setSharedWith(($email ?: $this->user2)) + ->setExpirationDate($expirationDate); + $share = $this->shareManager->createShare($share); + + $this->logout(); + $messageMock = $this->createMock(IMessage::class); + $this->mailer->method('createMessage')->willReturn($messageMock); + $this->mailer + ->expects(($shouldBeReminded ? $this->once() : $this->never())) + ->method('send') + ->with($messageMock); + $messageMock + ->expects(($shouldBeReminded ? $this->once() : $this->never())) + ->method('setTo') + ->with([$email ?: $this->userManager->get($this->user2)->getSystemEMailAddress()]); + $this->assertSame(false, $share->getReminderSent()); + $this->job->run([]); + $share = $this->shareManager->getShareById($share->getFullId()); + $this->assertEquals($shouldBeReminded, $share->getReminderSent()); + } +} diff --git a/apps/files_sharing/tests/SizePropagationTest.php b/apps/files_sharing/tests/SizePropagationTest.php index f7ad9057959..e1b67abca90 100644 --- a/apps/files_sharing/tests/SizePropagationTest.php +++ b/apps/files_sharing/tests/SizePropagationTest.php @@ -1,31 +1,17 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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 OCA\Files_Sharing\Tests; use OC\Files\View; +use OCP\Constants; +use OCP\ITempManager; +use OCP\Server; +use OCP\Share\IShare; use Test\Traits\UserTrait; /** @@ -40,13 +26,13 @@ class SizePropagationTest extends TestCase { protected function setupUser($name, $password = '') { $this->createUser($name, $password); - $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder(); + $tmpFolder = Server::get(ITempManager::class)->getTemporaryFolder(); $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]); $this->loginAsUser($name); return new View('/' . $name . '/files'); } - public function testSizePropagationWhenOwnerChangesFile() { + public function testSizePropagationWhenOwnerChangesFile(): void { $recipientView = $this->setupUser(self::TEST_FILES_SHARING_API_USER1); $ownerView = $this->setupUser(self::TEST_FILES_SHARING_API_USER2); @@ -54,11 +40,11 @@ class SizePropagationTest extends TestCase { $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'bar'); $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, '/sharedfolder', self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER1, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); $ownerRootInfo = $ownerView->getFileInfo('', false); @@ -81,7 +67,7 @@ class SizePropagationTest extends TestCase { $this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize()); } - public function testSizePropagationWhenRecipientChangesFile() { + public function testSizePropagationWhenRecipientChangesFile(): void { $recipientView = $this->setupUser(self::TEST_FILES_SHARING_API_USER1); $ownerView = $this->setupUser(self::TEST_FILES_SHARING_API_USER2); @@ -89,11 +75,11 @@ class SizePropagationTest extends TestCase { $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'bar'); $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, '/sharedfolder', self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER1, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); $ownerRootInfo = $ownerView->getFileInfo('', false); @@ -112,7 +98,7 @@ class SizePropagationTest extends TestCase { // but the size including mountpoints increases $newRecipientRootInfo = $recipientView->getFileInfo('', true); - $this->assertEquals($oldRecipientSize +3, $newRecipientRootInfo->getSize()); + $this->assertEquals($oldRecipientSize + 3, $newRecipientRootInfo->getSize()); // size of owner's root increases $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); diff --git a/apps/files_sharing/tests/TestCase.php b/apps/files_sharing/tests/TestCase.php index 3b1ccb71a94..9a6935e46b6 100644 --- a/apps/files_sharing/tests/TestCase.php +++ b/apps/files_sharing/tests/TestCase.php @@ -1,39 +1,29 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; -use OC\Files\Cache\Scanner; +use OC\Files\Cache\Storage; use OC\Files\Filesystem; +use OC\Files\View; +use OC\Group\Database; +use OC\SystemConfig; +use OC\User\DisplayNameCache; use OCA\Files_Sharing\AppInfo\Application; +use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider; +use OCA\Files_Sharing\MountProvider; +use OCP\Files\Config\IMountProviderCollection; +use OCP\Files\IRootFolder; +use OCP\IDBConnection; +use OCP\IGroupManager; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\Server; +use OCP\Share\IShare; use Test\Traits\MountProviderTrait; /** @@ -46,44 +36,52 @@ use Test\Traits\MountProviderTrait; abstract class TestCase extends \Test\TestCase { use MountProviderTrait; - const TEST_FILES_SHARING_API_USER1 = "test-share-user1"; - const TEST_FILES_SHARING_API_USER2 = "test-share-user2"; - const TEST_FILES_SHARING_API_USER3 = "test-share-user3"; - const TEST_FILES_SHARING_API_USER4 = "test-share-user4"; + public const TEST_FILES_SHARING_API_USER1 = 'test-share-user1'; + public const TEST_FILES_SHARING_API_USER2 = 'test-share-user2'; + public const TEST_FILES_SHARING_API_USER3 = 'test-share-user3'; + public const TEST_FILES_SHARING_API_USER4 = 'test-share-user4'; - const TEST_FILES_SHARING_API_GROUP1 = "test-share-group1"; + public const TEST_FILES_SHARING_API_GROUP1 = 'test-share-group1'; public $filename; public $data; /** - * @var \OC\Files\View + * @var View */ public $view; + /** + * @var View + */ + public $view2; public $folder; public $subfolder; /** @var \OCP\Share\IManager */ protected $shareManager; - /** @var \OCP\Files\IRootFolder */ + /** @var IRootFolder */ protected $rootFolder; - public static function setUpBeforeClass() { + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - $application = new Application(); - $application->registerMountProviders(); - + $app = new Application(); + $app->registerMountProviders( + Server::get(IMountProviderCollection::class), + Server::get(MountProvider::class), + Server::get(ExternalMountProvider::class), + ); + // reset backend - \OC_User::clearBackends(); - \OC::$server->getGroupManager()->clearBackends(); + Server::get(IUserManager::class)->clearBackends(); + Server::get(IGroupManager::class)->clearBackends(); // clear share hooks \OC_Hook::clear('OCP\\Share'); - \OC::registerShareHooks(); + \OC::registerShareHooks(Server::get(SystemConfig::class)); // create users $backend = new \Test\Util\User\Dummy(); - \OC_User::useBackend($backend); + Server::get(IUserManager::class)->registerBackend($backend); $backend->createUser(self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER1); $backend->createUser(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER2); $backend->createUser(self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_USER3); @@ -103,40 +101,57 @@ abstract class TestCase extends \Test\TestCase { $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group2'); $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER4, 'group3'); $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_GROUP1); - \OC::$server->getGroupManager()->addBackend($groupBackend); + Server::get(IGroupManager::class)->addBackend($groupBackend); } - protected function setUp() { + protected function setUp(): void { parent::setUp(); + Server::get(DisplayNameCache::class)->clear(); //login as user1 - self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); $this->data = 'foobar'; - $this->view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); + $this->view = new View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); + $this->view2 = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); - $this->shareManager = \OC::$server->getShareManager(); - $this->rootFolder = \OC::$server->getRootFolder(); + $this->shareManager = Server::get(\OCP\Share\IManager::class); + $this->rootFolder = Server::get(IRootFolder::class); } - protected function tearDown() { - $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share`'); - $query->execute(); + protected function tearDown(): void { + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); + $qb->delete('share'); + $qb->execute(); + + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); + $qb->delete('mounts'); + $qb->execute(); + + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); + $qb->delete('filecache')->runAcrossAllShards(); + $qb->execute(); parent::tearDown(); } - public static function tearDownAfterClass() { + public static function tearDownAfterClass(): void { // cleanup users - $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER1); - if ($user !== null) { $user->delete(); } - $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER2); - if ($user !== null) { $user->delete(); } - $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3); - if ($user !== null) { $user->delete(); } + $user = Server::get(IUserManager::class)->get(self::TEST_FILES_SHARING_API_USER1); + if ($user !== null) { + $user->delete(); + } + $user = Server::get(IUserManager::class)->get(self::TEST_FILES_SHARING_API_USER2); + if ($user !== null) { + $user->delete(); + } + $user = Server::get(IUserManager::class)->get(self::TEST_FILES_SHARING_API_USER3); + if ($user !== null) { + $user->delete(); + } // delete group - $group = \OC::$server->getGroupManager()->get(self::TEST_FILES_SHARING_API_GROUP1); + $group = Server::get(IGroupManager::class)->get(self::TEST_FILES_SHARING_API_GROUP1); if ($group) { $group->delete(); } @@ -146,10 +161,10 @@ abstract class TestCase extends \Test\TestCase { Filesystem::tearDown(); // reset backend - \OC_User::clearBackends(); - \OC_User::useBackend('database'); - \OC::$server->getGroupManager()->clearBackends(); - \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database()); + Server::get(IUserManager::class)->clearBackends(); + Server::get(IUserManager::class)->registerBackend(new \OC\User\Database()); + Server::get(IGroupManager::class)->clearBackends(); + Server::get(IGroupManager::class)->addBackend(new Database()); parent::tearDownAfterClass(); } @@ -159,66 +174,50 @@ abstract class TestCase extends \Test\TestCase { * @param bool $create * @param bool $password */ - protected static function loginHelper($user, $create = false, $password = false) { - + protected function loginHelper($user, $create = false, $password = false) { if ($password === false) { $password = $user; } if ($create) { - $userManager = \OC::$server->getUserManager(); - $groupManager = \OC::$server->getGroupManager(); + $userManager = Server::get(IUserManager::class); + $groupManager = Server::get(IGroupManager::class); $userObject = $userManager->createUser($user, $password); $group = $groupManager->createGroup('group'); - if ($group and $userObject) { + if ($group && $userObject) { $group->addUser($userObject); } } - self::resetStorage(); - \OC_Util::tearDownFS(); - \OC\Files\Cache\Storage::getGlobalCache()->clearCache(); - \OC::$server->getUserSession()->setUser(null); - \OC\Files\Filesystem::tearDown(); - \OC::$server->getUserSession()->login($user, $password); + Storage::getGlobalCache()->clearCache(); + Server::get(IUserSession::class)->setUser(null); + Filesystem::tearDown(); + Server::get(IUserSession::class)->login($user, $password); \OC::$server->getUserFolder($user); \OC_Util::setupFS($user); } /** - * reset init status for the share storage - */ - protected static function resetStorage() { - $storage = new \ReflectionClass('\OCA\Files_Sharing\SharedStorage'); - $isInitialized = $storage->getProperty('initialized'); - $isInitialized->setAccessible(true); - $isInitialized->setValue($storage, false); - $isInitialized->setAccessible(false); - } - - /** * get some information from a given share * @param int $shareID * @return array with: item_source, share_type, share_with, item_type, permissions */ protected function getShareFromId($shareID) { - $sql = 'SELECT `item_source`, `share_type`, `share_with`, `item_type`, `permissions` FROM `*PREFIX*share` WHERE `id` = ?'; - $args = array($shareID); - $query = \OCP\DB::prepare($sql); - $result = $query->execute($args); - - $share = Null; - - if ($result) { - $share = $result->fetchRow(); - } + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); + $qb->select('item_source', '`share_type', 'share_with', 'item_type', 'permissions') + ->from('share') + ->where( + $qb->expr()->eq('id', $qb->createNamedParameter($shareID)) + ); + $result = $qb->execute(); + $share = $result->fetch(); + $result->closeCursor(); return $share; - } /** @@ -227,7 +226,7 @@ abstract class TestCase extends \Test\TestCase { * @param string $initiator * @param string $recipient * @param int $permissions - * @return \OCP\Share\IShare + * @return IShare */ protected function share($type, $path, $initiator, $recipient, $permissions) { $userFolder = $this->rootFolder->getUserFolder($initiator); @@ -240,6 +239,8 @@ abstract class TestCase extends \Test\TestCase { ->setNode($node) ->setPermissions($permissions); $share = $this->shareManager->createShare($share); + $share->setStatus(IShare::STATUS_ACCEPTED); + $share = $this->shareManager->updateShare($share); return $share; } diff --git a/apps/files_sharing/tests/UnshareChildrenTest.php b/apps/files_sharing/tests/UnshareChildrenTest.php index ce7767d1920..ac870212c99 100644 --- a/apps/files_sharing/tests/UnshareChildrenTest.php +++ b/apps/files_sharing/tests/UnshareChildrenTest.php @@ -1,32 +1,17 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; +use OC\Files\Filesystem; +use OCP\Constants; +use OCP\Share\IShare; +use OCP\Util; + /** * Class UnshareChildrenTest * @@ -35,17 +20,16 @@ namespace OCA\Files_Sharing\Tests; * @package OCA\Files_Sharing\Tests */ class UnshareChildrenTest extends TestCase { - protected $subsubfolder; - const TEST_FOLDER_NAME = '/folder_share_api_test'; + public const TEST_FOLDER_NAME = '/folder_share_api_test'; private static $tempStorage; - protected function setUp() { + protected function setUp(): void { parent::setUp(); - \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren'); + Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren'); $this->folder = self::TEST_FOLDER_NAME; $this->subfolder = '/subfolder_share_api_test'; @@ -61,7 +45,7 @@ class UnshareChildrenTest extends TestCase { $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data); } - protected function tearDown() { + protected function tearDown(): void { if ($this->view) { $this->view->deleteAll($this->folder); } @@ -74,40 +58,39 @@ class UnshareChildrenTest extends TestCase { /** * @medium */ - public function testUnshareChildren() { - - $fileInfo2 = \OC\Files\Filesystem::getFileInfo($this->folder); + public function testUnshareChildren(): void { + $fileInfo2 = Filesystem::getFileInfo($this->folder); $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // one folder should be shared with the user - $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_USER); + $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_USER); $this->assertCount(1, $shares); // move shared folder to 'localDir' - \OC\Files\Filesystem::mkdir('localDir'); - $result = \OC\Files\Filesystem::rename($this->folder, '/localDir/' . $this->folder); + Filesystem::mkdir('localDir'); + $result = Filesystem::rename($this->folder, '/localDir/' . $this->folder); $this->assertTrue($result); - \OC\Files\Filesystem::unlink('localDir'); + Filesystem::unlink('localDir'); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // after the parent directory was deleted the share should be unshared - $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_USER); + $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, IShare::TYPE_USER); $this->assertEmpty($shares); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); // the folder for the owner should still exists - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder)); + $this->assertTrue(Filesystem::file_exists($this->folder)); } } diff --git a/apps/files_sharing/tests/UpdaterTest.php b/apps/files_sharing/tests/UpdaterTest.php index c8d089f54b4..23044e0b2f3 100644 --- a/apps/files_sharing/tests/UpdaterTest.php +++ b/apps/files_sharing/tests/UpdaterTest.php @@ -1,49 +1,38 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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 OCA\Files_Sharing\Tests; +use OC\Files\FileInfo; +use OC\Files\Filesystem; +use OC\Files\View; +use OCA\Files_Sharing\Helper; +use OCA\Files_Trashbin\AppInfo\Application; +use OCP\App\IAppManager; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\Constants; +use OCP\IConfig; +use OCP\Server; +use OCP\Share\IShare; + /** * Class UpdaterTest * * @group DB */ class UpdaterTest extends TestCase { + public const TEST_FOLDER_NAME = '/folder_share_updater_test'; - const TEST_FOLDER_NAME = '/folder_share_updater_test'; - - public static function setUpBeforeClass() { + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - \OCA\Files_Sharing\Helper::registerHooks(); + Helper::registerHooks(); } - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->folder = self::TEST_FOLDER_NAME; @@ -56,7 +45,7 @@ class UpdaterTest extends TestCase { $this->view->file_put_contents($this->folder . '/' . $this->filename, $this->data); } - protected function tearDown() { + protected function tearDown(): void { if ($this->view) { $this->view->unlink($this->filename); $this->view->deleteAll($this->folder); @@ -70,32 +59,34 @@ class UpdaterTest extends TestCase { * points should be unshared before the folder gets deleted so * that the mount point doesn't end up at the trash bin */ - public function testDeleteParentFolder() { - $status = \OC::$server->getAppManager()->isEnabledForUser('files_trashbin'); - (new \OC_App())->enable('files_trashbin'); + public function testDeleteParentFolder(): void { + $appManager = Server::get(IAppManager::class); + $status = $appManager->isEnabledForUser('files_trashbin'); + $appManager->enableApp('files_trashbin'); + // register trashbin hooks + $trashbinApp = new Application(); + $trashbinApp->boot($this->createMock(IBootContext::class)); - \OCA\Files_Trashbin\Trashbin::registerHooks(); - - $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder); - $this->assertTrue($fileinfo instanceof \OC\Files\FileInfo); + $fileinfo = Filesystem::getFileInfo($this->folder); + $this->assertTrue($fileinfo instanceof FileInfo); $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); - $view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + $view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); // check if user2 can see the shared folder $this->assertTrue($view->file_exists($this->folder)); - $foldersShared = \OC\Share\Share::getItemsSharedWith('folder'); - $this->assertSame(1, count($foldersShared)); + $foldersShared = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER); + $this->assertCount(1, $foldersShared); $view->mkdir('localFolder'); $view->file_put_contents('localFolder/localFile.txt', 'local file'); @@ -104,18 +95,18 @@ class UpdaterTest extends TestCase { // share mount point should now be moved to the subfolder $this->assertFalse($view->file_exists($this->folder)); - $this->assertTrue($view->file_exists('localFolder/' .$this->folder)); + $this->assertTrue($view->file_exists('localFolder/' . $this->folder)); $view->unlink('localFolder'); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); // shared folder should be unshared - $foldersShared = \OC\Share\Share::getItemsSharedWith('folder'); - $this->assertTrue(empty($foldersShared)); + $foldersShared = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER); + $this->assertCount(0, $foldersShared); // trashbin should contain the local file but not the mount point - $rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2); + $rootView = new View('/' . self::TEST_FILES_SHARING_API_USER2); $trashContent = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_FILES_SHARING_API_USER2); $this->assertSame(1, count($trashContent)); $firstElement = reset($trashContent); @@ -127,13 +118,13 @@ class UpdaterTest extends TestCase { $rootView->deleteAll('files_trashin'); if ($status === false) { - \OC_App::disable('files_trashbin'); + $appManager->disableApp('files_trashbin'); } - \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); + Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); } - public function shareFolderProvider() { + public static function shareFolderProvider() { return [ ['/'], ['/my_shares'], @@ -143,41 +134,41 @@ class UpdaterTest extends TestCase { /** * if a file gets shared the etag for the recipients root should change * - * @dataProvider shareFolderProvider * * @param string $shareFolder share folder to use */ - public function testShareFile($shareFolder) { - $config = \OC::$server->getConfig(); + #[\PHPUnit\Framework\Attributes\DataProvider('shareFolderProvider')] + public function testShareFile($shareFolder): void { + $config = Server::get(IConfig::class); $oldShareFolder = $config->getSystemValue('share_folder'); $config->setSystemValue('share_folder', $shareFolder); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); - $beforeShareRoot = \OC\Files\Filesystem::getFileInfo(''); + $beforeShareRoot = Filesystem::getFileInfo(''); $etagBeforeShareRoot = $beforeShareRoot->getEtag(); - \OC\Files\Filesystem::mkdir($shareFolder); + Filesystem::mkdir($shareFolder); - $beforeShareDir = \OC\Files\Filesystem::getFileInfo($shareFolder); + $beforeShareDir = Filesystem::getFileInfo($shareFolder); $etagBeforeShareDir = $beforeShareDir->getEtag(); $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); - $afterShareRoot = \OC\Files\Filesystem::getFileInfo(''); + $afterShareRoot = Filesystem::getFileInfo(''); $etagAfterShareRoot = $afterShareRoot->getEtag(); - $afterShareDir = \OC\Files\Filesystem::getFileInfo($shareFolder); + $afterShareDir = Filesystem::getFileInfo($shareFolder); $etagAfterShareDir = $afterShareDir->getEtag(); $this->assertTrue(is_string($etagBeforeShareRoot)); @@ -197,41 +188,158 @@ class UpdaterTest extends TestCase { /** * if a folder gets renamed all children mount points should be renamed too */ - public function testRename() { - - $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder); + public function testRename(): void { + $fileinfo = Filesystem::getFileInfo($this->folder); $share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); // make sure that the shared folder exists - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder)); + $this->assertTrue(Filesystem::file_exists($this->folder)); - \OC\Files\Filesystem::mkdir('oldTarget'); - \OC\Files\Filesystem::mkdir('oldTarget/subfolder'); - \OC\Files\Filesystem::mkdir('newTarget'); + Filesystem::mkdir('oldTarget'); + Filesystem::mkdir('oldTarget/subfolder'); + Filesystem::mkdir('newTarget'); - \OC\Files\Filesystem::rename($this->folder, 'oldTarget/subfolder/' . $this->folder); + Filesystem::rename($this->folder, 'oldTarget/subfolder/' . $this->folder); // re-login to make sure that the new mount points are initialized $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); - \OC\Files\Filesystem::rename('/oldTarget', '/newTarget/oldTarget'); + Filesystem::rename('/oldTarget', '/newTarget/oldTarget'); // re-login to make sure that the new mount points are initialized $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); - $this->assertTrue(\OC\Files\Filesystem::file_exists('/newTarget/oldTarget/subfolder/' . $this->folder)); + $this->assertTrue(Filesystem::file_exists('/newTarget/oldTarget/subfolder/' . $this->folder)); // cleanup $this->shareManager->deleteShare($share); } + /** + * If a folder gets moved into shared folder, children shares should have their uid_owner and permissions adjusted + * user1 + * |-folder1 --> shared with user2 + * user2 + * |-folder2 --> shared with user3 and moved into folder1 + * |-subfolder1 --> shared with user3 + * |-file1.txt --> shared with user3 + * |-subfolder2 + * |-file2.txt --> shared with user3 + */ + public function testMovedIntoShareChangeOwner(): void { + $this->markTestSkipped('Skipped because this is failing with S3 as primary as file id are change when moved.'); + + // user1 creates folder1 + $viewUser1 = new View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files'); + $folder1 = 'folder1'; + $viewUser1->mkdir($folder1); + + // user1 shares folder1 to user2 + $folder1Share = $this->share( + IShare::TYPE_USER, + $folder1, + self::TEST_FILES_SHARING_API_USER1, + self::TEST_FILES_SHARING_API_USER2, + Constants::PERMISSION_READ | Constants::PERMISSION_SHARE + ); + + $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); + $viewUser2 = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); + // Create user2 files + $folder2 = 'folder2'; + $viewUser2->mkdir($folder2); + $file1 = 'folder2/file1.txt'; + $viewUser2->touch($file1); + $subfolder1 = 'folder2/subfolder1'; + $viewUser2->mkdir($subfolder1); + $subfolder2 = 'folder2/subfolder2'; + $viewUser2->mkdir($subfolder2); + $file2 = 'folder2/subfolder2/file2.txt'; + $viewUser2->touch($file2); + + // user2 shares folder2 to user3 + $folder2Share = $this->share( + IShare::TYPE_USER, + $folder2, + self::TEST_FILES_SHARING_API_USER2, + self::TEST_FILES_SHARING_API_USER3, + Constants::PERMISSION_ALL + ); + // user2 shares folder2/file1 to user3 + $file1Share = $this->share( + IShare::TYPE_USER, + $file1, + self::TEST_FILES_SHARING_API_USER2, + self::TEST_FILES_SHARING_API_USER3, + Constants::PERMISSION_READ | Constants::PERMISSION_SHARE + ); + // user2 shares subfolder1 to user3 + $subfolder1Share = $this->share( + IShare::TYPE_USER, + $subfolder1, + self::TEST_FILES_SHARING_API_USER2, + self::TEST_FILES_SHARING_API_USER3, + Constants::PERMISSION_ALL + ); + // user2 shares subfolder2/file2.txt to user3 + $file2Share = $this->share( + IShare::TYPE_USER, + $file2, + self::TEST_FILES_SHARING_API_USER2, + self::TEST_FILES_SHARING_API_USER3, + Constants::PERMISSION_READ | Constants::PERMISSION_SHARE + ); + + // user2 moves folder2 into folder1 + $viewUser2->rename($folder2, $folder1 . '/' . $folder2); + $folder2Share = $this->shareManager->getShareById($folder2Share->getFullId()); + $file1Share = $this->shareManager->getShareById($file1Share->getFullId()); + $subfolder1Share = $this->shareManager->getShareById($subfolder1Share->getFullId()); + $file2Share = $this->shareManager->getShareById($file2Share->getFullId()); + + // Expect uid_owner of both shares to be user1 + $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $folder2Share->getShareOwner()); + $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $file1Share->getShareOwner()); + $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $subfolder1Share->getShareOwner()); + $this->assertEquals(self::TEST_FILES_SHARING_API_USER1, $file2Share->getShareOwner()); + // Expect permissions to be limited by the permissions of the destination share + $this->assertEquals(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, $folder2Share->getPermissions()); + $this->assertEquals(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, $file1Share->getPermissions()); + $this->assertEquals(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, $subfolder1Share->getPermissions()); + $this->assertEquals(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, $file2Share->getPermissions()); + + // user2 moves folder2 out of folder1 + $viewUser2->rename($folder1 . '/' . $folder2, $folder2); + $folder2Share = $this->shareManager->getShareById($folder2Share->getFullId()); + $file1Share = $this->shareManager->getShareById($file1Share->getFullId()); + $subfolder1Share = $this->shareManager->getShareById($subfolder1Share->getFullId()); + $file2Share = $this->shareManager->getShareById($file2Share->getFullId()); + + // Expect uid_owner of both shares to be user2 + $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $folder2Share->getShareOwner()); + $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $file1Share->getShareOwner()); + $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $subfolder1Share->getShareOwner()); + $this->assertEquals(self::TEST_FILES_SHARING_API_USER2, $file2Share->getShareOwner()); + // Expect permissions to not change + $this->assertEquals(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, $folder2Share->getPermissions()); + $this->assertEquals(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, $file1Share->getPermissions()); + $this->assertEquals(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, $subfolder1Share->getPermissions()); + $this->assertEquals(Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, $file2Share->getPermissions()); + + // cleanup + $this->shareManager->deleteShare($folder1Share); + $this->shareManager->deleteShare($folder2Share); + $this->shareManager->deleteShare($file1Share); + $this->shareManager->deleteShare($subfolder1Share); + $this->shareManager->deleteShare($file2Share); + } } diff --git a/apps/files_sharing/tests/WatcherTest.php b/apps/files_sharing/tests/WatcherTest.php index e6ca5f40a8f..15676836915 100644 --- a/apps/files_sharing/tests/WatcherTest.php +++ b/apps/files_sharing/tests/WatcherTest.php @@ -1,34 +1,18 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing\Tests; +use OC\Files\Cache\Cache; +use OC\Files\Storage\Storage; +use OC\Files\View; +use OCP\Constants; +use OCP\Share\IShare; + /** * Class WatcherTest * @@ -36,22 +20,22 @@ namespace OCA\Files_Sharing\Tests; */ class WatcherTest extends TestCase { - /** @var \OC\Files\Storage\Storage */ + /** @var Storage */ private $ownerStorage; - /** @var \OC\Files\Cache\Cache */ + /** @var Cache */ private $ownerCache; - /** @var \OC\Files\Storage\Storage */ + /** @var Storage */ private $sharedStorage; - /** @var \OC\Files\Cache\Cache */ + /** @var Cache */ private $sharedCache; - /** @var \OCP\Share\IShare */ + /** @var IShare */ private $_share; - protected function setUp() { + protected function setUp(): void { parent::setUp(); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); @@ -61,29 +45,32 @@ class WatcherTest extends TestCase { $this->view->mkdir('container/shareddir'); $this->view->mkdir('container/shareddir/subdir'); - list($this->ownerStorage, $internalPath) = $this->view->resolvePath(''); + [$this->ownerStorage, $internalPath] = $this->view->resolvePath(''); $this->ownerCache = $this->ownerStorage->getCache(); $this->ownerStorage->getScanner()->scan(''); // share "shareddir" with user2 $this->_share = $this->share( - \OCP\Share::SHARE_TYPE_USER, + IShare::TYPE_USER, 'container/shareddir', self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, - \OCP\Constants::PERMISSION_ALL + Constants::PERMISSION_ALL ); + $this->_share->setStatus(IShare::STATUS_ACCEPTED); + $this->shareManager->updateShare($this->_share); + // login as user2 self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // retrieve the shared storage - $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2); - list($this->sharedStorage, $internalPath) = $secondView->resolvePath('files/shareddir'); + $secondView = new View('/' . self::TEST_FILES_SHARING_API_USER2); + [$this->sharedStorage, $internalPath] = $secondView->resolvePath('files/shareddir'); $this->sharedCache = $this->sharedStorage->getCache(); } - protected function tearDown() { + protected function tearDown(): void { if ($this->sharedCache) { $this->sharedCache->clear(); } @@ -105,14 +92,14 @@ class WatcherTest extends TestCase { * Tests that writing a file using the shared storage will propagate the file * size to the owner's parent folders. */ - function testFolderSizePropagationToOwnerStorage() { + public function testFolderSizePropagationToOwnerStorage(): void { $initialSizes = self::getOwnerDirSizes('files/container/shareddir'); $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $dataLen = strlen($textData); - $this->sharedCache->put('bar.txt', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain')); + $this->sharedCache->put('bar.txt', ['mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain']); $this->sharedStorage->file_put_contents('bar.txt', $textData); - $this->sharedCache->put('', array('mtime' => 10, 'storage_mtime' => 10, 'size' => '-1', 'mimetype' => 'httpd/unix-directory')); + $this->sharedCache->put('', ['mtime' => 10, 'storage_mtime' => 10, 'size' => '-1', 'mimetype' => 'httpd/unix-directory']); // run the propagation code $this->sharedStorage->getWatcher()->checkUpdate(''); @@ -135,14 +122,14 @@ class WatcherTest extends TestCase { * Tests that writing a file using the shared storage will propagate the file * size to the owner's parent folders. */ - function testSubFolderSizePropagationToOwnerStorage() { + public function testSubFolderSizePropagationToOwnerStorage(): void { $initialSizes = self::getOwnerDirSizes('files/container/shareddir/subdir'); $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $dataLen = strlen($textData); - $this->sharedCache->put('subdir/bar.txt', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain')); + $this->sharedCache->put('subdir/bar.txt', ['mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain']); $this->sharedStorage->file_put_contents('subdir/bar.txt', $textData); - $this->sharedCache->put('subdir', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain')); + $this->sharedCache->put('subdir', ['mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain']); // run the propagation code $this->sharedStorage->getWatcher()->checkUpdate('subdir'); @@ -167,8 +154,8 @@ class WatcherTest extends TestCase { * where the key is the path and the value is the size. * @param string $path */ - function getOwnerDirSizes($path) { - $result = array(); + public function getOwnerDirSizes($path) { + $result = []; while ($path != '' && $path != '' && $path != '.') { $cachedData = $this->ownerCache->get($path); diff --git a/apps/files_sharing/tests/js/appSpec.js b/apps/files_sharing/tests/js/appSpec.js deleted file mode 100644 index 133bd44f750..00000000000 --- a/apps/files_sharing/tests/js/appSpec.js +++ /dev/null @@ -1,148 +0,0 @@ -/** -* ownCloud -* -* @author Vincent Petry -* @copyright 2014 Vincent Petry <pvince81@owncloud.com> -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE -* License as published by the Free Software Foundation; either -* version 3 of the License, or any later version. -* -* This library 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 library. If not, see <http://www.gnu.org/licenses/>. -* -*/ - -describe('OCA.Sharing.App tests', function() { - var App = OCA.Sharing.App; - var fileListIn; - var fileListOut; - - beforeEach(function() { - $('#testArea').append( - '<div id="app-navigation">' + - '<ul><li data-id="files"><a>Files</a></li>' + - '<li data-id="sharingin"><a></a></li>' + - '<li data-id="sharingout"><a></a></li>' + - '</ul></div>' + - '<div id="app-content">' + - '<div id="app-content-files" class="hidden">' + - '</div>' + - '<div id="app-content-sharingin" class="hidden">' + - '</div>' + - '<div id="app-content-sharingout" class="hidden">' + - '</div>' + - '</div>' + - '</div>' - ); - fileListIn = App.initSharingIn($('#app-content-sharingin')); - fileListOut = App.initSharingOut($('#app-content-sharingout')); - }); - afterEach(function() { - App.destroy(); - }); - - describe('initialization', function() { - it('inits sharing-in list on show', function() { - expect(fileListIn._sharedWithUser).toEqual(true); - }); - it('inits sharing-out list on show', function() { - expect(fileListOut._sharedWithUser).toBeFalsy(); - }); - }); - describe('file actions', function() { - var oldLegacyFileActions; - - beforeEach(function() { - oldLegacyFileActions = window.FileActions; - window.FileActions = new OCA.Files.FileActions(); - }); - - afterEach(function() { - window.FileActions = oldLegacyFileActions; - }); - it('provides default file actions', function() { - _.each([fileListIn, fileListOut], function(fileList) { - var fileActions = fileList.fileActions; - - expect(fileActions.actions.all).toBeDefined(); - expect(fileActions.actions.all.Delete).toBeDefined(); - expect(fileActions.actions.all.Rename).toBeDefined(); - expect(fileActions.actions.all.Download).toBeDefined(); - - expect(fileActions.defaults.dir).toEqual('Open'); - }); - }); - it('provides custom file actions', function() { - var actionStub = sinon.stub(); - // regular file action - OCA.Files.fileActions.register( - 'all', - 'RegularTest', - OC.PERMISSION_READ, - OC.imagePath('core', 'actions/shared'), - actionStub - ); - - App._inFileList = null; - fileListIn = App.initSharingIn($('#app-content-sharingin')); - - expect(fileListIn.fileActions.actions.all.RegularTest).toBeDefined(); - }); - it('does not provide legacy file actions', function() { - var actionStub = sinon.stub(); - // legacy file action - window.FileActions.register( - 'all', - 'LegacyTest', - OC.PERMISSION_READ, - OC.imagePath('core', 'actions/shared'), - actionStub - ); - - App._inFileList = null; - fileListIn = App.initSharingIn($('#app-content-sharingin')); - - expect(fileListIn.fileActions.actions.all.LegacyTest).not.toBeDefined(); - }); - it('redirects to files app when opening a directory', function() { - var oldList = OCA.Files.App.fileList; - // dummy new list to make sure it exists - OCA.Files.App.fileList = new OCA.Files.FileList($('<table><thead></thead><tbody></tbody></table>')); - - var setActiveViewStub = sinon.stub(OCA.Files.App, 'setActiveView'); - // create dummy table so we can click the dom - var $table = '<table><thead></thead><tbody id="fileList"></tbody></table>'; - $('#app-content-sharingin').append($table); - - App._inFileList = null; - fileListIn = App.initSharingIn($('#app-content-sharingin')); - - fileListIn.setFiles([{ - name: 'testdir', - type: 'dir', - path: '/somewhere/inside/subdir', - counterParts: ['user2'], - shareOwner: 'user2' - }]); - - fileListIn.findFileEl('testdir').find('td .nametext').click(); - - expect(OCA.Files.App.fileList.getCurrentDirectory()).toEqual('/somewhere/inside/subdir/testdir'); - - expect(setActiveViewStub.calledOnce).toEqual(true); - expect(setActiveViewStub.calledWith('files')).toEqual(true); - - setActiveViewStub.restore(); - - // restore old list - OCA.Files.App.fileList = oldList; - }); - }); -}); diff --git a/apps/files_sharing/tests/js/fileDropSpec.js b/apps/files_sharing/tests/js/fileDropSpec.js deleted file mode 100644 index 22bb95878b4..00000000000 --- a/apps/files_sharing/tests/js/fileDropSpec.js +++ /dev/null @@ -1,98 +0,0 @@ -/** - * - * @copyright Copyright (c) 2017, Artur Neumann (info@individual-it.net) - * - * @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/>. - * - */ - -describe("files Drop tests", function() { - //some testing data - var sharingToken = "fVCiSMhScgWfiuv"; - var testFiles = [ - { name: 'test.txt', expectedValidationResult: true }, - { name: 'testनेपाल.txt', expectedValidationResult: true }, - { name: 'test.part', expectedValidationResult: false }, - { name: 'test.filepart', expectedValidationResult: false }, - { name: '.', expectedValidationResult: false }, - { name: '..', expectedValidationResult: false }, - ]; - - //this pre/post positions should not change the result of the file name validation - var prePostPositions = [""," "," "," "]; - - //use the testFiles and the pre/post positions to generate more testing data - var replicatedTestFiles = []; - prePostPositions.map(function (prePostPosition) { - testFiles.map(function (testFile) { - replicatedTestFiles.push( - { - name: testFile.name + prePostPosition, - expectedValidationResult: testFile.expectedValidationResult - } - ); - replicatedTestFiles.push( - { - name: prePostPosition + testFile.name, - expectedValidationResult: testFile.expectedValidationResult - } - ); - replicatedTestFiles.push( - { - name: prePostPosition + testFile.name + prePostPosition, - expectedValidationResult: testFile.expectedValidationResult - } - ); - }); - }); - - beforeEach (function () { - //fake input for the sharing token - $('#testArea').append( - '<input name="sharingToken" value="" id="sharingToken" type="hidden">' - ); - }); - - - replicatedTestFiles.map(function (testFile) { - it("validates the filenames correctly", function() { - data = { - 'submit': function() {}, - 'files': [testFile] - } - expect(OCA.FilesSharingDrop.addFileToUpload('',data)). - toBe( - testFile.expectedValidationResult, - 'wrongly validated file named "'+testFile.name+'"' - ); - }); - - if (testFile.expectedValidationResult === true) { - it("should set correct PUT URL, Auth header and submit", function () { - data = { - 'submit': sinon.stub(), - 'files': [testFile] - } - $('#sharingToken').val(sharingToken); - - OCA.FilesSharingDrop.addFileToUpload('',data); - expect(data.submit.calledOnce).toEqual(true); - expect(data.url).toContain("/public.php/webdav/" + encodeURI(testFile.name)); - expect(data.headers['Authorization']).toEqual('Basic ' + btoa(sharingToken+":")); - }); - } - }); -}); diff --git a/apps/files_sharing/tests/js/publicAppSpec.js b/apps/files_sharing/tests/js/publicAppSpec.js deleted file mode 100644 index 8ea339fd9de..00000000000 --- a/apps/files_sharing/tests/js/publicAppSpec.js +++ /dev/null @@ -1,149 +0,0 @@ -/** -* ownCloud -* -* @author Vincent Petry -* @copyright 2015 Vincent Petry <pvince81@owncloud.com> -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE -* License as published by the Free Software Foundation; either -* version 3 of the License, or any later version. -* -* This library 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 library. If not, see <http://www.gnu.org/licenses/>. -* -*/ - -describe('OCA.Sharing.PublicApp tests', function() { - var App = OCA.Sharing.PublicApp; - var hostStub, protocolStub, webrootStub; - var $preview; - - beforeEach(function() { - protocolStub = sinon.stub(OC, 'getProtocol').returns('https'); - hostStub = sinon.stub(OC, 'getHost').returns('example.com:9876'); - webrootStub = sinon.stub(OC, 'getRootPath').returns('/owncloud'); - $preview = $('<div id="preview"></div>'); - $('#testArea').append($preview); - $preview.append( - '<div id="mimetype"></div>' + - '<div id="mimetypeIcon"></div>' + - '<input type="hidden" id="sharingToken" value="sh4tok"></input>' - ); - }); - - afterEach(function() { - protocolStub.restore(); - hostStub.restore(); - webrootStub.restore(); - }); - - describe('File list', function() { - // TODO: this should be moved to a separate file once the PublicFileList is extracted from public.js - beforeEach(function() { - $preview.append( - '<div id="app-content-files">' + - // init horrible parameters - '<input type="hidden" id="dir" value="/subdir"/>' + - '<input type="hidden" id="permissions" value="31"/>' + - // dummy controls - '<div id="controls">' + - ' <div class="actions creatable"></div>' + - ' <div class="notCreatable"></div>' + - '</div>' + - // uploader - '<input type="file" id="file_upload_start" name="files[]" multiple="multiple">' + - // dummy table - // TODO: at some point this will be rendered by the fileList class itself! - '<table id="filestable">' + - '<thead><tr>' + - '<th id="headerName" class="hidden column-name">' + - '<input type="checkbox" id="select_all_files" class="select-all">' + - '<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' + - '<span class="selectedActions hidden">' + - '<a href class="download">Download</a>' + - '</th>' + - '<th class="hidden column-size"><a class="columntitle" data-sort="size"><span class="sort-indicator"></span></a></th>' + - '<th class="hidden column-mtime"><a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a></th>' + - '</tr></thead>' + - '<tbody id="fileList"></tbody>' + - '<tfoot></tfoot>' + - '</table>' + - // TODO: move to handlebars template - '<div id="emptycontent"><h2>Empty content message</h2><p class="uploadmessage">Upload message</p></div>' + - '<div class="nofilterresults hidden"></div>' + - '</div>' - ); - - App.initialize($('#preview')); - }); - afterEach(function() { - App._initialized = false; - }); - - it('Uses public webdav endpoint', function() { - App._initialized = false; - fakeServer.restore(); - window.fakeServer = sinon.fakeServer.create(); - - // uploader function messes up with fakeServer - var uploaderDetectStub = sinon.stub(OC.Uploader.prototype, '_supportAjaxUploadWithProgress'); - App.initialize($('#preview')); - expect(fakeServer.requests.length).toEqual(1); - expect(fakeServer.requests[0].method).toEqual('PROPFIND'); - expect(fakeServer.requests[0].url).toEqual('https://example.com:9876/owncloud/public.php/webdav/subdir'); - expect(fakeServer.requests[0].requestHeaders.Authorization).toEqual('Basic c2g0dG9rOm51bGw='); - uploaderDetectStub.restore(); - }); - - describe('Download Url', function() { - var fileList; - - beforeEach(function() { - fileList = App.fileList; - }); - - it('returns correct download URL for single files', function() { - expect(fileList.getDownloadUrl('some file.txt')) - .toEqual(OC.webroot + '/index.php/s/sh4tok/download?path=%2Fsubdir&files=some%20file.txt'); - expect(fileList.getDownloadUrl('some file.txt', '/anotherpath/abc')) - .toEqual(OC.webroot + '/index.php/s/sh4tok/download?path=%2Fanotherpath%2Fabc&files=some%20file.txt'); - fileList.changeDirectory('/'); - expect(fileList.getDownloadUrl('some file.txt')) - .toEqual(OC.webroot + '/index.php/s/sh4tok/download?path=%2F&files=some%20file.txt'); - }); - it('returns correct download URL for multiple files', function() { - expect(fileList.getDownloadUrl(['a b c.txt', 'd e f.txt'])) - .toEqual(OC.webroot + '/index.php/s/sh4tok/download?path=%2Fsubdir&files=%5B%22a%20b%20c.txt%22%2C%22d%20e%20f.txt%22%5D'); - }); - it('returns the correct ajax URL', function() { - expect(fileList.getAjaxUrl('test', {a:1, b:'x y'})) - .toEqual(OC.webroot + '/index.php/apps/files_sharing/ajax/test.php?a=1&b=x%20y&t=sh4tok'); - }); - it('returns correct download URL for downloading everything', function() { - expect(fileList.getDownloadUrl()) - .toEqual(OC.webroot + '/index.php/s/sh4tok/download?path=%2Fsubdir'); - }); - }); - describe('Upload Url', function() { - var fileList; - - beforeEach(function() { - fileList = App.fileList; - }); - it('returns correct upload URL', function() { - expect(fileList.getUploadUrl('some file.txt')) - .toEqual('/owncloud/public.php/webdav/subdir/some%20file.txt'); - }); - it('returns correct upload URL with specified dir', function() { - expect(fileList.getUploadUrl('some file.txt', 'sub')) - .toEqual('/owncloud/public.php/webdav/sub/some%20file.txt'); - }); - }); - }); -}); diff --git a/apps/files_sharing/tests/js/shareSpec.js b/apps/files_sharing/tests/js/shareSpec.js deleted file mode 100644 index 91060f6a735..00000000000 --- a/apps/files_sharing/tests/js/shareSpec.js +++ /dev/null @@ -1,512 +0,0 @@ -/* - * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com> - * - * This file is licensed under the Affero General Public License version 3 - * or later. - * - * See the COPYING-README file. - * - */ - -describe('OCA.Sharing.Util tests', function() { - var fileList; - var testFiles; - - function getImageUrl($el) { - // might be slightly different cross-browser - var url = $el.css('background-image'); - var r = url.match(/url\(['"]?([^'")]*)['"]?\)/); - if (!r) { - return url; - } - return r[1]; - } - - beforeEach(function() { - var $content = $('<div id="content"></div>'); - $('#testArea').append($content); - // dummy file list - var $div = $( - '<div id="listContainer">' + - '<table id="filestable">' + - '<thead></thead>' + - '<tbody id="fileList"></tbody>' + - '</table>' + - '</div>'); - $('#content').append($div); - - var fileActions = new OCA.Files.FileActions(); - fileList = new OCA.Files.FileList( - $div, { - fileActions : fileActions - } - ); - OCA.Sharing.Util.attach(fileList); - - testFiles = [{ - id: 1, - type: 'file', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc', - shareOwner: 'User One', - isShareMountPoint: false, - shareTypes: [OC.Share.SHARE_TYPE_USER] - }]; - }); - afterEach(function() { - delete OCA.Sharing.sharesLoaded; - delete OC.Share.droppedDown; - fileList.destroy(); - fileList = null; - }); - - describe('Sharing data in table row', function() { - // TODO: test data-permissions, data-share-owner, etc - }); - describe('Share action icon', function() { - it('do not shows share text when not shared', function() { - var $action, $tr; - OC.Share.statuses = {}; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One', - path: '/subdir', - mimetype: 'httpd/unix-directory', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc', - shareTypes: [] - }]); - $tr = fileList.$el.find('tbody tr:first'); - $action = $tr.find('.action-share'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(true); - expect($action.find('.icon').hasClass('icon-public')).toEqual(false); - expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder.svg'); - }); - it('shows simple share text with share icon', function() { - var $action, $tr; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc', - shareTypes: [OC.Share.SHARE_TYPE_USER] - }]); - $tr = fileList.$el.find('tbody tr:first'); - $action = $tr.find('.action-share'); - expect($action.find('>span').text().trim()).toEqual('Shared'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(true); - expect($action.find('.icon').hasClass('icon-public')).toEqual(false); - expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg'); - }); - it('shows simple share text with public icon when shared with link', function() { - var $action, $tr; - OC.Share.statuses = {1: {link: true, path: '/subdir'}}; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc', - shareTypes: [OC.Share.SHARE_TYPE_LINK] - }]); - $tr = fileList.$el.find('tbody tr:first'); - $action = $tr.find('.action-share'); - expect($action.find('>span').text().trim()).toEqual('Shared'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(false); - expect($action.find('.icon').hasClass('icon-public')).toEqual(true); - expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-public.svg'); - }); - it('shows owner name when owner is available', function() { - var $action, $tr; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - shareOwner: 'User One', - shareOwnerId: 'User One', - etag: 'abc', - shareTypes: [] - }]); - $tr = fileList.$el.find('tbody tr:first'); - $action = $tr.find('.action-share'); - expect($action.find('>span').text().trim()).toEqual('Shared by User One'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(true); - expect($action.find('.icon').hasClass('icon-public')).toEqual(false); - expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg'); - }); - it('shows recipients when recipients are available', function() { - var $action, $tr; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - recipientsDisplayName: 'User One, User Two', - recipientData: { - 0: { - shareWith: 'User One', - shareWithDisplayName: 'User One' - }, - 1: { - shareWith: 'User Two', - shareWithDisplayName: 'User Two' - } - }, - etag: 'abc', - shareTypes: [OC.Share.SHARE_TYPE_USER] - }]); - $tr = fileList.$el.find('tbody tr:first'); - $action = $tr.find('.action-share'); - expect($action.text().trim()).toEqual('Shared with User One Shared with User Two'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(true); - expect($action.find('.icon').hasClass('icon-public')).toEqual(false); - expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg'); - }); - it('shows share action when shared with user who has no share permission', function() { - var $action, $tr; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_CREATE, - etag: 'abc', - shareOwner: 'User One' - }]); - $tr = fileList.$el.find('tbody tr:first'); - expect($tr.find('.action-share').length).toEqual(1); - }); - it('do not show share action when share exists but neither permission nor owner is available', function() { - var $action, $tr; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_CREATE, - etag: 'abc' - }]); - $tr = fileList.$el.find('tbody tr:first'); - expect($tr.find('.action-share').length).toEqual(0); - }); - }); - describe('Share action', function() { - var shareTab; - - function makeDummyShareItem(displayName) { - return { - share_with_displayname: displayName - }; - } - - beforeEach(function() { - // make it look like not the "All files" list - fileList.id = 'test'; - shareTab = fileList._detailsView._tabViews[0]; - }); - afterEach(function() { - shareTab = null; - }); - it('clicking share action opens sidebar and share tab', function() { - var showDetailsViewStub = sinon.stub(fileList, 'showDetailsView'); - - fileList.setFiles([{ - id: 1, - type: 'file', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc' - }]); - - var $tr = fileList.$el.find('tr:first'); - $tr.find('.action-share').click(); - - expect(showDetailsViewStub.calledOnce).toEqual(true); - expect(showDetailsViewStub.getCall(0).args[0]).toEqual('One.txt'); - expect(showDetailsViewStub.getCall(0).args[1]).toEqual('shareTabView'); - - showDetailsViewStub.restore(); - }); - it('adds share icon after sharing a non-shared file', function() { - var $action, $tr; - OC.Share.statuses = {}; - fileList.setFiles([{ - id: 1, - type: 'file', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc' - }]); - $action = fileList.$el.find('tbody tr:first .action-share'); - $tr = fileList.$el.find('tr:first'); - - $tr.find('.action-share').click(); - - // simulate updating shares - shareTab._dialog.model.set({ - shares: [ - {share_with_displayname: 'User One', share_with: 'User One'}, - {share_with_displayname: 'User Two', share_with: 'User Two'}, - {share_with_displayname: 'Group One', share_with: 'Group One'}, - {share_with_displayname: 'Group Two', share_with: 'Group Two'} - ] - }); - - expect($action.text().trim()).toEqual('Shared with Group One Shared with Group Two Shared with User One Shared with User Two'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(true); - expect($action.find('.icon').hasClass('icon-public')).toEqual(false); - }); - it('updates share icon after updating shares of a file', function() { - var $action, $tr; - OC.Share.statuses = {1: {link: false, path: '/subdir'}}; - fileList.setFiles([{ - id: 1, - type: 'file', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc' - }]); - $action = fileList.$el.find('tbody tr:first .action-share'); - $tr = fileList.$el.find('tr:first'); - - $tr.find('.action-share').click(); - - // simulate updating shares - shareTab._dialog.model.set({ - shares: [ - {share_with_displayname: 'User One', share_with: 'User One'}, - {share_with_displayname: 'User Two', share_with: 'User Two'}, - {share_with_displayname: 'User Three', share_with: 'User Three'} - ] - }); - - expect($action.text().trim()).toEqual('Shared with User One Shared with User Three Shared with User Two'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(true); - expect($action.find('.icon').hasClass('icon-public')).toEqual(false); - }); - it('removes share icon after removing all shares from a file', function() { - var $action, $tr; - OC.Share.statuses = {1: {link: false, path: '/subdir'}}; - fileList.setFiles([{ - id: 1, - type: 'file', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc', - recipients: 'User One, User Two' - }]); - $action = fileList.$el.find('tbody tr:first .action-share'); - $tr = fileList.$el.find('tr:first'); - - $tr.find('.action-share').click(); - - // simulate updating shares - shareTab._dialog.model.set({ - shares: [] - }); - - expect($tr.attr('data-share-recipient-data')).not.toBeDefined(); - }); - it('keep share text after updating reshare', function() { - var $action, $tr; - OC.Share.statuses = {1: {link: false, path: '/subdir'}}; - fileList.setFiles([{ - id: 1, - type: 'file', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc', - shareOwner: 'User One', - shareOwnerId: 'User One' - }]); - $action = fileList.$el.find('tbody tr:first .action-share'); - $tr = fileList.$el.find('tr:first'); - - $tr.find('.action-share').click(); - - // simulate updating shares - shareTab._dialog.model.set({ - shares: [{share_with_displayname: 'User Two'}] - }); - - expect($action.find('>span').text().trim()).toEqual('Shared by User One'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(true); - expect($action.find('.icon').hasClass('icon-public')).toEqual(false); - }); - it('keep share text after unsharing reshare', function() { - var $action, $tr; - OC.Share.statuses = {1: {link: false, path: '/subdir'}}; - fileList.setFiles([{ - id: 1, - type: 'file', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_ALL, - etag: 'abc', - shareOwner: 'User One', - shareOwnerId: 'User One', - recipients: 'User Two', - recipientData: {'User Two': 'User Two'} - }]); - $action = fileList.$el.find('tbody tr:first .action-share'); - $tr = fileList.$el.find('tr:first'); - - $tr.find('.action-share').click(); - - // simulate updating shares - shareTab._dialog.model.set({ - shares: [] - }); - - expect($tr.attr('data-share-recipient-data')).not.toBeDefined(); - - expect($action.find('>span').text().trim()).toEqual('Shared by User One'); - expect($action.find('.icon').hasClass('icon-shared')).toEqual(true); - expect($action.find('.icon').hasClass('icon-public')).toEqual(false); - }); - }); - describe('Excluded lists', function() { - function createListThenAttach(listId) { - var fileActions = new OCA.Files.FileActions(); - fileList.destroy(); - fileList = new OCA.Files.FileList( - $('#listContainer'), { - id: listId, - fileActions: fileActions - } - ); - OCA.Sharing.Util.attach(fileList); - fileList.setFiles(testFiles); - return fileList; - } - - it('does not attach to trashbin or public file lists', function() { - createListThenAttach('trashbin'); - expect($('.action-share').length).toEqual(0); - expect($('[data-share-recipient]').length).toEqual(0); - createListThenAttach('files.public'); - expect($('.action-share').length).toEqual(0); - expect($('[data-share-recipient]').length).toEqual(0); - }); - }); - - describe('ShareTabView interaction', function() { - var shareTabSpy; - var fileInfoModel; - var configModel; - var shareModel; - - beforeEach(function() { - shareTabSpy = sinon.spy(OCA.Sharing, 'ShareTabView'); - - var attributes = { - itemType: 'file', - itemSource: 123, - possiblePermissions: 31, - permissions: 31 - }; - fileInfoModel = new OCA.Files.FileInfoModel(testFiles[0]); - configModel = new OC.Share.ShareConfigModel({ - enforcePasswordForPublicLink: false, - isResharingAllowed: true, - isDefaultExpireDateEnabled: false, - isDefaultExpireDateEnforced: false, - defaultExpireDate: 7 - }); - shareModel = new OC.Share.ShareItemModel(attributes, { - configModel: configModel, - fileInfoModel: fileInfoModel - }); - - /* jshint camelcase: false */ - shareModel.set({ - reshare: {}, - shares: [{ - id: 100, - item_source: 1, - permissions: 31, - share_type: OC.Share.SHARE_TYPE_USER, - share_with: 'user1', - share_with_displayname: 'User One' - }, { - id: 102, - item_source: 1, - permissions: 31, - share_type: OC.Share.SHARE_TYPE_REMOTE, - share_with: 'foo@bar.com/baz', - share_with_displayname: 'foo@bar.com/baz' - - }] - }, {parse: true}); - - fileList.destroy(); - fileList = new OCA.Files.FileList( - $('#listContainer'), { - id: 'files', - fileActions: new OCA.Files.FileActions() - } - ); - OCA.Sharing.Util.attach(fileList); - fileList.setFiles(testFiles); - }); - afterEach(function() { - shareTabSpy.restore(); - }); - - it('updates fileInfoModel when shares changed', function() { - var changeHandler = sinon.stub(); - fileInfoModel.on('change', changeHandler); - - shareTabSpy.getCall(0).returnValue.trigger('sharesChanged', shareModel); - - expect(changeHandler.calledOnce).toEqual(true); - expect(changeHandler.getCall(0).args[0].changed).toEqual({ - shareTypes: [ - OC.Share.SHARE_TYPE_USER, - OC.Share.SHARE_TYPE_REMOTE - ] - }); - }); - }); -}); diff --git a/apps/files_sharing/tests/js/sharedbreadcrumviewSpec.js b/apps/files_sharing/tests/js/sharedbreadcrumviewSpec.js deleted file mode 100644 index c58e038c4ab..00000000000 --- a/apps/files_sharing/tests/js/sharedbreadcrumviewSpec.js +++ /dev/null @@ -1,240 +0,0 @@ -/** - * @copyright 2016, 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/>. - * - */ - -describe('OCA.Sharing.ShareBreadCrumbView tests', function() { - var BreadCrumb = OCA.Files.BreadCrumb; - var SharedBreadCrum = OCA.Sharing.ShareBreadCrumbView; - - describe('Rendering', function() { - var bc; - var sbc; - var shareTab; - beforeEach(function() { - bc = new BreadCrumb({ - getCrumbUrl: function(part, index) { - // for testing purposes - return part.dir + '#' + index; - } - }); - shareTab = new OCA.Sharing.ShareTabView(); - sbc = new SharedBreadCrum({ - shareTab: shareTab - }); - bc.addDetailView(sbc); - }); - afterEach(function() { - bc = null; - sbc = null; - shareModel = null; - }); - it('Do not render in root', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/', - type: 'dir', - name: '' - }); - bc.setDirectoryInfo(dirInfo); - bc.setDirectory(''); - bc.render(); - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(0); - expect(bc.$el.find('.shared').length).toEqual(0); - expect(bc.$el.find('.icon-public').length).toEqual(0); - }); - it('Render in dir', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir' - }); - bc.setDirectoryInfo(dirInfo); - bc.setDirectory('/foo'); - bc.render(); - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(1); - expect(bc.$el.find('.shared').length).toEqual(0); - expect(bc.$el.find('.icon-public').length).toEqual(0); - }); - it('Render shared if dir is shared with user', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir', - shareTypes: [OC.Share.SHARE_TYPE_USER] - }); - bc.setDirectoryInfo(dirInfo); - bc.setDirectory('/foo'); - bc.render(); - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(1); - expect(bc.$el.find('.shared').length).toEqual(1); - expect(bc.$el.find('.icon-public').length).toEqual(0); - }); - it('Render shared if dir is shared with group', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir', - shareTypes: [OC.Share.SHARE_TYPE_GROUP] - }); - bc.setDirectoryInfo(dirInfo); - bc.setDirectory('/foo'); - bc.render(); - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(1); - expect(bc.$el.find('.shared').length).toEqual(1); - expect(bc.$el.find('.icon-public').length).toEqual(0); - }); - it('Render shared if dir is shared by link', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir', - shareTypes: [OC.Share.SHARE_TYPE_LINK] - }); - bc.setDirectoryInfo(dirInfo); - bc.setDirectory('/foo'); - bc.render(); - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(0); - expect(bc.$el.find('.shared').length).toEqual(1); - expect(bc.$el.find('.icon-public').length).toEqual(1); - }); - it('Render shared if dir is shared by circle', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir', - shareTypes: [OC.Share.SHARE_TYPE_CIRCLE] - }); - bc.setDirectoryInfo(dirInfo); - bc.setDirectory('/foo'); - bc.render(); - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(1); - expect(bc.$el.find('.shared').length).toEqual(1); - expect(bc.$el.find('.icon-public').length).toEqual(0); - }); - it('Render shared if dir is shared with remote', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir', - shareTypes: [OC.Share.SHARE_TYPE_REMOTE] - }); - bc.setDirectoryInfo(dirInfo); - bc.setDirectory('/foo'); - bc.render(); - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(1); - expect(bc.$el.find('.shared').length).toEqual(1); - expect(bc.$el.find('.icon-public').length).toEqual(0); - }); - it('Render link shared if at least one is a link share', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir', - shareTypes: [ - OC.Share.SHARE_TYPE_USER, - OC.Share.SHARE_TYPE_GROUP, - OC.Share.SHARE_TYPE_LINK, - OC.Share.SHARE_TYPE_EMAIL, - OC.Share.SHARE_TYPE_REMOTE, - OC.Share.SHARE_TYPE_CIRCLE - ] - }); - bc.setDirectoryInfo(dirInfo); - bc.setDirectory('/foo'); - bc.render(); - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(0); - expect(bc.$el.find('.shared').length).toEqual(1); - expect(bc.$el.find('.icon-public').length).toEqual(1); - }); - it('Remove shared status from user share', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir', - shareTypes: [OC.Share.SHARE_TYPE_USER] - }); - - bc.setDirectory('/foo'); - bc.setDirectoryInfo(dirInfo); - bc.render(); - - var mock = sinon.createStubInstance(OCA.Files.FileList); - mock.showDetailsView = function() { }; - OCA.Files.App.fileList = mock; - var spy = sinon.spy(mock, 'showDetailsView'); - bc.$el.find('.icon-shared').click(); - - expect(spy.calledOnce).toEqual(true); - - var model = sinon.createStubInstance(OC.Share.ShareItemModel); - model.getSharesWithCurrentItem = function() { return [] }; - model.hasLinkShare = function() { return false; }; - - shareTab.trigger('sharesChanged', model); - - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(1); - expect(bc.$el.find('.shared').length).toEqual(0); - expect(bc.$el.find('.icon-public').length).toEqual(0); - }); - it('Add link share to user share', function() { - var dirInfo = new OC.Files.FileInfo({ - id: 42, - path: '/foo', - type: 'dir', - shareTypes: [OC.Share.SHARE_TYPE_USER] - }); - - bc.setDirectory('/foo'); - bc.setDirectoryInfo(dirInfo); - bc.render(); - - var mock = sinon.createStubInstance(OCA.Files.FileList); - mock.showDetailsView = function() { }; - OCA.Files.App.fileList = mock; - var spy = sinon.spy(mock, 'showDetailsView'); - bc.$el.find('.icon-shared').click(); - - expect(spy.calledOnce).toEqual(true); - - var model = sinon.createStubInstance(OC.Share.ShareItemModel); - model.getSharesWithCurrentItem = function() { return [ - {share_type: OC.Share.SHARE_TYPE_USER} - ] }; - model.hasLinkShare = function() { return true; }; - - shareTab.trigger('sharesChanged', model); - - expect(bc.$el.hasClass('breadcrumb')).toEqual(true); - expect(bc.$el.find('.icon-shared').length).toEqual(0); - expect(bc.$el.find('.shared').length).toEqual(1); - expect(bc.$el.find('.icon-public').length).toEqual(1); - }); - }); -}); diff --git a/apps/files_sharing/tests/js/sharedfilelistSpec.js b/apps/files_sharing/tests/js/sharedfilelistSpec.js deleted file mode 100644 index 903234947bd..00000000000 --- a/apps/files_sharing/tests/js/sharedfilelistSpec.js +++ /dev/null @@ -1,779 +0,0 @@ -/* - * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com> - * - * This file is licensed under the Affero General Public License version 3 - * or later. - * - * See the COPYING-README file. - * - */ - -describe('OCA.Sharing.FileList tests', function() { - var testFiles, alertStub, notificationStub, fileList; - - beforeEach(function() { - alertStub = sinon.stub(OC.dialogs, 'alert'); - notificationStub = sinon.stub(OC.Notification, 'show'); - - // init parameters and test table elements - $('#testArea').append( - '<div id="app-content-container">' + - // init horrible parameters - '<input type="hidden" id="dir" value="/"></input>' + - '<input type="hidden" id="permissions" value="31"></input>' + - // dummy controls - '<div id="controls">' + - ' <div class="actions creatable"></div>' + - ' <div class="notCreatable"></div>' + - '</div>' + - // dummy table - // TODO: at some point this will be rendered by the fileList class itself! - '<table id="filestable">' + - '<thead><tr>' + - '<th id="headerName" class="hidden column-name">' + - '<input type="checkbox" id="select_all_files" class="select-all">' + - '<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' + - '<span class="selectedActions hidden">' + - '</th>' + - '<th class="hidden column-mtime">' + - '<a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a>' + - '</th>' + - '<th class="column-expiration">' + - '<a class="columntitle"><span>Expiration date</span></a>' + - '</th>' + - '</tr></thead>' + - '<tbody id="fileList"></tbody>' + - '<tfoot></tfoot>' + - '</table>' + - '<div id="emptycontent">Empty content message</div>' + - '</div>' - ); - - OC.Plugins.register('OCA.Files.FileList', OCA.Files.TagsPlugin); - }); - afterEach(function() { - testFiles = undefined; - fileList.destroy(); - fileList = undefined; - - notificationStub.restore(); - alertStub.restore(); - }); - - describe('loading file list for incoming shares', function() { - var ocsResponse; - var ocsResponseRemote; - - beforeEach(function() { - fileList = new OCA.Sharing.FileList( - $('#app-content-container'), { - sharedWithUser: true - } - ); - OCA.Sharing.Util.attach(fileList); - - fileList.reload(); - - /* jshint camelcase: false */ - ocsResponse = { - ocs: { - meta: { - status: 'ok', - statuscode: 100, - message: null - }, - data: [{ - id: 7, - item_type: 'file', - item_source: 49, - item_target: '/49', - file_source: 49, - file_target: '/local path/local name.txt', - path: 'files/something shared.txt', - permissions: 31, - stime: 11111, - share_type: OC.Share.SHARE_TYPE_USER, - share_with: 'user1', - share_with_displayname: 'User One', - tags: [OC.TAG_FAVORITE], - mimetype: 'text/plain', - uid_owner: 'user2', - displayname_owner: 'User Two' - }] - } - }; - - /* jshint camelcase: false */ - ocsResponseRemote = { - ocs: { - meta: { - status: 'ok', - statuscode: 100, - message: null - }, - data: [{ - id: 8, - remote: 'https://foo.bar/', - remote_id: 42, - share_token: 'abc', - name: '/a.txt', - owner: 'user3', - user: 'user1', - mountpoint: '/b.txt', - mountpoint_hash: 'def', - accepted: 1, - mimetype: 'text/plain', - mtime: 22222, - permissions: 31, - type: 'file', - file_id: 1337 - }] - } - }; - - }); - it('render file shares', function() { - expect(fakeServer.requests.length).toEqual(2); - expect(fakeServer.requests[0].url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'shares?format=json&shared_with_me=true&include_tags=true' - ); - - expect(fakeServer.requests[1].url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'remote_shares?format=json&include_tags=true' - ); - - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponse) - ); - - fakeServer.requests[1].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponseRemote) - ); - - var $rows = fileList.$el.find('tbody tr'); - expect($rows.length).toEqual(2); - - var $tr = $rows.eq(0); - expect($tr.attr('data-id')).toEqual('49'); - expect($tr.attr('data-type')).toEqual('file'); - expect($tr.attr('data-file')).toEqual('local name.txt'); - expect($tr.attr('data-path')).toEqual('/local path'); - expect($tr.attr('data-size')).not.toBeDefined(); - expect($tr.attr('data-permissions')).toEqual('31'); // read and delete - expect($tr.attr('data-mime')).toEqual('text/plain'); - expect($tr.attr('data-mtime')).toEqual('11111000'); - expect($tr.attr('data-share-owner')).toEqual('User Two'); - expect($tr.attr('data-share-id')).toEqual('7'); - expect($tr.attr('data-favorite')).toEqual('true'); - expect($tr.attr('data-tags')).toEqual(OC.TAG_FAVORITE); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + - '/remote.php/webdav/local%20path/local%20name.txt' - ); - expect($tr.find('.nametext').text().trim()).toEqual('local name.txt'); - - $tr = $rows.eq(1); - expect($tr.attr('data-id')).toEqual('1337'); - expect($tr.attr('data-type')).toEqual('file'); - expect($tr.attr('data-file')).toEqual('b.txt'); - expect($tr.attr('data-path')).toEqual(''); - expect($tr.attr('data-size')).not.toBeDefined(); - expect(parseInt($tr.attr('data-permissions'), 10)) - .toEqual(OC.PERMISSION_ALL); // read and delete - expect($tr.attr('data-mime')).toEqual('text/plain'); - expect($tr.attr('data-mtime')).toEqual('22222000'); - expect($tr.attr('data-share-owner')).toEqual('user3@foo.bar/'); - expect($tr.attr('data-share-id')).toEqual('8'); - expect($tr.attr('data-favorite')).not.toBeDefined(); - expect($tr.attr('data-tags')).toEqual(''); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + - '/remote.php/webdav/b.txt' - ); - expect($tr.find('.nametext').text().trim()).toEqual('b.txt'); - }); - it('render folder shares', function() { - /* jshint camelcase: false */ - ocsResponse.ocs.data[0] = _.extend(ocsResponse.ocs.data[0], { - item_type: 'folder', - file_target: '/local path/local name', - path: 'files/something shared', - }); - - ocsResponseRemote.ocs.data[0] = _.extend(ocsResponseRemote.ocs.data[0], { - type: 'dir', - mimetype: 'httpd/unix-directory', - name: '/a', - mountpoint: '/b' - }); - - expect(fakeServer.requests.length).toEqual(2); - expect(fakeServer.requests[0].url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'shares?format=json&shared_with_me=true&include_tags=true' - ); - expect(fakeServer.requests[1].url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'remote_shares?format=json&include_tags=true' - ); - - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponse) - ); - fakeServer.requests[1].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponseRemote) - ); - - var $rows = fileList.$el.find('tbody tr'); - expect($rows.length).toEqual(2); - - var $tr = $rows.eq(0); - expect($tr.attr('data-id')).toEqual('49'); - expect($tr.attr('data-type')).toEqual('dir'); - expect($tr.attr('data-file')).toEqual('local name'); - expect($tr.attr('data-path')).toEqual('/local path'); - expect($tr.attr('data-size')).not.toBeDefined(); - expect($tr.attr('data-permissions')).toEqual('31'); // read and delete - expect($tr.attr('data-mime')).toEqual('httpd/unix-directory'); - expect($tr.attr('data-mtime')).toEqual('11111000'); - expect($tr.attr('data-share-owner')).toEqual('User Two'); - expect($tr.attr('data-share-id')).toEqual('7'); - expect($tr.attr('data-favorite')).toEqual('true'); - expect($tr.attr('data-tags')).toEqual(OC.TAG_FAVORITE); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + - '/index.php/apps/files' + - '?dir=/local%20path/local%20name' - ); - expect($tr.find('.nametext').text().trim()).toEqual('local name'); - - $tr = $rows.eq(1); - expect($tr.attr('data-id')).toEqual('1337'); - expect($tr.attr('data-type')).toEqual('dir'); - expect($tr.attr('data-file')).toEqual('b'); - expect($tr.attr('data-path')).toEqual(''); - expect($tr.attr('data-size')).not.toBeDefined(); - expect(parseInt($tr.attr('data-permissions'), 10)) - .toEqual(OC.PERMISSION_ALL); // read and delete - expect($tr.attr('data-mime')).toEqual('httpd/unix-directory'); - expect($tr.attr('data-mtime')).toEqual('22222000'); - expect($tr.attr('data-share-owner')).toEqual('user3@foo.bar/'); - expect($tr.attr('data-share-id')).toEqual('8'); - expect($tr.attr('data-favorite')).not.toBeDefined(); - expect($tr.attr('data-tags')).toEqual(''); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + - '/index.php/apps/files' + - '?dir=/b' - ); - expect($tr.find('.nametext').text().trim()).toEqual('b'); - - }); - }); - describe('loading file list for outgoing shares', function() { - var ocsResponse; - - beforeEach(function() { - fileList = new OCA.Sharing.FileList( - $('#app-content-container'), { - sharedWithUser: false - } - ); - OCA.Sharing.Util.attach(fileList); - - fileList.reload(); - - /* jshint camelcase: false */ - ocsResponse = { - ocs: { - meta: { - status: 'ok', - statuscode: 100, - message: null - }, - data: [{ - id: 7, - item_type: 'file', - item_source: 49, - file_source: 49, - path: '/local path/local name.txt', - permissions: 27, - stime: 11111, - share_type: OC.Share.SHARE_TYPE_USER, - share_with: 'user2', - share_with_displayname: 'User Two', - tags: [OC.TAG_FAVORITE], - mimetype: 'text/plain', - uid_owner: 'user1', - displayname_owner: 'User One' - }] - } - }; - }); - it('render file shares', function() { - var request; - - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'shares?format=json&shared_with_me=false&include_tags=true' - ); - - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponse) - ); - - var $rows = fileList.$el.find('tbody tr'); - var $tr = $rows.eq(0); - expect($rows.length).toEqual(1); - expect($tr.attr('data-id')).toEqual('49'); - expect($tr.attr('data-type')).toEqual('file'); - expect($tr.attr('data-file')).toEqual('local name.txt'); - expect($tr.attr('data-path')).toEqual('/local path'); - expect($tr.attr('data-size')).not.toBeDefined(); - expect($tr.attr('data-permissions')).toEqual('31'); // read and delete - expect($tr.attr('data-mime')).toEqual('text/plain'); - expect($tr.attr('data-mtime')).toEqual('11111000'); - expect($tr.attr('data-share-owner')).not.toBeDefined(); - expect($tr.attr('data-share-id')).toEqual('7'); - expect($tr.attr('data-favorite')).toEqual('true'); - expect($tr.attr('data-tags')).toEqual(OC.TAG_FAVORITE); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + - '/remote.php/webdav/local%20path/local%20name.txt' - ); - expect($tr.find('.nametext').text().trim()).toEqual('local name.txt'); - }); - it('render folder shares', function() { - var request; - /* jshint camelcase: false */ - ocsResponse.ocs.data[0] = _.extend(ocsResponse.ocs.data[0], { - item_type: 'folder', - path: '/local path/local name', - }); - - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'shares?format=json&shared_with_me=false&include_tags=true' - ); - - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponse) - ); - - var $rows = fileList.$el.find('tbody tr'); - var $tr = $rows.eq(0); - expect($rows.length).toEqual(1); - expect($tr.attr('data-id')).toEqual('49'); - expect($tr.attr('data-type')).toEqual('dir'); - expect($tr.attr('data-file')).toEqual('local name'); - expect($tr.attr('data-path')).toEqual('/local path'); - expect($tr.attr('data-size')).not.toBeDefined(); - expect($tr.attr('data-permissions')).toEqual('31'); // read and delete - expect($tr.attr('data-mime')).toEqual('httpd/unix-directory'); - expect($tr.attr('data-mtime')).toEqual('11111000'); - expect($tr.attr('data-share-owner')).not.toBeDefined(); - expect($tr.attr('data-share-id')).toEqual('7'); - expect($tr.attr('data-favorite')).toEqual('true'); - expect($tr.attr('data-tags')).toEqual(OC.TAG_FAVORITE); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + - '/index.php/apps/files' + - '?dir=/local%20path/local%20name' - ); - expect($tr.find('.nametext').text().trim()).toEqual('local name'); - }); - it('render link shares', function() { - /* jshint camelcase: false */ - var request; - ocsResponse.ocs.data[0] = { - id: 7, - item_type: 'file', - item_source: 49, - file_source: 49, - path: '/local path/local name.txt', - permissions: 1, - stime: 11111, - share_type: OC.Share.SHARE_TYPE_LINK, - share_with: null, - token: 'abc', - mimetype: 'text/plain', - uid_owner: 'user1', - displayname_owner: 'User One', - tags: [OC.TAG_FAVORITE] - }; - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'shares?format=json&shared_with_me=false&include_tags=true' - ); - - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponse) - ); - - var $rows = fileList.$el.find('tbody tr'); - var $tr = $rows.eq(0); - expect($rows.length).toEqual(1); - expect($tr.attr('data-id')).toEqual('49'); - expect($tr.attr('data-type')).toEqual('file'); - expect($tr.attr('data-file')).toEqual('local name.txt'); - expect($tr.attr('data-path')).toEqual('/local path'); - expect($tr.attr('data-size')).not.toBeDefined(); - expect($tr.attr('data-permissions')).toEqual('31'); // read and delete - expect($tr.attr('data-mime')).toEqual('text/plain'); - expect($tr.attr('data-mtime')).toEqual('11111000'); - expect($tr.attr('data-share-owner')).not.toBeDefined(); - expect($tr.attr('data-share-id')).toEqual('7'); - expect($tr.attr('data-favorite')).toEqual('true'); - expect($tr.attr('data-tags')).toEqual(OC.TAG_FAVORITE); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + '/remote.php/webdav/local%20path/local%20name.txt' - ); - - expect($tr.find('.nametext').text().trim()).toEqual('local name.txt'); - }); - it('groups link shares with regular shares', function() { - /* jshint camelcase: false */ - var request; - // link share - ocsResponse.ocs.data.push({ - id: 8, - item_type: 'file', - item_source: 49, - file_source: 49, - path: '/local path/local name.txt', - permissions: 1, - stime: 11111, - share_type: OC.Share.SHARE_TYPE_LINK, - share_with: null, - token: 'abc', - mimetype: 'text/plain', - uid_owner: 'user1', - displayname_owner: 'User One', - tags: [OC.TAG_FAVORITE], - }); - // another share of the same file - ocsResponse.ocs.data.push({ - id: 9, - item_type: 'file', - item_source: 49, - file_source: 49, - path: '/local path/local name.txt', - permissions: 27, - stime: 22222, - share_type: OC.Share.SHARE_TYPE_USER, - share_with: 'user3', - share_with_displayname: 'User Three', - mimetype: 'text/plain', - uid_owner: 'user1', - displayname_owner: 'User One' - }); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'shares?format=json&shared_with_me=false&include_tags=true' - ); - - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponse) - ); - - var $rows = fileList.$el.find('tbody tr'); - var $tr = $rows.eq(0); - expect($rows.length).toEqual(1); - expect($tr.attr('data-id')).toEqual('49'); - expect($tr.attr('data-type')).toEqual('file'); - expect($tr.attr('data-file')).toEqual('local name.txt'); - expect($tr.attr('data-path')).toEqual('/local path'); - expect($tr.attr('data-size')).not.toBeDefined(); - expect($tr.attr('data-permissions')).toEqual('31'); // read and delete - expect($tr.attr('data-mime')).toEqual('text/plain'); - // always use the most recent stime - expect($tr.attr('data-mtime')).toEqual('22222000'); - expect($tr.attr('data-share-owner')).not.toBeDefined(); - expect($tr.attr('data-share-id')).toEqual('7,8,9'); - expect($tr.attr('data-favorite')).toEqual('true'); - expect($tr.attr('data-tags')).toEqual(OC.TAG_FAVORITE); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + '/remote.php/webdav/local%20path/local%20name.txt' - ); - expect($tr.find('.nametext').text().trim()).toEqual('local name.txt'); - }); - }); - describe('loading file list for link shares', function() { - var ocsResponse; - - beforeEach(function() { - fileList = new OCA.Sharing.FileList( - $('#app-content-container'), { - linksOnly: true - } - ); - OCA.Sharing.Util.attach(fileList); - - fileList.reload(); - - var expirationDateInADay = moment() - .add(1, 'days').format('YYYY-MM-DD HH:mm:ss'); - - /* jshint camelcase: false */ - ocsResponse = { - ocs: { - meta: { - status: 'ok', - statuscode: 100, - message: null - }, - data: [{ - id: 7, - item_type: 'file', - item_source: 49, - file_source: 49, - path: '/local path/local name.txt', - permissions: 1, - stime: 11111, - expiration: null, - share_type: OC.Share.SHARE_TYPE_LINK, - share_with: null, - token: 'abc', - mimetype: 'text/plain', - uid_owner: 'user1', - displayname_owner: 'User One', - tags: [OC.TAG_FAVORITE] - },{ - id: 8, - item_type: 'file', - item_source: 50, - file_source: 50, - path: '/local path2/local name2.txt', - permissions: 1, - stime: 11112, - expiration: expirationDateInADay, - share_type: OC.Share.SHARE_TYPE_LINK, - share_with: null, - token: 'abcd', - mimetype: 'text/plain2', - uid_owner: 'user2', - displayname_owner: 'User One2' - }] - } - }; - }); - it('render only link shares', function() { - /* jshint camelcase: false */ - var request; - ocsResponse.ocs.data.push({ - // non-link share - id: 8, - item_type: 'file', - item_source: 49, - file_source: 49, - path: '/local path/local name.txt', - permissions: 27, - stime: 11111, - share_type: OC.Share.SHARE_TYPE_USER, - share_with: 'user2', - share_with_displayname: 'User Two', - mimetype: 'text/plain', - uid_owner: 'user1', - displayname_owner: 'User One', - tags: [OC.TAG_FAVORITE] - }); - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'shares?format=json&shared_with_me=false&include_tags=true' - ); - - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponse) - ); - - // only renders the link share entries - var $rows = fileList.$el.find('tbody tr'); - var $tr = $rows.eq(0); - expect($rows.length).toEqual(2); - expect($tr.attr('data-id')).toEqual('49'); - expect($tr.attr('data-type')).toEqual('file'); - expect($tr.attr('data-file')).toEqual('local name.txt'); - expect($tr.attr('data-path')).toEqual('/local path'); - expect($tr.attr('data-size')).not.toBeDefined(); - expect($tr.attr('data-permissions')).toEqual('31'); // read and delete - expect($tr.attr('data-mime')).toEqual('text/plain'); - expect($tr.attr('data-mtime')).toEqual('11111000'); - expect($tr.attr('data-share-recipient-data')).not.toBeDefined(); - expect($tr.attr('data-share-owner')).not.toBeDefined(); - expect($tr.attr('data-share-id')).toEqual('7'); - expect($tr.attr('data-favorite')).toEqual('true'); - expect($tr.attr('data-tags')).toEqual(OC.TAG_FAVORITE); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + '/remote.php/webdav/local%20path/local%20name.txt' - ); - expect($tr.attr('data-expiration')).toEqual('0'); - expect($tr.find('td:last-child span').text()).toEqual(''); - - expect($tr.find('.nametext').text().trim()).toEqual('local name.txt'); - - // change to next row - $tr = $rows.eq(1); - expect($tr.attr('data-id')).toEqual('50'); - expect($tr.attr('data-file')).toEqual('local name2.txt'); - expect($tr.attr('data-expiration')).not.toEqual('0'); - expect($tr.attr('data-favorite')).not.toBeDefined(); - expect($tr.attr('data-tags')).toEqual(''); - expect($tr.find('td:last-child span').text()).toEqual('in a day'); - }); - it('does not show virtual token recipient as recipient when password was set', function() { - /* jshint camelcase: false */ - var request; - // when a password is set, share_with contains an auth token - ocsResponse.ocs.data[0].share_with = 'abc01234/01234abc'; - ocsResponse.ocs.data[0].share_with_displayname = 'abc01234/01234abc'; - expect(fakeServer.requests.length).toEqual(1); - request = fakeServer.requests[0]; - expect(request.url).toEqual( - OC.linkToOCS('apps/files_sharing/api/v1') + - 'shares?format=json&shared_with_me=false&include_tags=true' - ); - - fakeServer.requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(ocsResponse) - ); - - // only renders the link share entry - var $rows = fileList.$el.find('tbody tr'); - var $tr = $rows.eq(0); - expect($rows.length).toEqual(2); - expect($tr.attr('data-id')).toEqual('49'); - expect($tr.attr('data-type')).toEqual('file'); - expect($tr.attr('data-file')).toEqual('local name.txt'); - expect($tr.attr('data-path')).toEqual('/local path'); - expect($tr.attr('data-size')).not.toBeDefined(); - expect($tr.attr('data-permissions')).toEqual('31'); // read and delete - expect($tr.attr('data-mime')).toEqual('text/plain'); - expect($tr.attr('data-mtime')).toEqual('11111000'); - expect($tr.attr('data-share-recipient-data')).not.toBeDefined(); - expect($tr.attr('data-share-owner')).not.toBeDefined(); - expect($tr.attr('data-share-id')).toEqual('7'); - expect($tr.attr('data-favorite')).toEqual('true'); - expect($tr.attr('data-tags')).toEqual(OC.TAG_FAVORITE); - expect($tr.find('a.name').attr('href')).toEqual( - OC.webroot + - '/remote.php/webdav/local%20path/local%20name.txt'); - - expect($tr.find('.nametext').text().trim()).toEqual('local name.txt'); - }); - }); - describe('setting share permissions for files', function () { - beforeEach(function () { - - var $content = $('<div id="content"></div>'); - $('#testArea').append($content); - // dummy file list - var $div = $( - '<div>' + - '<table id="filestable">' + - '<thead></thead>' + - '<tbody id="fileList"></tbody>' + - '</table>' + - '</div>'); - $('#content').append($div); - - fileList = new OCA.Files.FileList($div); - OCA.Sharing.Util.attach(fileList); - }); - - it('external storage root folder', function () { - var $tr; - OC.Share.statuses = {1: {link: false, path: '/subdir'}}; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_READ, - etag: 'abc', - shareOwner: 'User One', - recipients: 'User Two', - mountType: 'external-root' - }]); - $tr = fileList.$el.find('tr:first'); - - expect(parseInt($tr.attr('data-share-permissions'), 10)).toEqual(OC.PERMISSION_ALL - OC.PERMISSION_SHARE); - }); - - it('external storage root folder reshare', function () { - var $tr; - OC.Share.statuses = {1: {link: false, path: '/subdir'}}; - fileList.setFiles([{ - id: 1, - type: 'dir', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_READ + OC.PERMISSION_SHARE, - etag: 'abc', - shareOwner: 'User One', - recipients: 'User Two', - mountType: 'external-root' - }]); - $tr = fileList.$el.find('tr:first'); - - expect(parseInt($tr.attr('data-share-permissions'), 10)).toEqual(OC.PERMISSION_ALL); - }); - - it('external storage root folder file', function () { - var $tr; - OC.Share.statuses = {1: {link: false, path: '/subdir'}}; - fileList.setFiles([{ - id: 1, - type: 'file', - name: 'One.txt', - path: '/subdir', - mimetype: 'text/plain', - size: 12, - permissions: OC.PERMISSION_READ, - etag: 'abc', - shareOwner: 'User One', - recipients: 'User Two', - mountType: 'external-root' - }]); - $tr = fileList.$el.find('tr:first'); - - expect(parseInt($tr.attr('data-share-permissions'), 10)) - .toEqual(OC.PERMISSION_ALL - OC.PERMISSION_SHARE - OC.PERMISSION_DELETE - OC.PERMISSION_CREATE); - }); - }); -}); |