Fixed tests

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2018-07-04 18:50:49 +02:00 committed by Julius Härtl
parent 59db5250f9
commit 14bc9ffda4
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
11 changed files with 194 additions and 25 deletions

View File

@ -72,7 +72,7 @@ class Calendar implements IFilter {
* @since 11.0.0 * @since 11.0.0
*/ */
public function getIcon() { public function getIcon() {
return $this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.svg')); return $this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar.svg'));
} }
/** /**

View File

@ -55,7 +55,7 @@ class CalendarTest extends TestCase {
public function testGetIcon() { public function testGetIcon() {
$this->url->expects($this->once()) $this->url->expects($this->once())
->method('imagePath') ->method('imagePath')
->with('core', 'places/calendar-dark.svg') ->with('core', 'places/calendar.svg')
->willReturn('path-to-icon'); ->willReturn('path-to-icon');
$this->url->expects($this->once()) $this->url->expects($this->once())

View File

@ -28,7 +28,7 @@ for SCSSFILE in core/css/*.scss
do do
FILE=$(basename $SCSSFILE) FILE=$(basename $SCSSFILE)
FILENAME="${FILE%.*}" FILENAME="${FILE%.*}"
printf "@import 'variables.scss'; @import '${FILE}';" | ./build/bin/node-sass --include-path core/css/ > tests/css/${FILE}.css printf "\$webroot:''; @import 'functions.scss'; @import 'variables.scss'; @import '${FILE}';" | ./build/bin/node-sass --include-path core/css/ > tests/css/${FILE}.css
done done
KARMA="$PREFIX/node_modules/karma/bin/karma" KARMA="$PREFIX/node_modules/karma/bin/karma"

View File

@ -32,9 +32,10 @@
*/ */
@mixin icon-color($icon, $dir, $color, $core: false) { @mixin icon-color($icon, $dir, $color, $core: false) {
// remove # from color // remove # from color
$index: str-index($color, '#'); // inspect cast int to string
$index: str-index(inspect($color), '#');
@if $index { @if $index {
$color: str-slice($color, 2); $color: str-slice(inspect($color), 2);
} }
$varName: "--icon-#{$icon}-#{$color}"; $varName: "--icon-#{$icon}-#{$color}";
@if $core { @if $core {

View File

@ -116,6 +116,7 @@ use OC\Share20\ProviderFactory;
use OC\Share20\ShareHelper; use OC\Share20\ShareHelper;
use OC\SystemTag\ManagerFactory as SystemTagManagerFactory; use OC\SystemTag\ManagerFactory as SystemTagManagerFactory;
use OC\Tagging\TagMapper; use OC\Tagging\TagMapper;
use OC\Template\IconsCacher;
use OC\Template\JSCombiner; use OC\Template\JSCombiner;
use OC\Template\SCSSCacher; use OC\Template\SCSSCacher;
use OCA\Theming\ImageManager; use OCA\Theming\ImageManager;
@ -963,7 +964,8 @@ class Server extends ServerContainer implements IServerContainer {
$c->getConfig(), $c->getConfig(),
$c->getThemingDefaults(), $c->getThemingDefaults(),
\OC::$SERVERROOT, \OC::$SERVERROOT,
$this->getMemCacheFactory() $this->getMemCacheFactory(),
$c->query(IconsCacher::class)
); );
}); });
$this->registerService(JSCombiner::class, function (Server $c) { $this->registerService(JSCombiner::class, function (Server $c) {

View File

@ -26,6 +26,7 @@ namespace OC\Template;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OC\Files\AppData\Factory; use OC\Files\AppData\Factory;
@ -85,10 +86,11 @@ class IconsCacher {
*/ */
public function setIconsCss(string $css) { public function setIconsCss(string $css) {
try { $cachedFile = $this->getCachedCSS();
$currentData = $this->folder->getFile($this->fileName)->getContent(); if (!$cachedFile) {
} catch (NotFoundException $e) {
$currentData = ''; $currentData = '';
} else {
$currentData = $cachedFile->getContent();
} }
// remove :root // remove :root
@ -102,16 +104,14 @@ class IconsCacher {
} }
if (strlen($data) > 0) { if (strlen($data) > 0) {
try { if (!$cachedFile) {
$cachedfile = $this->folder->getFile($this->fileName); $cachedFile = $this->folder->newFile($this->fileName);
} catch (NotFoundException $e) {
$cachedfile = $this->folder->newFile($this->fileName);
} }
$data = ":root { $data = ":root {
$data $data
}"; }";
$cachedfile->putContent($data); $cachedFile->putContent($data);
} }
return preg_replace($this->iconVarRE, '', $css); return preg_replace($this->iconVarRE, '', $css);

View File

@ -85,6 +85,7 @@ class SCSSCacher {
* @param \OC_Defaults $defaults * @param \OC_Defaults $defaults
* @param string $serverRoot * @param string $serverRoot
* @param ICacheFactory $cacheFactory * @param ICacheFactory $cacheFactory
* @param IconsCacher $iconsCacher
*/ */
public function __construct(ILogger $logger, public function __construct(ILogger $logger,
Factory $appDataFactory, Factory $appDataFactory,
@ -92,7 +93,8 @@ class SCSSCacher {
IConfig $config, IConfig $config,
\OC_Defaults $defaults, \OC_Defaults $defaults,
$serverRoot, $serverRoot,
ICacheFactory $cacheFactory) { ICacheFactory $cacheFactory,
IconsCacher $iconsCacher) {
$this->logger = $logger; $this->logger = $logger;
$this->appData = $appDataFactory->get('css'); $this->appData = $appDataFactory->get('css');
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
@ -101,12 +103,7 @@ class SCSSCacher {
$this->serverRoot = $serverRoot; $this->serverRoot = $serverRoot;
$this->cacheFactory = $cacheFactory; $this->cacheFactory = $cacheFactory;
$this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl())); $this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl()));
$this->iconsCacher = $iconsCacher;
$this->iconsCacher = new IconsCacher(
$this->logger,
$appDataFactory,
$this->urlGenerator
);
} }
/** /**

View File

@ -122,7 +122,7 @@ class ManagerTest extends TestCase {
['core', 'actions/settings-dark.svg', '1'], ['core', 'actions/settings-dark.svg', '1'],
['core', 'actions/share.svg', '2'], ['core', 'actions/share.svg', '2'],
['core', 'actions/password.svg', '3'], ['core', 'actions/password.svg', '3'],
['core', 'places/contacts-dark.svg', '5'], ['core', 'places/contacts.svg', '5'],
['settings', 'help.svg', '4'], ['settings', 'help.svg', '4'],
]); ]);
@ -175,7 +175,7 @@ class ManagerTest extends TestCase {
['core', 'actions/settings-dark.svg', '1'], ['core', 'actions/settings-dark.svg', '1'],
['core', 'actions/share.svg', '2'], ['core', 'actions/share.svg', '2'],
['core', 'actions/password.svg', '3'], ['core', 'actions/password.svg', '3'],
['core', 'places/contacts-dark.svg', '5'], ['core', 'places/contacts.svg', '5'],
['settings', 'help.svg', '4'], ['settings', 'help.svg', '4'],
]); ]);

View File

@ -31,6 +31,7 @@ use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IConfig; use OCP\IConfig;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OC\Template\IconsCacher;
use OC\Template\SCSSCacher; use OC\Template\SCSSCacher;
use OC\Template\CSSResourceLocator; use OC\Template\CSSResourceLocator;
@ -47,6 +48,8 @@ class CSSResourceLocatorTest extends \Test\TestCase {
protected $cacheFactory; protected $cacheFactory;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger; protected $logger;
/** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */
protected $iconsCacher;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
@ -57,6 +60,7 @@ class CSSResourceLocatorTest extends \Test\TestCase {
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class); $this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->themingDefaults = $this->createMock(ThemingDefaults::class); $this->themingDefaults = $this->createMock(ThemingDefaults::class);
$this->iconsCacher = $this->createMock(IconsCacher::class);
} }
private function cssResourceLocator() { private function cssResourceLocator() {
@ -70,7 +74,8 @@ class CSSResourceLocatorTest extends \Test\TestCase {
$this->config, $this->config,
$this->themingDefaults, $this->themingDefaults,
\OC::$SERVERROOT, \OC::$SERVERROOT,
$this->cacheFactory $this->cacheFactory,
$this->iconsCacher
); );
return new CSSResourceLocator( return new CSSResourceLocator(
$this->logger, $this->logger,

View File

@ -0,0 +1,126 @@
<?php
/**
* @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 OCA\Theming\ThemingDefaults;
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\ILogger;
use OCP\IURLGenerator;
use OC_App;
class IconsCacherTest extends \Test\TestCase {
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger;
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
protected $appData;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
protected $urlGenerator;
protected function setUp() {
$this->logger = $this->createMock(ILogger::class);
$this->appData = $this->createMock(AppData::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
);
}
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');
background-image: var(--icon-test);
}
";
$actual = self::invokePrivate($this->iconsCacher, 'getIconsFromCss', [$css]);
$expected = array(
'icon-test' => '/svg/core/actions/add/000'
);
$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('/svg/core/actions/add/000');
background-image: var(--icon-test);
}
";
$expected = "
icon.test {
background-image: var(--icon-test);
}
";
$iconsFile = $this->createMock(ISimpleFile::class);
$this->folder->expects($this->once())
->method('getFile')
->willReturn($iconsFile);
$actual = $this->iconsCacher->setIconsCss($css);
$this->assertEquals($expected, $actual);
}
}

View File

@ -26,6 +26,7 @@ namespace Test\Template;
use OC\Files\AppData\AppData; use OC\Files\AppData\AppData;
use OC\Files\AppData\Factory; use OC\Files\AppData\Factory;
use OC\Template\SCSSCacher; use OC\Template\SCSSCacher;
use OC\Template\IconsCacher;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
@ -55,11 +56,14 @@ class SCSSCacherTest extends \Test\TestCase {
protected $depsCache; protected $depsCache;
/** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */ /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $cacheFactory; protected $cacheFactory;
/** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */
protected $iconsCacher;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
$this->appData = $this->createMock(AppData::class); $this->appData = $this->createMock(AppData::class);
$this->iconsCacher = $this->createMock(IconsCacher::class);
/** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */ /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
$factory = $this->createMock(Factory::class); $factory = $this->createMock(Factory::class);
@ -80,6 +84,11 @@ class SCSSCacherTest extends \Test\TestCase {
$this->themingDefaults = $this->createMock(ThemingDefaults::class); $this->themingDefaults = $this->createMock(ThemingDefaults::class);
$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]); $this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
$iconsFile = $this->createMock(ISimpleFile::class);
$this->iconsCacher->expects($this->any())
->method('getCachedCSS')
->willReturn($iconsFile);
$this->scssCacher = new SCSSCacher( $this->scssCacher = new SCSSCacher(
$this->logger, $this->logger,
$factory, $factory,
@ -87,7 +96,8 @@ class SCSSCacherTest extends \Test\TestCase {
$this->config, $this->config,
$this->themingDefaults, $this->themingDefaults,
\OC::$SERVERROOT, \OC::$SERVERROOT,
$this->cacheFactory $this->cacheFactory,
$this->iconsCacher
); );
} }
@ -126,6 +136,10 @@ class SCSSCacherTest extends \Test\TestCase {
->method('getBaseUrl') ->method('getBaseUrl')
->willReturn('http://localhost/nextcloud'); ->willReturn('http://localhost/nextcloud');
$this->iconsCacher->expects($this->any())
->method('setIconsCss')
->willReturn('scss {}');
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core'); $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
$this->assertTrue($actual); $this->assertTrue($actual);
} }
@ -158,6 +172,10 @@ class SCSSCacherTest extends \Test\TestCase {
->with($filePrefix.'styles.css.deps') ->with($filePrefix.'styles.css.deps')
->willReturn($fileDeps); ->willReturn($fileDeps);
$this->iconsCacher->expects($this->any())
->method('setIconsCss')
->willReturn('scss {}');
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core'); $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
$this->assertTrue($actual); $this->assertTrue($actual);
} }
@ -185,6 +203,10 @@ class SCSSCacherTest extends \Test\TestCase {
$this->fail(); $this->fail();
})); }));
$this->iconsCacher->expects($this->any())
->method('setIconsCss')
->willReturn('scss {}');
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core'); $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
$this->assertTrue($actual); $this->assertTrue($actual);
} }
@ -220,6 +242,10 @@ class SCSSCacherTest extends \Test\TestCase {
$this->fail(); $this->fail();
})); }));
$this->iconsCacher->expects($this->any())
->method('setIconsCss')
->willReturn('scss {}');
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core'); $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
$this->assertTrue($actual); $this->assertTrue($actual);
} }
@ -276,6 +302,10 @@ class SCSSCacherTest extends \Test\TestCase {
throw new \Exception(); throw new \Exception();
})); }));
$this->iconsCacher->expects($this->any())
->method('setIconsCss')
->willReturn('scss {}');
$file->expects($this->once())->method('putContent'); $file->expects($this->once())->method('putContent');
$depsFile->expects($this->once())->method('putContent'); $depsFile->expects($this->once())->method('putContent');
$gzipFile->expects($this->once())->method('putContent'); $gzipFile->expects($this->once())->method('putContent');
@ -310,6 +340,10 @@ class SCSSCacherTest extends \Test\TestCase {
$depsFile->expects($this->once())->method('putContent'); $depsFile->expects($this->once())->method('putContent');
$gzipFile->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]); $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
$this->assertTrue($actual); $this->assertTrue($actual);
} }
@ -336,6 +370,10 @@ class SCSSCacherTest extends \Test\TestCase {
throw new \Exception(); 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( $file->expects($this->at(0))->method('putContent')->with($this->callback(
function ($content){ function ($content){
return 'body{background-color:#0082c9}' === $content; return 'body{background-color:#0082c9}' === $content;