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