SCSSCacherTest.php 13 KB

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