2017-03-24 13:00:07 +03:00
|
|
|
<?php
|
2017-03-25 17:25:06 +03:00
|
|
|
/**
|
|
|
|
* @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/>.
|
|
|
|
*
|
|
|
|
*/
|
2017-03-24 13:00:07 +03:00
|
|
|
namespace Test\Template;
|
|
|
|
|
2017-03-29 00:13:59 +03:00
|
|
|
use function foo\func;
|
2017-03-24 13:00:07 +03:00
|
|
|
use OC\SystemConfig;
|
|
|
|
use OC\Template\JSCombiner;
|
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\NotFoundException;
|
2017-03-25 17:25:06 +03:00
|
|
|
use OCP\Files\NotPermittedException;
|
2017-03-24 13:00:07 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
|
|
|
use OCP\ICache;
|
2018-03-07 16:51:43 +03:00
|
|
|
use OCP\ICacheFactory;
|
2017-07-13 23:31:07 +03:00
|
|
|
use OCP\ILogger;
|
2017-03-24 13:00:07 +03:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class JSCombinerTest extends \Test\TestCase {
|
|
|
|
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $appData;
|
|
|
|
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $urlGenerator;
|
2017-03-25 17:25:06 +03:00
|
|
|
/** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2017-03-24 13:00:07 +03:00
|
|
|
protected $config;
|
|
|
|
/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $depsCache;
|
2017-03-25 17:25:06 +03:00
|
|
|
/** @var JSCombiner */
|
|
|
|
protected $jsCombiner;
|
2017-07-13 23:31:07 +03:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $logger;
|
2018-03-07 16:51:43 +03:00
|
|
|
/** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $cacheFactory;
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->appData = $this->createMock(IAppData::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
$this->config = $this->createMock(SystemConfig::class);
|
2018-03-07 16:51:43 +03:00
|
|
|
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
2017-03-24 13:00:07 +03:00
|
|
|
$this->depsCache = $this->createMock(ICache::class);
|
2018-03-07 16:51:43 +03:00
|
|
|
$this->cacheFactory->expects($this->at(0))
|
|
|
|
->method('createDistributed')
|
|
|
|
->willReturn($this->depsCache);
|
2017-07-13 23:31:07 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2017-03-24 13:00:07 +03:00
|
|
|
$this->jsCombiner = new JSCombiner(
|
|
|
|
$this->appData,
|
|
|
|
$this->urlGenerator,
|
2018-03-07 16:51:43 +03:00
|
|
|
$this->cacheFactory,
|
2017-07-13 23:31:07 +03:00
|
|
|
$this->config,
|
|
|
|
$this->logger
|
|
|
|
);
|
2018-03-07 16:51:43 +03:00
|
|
|
|
2017-03-24 13:00:07 +03:00
|
|
|
}
|
|
|
|
|
2017-03-25 17:25:06 +03:00
|
|
|
public function testProcessDebugMode() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getValue')
|
|
|
|
->with('debug')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
|
|
|
|
$this->assertFalse($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testProcessNotInstalled() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getValue')
|
|
|
|
->with('debug')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getValue')
|
|
|
|
->with('installed')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
|
|
|
|
$this->assertFalse($actual);
|
|
|
|
}
|
|
|
|
|
2017-03-24 13:00:07 +03:00
|
|
|
public function testProcessUncachedFileNoAppDataFolder() {
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getValue')
|
|
|
|
->with('debug')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getValue')
|
|
|
|
->with('installed')
|
|
|
|
->willReturn(true);
|
2017-03-24 13:00:07 +03:00
|
|
|
$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);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzfile = $this->createMock(ISimpleFile::class);
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$fileDeps = $this->createMock(ISimpleFile::class);
|
|
|
|
|
|
|
|
$folder->method('getFile')
|
2017-03-29 00:13:59 +03:00
|
|
|
->will($this->returnCallback(function($path) use ($file, $gzfile) {
|
2017-03-24 13:00:07 +03:00
|
|
|
if ($path === 'combine.js') {
|
|
|
|
return $file;
|
2017-03-29 00:13:59 +03:00
|
|
|
} else if ($path === 'combine.js.deps') {
|
2017-03-24 13:00:07 +03:00
|
|
|
throw new NotFoundException();
|
2017-03-29 09:11:51 +03:00
|
|
|
} else if ($path === 'combine.js.gzip') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $gzfile;
|
2017-03-24 13:00:07 +03:00
|
|
|
}
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->fail();
|
2017-03-24 13:00:07 +03:00
|
|
|
}));
|
|
|
|
$folder->expects($this->once())
|
|
|
|
->method('newFile')
|
|
|
|
->with('combine.js.deps')
|
|
|
|
->willReturn($fileDeps);
|
|
|
|
|
|
|
|
$actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testProcessUncachedFile() {
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getValue')
|
|
|
|
->with('debug')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getValue')
|
|
|
|
->with('installed')
|
|
|
|
->willReturn(true);
|
2017-03-24 13:00:07 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$fileDeps = $this->createMock(ISimpleFile::class);
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzfile = $this->createMock(ISimpleFile::class);
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$folder->method('getFile')
|
2017-03-29 00:13:59 +03:00
|
|
|
->will($this->returnCallback(function($path) use ($file, $gzfile) {
|
2017-03-24 13:00:07 +03:00
|
|
|
if ($path === 'combine.js') {
|
|
|
|
return $file;
|
2017-03-29 00:13:59 +03:00
|
|
|
} else if ($path === 'combine.js.deps') {
|
2017-03-24 13:00:07 +03:00
|
|
|
throw new NotFoundException();
|
2017-03-29 09:11:51 +03:00
|
|
|
} else if ($path === 'combine.js.gzip') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $gzfile;
|
2017-03-24 13:00:07 +03:00
|
|
|
}
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->fail();
|
2017-03-24 13:00:07 +03:00
|
|
|
}));
|
|
|
|
$folder->expects($this->once())
|
|
|
|
->method('newFile')
|
|
|
|
->with('combine.js.deps')
|
|
|
|
->willReturn($fileDeps);
|
|
|
|
|
|
|
|
$actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testProcessCachedFile() {
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getValue')
|
|
|
|
->with('debug')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getValue')
|
|
|
|
->with('installed')
|
|
|
|
->willReturn(true);
|
2017-03-24 13:00:07 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
|
|
|
|
$fileDeps = $this->createMock(ISimpleFile::class);
|
|
|
|
|
|
|
|
$fileDeps->expects($this->once())->method('getContent')->willReturn('{}');
|
|
|
|
|
2018-03-07 16:04:21 +03:00
|
|
|
$folder->method('fileExists')
|
|
|
|
->with('combine.js')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2017-03-24 13:00:07 +03:00
|
|
|
$folder->method('getFile')
|
|
|
|
->will($this->returnCallback(function($path) use ($file, $fileDeps) {
|
|
|
|
if ($path === 'combine.js') {
|
|
|
|
return $file;
|
2017-03-25 17:25:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($path === 'combine.js.deps') {
|
2017-03-24 13:00:07 +03:00
|
|
|
return $fileDeps;
|
|
|
|
}
|
2018-03-07 16:04:21 +03:00
|
|
|
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->fail();
|
2017-03-24 13:00:07 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
$actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testProcessCachedFileMemcache() {
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getValue')
|
|
|
|
->with('debug')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getValue')
|
|
|
|
->with('installed')
|
|
|
|
->willReturn(true);
|
2017-03-24 13:00:07 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$this->appData->expects($this->once())
|
|
|
|
->method('getFolder')
|
|
|
|
->with('awesomeapp')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder->method('getName')
|
|
|
|
->willReturn('awesomeapp');
|
2018-03-07 16:04:21 +03:00
|
|
|
$folder->method('fileExists')
|
|
|
|
->with('combine.js')
|
|
|
|
->willReturn(true);
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
|
|
|
|
$this->depsCache->method('get')
|
|
|
|
->with('awesomeapp-combine.js.deps')
|
|
|
|
->willReturn('{}');
|
|
|
|
|
|
|
|
$folder->method('getFile')
|
|
|
|
->will($this->returnCallback(function($path) use ($file) {
|
|
|
|
if ($path === 'combine.js') {
|
|
|
|
return $file;
|
|
|
|
}
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->fail();
|
2017-03-24 13:00:07 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
$actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsCachedNoDepsFile() {
|
2017-03-25 17:25:06 +03:00
|
|
|
$fileName = 'combine.json';
|
2017-03-24 13:00:07 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
|
|
|
|
$folder->method('getFile')
|
|
|
|
->will($this->returnCallback(function($path) use ($file) {
|
|
|
|
if ($path === 'combine.js') {
|
|
|
|
return $file;
|
2017-03-25 17:25:06 +03:00
|
|
|
}
|
|
|
|
if ($path === 'combine.js.deps') {
|
2017-03-24 13:00:07 +03:00
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->fail();
|
2017-03-24 13:00:07 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
$actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
|
|
|
|
$this->assertFalse($actual);
|
|
|
|
}
|
2017-03-25 17:25:06 +03:00
|
|
|
|
|
|
|
public function testIsCachedWithNotExistingFile() {
|
|
|
|
$fileName = 'combine.json';
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2018-03-07 16:04:21 +03:00
|
|
|
$folder->method('fileExists')
|
|
|
|
->with('combine.js')
|
|
|
|
->willReturn(true);
|
2017-03-25 17:25:06 +03:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$folder->method('getFile')
|
|
|
|
->with('combine.js.deps')
|
|
|
|
->willReturn($file);
|
|
|
|
$file->expects($this->once())
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn(json_encode(['/etc/certainlynotexisting/file/ihope' => 10000]));
|
|
|
|
|
|
|
|
$actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
|
|
|
|
$this->assertFalse($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsCachedWithOlderMtime() {
|
|
|
|
$fileName = 'combine.json';
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2018-03-07 16:04:21 +03:00
|
|
|
$folder->method('fileExists')
|
|
|
|
->with('combine.js')
|
|
|
|
->willReturn(true);
|
2017-03-25 17:25:06 +03:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$folder->method('getFile')
|
|
|
|
->with('combine.js.deps')
|
|
|
|
->willReturn($file);
|
|
|
|
$file->expects($this->once())
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn(json_encode([__FILE__ => 1234]));
|
|
|
|
|
|
|
|
$actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
|
2017-07-13 23:31:07 +03:00
|
|
|
$this->assertFalse($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsCachedWithoutContent() {
|
|
|
|
$fileName = 'combine.json';
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
2018-03-07 16:04:21 +03:00
|
|
|
$folder->method('fileExists')
|
|
|
|
->with('combine.js')
|
|
|
|
->willReturn(true);
|
2017-07-13 23:31:07 +03:00
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$folder->method('getFile')
|
|
|
|
->with('combine.js.deps')
|
|
|
|
->willReturn($file);
|
|
|
|
$file->expects($this->once())
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn('');
|
|
|
|
$this->logger->expects($this->once())
|
|
|
|
->method('info')
|
|
|
|
->with('JSCombiner: deps file empty: combine.js.deps');
|
|
|
|
$actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->assertFalse($actual);
|
|
|
|
}
|
|
|
|
|
2017-03-24 13:00:07 +03:00
|
|
|
public function testCacheNoFile() {
|
2017-03-25 17:25:06 +03:00
|
|
|
$fileName = 'combine.js';
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$depsFile = $this->createMock(ISimpleFile::class);
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzFile = $this->createMock(ISimpleFile::class);
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$path = __DIR__ . '/data/';
|
|
|
|
|
2017-03-29 00:13:59 +03:00
|
|
|
$folder->method('getFile')->willThrowException(new NotFoundException());
|
|
|
|
|
|
|
|
$folder->method('newFile')->will($this->returnCallback(
|
|
|
|
function ($filename) use ($file, $depsFile, $gzFile) {
|
|
|
|
if ($filename === 'combine.js') {
|
|
|
|
return $file;
|
|
|
|
} else if ($filename === 'combine.js.deps') {
|
|
|
|
return $depsFile;
|
2017-03-29 09:11:51 +03:00
|
|
|
} else if ($filename === 'combine.js.gzip') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $gzFile;
|
|
|
|
}
|
|
|
|
$this->fail();
|
|
|
|
}
|
|
|
|
));
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$file->expects($this->once())->method('putContent');
|
|
|
|
$depsFile->expects($this->once())->method('putContent');
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzFile->expects($this->once())->method('putContent');
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCache() {
|
2017-03-25 17:25:06 +03:00
|
|
|
$fileName = 'combine.js';
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$depsFile = $this->createMock(ISimpleFile::class);
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzFile = $this->createMock(ISimpleFile::class);
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$path = __DIR__ . '/data/';
|
|
|
|
|
2017-03-29 00:13:59 +03:00
|
|
|
$folder->method('getFile')->will($this->returnCallback(
|
|
|
|
function ($filename) use ($file, $depsFile, $gzFile) {
|
|
|
|
if ($filename === 'combine.js') {
|
|
|
|
return $file;
|
|
|
|
} else if ($filename === 'combine.js.deps') {
|
|
|
|
return $depsFile;
|
2017-03-29 09:11:51 +03:00
|
|
|
} else if ($filename === 'combine.js.gzip') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $gzFile;
|
|
|
|
}
|
|
|
|
$this->fail();
|
|
|
|
}
|
|
|
|
));
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$file->expects($this->once())->method('putContent');
|
|
|
|
$depsFile->expects($this->once())->method('putContent');
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzFile->expects($this->once())->method('putContent');
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
2017-03-25 17:25:06 +03:00
|
|
|
public function testCacheNotPermittedException() {
|
|
|
|
$fileName = 'combine.js';
|
|
|
|
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$depsFile = $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);
|
|
|
|
|
|
|
|
$file->expects($this->at(0))
|
|
|
|
->method('putContent')
|
|
|
|
->with('var a = \'hello\';
|
|
|
|
|
|
|
|
|
|
|
|
var b = \'world\';
|
|
|
|
|
|
|
|
|
|
|
|
');
|
|
|
|
$depsFile
|
|
|
|
->expects($this->at(0))
|
|
|
|
->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);
|
|
|
|
}))
|
|
|
|
->willThrowException(new NotPermittedException());
|
|
|
|
|
|
|
|
$actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
|
|
|
|
$this->assertFalse($actual);
|
|
|
|
}
|
|
|
|
|
2017-03-24 13:00:07 +03:00
|
|
|
public function testCacheSuccess() {
|
|
|
|
$fileName = 'combine.js';
|
|
|
|
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$depsFile = $this->createMock(ISimpleFile::class);
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzFile = $this->createMock(ISimpleFile::class);
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$path = __DIR__ . '/data/';
|
|
|
|
|
2017-03-29 00:13:59 +03:00
|
|
|
|
|
|
|
$folder->method('getFile')->will($this->returnCallback(
|
|
|
|
function ($filename) use ($file, $depsFile, $gzFile) {
|
|
|
|
if ($filename === 'combine.js') {
|
|
|
|
return $file;
|
|
|
|
} else if ($filename === 'combine.js.deps') {
|
|
|
|
return $depsFile;
|
2017-03-29 09:11:51 +03:00
|
|
|
} else if ($filename === 'combine.js.gzip') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $gzFile;
|
|
|
|
}
|
|
|
|
$this->fail();
|
|
|
|
}
|
|
|
|
));
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$file->expects($this->at(0))
|
|
|
|
->method('putContent')
|
|
|
|
->with('var a = \'hello\';
|
|
|
|
|
|
|
|
|
|
|
|
var b = \'world\';
|
|
|
|
|
|
|
|
|
|
|
|
');
|
|
|
|
$depsFile->expects($this->at(0))->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);
|
|
|
|
}));
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzFile->expects($this->at(0))->method('putContent')->with($this->callback(
|
|
|
|
function ($content) {
|
|
|
|
return gzdecode($content) === 'var a = \'hello\';
|
|
|
|
|
|
|
|
|
|
|
|
var b = \'world\';
|
|
|
|
|
|
|
|
|
|
|
|
';
|
|
|
|
}
|
|
|
|
));
|
2017-03-24 13:00:07 +03:00
|
|
|
|
|
|
|
$actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
|
|
|
|
$this->assertTrue($actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataGetCachedSCSS() {
|
|
|
|
return [
|
|
|
|
['awesomeapp', 'core/js/foo.json', '/js/core/foo.js'],
|
|
|
|
['files', 'apps/files/js/foo.json', '/js/files/foo.js']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $appName
|
|
|
|
* @param $fileName
|
|
|
|
* @param $result
|
|
|
|
* @dataProvider dataGetCachedSCSS
|
|
|
|
*/
|
|
|
|
public function testGetCachedSCSS($appName, $fileName, $result) {
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.Js.getJs', [
|
|
|
|
'fileName' => 'foo.js',
|
|
|
|
'appName' => $appName
|
|
|
|
])
|
|
|
|
->willReturn(\OC::$WEBROOT . $result);
|
|
|
|
|
|
|
|
$actual = $this->jsCombiner->getCachedJS($appName, $fileName);
|
|
|
|
$this->assertEquals(substr($result, 1), $actual);
|
|
|
|
}
|
|
|
|
|
2017-03-25 17:25:06 +03:00
|
|
|
public function testGetContent() {
|
|
|
|
// Create temporary file with some content
|
|
|
|
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest');
|
|
|
|
$pathInfo = pathinfo($tmpFile);
|
|
|
|
file_put_contents($tmpFile, json_encode(['/foo/bar/test', $pathInfo['dirname'] . '/js/mytest.js']));
|
|
|
|
$tmpFilePathArray = explode('/', $pathInfo['basename']);
|
|
|
|
array_pop($tmpFilePathArray);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'//foo/bar/test',
|
|
|
|
'/' . implode('/', $tmpFilePathArray) . $pathInfo['dirname'] . '/js/mytest.js',
|
|
|
|
];
|
|
|
|
$this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename']));
|
|
|
|
}
|
2017-03-24 13:00:07 +03:00
|
|
|
|
2017-03-25 17:25:06 +03:00
|
|
|
public function testGetContentInvalidJson() {
|
|
|
|
// Create temporary file with some content
|
|
|
|
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest');
|
|
|
|
$pathInfo = pathinfo($tmpFile);
|
|
|
|
file_put_contents($tmpFile, 'CertainlyNotJson');
|
|
|
|
$expected = [];
|
|
|
|
$this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename']));
|
|
|
|
}
|
2018-02-03 14:41:21 +03: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 16:51:43 +03:00
|
|
|
$cache = $this->createMock(ICache::class);
|
|
|
|
$this->cacheFactory->expects($this->once())
|
|
|
|
->method('createDistributed')
|
|
|
|
->willReturn($cache);
|
|
|
|
$cache->expects($this->once())
|
2018-02-03 14:41:21 +03:00
|
|
|
->method('clear')
|
|
|
|
->with('');
|
|
|
|
$this->appData->expects($this->once())
|
|
|
|
->method('getDirectoryListing')
|
|
|
|
->willReturn([$folder]);
|
|
|
|
|
|
|
|
$this->jsCombiner->resetCache();
|
|
|
|
}
|
|
|
|
|
2017-03-24 13:00:07 +03:00
|
|
|
}
|