You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SCSSCacherTest.php 19KB

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