diff options
Diffstat (limited to 'tests/lib/Files/FilesystemTest.php')
-rw-r--r-- | tests/lib/Files/FilesystemTest.php | 223 |
1 files changed, 105 insertions, 118 deletions
diff --git a/tests/lib/Files/FilesystemTest.php b/tests/lib/Files/FilesystemTest.php index 5473f164cef..a819acb1620 100644 --- a/tests/lib/Files/FilesystemTest.php +++ b/tests/lib/Files/FilesystemTest.php @@ -1,42 +1,37 @@ <?php + /** - * ownCloud - * - * @author Robin Appelman - * @copyright 2012 Robin Appelman icewind@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\Files; +use OC\Files\Filesystem; use OC\Files\Mount\MountPoint; use OC\Files\Storage\Temporary; +use OC\Files\View; use OC\User\NoUserException; +use OCP\Files; use OCP\Files\Config\IMountProvider; +use OCP\Files\Config\IMountProviderCollection; +use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage\IStorageFactory; +use OCP\IConfig; +use OCP\ITempManager; use OCP\IUser; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\Server; class DummyMountProvider implements IMountProvider { - private $mounts = []; - /** * @param array $mounts */ - public function __construct(array $mounts) { - $this->mounts = $mounts; + public function __construct( + private array $mounts, + ) { } /** @@ -44,7 +39,7 @@ class DummyMountProvider implements IMountProvider { * * @param IUser $user * @param IStorageFactory $loader - * @return \OCP\Files\Mount\IMountPoint[] + * @return IMountPoint[] */ public function getMountsForUser(IUser $user, IStorageFactory $loader) { return isset($this->mounts[$user->getUID()]) ? $this->mounts[$user->getUID()] : []; @@ -59,8 +54,8 @@ class DummyMountProvider implements IMountProvider { * @package Test\Files */ class FilesystemTest extends \Test\TestCase { - public const TEST_FILESYSTEM_USER1 = "test-filesystem-user1"; - public const TEST_FILESYSTEM_USER2 = "test-filesystem-user1"; + public const TEST_FILESYSTEM_USER1 = 'test-filesystem-user1'; + public const TEST_FILESYSTEM_USER2 = 'test-filesystem-user1'; /** * @var array tmpDirs @@ -71,7 +66,7 @@ class FilesystemTest extends \Test\TestCase { * @return array */ private function getStorageData() { - $dir = \OC::$server->getTempManager()->getTemporaryFolder(); + $dir = Server::get(ITempManager::class)->getTemporaryFolder(); $this->tmpDirs[] = $dir; return ['datadir' => $dir]; } @@ -81,13 +76,13 @@ class FilesystemTest extends \Test\TestCase { $userBackend = new \Test\Util\User\Dummy(); $userBackend->createUser(self::TEST_FILESYSTEM_USER1, self::TEST_FILESYSTEM_USER1); $userBackend->createUser(self::TEST_FILESYSTEM_USER2, self::TEST_FILESYSTEM_USER2); - \OC::$server->getUserManager()->registerBackend($userBackend); + Server::get(IUserManager::class)->registerBackend($userBackend); $this->loginAsUser(); } protected function tearDown(): void { foreach ($this->tmpDirs as $dir) { - \OC_Helper::rmdirr($dir); + Files::rmdirr($dir); } $this->logout(); @@ -95,25 +90,25 @@ class FilesystemTest extends \Test\TestCase { parent::tearDown(); } - public function testMount() { - \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/'); - $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/')); - $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/some/folder')); - [, $internalPath] = \OC\Files\Filesystem::resolvePath('/'); + public function testMount(): void { + Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/'); + $this->assertEquals('/', Filesystem::getMountPoint('/')); + $this->assertEquals('/', Filesystem::getMountPoint('/some/folder')); + [, $internalPath] = Filesystem::resolvePath('/'); $this->assertEquals('', $internalPath); - [, $internalPath] = \OC\Files\Filesystem::resolvePath('/some/folder'); + [, $internalPath] = Filesystem::resolvePath('/some/folder'); $this->assertEquals('some/folder', $internalPath); - \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/some'); - $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/')); - $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/folder')); - $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/')); - $this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some')); - [, $internalPath] = \OC\Files\Filesystem::resolvePath('/some/folder'); + Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/some'); + $this->assertEquals('/', Filesystem::getMountPoint('/')); + $this->assertEquals('/some/', Filesystem::getMountPoint('/some/folder')); + $this->assertEquals('/some/', Filesystem::getMountPoint('/some/')); + $this->assertEquals('/some/', Filesystem::getMountPoint('/some')); + [, $internalPath] = Filesystem::resolvePath('/some/folder'); $this->assertEquals('folder', $internalPath); } - public function normalizePathData() { + public static function normalizePathData(): array { return [ ['/', ''], ['/', '/'], @@ -207,14 +202,12 @@ class FilesystemTest extends \Test\TestCase { ]; } - /** - * @dataProvider normalizePathData - */ - public function testNormalizePath($expected, $path, $stripTrailingSlash = true) { - $this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash)); + #[\PHPUnit\Framework\Attributes\DataProvider('normalizePathData')] + public function testNormalizePath($expected, $path, $stripTrailingSlash = true): void { + $this->assertEquals($expected, Filesystem::normalizePath($path, $stripTrailingSlash)); } - public function normalizePathKeepUnicodeData() { + public static function normalizePathKeepUnicodeData(): array { $nfdName = 'ümlaut'; $nfcName = 'ümlaut'; return [ @@ -225,22 +218,20 @@ class FilesystemTest extends \Test\TestCase { ]; } - /** - * @dataProvider normalizePathKeepUnicodeData - */ - public function testNormalizePathKeepUnicode($expected, $path, $keepUnicode = false) { - $this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, true, false, $keepUnicode)); + #[\PHPUnit\Framework\Attributes\DataProvider('normalizePathKeepUnicodeData')] + public function testNormalizePathKeepUnicode($expected, $path, $keepUnicode = false): void { + $this->assertEquals($expected, Filesystem::normalizePath($path, true, false, $keepUnicode)); } - public function testNormalizePathKeepUnicodeCache() { + public function testNormalizePathKeepUnicodeCache(): void { $nfdName = 'ümlaut'; $nfcName = 'ümlaut'; // call in succession due to cache - $this->assertEquals('/' . $nfcName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, false)); - $this->assertEquals('/' . $nfdName, \OC\Files\Filesystem::normalizePath($nfdName, true, false, true)); + $this->assertEquals('/' . $nfcName, Filesystem::normalizePath($nfdName, true, false, false)); + $this->assertEquals('/' . $nfdName, Filesystem::normalizePath($nfdName, true, false, true)); } - public function isValidPathData() { + public static function isValidPathData(): array { return [ ['/', true], ['/path', true], @@ -266,14 +257,12 @@ class FilesystemTest extends \Test\TestCase { ]; } - /** - * @dataProvider isValidPathData - */ - public function testIsValidPath($path, $expected) { - $this->assertSame($expected, \OC\Files\Filesystem::isValidPath($path)); + #[\PHPUnit\Framework\Attributes\DataProvider('isValidPathData')] + public function testIsValidPath($path, $expected): void { + $this->assertSame($expected, Filesystem::isValidPath($path)); } - public function isFileBlacklistedData() { + public static function isFileBlacklistedData(): array { return [ ['/etc/foo/bar/foo.txt', false], ['\etc\foo/bar\foo.txt', false], @@ -288,48 +277,46 @@ class FilesystemTest extends \Test\TestCase { ]; } - /** - * @dataProvider isFileBlacklistedData - */ - public function testIsFileBlacklisted($path, $expected) { - $this->assertSame($expected, \OC\Files\Filesystem::isFileBlacklisted($path)); + #[\PHPUnit\Framework\Attributes\DataProvider('isFileBlacklistedData')] + public function testIsFileBlacklisted($path, $expected): void { + $this->assertSame($expected, Filesystem::isFileBlacklisted($path)); } - public function testNormalizePathUTF8() { + public function testNormalizePathUTF8(): void { if (!class_exists('Patchwork\PHP\Shim\Normalizer')) { $this->markTestSkipped('UTF8 normalizer Patchwork was not found'); } - $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88")); - $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("\\foo\\baru\xCC\x88")); + $this->assertEquals("/foo/bar\xC3\xBC", Filesystem::normalizePath("/foo/baru\xCC\x88")); + $this->assertEquals("/foo/bar\xC3\xBC", Filesystem::normalizePath("\\foo\\baru\xCC\x88")); } - public function testHooks() { - if (\OC\Files\Filesystem::getView()) { + public function testHooks(): void { + if (Filesystem::getView()) { $user = \OC_User::getUser(); } else { $user = self::TEST_FILESYSTEM_USER1; $backend = new \Test\Util\User\Dummy(); - \OC_User::useBackend($backend); + Server::get(IUserManager::class)->registerBackend($backend); $backend->createUser($user, $user); - $userObj = \OC::$server->getUserManager()->get($user); - \OC::$server->getUserSession()->setUser($userObj); - \OC\Files\Filesystem::init($user, '/' . $user . '/files'); + $userObj = Server::get(IUserManager::class)->get($user); + Server::get(IUserSession::class)->setUser($userObj); + Filesystem::init($user, '/' . $user . '/files'); } \OC_Hook::clear('OC_Filesystem'); \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook'); - \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', [], '/'); + Filesystem::mount('OC\Files\Storage\Temporary', [], '/'); - $rootView = new \OC\Files\View(''); + $rootView = new View(''); $rootView->mkdir('/' . $user); $rootView->mkdir('/' . $user . '/files'); // \OC\Files\Filesystem::file_put_contents('/foo', 'foo'); - \OC\Files\Filesystem::mkdir('/bar'); + Filesystem::mkdir('/bar'); // \OC\Files\Filesystem::file_put_contents('/bar//foo', 'foo'); - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile(); + $tmpFile = Server::get(ITempManager::class)->getTemporaryFile(); file_put_contents($tmpFile, 'foo'); $fh = fopen($tmpFile, 'r'); // \OC\Files\Filesystem::file_put_contents('/bar//foo', $fh); @@ -339,30 +326,30 @@ class FilesystemTest extends \Test\TestCase { * Tests that an exception is thrown when passed user does not exist. * */ - public function testLocalMountWhenUserDoesNotExist() { - $this->expectException(\OC\User\NoUserException::class); + public function testLocalMountWhenUserDoesNotExist(): void { + $this->expectException(NoUserException::class); $userId = $this->getUniqueID('user_'); - \OC\Files\Filesystem::initMountPoints($userId); + Filesystem::initMountPoints($userId); } - public function testNullUserThrows() { - $this->expectException(\OC\User\NoUserException::class); + public function testNullUserThrows(): void { + $this->expectException(NoUserException::class); - \OC\Files\Filesystem::initMountPoints(null); + Filesystem::initMountPoints(null); } - public function testNullUserThrowsTwice() { + public function testNullUserThrowsTwice(): void { $thrown = 0; try { - \OC\Files\Filesystem::initMountPoints(null); + Filesystem::initMountPoints(null); } catch (NoUserException $e) { $thrown++; } try { - \OC\Files\Filesystem::initMountPoints(null); + Filesystem::initMountPoints(null); } catch (NoUserException $e) { $thrown++; } @@ -372,18 +359,18 @@ class FilesystemTest extends \Test\TestCase { /** * Tests that an exception is thrown when passed user does not exist. */ - public function testLocalMountWhenUserDoesNotExistTwice() { + public function testLocalMountWhenUserDoesNotExistTwice(): void { $thrown = 0; $userId = $this->getUniqueID('user_'); try { - \OC\Files\Filesystem::initMountPoints($userId); + Filesystem::initMountPoints($userId); } catch (NoUserException $e) { $thrown++; } try { - \OC\Files\Filesystem::initMountPoints($userId); + Filesystem::initMountPoints($userId); } catch (NoUserException $e) { $thrown++; } @@ -394,14 +381,14 @@ class FilesystemTest extends \Test\TestCase { /** * Tests that the home storage is used for the user's mount point */ - public function testHomeMount() { + public function testHomeMount(): void { $userId = $this->getUniqueID('user_'); - \OC::$server->getUserManager()->createUser($userId, $userId); + Server::get(IUserManager::class)->createUser($userId, $userId); - \OC\Files\Filesystem::initMountPoints($userId); + Filesystem::initMountPoints($userId); - $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/'); + $homeMount = Filesystem::getStorage('/' . $userId . '/'); $this->assertTrue($homeMount->instanceOfStorage('\OCP\Files\IHomeStorage')); if ($homeMount->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { @@ -410,7 +397,7 @@ class FilesystemTest extends \Test\TestCase { $this->assertEquals('home::' . $userId, $homeMount->getId()); } - $user = \OC::$server->getUserManager()->get($userId); + $user = Server::get(IUserManager::class)->get($userId); if ($user !== null) { $user->delete(); } @@ -418,30 +405,30 @@ class FilesystemTest extends \Test\TestCase { public function dummyHook($arguments) { $path = $arguments['path']; - $this->assertEquals($path, \OC\Files\Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized + $this->assertEquals($path, Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized } /** * Test that the default cache dir is part of the user's home */ - public function testMountDefaultCacheDir() { + public function testMountDefaultCacheDir(): void { $userId = $this->getUniqueID('user_'); - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); $oldCachePath = $config->getSystemValueString('cache_path', ''); // no cache path configured $config->setSystemValue('cache_path', ''); - \OC::$server->getUserManager()->createUser($userId, $userId); - \OC\Files\Filesystem::initMountPoints($userId); + Server::get(IUserManager::class)->createUser($userId, $userId); + Filesystem::initMountPoints($userId); $this->assertEquals( '/' . $userId . '/', - \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache') + Filesystem::getMountPoint('/' . $userId . '/cache') ); - [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache'); + [$storage, $internalPath] = Filesystem::resolvePath('/' . $userId . '/cache'); $this->assertTrue($storage->instanceOfStorage('\OCP\Files\IHomeStorage')); $this->assertEquals('cache', $internalPath); - $user = \OC::$server->getUserManager()->get($userId); + $user = Server::get(IUserManager::class)->get($userId); if ($user !== null) { $user->delete(); } @@ -453,26 +440,26 @@ class FilesystemTest extends \Test\TestCase { * Test that an external cache is mounted into * the user's home */ - public function testMountExternalCacheDir() { + public function testMountExternalCacheDir(): void { $userId = $this->getUniqueID('user_'); - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); $oldCachePath = $config->getSystemValueString('cache_path', ''); // set cache path to temp dir - $cachePath = \OC::$server->getTempManager()->getTemporaryFolder() . '/extcache'; + $cachePath = Server::get(ITempManager::class)->getTemporaryFolder() . '/extcache'; $config->setSystemValue('cache_path', $cachePath); - \OC::$server->getUserManager()->createUser($userId, $userId); - \OC\Files\Filesystem::initMountPoints($userId); + Server::get(IUserManager::class)->createUser($userId, $userId); + Filesystem::initMountPoints($userId); $this->assertEquals( '/' . $userId . '/cache/', - \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache') + Filesystem::getMountPoint('/' . $userId . '/cache') ); - [$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache'); + [$storage, $internalPath] = Filesystem::resolvePath('/' . $userId . '/cache'); $this->assertTrue($storage->instanceOfStorage('\OC\Files\Storage\Local')); $this->assertEquals('', $internalPath); - $user = \OC::$server->getUserManager()->get($userId); + $user = Server::get(IUserManager::class)->get($userId); if ($user !== null) { $user->delete(); } @@ -480,12 +467,12 @@ class FilesystemTest extends \Test\TestCase { $config->setSystemValue('cache_path', $oldCachePath); } - public function testRegisterMountProviderAfterSetup() { - \OC\Files\Filesystem::initMountPoints(self::TEST_FILESYSTEM_USER2); - $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/foo/bar')); + public function testRegisterMountProviderAfterSetup(): void { + Filesystem::initMountPoints(self::TEST_FILESYSTEM_USER2); + $this->assertEquals('/', Filesystem::getMountPoint('/foo/bar')); $mount = new MountPoint(new Temporary([]), '/foo/bar'); $mountProvider = new DummyMountProvider([self::TEST_FILESYSTEM_USER2 => [$mount]]); - \OC::$server->getMountProviderCollection()->registerProvider($mountProvider); - $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::getMountPoint('/foo/bar')); + Server::get(IMountProviderCollection::class)->registerProvider($mountProvider); + $this->assertEquals('/foo/bar/', Filesystem::getMountPoint('/foo/bar')); } } |