diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 8f95f1415a1e5..58bcde00400e5 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -4037,27 +4037,6 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/build/rector.php b/build/rector.php index 90b33bad6ff75..fe6952d2b2bd7 100644 --- a/build/rector.php +++ b/build/rector.php @@ -67,6 +67,7 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO $nextcloudDir . '/status.php', $nextcloudDir . '/version.php', $nextcloudDir . '/lib/private/Share20/ProviderFactory.php', + $nextcloudDir . '/lib/private/Template', $nextcloudDir . '/tests', // $nextcloudDir . '/config', // $nextcloudDir . '/lib', diff --git a/lib/private/L10N/L10N.php b/lib/private/L10N/L10N.php index 50db373a65dfc..16bdf88499359 100644 --- a/lib/private/L10N/L10N.php +++ b/lib/private/L10N/L10N.php @@ -117,7 +117,7 @@ public function n(string $text_singular, string $text_plural, int $count, array /** * Localization * @param string $type Type of localization - * @param \DateTime|int|string $data parameters for this localization + * @param \DateTime|int|string|null $data parameters for this localization * @param array $options * @return string|int|false * diff --git a/lib/private/Server.php b/lib/private/Server.php index 2804acbbb7964..d2c3ce778a99c 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -1048,7 +1048,7 @@ public function __construct($webRoot, \OC\Config $config) { $c->getAppDataDir('js'), $c->get(IURLGenerator::class), $this->get(ICacheFactory::class), - $c->get(SystemConfig::class), + $c->get(\OCP\IConfig::class), $c->get(LoggerInterface::class) ); }); diff --git a/lib/private/Template/Base.php b/lib/private/Template/Base.php index a13e67039602a..d683cd6520a2b 100644 --- a/lib/private/Template/Base.php +++ b/lib/private/Template/Base.php @@ -1,39 +1,32 @@ vars = [ 'cspNonce' => $cspNonce, 'requesttoken' => $requestToken, ]; - $this->l10n = $l10n; - $this->template = $template; - $this->theme = $theme; } /** diff --git a/lib/private/Template/CSSResourceLocator.php b/lib/private/Template/CSSResourceLocator.php index b501fd69874b1..59281a36e42f9 100644 --- a/lib/private/Template/CSSResourceLocator.php +++ b/lib/private/Template/CSSResourceLocator.php @@ -1,37 +1,48 @@ appendIfExist($this->serverroot, $style . '.css') - || $this->appendIfExist($this->serverroot, 'core/' . $style . '.css') + public function doFind(string $resource): void { + $parts = explode('/', $resource, 2); + if (count($parts) < 2) { + return; + } + [$app,$subpath] = $parts; + if ($this->appendIfExist($this->serverroot, $resource . '.css') + || $this->appendIfExist($this->serverroot, 'core/' . $resource . '.css') ) { return; } - $style = substr($style, strpos($style, '/') + 1); - $app_path = \OC_App::getAppPath($app); - $app_url = \OC_App::getAppWebPath($app); - - if ($app_path === false && $app_url === false) { + try { + $app_path = $this->appManager->getAppPath($app); + $app_url = $this->appManager->getAppWebPath($app); + } catch (AppPathNotFoundException $e) { $this->logger->error('Could not find resource {resource} to load', [ - 'resource' => $app . '/' . $style . '.css', + 'resource' => $app . '/' . $subpath . '.css', 'app' => 'cssresourceloader', + 'exception' => $e, ]); return; } @@ -41,24 +52,21 @@ public function doFind($style) { // turned into cwd. $app_path = realpath($app_path); - $this->append($app_path, $style . '.css', $app_url); + $this->append($app_path, $subpath . '.css', $app_url); } - /** - * @param string $style - */ - public function doFindTheme($style) { + public function doFindTheme(string $resource): void { $theme_dir = 'themes/' . $this->theme . '/'; - $this->appendIfExist($this->serverroot, $theme_dir . 'apps/' . $style . '.css') - || $this->appendIfExist($this->serverroot, $theme_dir . $style . '.css') - || $this->appendIfExist($this->serverroot, $theme_dir . 'core/' . $style . '.css'); + $this->appendIfExist($this->serverroot, $theme_dir . 'apps/' . $resource . '.css') + || $this->appendIfExist($this->serverroot, $theme_dir . $resource . '.css') + || $this->appendIfExist($this->serverroot, $theme_dir . 'core/' . $resource . '.css'); } - public function append($root, $file, $webRoot = null, $throw = true, $scss = false) { + public function append(string $root, string $file, ?string $webRoot = null, bool $throw = true, bool $scss = false): void { if (!$scss) { parent::append($root, $file, $webRoot, $throw); } else { - if (!$webRoot) { + if ($webRoot === null || $webRoot === '') { $webRoot = $this->findWebRoot($root); if ($webRoot === null) { diff --git a/lib/private/Template/JSCombiner.php b/lib/private/Template/JSCombiner.php index a94f822a4480a..4b963aa5e4a19 100644 --- a/lib/private/Template/JSCombiner.php +++ b/lib/private/Template/JSCombiner.php @@ -1,60 +1,39 @@ appData = $appData; - $this->urlGenerator = $urlGenerator; - $this->cacheFactory = $cacheFactory; + protected ICache $depsCache; + + public function __construct( + protected IAppData $appData, + protected IURLGenerator $urlGenerator, + protected ICacheFactory $cacheFactory, + protected IConfig $config, + protected LoggerInterface $logger, + ) { $this->depsCache = $this->cacheFactory->createDistributed('JS-' . md5($this->urlGenerator->getBaseUrl())); - $this->config = $config; - $this->logger = $logger; } - /** - * @param string $root - * @param string $file - * @param string $app - * @return bool - */ - public function process($root, $file, $app) { - if ($this->config->getValue('debug') || !$this->config->getValue('installed')) { + public function process(string $root, string $file, string $app): bool { + if ($this->config->getSystemValueBool('debug') || !$this->config->getSystemValueBool('installed')) { return false; } @@ -76,12 +55,7 @@ public function process($root, $file, $app) { return $this->cache($path, $fileName, $folder); } - /** - * @param string $fileName - * @param ISimpleFolder $folder - * @return bool - */ - protected function isCached($fileName, ISimpleFolder $folder) { + protected function isCached(string $fileName, ISimpleFolder $folder): bool { $fileName = str_replace('.json', '.js', $fileName); if (!$folder->fileExists($fileName)) { @@ -126,13 +100,7 @@ protected function isCached($fileName, ISimpleFolder $folder) { } } - /** - * @param string $path - * @param string $fileName - * @param ISimpleFolder $folder - * @return bool - */ - protected function cache($path, $fileName, ISimpleFolder $folder) { + protected function cache(string $path, string $fileName, ISimpleFolder $folder): bool { $deps = []; $fullPath = $path . '/' . $fileName; $data = json_decode(file_get_contents($fullPath)); @@ -183,12 +151,7 @@ protected function cache($path, $fileName, ISimpleFolder $folder) { } } - /** - * @param string $appName - * @param string $fileName - * @return string - */ - public function getCachedJS($appName, $fileName) { + public function getCachedJS(string $appName, string $fileName): string { $tmpfileLoc = explode('/', $fileName); $fileName = array_pop($tmpfileLoc); $fileName = str_replace('.json', '.js', $fileName); @@ -197,12 +160,9 @@ public function getCachedJS($appName, $fileName) { } /** - * @param string $root - * @param string $file * @return string[] */ - public function getContent($root, $file) { - /** @var array $data */ + public function getContent(string $root, string $file): array { $data = json_decode(file_get_contents($root . '/' . $file)); if (!is_array($data)) { return []; @@ -226,7 +186,7 @@ public function getContent($root, $file) { * * @throws NotFoundException */ - public function resetCache() { + public function resetCache(): void { $this->cacheFactory->createDistributed('JS-')->clear(); $appDirectory = $this->appData->getDirectoryListing(); foreach ($appDirectory as $folder) { diff --git a/lib/private/Template/JSConfigHelper.php b/lib/private/Template/JSConfigHelper.php index c6060415b6d93..42c0c178ebe64 100644 --- a/lib/private/Template/JSConfigHelper.php +++ b/lib/private/Template/JSConfigHelper.php @@ -141,8 +141,12 @@ public function getConfig(): string { $capabilities = $this->capabilitiesManager->getCapabilities(false, true); - $userFirstDay = $this->config->getUserValue($uid, 'core', AUserDataOCSController::USER_FIELD_FIRST_DAY_OF_WEEK, null); - $firstDay = (int)($userFirstDay ?? $this->l->l('firstday', null)); + $firstDay = $this->config->getUserValue($uid, 'core', AUserDataOCSController::USER_FIELD_FIRST_DAY_OF_WEEK, ''); + if ($firstDay === '') { + $firstDay = (int)$this->l->l('firstday', null); + } else { + $firstDay = (int)$firstDay; + } $config = [ /** @deprecated 30.0.0 - use files capabilities instead */ diff --git a/lib/private/Template/JSResourceLocator.php b/lib/private/Template/JSResourceLocator.php index a6d2d13a2ada4..fc35b6c31283b 100644 --- a/lib/private/Template/JSResourceLocator.php +++ b/lib/private/Template/JSResourceLocator.php @@ -1,36 +1,36 @@ jsCombiner = $JSCombiner; - $this->appManager = $appManager; + public function __construct( + LoggerInterface $logger, + IConfig $config, + protected JSCombiner $jsCombiner, + protected IAppManager $appManager, + ) { + parent::__construct($logger, $config); } - /** - * @param string $script - */ - public function doFind($script) { + public function doFind(string $resource): void { $theme_dir = 'themes/' . $this->theme . '/'; // Extracting the appId and the script file name - $app = substr($script, 0, strpos($script, '/')); - $scriptName = basename($script); + [$app] = explode('/', $resource, 2); + $scriptName = basename($resource); // Get the app root path $appRoot = $this->serverroot . '/apps/'; $appWebRoot = null; @@ -48,69 +48,65 @@ public function doFind($script) { // ignore } - if (str_contains($script, '/l10n/')) { + if (str_contains($resource, '/l10n/')) { // For language files we try to load them all, so themes can overwrite // single l10n strings without having to translate all of them. - $found = 0; - $found += $this->appendScriptIfExist($this->serverroot, 'core/' . $script); - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $script); - $found += $this->appendScriptIfExist($this->serverroot, $script); - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . $script); - $found += $this->appendScriptIfExist($appRoot, $script, $appWebRoot); - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $script); + $found = $this->appendScriptIfExist($this->serverroot, 'core/' . $resource) + || $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $resource) + || $this->appendScriptIfExist($this->serverroot, $resource) + || $this->appendScriptIfExist($this->serverroot, $theme_dir . $resource) + || $this->appendScriptIfExist($appRoot, $resource, $appWebRoot) + || $this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $resource); if ($found) { return; } - } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $script) - || $this->appendScriptIfExist($this->serverroot, $theme_dir . $script) - || $this->appendScriptIfExist($this->serverroot, $script) + } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $resource) + || $this->appendScriptIfExist($this->serverroot, $theme_dir . $resource) + || $this->appendScriptIfExist($this->serverroot, $resource) || $this->appendScriptIfExist($this->serverroot, $theme_dir . "dist/$app-$scriptName") || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName") - || $this->appendScriptIfExist($appRoot, $script, $appWebRoot) - || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script . '.json') - || $this->cacheAndAppendCombineJsonIfExist($appRoot, $script . '.json', $app) - || $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $script) - || $this->appendScriptIfExist($this->serverroot, 'core/' . $script) - || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir . "dist/core-$scriptName") - || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName"))) - || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/' . $script . '.json') + || $this->appendScriptIfExist($appRoot, $resource, $appWebRoot) + || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $resource . '.json') + || $this->cacheAndAppendCombineJsonIfExist($appRoot, $resource . '.json', $app) + || $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $resource) + || $this->appendScriptIfExist($this->serverroot, 'core/' . $resource) + || $this->appendScriptIfExist($this->serverroot, $theme_dir . "dist/core-$scriptName") + || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName") + || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/' . $resource . '.json') ) { return; } // missing translations files will be ignored - if (str_contains($script, '/l10n/')) { + if (str_contains($resource, '/l10n/')) { return; } $this->logger->error('Could not find resource {resource} to load', [ - 'resource' => $script . '.js', + 'resource' => $resource . '.js', 'app' => 'jsresourceloader', ]); } - /** - * @param string $script - */ - public function doFindTheme($script) { + public function doFindTheme(string $resource): void { } /** * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`) * @see appendIfExist() */ - protected function appendScriptIfExist(string $root, string $file, ?string $webRoot = null) { + protected function appendScriptIfExist(string $root, string $file, ?string $webRoot = null): bool { if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) { return $this->appendIfExist($root, $file . '.js', $webRoot); } return true; } - protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') { + protected function cacheAndAppendCombineJsonIfExist(string $root, string $file, string $app = 'core'): bool { if (is_file($root . '/' . $file)) { if ($this->jsCombiner->process($root, $file, $app)) { - $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false); + $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), null, false); } else { // Add all the files from the json $files = $this->jsCombiner->getContent($root, $file); diff --git a/lib/private/Template/ResourceLocator.php b/lib/private/Template/ResourceLocator.php index fa52f8e5c0d54..05024b20a93f9 100644 --- a/lib/private/Template/ResourceLocator.php +++ b/lib/private/Template/ResourceLocator.php @@ -1,56 +1,57 @@ logger = $logger; + public function __construct( + protected LoggerInterface $logger, + IConfig $config, + ) { $this->mapping = [ \OC::$SERVERROOT => \OC::$WEBROOT ]; $this->serverroot = \OC::$SERVERROOT; - $this->webroot = \OC::$WEBROOT; - $this->theme = \OC_Util::getTheme(); + + $this->theme = $config->getSystemValueString('theme', ''); + + if ($this->theme === '') { + if (is_dir(\OC::$SERVERROOT . '/themes/default')) { + $this->theme = 'default'; + } + } } - /** - * @param string $resource - */ - abstract public function doFind($resource); + abstract public function doFind(string $resource): void; - /** - * @param string $resource - */ - abstract public function doFindTheme($resource); + abstract public function doFindTheme(string $resource): void; /** * Finds the resources and adds them to the list - * - * @param array $resources */ - public function find($resources) { + public function find(array $resources): void { foreach ($resources as $resource) { try { $this->doFind($resource); } catch (ResourceNotFoundException $e) { - $resourceApp = substr($resource, 0, strpos($resource, '/')); + [$resourceApp] = explode('/', $resource, 2); $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); } } @@ -59,7 +60,7 @@ public function find($resources) { try { $this->doFindTheme($resource); } catch (ResourceNotFoundException $e) { - $resourceApp = substr($resource, 0, strpos($resource, '/')); + [$resourceApp] = explode('/', $resource, 2); $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); } } @@ -74,8 +75,8 @@ public function find($resources) { * @param string|null $webRoot base for path, default map $root to $webRoot * @return bool True if the resource was found, false otherwise */ - protected function appendIfExist($root, $file, $webRoot = null) { - if ($root !== false && is_file($root . '/' . $file)) { + protected function appendIfExist(string $root, string $file, ?string $webRoot = null): bool { + if (is_file($root . '/' . $file)) { $this->append($root, $file, $webRoot, false); return true; } @@ -95,10 +96,9 @@ protected function appendIfExist($root, $file, $webRoot = null) { * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a * valid web root * - * @param string $root * @return string|null The web root or null on failure */ - protected function findWebRoot($root) { + protected function findWebRoot(string $root): ?string { $webRoot = null; $tmpRoot = $root; @@ -135,15 +135,8 @@ protected function findWebRoot($root) { * @param bool $throw Throw an exception, when the route does not exist * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing */ - protected function append($root, $file, $webRoot = null, $throw = true) { - if (!is_string($root)) { - if ($throw) { - throw new ResourceNotFoundException($file, $webRoot); - } - return; - } - - if (!$webRoot) { + protected function append(string $root, string $file, ?string $webRoot = null, bool $throw = true): void { + if ($webRoot === null || $webRoot === '') { $webRoot = $this->findWebRoot($root); if ($webRoot === null) { @@ -166,9 +159,8 @@ protected function append($root, $file, $webRoot = null, $throw = true) { /** * Returns the list of all resources that should be loaded - * @return array */ - public function getResources() { + public function getResources(): array { return $this->resources; } } diff --git a/lib/private/Template/ResourceNotFoundException.php b/lib/private/Template/ResourceNotFoundException.php index e51dfb5cb8915..ca6756c1e7d1f 100644 --- a/lib/private/Template/ResourceNotFoundException.php +++ b/lib/private/Template/ResourceNotFoundException.php @@ -1,30 +1,24 @@ resource = $resource; - $this->webPath = $webPath; } - /** - * @return string - */ - public function getResourcePath() { + public function getResourcePath(): string { return $this->webPath . '/' . $this->resource; } } diff --git a/lib/private/Template/TemplateManager.php b/lib/private/Template/TemplateManager.php index 34da4deac7292..0f0af51798a1d 100644 --- a/lib/private/Template/TemplateManager.php +++ b/lib/private/Template/TemplateManager.php @@ -9,6 +9,7 @@ namespace OC\Template; +use OC\SystemConfig; use OCP\App\IAppManager; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\AppFramework\Http\TemplateResponse; @@ -79,7 +80,7 @@ public function printErrorPage(string $error_msg, string $hint = '', int $status $this->eventDispatcher->dispatchTyped($event); print($response->render()); } catch (\Throwable $e1) { - $logger = \OCP\Server::get(LoggerInterface::class); + $logger = Server::get(LoggerInterface::class); $logger->error('Rendering themed error page failed. Falling back to un-themed error page.', [ 'app' => 'core', 'exception' => $e1, @@ -112,8 +113,8 @@ public function printExceptionErrorPage(\Throwable $exception, int $statusCode = $debug = false; http_response_code($statusCode); try { - $debug = (bool)Server::get(\OC\SystemConfig::class)->getValue('debug', false); - $serverLogsDocumentation = Server::get(\OC\SystemConfig::class)->getValue('documentation_url.server_logs', ''); + $debug = (bool)Server::get(SystemConfig::class)->getValue('debug', false); + $serverLogsDocumentation = Server::get(SystemConfig::class)->getValue('documentation_url.server_logs', ''); $request = Server::get(IRequest::class); $content = $this->getTemplate('', 'exception', 'error', false); $content->assign('errorClass', get_class($exception)); diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index b37f919abe79b..6c1fe71e4e9c5 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -7,6 +7,7 @@ * SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-License-Identifier: AGPL-3.0-only */ + namespace OC; use bantu\IniGetWrapper\IniGetWrapper; @@ -42,8 +43,8 @@ class TemplateLayout { /** @var string[] */ private static array $cacheBusterCache = []; - public static ?CSSResourceLocator $cssLocator = null; - public static ?JSResourceLocator $jsLocator = null; + public ?CSSResourceLocator $cssLocator = null; + public ?JSResourceLocator $jsLocator = null; public function __construct( private IConfig $config, @@ -214,7 +215,7 @@ public function getPageTemplate(string $renderAs, string $appId): ITemplate { } // Add the js files - $jsFiles = self::findJavascriptFiles(Util::getScripts()); + $jsFiles = $this->findJavascriptFiles(Util::getScripts()); $page->assign('jsfiles', []); if ($this->config->getSystemValueBool('installed', false) && $renderAs != TemplateResponse::RENDER_AS_ERROR) { // this is on purpose outside of the if statement below so that the initial state is prefilled (done in the getConfig() call) @@ -265,12 +266,12 @@ public function getPageTemplate(string $renderAs, string $appId): ITemplate { && !preg_match('/^\/login/', $pathInfo) && $renderAs !== TemplateResponse::RENDER_AS_ERROR ) { - $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); + $cssFiles = $this->findStylesheetFiles(\OC_Util::$styles); } else { // If we ignore the scss compiler, // we need to load the guest css fallback Util::addStyle('guest'); - $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); + $cssFiles = $this->findStylesheetFiles(\OC_Util::$styles); } $page->assign('cssfiles', []); @@ -365,12 +366,12 @@ private function getVersionHashByPath(string $path): string|false { return self::$cacheBusterCache[$path]; } - public static function findStylesheetFiles(array $styles): array { - if (!self::$cssLocator) { - self::$cssLocator = \OCP\Server::get(CSSResourceLocator::class); + private function findStylesheetFiles(array $styles): array { + if ($this->cssLocator === null) { + $this->cssLocator = \OCP\Server::get(CSSResourceLocator::class); } - self::$cssLocator->find($styles); - return self::$cssLocator->getResources(); + $this->cssLocator->find($styles); + return $this->cssLocator->getResources(); } public function getAppNamefromPath(string $path): string|false { @@ -387,12 +388,12 @@ public function getAppNamefromPath(string $path): string|false { return false; } - public static function findJavascriptFiles(array $scripts): array { - if (!self::$jsLocator) { - self::$jsLocator = \OCP\Server::get(JSResourceLocator::class); + private function findJavascriptFiles(array $scripts): array { + if ($this->jsLocator === null) { + $this->jsLocator = \OCP\Server::get(JSResourceLocator::class); } - self::$jsLocator->find($scripts); - return self::$jsLocator->getResources(); + $this->jsLocator->find($scripts); + return $this->jsLocator->getResources(); } /** diff --git a/lib/private/legacy/OC_App.php b/lib/private/legacy/OC_App.php index c97c4f3d81eba..f18e6064bfa23 100644 --- a/lib/private/legacy/OC_App.php +++ b/lib/private/legacy/OC_App.php @@ -248,41 +248,6 @@ public static function findAppInDirectories(string $appId, bool $ignoreCache = f return Server::get(AppManager::class)->findAppInDirectories($appId, $ignoreCache); } - /** - * Get the directory for the given app. - * If the app is defined in multiple directories, the first one is taken. (false if not found) - * - * @psalm-taint-specialize - * - * @param string $appId - * @param bool $refreshAppPath should be set to true only during install/upgrade - * @return string|false - * @deprecated 11.0.0 use Server::get(IAppManager)->getAppPath() - */ - public static function getAppPath(string $appId, bool $refreshAppPath = false) { - try { - return Server::get(IAppManager::class)->getAppPath($appId, $refreshAppPath); - } catch (AppPathNotFoundException) { - return false; - } - } - - /** - * Get the path for the given app on the access - * If the app is defined in multiple directories, the first one is taken. (false if not found) - * - * @param string $appId - * @return string|false - * @deprecated 18.0.0 use Server::get(IAppManager)->getAppWebPath() - */ - public static function getAppWebPath(string $appId) { - try { - return Server::get(IAppManager::class)->getAppWebPath($appId); - } catch (AppPathNotFoundException) { - return false; - } - } - /** * get app's version based on it's path * @@ -462,7 +427,11 @@ public function listAllApps(): array { $info['level'] = self::supportedApp; } - $appPath = self::getAppPath($app); + try { + $appPath = $appManager->getAppPath($app); + } catch (AppPathNotFoundException) { + $appPath = false; + } if ($appPath !== false) { $appIcon = $appPath . '/img/' . $app . '.svg'; if (file_exists($appIcon)) { diff --git a/lib/public/IL10N.php b/lib/public/IL10N.php index 9f7dedc5ff258..c4a64e371749c 100644 --- a/lib/public/IL10N.php +++ b/lib/public/IL10N.php @@ -50,7 +50,7 @@ public function n(string $text_singular, string $text_plural, int $count, array /** * Localization * @param string $type Type of localization - * @param \DateTime|int|string $data parameters for this localization + * @param \DateTime|int|string|null $data parameters for this localization * @param array $options currently supports following options: * - 'width': handed into \Punic\Calendar::formatDate as second parameter * @return string|int|false diff --git a/tests/lib/Template/CSSResourceLocatorTest.php b/tests/lib/Template/CSSResourceLocatorTest.php index fc82c9c3e5728..2db9899d0f98f 100644 --- a/tests/lib/Template/CSSResourceLocatorTest.php +++ b/tests/lib/Template/CSSResourceLocatorTest.php @@ -1,5 +1,7 @@ method('get')->with('css')->willReturn($this->appData); return new CSSResourceLocator( $this->logger, + $this->createMock(IConfig::class), + Server::get(IAppManager::class), 'theme', ['core' => 'map'], ['3rd' => 'party'], @@ -74,8 +73,8 @@ private function rrmdir($directory) { return rmdir($directory); } - private function randomString() { - return sha1(uniqid(mt_rand(), true)); + private function randomString(): string { + return sha1(random_bytes(10)); } public function testFindWithAppPathSymlink(): void { diff --git a/tests/lib/Template/JSCombinerTest.php b/tests/lib/Template/JSCombinerTest.php index bc286695bc727..52ed7e7056caf 100644 --- a/tests/lib/Template/JSCombinerTest.php +++ b/tests/lib/Template/JSCombinerTest.php @@ -1,5 +1,7 @@ appData = $this->createMock(IAppData::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); - $this->config = $this->createMock(SystemConfig::class); + $this->config = $this->createMock(IConfig::class); $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->depsCache = $this->createMock(ICache::class); $this->cacheFactory->expects($this->atLeastOnce()) @@ -61,7 +58,7 @@ protected function setUp(): void { public function testProcessDebugMode(): void { $this->config ->expects($this->once()) - ->method('getValue') + ->method('getSystemValueBool') ->with('debug') ->willReturn(true); @@ -72,7 +69,7 @@ public function testProcessDebugMode(): void { public function testProcessNotInstalled(): void { $this->config ->expects($this->exactly(2)) - ->method('getValue') + ->method('getSystemValueBool') ->willReturnMap([ ['debug', false], ['installed', false] @@ -85,10 +82,10 @@ public function testProcessNotInstalled(): void { public function testProcessUncachedFileNoAppDataFolder(): void { $this->config ->expects($this->exactly(2)) - ->method('getValue') + ->method('getSystemValueBool') ->willReturnMap([ - ['debug', '', false], - ['installed', '', true], + ['debug', false], + ['installed', true], ]); $folder = $this->createMock(ISimpleFolder::class); $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willThrowException(new NotFoundException()); @@ -121,10 +118,10 @@ public function testProcessUncachedFileNoAppDataFolder(): void { public function testProcessUncachedFile(): void { $this->config ->expects($this->exactly(2)) - ->method('getValue') + ->method('getSystemValueBool') ->willReturnMap([ - ['debug', '', false], - ['installed', '', true], + ['debug', false], + ['installed', true], ]); $folder = $this->createMock(ISimpleFolder::class); $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder); @@ -155,10 +152,10 @@ public function testProcessUncachedFile(): void { public function testProcessCachedFile(): void { $this->config ->expects($this->exactly(2)) - ->method('getValue') + ->method('getSystemValueBool') ->willReturnMap([ - ['debug', '', false], - ['installed', '', true], + ['debug', false], + ['installed', true], ]); $folder = $this->createMock(ISimpleFolder::class); $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder); @@ -192,10 +189,10 @@ public function testProcessCachedFile(): void { public function testProcessCachedFileMemcache(): void { $this->config ->expects($this->exactly(2)) - ->method('getValue') + ->method('getSystemValueBool') ->willReturnMap([ - ['debug', '', false], - ['installed', '', true], + ['debug', false], + ['installed', true], ]); $folder = $this->createMock(ISimpleFolder::class); $this->appData->expects($this->once()) diff --git a/tests/lib/Template/JSResourceLocatorTest.php b/tests/lib/Template/JSResourceLocatorTest.php index 89ab8e66dd752..599346e03d8bd 100644 --- a/tests/lib/Template/JSResourceLocatorTest.php +++ b/tests/lib/Template/JSResourceLocatorTest.php @@ -1,5 +1,7 @@ appData = $this->createMock(IAppData::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); - $this->config = $this->createMock(SystemConfig::class); + $this->config = $this->createMock(IConfig::class); $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->logger = $this->createMock(LoggerInterface::class); $this->appManager = $this->createMock(IAppManager::class); } - private function jsResourceLocator() { + private function jsResourceLocator(): JSResourceLocator { $jsCombiner = new JSCombiner( $this->appData, $this->urlGenerator, @@ -52,6 +49,7 @@ private function jsResourceLocator() { ); return new JSResourceLocator( $this->logger, + $this->config, $jsCombiner, $this->appManager, ); @@ -69,8 +67,8 @@ private function rrmdir($directory) { return rmdir($directory); } - private function randomString() { - return sha1(uniqid(mt_rand(), true)); + private function randomString(): string { + return sha1(random_bytes(10)); } public function testFindWithAppPathSymlink(): void { diff --git a/tests/lib/Template/ResourceLocatorTest.php b/tests/lib/Template/ResourceLocatorTest.php index 599c8391adee8..25a0c2479d9ce 100644 --- a/tests/lib/Template/ResourceLocatorTest.php +++ b/tests/lib/Template/ResourceLocatorTest.php @@ -1,5 +1,7 @@ logger = $this->createMock(LoggerInterface::class); + $this->config = $this->createMock(IConfig::class); } - /** - * @param string $theme - * @return \PHPUnit\Framework\MockObject\MockObject - */ - public function getResourceLocator($theme) { - $systemConfig = $this->createMock(SystemConfig::class); - $systemConfig + public function getResourceLocator(string $theme): ResourceLocator&MockObject { + $this->config ->expects($this->any()) - ->method('getValue') + ->method('getSystemValueString') ->with('theme', '') ->willReturn($theme); - $this->overwriteService(SystemConfig::class, $systemConfig); - return $this->getMockForAbstractClass('OC\Template\ResourceLocator', - [$this->logger], + return $this->getMockForAbstractClass(ResourceLocator::class, + [$this->logger, $this->config], '', true, true, true, []); } @@ -46,16 +45,10 @@ public function testFind(): void { $locator->expects($this->once()) ->method('doFindTheme') ->with('foo'); - /** @var \OC\Template\ResourceLocator $locator */ $locator->find(['foo']); } public function testFindNotFound(): void { - $systemConfig = $this->createMock(SystemConfig::class); - $systemConfig->method('getValue') - ->with('theme', '') - ->willReturn('theme'); - $this->overwriteService(SystemConfig::class, $systemConfig); $locator = $this->getResourceLocator('theme', ['core' => 'map'], ['3rd' => 'party'], ['foo' => 'bar']); $locator->expects($this->once()) @@ -69,13 +62,11 @@ public function testFindNotFound(): void { $this->logger->expects($this->exactly(2)) ->method('debug') ->with($this->stringContains('map/foo')); - /** @var \OC\Template\ResourceLocator $locator */ $locator->find(['foo']); } public function testAppendIfExist(): void { $locator = $this->getResourceLocator('theme'); - /** @var \OC\Template\ResourceLocator $locator */ $method = new \ReflectionMethod($locator, 'appendIfExist'); $method->setAccessible(true); diff --git a/tests/lib/TemplateLayoutTest.php b/tests/lib/TemplateLayoutTest.php index ce5d2f6dd0bd2..918c06bebb746 100644 --- a/tests/lib/TemplateLayoutTest.php +++ b/tests/lib/TemplateLayoutTest.php @@ -44,7 +44,13 @@ protected function setUp(): void { } #[\PHPUnit\Framework\Attributes\DataProvider('dataVersionHash')] - public function testVersionHash($path, $file, $installed, $debug, $expected): void { + public function testVersionHash( + string|false $path, + string|false $file, + bool $installed, + bool $debug, + string $expected, + ): void { $this->appManager->expects(self::any()) ->method('getAppVersion') ->willReturnCallback(fn ($appId) => match ($appId) { @@ -59,8 +65,8 @@ public function testVersionHash($path, $file, $installed, $debug, $expected): vo $this->config->expects(self::atLeastOnce()) ->method('getSystemValueBool') ->willReturnMap([ - ['installed', false, $installed], - ['debug', false, $debug], + ['installed', $installed], + ['debug', $debug], ]); $this->config->expects(self::any()) ->method('getAppValue')