diff options
Diffstat (limited to 'tests/lib/Share')
-rw-r--r-- | tests/lib/Share/Backend.php | 40 | ||||
-rw-r--r-- | tests/lib/Share/HelperTest.php | 41 | ||||
-rw-r--r-- | tests/lib/Share/SearchResultSorterTest.php | 42 | ||||
-rw-r--r-- | tests/lib/Share/ShareTest.php | 221 |
4 files changed, 95 insertions, 249 deletions
diff --git a/tests/lib/Share/Backend.php b/tests/lib/Share/Backend.php index 18443a4e247..94ac25111c2 100644 --- a/tests/lib/Share/Backend.php +++ b/tests/lib/Share/Backend.php @@ -1,27 +1,19 @@ <?php + /** - * ownCloud - * - * @author Michael Gapczynski - * @copyright 2012 Michael Gapczynski mtgap@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/>. + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Share; -class Backend implements \OCP\Share_Backend { +use OC\Share20\Manager; +use OCP\Server; +use OCP\Share\IShare; +use OCP\Share_Backend; + +class Backend implements Share_Backend { public const FORMAT_SOURCE = 0; public const FORMAT_TARGET = 1; public const FORMAT_PERMISSIONS = 2; @@ -40,13 +32,17 @@ class Backend implements \OCP\Share_Backend { // Always make target be test.txt to cause conflicts if (substr($itemSource, 0, strlen('test')) !== 'test') { - $target = "test.txt"; + $target = 'test.txt'; } else { $target = $itemSource; } - $shares = \OC\Share\Share::getItemsSharedWithUser('test', $shareWith); + $shareManager = Server::get(Manager::class); + $shares = array_merge( + $shareManager->getSharedWith($shareWith, IShare::TYPE_USER), + $shareManager->getSharedWith($shareWith, IShare::TYPE_GROUP), + ); $knownTargets = []; foreach ($shares as $share) { @@ -60,11 +56,11 @@ class Backend implements \OCP\Share_Backend { $ext = substr($target, $pos); $append = ''; $i = 1; - while (in_array($name.$append.$ext, $knownTargets)) { + while (in_array($name . $append . $ext, $knownTargets)) { $append = $i; $i++; } - $target = $name.$append.$ext; + $target = $name . $append . $ext; } return $target; diff --git a/tests/lib/Share/HelperTest.php b/tests/lib/Share/HelperTest.php index 8d010400273..3438f108749 100644 --- a/tests/lib/Share/HelperTest.php +++ b/tests/lib/Share/HelperTest.php @@ -1,32 +1,21 @@ <?php + /** - * ownCloud - * - * @author Bjoern Schiessle - * @copyright 2014 Bjoern Schiessle <schiessle@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/>. + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Share; +use OC\Share\Helper; + /** * @group DB * Class Helper */ class HelperTest extends \Test\TestCase { - public function expireDateProvider() { + public static function expireDateProvider(): array { return [ // no default expire date, we take the users expire date [['defaultExpireDateSet' => false], 2000000000, 2000010000, 2000010000], @@ -47,28 +36,26 @@ class HelperTest extends \Test\TestCase { ]; } - /** - * @dataProvider expireDateProvider - */ - public function testCalculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate, $expected) { - $result = \OC\Share\Helper::calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate); + #[\PHPUnit\Framework\Attributes\DataProvider('expireDateProvider')] + public function testCalculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate, $expected): void { + $result = Helper::calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate); $this->assertSame($expected, $result); } /** - * @dataProvider dataTestCompareServerAddresses * * @param string $server1 * @param string $server2 * @param bool $expected */ - public function testIsSameUserOnSameServer($user1, $server1, $user2, $server2, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestCompareServerAddresses')] + public function testIsSameUserOnSameServer($user1, $server1, $user2, $server2, $expected): void { $this->assertSame($expected, - \OC\Share\Helper::isSameUserOnSameServer($user1, $server1, $user2, $server2) + Helper::isSameUserOnSameServer($user1, $server1, $user2, $server2) ); } - public function dataTestCompareServerAddresses() { + public static function dataTestCompareServerAddresses(): array { return [ ['user1', 'http://server1', 'user1', 'http://server1', true], ['user1', 'https://server1', 'user1', 'http://server1', true], diff --git a/tests/lib/Share/SearchResultSorterTest.php b/tests/lib/Share/SearchResultSorterTest.php deleted file mode 100644 index 63c3aead62e..00000000000 --- a/tests/lib/Share/SearchResultSorterTest.php +++ /dev/null @@ -1,42 +0,0 @@ -<?php -/** - * ownCloud - * - * @author Arthur Schiwon - * @copyright 2014 Arthur Schiwon <blizzz@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/>. - */ - -namespace Test\Share; - -class SearchResultSorterTest extends \Test\TestCase { - public function testSort() { - $search = 'lin'; - $sorter = new \OC\Share\SearchResultSorter($search, 'foobar'); - - $result = [ - ['foobar' => 'woot'], - ['foobar' => 'linux'], - ['foobar' => 'Linus'], - ['foobar' => 'Bicyclerepairwoman'], - ]; - - usort($result, [$sorter, 'sort']); - $this->assertTrue($result[0]['foobar'] === 'Linus'); - $this->assertTrue($result[1]['foobar'] === 'linux'); - $this->assertTrue($result[2]['foobar'] === 'Bicyclerepairwoman'); - $this->assertTrue($result[3]['foobar'] === 'woot'); - } -} diff --git a/tests/lib/Share/ShareTest.php b/tests/lib/Share/ShareTest.php index c690ada99ca..9690d242067 100644 --- a/tests/lib/Share/ShareTest.php +++ b/tests/lib/Share/ShareTest.php @@ -1,31 +1,23 @@ <?php + /** - * ownCloud - * - * @author Michael Gapczynski - * @copyright 2012 Michael Gapczynski mtgap@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/>. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Share; +use OC\Share\Share; +use OC\SystemConfig; +use OCP\Constants; +use OCP\IConfig; +use OCP\IDBConnection; use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; use OCP\IUserManager; -use OCP\Share\IShare; +use OCP\Server; /** * Class Test_Share @@ -35,42 +27,31 @@ use OCP\Share\IShare; class ShareTest extends \Test\TestCase { protected $itemType; - /** @var IUser */ - protected $user1; - /** @var IUser */ - protected $user2; - /** @var IUser */ - protected $user3; - /** @var IUser */ - protected $user4; - /** @var IUser */ - protected $user5; - /** @var IUser */ - protected $user6; - /** @var IUser */ - protected $groupAndUser_user; + protected IUser $user1; + protected IUser $user2; + protected IUser $user3; + protected IUser $user4; + protected IUser $user5; + protected IUser $user6; + protected IUser $groupAndUser_user; - /** @var IGroup */ - protected $group1; - /** @var IGroup */ - protected $group2; - /** @var IGroup */ - protected $groupAndUser_group; + protected IGroup $group1; + protected IGroup $group2; + protected IGroup $groupAndUser_group; - protected $resharing; - protected $dateInFuture; - protected $dateInPast; + protected string $resharing; + protected string $dateInFuture; + protected string $dateInPast; - /** @var IGroupManager */ - protected $groupManager; - /** @var IUserManager */ - protected $userManager; + protected IGroupManager $groupManager; + protected IUserManager $userManager; + private IDBConnection $connection; protected function setUp(): void { parent::setUp(); - $this->groupManager = \OC::$server->getGroupManager(); - $this->userManager = \OC::$server->getUserManager(); + $this->groupManager = Server::get(IGroupManager::class); + $this->userManager = Server::get(IUserManager::class); $this->userManager->clearBackends(); $this->userManager->registerBackend(new \Test\Util\User\Dummy()); @@ -90,6 +71,7 @@ class ShareTest extends \Test\TestCase { $this->group1 = $this->groupManager->createGroup($this->getUniqueID('group1_')); $this->group2 = $this->groupManager->createGroup($this->getUniqueID('group2_')); $this->groupAndUser_group = $this->groupManager->createGroup($groupAndUserId); + $this->connection = Server::get(IDBConnection::class); $this->group1->addUser($this->user1); $this->group1->addUser($this->user2); @@ -99,11 +81,11 @@ class ShareTest extends \Test\TestCase { $this->groupAndUser_group->addUser($this->user2); $this->groupAndUser_group->addUser($this->user3); - \OC\Share\Share::registerBackend('test', 'Test\Share\Backend'); + Share::registerBackend('test', 'Test\Share\Backend'); \OC_Hook::clear('OCP\\Share'); - \OC::registerShareHooks(\OC::$server->getSystemConfig()); - $this->resharing = \OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes'); - \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', 'yes'); + \OC::registerShareHooks(Server::get(SystemConfig::class)); + $this->resharing = Server::get(IConfig::class)->getAppValue('core', 'shareapi_allow_resharing', 'yes'); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_allow_resharing', 'yes'); // 20 Minutes in the past, 20 minutes in the future. $now = time(); @@ -113,9 +95,10 @@ class ShareTest extends \Test\TestCase { } protected function tearDown(): void { - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?'); - $query->execute(['test']); - \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', $this->resharing); + $query = $this->connection->getQueryBuilder(); + $query->delete('share')->andWhere($query->expr()->eq('item_type', $query->createNamedParameter('test'))); + $query->executeStatement(); + Server::get(IConfig::class)->setAppValue('core', 'shareapi_allow_resharing', $this->resharing); $this->user1->delete(); $this->user2->delete(); @@ -133,84 +116,6 @@ class ShareTest extends \Test\TestCase { parent::tearDown(); } - public function testGetItemSharedWithUser() { - \OC_User::setUserId($this->user1->getUID()); - - //add dummy values to the share table - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' - .' `item_type`, `item_source`, `item_target`, `share_type`,' - .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); - $args = ['test', 99, 'target1', IShare::TYPE_USER, $this->user2->getUID(), $this->user1->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target2', IShare::TYPE_USER, $this->user4->getUID(), $this->user1->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target3', IShare::TYPE_USER, $this->user3->getUID(), $this->user2->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target4', IShare::TYPE_USER, $this->user3->getUID(), $this->user4->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target4', IShare::TYPE_USER, $this->user6->getUID(), $this->user4->getUID()]; - $query->execute($args); - - - $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID()); - $this->assertSame(1, count($result1)); - $this->verifyResult($result1, ['target1']); - - $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID()); - $this->assertSame(2, count($result2)); - $this->verifyResult($result2, ['target1', 'target2']); - - $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID()); - $this->assertSame(2, count($result3)); - $this->verifyResult($result3, ['target3', 'target4']); - - $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null); - $this->assertSame(5, count($result4)); // 5 because target4 appears twice - $this->verifyResult($result4, ['target1', 'target2', 'target3', 'target4']); - - $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null); - $this->assertSame(1, count($result6)); - $this->verifyResult($result6, ['target4']); - } - - public function testGetItemSharedWithUserFromGroupShare() { - \OC_User::setUserId($this->user1->getUID()); - - //add dummy values to the share table - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' - .' `item_type`, `item_source`, `item_target`, `share_type`,' - .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); - $args = ['test', 99, 'target1', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user1->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target2', IShare::TYPE_GROUP, $this->group2->getGID(), $this->user1->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target3', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user2->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target4', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user4->getUID()]; - $query->execute($args); - - // user2 is in group1 and group2 - $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID()); - $this->assertSame(2, count($result1)); - $this->verifyResult($result1, ['target1', 'target2']); - - $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID()); - $this->assertSame(2, count($result2)); - $this->verifyResult($result2, ['target1', 'target2']); - - // user3 is in group1 and group2 - $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID()); - $this->assertSame(3, count($result3)); - $this->verifyResult($result3, ['target1', 'target3', 'target4']); - - $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null); - $this->assertSame(4, count($result4)); - $this->verifyResult($result4, ['target1', 'target2', 'target3', 'target4']); - - $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null); - $this->assertSame(0, count($result6)); - } - public function verifyResult($result, $expected) { foreach ($result as $r) { if (in_array($r['item_target'], $expected)) { @@ -222,17 +127,17 @@ class ShareTest extends \Test\TestCase { } /** - * @dataProvider urls * @param string $url * @param string $expectedResult */ - public function testRemoveProtocolFromUrl($url, $expectedResult) { - $share = new \OC\Share\Share(); + #[\PHPUnit\Framework\Attributes\DataProvider('urls')] + public function testRemoveProtocolFromUrl($url, $expectedResult): void { + $share = new Share(); $result = self::invokePrivate($share, 'removeProtocolFromUrl', [$url]); $this->assertSame($expectedResult, $result); } - public function urls() { + public static function urls(): array { return [ ['http://owncloud.org', 'owncloud.org'], ['https://owncloud.org', 'owncloud.org'], @@ -241,11 +146,11 @@ class ShareTest extends \Test\TestCase { } /** - * @dataProvider dataProviderTestGroupItems * @param array $ungrouped * @param array $grouped */ - public function testGroupItems($ungrouped, $grouped) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderTestGroupItems')] + public function testGroupItems($ungrouped, $grouped): void { $result = DummyShareClass::groupItemsTest($ungrouped); $this->compareArrays($grouped, $result); @@ -261,25 +166,25 @@ class ShareTest extends \Test\TestCase { } } - public function dataProviderTestGroupItems() { + public static function dataProviderTestGroupItems(): array { return [ // one array with one share [ [ // input - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1']], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_ALL, 'item_target' => 't1']], [ // expected result - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1']]], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_ALL, 'item_target' => 't1']]], // two shares both point to the same source [ [ // input - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'], - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_READ, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_UPDATE, 'item_target' => 't1'], ], [ // expected result - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1', + ['item_source' => 1, 'permissions' => Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE, 'item_target' => 't1', 'grouped' => [ - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'], - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_READ, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_UPDATE, 'item_target' => 't1'], ] ], ] @@ -287,36 +192,36 @@ class ShareTest extends \Test\TestCase { // two shares both point to the same source but with different targets [ [ // input - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'], - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_READ, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_UPDATE, 'item_target' => 't2'], ], [ // expected result - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'], - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_READ, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_UPDATE, 'item_target' => 't2'], ] ], // three shares two point to the same source [ [ // input - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'], - ['item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'], - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_READ, 'item_target' => 't1'], + ['item_source' => 2, 'permissions' => Constants::PERMISSION_CREATE, 'item_target' => 't2'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_UPDATE, 'item_target' => 't1'], ], [ // expected result - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1', + ['item_source' => 1, 'permissions' => Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE, 'item_target' => 't1', 'grouped' => [ - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'], - ['item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_READ, 'item_target' => 't1'], + ['item_source' => 1, 'permissions' => Constants::PERMISSION_UPDATE, 'item_target' => 't1'], ] ], - ['item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'], + ['item_source' => 2, 'permissions' => Constants::PERMISSION_CREATE, 'item_target' => 't2'], ] ], ]; } } -class DummyShareClass extends \OC\Share\Share { +class DummyShareClass extends Share { public static function groupItemsTest($items) { return parent::groupItems($items, 'test'); } |