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;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\SystemConfig;
|
2017-11-08 01:54:21 +03:00
|
|
|
use OC\Template\JSCombiner;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Template\JSResourceLocator;
|
2017-11-08 01:54:21 +03:00
|
|
|
use OCP\Files\IAppData;
|
2018-03-07 16:51:43 +03:00
|
|
|
use OCP\ICacheFactory;
|
2017-11-08 01:54:21 +03:00
|
|
|
use OCP\ILogger;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IURLGenerator;
|
2017-11-08 01:54:21 +03:00
|
|
|
|
|
|
|
class JSResourceLocatorTest extends \Test\TestCase {
|
|
|
|
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $appData;
|
|
|
|
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $urlGenerator;
|
|
|
|
/** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $config;
|
2018-03-07 16:51:43 +03:00
|
|
|
/** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $cacheFactory;
|
2017-11-08 01:54:21 +03:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $logger;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2017-11-08 01:54:21 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->appData = $this->createMock(IAppData::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
$this->config = $this->createMock(SystemConfig::class);
|
2018-03-07 16:51:43 +03:00
|
|
|
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
2017-11-08 01:54:21 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function jsResourceLocator() {
|
|
|
|
$jsCombiner = new JSCombiner(
|
|
|
|
$this->appData,
|
|
|
|
$this->urlGenerator,
|
2018-03-07 16:51:43 +03:00
|
|
|
$this->cacheFactory,
|
2017-11-08 01:54:21 +03:00
|
|
|
$this->config,
|
|
|
|
$this->logger
|
|
|
|
);
|
|
|
|
return new JSResourceLocator(
|
|
|
|
$this->logger,
|
|
|
|
'theme',
|
2020-03-26 11:30:18 +03:00
|
|
|
['core'=>'map'],
|
|
|
|
['3rd'=>'party'],
|
2017-11-08 01:54:21 +03:00
|
|
|
$jsCombiner
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 testConstructor() {
|
|
|
|
$locator = $this->jsResourceLocator();
|
|
|
|
$this->assertAttributeEquals('theme', 'theme', $locator);
|
|
|
|
$this->assertAttributeEquals('core', 'serverroot', $locator);
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertAttributeEquals(['core'=>'map','3rd'=>'party'], 'mapping', $locator);
|
2017-11-08 01:54:21 +03:00
|
|
|
$this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator);
|
|
|
|
$this->assertAttributeEquals('map', 'webroot', $locator);
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertAttributeEquals([], 'resources', $locator);
|
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-js-app');
|
2017-11-08 01:54:21 +03:00
|
|
|
|
|
|
|
// Use the symlink as the app path
|
|
|
|
\OC::$APPSROOTS[] = [
|
|
|
|
'path' => $new_apps_path_symlink,
|
2017-11-08 19:31:04 +03:00
|
|
|
'url' => '/js-apps-test',
|
2017-11-08 01:54:21 +03:00
|
|
|
'writable' => false,
|
|
|
|
];
|
|
|
|
|
|
|
|
$locator = $this->jsResourceLocator();
|
2020-03-26 11:30:18 +03:00
|
|
|
$locator->find(['test-js-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-js-app';
|
|
|
|
$expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
|
2017-11-08 01:54:21 +03:00
|
|
|
$expectedFile = 'test-file.js';
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|