SCSSCacherTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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\IConfig;
  33. use OCP\ILogger;
  34. use OCP\IURLGenerator;
  35. class SCSSCacherTest extends \Test\TestCase {
  36. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $logger;
  38. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $appData;
  40. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $urlGenerator;
  42. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  43. protected $config;
  44. /** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
  45. protected $themingDefaults;
  46. /** @var SCSSCacher */
  47. protected $scssCacher;
  48. /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
  49. protected $depsCache;
  50. protected function setUp() {
  51. parent::setUp();
  52. $this->logger = $this->createMock(ILogger::class);
  53. $this->appData = $this->createMock(IAppData::class);
  54. /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
  55. $factory = $this->createMock(Factory::class);
  56. $factory->method('get')->with('css')->willReturn($this->appData);
  57. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  58. $this->config = $this->createMock(IConfig::class);
  59. $this->depsCache = $this->createMock(ICache::class);
  60. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  61. $this->scssCacher = new SCSSCacher(
  62. $this->logger,
  63. $factory,
  64. $this->urlGenerator,
  65. $this->config,
  66. $this->themingDefaults,
  67. \OC::$SERVERROOT,
  68. $this->depsCache
  69. );
  70. $this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
  71. $this->urlGenerator->expects($this->any())
  72. ->method('getBaseUrl')
  73. ->willReturn('http://localhost/nextcloud');
  74. }
  75. public function testProcessUncachedFileNoAppDataFolder() {
  76. $folder = $this->createMock(ISimpleFolder::class);
  77. $file = $this->createMock(ISimpleFile::class);
  78. $file->expects($this->any())->method('getSize')->willReturn(1);
  79. $this->appData->expects($this->once())->method('getFolder')->with('core')->willThrowException(new NotFoundException());
  80. $this->appData->expects($this->once())->method('newFolder')->with('core')->willReturn($folder);
  81. $fileDeps = $this->createMock(ISimpleFile::class);
  82. $gzfile = $this->createMock(ISimpleFile::class);
  83. $filePrefix = md5('http://localhost/nextcloud') . '-';
  84. $folder->method('getFile')
  85. ->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
  86. if ($path === $filePrefix.'styles.css') {
  87. return $file;
  88. } else if ($path === $filePrefix.'styles.css.deps') {
  89. throw new NotFoundException();
  90. } else if ($path === $filePrefix.'styles.css.gzip') {
  91. return $gzfile;
  92. } else {
  93. $this->fail();
  94. }
  95. }));
  96. $folder->expects($this->once())
  97. ->method('newFile')
  98. ->with($filePrefix.'styles.css.deps')
  99. ->willReturn($fileDeps);
  100. $this->urlGenerator->expects($this->once())
  101. ->method('getBaseUrl')
  102. ->willReturn('http://localhost/nextcloud');
  103. $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
  104. $this->assertTrue($actual);
  105. }
  106. public function testProcessUncachedFile() {
  107. $folder = $this->createMock(ISimpleFolder::class);
  108. $this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
  109. $file = $this->createMock(ISimpleFile::class);
  110. $file->expects($this->any())->method('getSize')->willReturn(1);
  111. $fileDeps = $this->createMock(ISimpleFile::class);
  112. $gzfile = $this->createMock(ISimpleFile::class);
  113. $filePrefix = md5('http://localhost/nextcloud') . '-';
  114. $folder->method('getFile')
  115. ->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
  116. if ($path === $filePrefix.'styles.css') {
  117. return $file;
  118. } else if ($path === $filePrefix.'styles.css.deps') {
  119. throw new NotFoundException();
  120. } else if ($path === $filePrefix.'styles.css.gzip') {
  121. return $gzfile;
  122. }else {
  123. $this->fail();
  124. }
  125. }));
  126. $folder->expects($this->once())
  127. ->method('newFile')
  128. ->with($filePrefix.'styles.css.deps')
  129. ->willReturn($fileDeps);
  130. $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
  131. $this->assertTrue($actual);
  132. }
  133. public function testProcessCachedFile() {
  134. $folder = $this->createMock(ISimpleFolder::class);
  135. $this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
  136. $file = $this->createMock(ISimpleFile::class);
  137. $fileDeps = $this->createMock(ISimpleFile::class);
  138. $fileDeps->expects($this->any())->method('getSize')->willReturn(1);
  139. $gzFile = $this->createMock(ISimpleFile::class);
  140. $filePrefix = md5('http://localhost/nextcloud') . '-';
  141. $folder->method('getFile')
  142. ->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
  143. if ($name === $filePrefix.'styles.css') {
  144. return $file;
  145. } else if ($name === $filePrefix.'styles.css.deps') {
  146. return $fileDeps;
  147. } else if ($name === $filePrefix.'styles.css.gzip') {
  148. return $gzFile;
  149. }
  150. $this->fail();
  151. }));
  152. $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
  153. $this->assertTrue($actual);
  154. }
  155. public function testProcessCachedFileMemcache() {
  156. $folder = $this->createMock(ISimpleFolder::class);
  157. $this->appData->expects($this->once())
  158. ->method('getFolder')
  159. ->with('core')
  160. ->willReturn($folder);
  161. $folder->method('getName')
  162. ->willReturn('core');
  163. $file = $this->createMock(ISimpleFile::class);
  164. $fileDeps = $this->createMock(ISimpleFile::class);
  165. $fileDeps->expects($this->any())->method('getSize')->willReturn(1);
  166. $gzFile = $this->createMock(ISimpleFile::class);
  167. $filePrefix = md5('http://localhost/nextcloud') . '-';
  168. $folder->method('getFile')
  169. ->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
  170. if ($name === $filePrefix.'styles.css') {
  171. return $file;
  172. } else if ($name === $filePrefix.'styles.css.deps') {
  173. return $fileDeps;
  174. } else if ($name === $filePrefix.'styles.css.gzip') {
  175. return $gzFile;
  176. }
  177. $this->fail();
  178. }));
  179. $actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
  180. $this->assertTrue($actual);
  181. }
  182. public function testIsCachedNoFile() {
  183. $fileNameCSS = "styles.css";
  184. $folder = $this->createMock(ISimpleFolder::class);
  185. $folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willThrowException(new NotFoundException());
  186. $actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, $folder]);
  187. $this->assertFalse($actual);
  188. }
  189. public function testIsCachedNoDepsFile() {
  190. $fileNameCSS = "styles.css";
  191. $folder = $this->createMock(ISimpleFolder::class);
  192. $file = $this->createMock(ISimpleFile::class);
  193. $file->expects($this->once())->method('getSize')->willReturn(1);
  194. $folder->method('getFile')
  195. ->will($this->returnCallback(function($path) use ($file) {
  196. if ($path === 'styles.css') {
  197. return $file;
  198. } else if ($path === 'styles.css.deps') {
  199. throw new NotFoundException();
  200. } else {
  201. $this->fail();
  202. }
  203. }));
  204. $actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, $folder]);
  205. $this->assertFalse($actual);
  206. }
  207. public function testCacheNoFile() {
  208. $fileNameCSS = "styles.css";
  209. $fileNameSCSS = "styles.scss";
  210. $folder = $this->createMock(ISimpleFolder::class);
  211. $file = $this->createMock(ISimpleFile::class);
  212. $depsFile = $this->createMock(ISimpleFile::class);
  213. $gzipFile = $this->createMock(ISimpleFile::class);
  214. $webDir = "core/css";
  215. $path = \OC::$SERVERROOT . '/core/css/';
  216. $folder->method('getFile')->willThrowException(new NotFoundException());
  217. $folder->method('newFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
  218. if ($fileName === 'styles.css') {
  219. return $file;
  220. } else if ($fileName === 'styles.css.deps') {
  221. return $depsFile;
  222. } else if ($fileName === 'styles.css.gzip') {
  223. return $gzipFile;
  224. }
  225. throw new \Exception();
  226. }));
  227. $file->expects($this->once())->method('putContent');
  228. $depsFile->expects($this->once())->method('putContent');
  229. $gzipFile->expects($this->once())->method('putContent');
  230. $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
  231. $this->assertTrue($actual);
  232. }
  233. public function testCache() {
  234. $fileNameCSS = "styles.css";
  235. $fileNameSCSS = "styles.scss";
  236. $folder = $this->createMock(ISimpleFolder::class);
  237. $file = $this->createMock(ISimpleFile::class);
  238. $depsFile = $this->createMock(ISimpleFile::class);
  239. $gzipFile = $this->createMock(ISimpleFile::class);
  240. $webDir = "core/css";
  241. $path = \OC::$SERVERROOT;
  242. $folder->method('getFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
  243. if ($fileName === 'styles.css') {
  244. return $file;
  245. } else if ($fileName === 'styles.css.deps') {
  246. return $depsFile;
  247. } else if ($fileName === 'styles.css.gzip') {
  248. return $gzipFile;
  249. }
  250. throw new \Exception();
  251. }));
  252. $file->expects($this->once())->method('putContent');
  253. $depsFile->expects($this->once())->method('putContent');
  254. $gzipFile->expects($this->once())->method('putContent');
  255. $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
  256. $this->assertTrue($actual);
  257. }
  258. public function testCacheSuccess() {
  259. $fileNameCSS = "styles-success.css";
  260. $fileNameSCSS = "../../tests/data/scss/styles-success.scss";
  261. $folder = $this->createMock(ISimpleFolder::class);
  262. $file = $this->createMock(ISimpleFile::class);
  263. $depsFile = $this->createMock(ISimpleFile::class);
  264. $gzipFile = $this->createMock(ISimpleFile::class);
  265. $webDir = "tests/data/scss";
  266. $path = \OC::$SERVERROOT . $webDir;
  267. $folder->method('getFile')->will($this->returnCallback(function($fileName) use ($file, $depsFile, $gzipFile) {
  268. if ($fileName === 'styles-success.css') {
  269. return $file;
  270. } else if ($fileName === 'styles-success.css.deps') {
  271. return $depsFile;
  272. } else if ($fileName === 'styles-success.css.gzip') {
  273. return $gzipFile;
  274. }
  275. throw new \Exception();
  276. }));
  277. $file->expects($this->at(0))->method('putContent')->with($this->callback(
  278. function ($content){
  279. return 'body{background-color:#0082c9}' === $content;
  280. }));
  281. $depsFile->expects($this->at(0))->method('putContent')->with($this->callback(
  282. function ($content) {
  283. $deps = json_decode($content, true);
  284. return array_key_exists(\OC::$SERVERROOT . '/core/css/variables.scss', $deps)
  285. && array_key_exists(\OC::$SERVERROOT . '/tests/data/scss/styles-success.scss', $deps);
  286. }));
  287. $gzipFile->expects($this->at(0))->method('putContent')->with($this->callback(
  288. function ($content) {
  289. return gzdecode($content) === 'body{background-color:#0082c9}';
  290. }
  291. ));
  292. $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
  293. $this->assertTrue($actual);
  294. }
  295. public function testCacheFailure() {
  296. $fileNameCSS = "styles-error.css";
  297. $fileNameSCSS = "../../tests/data/scss/styles-error.scss";
  298. $folder = $this->createMock(ISimpleFolder::class);
  299. $file = $this->createMock(ISimpleFile::class);
  300. $depsFile = $this->createMock(ISimpleFile::class);
  301. $webDir = "/tests/data/scss";
  302. $path = \OC::$SERVERROOT . $webDir;
  303. $folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willReturn($file);
  304. $folder->expects($this->at(1))->method('getFile')->with($fileNameCSS . '.deps')->willReturn($depsFile);
  305. $actual = self::invokePrivate($this->scssCacher, 'cache', [$path, $fileNameCSS, $fileNameSCSS, $folder, $webDir]);
  306. $this->assertFalse($actual);
  307. }
  308. public function testRebaseUrls() {
  309. $webDir = 'apps/files/css';
  310. $css = '#id { background-image: url(\'../img/image.jpg\'); }';
  311. $actual = self::invokePrivate($this->scssCacher, 'rebaseUrls', [$css, $webDir]);
  312. $expected = '#id { background-image: url(\'../../../apps/files/css/../img/image.jpg\'); }';
  313. $this->assertEquals($expected, $actual);
  314. }
  315. public function testRebaseUrlsIgnoreFrontendController() {
  316. $this->config->expects($this->once())->method('getSystemValue')->with('htaccess.IgnoreFrontController', false)->willReturn(true);
  317. $webDir = 'apps/files/css';
  318. $css = '#id { background-image: url(\'../img/image.jpg\'); }';
  319. $actual = self::invokePrivate($this->scssCacher, 'rebaseUrls', [$css, $webDir]);
  320. $expected = '#id { background-image: url(\'../../apps/files/css/../img/image.jpg\'); }';
  321. $this->assertEquals($expected, $actual);
  322. }
  323. public function dataGetCachedSCSS() {
  324. return [
  325. ['core', 'core/css/styles.scss', '/css/core/styles.css'],
  326. ['files', 'apps/files/css/styles.scss', '/css/files/styles.css']
  327. ];
  328. }
  329. /**
  330. * @param $appName
  331. * @param $fileName
  332. * @param $result
  333. * @dataProvider dataGetCachedSCSS
  334. */
  335. public function testGetCachedSCSS($appName, $fileName, $result) {
  336. $this->urlGenerator->expects($this->once())
  337. ->method('linkToRoute')
  338. ->with('core.Css.getCss', [
  339. 'fileName' => md5('http://localhost/nextcloud') . '-styles.css',
  340. 'appName' => $appName
  341. ])
  342. ->willReturn(\OC::$WEBROOT . $result);
  343. $actual = $this->scssCacher->getCachedSCSS($appName, $fileName);
  344. $this->assertEquals(substr($result, 1), $actual);
  345. }
  346. }