1
0

CSSResourceLocatorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Template;
  7. use OC\AppConfig;
  8. use OC\Files\AppData\AppData;
  9. use OC\Files\AppData\Factory;
  10. use OC\Template\CSSResourceLocator;
  11. use OCA\Theming\ThemingDefaults;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\Files\IAppData;
  14. use OCP\ICacheFactory;
  15. use OCP\IConfig;
  16. use OCP\IURLGenerator;
  17. use Psr\Log\LoggerInterface;
  18. class CSSResourceLocatorTest extends \Test\TestCase {
  19. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $appData;
  21. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  22. protected $urlGenerator;
  23. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  24. protected $config;
  25. /** @var ThemingDefaults|\PHPUnit\Framework\MockObject\MockObject */
  26. protected $themingDefaults;
  27. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  28. protected $cacheFactory;
  29. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  30. protected $logger;
  31. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  32. private $timeFactory;
  33. /** @var AppConfig|\PHPUnit\Framework\MockObject\MockObject */
  34. private $appConfig;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->logger = $this->createMock(LoggerInterface::class);
  38. $this->appData = $this->createMock(AppData::class);
  39. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  40. $this->config = $this->createMock(IConfig::class);
  41. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  42. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  43. $this->timeFactory = $this->createMock(ITimeFactory::class);
  44. $this->appConfig = $this->createMock(AppConfig::class);
  45. }
  46. private function cssResourceLocator() {
  47. /** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
  48. $factory = $this->createMock(Factory::class);
  49. $factory->method('get')->with('css')->willReturn($this->appData);
  50. return new CSSResourceLocator(
  51. $this->logger,
  52. 'theme',
  53. ['core' => 'map'],
  54. ['3rd' => 'party'],
  55. );
  56. }
  57. private function rrmdir($directory) {
  58. $files = array_diff(scandir($directory), ['.','..']);
  59. foreach ($files as $file) {
  60. if (is_dir($directory . '/' . $file)) {
  61. $this->rrmdir($directory . '/' . $file);
  62. } else {
  63. unlink($directory . '/' . $file);
  64. }
  65. }
  66. return rmdir($directory);
  67. }
  68. private function randomString() {
  69. return sha1(uniqid(mt_rand(), true));
  70. }
  71. public function testFindWithAppPathSymlink() {
  72. // First create new apps path, and a symlink to it
  73. $apps_dirname = $this->randomString();
  74. $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
  75. $new_apps_path_symlink = $new_apps_path . '_link';
  76. mkdir($new_apps_path);
  77. symlink($apps_dirname, $new_apps_path_symlink);
  78. // Create an app within that path
  79. mkdir($new_apps_path . '/' . 'test-css-app');
  80. // Use the symlink as the app path
  81. \OC::$APPSROOTS[] = [
  82. 'path' => $new_apps_path_symlink,
  83. 'url' => '/css-apps-test',
  84. 'writable' => false,
  85. ];
  86. $locator = $this->cssResourceLocator();
  87. $locator->find(['test-css-app/test-file']);
  88. $resources = $locator->getResources();
  89. $this->assertCount(1, $resources);
  90. $resource = $resources[0];
  91. $this->assertCount(3, $resource);
  92. $root = $resource[0];
  93. $webRoot = $resource[1];
  94. $file = $resource[2];
  95. $expectedRoot = $new_apps_path . '/test-css-app';
  96. $expectedWebRoot = \OC::$WEBROOT . '/css-apps-test/test-css-app';
  97. $expectedFile = 'test-file.css';
  98. $this->assertEquals($expectedRoot, $root,
  99. 'Ensure the app path symlink is resolved into the real path');
  100. $this->assertEquals($expectedWebRoot, $webRoot);
  101. $this->assertEquals($expectedFile, $file);
  102. array_pop(\OC::$APPSROOTS);
  103. unlink($new_apps_path_symlink);
  104. $this->rrmdir($new_apps_path);
  105. }
  106. }