aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Template
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Template')
-rw-r--r--tests/lib/Template/CSSResourceLocatorTest.php42
-rw-r--r--tests/lib/Template/IconsCacherTest.php149
-rw-r--r--tests/lib/Template/JSCombinerTest.php170
-rw-r--r--tests/lib/Template/JSResourceLocatorTest.php128
-rw-r--r--tests/lib/Template/ResourceLocatorTest.php50
-rw-r--r--tests/lib/Template/SCSSCacherTest.php560
-rw-r--r--tests/lib/Template/data/1.js.license2
-rw-r--r--tests/lib/Template/data/2.js.license2
-rw-r--r--tests/lib/Template/data/combine.json.license2
9 files changed, 200 insertions, 905 deletions
diff --git a/tests/lib/Template/CSSResourceLocatorTest.php b/tests/lib/Template/CSSResourceLocatorTest.php
index 4832b03fe9b..2ae37999b32 100644
--- a/tests/lib/Template/CSSResourceLocatorTest.php
+++ b/tests/lib/Template/CSSResourceLocatorTest.php
@@ -1,24 +1,8 @@
<?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/>.
- *
+ * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Template;
@@ -27,8 +11,6 @@ use OC\AppConfig;
use OC\Files\AppData\AppData;
use OC\Files\AppData\Factory;
use OC\Template\CSSResourceLocator;
-use OC\Template\IconsCacher;
-use OC\Template\SCSSCacher;
use OCA\Theming\ThemingDefaults;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
@@ -50,8 +32,6 @@ class CSSResourceLocatorTest extends \Test\TestCase {
protected $cacheFactory;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
protected $logger;
- /** @var IconsCacher|\PHPUnit\Framework\MockObject\MockObject */
- protected $iconsCacher;
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
private $timeFactory;
/** @var AppConfig|\PHPUnit\Framework\MockObject\MockObject */
@@ -66,7 +46,6 @@ class CSSResourceLocatorTest extends \Test\TestCase {
$this->config = $this->createMock(IConfig::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
- $this->iconsCacher = $this->createMock(IconsCacher::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->appConfig = $this->createMock(AppConfig::class);
}
@@ -75,24 +54,11 @@ class CSSResourceLocatorTest extends \Test\TestCase {
/** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
$factory = $this->createMock(Factory::class);
$factory->method('get')->with('css')->willReturn($this->appData);
- $scssCacher = new SCSSCacher(
- $this->logger,
- $factory,
- $this->urlGenerator,
- $this->config,
- $this->themingDefaults,
- \OC::$SERVERROOT,
- $this->cacheFactory,
- $this->iconsCacher,
- $this->timeFactory,
- $this->appConfig
- );
return new CSSResourceLocator(
$this->logger,
'theme',
['core' => 'map'],
['3rd' => 'party'],
- $scssCacher
);
}
@@ -112,7 +78,7 @@ class CSSResourceLocatorTest extends \Test\TestCase {
return sha1(uniqid(mt_rand(), true));
}
- public function testFindWithAppPathSymlink() {
+ public function testFindWithAppPathSymlink(): void {
// First create new apps path, and a symlink to it
$apps_dirname = $this->randomString();
$new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
diff --git a/tests/lib/Template/IconsCacherTest.php b/tests/lib/Template/IconsCacherTest.php
deleted file mode 100644
index 188c0596799..00000000000
--- a/tests/lib/Template/IconsCacherTest.php
+++ /dev/null
@@ -1,149 +0,0 @@
-<?php
-
-declare(strict_types = 1);
-/**
- * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
- *
- * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.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;
-
-use OC\Files\AppData\AppData;
-use OC\Files\AppData\Factory;
-use OC\Template\IconsCacher;
-use OCP\AppFramework\Utility\ITimeFactory;
-use OCP\Files\IAppData;
-use OCP\Files\SimpleFS\ISimpleFile;
-use OCP\Files\SimpleFS\ISimpleFolder;
-use OCP\IURLGenerator;
-use Psr\Log\LoggerInterface;
-
-class IconsCacherTest extends \Test\TestCase {
- /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
- protected $logger;
- /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
- protected $appData;
- /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
- protected $urlGenerator;
- /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
- private $timeFactory;
-
- protected function setUp(): void {
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->appData = $this->createMock(AppData::class);
- $this->timeFactory = $this->createMock(ITimeFactory::class);
-
- /** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
- $factory = $this->createMock(Factory::class);
- $factory->method('get')->with('css')->willReturn($this->appData);
-
- $this->folder = $this->createMock(ISimpleFolder::class);
- $this->appData->method('getFolder')->willReturn($this->folder);
-
- $this->urlGenerator = $this->createMock(IURLGenerator::class);
-
- $this->iconsCacher = new IconsCacher(
- $this->logger,
- $factory,
- $this->urlGenerator,
- $this->timeFactory
- );
- }
-
- public function testGetIconsFromEmptyCss() {
- $css = "
- icon.test {
- color: #aaa;
- }
- ";
- $icons = self::invokePrivate($this->iconsCacher, 'getIconsFromCss', [$css]);
- $this->assertTrue(empty($icons));
- }
-
- public function testGetIconsFromValidCss() {
- $css = "
- icon.test {
- --icon-test: url('/svg/core/actions/add/000?v=1');
- background-image: var(--icon-test);
- }
- ";
- $actual = self::invokePrivate($this->iconsCacher, 'getIconsFromCss', [$css]);
- $expected = [
- 'icon-test' => '/svg/core/actions/add/000?v=1'
- ];
- $this->assertEquals($expected, $actual);
- }
-
- public function testSetIconsFromEmptyCss() {
- $expected = "
- icon.test {
- color: #aaa;
- }
- ";
- $actual = $this->iconsCacher->setIconsCss($expected);
- $this->assertEquals($expected, $actual);
- }
-
- public function testSetIconsFromValidCss() {
- $css = "
- icon.test {
- --icon-test: url('/index.php/svg/core/actions/add?color=000&v=1');
- background-image: var(--icon-test);
- }
- ";
- $expected = "
- icon.test {
- \n background-image: var(--icon-test);
- }
- ";
-
- $iconsFile = $this->createMock(ISimpleFile::class);
- $this->folder->expects($this->exactly(2))
- ->method('getFile')
- ->willReturn($iconsFile);
-
- $actual = $this->iconsCacher->setIconsCss($css);
- $this->assertEquals($expected, $actual);
- }
-
- public function testSetIconsFromValidCssMultipleTimes() {
- $css = "
- icon.test {
- --icon-test: url('/index.php/svg/core/actions/add?color=000&v=1');
- background-image: var(--icon-test);
- }
- ";
- $expected = "
- icon.test {
- \n background-image: var(--icon-test);
- }
- ";
-
- $iconsFile = $this->createMock(ISimpleFile::class);
- $this->folder->expects($this->exactly(4))
- ->method('getFile')
- ->willReturn($iconsFile);
-
- $actual = $this->iconsCacher->setIconsCss($css);
- $actual = $this->iconsCacher->setIconsCss($actual);
- $actual = $this->iconsCacher->setIconsCss($actual);
- $this->assertEquals($expected, $actual);
- }
-}
diff --git a/tests/lib/Template/JSCombinerTest.php b/tests/lib/Template/JSCombinerTest.php
index 5c678840c63..bc286695bc7 100644
--- a/tests/lib/Template/JSCombinerTest.php
+++ b/tests/lib/Template/JSCombinerTest.php
@@ -1,24 +1,8 @@
<?php
+
/**
- * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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/>.
- *
+ * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Template;
@@ -32,7 +16,9 @@ use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICache;
use OCP\ICacheFactory;
+use OCP\ITempManager;
use OCP\IURLGenerator;
+use OCP\Server;
use Psr\Log\LoggerInterface;
class JSCombinerTest extends \Test\TestCase {
@@ -59,7 +45,7 @@ class JSCombinerTest extends \Test\TestCase {
$this->config = $this->createMock(SystemConfig::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->depsCache = $this->createMock(ICache::class);
- $this->cacheFactory->expects($this->at(0))
+ $this->cacheFactory->expects($this->atLeastOnce())
->method('createDistributed')
->willReturn($this->depsCache);
$this->logger = $this->createMock(LoggerInterface::class);
@@ -72,7 +58,7 @@ class JSCombinerTest extends \Test\TestCase {
);
}
- public function testProcessDebugMode() {
+ public function testProcessDebugMode(): void {
$this->config
->expects($this->once())
->method('getValue')
@@ -83,33 +69,27 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertFalse($actual);
}
- public function testProcessNotInstalled() {
- $this->config
- ->expects($this->at(0))
- ->method('getValue')
- ->with('debug')
- ->willReturn(false);
+ public function testProcessNotInstalled(): void {
$this->config
- ->expects($this->at(1))
+ ->expects($this->exactly(2))
->method('getValue')
- ->with('installed')
- ->willReturn(false);
+ ->willReturnMap([
+ ['debug', false],
+ ['installed', false]
+ ]);
$actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
$this->assertFalse($actual);
}
- public function testProcessUncachedFileNoAppDataFolder() {
- $this->config
- ->expects($this->at(0))
- ->method('getValue')
- ->with('debug')
- ->willReturn(false);
+ public function testProcessUncachedFileNoAppDataFolder(): void {
$this->config
- ->expects($this->at(1))
+ ->expects($this->exactly(2))
->method('getValue')
- ->with('installed')
- ->willReturn(true);
+ ->willReturnMap([
+ ['debug', '', false],
+ ['installed', '', true],
+ ]);
$folder = $this->createMock(ISimpleFolder::class);
$this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willThrowException(new NotFoundException());
$this->appData->expects($this->once())->method('newFolder')->with('awesomeapp')->willReturn($folder);
@@ -138,17 +118,14 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertTrue($actual);
}
- public function testProcessUncachedFile() {
- $this->config
- ->expects($this->at(0))
- ->method('getValue')
- ->with('debug')
- ->willReturn(false);
+ public function testProcessUncachedFile(): void {
$this->config
- ->expects($this->at(1))
+ ->expects($this->exactly(2))
->method('getValue')
- ->with('installed')
- ->willReturn(true);
+ ->willReturnMap([
+ ['debug', '', false],
+ ['installed', '', true],
+ ]);
$folder = $this->createMock(ISimpleFolder::class);
$this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder);
$file = $this->createMock(ISimpleFile::class);
@@ -175,17 +152,14 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertTrue($actual);
}
- public function testProcessCachedFile() {
- $this->config
- ->expects($this->at(0))
- ->method('getValue')
- ->with('debug')
- ->willReturn(false);
+ public function testProcessCachedFile(): void {
$this->config
- ->expects($this->at(1))
+ ->expects($this->exactly(2))
->method('getValue')
- ->with('installed')
- ->willReturn(true);
+ ->willReturnMap([
+ ['debug', '', false],
+ ['installed', '', true],
+ ]);
$folder = $this->createMock(ISimpleFolder::class);
$this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder);
$file = $this->createMock(ISimpleFile::class);
@@ -215,17 +189,14 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertTrue($actual);
}
- public function testProcessCachedFileMemcache() {
+ public function testProcessCachedFileMemcache(): void {
$this->config
- ->expects($this->at(0))
+ ->expects($this->exactly(2))
->method('getValue')
- ->with('debug')
- ->willReturn(false);
- $this->config
- ->expects($this->at(1))
- ->method('getValue')
- ->with('installed')
- ->willReturn(true);
+ ->willReturnMap([
+ ['debug', '', false],
+ ['installed', '', true],
+ ]);
$folder = $this->createMock(ISimpleFolder::class);
$this->appData->expects($this->once())
->method('getFolder')
@@ -255,7 +226,7 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertTrue($actual);
}
- public function testIsCachedNoDepsFile() {
+ public function testIsCachedNoDepsFile(): void {
$fileName = 'combine.json';
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
@@ -275,7 +246,7 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertFalse($actual);
}
- public function testIsCachedWithNotExistingFile() {
+ public function testIsCachedWithNotExistingFile(): void {
$fileName = 'combine.json';
$folder = $this->createMock(ISimpleFolder::class);
$folder->method('fileExists')
@@ -293,7 +264,7 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertFalse($actual);
}
- public function testIsCachedWithOlderMtime() {
+ public function testIsCachedWithOlderMtime(): void {
$fileName = 'combine.json';
$folder = $this->createMock(ISimpleFolder::class);
$folder->method('fileExists')
@@ -311,7 +282,7 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertFalse($actual);
}
- public function testIsCachedWithoutContent() {
+ public function testIsCachedWithoutContent(): void {
$fileName = 'combine.json';
$folder = $this->createMock(ISimpleFolder::class);
$folder->method('fileExists')
@@ -331,7 +302,7 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertFalse($actual);
}
- public function testCacheNoFile() {
+ public function testCacheNoFile(): void {
$fileName = 'combine.js';
$folder = $this->createMock(ISimpleFolder::class);
@@ -364,7 +335,7 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertTrue($actual);
}
- public function testCache() {
+ public function testCache(): void {
$fileName = 'combine.js';
$folder = $this->createMock(ISimpleFolder::class);
@@ -395,19 +366,25 @@ class JSCombinerTest extends \Test\TestCase {
$this->assertTrue($actual);
}
- public function testCacheNotPermittedException() {
+ public function testCacheNotPermittedException(): void {
$fileName = 'combine.js';
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$depsFile = $this->createMock(ISimpleFile::class);
+ $gzFile = $this->createMock(ISimpleFile::class);
$path = __DIR__ . '/data/';
- $folder->expects($this->at(0))->method('getFile')->with($fileName)->willReturn($file);
- $folder->expects($this->at(1))->method('getFile')->with($fileName . '.deps')->willReturn($depsFile);
+ $folder->expects($this->exactly(3))
+ ->method('getFile')
+ ->willReturnMap([
+ [$fileName, $file],
+ [$fileName . '.deps', $depsFile],
+ [$fileName . '.gzip', $gzFile]
+ ]);
- $file->expects($this->at(0))
+ $file->expects($this->once())
->method('putContent')
->with('var a = \'hello\';
@@ -417,21 +394,21 @@ var b = \'world\';
');
$depsFile
- ->expects($this->at(0))
+ ->expects($this->once())
->method('putContent')
->with($this->callback(
- function ($content) {
- $deps = json_decode($content, true);
- return array_key_exists(__DIR__ . '/data//1.js', $deps)
- && array_key_exists(__DIR__ . '/data//2.js', $deps);
- }))
+ function ($content) {
+ $deps = json_decode($content, true);
+ return array_key_exists(__DIR__ . '/data//1.js', $deps)
+ && array_key_exists(__DIR__ . '/data//2.js', $deps);
+ }))
->willThrowException(new NotPermittedException());
$actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
$this->assertFalse($actual);
}
- public function testCacheSuccess() {
+ public function testCacheSuccess(): void {
$fileName = 'combine.js';
$folder = $this->createMock(ISimpleFolder::class);
@@ -455,7 +432,7 @@ var b = \'world\';
}
);
- $file->expects($this->at(0))
+ $file->expects($this->once())
->method('putContent')
->with('var a = \'hello\';
@@ -464,13 +441,13 @@ var b = \'world\';
');
- $depsFile->expects($this->at(0))->method('putContent')->with($this->callback(
+ $depsFile->expects($this->once())->method('putContent')->with($this->callback(
function ($content) {
$deps = json_decode($content, true);
return array_key_exists(__DIR__ . '/data//1.js', $deps)
&& array_key_exists(__DIR__ . '/data//2.js', $deps);
}));
- $gzFile->expects($this->at(0))->method('putContent')->with($this->callback(
+ $gzFile->expects($this->once())->method('putContent')->with($this->callback(
function ($content) {
return gzdecode($content) === 'var a = \'hello\';
@@ -486,7 +463,7 @@ var b = \'world\';
$this->assertTrue($actual);
}
- public function dataGetCachedSCSS() {
+ public static function dataGetCachedSCSS(): array {
return [
['awesomeapp', 'core/js/foo.json', '/js/core/foo.js'],
['files', 'apps/files/js/foo.json', '/js/files/foo.js']
@@ -497,9 +474,9 @@ var b = \'world\';
* @param $appName
* @param $fileName
* @param $result
- * @dataProvider dataGetCachedSCSS
*/
- public function testGetCachedSCSS($appName, $fileName, $result) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataGetCachedSCSS')]
+ public function testGetCachedSCSS($appName, $fileName, $result): void {
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
->with('core.Js.getJs', [
@@ -512,9 +489,9 @@ var b = \'world\';
$this->assertEquals(substr($result, 1), $actual);
}
- public function testGetContent() {
+ public function testGetContent(): void {
// Create temporary file with some content
- $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest');
+ $tmpFile = Server::get(ITempManager::class)->getTemporaryFile('JSCombinerTest');
$pathInfo = pathinfo($tmpFile);
file_put_contents($tmpFile, json_encode(['/foo/bar/test', $pathInfo['dirname'] . '/js/mytest.js']));
$tmpFilePathArray = explode('/', $pathInfo['basename']);
@@ -527,16 +504,16 @@ var b = \'world\';
$this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename']));
}
- public function testGetContentInvalidJson() {
+ public function testGetContentInvalidJson(): void {
// Create temporary file with some content
- $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest');
+ $tmpFile = Server::get(ITempManager::class)->getTemporaryFile('JSCombinerTest');
$pathInfo = pathinfo($tmpFile);
file_put_contents($tmpFile, 'CertainlyNotJson');
$expected = [];
$this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename']));
}
- public function testResetCache() {
+ public function testResetCache(): void {
$file = $this->createMock(ISimpleFile::class);
$file->expects($this->once())
->method('delete');
@@ -550,9 +527,8 @@ var b = \'world\';
$this->cacheFactory->expects($this->once())
->method('createDistributed')
->willReturn($cache);
- $cache->expects($this->once())
- ->method('clear')
- ->with('');
+ $cache->expects($this->never())
+ ->method('clear');
$this->appData->expects($this->once())
->method('getDirectoryListing')
->willReturn([$folder]);
diff --git a/tests/lib/Template/JSResourceLocatorTest.php b/tests/lib/Template/JSResourceLocatorTest.php
index 7dedd4ad3c7..89ab8e66dd7 100644
--- a/tests/lib/Template/JSResourceLocatorTest.php
+++ b/tests/lib/Template/JSResourceLocatorTest.php
@@ -1,24 +1,8 @@
<?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/>.
- *
+ * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Template;
@@ -26,6 +10,8 @@ namespace Test\Template;
use OC\SystemConfig;
use OC\Template\JSCombiner;
use OC\Template\JSResourceLocator;
+use OCP\App\AppPathNotFoundException;
+use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\ICacheFactory;
use OCP\IURLGenerator;
@@ -42,6 +28,8 @@ class JSResourceLocatorTest extends \Test\TestCase {
protected $cacheFactory;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
protected $logger;
+ /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
+ protected $appManager;
protected function setUp(): void {
parent::setUp();
@@ -51,6 +39,7 @@ class JSResourceLocatorTest extends \Test\TestCase {
$this->config = $this->createMock(SystemConfig::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
+ $this->appManager = $this->createMock(IAppManager::class);
}
private function jsResourceLocator() {
@@ -63,10 +52,8 @@ class JSResourceLocatorTest extends \Test\TestCase {
);
return new JSResourceLocator(
$this->logger,
- 'theme',
- ['core' => 'map'],
- ['3rd' => 'party'],
- $jsCombiner
+ $jsCombiner,
+ $this->appManager,
);
}
@@ -86,26 +73,35 @@ class JSResourceLocatorTest extends \Test\TestCase {
return sha1(uniqid(mt_rand(), true));
}
- public function testFindWithAppPathSymlink() {
+ public function testFindWithAppPathSymlink(): void {
+ $appName = 'test-js-app';
+
// First create new apps path, and a symlink to it
$apps_dirname = $this->randomString();
$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);
+ $this->assertTrue((
+ mkdir($new_apps_path) && symlink($apps_dirname, $new_apps_path_symlink)
+ ), 'Setup of apps path failed');
// Create an app within that path
- mkdir($new_apps_path . '/' . 'test-js-app');
+ $this->assertTrue((
+ mkdir($new_apps_path . '/' . $appName) && touch($new_apps_path . '/' . $appName . '/' . 'test-file.js')
+ ), 'Setup of app within the new apps path failed');
// Use the symlink as the app path
- \OC::$APPSROOTS[] = [
- 'path' => $new_apps_path_symlink,
- 'url' => '/js-apps-test',
- 'writable' => false,
- ];
-
+ $this->appManager->expects($this->once())
+ ->method('getAppPath')
+ ->with($appName)
+ ->willReturn("$new_apps_path_symlink/$appName");
+ $this->appManager->expects($this->once())
+ ->method('getAppWebPath')
+ ->with($appName)
+ ->willReturn("/js-apps-test/$appName");
+
+ // Run the tests
$locator = $this->jsResourceLocator();
- $locator->find(['test-js-app/test-file']);
+ $locator->find(["$appName/test-file"]);
$resources = $locator->getResources();
$this->assertCount(1, $resources);
@@ -115,17 +111,73 @@ class JSResourceLocatorTest extends \Test\TestCase {
$webRoot = $resource[1];
$file = $resource[2];
- $expectedRoot = $new_apps_path . '/test-js-app';
- $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
- $expectedFile = 'test-file.js';
+ $expectedRoot = $new_apps_path;
+ $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test';
+ $expectedFile = $appName . '/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);
- array_pop(\OC::$APPSROOTS);
unlink($new_apps_path_symlink);
$this->rrmdir($new_apps_path);
}
+
+ public function testNotExistingTranslationHandledSilent(): void {
+ $this->appManager->expects($this->once())
+ ->method('getAppPath')
+ ->with('core')
+ ->willThrowException(new AppPathNotFoundException());
+ $this->appManager->expects($this->atMost(1))
+ ->method('getAppWebPath')
+ ->with('core')
+ ->willThrowException(new AppPathNotFoundException());
+ // Assert logger is not called
+ $this->logger->expects($this->never())
+ ->method('error');
+
+ // Run the tests
+ $locator = $this->jsResourceLocator();
+ $locator->find(['core/l10n/en.js']);
+
+ $resources = $locator->getResources();
+ $this->assertCount(0, $resources);
+ }
+
+ public function testFindModuleJSWithFallback(): void {
+ // First create new apps path, and a symlink to it
+ $apps_dirname = $this->randomString();
+ $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
+ mkdir($new_apps_path);
+
+ // Create an app within that path
+ mkdir("$new_apps_path/test-js-app");
+ touch("$new_apps_path/test-js-app/module.mjs");
+ touch("$new_apps_path/test-js-app/both.mjs");
+ touch("$new_apps_path/test-js-app/both.js");
+ touch("$new_apps_path/test-js-app/plain.js");
+
+ // Use the app path
+ $this->appManager->expects($this->any())
+ ->method('getAppPath')
+ ->with('test-js-app')
+ ->willReturn("$new_apps_path/test-js-app");
+
+ $locator = $this->jsResourceLocator();
+ $locator->find(['test-js-app/module', 'test-js-app/both', 'test-js-app/plain']);
+
+ $resources = $locator->getResources();
+ $this->assertCount(3, $resources);
+
+ $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
+ $expectedFiles = ['module.mjs', 'both.mjs', 'plain.js'];
+
+ for ($idx = 0; $idx++; $idx < 3) {
+ $this->assertEquals($expectedWebRoot, $resources[$idx][1]);
+ $this->assertEquals($expectedFiles[$idx], $resources[$idx][2]);
+ }
+
+ $this->rrmdir($new_apps_path);
+ }
}
diff --git a/tests/lib/Template/ResourceLocatorTest.php b/tests/lib/Template/ResourceLocatorTest.php
index 0cb28843a53..599c8391ade 100644
--- a/tests/lib/Template/ResourceLocatorTest.php
+++ b/tests/lib/Template/ResourceLocatorTest.php
@@ -1,13 +1,14 @@
<?php
+
/**
- * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
- * 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\Template;
+use OC\SystemConfig;
use OC\Template\ResourceNotFoundException;
use Psr\Log\LoggerInterface;
@@ -22,20 +23,23 @@ class ResourceLocatorTest extends \Test\TestCase {
/**
* @param string $theme
- * @param array $core_map
- * @param array $party_map
- * @param array $appsRoots
* @return \PHPUnit\Framework\MockObject\MockObject
*/
- public function getResourceLocator($theme, $core_map, $party_map, $appsRoots) {
+ public function getResourceLocator($theme) {
+ $systemConfig = $this->createMock(SystemConfig::class);
+ $systemConfig
+ ->expects($this->any())
+ ->method('getValue')
+ ->with('theme', '')
+ ->willReturn($theme);
+ $this->overwriteService(SystemConfig::class, $systemConfig);
return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
- [$this->logger, $theme, $core_map, $party_map, $appsRoots ],
+ [$this->logger],
'', true, true, true, []);
}
- public function testFind() {
- $locator = $this->getResourceLocator('theme',
- ['core' => 'map'], ['3rd' => 'party'], ['foo' => 'bar']);
+ public function testFind(): void {
+ $locator = $this->getResourceLocator('theme');
$locator->expects($this->once())
->method('doFind')
->with('foo');
@@ -46,17 +50,22 @@ class ResourceLocatorTest extends \Test\TestCase {
$locator->find(['foo']);
}
- public function testFindNotFound() {
+ 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())
->method('doFind')
->with('foo')
- ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
+ ->willThrowException(new ResourceNotFoundException('foo', 'map'));
$locator->expects($this->once())
->method('doFindTheme')
->with('foo')
- ->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
+ ->willThrowException(new ResourceNotFoundException('foo', 'map'));
$this->logger->expects($this->exactly(2))
->method('debug')
->with($this->stringContains('map/foo'));
@@ -64,9 +73,8 @@ class ResourceLocatorTest extends \Test\TestCase {
$locator->find(['foo']);
}
- public function testAppendIfExist() {
- $locator = $this->getResourceLocator('theme',
- [__DIR__ => 'map'], ['3rd' => 'party'], ['foo' => 'bar']);
+ public function testAppendIfExist(): void {
+ $locator = $this->getResourceLocator('theme');
/** @var \OC\Template\ResourceLocator $locator */
$method = new \ReflectionMethod($locator, 'appendIfExist');
$method->setAccessible(true);
@@ -75,11 +83,7 @@ class ResourceLocatorTest extends \Test\TestCase {
$resource1 = [__DIR__, 'webroot', basename(__FILE__)];
$this->assertEquals([$resource1], $locator->getResources());
- $method->invoke($locator, __DIR__, basename(__FILE__));
- $resource2 = [__DIR__, 'map', basename(__FILE__)];
- $this->assertEquals([$resource1, $resource2], $locator->getResources());
-
$method->invoke($locator, __DIR__, 'does-not-exist');
- $this->assertEquals([$resource1, $resource2], $locator->getResources());
+ $this->assertEquals([$resource1], $locator->getResources());
}
}
diff --git a/tests/lib/Template/SCSSCacherTest.php b/tests/lib/Template/SCSSCacherTest.php
deleted file mode 100644
index f7b7b3bb6d3..00000000000
--- a/tests/lib/Template/SCSSCacherTest.php
+++ /dev/null
@@ -1,560 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
- *
- * @author Julius Härtl <jus@bitgrid.net>
- *
- * @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;
-
-use OC\AppConfig;
-use OC\Files\AppData\AppData;
-use OC\Files\AppData\Factory;
-use OC\Template\IconsCacher;
-use OC\Template\SCSSCacher;
-use OCA\Theming\ThemingDefaults;
-use OCP\AppFramework\Utility\ITimeFactory;
-use OCP\Files\IAppData;
-use OCP\Files\NotFoundException;
-use OCP\Files\SimpleFS\ISimpleFile;
-use OCP\Files\SimpleFS\ISimpleFolder;
-use OCP\ICache;
-use OCP\ICacheFactory;
-use OCP\IConfig;
-use OCP\IURLGenerator;
-use Psr\Log\LoggerInterface;
-
-class SCSSCacherTest extends \Test\TestCase {
- /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
- protected $logger;
- /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
- protected $appData;
- /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
- protected $urlGenerator;
- /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
- protected $config;
- /** @var ThemingDefaults|\PHPUnit\Framework\MockObject\MockObject */
- protected $themingDefaults;
- /** @var SCSSCacher */
- protected $scssCacher;
- /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
- protected $depsCache;
- /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
- protected $isCachedCache;
- /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
- protected $cacheFactory;
- /** @var IconsCacher|\PHPUnit\Framework\MockObject\MockObject */
- protected $iconsCacher;
- /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
- protected $timeFactory;
- /** @var AppConfig|\PHPUnit\Framework\MockObject\MockObject */
- protected $appConfig;
-
- protected function setUp(): void {
- parent::setUp();
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->appData = $this->createMock(AppData::class);
- $this->iconsCacher = $this->createMock(IconsCacher::class);
- $this->timeFactory = $this->createMock(ITimeFactory::class);
-
- /** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
- $factory = $this->createMock(Factory::class);
- $factory->method('get')->with('css')->willReturn($this->appData);
-
- $this->urlGenerator = $this->createMock(IURLGenerator::class);
- $this->urlGenerator->expects($this->any())
- ->method('getBaseUrl')
- ->willReturn('http://localhost/nextcloud');
-
- $this->config = $this->createMock(IConfig::class);
- $this->config->expects($this->any())
- ->method('getAppValue')
- ->will($this->returnCallback(function ($appId, $configKey, $defaultValue) {
- return $defaultValue;
- }));
- $this->cacheFactory = $this->createMock(ICacheFactory::class);
- $this->depsCache = $this->createMock(ICache::class);
- $this->isCachedCache = $this->createMock(ICache::class);
- $this->cacheFactory
- ->method('createDistributed')
- ->withConsecutive()
- ->willReturnOnConsecutiveCalls(
- $this->depsCache,
- $this->isCachedCache,
- $this->createMock(ICache::class)
- );
-
- $this->themingDefaults = $this->createMock(ThemingDefaults::class);
- $this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
-
- $iconsFile = $this->createMock(ISimpleFile::class);
- $this->iconsCacher->expects($this->any())
- ->method('getCachedCSS')
- ->willReturn($iconsFile);
-
- $this->appConfig = $this->createMock(AppConfig::class);
-
- $this->scssCacher = new SCSSCacher(
- $this->logger,
- $factory,
- $this->urlGenerator,
- $this->config,
- $this->themingDefaults,
- \OC::$SERVERROOT,
- $this->cacheFactory,
- $this->iconsCacher,
- $this->timeFactory,
- $this->appConfig
- );
- }
-
- public function testProcessUncachedFileNoAppDataFolder() {
- $folder = $this->createMock(ISimpleFolder::class);
- $file = $this->createMock(ISimpleFile::class);
- $file->expects($this->any())->method('getSize')->willReturn(1);
-
- $this->appData->expects($this->once())->method('getFolder')->with('core')->willThrowException(new NotFoundException());
- $this->appData->expects($this->once())->method('newFolder')->with('core')->willReturn($folder);
- $this->appData->method('getDirectoryListing')->willReturn([]);
-
- $fileDeps = $this->createMock(ISimpleFile::class);
- $gzfile = $this->createMock(ISimpleFile::class);
- $filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
- substr(md5('http://localhost/nextcloud/index.php'), 0, 4) . '-';
-
- $folder->method('getFile')
- ->willReturnCallback(function ($path) use ($file, $gzfile, $filePrefix) {
- if ($path === $filePrefix.'styles.css') {
- return $file;
- } elseif ($path === $filePrefix.'styles.css.deps') {
- throw new NotFoundException();
- } elseif ($path === $filePrefix.'styles.css.gzip') {
- return $gzfile;
- } else {
- $this->fail();
- }
- });
- $folder->expects($this->once())
- ->method('newFile')
- ->with($filePrefix.'styles.css.deps')
- ->willReturn($fileDeps);
-
- $this->urlGenerator->expects($this->once())
- ->method('getBaseUrl')
- ->willReturn('http://localhost/nextcloud');
-
- $this->iconsCacher->expects($this->any())
- ->method('setIconsCss')
- ->willReturn('scss {}');
-
- $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
- $this->assertTrue($actual);
- }
-
- public function testProcessUncachedFile() {
- $folder = $this->createMock(ISimpleFolder::class);
- $this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
- $this->appData->method('getDirectoryListing')->willReturn([]);
- $file = $this->createMock(ISimpleFile::class);
- $file->expects($this->any())->method('getSize')->willReturn(1);
- $fileDeps = $this->createMock(ISimpleFile::class);
- $gzfile = $this->createMock(ISimpleFile::class);
- $filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
- substr(md5('http://localhost/nextcloud/index.php'), 0, 4) . '-';
-
- $folder->method('getFile')
- ->willReturnCallback(function ($path) use ($file, $gzfile, $filePrefix) {
- if ($path === $filePrefix.'styles.css') {
- return $file;
- } elseif ($path === $filePrefix.'styles.css.deps') {
- throw new NotFoundException();
- } elseif ($path === $filePrefix.'styles.css.gzip') {
- return $gzfile;
- } else {
- $this->fail();
- }
- });
- $folder->expects($this->once())
- ->method('newFile')
- ->with($filePrefix.'styles.css.deps')
- ->willReturn($fileDeps);
-
- $this->iconsCacher->expects($this->any())
- ->method('setIconsCss')
- ->willReturn('scss {}');
-
- $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
- $this->assertTrue($actual);
- }
-
- public function testProcessCachedFile() {
- $folder = $this->createMock(ISimpleFolder::class);
- $this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
- $this->appData->method('getDirectoryListing')->willReturn([]);
- $file = $this->createMock(ISimpleFile::class);
- $fileDeps = $this->createMock(ISimpleFile::class);
- $fileDeps->expects($this->any())->method('getSize')->willReturn(1);
- $gzFile = $this->createMock(ISimpleFile::class);
- $filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
- substr(md5('http://localhost/nextcloud/index.php'), 0, 4) . '-';
-
- $folder->method('getFile')
- ->willReturnCallback(function ($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
- if ($name === $filePrefix.'styles.css') {
- return $file;
- } elseif ($name === $filePrefix.'styles.css.deps') {
- return $fileDeps;
- } elseif ($name === $filePrefix.'styles.css.gzip') {
- return $gzFile;
- }
- $this->fail();
- });
-
- $this->iconsCacher->expects($this->any())
- ->method('setIconsCss')
- ->willReturn('scss {}');
-
- $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
- $this->assertTrue($actual);
- }
-
- public function testProcessCachedFileMemcache() {
- $folder = $this->createMock(ISimpleFolder::class);
- $this->appData->expects($this->once())
- ->method('getFolder')
- ->with('core')
- ->willReturn($folder);
- $folder->method('getName')
- ->willReturn('core');
- $this->appData->method('getDirectoryListing')->willReturn([]);
-
- $file = $this->createMock(ISimpleFile::class);
-
- $fileDeps = $this->createMock(ISimpleFile::class);
- $fileDeps->expects($this->any())->method('getSize')->willReturn(1);
-
- $gzFile = $this->createMock(ISimpleFile::class);
- $filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
- substr(md5('http://localhost/nextcloud/index.php'), 0, 4) . '-';
- $folder->method('getFile')
- ->willReturnCallback(function ($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
- if ($name === $filePrefix.'styles.css') {
- return $file;
- } elseif ($name === $filePrefix.'styles.css.deps') {
- return $fileDeps;
- } elseif ($name === $filePrefix.'styles.css.gzip') {
- return $gzFile;
- }
- $this->fail();
- });
-
- $this->iconsCacher->expects($this->any())
- ->method('setIconsCss')
- ->willReturn('scss {}');
-
- $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
- $this->assertTrue($actual);
- }
-
- public function testIsCachedNoFile() {
- $fileNameCSS = "styles.css";
- $folder = $this->createMock(ISimpleFolder::class);
-
- $folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willThrowException(new NotFoundException());
- $this->appData->expects($this->any())
- ->method('getFolder')
- ->willReturn($folder);
- $actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, 'core']);
- $this->assertFalse($actual);
- }
-
- public function testIsCachedNoDepsFile() {
- $fileNameCSS = "styles.css";
- $folder = $this->createMock(ISimpleFolder::class);
- $file = $this->createMock(ISimpleFile::class);
-
- $file->expects($this->once())->method('getSize')->willReturn(1);
- $folder->method('getFile')
- ->willReturnCallback(function ($path) use ($file) {
- if ($path === 'styles.css') {
- return $file;
- } elseif ($path === 'styles.css.deps') {
- throw new NotFoundException();
- } else {
- $this->fail();
- }
- });
-
- $this->appData->expects($this->any())
- ->method('getFolder')
- ->willReturn($folder);
- $actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, 'core']);
- $this->assertFalse($actual);
- }
- public function testCacheNoFile() {
- $fileNameCSS = "styles.css";
- $fileNameSCSS = "styles.scss";
- $folder = $this->createMock(ISimpleFolder::class);
- $file = $this->createMock(ISimpleFile::class);
- $depsFile = $this->createMock(ISimpleFile::class);
- $gzipFile = $this->createMock(ISimpleFile::class);
-
- $webDir = "core/css";
- $path = \OC::$SERVERROOT . '/core/css/';
-
- $folder->method('getFile')->willThrowException(new NotFoundException());
- $folder->method('newFile')->willReturnCallback(function ($fileName) use ($file, $depsFile, $gzipFile) {
- if ($fileName === 'styles.css') {
- return $file;
- } elseif ($fileName === 'styles.css.deps') {
- return $depsFile;
- } elseif ($fileName === 'styles.css.gzip') {
- return $gzipFile;
- }
- throw new \Exception();
- });
-
- $this->iconsCacher->expects($this->any())
- ->method('setIconsCss')
- ->willReturn('scss {}');
-
- $file->expects($this->once())->method('putContent');
- $depsFile->expects($this->once())->method('putContent');
- $gzipFile->expects($this->once())->method('putContent');
-
- $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
- $this->assertTrue($actual);
- }
-
- public function testCache() {
- $fileNameCSS = "styles.css";
- $fileNameSCSS = "styles.scss";
- $folder = $this->createMock(ISimpleFolder::class);
- $file = $this->createMock(ISimpleFile::class);
- $depsFile = $this->createMock(ISimpleFile::class);
- $gzipFile = $this->createMock(ISimpleFile::class);
-
- $webDir = "core/css";
- $path = \OC::$SERVERROOT;
-
- $folder->method('getFile')->willReturnCallback(function ($fileName) use ($file, $depsFile, $gzipFile) {
- if ($fileName === 'styles.css') {
- return $file;
- } elseif ($fileName === 'styles.css.deps') {
- return $depsFile;
- } elseif ($fileName === 'styles.css.gzip') {
- return $gzipFile;
- }
- throw new \Exception();
- });
-
- $file->expects($this->once())->method('putContent');
- $depsFile->expects($this->once())->method('putContent');
- $gzipFile->expects($this->once())->method('putContent');
-
- $this->iconsCacher->expects($this->any())
- ->method('setIconsCss')
- ->willReturn('scss {}');
-
- $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
- $this->assertTrue($actual);
- }
-
- public function testCacheSuccess() {
- $fileNameCSS = "styles-success.css";
- $fileNameSCSS = "../../tests/data/scss/styles-success.scss";
- $folder = $this->createMock(ISimpleFolder::class);
- $file = $this->createMock(ISimpleFile::class);
- $depsFile = $this->createMock(ISimpleFile::class);
- $gzipFile = $this->createMock(ISimpleFile::class);
-
- $webDir = "tests/data/scss";
- $path = \OC::$SERVERROOT . $webDir;
-
- $folder->method('getFile')->willReturnCallback(function ($fileName) use ($file, $depsFile, $gzipFile) {
- if ($fileName === 'styles-success.css') {
- return $file;
- } elseif ($fileName === 'styles-success.css.deps') {
- return $depsFile;
- } elseif ($fileName === 'styles-success.css.gzip') {
- return $gzipFile;
- }
- throw new \Exception();
- });
-
- $this->iconsCacher->expects($this->at(0))
- ->method('setIconsCss')
- ->willReturn('body{background-color:#0082c9}');
-
- $file->expects($this->at(0))->method('putContent')->with($this->callback(
- function ($content) {
- return 'body{background-color:#0082c9}' === $content;
- }));
- $depsFile->expects($this->at(0))->method('putContent')->with($this->callback(
- function ($content) {
- $deps = json_decode($content, true);
- return array_key_exists(\OC::$SERVERROOT . '/core/css/variables.scss', $deps)
- && array_key_exists(\OC::$SERVERROOT . '/tests/data/scss/styles-success.scss', $deps);
- }));
- $gzipFile->expects($this->at(0))->method('putContent')->with($this->callback(
- function ($content) {
- return gzdecode($content) === 'body{background-color:#0082c9}';
- }
- ));
-
- $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
- $this->assertTrue($actual);
- }
-
- public function testCacheFailure() {
- $fileNameCSS = "styles-error.css";
- $fileNameSCSS = "../../tests/data/scss/styles-error.scss";
- $folder = $this->createMock(ISimpleFolder::class);
- $file = $this->createMock(ISimpleFile::class);
- $depsFile = $this->createMock(ISimpleFile::class);
-
- $webDir = "/tests/data/scss";
- $path = \OC::$SERVERROOT . $webDir;
-
- $folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willReturn($file);
- $folder->expects($this->at(1))->method('getFile')->with($fileNameCSS . '.deps')->willReturn($depsFile);
-
- $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
- $this->assertFalse($actual);
- }
-
- public function dataRebaseUrls() {
- return [
- ['#id { background-image: url(\'../img/image.jpg\'); }','#id { background-image: url(\'/apps/files/css/../img/image.jpg\'); }'],
- ['#id { background-image: url("../img/image.jpg"); }','#id { background-image: url(\'/apps/files/css/../img/image.jpg\'); }'],
- ['#id { background-image: url(\'/img/image.jpg\'); }','#id { background-image: url(\'/img/image.jpg\'); }'],
- ['#id { background-image: url("http://example.com/test.jpg"); }','#id { background-image: url("http://example.com/test.jpg"); }'],
- ];
- }
-
- /**
- * @dataProvider dataRebaseUrls
- */
- public function testRebaseUrls($scss, $expected) {
- $webDir = '/apps/files/css';
- $actual = self::invokePrivate($this->scssCacher, 'rebaseUrls', [$scss, $webDir]);
- $this->assertEquals($expected, $actual);
- }
-
- public function dataGetCachedSCSS() {
- return [
- ['core', 'core/css/styles.scss', '/css/core/styles.css', \OC_Util::getVersionString()],
- ['files', 'apps/files/css/styles.scss', '/css/files/styles.css', \OC_App::getAppVersion('files')]
- ];
- }
-
- /**
- * @param $appName
- * @param $fileName
- * @param $result
- * @dataProvider dataGetCachedSCSS
- */
- public function testGetCachedSCSS($appName, $fileName, $result, $version) {
- $this->urlGenerator->expects($this->once())
- ->method('linkToRoute')
- ->with('core.Css.getCss', [
- 'fileName' => substr(md5($version), 0, 4) . '-' .
- substr(md5('http://localhost/nextcloud/index.php'), 0, 4) . '-styles.css',
- 'appName' => $appName,
- 'v' => 0,
- ])
- ->willReturn(\OC::$WEBROOT . $result);
- $actual = $this->scssCacher->getCachedSCSS($appName, $fileName);
- $this->assertEquals(substr($result, 1), $actual);
- }
-
- private function randomString() {
- return sha1(uniqid(mt_rand(), true));
- }
-
- private function rrmdir($directory) {
- $files = array_diff(scandir($directory), ['.','..']);
- foreach ($files as $file) {
- if (is_dir($directory . '/' . $file)) {
- $this->rrmdir($directory . '/' . $file);
- } else {
- unlink($directory . '/' . $file);
- }
- }
- return rmdir($directory);
- }
-
- public function dataGetWebDir() {
- return [
- // Root installation
- ['/http/core/css', 'core', '', '/http', '/core/css'],
- ['/http/apps/scss/css', 'scss', '', '/http', '/apps/scss/css'],
- ['/srv/apps2/scss/css', 'scss', '', '/http', '/apps2/scss/css'],
- // Sub directory install
- ['/http/nextcloud/core/css', 'core', '/nextcloud', '/http/nextcloud', '/nextcloud/core/css'],
- ['/http/nextcloud/apps/scss/css', 'scss', '/nextcloud', '/http/nextcloud', '/nextcloud/apps/scss/css'],
- ['/srv/apps2/scss/css', 'scss', '/nextcloud', '/http/nextcloud', '/apps2/scss/css']
- ];
- }
-
- /**
- * @param $path
- * @param $appName
- * @param $webRoot
- * @param $serverRoot
- * @dataProvider dataGetWebDir
- */
- public function testgetWebDir($path, $appName, $webRoot, $serverRoot, $correctWebDir) {
- $tmpDir = sys_get_temp_dir().'/'.$this->randomString();
- // Adding fake apps folder and create fake app install
- \OC::$APPSROOTS[] = [
- 'path' => $tmpDir.'/srv/apps2',
- 'url' => '/apps2',
- 'writable' => false
- ];
- mkdir($tmpDir.$path, 0777, true);
- $actual = self::invokePrivate($this->scssCacher, 'getWebDir', [$tmpDir.$path, $appName, $tmpDir.$serverRoot, $webRoot]);
- $this->assertEquals($correctWebDir, $actual);
- array_pop(\OC::$APPSROOTS);
- $this->rrmdir($tmpDir.$path);
- }
-
- public function testResetCache() {
- $file = $this->createMock(ISimpleFile::class);
- $file->expects($this->once())
- ->method('delete');
-
- $folder = $this->createMock(ISimpleFolder::class);
- $folder->expects($this->once())
- ->method('getDirectoryListing')
- ->willReturn([$file]);
-
- $this->depsCache->expects($this->once())
- ->method('clear')
- ->with('');
- $this->isCachedCache->expects($this->once())
- ->method('clear')
- ->with('');
- $this->appData->expects($this->once())
- ->method('getDirectoryListing')
- ->willReturn([$folder]);
-
- $this->scssCacher->resetCache();
- }
-}
diff --git a/tests/lib/Template/data/1.js.license b/tests/lib/Template/data/1.js.license
new file mode 100644
index 00000000000..dddb428772d
--- /dev/null
+++ b/tests/lib/Template/data/1.js.license
@@ -0,0 +1,2 @@
+SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
+SPDX-License-Identifier: AGPL-3.0-or-later
diff --git a/tests/lib/Template/data/2.js.license b/tests/lib/Template/data/2.js.license
new file mode 100644
index 00000000000..dddb428772d
--- /dev/null
+++ b/tests/lib/Template/data/2.js.license
@@ -0,0 +1,2 @@
+SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
+SPDX-License-Identifier: AGPL-3.0-or-later
diff --git a/tests/lib/Template/data/combine.json.license b/tests/lib/Template/data/combine.json.license
new file mode 100644
index 00000000000..dddb428772d
--- /dev/null
+++ b/tests/lib/Template/data/combine.json.license
@@ -0,0 +1,2 @@
+SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
+SPDX-License-Identifier: AGPL-3.0-or-later