JSCombinerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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\IURLGenerator;
  34. class JSCombinerTest extends \Test\TestCase {
  35. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $appData;
  37. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $urlGenerator;
  39. /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $config;
  41. /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
  42. protected $depsCache;
  43. /** @var JSCombiner */
  44. protected $jsCombiner;
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->appData = $this->createMock(IAppData::class);
  48. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  49. $this->config = $this->createMock(SystemConfig::class);
  50. $this->depsCache = $this->createMock(ICache::class);
  51. $this->jsCombiner = new JSCombiner(
  52. $this->appData,
  53. $this->urlGenerator,
  54. $this->depsCache,
  55. $this->config);
  56. }
  57. public function testProcessDebugMode() {
  58. $this->config
  59. ->expects($this->once())
  60. ->method('getValue')
  61. ->with('debug')
  62. ->willReturn(true);
  63. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  64. $this->assertFalse($actual);
  65. }
  66. public function testProcessNotInstalled() {
  67. $this->config
  68. ->expects($this->at(0))
  69. ->method('getValue')
  70. ->with('debug')
  71. ->willReturn(false);
  72. $this->config
  73. ->expects($this->at(1))
  74. ->method('getValue')
  75. ->with('installed')
  76. ->willReturn(false);
  77. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  78. $this->assertFalse($actual);
  79. }
  80. public function testProcessUncachedFileNoAppDataFolder() {
  81. $this->config
  82. ->expects($this->at(0))
  83. ->method('getValue')
  84. ->with('debug')
  85. ->willReturn(false);
  86. $this->config
  87. ->expects($this->at(1))
  88. ->method('getValue')
  89. ->with('installed')
  90. ->willReturn(true);
  91. $folder = $this->createMock(ISimpleFolder::class);
  92. $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willThrowException(new NotFoundException());
  93. $this->appData->expects($this->once())->method('newFolder')->with('awesomeapp')->willReturn($folder);
  94. $file = $this->createMock(ISimpleFile::class);
  95. $gzfile = $this->createMock(ISimpleFile::class);
  96. $fileDeps = $this->createMock(ISimpleFile::class);
  97. $folder->method('getFile')
  98. ->will($this->returnCallback(function($path) use ($file, $gzfile) {
  99. if ($path === 'combine.js') {
  100. return $file;
  101. } else if ($path === 'combine.js.deps') {
  102. throw new NotFoundException();
  103. } else if ($path === 'combine.js.gz') {
  104. return $gzfile;
  105. }
  106. $this->fail();
  107. }));
  108. $folder->expects($this->once())
  109. ->method('newFile')
  110. ->with('combine.js.deps')
  111. ->willReturn($fileDeps);
  112. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  113. $this->assertTrue($actual);
  114. }
  115. public function testProcessUncachedFile() {
  116. $this->config
  117. ->expects($this->at(0))
  118. ->method('getValue')
  119. ->with('debug')
  120. ->willReturn(false);
  121. $this->config
  122. ->expects($this->at(1))
  123. ->method('getValue')
  124. ->with('installed')
  125. ->willReturn(true);
  126. $folder = $this->createMock(ISimpleFolder::class);
  127. $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder);
  128. $file = $this->createMock(ISimpleFile::class);
  129. $fileDeps = $this->createMock(ISimpleFile::class);
  130. $gzfile = $this->createMock(ISimpleFile::class);
  131. $folder->method('getFile')
  132. ->will($this->returnCallback(function($path) use ($file, $gzfile) {
  133. if ($path === 'combine.js') {
  134. return $file;
  135. } else if ($path === 'combine.js.deps') {
  136. throw new NotFoundException();
  137. } else if ($path === 'combine.js.gz') {
  138. return $gzfile;
  139. }
  140. $this->fail();
  141. }));
  142. $folder->expects($this->once())
  143. ->method('newFile')
  144. ->with('combine.js.deps')
  145. ->willReturn($fileDeps);
  146. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  147. $this->assertTrue($actual);
  148. }
  149. public function testProcessCachedFile() {
  150. $this->config
  151. ->expects($this->at(0))
  152. ->method('getValue')
  153. ->with('debug')
  154. ->willReturn(false);
  155. $this->config
  156. ->expects($this->at(1))
  157. ->method('getValue')
  158. ->with('installed')
  159. ->willReturn(true);
  160. $folder = $this->createMock(ISimpleFolder::class);
  161. $this->appData->expects($this->once())->method('getFolder')->with('awesomeapp')->willReturn($folder);
  162. $file = $this->createMock(ISimpleFile::class);
  163. $fileDeps = $this->createMock(ISimpleFile::class);
  164. $fileDeps->expects($this->once())->method('getContent')->willReturn('{}');
  165. $folder->method('getFile')
  166. ->will($this->returnCallback(function($path) use ($file, $fileDeps) {
  167. if ($path === 'combine.js') {
  168. return $file;
  169. }
  170. if ($path === 'combine.js.deps') {
  171. return $fileDeps;
  172. }
  173. $this->fail();
  174. }));
  175. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  176. $this->assertTrue($actual);
  177. }
  178. public function testProcessCachedFileMemcache() {
  179. $this->config
  180. ->expects($this->at(0))
  181. ->method('getValue')
  182. ->with('debug')
  183. ->willReturn(false);
  184. $this->config
  185. ->expects($this->at(1))
  186. ->method('getValue')
  187. ->with('installed')
  188. ->willReturn(true);
  189. $folder = $this->createMock(ISimpleFolder::class);
  190. $this->appData->expects($this->once())
  191. ->method('getFolder')
  192. ->with('awesomeapp')
  193. ->willReturn($folder);
  194. $folder->method('getName')
  195. ->willReturn('awesomeapp');
  196. $file = $this->createMock(ISimpleFile::class);
  197. $this->depsCache->method('get')
  198. ->with('awesomeapp-combine.js.deps')
  199. ->willReturn('{}');
  200. $folder->method('getFile')
  201. ->will($this->returnCallback(function($path) use ($file) {
  202. if ($path === 'combine.js') {
  203. return $file;
  204. }
  205. $this->fail();
  206. }));
  207. $actual = $this->jsCombiner->process(__DIR__, '/data/combine.json', 'awesomeapp');
  208. $this->assertTrue($actual);
  209. }
  210. public function testIsCachedNoDepsFile() {
  211. $fileName = 'combine.json';
  212. $folder = $this->createMock(ISimpleFolder::class);
  213. $file = $this->createMock(ISimpleFile::class);
  214. $folder->method('getFile')
  215. ->will($this->returnCallback(function($path) use ($file) {
  216. if ($path === 'combine.js') {
  217. return $file;
  218. }
  219. if ($path === 'combine.js.deps') {
  220. throw new NotFoundException();
  221. }
  222. $this->fail();
  223. }));
  224. $actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
  225. $this->assertFalse($actual);
  226. }
  227. public function testIsCachedWithNotExistingFile() {
  228. $fileName = 'combine.json';
  229. $folder = $this->createMock(ISimpleFolder::class);
  230. $file = $this->createMock(ISimpleFile::class);
  231. $folder->method('getFile')
  232. ->with('combine.js.deps')
  233. ->willReturn($file);
  234. $file->expects($this->once())
  235. ->method('getContent')
  236. ->willReturn(json_encode(['/etc/certainlynotexisting/file/ihope' => 10000]));
  237. $actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
  238. $this->assertFalse($actual);
  239. }
  240. public function testIsCachedWithOlderMtime() {
  241. $fileName = 'combine.json';
  242. $folder = $this->createMock(ISimpleFolder::class);
  243. $file = $this->createMock(ISimpleFile::class);
  244. $folder->method('getFile')
  245. ->with('combine.js.deps')
  246. ->willReturn($file);
  247. $file->expects($this->once())
  248. ->method('getContent')
  249. ->willReturn(json_encode([__FILE__ => 1234]));
  250. $actual = self::invokePrivate($this->jsCombiner, 'isCached', [$fileName, $folder]);
  251. $this->assertFalse($actual);
  252. }
  253. public function testCacheNoFile() {
  254. $fileName = 'combine.js';
  255. $folder = $this->createMock(ISimpleFolder::class);
  256. $file = $this->createMock(ISimpleFile::class);
  257. $depsFile = $this->createMock(ISimpleFile::class);
  258. $gzFile = $this->createMock(ISimpleFile::class);
  259. $path = __DIR__ . '/data/';
  260. $folder->method('getFile')->willThrowException(new NotFoundException());
  261. $folder->method('newFile')->will($this->returnCallback(
  262. function ($filename) use ($file, $depsFile, $gzFile) {
  263. if ($filename === 'combine.js') {
  264. return $file;
  265. } else if ($filename === 'combine.js.deps') {
  266. return $depsFile;
  267. } else if ($filename === 'combine.js.gz') {
  268. return $gzFile;
  269. }
  270. $this->fail();
  271. }
  272. ));
  273. $file->expects($this->once())->method('putContent');
  274. $depsFile->expects($this->once())->method('putContent');
  275. $gzFile->expects($this->once())->method('putContent');
  276. $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
  277. $this->assertTrue($actual);
  278. }
  279. public function testCache() {
  280. $fileName = 'combine.js';
  281. $folder = $this->createMock(ISimpleFolder::class);
  282. $file = $this->createMock(ISimpleFile::class);
  283. $depsFile = $this->createMock(ISimpleFile::class);
  284. $gzFile = $this->createMock(ISimpleFile::class);
  285. $path = __DIR__ . '/data/';
  286. $folder->method('getFile')->will($this->returnCallback(
  287. function ($filename) use ($file, $depsFile, $gzFile) {
  288. if ($filename === 'combine.js') {
  289. return $file;
  290. } else if ($filename === 'combine.js.deps') {
  291. return $depsFile;
  292. } else if ($filename === 'combine.js.gz') {
  293. return $gzFile;
  294. }
  295. $this->fail();
  296. }
  297. ));
  298. $file->expects($this->once())->method('putContent');
  299. $depsFile->expects($this->once())->method('putContent');
  300. $gzFile->expects($this->once())->method('putContent');
  301. $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
  302. $this->assertTrue($actual);
  303. }
  304. public function testCacheNotPermittedException() {
  305. $fileName = 'combine.js';
  306. $folder = $this->createMock(ISimpleFolder::class);
  307. $file = $this->createMock(ISimpleFile::class);
  308. $depsFile = $this->createMock(ISimpleFile::class);
  309. $path = __DIR__ . '/data/';
  310. $folder->expects($this->at(0))->method('getFile')->with($fileName)->willReturn($file);
  311. $folder->expects($this->at(1))->method('getFile')->with($fileName . '.deps')->willReturn($depsFile);
  312. $file->expects($this->at(0))
  313. ->method('putContent')
  314. ->with('var a = \'hello\';
  315. var b = \'world\';
  316. ');
  317. $depsFile
  318. ->expects($this->at(0))
  319. ->method('putContent')
  320. ->with($this->callback(
  321. function ($content) {
  322. $deps = json_decode($content, true);
  323. return array_key_exists(__DIR__ . '/data//1.js', $deps)
  324. && array_key_exists(__DIR__ . '/data//2.js', $deps);
  325. }))
  326. ->willThrowException(new NotPermittedException());
  327. $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
  328. $this->assertFalse($actual);
  329. }
  330. public function testCacheSuccess() {
  331. $fileName = 'combine.js';
  332. $folder = $this->createMock(ISimpleFolder::class);
  333. $file = $this->createMock(ISimpleFile::class);
  334. $depsFile = $this->createMock(ISimpleFile::class);
  335. $gzFile = $this->createMock(ISimpleFile::class);
  336. $path = __DIR__ . '/data/';
  337. $folder->method('getFile')->will($this->returnCallback(
  338. function ($filename) use ($file, $depsFile, $gzFile) {
  339. if ($filename === 'combine.js') {
  340. return $file;
  341. } else if ($filename === 'combine.js.deps') {
  342. return $depsFile;
  343. } else if ($filename === 'combine.js.gz') {
  344. return $gzFile;
  345. }
  346. $this->fail();
  347. }
  348. ));
  349. $file->expects($this->at(0))
  350. ->method('putContent')
  351. ->with('var a = \'hello\';
  352. var b = \'world\';
  353. ');
  354. $depsFile->expects($this->at(0))->method('putContent')->with($this->callback(
  355. function ($content) {
  356. $deps = json_decode($content, true);
  357. return array_key_exists(__DIR__ . '/data//1.js', $deps)
  358. && array_key_exists(__DIR__ . '/data//2.js', $deps);
  359. }));
  360. $gzFile->expects($this->at(0))->method('putContent')->with($this->callback(
  361. function ($content) {
  362. return gzdecode($content) === 'var a = \'hello\';
  363. var b = \'world\';
  364. ';
  365. }
  366. ));
  367. $actual = self::invokePrivate($this->jsCombiner, 'cache', [$path, 'combine.json', $folder]);
  368. $this->assertTrue($actual);
  369. }
  370. public function dataGetCachedSCSS() {
  371. return [
  372. ['awesomeapp', 'core/js/foo.json', '/js/core/foo.js'],
  373. ['files', 'apps/files/js/foo.json', '/js/files/foo.js']
  374. ];
  375. }
  376. /**
  377. * @param $appName
  378. * @param $fileName
  379. * @param $result
  380. * @dataProvider dataGetCachedSCSS
  381. */
  382. public function testGetCachedSCSS($appName, $fileName, $result) {
  383. $this->urlGenerator->expects($this->once())
  384. ->method('linkToRoute')
  385. ->with('core.Js.getJs', [
  386. 'fileName' => 'foo.js',
  387. 'appName' => $appName
  388. ])
  389. ->willReturn(\OC::$WEBROOT . $result);
  390. $actual = $this->jsCombiner->getCachedJS($appName, $fileName);
  391. $this->assertEquals(substr($result, 1), $actual);
  392. }
  393. public function testGetContent() {
  394. // Create temporary file with some content
  395. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest');
  396. $pathInfo = pathinfo($tmpFile);
  397. file_put_contents($tmpFile, json_encode(['/foo/bar/test', $pathInfo['dirname'] . '/js/mytest.js']));
  398. $tmpFilePathArray = explode('/', $pathInfo['basename']);
  399. array_pop($tmpFilePathArray);
  400. $expected = [
  401. '//foo/bar/test',
  402. '/' . implode('/', $tmpFilePathArray) . $pathInfo['dirname'] . '/js/mytest.js',
  403. ];
  404. $this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename']));
  405. }
  406. public function testGetContentInvalidJson() {
  407. // Create temporary file with some content
  408. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('JSCombinerTest');
  409. $pathInfo = pathinfo($tmpFile);
  410. file_put_contents($tmpFile, 'CertainlyNotJson');
  411. $expected = [];
  412. $this->assertEquals($expected, $this->jsCombiner->getContent($pathInfo['dirname'], $pathInfo['basename']));
  413. }
  414. }