2017-11-08 01:54:21 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Kyle Fazzari <kyrofa@ubuntu.com>
|
|
|
|
*
|
|
|
|
* @author Kyle Fazzari <kyrofa@ubuntu.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Template;
|
|
|
|
|
2020-10-15 16:59:21 +03:00
|
|
|
use OC\AppConfig;
|
2018-05-08 21:59:31 +03:00
|
|
|
use OC\Files\AppData\AppData;
|
2017-11-08 01:54:21 +03:00
|
|
|
use OC\Files\AppData\Factory;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Template\CSSResourceLocator;
|
|
|
|
use OC\Template\IconsCacher;
|
|
|
|
use OC\Template\SCSSCacher;
|
|
|
|
use OCA\Theming\ThemingDefaults;
|
2018-07-24 18:30:39 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2018-01-24 20:10:16 +03:00
|
|
|
use OCP\Files\IAppData;
|
2018-03-07 16:51:43 +03:00
|
|
|
use OCP\ICacheFactory;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IConfig;
|
2017-11-08 01:54:21 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class CSSResourceLocatorTest extends \Test\TestCase {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 01:54:21 +03:00
|
|
|
protected $appData;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 01:54:21 +03:00
|
|
|
protected $urlGenerator;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 01:54:21 +03:00
|
|
|
protected $config;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ThemingDefaults|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 01:54:21 +03:00
|
|
|
protected $themingDefaults;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-03-07 16:51:43 +03:00
|
|
|
protected $cacheFactory;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-08 01:54:21 +03:00
|
|
|
protected $logger;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IconsCacher|\PHPUnit\Framework\MockObject\MockObject */
|
2018-07-04 19:50:49 +03:00
|
|
|
protected $iconsCacher;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-07-24 18:30:39 +03:00
|
|
|
private $timeFactory;
|
2020-10-15 16:59:21 +03:00
|
|
|
/** @var AppConfig|\PHPUnit\Framework\MockObject\MockObject */
|
|
|
|
private $appConfig;
|
2017-11-08 01:54:21 +03:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2017-11-08 01:54:21 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2018-05-08 21:59:31 +03:00
|
|
|
$this->appData = $this->createMock(AppData::class);
|
2017-11-08 01:54:21 +03:00
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2018-03-07 16:51:43 +03:00
|
|
|
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
2017-11-08 01:54:21 +03:00
|
|
|
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
|
2018-07-04 19:50:49 +03:00
|
|
|
$this->iconsCacher = $this->createMock(IconsCacher::class);
|
2018-07-24 18:30:39 +03:00
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2020-10-15 16:59:21 +03:00
|
|
|
$this->appConfig = $this->createMock(AppConfig::class);
|
2017-11-08 01:54:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function cssResourceLocator() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
|
2020-04-10 15:19:56 +03:00
|
|
|
$factory = $this->createMock(Factory::class);
|
|
|
|
$factory->method('get')->with('css')->willReturn($this->appData);
|
2017-11-08 01:54:21 +03:00
|
|
|
$scssCacher = new SCSSCacher(
|
|
|
|
$this->logger,
|
|
|
|
$factory,
|
|
|
|
$this->urlGenerator,
|
|
|
|
$this->config,
|
|
|
|
$this->themingDefaults,
|
|
|
|
\OC::$SERVERROOT,
|
2018-07-04 19:50:49 +03:00
|
|
|
$this->cacheFactory,
|
2018-07-24 18:30:39 +03:00
|
|
|
$this->iconsCacher,
|
2020-10-15 16:59:21 +03:00
|
|
|
$this->timeFactory,
|
|
|
|
$this->appConfig
|
2017-11-08 01:54:21 +03:00
|
|
|
);
|
|
|
|
return new CSSResourceLocator(
|
|
|
|
$this->logger,
|
|
|
|
'theme',
|
2020-10-05 16:12:57 +03:00
|
|
|
['core' => 'map'],
|
|
|
|
['3rd' => 'party'],
|
2017-11-08 01:54:21 +03:00
|
|
|
$scssCacher
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:31:04 +03:00
|
|
|
private function rrmdir($directory) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$files = array_diff(scandir($directory), ['.','..']);
|
2017-11-08 19:31:04 +03:00
|
|
|
foreach ($files as $file) {
|
|
|
|
if (is_dir($directory . '/' . $file)) {
|
|
|
|
$this->rrmdir($directory . '/' . $file);
|
|
|
|
} else {
|
|
|
|
unlink($directory . '/' . $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rmdir($directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function randomString() {
|
|
|
|
return sha1(uniqid(mt_rand(), true));
|
|
|
|
}
|
|
|
|
|
2017-11-08 01:54:21 +03:00
|
|
|
public function testFindWithAppPathSymlink() {
|
|
|
|
// First create new apps path, and a symlink to it
|
2017-11-08 19:31:04 +03:00
|
|
|
$apps_dirname = $this->randomString();
|
2017-11-08 01:54:21 +03: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
|
2017-11-08 19:31:04 +03:00
|
|
|
mkdir($new_apps_path . '/' . 'test-css-app');
|
2017-11-08 01:54:21 +03:00
|
|
|
|
|
|
|
// Use the symlink as the app path
|
|
|
|
\OC::$APPSROOTS[] = [
|
2020-04-09 10:22:29 +03:00
|
|
|
'path' => $new_apps_path_symlink,
|
|
|
|
'url' => '/css-apps-test',
|
|
|
|
'writable' => false,
|
|
|
|
];
|
2017-11-08 01:54:21 +03:00
|
|
|
|
|
|
|
$locator = $this->cssResourceLocator();
|
2020-03-26 11:30:18 +03:00
|
|
|
$locator->find(['test-css-app/test-file']);
|
2017-11-08 01:54:21 +03:00
|
|
|
|
|
|
|
$resources = $locator->getResources();
|
|
|
|
$this->assertCount(1, $resources);
|
|
|
|
$resource = $resources[0];
|
|
|
|
$this->assertCount(3, $resource);
|
|
|
|
$root = $resource[0];
|
|
|
|
$webRoot = $resource[1];
|
|
|
|
$file = $resource[2];
|
|
|
|
|
2017-11-08 19:31:04 +03:00
|
|
|
$expectedRoot = $new_apps_path . '/test-css-app';
|
|
|
|
$expectedWebRoot = \OC::$WEBROOT . '/css-apps-test/test-css-app';
|
2017-11-08 01:54:21 +03: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 19:31:04 +03:00
|
|
|
array_pop(\OC::$APPSROOTS);
|
|
|
|
unlink($new_apps_path_symlink);
|
|
|
|
$this->rrmdir($new_apps_path);
|
2017-11-08 01:54:21 +03:00
|
|
|
}
|
|
|
|
}
|