SCSSCacherTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  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 OC\Files\AppData\Factory;
  25. use OC\Template\SCSSCacher;
  26. use OCA\Theming\ThemingDefaults;
  27. use OCP\Files\IAppData;
  28. use OCP\Files\NotFoundException;
  29. use OCP\Files\SimpleFS\ISimpleFile;
  30. use OCP\Files\SimpleFS\ISimpleFolder;
  31. use OCP\ICache;
  32. use OCP\ICacheFactory;
  33. use OCP\IConfig;
  34. use OCP\ILogger;
  35. use OCP\IURLGenerator;
  36. use OC_App;
  37. class SCSSCacherTest extends \Test\TestCase {
  38. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $logger;
  40. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $appData;
  42. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  43. protected $urlGenerator;
  44. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  45. protected $config;
  46. /** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
  47. protected $themingDefaults;
  48. /** @var SCSSCacher */
  49. protected $scssCacher;
  50. /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
  51. protected $depsCache;
  52. /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
  53. protected $cacheFactory;
  54. protected function setUp() {
  55. parent::setUp();
  56. $this->logger = $this->createMock(ILogger::class);
  57. $this->appData = $this->createMock(IAppData::class);
  58. /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
  59. $factory = $this->createMock(Factory::class);
  60. $factory->method('get')->with('css')->willReturn($this->appData);
  61. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  62. $this->urlGenerator->expects($this->any())
  63. ->method('getBaseUrl')
  64. ->willReturn('http://localhost/nextcloud');
  65. $this->config = $this->createMock(IConfig::class);
  66. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  67. $this->depsCache = $this->createMock(ICache::class);
  68. $this->cacheFactory
  69. ->method('createDistributed')
  70. ->willReturn($this->depsCache);
  71. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  72. $this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
  73. $this->scssCacher = new SCSSCacher(
  74. $this->logger,
  75. $factory,
  76. $this->urlGenerator,
  77. $this->config,
  78. $this->themingDefaults,
  79. \OC::$SERVERROOT,
  80. $this->cacheFactory
  81. );
  82. }
  83. public function testProcessUncachedFileNoAppDataFolder() {
  84. $folder = $this->createMock(ISimpleFolder::class);
  85. $file = $this->createMock(ISimpleFile::class);
  86. $file->expects($this->any())->method('getSize')->willReturn(1);
  87. $this->appData->expects($this->once())->method('getFolder')->with('core')->willThrowException(new NotFoundException());
  88. $this->appData->expects($this->once())->method('newFolder')->with('core')->willReturn($folder);
  89. $this->appData->method('getDirectoryListing')->willReturn([]);
  90. $fileDeps = $this->createMock(ISimpleFile::class);
  91. $gzfile = $this->createMock(ISimpleFile::class);
  92. $filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
  93. substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
  94. $folder->method('getFile')
  95. ->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
  96. if ($path === $filePrefix.'styles.css') {
  97. return $file;
  98. } else if ($path === $filePrefix.'styles.css.deps') {
  99. throw new NotFoundException();
  100. } else if ($path === $filePrefix.'styles.css.gzip') {
  101. return $gzfile;
  102. } else {
  103. $this->fail();
  104. }
  105. }));
  106. $folder->expects($this->once())
  107. ->method('newFile')
  108. ->with($filePrefix.'styles.css.deps')
  109. ->willReturn($fileDeps);
  110. $this->urlGenerator->expects($this->once())
  111. ->method('getBaseUrl')
  112. ->willReturn('http://localhost/nextcloud');
  113. $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
  114. $this->assertTrue($actual);
  115. }
  116. public function testProcessUncachedFile() {
  117. $folder = $this->createMock(ISimpleFolder::class);
  118. $this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
  119. $this->appData->method('getDirectoryListing')->willReturn([]);
  120. $file = $this->createMock(ISimpleFile::class);
  121. $file->expects($this->any())->method('getSize')->willReturn(1);
  122. $fileDeps = $this->createMock(ISimpleFile::class);
  123. $gzfile = $this->createMock(ISimpleFile::class);
  124. $filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
  125. substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
  126. $folder->method('getFile')
  127. ->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
  128. if ($path === $filePrefix.'styles.css') {
  129. return $file;
  130. } else if ($path === $filePrefix.'styles.css.deps') {
  131. throw new NotFoundException();
  132. } else if ($path === $filePrefix.'styles.css.gzip') {
  133. return $gzfile;
  134. }else {
  135. $this->fail();
  136. }
  137. }));
  138. $folder->expects($this->once())
  139. ->method('newFile')
  140. ->with($filePrefix.'styles.css.deps')
  141. ->willReturn($fileDeps);
  142. $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
  143. $this->assertTrue($actual);
  144. }
  145. public function testProcessCachedFile() {
  146. $folder = $this->createMock(ISimpleFolder::class);
  147. $this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
  148. $this->appData->method('getDirectoryListing')->willReturn([]);
  149. $file = $this->createMock(ISimpleFile::class);
  150. $fileDeps = $this->createMock(ISimpleFile::class);
  151. $fileDeps->expects($this->any())->method('getSize')->willReturn(1);
  152. $gzFile = $this->createMock(ISimpleFile::class);
  153. $filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
  154. substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
  155. $folder->method('getFile')
  156. ->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
  157. if ($name === $filePrefix.'styles.css') {
  158. return $file;
  159. } else if ($name === $filePrefix.'styles.css.deps') {
  160. return $fileDeps;
  161. } else if ($name === $filePrefix.'styles.css.gzip') {
  162. return $gzFile;
  163. }
  164. $this->fail();
  165. }));
  166. $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
  167. $this->assertTrue($actual);
  168. }
  169. public function testProcessCachedFileMemcache() {
  170. $folder = $this->createMock(ISimpleFolder::class);
  171. $this->appData->expects($this->once())
  172. ->method('getFolder')
  173. ->with('core')
  174. ->willReturn($folder);
  175. $folder->method('getName')
  176. ->willReturn('core');
  177. $this->appData->method('getDirectoryListing')->willReturn([]);
  178. $file = $this->createMock(ISimpleFile::class);
  179. $fileDeps = $this->createMock(ISimpleFile::class);
  180. $fileDeps->expects($this->any())->method('getSize')->willReturn(1);
  181. $gzFile = $this->createMock(ISimpleFile::class);
  182. $filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';
  183. $filePrefix = substr(md5(\OC_Util::getVersionString('core')), 0, 4) . '-' .
  184. substr(md5('http://localhost/nextcloud'), 0, 4) . '-';
  185. $folder->method('getFile')
  186. ->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
  187. if ($name === $filePrefix.'styles.css') {
  188. return $file;
  189. } else if ($name === $filePrefix.'styles.css.deps') {
  190. return $fileDeps;
  191. } else if ($name === $filePrefix.'styles.css.gzip') {
  192. return $gzFile;
  193. }
  194. $this->fail();
  195. }));
  196. $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
  197. $this->assertTrue($actual);
  198. }
  199. public function testIsCachedNoFile() {
  200. $fileNameCSS = "styles.css";
  201. $folder = $this->createMock(ISimpleFolder::class);
  202. $folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willThrowException(new NotFoundException());
  203. $actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, $folder]);
  204. $this->assertFalse($actual);
  205. }
  206. public function testIsCachedNoDepsFile() {
  207. $fileNameCSS = "styles.css";
  208. $folder = $this->createMock(ISimpleFolder::class);
  209. $file = $this->createMock(ISimpleFile::class);
  210. $file->expects($this->once())->method('getSize')->willReturn(1);
  211. $folder->method('getFile')
  212. ->will($this->returnCallback(function($path) use ($file) {
  213. if ($path === 'styles.css') {
  214. return $file;
  215. } else if ($path === 'styles.css.deps') {
  216. throw new NotFoundException();
  217. } else {
  218. $this->fail();
  219. }
  220. }));
  221. $actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, $folder]);
  222. $this->assertFalse($actual);
  223. }
  224. public function testCacheNoFile() {
  225. $fileNameCSS = "styles.css";
  226. $fileNameSCSS = "styles.scss";
  227. $folder = $this->createMock(ISimpleFolder::class);
  228. $file = $this->createMock(ISimpleFile::class);
  229. $depsFile = $this->createMock(ISimpleFile::class);
  230. $gzipFile = $this->createMock(ISimpleFile::class);
  231. $webDir = "core/css";
  232. $path = \OC::$SERVERROOT . '/core/css/';
  233. $folder->method('getFile')->willThrowException(new NotFoundException());
  234. $folder->method('newFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
  235. if ($fileName === 'styles.css') {
  236. return $file;
  237. } else if ($fileName === 'styles.css.deps') {
  238. return $depsFile;
  239. } else if ($fileName === 'styles.css.gzip') {
  240. return $gzipFile;
  241. }
  242. throw new \Exception();
  243. }));
  244. $file->expects($this->once())->method('putContent');
  245. $depsFile->expects($this->once())->method('putContent');
  246. $gzipFile->expects($this->once())->method('putContent');
  247. $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
  248. $this->assertTrue($actual);
  249. }
  250. public function testCache() {
  251. $fileNameCSS = "styles.css";
  252. $fileNameSCSS = "styles.scss";
  253. $folder = $this->createMock(ISimpleFolder::class);
  254. $file = $this->createMock(ISimpleFile::class);
  255. $depsFile = $this->createMock(ISimpleFile::class);
  256. $gzipFile = $this->createMock(ISimpleFile::class);
  257. $webDir = "core/css";
  258. $path = \OC::$SERVERROOT;
  259. $folder->method('getFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
  260. if ($fileName === 'styles.css') {
  261. return $file;
  262. } else if ($fileName === 'styles.css.deps') {
  263. return $depsFile;
  264. } else if ($fileName === 'styles.css.gzip') {
  265. return $gzipFile;
  266. }
  267. throw new \Exception();
  268. }));
  269. $file->expects($this->once())->method('putContent');
  270. $depsFile->expects($this->once())->method('putContent');
  271. $gzipFile->expects($this->once())->method('putContent');
  272. $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
  273. $this->assertTrue($actual);
  274. }
  275. public function testCacheSuccess() {
  276. $fileNameCSS = "styles-success.css";
  277. $fileNameSCSS = "../../tests/data/scss/styles-success.scss";
  278. $folder = $this->createMock(ISimpleFolder::class);
  279. $file = $this->createMock(ISimpleFile::class);
  280. $depsFile = $this->createMock(ISimpleFile::class);
  281. $gzipFile = $this->createMock(ISimpleFile::class);
  282. $webDir = "tests/data/scss";
  283. $path = \OC::$SERVERROOT . $webDir;
  284. $folder->method('getFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
  285. if ($fileName === 'styles-success.css') {
  286. return $file;
  287. } else if ($fileName === 'styles-success.css.deps') {
  288. return $depsFile;
  289. } else if ($fileName === 'styles-success.css.gzip') {
  290. return $gzipFile;
  291. }
  292. throw new \Exception();
  293. }));
  294. $file->expects($this->at(0))->method('putContent')->with($this->callback(
  295. function ($content){
  296. return 'body{background-color:#0082c9}' === $content;
  297. }));
  298. $depsFile->expects($this->at(0))->method('putContent')->with($this->callback(
  299. function ($content) {
  300. $deps = json_decode($content, true);
  301. return array_key_exists(\OC::$SERVERROOT . '/core/css/variables.scss', $deps)
  302. && array_key_exists(\OC::$SERVERROOT . '/tests/data/scss/styles-success.scss', $deps);
  303. }));
  304. $gzipFile->expects($this->at(0))->method('putContent')->with($this->callback(
  305. function ($content) {
  306. return gzdecode($content) === 'body{background-color:#0082c9}';
  307. }
  308. ));
  309. $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
  310. $this->assertTrue($actual);
  311. }
  312. public function testCacheFailure() {
  313. $fileNameCSS = "styles-error.css";
  314. $fileNameSCSS = "../../tests/data/scss/styles-error.scss";
  315. $folder = $this->createMock(ISimpleFolder::class);
  316. $file = $this->createMock(ISimpleFile::class);
  317. $depsFile = $this->createMock(ISimpleFile::class);
  318. $webDir = "/tests/data/scss";
  319. $path = \OC::$SERVERROOT . $webDir;
  320. $folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willReturn($file);
  321. $folder->expects($this->at(1))->method('getFile')->with($fileNameCSS . '.deps')->willReturn($depsFile);
  322. $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
  323. $this->assertFalse($actual);
  324. }
  325. public function dataRebaseUrls() {
  326. return [
  327. ['#id { background-image: url(\'../img/image.jpg\'); }','#id { background-image: url(\'/apps/files/css/../img/image.jpg\'); }'],
  328. ['#id { background-image: url("../img/image.jpg"); }','#id { background-image: url(\'/apps/files/css/../img/image.jpg\'); }'],
  329. ['#id { background-image: url(\'/img/image.jpg\'); }','#id { background-image: url(\'/img/image.jpg\'); }'],
  330. ['#id { background-image: url("http://example.com/test.jpg"); }','#id { background-image: url("http://example.com/test.jpg"); }'],
  331. ];
  332. }
  333. /**
  334. * @dataProvider dataRebaseUrls
  335. */
  336. public function testRebaseUrls($scss, $expected) {
  337. $webDir = '/apps/files/css';
  338. $actual = self::invokePrivate($this->scssCacher, 'rebaseUrls', [$scss, $webDir]);
  339. $this->assertEquals($expected, $actual);
  340. }
  341. public function dataGetCachedSCSS() {
  342. return [
  343. ['core', 'core/css/styles.scss', '/css/core/styles.css', \OC_Util::getVersionString()],
  344. ['files', 'apps/files/css/styles.scss', '/css/files/styles.css', \OC_App::getAppVersion('files')]
  345. ];
  346. }
  347. /**
  348. * @param $appName
  349. * @param $fileName
  350. * @param $result
  351. * @dataProvider dataGetCachedSCSS
  352. */
  353. public function testGetCachedSCSS($appName, $fileName, $result, $version) {
  354. $this->urlGenerator->expects($this->once())
  355. ->method('linkToRoute')
  356. ->with('core.Css.getCss', [
  357. 'fileName' => substr(md5($version), 0, 4) . '-' .
  358. substr(md5('http://localhost/nextcloud'), 0, 4) . '-styles.css',
  359. 'appName' => $appName
  360. ])
  361. ->willReturn(\OC::$WEBROOT . $result);
  362. $actual = $this->scssCacher->getCachedSCSS($appName, $fileName);
  363. $this->assertEquals(substr($result, 1), $actual);
  364. }
  365. private function randomString() {
  366. return sha1(uniqid(mt_rand(), true));
  367. }
  368. private function rrmdir($directory) {
  369. $files = array_diff(scandir($directory), array('.','..'));
  370. foreach ($files as $file) {
  371. if (is_dir($directory . '/' . $file)) {
  372. $this->rrmdir($directory . '/' . $file);
  373. } else {
  374. unlink($directory . '/' . $file);
  375. }
  376. }
  377. return rmdir($directory);
  378. }
  379. public function dataGetWebDir() {
  380. return [
  381. // Root installation
  382. ['/http/core/css', 'core', '', '/http', '/core/css'],
  383. ['/http/apps/scss/css', 'scss', '', '/http', '/apps/scss/css'],
  384. ['/srv/apps2/scss/css', 'scss', '', '/http', '/apps2/scss/css'],
  385. // Sub directory install
  386. ['/http/nextcloud/core/css', 'core', '/nextcloud', '/http/nextcloud', '/nextcloud/core/css'],
  387. ['/http/nextcloud/apps/scss/css', 'scss', '/nextcloud', '/http/nextcloud', '/nextcloud/apps/scss/css'],
  388. ['/srv/apps2/scss/css', 'scss', '/nextcloud', '/http/nextcloud', '/apps2/scss/css']
  389. ];
  390. }
  391. /**
  392. * @param $path
  393. * @param $appName
  394. * @param $webRoot
  395. * @param $serverRoot
  396. * @dataProvider dataGetWebDir
  397. */
  398. public function testgetWebDir($path, $appName, $webRoot, $serverRoot, $correctWebDir) {
  399. $tmpDir = sys_get_temp_dir().'/'.$this->randomString();
  400. // Adding fake apps folder and create fake app install
  401. \OC::$APPSROOTS[] = [
  402. 'path' => $tmpDir.'/srv/apps2',
  403. 'url' => '/apps2',
  404. 'writable' => false
  405. ];
  406. mkdir($tmpDir.$path, 0777, true);
  407. $actual = self::invokePrivate($this->scssCacher, 'getWebDir', [$tmpDir.$path, $appName, $tmpDir.$serverRoot, $webRoot]);
  408. $this->assertEquals($correctWebDir, $actual);
  409. array_pop(\OC::$APPSROOTS);
  410. $this->rrmdir($tmpDir.$path);
  411. }
  412. public function testResetCache() {
  413. $file = $this->createMock(ISimpleFile::class);
  414. $file->expects($this->once())
  415. ->method('delete');
  416. $folder = $this->createMock(ISimpleFolder::class);
  417. $folder->expects($this->once())
  418. ->method('getDirectoryListing')
  419. ->willReturn([$file]);
  420. $this->depsCache->expects($this->once())
  421. ->method('clear')
  422. ->with('');
  423. $this->appData->expects($this->once())
  424. ->method('getDirectoryListing')
  425. ->willReturn([$folder]);
  426. $this->scssCacher->resetCache();
  427. }
  428. }