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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-03-24 13:00:07 +03:00
|
|
|
namespace Test\Template;
|
|
|
|
|
|
|
|
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 {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
|
2017-03-24 13:00:07 +03:00
|
|
|
protected $appData;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
|
2017-03-24 13:00:07 +03:00
|
|
|
protected $urlGenerator;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2017-03-24 13:00:07 +03:00
|
|
|
protected $config;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
|
2017-03-24 13:00:07 +03:00
|
|
|
protected $depsCache;
|
2017-03-25 17:25:06 +03:00
|
|
|
/** @var JSCombiner */
|
|
|
|
protected $jsCombiner;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2017-07-13 23:31:07 +03:00
|
|
|
protected $logger;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-03-07 16:51:43 +03:00
|
|
|
protected $cacheFactory;
|
2017-03-24 13:00:07 +03:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2017-03-24 13:00:07 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->appData = $this->createMock(IAppData::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
$this->config = $this->createMock(SystemConfig::class);
|
2018-03-07 16:51:43 +03:00
|
|
|
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
2017-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
|
|
|
|
);
|
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')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($path) use ($file, $gzfile) {
|
2017-03-24 13:00:07 +03:00
|
|
|
if ($path === 'combine.js') {
|
|
|
|
return $file;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($path === 'combine.js.deps') {
|
2017-03-24 13:00:07 +03:00
|
|
|
throw new NotFoundException();
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($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();
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
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')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($path) use ($file, $gzfile) {
|
2017-03-24 13:00:07 +03:00
|
|
|
if ($path === 'combine.js') {
|
|
|
|
return $file;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($path === 'combine.js.deps') {
|
2017-03-24 13:00:07 +03:00
|
|
|
throw new NotFoundException();
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($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();
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
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')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($path) use ($file, $fileDeps) {
|
2017-03-24 13:00:07 +03:00
|
|
|
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();
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
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')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($path) use ($file) {
|
2017-03-24 13:00:07 +03:00
|
|
|
if ($path === 'combine.js') {
|
|
|
|
return $file;
|
|
|
|
}
|
2017-03-25 17:25:06 +03:00
|
|
|
$this->fail();
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
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')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($path) use ($file) {
|
2017-03-24 13:00:07 +03:00
|
|
|
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();
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
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());
|
|
|
|
|
2020-03-26 00:21:27 +03:00
|
|
|
$folder->method('newFile')->willReturnCallback(
|
2017-03-29 00:13:59 +03:00
|
|
|
function ($filename) use ($file, $depsFile, $gzFile) {
|
|
|
|
if ($filename === 'combine.js') {
|
|
|
|
return $file;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($filename === 'combine.js.deps') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $depsFile;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($filename === 'combine.js.gzip') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $gzFile;
|
|
|
|
}
|
|
|
|
$this->fail();
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
);
|
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/';
|
|
|
|
|
2020-03-26 00:21:27 +03:00
|
|
|
$folder->method('getFile')->willReturnCallback(
|
2017-03-29 00:13:59 +03:00
|
|
|
function ($filename) use ($file, $depsFile, $gzFile) {
|
|
|
|
if ($filename === 'combine.js') {
|
|
|
|
return $file;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($filename === 'combine.js.deps') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $depsFile;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($filename === 'combine.js.gzip') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $gzFile;
|
|
|
|
}
|
|
|
|
$this->fail();
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
);
|
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
|
|
|
|
2020-03-26 00:21:27 +03:00
|
|
|
$folder->method('getFile')->willReturnCallback(
|
2017-03-29 00:13:59 +03:00
|
|
|
function ($filename) use ($file, $depsFile, $gzFile) {
|
|
|
|
if ($filename === 'combine.js') {
|
|
|
|
return $file;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($filename === 'combine.js.deps') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $depsFile;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($filename === 'combine.js.gzip') {
|
2017-03-29 00:13:59 +03:00
|
|
|
return $gzFile;
|
|
|
|
}
|
|
|
|
$this->fail();
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
);
|
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
|
|
|
}
|