SCSSCacherTest.php 13 KB

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