JSCombinerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Template;
  24. use function foo\func;
  25. use OC\SystemConfig;
  26. use OC\Template\JSCombiner;
  27. use OCP\Files\IAppData;
  28. use OCP\Files\NotFoundException;
  29. use OCP\Files\NotPermittedException;
  30. use OCP\Files\SimpleFS\ISimpleFile;
  31. use OCP\Files\SimpleFS\ISimpleFolder;
  32. use OCP\ICache;
  33. use OCP\ICacheFactory;
  34. use OCP\ILogger;
  35. use OCP\IURLGenerator;
  36. class JSCombinerTest extends \Test\TestCase {
  37. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $appData;
  39. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $urlGenerator;
  41. /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  42. protected $config;
  43. /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $depsCache;
  45. /** @var JSCombiner */
  46. protected $jsCombiner;
  47. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  48. protected $logger;
  49. /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
  50. protected $cacheFactory;
  51. protected function setUp() {
  52. parent::setUp();
  53. $this->appData = $this->createMock(IAppData::class);
  54. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  55. $this->config = $this->createMock(SystemConfig::class);
  56. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  57. $this->depsCache = $this->createMock(ICache::class);
  58. $this->cacheFactory->expects($this->at(0))
  59. ->method('createDistributed')
  60. ->willReturn($this->depsCache);
  61. $this->logger = $this->createMock(ILogger::class);
  62. $this->jsCombiner = new JSCombiner(
  63. $this->appData,
  64. $this->urlGenerator,
  65. $this->cacheFactory,
  66. $this->config,
  67. $this->logger
  68. );
  69. }
  70. public function testProcessDebugMode() {
  71. $this->config
  72. ->expects($this->once())
  73. ->method('getValue')
  74. ->with('debug')
  75. ->willReturn(true);
  76. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  77. $this->assertFalse($actual);
  78. }
  79. public function testProcessNotInstalled() {
  80. $this->config
  81. ->expects($this->at(0))
  82. ->method('getValue')
  83. ->with('debug')
  84. ->willReturn(false);
  85. $this->config
  86. ->expects($this->at(1))
  87. ->method('getValue')
  88. ->with('installed')
  89. ->willReturn(false);
  90. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  91. $this->assertFalse($actual);
  92. }
  93. public function testProcessUncachedFileNoAppDataFolder() {
  94. $this->config
  95. ->expects($this->at(0))
  96. ->method('getValue')
  97. ->with('debug')
  98. ->willReturn(false);
  99. $this->config
  100. ->expects($this->at(1))
  101. ->method('getValue')
  102. ->with('installed')
  103. ->willReturn(true);
  104. $folder = $this->createMock(ISimpleFolder::class);
  105. $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willThrowException(new NotFoundException());
  106. $this->appData->expects($this->once())->method('newFolder')->with('awesomeapp')->willReturn($folder);
  107. $file = $this->createMock(ISimpleFile::class);
  108. $gzfile = $this->createMock(ISimpleFile::class);
  109. $fileDeps = $this->createMock(ISimpleFile::class);
  110. $folder->method('getFile')
  111. ->will($this->returnCallback(function($path) use ($file, $gzfile) {
  112. if ($path === 'combine.js') {
  113. return $file;
  114. } else if ($path === 'combine.js.deps') {
  115. throw new NotFoundException();
  116. } else if ($path === 'combine.js.gzip') {
  117. return $gzfile;
  118. }
  119. $this->fail();
  120. }));
  121. $folder->expects($this->once())
  122. ->method('newFile')
  123. ->with('combine.js.deps')
  124. ->willReturn($fileDeps);
  125. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  126. $this->assertTrue($actual);
  127. }
  128. public function testProcessUncachedFile() {
  129. $this->config
  130. ->expects($this->at(0))
  131. ->method('getValue')
  132. ->with('debug')
  133. ->willReturn(false);
  134. $this->config
  135. ->expects($this->at(1))
  136. ->method('getValue')
  137. ->with('installed')
  138. ->willReturn(true);
  139. $folder = $this->createMock(ISimpleFolder::class);
  140. $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder);
  141. $file = $this->createMock(ISimpleFile::class);
  142. $fileDeps = $this->createMock(ISimpleFile::class);
  143. $gzfile = $this->createMock(ISimpleFile::class);
  144. $folder->method('getFile')
  145. ->will($this->returnCallback(function($path) use ($file, $gzfile) {
  146. if ($path === 'combine.js') {
  147. return $file;
  148. } else if ($path === 'combine.js.deps') {
  149. throw new NotFoundException();
  150. } else if ($path === 'combine.js.gzip') {
  151. return $gzfile;
  152. }
  153. $this->fail();
  154. }));
  155. $folder->expects($this->once())
  156. ->method('newFile')
  157. ->with('combine.js.deps')
  158. ->willReturn($fileDeps);
  159. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  160. $this->assertTrue($actual);
  161. }
  162. public function testProcessCachedFile() {
  163. $this->config
  164. ->expects($this->at(0))
  165. ->method('getValue')
  166. ->with('debug')
  167. ->willReturn(false);
  168. $this->config
  169. ->expects($this->at(1))
  170. ->method('getValue')
  171. ->with('installed')
  172. ->willReturn(true);
  173. $folder = $this->createMock(ISimpleFolder::class);
  174. $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder);
  175. $file = $this->createMock(ISimpleFile::class);
  176. $fileDeps = $this->createMock(ISimpleFile::class);
  177. $fileDeps->expects($this->once())->method('getContent')->willReturn('{}');
  178. $folder->method('fileExists')
  179. ->with('combine.js')
  180. ->willReturn(true);
  181. $folder->method('getFile')
  182. ->will($this->returnCallback(function($path) use ($file, $fileDeps) {
  183. if ($path === 'combine.js') {
  184. return $file;
  185. }
  186. if ($path === 'combine.js.deps') {
  187. return $fileDeps;
  188. }
  189. $this->fail();
  190. }));
  191. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  192. $this->assertTrue($actual);
  193. }
  194. public function testProcessCachedFileMemcache() {
  195. $this->config
  196. ->expects($this->at(0))
  197. ->method('getValue')
  198. ->with('debug')
  199. ->willReturn(false);
  200. $this->config
  201. ->expects($this->at(1))
  202. ->method('getValue')
  203. ->with('installed')
  204. ->willReturn(true);
  205. $folder = $this->createMock(ISimpleFolder::class);
  206. $this->appData->expects($this->once())
  207. ->method('getFolder')
  208. ->with('awesomeapp')
  209. ->willReturn($folder);
  210. $folder->method('getName')
  211. ->willReturn('awesomeapp');
  212. $folder->method('fileExists')
  213. ->with('combine.js')
  214. ->willReturn(true);
  215. $file = $this->createMock(ISimpleFile::class);
  216. $this->depsCache->method('get')
  217. ->with('awesomeapp-combine.js.deps')
  218. ->willReturn('{}');
  219. $folder->method('getFile')
  220. ->will($this->returnCallback(function($path) use ($file) {
  221. if ($path === 'combine.js') {
  222. return $file;
  223. }
  224. $this->fail();
  225. }));
  226. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  227. $this->assertTrue($actual);
  228. }
  229. public function testIsCachedNoDepsFile() {
  230. $fileName = 'combine.json';
  231. $folder = $this->createMock(ISimpleFolder::class);
  232. $file = $this->createMock(ISimpleFile::class);
  233. $folder->method('getFile')
  234. ->will($this->returnCallback(function($path) use ($file) {
  235. if ($path === 'combine.js') {
  236. return $file;
  237. }
  238. if ($path === 'combine.js.deps') {
  239. throw new NotFoundException();
  240. }
  241. $this->fail();
  242. }));
  243. $actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
  244. $this->assertFalse($actual);
  245. }
  246. public function testIsCachedWithNotExistingFile() {
  247. $fileName = 'combine.json';
  248. $folder = $this->createMock(ISimpleFolder::class);
  249. $folder->method('fileExists')
  250. ->with('combine.js')
  251. ->willReturn(true);
  252. $file = $this->createMock(ISimpleFile::class);
  253. $folder->method('getFile')
  254. ->with('combine.js.deps')
  255. ->willReturn($file);
  256. $file->expects($this->once())
  257. ->method('getContent')
  258. ->willReturn(json_encode(['/etc/certainlynotexisting/file/ihope' => 10000]));
  259. $actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
  260. $this->assertFalse($actual);
  261. }
  262. public function testIsCachedWithOlderMtime() {
  263. $fileName = 'combine.json';
  264. $folder = $this->createMock(ISimpleFolder::class);
  265. $folder->method('fileExists')
  266. ->with('combine.js')
  267. ->willReturn(true);
  268. $file = $this->createMock(ISimpleFile::class);
  269. $folder->method('getFile')
  270. ->with('combine.js.deps')
  271. ->willReturn($file);
  272. $file->expects($this->once())
  273. ->method('getContent')
  274. ->willReturn(json_encode([__FILE__ => 1234]));
  275. $actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
  276. $this->assertFalse($actual);
  277. }
  278. public function testIsCachedWithoutContent() {
  279. $fileName = 'combine.json';
  280. $folder = $this->createMock(ISimpleFolder::class);
  281. $folder->method('fileExists')
  282. ->with('combine.js')
  283. ->willReturn(true);
  284. $file = $this->createMock(ISimpleFile::class);
  285. $folder->method('getFile')
  286. ->with('combine.js.deps')
  287. ->willReturn($file);
  288. $file->expects($this->once())
  289. ->method('getContent')
  290. ->willReturn('');
  291. $this->logger->expects($this->once())
  292. ->method('info')
  293. ->with('JSCombiner: deps file empty: combine.js.deps');
  294. $actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
  295. $this->assertFalse($actual);
  296. }
  297. public function testCacheNoFile() {
  298. $fileName = 'combine.js';
  299. $folder = $this->createMock(ISimpleFolder::class);
  300. $file = $this->createMock(ISimpleFile::class);
  301. $depsFile = $this->createMock(ISimpleFile::class);
  302. $gzFile = $this->createMock(ISimpleFile::class);
  303. $path = __DIR__ . '/data/';
  304. $folder->method('getFile')->willThrowException(new NotFoundException());
  305. $folder->method('newFile')->will($this->returnCallback(
  306. function ($filename) use ($file, $depsFile, $gzFile) {
  307. if ($filename === 'combine.js') {
  308. return $file;
  309. } else if ($filename === 'combine.js.deps') {
  310. return $depsFile;
  311. } else if ($filename === 'combine.js.gzip') {
  312. return $gzFile;
  313. }
  314. $this->fail();
  315. }
  316. ));
  317. $file->expects($this->once())->method('putContent');
  318. $depsFile->expects($this->once())->method('putContent');
  319. $gzFile->expects($this->once())->method('putContent');
  320. $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
  321. $this->assertTrue($actual);
  322. }
  323. public function testCache() {
  324. $fileName = 'combine.js';
  325. $folder = $this->createMock(ISimpleFolder::class);
  326. $file = $this->createMock(ISimpleFile::class);
  327. $depsFile = $this->createMock(ISimpleFile::class);
  328. $gzFile = $this->createMock(ISimpleFile::class);
  329. $path = __DIR__ . '/data/';
  330. $folder->method('getFile')->will($this->returnCallback(
  331. function ($filename) use ($file, $depsFile, $gzFile) {
  332. if ($filename === 'combine.js') {
  333. return $file;
  334. } else if ($filename === 'combine.js.deps') {
  335. return $depsFile;
  336. } else if ($filename === 'combine.js.gzip') {
  337. return $gzFile;
  338. }
  339. $this->fail();
  340. }
  341. ));
  342. $file->expects($this->once())->method('putContent');
  343. $depsFile->expects($this->once())->method('putContent');
  344. $gzFile->expects($this->once())->method('putContent');
  345. $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
  346. $this->assertTrue($actual);
  347. }
  348. public function testCacheNotPermittedException() {
  349. $fileName = 'combine.js';
  350. $folder = $this->createMock(ISimpleFolder::class);
  351. $file = $this->createMock(ISimpleFile::class);
  352. $depsFile = $this->createMock(ISimpleFile::class);
  353. $path = __DIR__ . '/data/';
  354. $folder->expects($this->at(0))->method('getFile')->with($fileName)->willReturn($file);
  355. $folder->expects($this->at(1))->method('getFile')->with($fileName . '.deps')->willReturn($depsFile);
  356. $file->expects($this->at(0))
  357. ->method('putContent')
  358. ->with('var a = \'hello\';
  359. var b = \'world\';
  360. ');
  361. $depsFile
  362. ->expects($this->at(0))
  363. ->method('putContent')
  364. ->with($this->callback(
  365. function ($content) {
  366. $deps = json_decode($content, true);
  367. return array_key_exists(__DIR__ . '/data//1.js', $deps)
  368. && array_key_exists(__DIR__ . '/data//2.js', $deps);
  369. }))
  370. ->willThrowException(new NotPermittedException());
  371. $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
  372. $this->assertFalse($actual);
  373. }
  374. public function testCacheSuccess() {
  375. $fileName = 'combine.js';
  376. $folder = $this->createMock(ISimpleFolder::class);
  377. $file = $this->createMock(ISimpleFile::class);
  378. $depsFile = $this->createMock(ISimpleFile::class);
  379. $gzFile = $this->createMock(ISimpleFile::class);
  380. $path = __DIR__ . '/data/';
  381. $folder->method('getFile')->will($this->returnCallback(
  382. function ($filename) use ($file, $depsFile, $gzFile) {
  383. if ($filename === 'combine.js') {
  384. return $file;
  385. } else if ($filename === 'combine.js.deps') {
  386. return $depsFile;
  387. } else if ($filename === 'combine.js.gzip') {
  388. return $gzFile;
  389. }
  390. $this->fail();
  391. }
  392. ));
  393. $file->expects($this->at(0))
  394. ->method('putContent')
  395. ->with('var a = \'hello\';
  396. var b = \'world\';
  397. ');
  398. $depsFile->expects($this->at(0))->method('putContent')->with($this->callback(
  399. function ($content) {
  400. $deps = json_decode($content, true);
  401. return array_key_exists(__DIR__ . '/data//1.js', $deps)
  402. && array_key_exists(__DIR__ . '/data//2.js', $deps);
  403. }));
  404. $gzFile->expects($this->at(0))->method('putContent')->with($this->callback(
  405. function ($content) {
  406. return gzdecode($content) === 'var a = \'hello\';
  407. var b = \'world\';
  408. ';
  409. }
  410. ));
  411. $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
  412. $this->assertTrue($actual);
  413. }
  414. public function dataGetCachedSCSS() {
  415. return [
  416. ['awesomeapp', 'core/js/foo.json', '/js/core/foo.js'],
  417. ['files', 'apps/files/js/foo.json', '/js/files/foo.js']
  418. ];
  419. }
  420. /**
  421. * @param $appName
  422. * @param $fileName
  423. * @param $result
  424. * @dataProvider dataGetCachedSCSS
  425. */
  426. public function testGetCachedSCSS($appName, $fileName, $result) {
  427. $this->urlGenerator->expects($this->once())
  428. ->method('linkToRoute')
  429. ->with('core.Js.getJs', [
  430. 'fileName' => 'foo.js',
  431. 'appName' => $appName
  432. ])
  433. ->willReturn(\OC::$WEBROOT . $result);
  434. $actual = $this->jsCombiner->getCachedJS($appName, $fileName);
  435. $this->assertEquals(substr($result, 1), $actual);
  436. }
  437. public function testGetContent() {
  438. // Create temporary file with some content
  439. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest');
  440. $pathInfo = pathinfo($tmpFile);
  441. file_put_contents($tmpFile, json_encode(['/foo/bar/test', $pathInfo['dirname'] . '/js/mytest.js']));
  442. $tmpFilePathArray = explode('/', $pathInfo['basename']);
  443. array_pop($tmpFilePathArray);
  444. $expected = [
  445. '//foo/bar/test',
  446. '/' . implode('/', $tmpFilePathArray) . $pathInfo['dirname'] . '/js/mytest.js',
  447. ];
  448. $this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename']));
  449. }
  450. public function testGetContentInvalidJson() {
  451. // Create temporary file with some content
  452. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest');
  453. $pathInfo = pathinfo($tmpFile);
  454. file_put_contents($tmpFile, 'CertainlyNotJson');
  455. $expected = [];
  456. $this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename']));
  457. }
  458. public function testResetCache() {
  459. $file = $this->createMock(ISimpleFile::class);
  460. $file->expects($this->once())
  461. ->method('delete');
  462. $folder = $this->createMock(ISimpleFolder::class);
  463. $folder->expects($this->once())
  464. ->method('getDirectoryListing')
  465. ->willReturn([$file]);
  466. $cache = $this->createMock(ICache::class);
  467. $this->cacheFactory->expects($this->once())
  468. ->method('createDistributed')
  469. ->willReturn($cache);
  470. $cache->expects($this->once())
  471. ->method('clear')
  472. ->with('');
  473. $this->appData->expects($this->once())
  474. ->method('getDirectoryListing')
  475. ->willReturn([$folder]);
  476. $this->jsCombiner->resetCache();
  477. }
  478. }