diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/autoload.php | 16 | ||||
-rw-r--r-- | tests/bootstrap.php | 2 | ||||
-rw-r--r-- | tests/lib/Files/Cache/CacheTest.php | 2 | ||||
-rw-r--r-- | tests/lib/Files/Config/UserMountCacheTest.php | 85 | ||||
-rw-r--r-- | tests/lib/Files/FilesystemTest.php | 5 | ||||
-rw-r--r-- | tests/lib/Files/Node/HookConnectorTest.php | 2 | ||||
-rw-r--r-- | tests/lib/Files/Storage/CommonTest.php | 3 | ||||
-rw-r--r-- | tests/lib/Files/Storage/HomeTest.php | 3 | ||||
-rw-r--r-- | tests/lib/Files/Storage/LocalTest.php | 3 | ||||
-rw-r--r-- | tests/lib/Files/Storage/Wrapper/QuotaTest.php | 3 | ||||
-rw-r--r-- | tests/lib/Files/Storage/Wrapper/WrapperTest.php | 4 | ||||
-rw-r--r-- | tests/lib/Files/Utils/ScannerTest.php | 2 | ||||
-rw-r--r-- | tests/lib/Files/ViewTest.php | 4 | ||||
-rw-r--r-- | tests/lib/FilesTest.php | 42 | ||||
-rw-r--r-- | tests/lib/HelperStorageTest.php | 1 | ||||
-rw-r--r-- | tests/lib/LegacyHelperTest.php | 104 | ||||
-rw-r--r-- | tests/lib/Preview/OfficeTest.php | 10 | ||||
-rw-r--r-- | tests/lib/TempManagerTest.php | 3 | ||||
-rw-r--r-- | tests/lib/Traits/MountProviderTrait.php | 2 | ||||
-rw-r--r-- | tests/lib/UtilTest.php | 80 |
20 files changed, 237 insertions, 139 deletions
diff --git a/tests/autoload.php b/tests/autoload.php new file mode 100644 index 00000000000..05fc3852924 --- /dev/null +++ b/tests/autoload.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +require_once __DIR__ . '/../lib/base.php'; + +/** + * This is a file that applications can require to be able to autoload the class Test\TestCase from Nextcloud tests + */ + +\OC::$composerAutoloader->addPsr4('Test\\', OC::$SERVERROOT . '/tests/lib/', true); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3fdc6b51fba..655b60f114b 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -12,8 +12,8 @@ if ($configDir) { } require_once __DIR__ . '/../lib/base.php'; +require_once __DIR__ . '/autoload.php'; -\OC::$composerAutoloader->addPsr4('Test\\', OC::$SERVERROOT . '/tests/lib/', true); \OC::$composerAutoloader->addPsr4('Tests\\', OC::$SERVERROOT . '/tests/', true); // load all enabled apps diff --git a/tests/lib/Files/Cache/CacheTest.php b/tests/lib/Files/Cache/CacheTest.php index 55b210a1e2d..2815003a996 100644 --- a/tests/lib/Files/Cache/CacheTest.php +++ b/tests/lib/Files/Cache/CacheTest.php @@ -393,7 +393,7 @@ class CacheTest extends \Test\TestCase { $id4 = $this->cache->put($file4, $fileData['foo2']); $id5 = $this->cache->put($file5, $fileData['foo3']); - $tagManager = \OC::$server->getTagManager()->load('files', [], false, $userId); + $tagManager = \OCP\Server::get(\OCP\ITagManager::class)->load('files', [], false, $userId); $this->assertTrue($tagManager->tagAs($id1, 'tag1')); $this->assertTrue($tagManager->tagAs($id1, 'tag2')); $this->assertTrue($tagManager->tagAs($id2, 'tag2')); diff --git a/tests/lib/Files/Config/UserMountCacheTest.php b/tests/lib/Files/Config/UserMountCacheTest.php index 88b8b7767dd..88c0ceaef08 100644 --- a/tests/lib/Files/Config/UserMountCacheTest.php +++ b/tests/lib/Files/Config/UserMountCacheTest.php @@ -10,6 +10,7 @@ namespace Test\Files\Config; use OC\DB\Exceptions\DbalException; use OC\DB\QueryBuilder\Literal; use OC\Files\Cache\Cache; +use OC\Files\Config\UserMountCache; use OC\Files\Mount\MountPoint; use OC\Files\Storage\Storage; use OC\User\Manager; @@ -17,6 +18,9 @@ use OCP\Cache\CappedMemoryCache; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Diagnostics\IEventLogger; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Config\Event\UserMountAddedEvent; +use OCP\Files\Config\Event\UserMountRemovedEvent; +use OCP\Files\Config\Event\UserMountUpdatedEvent; use OCP\Files\Config\ICachedMountInfo; use OCP\ICacheFactory; use OCP\IConfig; @@ -30,28 +34,19 @@ use Test\Util\User\Dummy; * @group DB */ class UserMountCacheTest extends TestCase { - /** - * @var IDBConnection - */ - private $connection; - - /** - * @var IUserManager - */ - private $userManager; - - /** - * @var \OC\Files\Config\UserMountCache - */ - private $cache; - - private $fileIds = []; + private IDBConnection $connection; + private IUserManager $userManager; + private IEventDispatcher $eventDispatcher; + private UserMountCache $cache; + private array $fileIds = []; protected function setUp(): void { parent::setUp(); $this->fileIds = []; + $this->connection = \OC::$server->getDatabaseConnection(); + $config = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); @@ -63,13 +58,22 @@ class UserMountCacheTest extends TestCase { ->expects($this->any()) ->method('getAppValue') ->willReturnArgument(2); + $this->userManager = new Manager($config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), $this->createMock(LoggerInterface::class)); $userBackend = new Dummy(); $userBackend->createUser('u1', ''); $userBackend->createUser('u2', ''); $userBackend->createUser('u3', ''); $this->userManager->registerBackend($userBackend); - $this->cache = new \OC\Files\Config\UserMountCache($this->connection, $this->userManager, $this->createMock(LoggerInterface::class), $this->createMock(IEventLogger::class)); + + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + + $this->cache = new UserMountCache($this->connection, + $this->userManager, + $this->createMock(LoggerInterface::class), + $this->createMock(IEventLogger::class), + $this->eventDispatcher, + ); } protected function tearDown(): void { @@ -121,6 +125,11 @@ class UserMountCacheTest extends TestCase { } public function testNewMounts(): void { + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(fn (UserMountAddedEvent $event) => $event->mountPoint->getMountPoint() === '/asd/')); + $user = $this->userManager->get('u1'); [$storage] = $this->getStorage(10); @@ -141,6 +150,11 @@ class UserMountCacheTest extends TestCase { } public function testSameMounts(): void { + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with($this->callback(fn (UserMountAddedEvent $event) => $event->mountPoint->getMountPoint() === '/asd/')); + $user = $this->userManager->get('u1'); [$storage] = $this->getStorage(10); @@ -165,6 +179,18 @@ class UserMountCacheTest extends TestCase { } public function testRemoveMounts(): void { + $operation = 0; + $this->eventDispatcher + ->expects($this->exactly(2)) + ->method('dispatchTyped') + ->with($this->callback(function (UserMountAddedEvent|UserMountRemovedEvent $event) use (&$operation) { + return match(++$operation) { + 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/asd/', + 2 => $event instanceof UserMountRemovedEvent && $event->mountPoint->getMountPoint() === '/asd/', + default => false, + }; + })); + $user = $this->userManager->get('u1'); [$storage] = $this->getStorage(10); @@ -184,6 +210,19 @@ class UserMountCacheTest extends TestCase { } public function testChangeMounts(): void { + $operation = 0; + $this->eventDispatcher + ->expects($this->exactly(3)) + ->method('dispatchTyped') + ->with($this->callback(function (UserMountAddedEvent|UserMountRemovedEvent $event) use (&$operation) { + return match(++$operation) { + 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/bar/', + 2 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/foo/', + 3 => $event instanceof UserMountRemovedEvent && $event->mountPoint->getMountPoint() === '/bar/', + default => false, + }; + })); + $user = $this->userManager->get('u1'); [$storage] = $this->getStorage(10); @@ -207,6 +246,18 @@ class UserMountCacheTest extends TestCase { } public function testChangeMountId(): void { + $operation = 0; + $this->eventDispatcher + ->expects($this->exactly(2)) + ->method('dispatchTyped') + ->with($this->callback(function (UserMountAddedEvent|UserMountUpdatedEvent $event) use (&$operation) { + return match(++$operation) { + 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/foo/', + 2 => $event instanceof UserMountUpdatedEvent && $event->oldMountPoint->getMountId() === null && $event->newMountPoint->getMountId() === 1, + default => false, + }; + })); + $user = $this->userManager->get('u1'); [$storage] = $this->getStorage(10); diff --git a/tests/lib/Files/FilesystemTest.php b/tests/lib/Files/FilesystemTest.php index 4a3543474a1..529b4f58428 100644 --- a/tests/lib/Files/FilesystemTest.php +++ b/tests/lib/Files/FilesystemTest.php @@ -10,6 +10,7 @@ namespace Test\Files; use OC\Files\Mount\MountPoint; use OC\Files\Storage\Temporary; use OC\User\NoUserException; +use OCP\Files; use OCP\Files\Config\IMountProvider; use OCP\Files\Storage\IStorageFactory; use OCP\IUser; @@ -74,7 +75,7 @@ class FilesystemTest extends \Test\TestCase { protected function tearDown(): void { foreach ($this->tmpDirs as $dir) { - \OC_Helper::rmdirr($dir); + Files::rmdirr($dir); } $this->logout(); @@ -472,7 +473,7 @@ class FilesystemTest extends \Test\TestCase { $this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/foo/bar')); $mount = new MountPoint(new Temporary([]), '/foo/bar'); $mountProvider = new DummyMountProvider([self::TEST_FILESYSTEM_USER2 => [$mount]]); - \OC::$server->getMountProviderCollection()->registerProvider($mountProvider); + \OCP\Server::get(\OCP\Files\Config\IMountProviderCollection::class)->registerProvider($mountProvider); $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::getMountPoint('/foo/bar')); } } diff --git a/tests/lib/Files/Node/HookConnectorTest.php b/tests/lib/Files/Node/HookConnectorTest.php index b9e16299dd0..87e83fd0a3b 100644 --- a/tests/lib/Files/Node/HookConnectorTest.php +++ b/tests/lib/Files/Node/HookConnectorTest.php @@ -80,7 +80,7 @@ class HookConnectorTest extends TestCase { Filesystem::getMountManager(), $this->view, \OC::$server->getUserManager()->get($this->userId), - \OC::$server->getUserMountCache(), + \OCP\Server::get(\OCP\Files\Config\IUserMountCache::class), $this->createMock(LoggerInterface::class), $this->createMock(IUserManager::class), $this->createMock(IEventDispatcher::class), diff --git a/tests/lib/Files/Storage/CommonTest.php b/tests/lib/Files/Storage/CommonTest.php index b51b35be8f9..529615f3733 100644 --- a/tests/lib/Files/Storage/CommonTest.php +++ b/tests/lib/Files/Storage/CommonTest.php @@ -9,6 +9,7 @@ namespace Test\Files\Storage; use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Wrapper\Wrapper; +use OCP\Files; use OCP\Files\IFilenameValidator; use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidPathException; @@ -37,7 +38,7 @@ class CommonTest extends Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); $this->restoreService(IFilenameValidator::class); parent::tearDown(); } diff --git a/tests/lib/Files/Storage/HomeTest.php b/tests/lib/Files/Storage/HomeTest.php index b6d0f1aa85a..26b81c6f1a0 100644 --- a/tests/lib/Files/Storage/HomeTest.php +++ b/tests/lib/Files/Storage/HomeTest.php @@ -8,6 +8,7 @@ namespace Test\Files\Storage; use OC\User\User; +use OCP\Files; class DummyUser extends User { private $home; @@ -62,7 +63,7 @@ class HomeTest extends Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); parent::tearDown(); } diff --git a/tests/lib/Files/Storage/LocalTest.php b/tests/lib/Files/Storage/LocalTest.php index 65bc538ef17..13317fad7bb 100644 --- a/tests/lib/Files/Storage/LocalTest.php +++ b/tests/lib/Files/Storage/LocalTest.php @@ -8,6 +8,7 @@ namespace Test\Files\Storage; use OC\Files\Storage\Wrapper\Jail; +use OCP\Files; /** * Class LocalTest @@ -30,7 +31,7 @@ class LocalTest extends Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); parent::tearDown(); } diff --git a/tests/lib/Files/Storage/Wrapper/QuotaTest.php b/tests/lib/Files/Storage/Wrapper/QuotaTest.php index aebae9b3c62..791e8047f7f 100644 --- a/tests/lib/Files/Storage/Wrapper/QuotaTest.php +++ b/tests/lib/Files/Storage/Wrapper/QuotaTest.php @@ -10,6 +10,7 @@ namespace Test\Files\Storage\Wrapper; //ensure the constants are loaded use OC\Files\Cache\CacheEntry; use OC\Files\Storage\Local; +use OCP\Files; \OC::$loader->load('\OC\Files\Filesystem'); @@ -35,7 +36,7 @@ class QuotaTest extends \Test\Files\Storage\Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); parent::tearDown(); } diff --git a/tests/lib/Files/Storage/Wrapper/WrapperTest.php b/tests/lib/Files/Storage/Wrapper/WrapperTest.php index 1d0f41bf3ed..4cbae1762fc 100644 --- a/tests/lib/Files/Storage/Wrapper/WrapperTest.php +++ b/tests/lib/Files/Storage/Wrapper/WrapperTest.php @@ -7,6 +7,8 @@ namespace Test\Files\Storage\Wrapper; +use OCP\Files; + class WrapperTest extends \Test\Files\Storage\Storage { /** * @var string tmpDir @@ -22,7 +24,7 @@ class WrapperTest extends \Test\Files\Storage\Storage { } protected function tearDown(): void { - \OC_Helper::rmdirr($this->tmpDir); + Files::rmdirr($this->tmpDir); parent::tearDown(); } diff --git a/tests/lib/Files/Utils/ScannerTest.php b/tests/lib/Files/Utils/ScannerTest.php index 51089e5560c..047da5851b7 100644 --- a/tests/lib/Files/Utils/ScannerTest.php +++ b/tests/lib/Files/Utils/ScannerTest.php @@ -124,7 +124,7 @@ class ScannerTest extends \Test\TestCase { } }); - \OC::$server->getMountProviderCollection()->registerProvider($mountProvider); + \OCP\Server::get(\OCP\Files\Config\IMountProviderCollection::class)->registerProvider($mountProvider); $cache = $storage->getCache(); $storage->mkdir('folder'); diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php index b790c9ed6e8..d611df372e6 100644 --- a/tests/lib/Files/ViewTest.php +++ b/tests/lib/Files/ViewTest.php @@ -155,7 +155,7 @@ class ViewTest extends \Test\TestCase { $this->userObject->delete(); $this->groupObject->delete(); - $mountProviderCollection = \OC::$server->getMountProviderCollection(); + $mountProviderCollection = \OCP\Server::get(\OCP\Files\Config\IMountProviderCollection::class); self::invokePrivate($mountProviderCollection, 'providers', [[]]); parent::tearDown(); @@ -1626,7 +1626,7 @@ class ViewTest extends \Test\TestCase { ->method('getMountsForUser') ->willReturn($mounts); - $mountProviderCollection = \OC::$server->getMountProviderCollection(); + $mountProviderCollection = \OCP\Server::get(\OCP\Files\Config\IMountProviderCollection::class); $mountProviderCollection->registerProvider($mountProvider); return $mounts; diff --git a/tests/lib/FilesTest.php b/tests/lib/FilesTest.php new file mode 100644 index 00000000000..2ba6ce2666b --- /dev/null +++ b/tests/lib/FilesTest.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test; + +use OCP\Files; +use OCP\ITempManager; +use OCP\Server; + +class FilesTest extends TestCase { + + /** + * Tests recursive folder deletion with rmdirr() + */ + public function testRecursiveFolderDeletion(): void { + $baseDir = Server::get(ITempManager::class)->getTemporaryFolder() . '/'; + mkdir($baseDir . 'a/b/c/d/e', 0777, true); + mkdir($baseDir . 'a/b/c1/d/e', 0777, true); + mkdir($baseDir . 'a/b/c2/d/e', 0777, true); + mkdir($baseDir . 'a/b1/c1/d/e', 0777, true); + mkdir($baseDir . 'a/b2/c1/d/e', 0777, true); + mkdir($baseDir . 'a/b3/c1/d/e', 0777, true); + mkdir($baseDir . 'a1/b', 0777, true); + mkdir($baseDir . 'a1/c', 0777, true); + file_put_contents($baseDir . 'a/test.txt', 'Hello file!'); + file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!'); + file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!'); + Files::rmdirr($baseDir . 'a'); + + $this->assertFalse(file_exists($baseDir . 'a')); + $this->assertTrue(file_exists($baseDir . 'a1')); + + Files::rmdirr($baseDir); + $this->assertFalse(file_exists($baseDir)); + } +} diff --git a/tests/lib/HelperStorageTest.php b/tests/lib/HelperStorageTest.php index e4e877ab2a8..628e77e935a 100644 --- a/tests/lib/HelperStorageTest.php +++ b/tests/lib/HelperStorageTest.php @@ -94,6 +94,7 @@ class HelperStorageTest extends \Test\TestCase { $this->assertEquals(5, $storageInfo['used']); $this->assertEquals(17, $storageInfo['total']); } + private function getIncludeExternalStorage(): bool { $class = new \ReflectionClass(\OC_Helper::class); $prop = $class->getProperty('quotaIncludeExternalStorage'); diff --git a/tests/lib/LegacyHelperTest.php b/tests/lib/LegacyHelperTest.php index 15af5ca06e8..0938640b892 100644 --- a/tests/lib/LegacyHelperTest.php +++ b/tests/lib/LegacyHelperTest.php @@ -23,85 +23,6 @@ class LegacyHelperTest extends \Test\TestCase { \OC::$WEBROOT = $this->originalWebRoot; } - /** - * @dataProvider humanFileSizeProvider - */ - public function testHumanFileSize($expected, $input): void { - $result = OC_Helper::humanFileSize($input); - $this->assertEquals($expected, $result); - } - - public static function humanFileSizeProvider(): array { - return [ - ['0 B', 0], - ['1 KB', 1024], - ['9.5 MB', 10000000], - ['1.3 GB', 1395864371], - ['465.7 GB', 500000000000], - ['454.7 TB', 500000000000000], - ['444.1 PB', 500000000000000000], - ]; - } - - /** - * @dataProvider providesComputerFileSize - */ - public function testComputerFileSize($expected, $input): void { - $result = OC_Helper::computerFileSize($input); - $this->assertEquals($expected, $result); - } - - public static function providesComputerFileSize(): array { - return [ - [0.0, '0 B'], - [1024.0, '1 KB'], - [1395864371.0, '1.3 GB'], - [9961472.0, '9.5 MB'], - [500041567437.0, '465.7 GB'], - [false, '12 GB etfrhzui'] - ]; - } - - public function testMb_array_change_key_case(): void { - $arrayStart = [ - 'Foo' => 'bar', - 'Bar' => 'foo', - ]; - $arrayResult = [ - 'foo' => 'bar', - 'bar' => 'foo', - ]; - $result = OC_Helper::mb_array_change_key_case($arrayStart); - $expected = $arrayResult; - $this->assertEquals($result, $expected); - - $arrayStart = [ - 'foo' => 'bar', - 'bar' => 'foo', - ]; - $arrayResult = [ - 'FOO' => 'bar', - 'BAR' => 'foo', - ]; - $result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER); - $expected = $arrayResult; - $this->assertEquals($result, $expected); - } - - public function testRecursiveArraySearch(): void { - $haystack = [ - 'Foo' => 'own', - 'Bar' => 'Cloud', - ]; - - $result = OC_Helper::recursiveArraySearch($haystack, 'own'); - $expected = 'Foo'; - $this->assertEquals($result, $expected); - - $result = OC_Helper::recursiveArraySearch($haystack, 'NotFound'); - $this->assertFalse($result); - } - public function testBuildNotExistingFileNameForView(): void { $viewMock = $this->createMock(View::class); $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock)); @@ -227,29 +148,4 @@ class LegacyHelperTest extends \Test\TestCase { [3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'], ]; } - - /** - * Tests recursive folder deletion with rmdirr() - */ - public function testRecursiveFolderDeletion(): void { - $baseDir = \OC::$server->getTempManager()->getTemporaryFolder() . '/'; - mkdir($baseDir . 'a/b/c/d/e', 0777, true); - mkdir($baseDir . 'a/b/c1/d/e', 0777, true); - mkdir($baseDir . 'a/b/c2/d/e', 0777, true); - mkdir($baseDir . 'a/b1/c1/d/e', 0777, true); - mkdir($baseDir . 'a/b2/c1/d/e', 0777, true); - mkdir($baseDir . 'a/b3/c1/d/e', 0777, true); - mkdir($baseDir . 'a1/b', 0777, true); - mkdir($baseDir . 'a1/c', 0777, true); - file_put_contents($baseDir . 'a/test.txt', 'Hello file!'); - file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!'); - file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!'); - \OC_Helper::rmdirr($baseDir . 'a'); - - $this->assertFalse(file_exists($baseDir . 'a')); - $this->assertTrue(file_exists($baseDir . 'a1')); - - \OC_Helper::rmdirr($baseDir); - $this->assertFalse(file_exists($baseDir)); - } } diff --git a/tests/lib/Preview/OfficeTest.php b/tests/lib/Preview/OfficeTest.php index c9019d017a3..167c442dd34 100644 --- a/tests/lib/Preview/OfficeTest.php +++ b/tests/lib/Preview/OfficeTest.php @@ -7,6 +7,9 @@ namespace Test\Preview; +use OCP\IBinaryFinder; +use OCP\Server; + /** * Class OfficeTest * @@ -16,10 +19,11 @@ namespace Test\Preview; */ class OfficeTest extends Provider { protected function setUp(): void { - $libreofficeBinary = \OC_Helper::findBinaryPath('libreoffice'); - $openofficeBinary = ($libreofficeBinary) ? null : \OC_Helper::findBinaryPath('openoffice'); + $binaryFinder = Server::get(IBinaryFinder::class); + $libreofficeBinary = $binaryFinder->findBinaryPath('libreoffice'); + $openofficeBinary = $libreofficeBinary === false ? $binaryFinder->findBinaryPath('openoffice') : false; - if ($libreofficeBinary || $openofficeBinary) { + if ($libreofficeBinary !== false || $openofficeBinary !== false) { parent::setUp(); $fileName = 'testimage.odt'; diff --git a/tests/lib/TempManagerTest.php b/tests/lib/TempManagerTest.php index 63cffc0dcae..b607772f5c3 100644 --- a/tests/lib/TempManagerTest.php +++ b/tests/lib/TempManagerTest.php @@ -9,6 +9,7 @@ namespace Test; use bantu\IniGetWrapper\IniGetWrapper; +use OCP\Files; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -26,7 +27,7 @@ class TempManagerTest extends \Test\TestCase { protected function tearDown(): void { if ($this->baseDir !== null) { - \OC_Helper::rmdirr($this->baseDir); + Files::rmdirr($this->baseDir); } $this->baseDir = null; parent::tearDown(); diff --git a/tests/lib/Traits/MountProviderTrait.php b/tests/lib/Traits/MountProviderTrait.php index 6d947d645dd..3bb92f48e20 100644 --- a/tests/lib/Traits/MountProviderTrait.php +++ b/tests/lib/Traits/MountProviderTrait.php @@ -58,6 +58,6 @@ trait MountProviderTrait { return []; } })); - \OC::$server->getMountProviderCollection()->registerProvider($this->mountProvider); + \OCP\Server::get(\OCP\Files\Config\IMountProviderCollection::class)->registerProvider($this->mountProvider); } } diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php index 0063a991e48..e124ea687d8 100644 --- a/tests/lib/UtilTest.php +++ b/tests/lib/UtilTest.php @@ -334,4 +334,84 @@ class UtilTest extends \Test\TestCase { // each of the characters is 12 bytes $this->assertEquals('🙈', Util::shortenMultibyteString('🙈🙊🙉', 16, 2)); } + + /** + * @dataProvider humanFileSizeProvider + */ + public function testHumanFileSize($expected, $input): void { + $result = Util::humanFileSize($input); + $this->assertEquals($expected, $result); + } + + public static function humanFileSizeProvider(): array { + return [ + ['0 B', 0], + ['1 KB', 1024], + ['9.5 MB', 10000000], + ['1.3 GB', 1395864371], + ['465.7 GB', 500000000000], + ['454.7 TB', 500000000000000], + ['444.1 PB', 500000000000000000], + ]; + } + + /** + * @dataProvider providesComputerFileSize + */ + public function testComputerFileSize($expected, $input): void { + $result = Util::computerFileSize($input); + $this->assertEquals($expected, $result); + } + + public static function providesComputerFileSize(): array { + return [ + [0.0, '0 B'], + [1024.0, '1 KB'], + [1395864371.0, '1.3 GB'], + [9961472.0, '9.5 MB'], + [500041567437.0, '465.7 GB'], + [false, '12 GB etfrhzui'] + ]; + } + + public function testMb_array_change_key_case(): void { + $arrayStart = [ + 'Foo' => 'bar', + 'Bar' => 'foo', + ]; + $arrayResult = [ + 'foo' => 'bar', + 'bar' => 'foo', + ]; + $result = Util::mb_array_change_key_case($arrayStart); + $expected = $arrayResult; + $this->assertEquals($result, $expected); + + $arrayStart = [ + 'foo' => 'bar', + 'bar' => 'foo', + ]; + $arrayResult = [ + 'FOO' => 'bar', + 'BAR' => 'foo', + ]; + $result = Util::mb_array_change_key_case($arrayStart, MB_CASE_UPPER); + $expected = $arrayResult; + $this->assertEquals($result, $expected); + } + + public function testRecursiveArraySearch(): void { + $haystack = [ + 'Foo' => 'own', + 'Bar' => 'Cloud', + ]; + + $result = Util::recursiveArraySearch($haystack, 'own'); + $expected = 'Foo'; + $this->assertEquals($result, $expected); + + $result = Util::recursiveArraySearch($haystack, 'NotFound'); + $this->assertFalse($result); + } + } |