diff options
Diffstat (limited to 'tests/lib/UtilTest.php')
-rw-r--r-- | tests/lib/UtilTest.php | 109 |
1 files changed, 97 insertions, 12 deletions
diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php index 0063a991e48..d1145fc3149 100644 --- a/tests/lib/UtilTest.php +++ b/tests/lib/UtilTest.php @@ -8,6 +8,11 @@ namespace Test; use OC_Util; +use OCP\Files; +use OCP\IConfig; +use OCP\ISession; +use OCP\ITempManager; +use OCP\Server; use OCP\Util; /** @@ -95,7 +100,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 +108,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 +116,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 +126,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 +137,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); @@ -334,4 +339,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); + } + } |