2017-03-05 22:28:59 +00:00
|
|
|
<?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;
|
|
|
|
|
2018-05-08 18:59:31 +00:00
|
|
|
use OC\Files\AppData\AppData;
|
2017-02-17 15:42:07 +00:00
|
|
|
use OC\Files\AppData\Factory;
|
2017-03-05 22:28:59 +00:00
|
|
|
use OC\Template\SCSSCacher;
|
2018-07-04 16:50:49 +00:00
|
|
|
use OC\Template\IconsCacher;
|
2017-02-17 15:42:07 +00:00
|
|
|
use OCA\Theming\ThemingDefaults;
|
2017-03-05 22:28:59 +00:00
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2017-03-21 20:53:20 +00:00
|
|
|
use OCP\ICache;
|
2018-03-07 13:51:43 +00:00
|
|
|
use OCP\ICacheFactory;
|
2017-03-05 22:28:59 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
2018-03-27 08:21:54 +00:00
|
|
|
use OC_App;
|
2017-03-05 22:28:59 +00:00
|
|
|
|
|
|
|
class SCSSCacherTest 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;
|
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $config;
|
2017-02-17 15:42:07 +00:00
|
|
|
/** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $themingDefaults;
|
2017-03-05 22:28:59 +00:00
|
|
|
/** @var SCSSCacher */
|
|
|
|
protected $scssCacher;
|
2017-03-22 12:26:18 +00:00
|
|
|
/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $depsCache;
|
2018-03-07 13:51:43 +00:00
|
|
|
/** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $cacheFactory;
|
2018-07-04 16:50:49 +00:00
|
|
|
/** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $iconsCacher;
|
2017-03-05 22:28:59 +00:00
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2018-05-08 18:59:31 +00:00
|
|
|
$this->appData = $this->createMock(AppData::class);
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->iconsCacher = $this->createMock(IconsCacher::class);
|
2018-03-27 08:21:54 +00:00
|
|
|
|
2017-02-17 15:42:07 +00:00
|
|
|
/** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
|
|
|
|
$factory = $this->createMock(Factory::class);
|
|
|
|
$factory->method('get')->with('css')->willReturn($this->appData);
|
2018-03-27 08:21:54 +00:00
|
|
|
|
2017-03-05 22:28:59 +00:00
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
2018-03-27 08:21:54 +00:00
|
|
|
$this->urlGenerator->expects($this->any())
|
|
|
|
->method('getBaseUrl')
|
|
|
|
->willReturn('http://localhost/nextcloud');
|
|
|
|
|
2017-03-05 22:28:59 +00:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2018-03-07 13:51:43 +00:00
|
|
|
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
2017-03-22 12:26:18 +00:00
|
|
|
$this->depsCache = $this->createMock(ICache::class);
|
2018-03-07 13:51:43 +00:00
|
|
|
$this->cacheFactory->expects($this->at(0))
|
|
|
|
->method('createDistributed')
|
|
|
|
->willReturn($this->depsCache);
|
2018-03-27 08:21:54 +00:00
|
|
|
|
2017-02-17 15:42:07 +00:00
|
|
|
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
|
2018-03-27 08:21:54 +00:00
|
|
|
$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
|
|
|
|
|
2018-07-04 16:50:49 +00:00
|
|
|
$iconsFile = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->iconsCacher->expects($this->any())
|
|
|
|
->method('getCachedCSS')
|
|
|
|
->willReturn($iconsFile);
|
|
|
|
|
2017-03-05 22:28:59 +00:00
|
|
|
$this->scssCacher = new SCSSCacher(
|
|
|
|
$this->logger,
|
2017-02-17 15:42:07 +00:00
|
|
|
$factory,
|
2017-03-05 22:28:59 +00:00
|
|
|
$this->urlGenerator,
|
|
|
|
$this->config,
|
2017-02-17 15:42:07 +00:00
|
|
|
$this->themingDefaults,
|
2017-03-21 20:53:20 +00:00
|
|
|
\OC::$SERVERROOT,
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->cacheFactory,
|
|
|
|
$this->iconsCacher
|
2017-03-05 22:28:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
public function testProcessUncachedFileNoAppDataFolder() {
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$file->expects($this->any())->method('getSize')->willReturn(1);
|
2017-02-17 15:42:07 +00:00
|
|
|
|
|
|
|
$this->appData->expects($this->once())->method('getFolder')->with('core')->willThrowException(new NotFoundException());
|
|
|
|
$this->appData->expects($this->once())->method('newFolder')->with('core')->willReturn($folder);
|
2018-02-03 21:16:09 +00:00
|
|
|
$this->appData->method('getDirectoryListing')->willReturn([]);
|
2017-02-17 15:42:07 +00:00
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$fileDeps = $this->createMock(ISimpleFile::class);
|
2017-03-28 21:13:59 +00:00
|
|
|
$gzfile = $this->createMock(ISimpleFile::class);
|
2018-03-27 08:21:54 +00:00
|
|
|
$filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
|
|
|
|
substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
|
2017-03-22 12:26:18 +00:00
|
|
|
|
|
|
|
$folder->method('getFile')
|
2017-06-16 09:06:26 +00:00
|
|
|
->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
|
|
|
|
if ($path === $filePrefix.'styles.css') {
|
2017-03-22 12:26:18 +00:00
|
|
|
return $file;
|
2017-06-16 09:06:26 +00:00
|
|
|
} else if ($path === $filePrefix.'styles.css.deps') {
|
2017-03-22 12:26:18 +00:00
|
|
|
throw new NotFoundException();
|
2017-06-16 09:06:26 +00:00
|
|
|
} else if ($path === $filePrefix.'styles.css.gzip') {
|
2017-03-28 21:13:59 +00:00
|
|
|
return $gzfile;
|
2017-03-22 12:26:18 +00:00
|
|
|
} else {
|
|
|
|
$this->fail();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
$folder->expects($this->once())
|
|
|
|
->method('newFile')
|
2017-06-16 09:06:26 +00:00
|
|
|
->with($filePrefix.'styles.css.deps')
|
2017-03-22 12:26:18 +00:00
|
|
|
->willReturn($fileDeps);
|
|
|
|
|
2017-06-16 09:06:26 +00:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('getBaseUrl')
|
|
|
|
->willReturn('http://localhost/nextcloud');
|
|
|
|
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->iconsCacher->expects($this->any())
|
|
|
|
->method('setIconsCss')
|
|
|
|
->willReturn('scss {}');
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
2017-03-05 22:28:59 +00:00
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
public function testProcessUncachedFile() {
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
|
2018-02-03 21:16:09 +00:00
|
|
|
$this->appData->method('getDirectoryListing')->willReturn([]);
|
2017-03-20 11:31:29 +00:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$file->expects($this->any())->method('getSize')->willReturn(1);
|
|
|
|
$fileDeps = $this->createMock(ISimpleFile::class);
|
2017-03-28 21:13:59 +00:00
|
|
|
$gzfile = $this->createMock(ISimpleFile::class);
|
2018-03-27 08:21:54 +00:00
|
|
|
$filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
|
|
|
|
substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
|
2017-03-22 12:26:18 +00:00
|
|
|
|
|
|
|
$folder->method('getFile')
|
2017-06-16 09:06:26 +00:00
|
|
|
->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
|
|
|
|
if ($path === $filePrefix.'styles.css') {
|
2017-03-22 12:26:18 +00:00
|
|
|
return $file;
|
2017-06-16 09:06:26 +00:00
|
|
|
} else if ($path === $filePrefix.'styles.css.deps') {
|
2017-03-22 12:26:18 +00:00
|
|
|
throw new NotFoundException();
|
2017-06-16 09:06:26 +00:00
|
|
|
} else if ($path === $filePrefix.'styles.css.gzip') {
|
2017-03-28 21:13:59 +00:00
|
|
|
return $gzfile;
|
|
|
|
}else {
|
2017-03-22 12:26:18 +00:00
|
|
|
$this->fail();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
$folder->expects($this->once())
|
|
|
|
->method('newFile')
|
2017-06-16 09:06:26 +00:00
|
|
|
->with($filePrefix.'styles.css.deps')
|
2017-03-22 12:26:18 +00:00
|
|
|
->willReturn($fileDeps);
|
|
|
|
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->iconsCacher->expects($this->any())
|
|
|
|
->method('setIconsCss')
|
|
|
|
->willReturn('scss {}');
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
|
|
|
|
$this->assertTrue($actual);
|
2017-03-05 22:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 12:26:18 +00:00
|
|
|
public function testProcessCachedFile() {
|
2017-03-20 11:31:29 +00:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
|
2018-02-03 21:16:09 +00:00
|
|
|
$this->appData->method('getDirectoryListing')->willReturn([]);
|
2017-03-05 22:28:59 +00:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
$fileDeps = $this->createMock(ISimpleFile::class);
|
|
|
|
$fileDeps->expects($this->any())->method('getSize')->willReturn(1);
|
2017-02-17 15:42:07 +00:00
|
|
|
$gzFile = $this->createMock(ISimpleFile::class);
|
2018-03-27 08:21:54 +00:00
|
|
|
$filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
|
|
|
|
substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
|
2017-03-22 12:26:18 +00:00
|
|
|
|
|
|
|
$folder->method('getFile')
|
2017-06-16 09:06:26 +00:00
|
|
|
->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
|
|
|
|
if ($name === $filePrefix.'styles.css') {
|
2017-03-22 12:26:18 +00:00
|
|
|
return $file;
|
2017-06-16 09:06:26 +00:00
|
|
|
} else if ($name === $filePrefix.'styles.css.deps') {
|
2017-03-22 12:26:18 +00:00
|
|
|
return $fileDeps;
|
2017-06-16 09:06:26 +00:00
|
|
|
} else if ($name === $filePrefix.'styles.css.gzip') {
|
2017-02-17 15:42:07 +00:00
|
|
|
return $gzFile;
|
2017-03-22 12:26:18 +00:00
|
|
|
}
|
2017-02-17 15:42:07 +00:00
|
|
|
$this->fail();
|
2017-03-22 12:26:18 +00:00
|
|
|
}));
|
|
|
|
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->iconsCacher->expects($this->any())
|
|
|
|
->method('setIconsCss')
|
|
|
|
->willReturn('scss {}');
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
2017-03-22 12:26:18 +00:00
|
|
|
public function testProcessCachedFileMemcache() {
|
2017-03-20 11:31:29 +00:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2017-03-22 12:26:18 +00:00
|
|
|
$this->appData->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('core')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder->method('getName')
|
|
|
|
->willReturn('core');
|
2018-02-03 21:16:09 +00:00
|
|
|
$this->appData->method('getDirectoryListing')->willReturn([]);
|
2017-03-22 12:26:18 +00:00
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
2017-03-22 12:26:18 +00:00
|
|
|
|
2017-02-17 15:42:07 +00:00
|
|
|
$fileDeps = $this->createMock(ISimpleFile::class);
|
|
|
|
$fileDeps->expects($this->any())->method('getSize')->willReturn(1);
|
|
|
|
|
|
|
|
$gzFile = $this->createMock(ISimpleFile::class);
|
2017-12-14 10:28:59 +00:00
|
|
|
$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';
|
2018-03-27 08:21:54 +00:00
|
|
|
$filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
|
|
|
|
substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
|
2017-03-22 12:26:18 +00:00
|
|
|
$folder->method('getFile')
|
2017-06-16 09:06:26 +00:00
|
|
|
->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
|
|
|
|
if ($name === $filePrefix.'styles.css') {
|
2017-03-22 12:26:18 +00:00
|
|
|
return $file;
|
2017-06-16 09:06:26 +00:00
|
|
|
} else if ($name === $filePrefix.'styles.css.deps') {
|
2017-02-17 15:42:07 +00:00
|
|
|
return $fileDeps;
|
2017-06-16 09:06:26 +00:00
|
|
|
} else if ($name === $filePrefix.'styles.css.gzip') {
|
2017-02-17 15:42:07 +00:00
|
|
|
return $gzFile;
|
2017-03-22 12:26:18 +00:00
|
|
|
}
|
2017-02-17 15:42:07 +00:00
|
|
|
$this->fail();
|
2017-03-22 12:26:18 +00:00
|
|
|
}));
|
|
|
|
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->iconsCacher->expects($this->any())
|
|
|
|
->method('setIconsCss')
|
|
|
|
->willReturn('scss {}');
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsCachedNoFile() {
|
|
|
|
$fileNameCSS = "styles.css";
|
2017-03-05 22:28:59 +00:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
|
|
|
|
$folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willThrowException(new NotFoundException());
|
2017-03-22 12:26:18 +00:00
|
|
|
$actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, $folder]);
|
2017-03-20 11:31:29 +00:00
|
|
|
$this->assertFalse($actual);
|
2017-03-05 22:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
public function testIsCachedNoDepsFile() {
|
|
|
|
$fileNameCSS = "styles.css";
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
|
|
|
|
$file->expects($this->once())->method('getSize')->willReturn(1);
|
2017-03-22 12:26:18 +00:00
|
|
|
$folder->method('getFile')
|
|
|
|
->will($this->returnCallback(function($path) use ($file) {
|
|
|
|
if ($path === 'styles.css') {
|
|
|
|
return $file;
|
|
|
|
} else if ($path === 'styles.css.deps') {
|
|
|
|
throw new NotFoundException();
|
|
|
|
} else {
|
|
|
|
$this->fail();
|
|
|
|
}
|
|
|
|
}));
|
2017-03-20 11:31:29 +00:00
|
|
|
|
2017-03-22 12:26:18 +00:00
|
|
|
$actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, $folder]);
|
2017-03-20 11:31:29 +00:00
|
|
|
$this->assertFalse($actual);
|
|
|
|
}
|
|
|
|
public function testCacheNoFile() {
|
|
|
|
$fileNameCSS = "styles.css";
|
|
|
|
$fileNameSCSS = "styles.scss";
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2017-03-05 22:28:59 +00:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
$depsFile = $this->createMock(ISimpleFile::class);
|
2017-03-28 21:13:59 +00:00
|
|
|
$gzipFile = $this->createMock(ISimpleFile::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
|
|
|
|
$webDir = "core/css";
|
|
|
|
$path = \OC::$SERVERROOT . '/core/css/';
|
|
|
|
|
2017-03-28 21:13:59 +00:00
|
|
|
$folder->method('getFile')->willThrowException(new NotFoundException());
|
|
|
|
$folder->method('newFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
|
|
|
|
if ($fileName === 'styles.css') {
|
|
|
|
return $file;
|
|
|
|
} else if ($fileName === 'styles.css.deps') {
|
|
|
|
return $depsFile;
|
2017-03-29 06:11:51 +00:00
|
|
|
} else if ($fileName === 'styles.css.gzip') {
|
2017-03-28 21:13:59 +00:00
|
|
|
return $gzipFile;
|
|
|
|
}
|
|
|
|
throw new \Exception();
|
|
|
|
}));
|
2017-03-20 11:31:29 +00:00
|
|
|
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->iconsCacher->expects($this->any())
|
|
|
|
->method('setIconsCss')
|
|
|
|
->willReturn('scss {}');
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$file->expects($this->once())->method('putContent');
|
|
|
|
$depsFile->expects($this->once())->method('putContent');
|
2017-03-28 21:13:59 +00:00
|
|
|
$gzipFile->expects($this->once())->method('putContent');
|
2017-03-20 11:31:29 +00:00
|
|
|
|
|
|
|
$actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCache() {
|
|
|
|
$fileNameCSS = "styles.css";
|
|
|
|
$fileNameSCSS = "styles.scss";
|
2017-03-05 22:28:59 +00:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$depsFile = $this->createMock(ISimpleFile::class);
|
2017-03-28 21:13:59 +00:00
|
|
|
$gzipFile = $this->createMock(ISimpleFile::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
|
|
|
|
$webDir = "core/css";
|
|
|
|
$path = \OC::$SERVERROOT;
|
|
|
|
|
2017-03-28 21:13:59 +00:00
|
|
|
$folder->method('getFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
|
|
|
|
if ($fileName === 'styles.css') {
|
|
|
|
return $file;
|
|
|
|
} else if ($fileName === 'styles.css.deps') {
|
|
|
|
return $depsFile;
|
2017-03-29 06:11:51 +00:00
|
|
|
} else if ($fileName === 'styles.css.gzip') {
|
2017-03-28 21:13:59 +00:00
|
|
|
return $gzipFile;
|
|
|
|
}
|
|
|
|
throw new \Exception();
|
|
|
|
}));
|
2017-03-20 11:31:29 +00:00
|
|
|
|
|
|
|
$file->expects($this->once())->method('putContent');
|
|
|
|
$depsFile->expects($this->once())->method('putContent');
|
2017-03-28 21:13:59 +00:00
|
|
|
$gzipFile->expects($this->once())->method('putContent');
|
2017-03-20 11:31:29 +00:00
|
|
|
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->iconsCacher->expects($this->any())
|
|
|
|
->method('setIconsCss')
|
|
|
|
->willReturn('scss {}');
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
|
|
|
|
$this->assertTrue($actual);
|
2017-03-05 22:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
public function testCacheSuccess() {
|
|
|
|
$fileNameCSS = "styles-success.css";
|
|
|
|
$fileNameSCSS = "../../tests/data/scss/styles-success.scss";
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2017-03-05 22:28:59 +00:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
$depsFile = $this->createMock(ISimpleFile::class);
|
2017-03-28 21:13:59 +00:00
|
|
|
$gzipFile = $this->createMock(ISimpleFile::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
|
|
|
|
$webDir = "tests/data/scss";
|
|
|
|
$path = \OC::$SERVERROOT . $webDir;
|
|
|
|
|
2017-03-28 21:13:59 +00:00
|
|
|
$folder->method('getFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
|
|
|
|
if ($fileName === 'styles-success.css') {
|
|
|
|
return $file;
|
|
|
|
} else if ($fileName === 'styles-success.css.deps') {
|
|
|
|
return $depsFile;
|
2017-03-29 06:11:51 +00:00
|
|
|
} else if ($fileName === 'styles-success.css.gzip') {
|
2017-03-28 21:13:59 +00:00
|
|
|
return $gzipFile;
|
|
|
|
}
|
|
|
|
throw new \Exception();
|
|
|
|
}));
|
2017-03-20 11:31:29 +00:00
|
|
|
|
2018-07-04 16:50:49 +00:00
|
|
|
$this->iconsCacher->expects($this->at(0))
|
|
|
|
->method('setIconsCss')
|
|
|
|
->willReturn('body{background-color:#0082c9}');
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
$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);
|
|
|
|
}));
|
2017-03-28 21:13:59 +00:00
|
|
|
$gzipFile->expects($this->at(0))->method('putContent')->with($this->callback(
|
|
|
|
function ($content) {
|
|
|
|
return gzdecode($content) === 'body{background-color:#0082c9}';
|
|
|
|
}
|
|
|
|
));
|
2017-03-20 11:31:29 +00:00
|
|
|
|
|
|
|
$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";
|
2017-03-05 22:28:59 +00:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2017-03-20 11:31:29 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2018-01-26 18:56:13 +00:00
|
|
|
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) {
|
2017-12-27 10:01:27 +00:00
|
|
|
$webDir = '/apps/files/css';
|
2018-01-26 18:56:13 +00:00
|
|
|
$actual = self::invokePrivate($this->scssCacher, 'rebaseUrls', [$scss, $webDir]);
|
2017-03-20 11:31:29 +00:00
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataGetCachedSCSS() {
|
|
|
|
return [
|
2018-03-27 08:21:54 +00:00
|
|
|
['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')]
|
2017-03-20 11:31:29 +00:00
|
|
|
];
|
2017-03-05 22:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 11:31:29 +00:00
|
|
|
/**
|
|
|
|
* @param $appName
|
|
|
|
* @param $fileName
|
|
|
|
* @param $result
|
|
|
|
* @dataProvider dataGetCachedSCSS
|
|
|
|
*/
|
2018-03-27 08:21:54 +00:00
|
|
|
public function testGetCachedSCSS($appName, $fileName, $result, $version) {
|
2017-03-20 11:31:29 +00:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.Css.getCss', [
|
2018-03-27 08:21:54 +00:00
|
|
|
'fileName' => substr(md5($version), 0, 4) . '-' .
|
|
|
|
substr(md5('http://localhost/nextcloud'), 0, 4) . '-styles.css',
|
2017-03-20 11:31:29 +00:00
|
|
|
'appName' => $appName
|
|
|
|
])
|
2017-03-20 13:20:34 +00:00
|
|
|
->willReturn(\OC::$WEBROOT . $result);
|
2017-03-20 11:31:29 +00:00
|
|
|
$actual = $this->scssCacher->getCachedSCSS($appName, $fileName);
|
|
|
|
$this->assertEquals(substr($result, 1), $actual);
|
|
|
|
}
|
|
|
|
|
2017-12-28 12:32:45 +00:00
|
|
|
private function randomString() {
|
|
|
|
return sha1(uniqid(mt_rand(), true));
|
|
|
|
}
|
|
|
|
|
2017-12-28 12:41:38 +00:00
|
|
|
private function rrmdir($directory) {
|
|
|
|
$files = array_diff(scandir($directory), array('.','..'));
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (is_dir($directory . '/' . $file)) {
|
|
|
|
$this->rrmdir($directory . '/' . $file);
|
|
|
|
} else {
|
|
|
|
unlink($directory . '/' . $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rmdir($directory);
|
|
|
|
}
|
|
|
|
|
2017-12-28 13:19:13 +00:00
|
|
|
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']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-12-28 12:32:45 +00:00
|
|
|
/**
|
|
|
|
* @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);
|
2017-12-28 12:41:38 +00:00
|
|
|
$this->rrmdir($tmpDir.$path);
|
2017-12-28 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 11:41:21 +00:00
|
|
|
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]);
|
|
|
|
|
2018-03-07 13:51:43 +00:00
|
|
|
$cache = $this->createMock(ICache::class);
|
|
|
|
$this->cacheFactory->expects($this->once())
|
|
|
|
->method('createDistributed')
|
|
|
|
->willReturn($cache);
|
|
|
|
$cache->expects($this->once())
|
2018-02-03 11:41:21 +00:00
|
|
|
->method('clear')
|
|
|
|
->with('');
|
|
|
|
$this->appData->expects($this->once())
|
|
|
|
->method('getDirectoryListing')
|
|
|
|
->willReturn([$folder]);
|
|
|
|
|
|
|
|
$this->scssCacher->resetCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-21 20:53:20 +00:00
|
|
|
}
|