diff options
Diffstat (limited to 'tests/lib/UtilTest.php')
-rw-r--r-- | tests/lib/UtilTest.php | 126 |
1 files changed, 104 insertions, 22 deletions
diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php index fcb77332fcc..6d995be2434 100644 --- a/tests/lib/UtilTest.php +++ b/tests/lib/UtilTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,6 +9,11 @@ namespace Test; use OC_Util; +use OCP\Files; +use OCP\IConfig; +use OCP\ISession; +use OCP\ITempManager; +use OCP\Server; use OCP\Util; /** @@ -42,25 +48,25 @@ class UtilTest extends \Test\TestCase { 'And It Even May <strong>Nest</strong>' ], ]; - $result = OC_Util::sanitizeHTML($badArray); + $result = Util::sanitizeHTML($badArray); $this->assertEquals($goodArray, $result); $badString = '<img onload="alert(1)" />'; - $result = OC_Util::sanitizeHTML($badString); + $result = Util::sanitizeHTML($badString); $this->assertEquals('<img onload="alert(1)" />', $result); $badString = "<script>alert('Hacked!');</script>"; - $result = OC_Util::sanitizeHTML($badString); + $result = Util::sanitizeHTML($badString); $this->assertEquals('<script>alert('Hacked!');</script>', $result); $goodString = 'This is a good string without HTML.'; - $result = OC_Util::sanitizeHTML($goodString); + $result = Util::sanitizeHTML($goodString); $this->assertEquals('This is a good string without HTML.', $result); } public function testEncodePath(): void { $component = '/ยง#@test%&^รค/-child'; - $result = OC_Util::encodePath($component); + $result = Util::encodePath($component); $this->assertEquals('/%C2%A7%23%40test%25%26%5E%C3%A4/-child', $result); } @@ -95,7 +101,7 @@ class UtilTest extends \Test\TestCase { * If no strict email check is enabled "localhost" should validate as a valid email domain */ public function testGetDefaultEmailAddress(): void { - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); $config->setAppValue('core', 'enforce_strict_email_check', 'no'); $email = Util::getDefaultEmailAddress('no-reply'); $this->assertEquals('no-reply@localhost', $email); @@ -103,7 +109,7 @@ class UtilTest extends \Test\TestCase { } public function testGetDefaultEmailAddressFromConfig(): void { - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); $config->setSystemValue('mail_domain', 'example.com'); $email = Util::getDefaultEmailAddress('no-reply'); $this->assertEquals('no-reply@example.com', $email); @@ -111,7 +117,7 @@ class UtilTest extends \Test\TestCase { } public function testGetConfiguredEmailAddressFromConfig(): void { - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); $config->setSystemValue('mail_domain', 'example.com'); $config->setSystemValue('mail_from_address', 'owncloud'); $email = Util::getDefaultEmailAddress('no-reply'); @@ -121,7 +127,7 @@ class UtilTest extends \Test\TestCase { } public function testGetInstanceIdGeneratesValidId(): void { - \OC::$server->getConfig()->deleteSystemValue('instanceid'); + Server::get(IConfig::class)->deleteSystemValue('instanceid'); $instanceId = OC_Util::getInstanceId(); $this->assertStringStartsWith('oc', $instanceId); $matchesRegex = preg_match('/^[a-z0-9]+$/', $instanceId); @@ -132,37 +138,37 @@ class UtilTest extends \Test\TestCase { * Test needUpgrade() when the core version is increased */ public function testNeedUpgradeCore(): void { - $config = \OC::$server->getConfig(); + $config = Server::get(IConfig::class); $oldConfigVersion = $config->getSystemValue('version', '0.0.0'); - $oldSessionVersion = \OC::$server->getSession()->get('OC_Version'); + $oldSessionVersion = Server::get(ISession::class)->get('OC_Version'); $this->assertFalse(Util::needUpgrade()); $config->setSystemValue('version', '7.0.0.0'); - \OC::$server->getSession()->set('OC_Version', [7, 0, 0, 1]); + Server::get(ISession::class)->set('OC_Version', [7, 0, 0, 1]); self::invokePrivate(new Util, 'needUpgradeCache', [null]); $this->assertTrue(Util::needUpgrade()); $config->setSystemValue('version', $oldConfigVersion); - \OC::$server->getSession()->set('OC_Version', $oldSessionVersion); + Server::get(ISession::class)->set('OC_Version', $oldSessionVersion); self::invokePrivate(new Util, 'needUpgradeCache', [null]); $this->assertFalse(Util::needUpgrade()); } public function testCheckDataDirectoryValidity(): void { - $dataDir = \OC::$server->getTempManager()->getTemporaryFolder(); + $dataDir = Server::get(ITempManager::class)->getTemporaryFolder(); touch($dataDir . '/.ncdata'); $errors = \OC_Util::checkDataDirectoryValidity($dataDir); $this->assertEmpty($errors); - \OCP\Files::rmdirr($dataDir); + Files::rmdirr($dataDir); - $dataDir = \OC::$server->getTempManager()->getTemporaryFolder(); + $dataDir = Server::get(ITempManager::class)->getTemporaryFolder(); // no touch $errors = \OC_Util::checkDataDirectoryValidity($dataDir); $this->assertNotEmpty($errors); - \OCP\Files::rmdirr($dataDir); + Files::rmdirr($dataDir); $errors = \OC_Util::checkDataDirectoryValidity('relative/path'); $this->assertNotEmpty($errors); @@ -295,12 +301,12 @@ class UtilTest extends \Test\TestCase { } public function testAddStyle(): void { - \OC_Util::addStyle('core', 'myFancyCSSFile1'); - \OC_Util::addStyle('myApp', 'myFancyCSSFile2'); - \OC_Util::addStyle('core', 'myFancyCSSFile0', true); - \OC_Util::addStyle('core', 'myFancyCSSFile10', true); + Util::addStyle('core', 'myFancyCSSFile1'); + Util::addStyle('myApp', 'myFancyCSSFile2'); + Util::addStyle('core', 'myFancyCSSFile0', true); + Util::addStyle('core', 'myFancyCSSFile10', true); // add duplicate - \OC_Util::addStyle('core', 'myFancyCSSFile1'); + Util::addStyle('core', 'myFancyCSSFile1'); $this->assertEquals([], Util::getScripts()); $this->assertEquals([ @@ -334,4 +340,80 @@ class UtilTest extends \Test\TestCase { // each of the characters is 12 bytes $this->assertEquals('๐', Util::shortenMultibyteString('๐๐๐', 16, 2)); } + + #[\PHPUnit\Framework\Attributes\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], + ]; + } + + #[\PHPUnit\Framework\Attributes\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); + } + } |