CSSResourceLocatorTest.php 5.3 KB

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