CSSResourceLocatorTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Kyle Fazzari <kyrofa@ubuntu.com>
  4. *
  5. * @author Kyle Fazzari <kyrofa@ubuntu.com>
  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\AppConfig;
  25. use OC\Files\AppData\AppData;
  26. use OC\Files\AppData\Factory;
  27. use OC\Template\CSSResourceLocator;
  28. use OC\Template\IconsCacher;
  29. use OC\Template\SCSSCacher;
  30. use OCA\Theming\ThemingDefaults;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Files\IAppData;
  33. use OCP\ICacheFactory;
  34. use OCP\IConfig;
  35. use OCP\ILogger;
  36. use OCP\IURLGenerator;
  37. class CSSResourceLocatorTest extends \Test\TestCase {
  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 ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $cacheFactory;
  48. /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
  49. protected $logger;
  50. /** @var IconsCacher|\PHPUnit\Framework\MockObject\MockObject */
  51. protected $iconsCacher;
  52. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  53. private $timeFactory;
  54. /** @var AppConfig|\PHPUnit\Framework\MockObject\MockObject */
  55. private $appConfig;
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->logger = $this->createMock(ILogger::class);
  59. $this->appData = $this->createMock(AppData::class);
  60. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  61. $this->config = $this->createMock(IConfig::class);
  62. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  63. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  64. $this->iconsCacher = $this->createMock(IconsCacher::class);
  65. $this->timeFactory = $this->createMock(ITimeFactory::class);
  66. $this->appConfig = $this->createMock(AppConfig::class);
  67. }
  68. private function cssResourceLocator() {
  69. /** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
  70. $factory = $this->createMock(Factory::class);
  71. $factory->method('get')->with('css')->willReturn($this->appData);
  72. $scssCacher = new SCSSCacher(
  73. $this->logger,
  74. $factory,
  75. $this->urlGenerator,
  76. $this->config,
  77. $this->themingDefaults,
  78. \OC::$SERVERROOT,
  79. $this->cacheFactory,
  80. $this->iconsCacher,
  81. $this->timeFactory,
  82. $this->appConfig
  83. );
  84. return new CSSResourceLocator(
  85. $this->logger,
  86. 'theme',
  87. ['core' => 'map'],
  88. ['3rd' => 'party'],
  89. $scssCacher
  90. );
  91. }
  92. private function rrmdir($directory) {
  93. $files = array_diff(scandir($directory), ['.','..']);
  94. foreach ($files as $file) {
  95. if (is_dir($directory . '/' . $file)) {
  96. $this->rrmdir($directory . '/' . $file);
  97. } else {
  98. unlink($directory . '/' . $file);
  99. }
  100. }
  101. return rmdir($directory);
  102. }
  103. private function randomString() {
  104. return sha1(uniqid(mt_rand(), true));
  105. }
  106. public function testFindWithAppPathSymlink() {
  107. // First create new apps path, and a symlink to it
  108. $apps_dirname = $this->randomString();
  109. $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
  110. $new_apps_path_symlink = $new_apps_path . '_link';
  111. mkdir($new_apps_path);
  112. symlink($apps_dirname, $new_apps_path_symlink);
  113. // Create an app within that path
  114. mkdir($new_apps_path . '/' . 'test-css-app');
  115. // Use the symlink as the app path
  116. \OC::$APPSROOTS[] = [
  117. 'path' => $new_apps_path_symlink,
  118. 'url' => '/css-apps-test',
  119. 'writable' => false,
  120. ];
  121. $locator = $this->cssResourceLocator();
  122. $locator->find(['test-css-app/test-file']);
  123. $resources = $locator->getResources();
  124. $this->assertCount(1, $resources);
  125. $resource = $resources[0];
  126. $this->assertCount(3, $resource);
  127. $root = $resource[0];
  128. $webRoot = $resource[1];
  129. $file = $resource[2];
  130. $expectedRoot = $new_apps_path . '/test-css-app';
  131. $expectedWebRoot = \OC::$WEBROOT . '/css-apps-test/test-css-app';
  132. $expectedFile = 'test-file.css';
  133. $this->assertEquals($expectedRoot, $root,
  134. 'Ensure the app path symlink is resolved into the real path');
  135. $this->assertEquals($expectedWebRoot, $webRoot);
  136. $this->assertEquals($expectedFile, $file);
  137. array_pop(\OC::$APPSROOTS);
  138. unlink($new_apps_path_symlink);
  139. $this->rrmdir($new_apps_path);
  140. }
  141. }