aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/UtilTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/UtilTest.php')
-rw-r--r--tests/lib/UtilTest.php564
1 files changed, 264 insertions, 300 deletions
diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php
index 6e25ce16e75..6d995be2434 100644
--- a/tests/lib/UtilTest.php
+++ b/tests/lib/UtilTest.php
@@ -1,17 +1,20 @@
<?php
+
/**
- * Copyright (c) 2012 Lukas Reschke <lukas@statuscode.ch>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test;
use OC_Util;
-use OCP\App\IAppManager;
+use OCP\Files;
use OCP\IConfig;
-use OCP\IUser;
+use OCP\ISession;
+use OCP\ITempManager;
+use OCP\Server;
+use OCP\Util;
/**
* Class UtilTest
@@ -20,25 +23,15 @@ use OCP\IUser;
* @group DB
*/
class UtilTest extends \Test\TestCase {
- public function testGetVersion() {
- $version = \OCP\Util::getVersion();
+ public function testGetVersion(): void {
+ $version = Util::getVersion();
$this->assertTrue(is_array($version));
foreach ($version as $num) {
$this->assertTrue(is_int($num));
}
}
- public function testGetVersionString() {
- $version = \OC_Util::getVersionString();
- $this->assertTrue(is_string($version));
- }
-
- public function testGetEditionString() {
- $edition = \OC_Util::getEditionString();
- $this->assertTrue(is_string($edition));
- }
-
- public function testSanitizeHTML() {
+ public function testSanitizeHTML(): void {
$badArray = [
'While it is unusual to pass an array',
'this function actually <blink>supports</blink> it.',
@@ -55,58 +48,86 @@ class UtilTest extends \Test\TestCase {
'And It Even May &lt;strong&gt;Nest&lt;/strong&gt;'
],
];
- $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('&lt;img onload=&quot;alert(1)&quot; /&gt;', $result);
$badString = "<script>alert('Hacked!');</script>";
- $result = OC_Util::sanitizeHTML($badString);
+ $result = Util::sanitizeHTML($badString);
$this->assertEquals('&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;', $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() {
+ public function testEncodePath(): void {
$component = '/§#@test%&^ä/-child';
- $result = OC_Util::encodePath($component);
- $this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
+ $result = Util::encodePath($component);
+ $this->assertEquals('/%C2%A7%23%40test%25%26%5E%C3%A4/-child', $result);
+ }
+
+ public function testIsNonUTF8Locale(): void {
+ // OC_Util::isNonUTF8Locale() assumes escapeshellcmd('§') returns '' with non-UTF-8 locale.
+ $locale = setlocale(LC_CTYPE, 0);
+ setlocale(LC_CTYPE, 'C');
+ $this->assertEquals('', escapeshellcmd('§'));
+ $this->assertEquals('\'\'', escapeshellarg('§'));
+ setlocale(LC_CTYPE, 'C.UTF-8');
+ $this->assertEquals('§', escapeshellcmd('§'));
+ $this->assertEquals('\'§\'', escapeshellarg('§'));
+ setlocale(LC_CTYPE, $locale);
}
- public function testFileInfoLoaded() {
+ public function testFileInfoLoaded(): void {
$expected = function_exists('finfo_open');
$this->assertEquals($expected, \OC_Util::fileInfoLoaded());
}
- public function testGetDefaultEmailAddress() {
- $email = \OCP\Util::getDefaultEmailAddress("no-reply");
+ /**
+ * Host is "localhost" this is a valid for emails,
+ * but not for default strict email verification that requires a top level domain.
+ * So we check that with strict email verification we fallback to the default
+ */
+ public function testGetDefaultEmailAddressStrict(): void {
+ $email = Util::getDefaultEmailAddress('no-reply');
+ $this->assertEquals('no-reply@localhost.localdomain', $email);
+ }
+
+ /**
+ * If no strict email check is enabled "localhost" should validate as a valid email domain
+ */
+ public function testGetDefaultEmailAddress(): void {
+ $config = Server::get(IConfig::class);
+ $config->setAppValue('core', 'enforce_strict_email_check', 'no');
+ $email = Util::getDefaultEmailAddress('no-reply');
$this->assertEquals('no-reply@localhost', $email);
+ $config->deleteAppValue('core', 'enforce_strict_email_check');
}
- public function testGetDefaultEmailAddressFromConfig() {
- $config = \OC::$server->getConfig();
+ public function testGetDefaultEmailAddressFromConfig(): void {
+ $config = Server::get(IConfig::class);
$config->setSystemValue('mail_domain', 'example.com');
- $email = \OCP\Util::getDefaultEmailAddress("no-reply");
+ $email = Util::getDefaultEmailAddress('no-reply');
$this->assertEquals('no-reply@example.com', $email);
$config->deleteSystemValue('mail_domain');
}
- public function testGetConfiguredEmailAddressFromConfig() {
- $config = \OC::$server->getConfig();
+ public function testGetConfiguredEmailAddressFromConfig(): void {
+ $config = Server::get(IConfig::class);
$config->setSystemValue('mail_domain', 'example.com');
$config->setSystemValue('mail_from_address', 'owncloud');
- $email = \OCP\Util::getDefaultEmailAddress("no-reply");
+ $email = Util::getDefaultEmailAddress('no-reply');
$this->assertEquals('owncloud@example.com', $email);
$config->deleteSystemValue('mail_domain');
$config->deleteSystemValue('mail_from_address');
}
- public function testGetInstanceIdGeneratesValidId() {
- \OC::$server->getConfig()->deleteSystemValue('instanceid');
+ public function testGetInstanceIdGeneratesValidId(): void {
+ Server::get(IConfig::class)->deleteSystemValue('instanceid');
$instanceId = OC_Util::getInstanceId();
$this->assertStringStartsWith('oc', $instanceId);
$matchesRegex = preg_match('/^[a-z0-9]+$/', $instanceId);
@@ -114,235 +135,40 @@ class UtilTest extends \Test\TestCase {
}
/**
- * @dataProvider filenameValidationProvider
- */
- public function testFilenameValidation($file, $valid) {
- // private API
- $this->assertEquals($valid, \OC_Util::isValidFileName($file));
- // public API
- $this->assertEquals($valid, \OCP\Util::isValidFileName($file));
- }
-
- public function filenameValidationProvider() {
- return [
- // valid names
- ['boringname', true],
- ['something.with.extension', true],
- ['now with spaces', true],
- ['.a', true],
- ['..a', true],
- ['.dotfile', true],
- ['single\'quote', true],
- [' spaces before', true],
- ['spaces after ', true],
- ['allowed chars including the crazy ones $%&_-^@!,()[]{}=;#', true],
- ['汉字也能用', true],
- ['und Ümläüte sind auch willkommen', true],
- // disallowed names
- ['', false],
- [' ', false],
- ['.', false],
- ['..', false],
- ['back\\slash', false],
- ['sl/ash', false],
- ['lt<lt', true],
- ['gt>gt', true],
- ['col:on', true],
- ['double"quote', true],
- ['pi|pe', true],
- ['dont?ask?questions?', true],
- ['super*star', true],
- ['new\nline', false],
-
- // better disallow these to avoid unexpected trimming to have side effects
- [' ..', false],
- ['.. ', false],
- ['. ', false],
- [' .', false],
-
- // part files not allowed
- ['.part', false],
- ['notallowed.part', false],
- ['neither.filepart', false],
-
- // part in the middle is ok
- ['super movie part one.mkv', true],
- ['super.movie.part.mkv', true],
- ];
- }
-
- /**
- * @dataProvider dataProviderForTestIsSharingDisabledForUser
- * @param array $groups existing groups
- * @param array $membership groups the user belong to
- * @param array $excludedGroups groups which should be excluded from sharing
- * @param bool $expected expected result
- */
- public function testIsSharingDisabledForUser($groups, $membership, $excludedGroups, $expected) {
- $config = $this->getMockBuilder(IConfig::class)->disableOriginalConstructor()->getMock();
- $groupManager = $this->getMockBuilder('OCP\IGroupManager')->disableOriginalConstructor()->getMock();
- $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
-
- $config
- ->expects($this->at(0))
- ->method('getAppValue')
- ->with('core', 'shareapi_exclude_groups', 'no')
- ->willReturn('yes');
- $config
- ->expects($this->at(1))
- ->method('getAppValue')
- ->with('core', 'shareapi_exclude_groups_list')
- ->willReturn(json_encode($excludedGroups));
-
- $groupManager
- ->expects($this->at(0))
- ->method('getUserGroupIds')
- ->with($user)
- ->willReturn($membership);
-
- $result = \OC_Util::isSharingDisabledForUser($config, $groupManager, $user);
-
- $this->assertSame($expected, $result);
- }
-
- public function dataProviderForTestIsSharingDisabledForUser() {
- return [
- // existing groups, groups the user belong to, groups excluded from sharing, expected result
- [['g1', 'g2', 'g3'], [], ['g1'], false],
- [['g1', 'g2', 'g3'], [], [], false],
- [['g1', 'g2', 'g3'], ['g2'], ['g1'], false],
- [['g1', 'g2', 'g3'], ['g2'], [], false],
- [['g1', 'g2', 'g3'], ['g1', 'g2'], ['g1'], false],
- [['g1', 'g2', 'g3'], ['g1', 'g2'], ['g1', 'g2'], true],
- [['g1', 'g2', 'g3'], ['g1', 'g2'], ['g1', 'g2', 'g3'], true],
- ];
- }
-
- /**
- * Test default apps
- *
- * @dataProvider defaultAppsProvider
- * @group DB
- */
- public function testDefaultApps($defaultAppConfig, $expectedPath, $enabledApps) {
- $oldDefaultApps = \OC::$server->getConfig()->getSystemValue('defaultapp', '');
- // CLI is doing messy stuff with the webroot, so need to work it around
- $oldWebRoot = \OC::$WEBROOT;
- \OC::$WEBROOT = '';
-
- $appManager = $this->createMock(IAppManager::class);
- $appManager->expects($this->any())
- ->method('isEnabledForUser')
- ->willReturnCallback(function ($appId) use ($enabledApps) {
- return in_array($appId, $enabledApps);
- });
- Dummy_OC_Util::$appManager = $appManager;
-
- // need to set a user id to make sure enabled apps are read from cache
- \OC_User::setUserId($this->getUniqueID());
- \OC::$server->getConfig()->setSystemValue('defaultapp', $defaultAppConfig);
- $this->assertEquals('http://localhost/' . $expectedPath, Dummy_OC_Util::getDefaultPageUrl());
-
- // restore old state
- \OC::$WEBROOT = $oldWebRoot;
- \OC::$server->getConfig()->setSystemValue('defaultapp', $oldDefaultApps);
- \OC_User::setUserId(null);
- }
-
- public function defaultAppsProvider() {
- return [
- // none specified, default to files
- [
- '',
- 'index.php/apps/files/',
- ['files'],
- ],
- // unexisting or inaccessible app specified, default to files
- [
- 'unexist',
- 'index.php/apps/files/',
- ['files'],
- ],
- // non-standard app
- [
- 'calendar',
- 'index.php/apps/calendar/',
- ['files', 'calendar'],
- ],
- // non-standard app with fallback
- [
- 'contacts,calendar',
- 'index.php/apps/calendar/',
- ['files', 'calendar'],
- ],
- ];
- }
-
- public function testGetDefaultPageUrlWithRedirectUrlWithoutFrontController() {
- putenv('front_controller_active=false');
- \OC::$server->getConfig()->deleteSystemValue('htaccess.IgnoreFrontController');
-
- $_REQUEST['redirect_url'] = 'myRedirectUrl.com';
- $this->assertSame('http://localhost'.\OC::$WEBROOT.'/myRedirectUrl.com', OC_Util::getDefaultPageUrl());
- }
-
- public function testGetDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController() {
- putenv('front_controller_active=false');
- \OC::$server->getConfig()->deleteSystemValue('htaccess.IgnoreFrontController');
-
- $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
- $this->assertSame('http://localhost'.\OC::$WEBROOT.'/index.php/apps/files/', OC_Util::getDefaultPageUrl());
- }
-
- public function testGetDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController() {
- putenv('front_controller_active=true');
- $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
- $this->assertSame('http://localhost'.\OC::$WEBROOT.'/apps/files/', OC_Util::getDefaultPageUrl());
- }
-
- public function testGetDefaultPageUrlWithRedirectUrlWithIgnoreFrontController() {
- putenv('front_controller_active=false');
- \OC::$server->getConfig()->setSystemValue('htaccess.IgnoreFrontController', true);
-
- $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
- $this->assertSame('http://localhost'.\OC::$WEBROOT.'/apps/files/', OC_Util::getDefaultPageUrl());
- }
-
- /**
* Test needUpgrade() when the core version is increased
*/
- public function testNeedUpgradeCore() {
- $config = \OC::$server->getConfig();
+ public function testNeedUpgradeCore(): void {
+ $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(\OCP\Util::needUpgrade());
+ $this->assertFalse(Util::needUpgrade());
$config->setSystemValue('version', '7.0.0.0');
- \OC::$server->getSession()->set('OC_Version', [7, 0, 0, 1]);
- self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
+ Server::get(ISession::class)->set('OC_Version', [7, 0, 0, 1]);
+ self::invokePrivate(new Util, 'needUpgradeCache', [null]);
- $this->assertTrue(\OCP\Util::needUpgrade());
+ $this->assertTrue(Util::needUpgrade());
$config->setSystemValue('version', $oldConfigVersion);
- \OC::$server->getSession()->set('OC_Version', $oldSessionVersion);
- self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
+ Server::get(ISession::class)->set('OC_Version', $oldSessionVersion);
+ self::invokePrivate(new Util, 'needUpgradeCache', [null]);
- $this->assertFalse(\OCP\Util::needUpgrade());
+ $this->assertFalse(Util::needUpgrade());
}
- public function testCheckDataDirectoryValidity() {
- $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
- touch($dataDir . '/.ocdata');
+ public function testCheckDataDirectoryValidity(): void {
+ $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);
@@ -351,69 +177,138 @@ class UtilTest extends \Test\TestCase {
protected function setUp(): void {
parent::setUp();
- \OC_Util::$scripts = [];
\OC_Util::$styles = [];
+ self::invokePrivate(Util::class, 'scripts', [[]]);
+ self::invokePrivate(Util::class, 'scriptDeps', [[]]);
}
protected function tearDown(): void {
parent::tearDown();
- \OC_Util::$scripts = [];
\OC_Util::$styles = [];
+ self::invokePrivate(Util::class, 'scripts', [[]]);
+ self::invokePrivate(Util::class, 'scriptDeps', [[]]);
}
- public function testAddScript() {
- \OC_Util::addScript('core', 'myFancyJSFile1');
- \OC_Util::addScript('myApp', 'myFancyJSFile2');
- \OC_Util::addScript('core', 'myFancyJSFile0', true);
- \OC_Util::addScript('core', 'myFancyJSFile10', true);
+ public function testAddScript(): void {
+ Util::addScript('first', 'myFirstJSFile');
+ Util::addScript('core', 'myFancyJSFile1');
+ Util::addScript('files', 'myFancyJSFile2', 'core');
+ Util::addScript('myApp5', 'myApp5JSFile', 'myApp2');
+ Util::addScript('myApp', 'myFancyJSFile3');
+ Util::addScript('core', 'myFancyJSFile4');
+ // after itself
+ Util::addScript('core', 'myFancyJSFile5', 'core');
// add duplicate
- \OC_Util::addScript('core', 'myFancyJSFile1');
-
- $this->assertEquals([
- 'core/js/myFancyJSFile10',
- 'core/js/myFancyJSFile0',
+ Util::addScript('core', 'myFancyJSFile1');
+ // dependency chain
+ Util::addScript('myApp4', 'myApp4JSFile', 'myApp3');
+ Util::addScript('myApp3', 'myApp3JSFile', 'myApp2');
+ Util::addScript('myApp2', 'myApp2JSFile', 'myApp');
+ Util::addScript('core', 'common');
+ Util::addScript('core', 'main');
+
+ $scripts = Util::getScripts();
+
+ // Core should appear first
+ $this->assertEquals(
+ 0,
+ array_search('core/js/common', $scripts, true)
+ );
+ $this->assertEquals(
+ 1,
+ array_search('core/js/main', $scripts, true)
+ );
+ $this->assertEquals(
+ 2,
+ array_search('core/js/myFancyJSFile1', $scripts, true)
+ );
+ $this->assertEquals(
+ 3,
+ array_search('core/js/myFancyJSFile4', $scripts, true)
+ );
+
+ // Dependencies should appear before their children
+ $this->assertLessThan(
+ array_search('files/js/myFancyJSFile2', $scripts, true),
+ array_search('core/js/myFancyJSFile3', $scripts, true)
+ );
+ $this->assertLessThan(
+ array_search('myApp2/js/myApp2JSFile', $scripts, true),
+ array_search('myApp/js/myFancyJSFile3', $scripts, true)
+ );
+ $this->assertLessThan(
+ array_search('myApp3/js/myApp3JSFile', $scripts, true),
+ array_search('myApp2/js/myApp2JSFile', $scripts, true)
+ );
+ $this->assertLessThan(
+ array_search('myApp4/js/myApp4JSFile', $scripts, true),
+ array_search('myApp3/js/myApp3JSFile', $scripts, true)
+ );
+ $this->assertLessThan(
+ array_search('myApp5/js/myApp5JSFile', $scripts, true),
+ array_search('myApp2/js/myApp2JSFile', $scripts, true)
+ );
+
+ // No duplicates
+ $this->assertEquals(
+ $scripts,
+ array_unique($scripts)
+ );
+
+ // All scripts still there
+ $scripts = [
+ 'core/js/common',
+ 'core/js/main',
'core/js/myFancyJSFile1',
+ 'core/js/myFancyJSFile4',
+ 'core/js/myFancyJSFile5',
+ 'first/l10n/en',
+ 'first/js/myFirstJSFile',
+ 'files/l10n/en',
+ 'files/js/myFancyJSFile2',
'myApp/l10n/en',
- 'myApp/js/myFancyJSFile2',
- ], \OC_Util::$scripts);
- $this->assertEquals([], \OC_Util::$styles);
+ 'myApp/js/myFancyJSFile3',
+ 'myApp2/l10n/en',
+ 'myApp2/js/myApp2JSFile',
+ 'myApp5/l10n/en',
+ 'myApp5/js/myApp5JSFile',
+ 'myApp3/l10n/en',
+ 'myApp3/js/myApp3JSFile',
+ 'myApp4/l10n/en',
+ 'myApp4/js/myApp4JSFile',
+ ];
+ foreach ($scripts as $script) {
+ $this->assertContains($script, $scripts);
+ }
}
- public function testAddVendorScript() {
- \OC_Util::addVendorScript('core', 'myFancyJSFile1');
- \OC_Util::addVendorScript('myApp', 'myFancyJSFile2');
- \OC_Util::addVendorScript('core', 'myFancyJSFile0', true);
- \OC_Util::addVendorScript('core', 'myFancyJSFile10', true);
- // add duplicate
- \OC_Util::addVendorScript('core', 'myFancyJSFile1');
+ public function testAddScriptCircularDependency(): void {
+ Util::addScript('circular', 'file1', 'dependency');
+ Util::addScript('dependency', 'file2', 'circular');
- $this->assertEquals([
- 'core/vendor/myFancyJSFile10',
- 'core/vendor/myFancyJSFile0',
- 'core/vendor/myFancyJSFile1',
- 'myApp/vendor/myFancyJSFile2',
- ], \OC_Util::$scripts);
- $this->assertEquals([], \OC_Util::$styles);
+ $scripts = Util::getScripts();
+ $this->assertContains('circular/js/file1', $scripts);
+ $this->assertContains('dependency/js/file2', $scripts);
}
- public function testAddTranslations() {
- \OC_Util::addTranslations('appId', 'de');
+ public function testAddTranslations(): void {
+ Util::addTranslations('appId', 'de');
$this->assertEquals([
'appId/l10n/de'
- ], \OC_Util::$scripts);
+ ], Util::getScripts());
$this->assertEquals([], \OC_Util::$styles);
}
- public function testAddStyle() {
- \OC_Util::addStyle('core', 'myFancyCSSFile1');
- \OC_Util::addStyle('myApp', 'myFancyCSSFile2');
- \OC_Util::addStyle('core', 'myFancyCSSFile0', true);
- \OC_Util::addStyle('core', 'myFancyCSSFile10', true);
+ public function testAddStyle(): void {
+ 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([], \OC_Util::$scripts);
+ $this->assertEquals([], Util::getScripts());
$this->assertEquals([
'core/css/myFancyCSSFile10',
'core/css/myFancyCSSFile0',
@@ -422,7 +317,7 @@ class UtilTest extends \Test\TestCase {
], \OC_Util::$styles);
}
- public function testAddVendorStyle() {
+ public function testAddVendorStyle(): void {
\OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
\OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2');
\OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true);
@@ -430,7 +325,7 @@ class UtilTest extends \Test\TestCase {
// add duplicate
\OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
- $this->assertEquals([], \OC_Util::$scripts);
+ $this->assertEquals([], Util::getScripts());
$this->assertEquals([
'core/vendor/myFancyCSSFile10',
'core/vendor/myFancyCSSFile0',
@@ -438,18 +333,87 @@ class UtilTest extends \Test\TestCase {
'myApp/vendor/myFancyCSSFile2',
], \OC_Util::$styles);
}
-}
-/**
- * Dummy OC Util class to make it possible to override the app manager
- */
-class Dummy_OC_Util extends OC_Util {
- /**
- * @var \OCP\App\IAppManager
- */
- public static $appManager;
+ public function testShortenMultibyteString(): void {
+ $this->assertEquals('Short nuff', Util::shortenMultibyteString('Short nuff', 255));
+ $this->assertEquals('ABC', Util::shortenMultibyteString('ABCDEF', 3));
+ // each of the characters is 12 bytes
+ $this->assertEquals('🙈', Util::shortenMultibyteString('🙈🙊🙉', 16, 2));
+ }
- protected static function getAppManager() {
- return self::$appManager;
+ #[\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);
+ }
+
}