2017-11-07 17:54:21 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2025-09-29 08:39:15 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-11-07 17:54:21 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-11-07 17:54:21 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Template;
|
|
|
|
|
|
2020-10-15 09:59:21 -04:00
|
|
|
use OC\AppConfig;
|
2018-05-08 14:59:31 -04:00
|
|
|
use OC\Files\AppData\AppData;
|
2017-11-07 17:54:21 -05:00
|
|
|
use OC\Files\AppData\Factory;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OC\Template\CSSResourceLocator;
|
|
|
|
|
use OCA\Theming\ThemingDefaults;
|
2025-09-29 08:39:15 -04:00
|
|
|
use OCP\App\IAppManager;
|
2018-07-24 11:30:39 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2018-01-24 12:10:16 -05:00
|
|
|
use OCP\Files\IAppData;
|
2018-03-07 08:51:43 -05:00
|
|
|
use OCP\ICacheFactory;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IConfig;
|
2017-11-07 17:54:21 -05:00
|
|
|
use OCP\IURLGenerator;
|
2025-09-29 08:39:15 -04:00
|
|
|
use OCP\Server;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2022-03-21 06:17:14 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-11-07 17:54:21 -05:00
|
|
|
|
|
|
|
|
class CSSResourceLocatorTest extends \Test\TestCase {
|
2025-09-29 08:39:15 -04:00
|
|
|
private IAppData&MockObject $appData;
|
|
|
|
|
private IURLGenerator&MockObject $urlGenerator;
|
|
|
|
|
private IConfig&MockObject $config;
|
|
|
|
|
private ThemingDefaults&MockObject $themingDefaults;
|
|
|
|
|
private ICacheFactory&MockObject $cacheFactory;
|
|
|
|
|
private LoggerInterface&MockObject $logger;
|
|
|
|
|
private ITimeFactory&MockObject $timeFactory;
|
|
|
|
|
private AppConfig&MockObject $appConfig;
|
2017-11-07 17:54:21 -05:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2017-11-07 17:54:21 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2022-03-21 06:17:14 -04:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2018-05-08 14:59:31 -04:00
|
|
|
$this->appData = $this->createMock(AppData::class);
|
2017-11-07 17:54:21 -05:00
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2018-03-07 08:51:43 -05:00
|
|
|
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
2017-11-07 17:54:21 -05:00
|
|
|
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
|
2018-07-24 11:30:39 -04:00
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2020-10-15 09:59:21 -04:00
|
|
|
$this->appConfig = $this->createMock(AppConfig::class);
|
2017-11-07 17:54:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function cssResourceLocator() {
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
|
2020-04-10 08:19:56 -04:00
|
|
|
$factory = $this->createMock(Factory::class);
|
|
|
|
|
$factory->method('get')->with('css')->willReturn($this->appData);
|
2017-11-07 17:54:21 -05:00
|
|
|
return new CSSResourceLocator(
|
|
|
|
|
$this->logger,
|
2025-09-29 08:39:15 -04:00
|
|
|
$this->createMock(IConfig::class),
|
|
|
|
|
Server::get(IAppManager::class),
|
2017-11-07 17:54:21 -05:00
|
|
|
'theme',
|
2020-10-05 09:12:57 -04:00
|
|
|
['core' => 'map'],
|
|
|
|
|
['3rd' => 'party'],
|
2017-11-07 17:54:21 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-08 11:31:04 -05:00
|
|
|
private function rrmdir($directory) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$files = array_diff(scandir($directory), ['.','..']);
|
2017-11-08 11:31:04 -05:00
|
|
|
foreach ($files as $file) {
|
|
|
|
|
if (is_dir($directory . '/' . $file)) {
|
|
|
|
|
$this->rrmdir($directory . '/' . $file);
|
|
|
|
|
} else {
|
|
|
|
|
unlink($directory . '/' . $file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rmdir($directory);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 08:39:15 -04:00
|
|
|
private function randomString(): string {
|
|
|
|
|
return sha1(random_bytes(10));
|
2017-11-08 11:31:04 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testFindWithAppPathSymlink(): void {
|
2017-11-07 17:54:21 -05:00
|
|
|
// First create new apps path, and a symlink to it
|
2017-11-08 11:31:04 -05:00
|
|
|
$apps_dirname = $this->randomString();
|
2017-11-07 17:54:21 -05:00
|
|
|
$new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
|
|
|
|
|
$new_apps_path_symlink = $new_apps_path . '_link';
|
|
|
|
|
mkdir($new_apps_path);
|
|
|
|
|
symlink($apps_dirname, $new_apps_path_symlink);
|
|
|
|
|
|
|
|
|
|
// Create an app within that path
|
2025-07-31 08:31:43 -04:00
|
|
|
mkdir($new_apps_path . '/' . 'test_css_app');
|
2017-11-07 17:54:21 -05:00
|
|
|
|
|
|
|
|
// Use the symlink as the app path
|
|
|
|
|
\OC::$APPSROOTS[] = [
|
2020-04-09 03:22:29 -04:00
|
|
|
'path' => $new_apps_path_symlink,
|
|
|
|
|
'url' => '/css-apps-test',
|
|
|
|
|
'writable' => false,
|
|
|
|
|
];
|
2017-11-07 17:54:21 -05:00
|
|
|
|
|
|
|
|
$locator = $this->cssResourceLocator();
|
2025-07-31 08:31:43 -04:00
|
|
|
$locator->find(['test_css_app/test-file']);
|
2017-11-07 17:54:21 -05:00
|
|
|
|
|
|
|
|
$resources = $locator->getResources();
|
|
|
|
|
$this->assertCount(1, $resources);
|
|
|
|
|
$resource = $resources[0];
|
|
|
|
|
$this->assertCount(3, $resource);
|
|
|
|
|
$root = $resource[0];
|
|
|
|
|
$webRoot = $resource[1];
|
|
|
|
|
$file = $resource[2];
|
|
|
|
|
|
2025-07-31 08:31:43 -04:00
|
|
|
$expectedRoot = $new_apps_path . '/test_css_app';
|
|
|
|
|
$expectedWebRoot = \OC::$WEBROOT . '/css-apps-test/test_css_app';
|
2017-11-07 17:54:21 -05:00
|
|
|
$expectedFile = 'test-file.css';
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expectedRoot, $root,
|
|
|
|
|
'Ensure the app path symlink is resolved into the real path');
|
|
|
|
|
$this->assertEquals($expectedWebRoot, $webRoot);
|
|
|
|
|
$this->assertEquals($expectedFile, $file);
|
|
|
|
|
|
2017-11-08 11:31:04 -05:00
|
|
|
array_pop(\OC::$APPSROOTS);
|
|
|
|
|
unlink($new_apps_path_symlink);
|
|
|
|
|
$this->rrmdir($new_apps_path);
|
2017-11-07 17:54:21 -05:00
|
|
|
}
|
|
|
|
|
}
|