JSResourceLocatorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\SystemConfig;
  25. use OC\Template\JSCombiner;
  26. use OC\Template\JSResourceLocator;
  27. use OCP\Files\IAppData;
  28. use OCP\ICacheFactory;
  29. use OCP\IURLGenerator;
  30. use Psr\Log\LoggerInterface;
  31. class JSResourceLocatorTest extends \Test\TestCase {
  32. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  33. protected $appData;
  34. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  35. protected $urlGenerator;
  36. /** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $config;
  38. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $cacheFactory;
  40. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $logger;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->appData = $this->createMock(IAppData::class);
  45. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  46. $this->config = $this->createMock(SystemConfig::class);
  47. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  48. $this->logger = $this->createMock(LoggerInterface::class);
  49. }
  50. private function jsResourceLocator() {
  51. $jsCombiner = new JSCombiner(
  52. $this->appData,
  53. $this->urlGenerator,
  54. $this->cacheFactory,
  55. $this->config,
  56. $this->logger
  57. );
  58. return new JSResourceLocator(
  59. $this->logger,
  60. $jsCombiner
  61. );
  62. }
  63. private function rrmdir($directory) {
  64. $files = array_diff(scandir($directory), ['.','..']);
  65. foreach ($files as $file) {
  66. if (is_dir($directory . '/' . $file)) {
  67. $this->rrmdir($directory . '/' . $file);
  68. } else {
  69. unlink($directory . '/' . $file);
  70. }
  71. }
  72. return rmdir($directory);
  73. }
  74. private function randomString() {
  75. return sha1(uniqid(mt_rand(), true));
  76. }
  77. public function testFindWithAppPathSymlink() {
  78. // First create new apps path, and a symlink to it
  79. $apps_dirname = $this->randomString();
  80. $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
  81. $new_apps_path_symlink = $new_apps_path . '_link';
  82. mkdir($new_apps_path);
  83. symlink($apps_dirname, $new_apps_path_symlink);
  84. // Create an app within that path
  85. mkdir($new_apps_path . '/' . 'test-js-app');
  86. // Use the symlink as the app path
  87. \OC::$APPSROOTS[] = [
  88. 'path' => $new_apps_path_symlink,
  89. 'url' => '/js-apps-test',
  90. 'writable' => false,
  91. ];
  92. $locator = $this->jsResourceLocator();
  93. $locator->find(['test-js-app/test-file']);
  94. $resources = $locator->getResources();
  95. $this->assertCount(1, $resources);
  96. $resource = $resources[0];
  97. $this->assertCount(3, $resource);
  98. $root = $resource[0];
  99. $webRoot = $resource[1];
  100. $file = $resource[2];
  101. $expectedRoot = $new_apps_path . '/test-js-app';
  102. $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
  103. $expectedFile = 'test-file.js';
  104. $this->assertEquals($expectedRoot, $root,
  105. 'Ensure the app path symlink is resolved into the real path');
  106. $this->assertEquals($expectedWebRoot, $webRoot);
  107. $this->assertEquals($expectedFile, $file);
  108. array_pop(\OC::$APPSROOTS);
  109. unlink($new_apps_path_symlink);
  110. $this->rrmdir($new_apps_path);
  111. }
  112. }