JSResourceLocatorTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\SystemConfig;
  8. use OC\Template\JSCombiner;
  9. use OC\Template\JSResourceLocator;
  10. use OCP\App\AppPathNotFoundException;
  11. use OCP\App\IAppManager;
  12. use OCP\Files\IAppData;
  13. use OCP\ICacheFactory;
  14. use OCP\IURLGenerator;
  15. use Psr\Log\LoggerInterface;
  16. class JSResourceLocatorTest extends \Test\TestCase {
  17. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  18. protected $appData;
  19. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $urlGenerator;
  21. /** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
  22. protected $config;
  23. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  24. protected $cacheFactory;
  25. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  26. protected $logger;
  27. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  28. protected $appManager;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->appData = $this->createMock(IAppData::class);
  32. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  33. $this->config = $this->createMock(SystemConfig::class);
  34. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  35. $this->logger = $this->createMock(LoggerInterface::class);
  36. $this->appManager = $this->createMock(IAppManager::class);
  37. }
  38. private function jsResourceLocator() {
  39. $jsCombiner = new JSCombiner(
  40. $this->appData,
  41. $this->urlGenerator,
  42. $this->cacheFactory,
  43. $this->config,
  44. $this->logger
  45. );
  46. return new JSResourceLocator(
  47. $this->logger,
  48. $jsCombiner,
  49. $this->appManager,
  50. );
  51. }
  52. private function rrmdir($directory) {
  53. $files = array_diff(scandir($directory), ['.','..']);
  54. foreach ($files as $file) {
  55. if (is_dir($directory . '/' . $file)) {
  56. $this->rrmdir($directory . '/' . $file);
  57. } else {
  58. unlink($directory . '/' . $file);
  59. }
  60. }
  61. return rmdir($directory);
  62. }
  63. private function randomString() {
  64. return sha1(uniqid(mt_rand(), true));
  65. }
  66. public function testFindWithAppPathSymlink() {
  67. $appName = 'test-js-app';
  68. // First create new apps path, and a symlink to it
  69. $apps_dirname = $this->randomString();
  70. $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
  71. $new_apps_path_symlink = $new_apps_path . '_link';
  72. $this->assertTrue((
  73. mkdir($new_apps_path) && symlink($apps_dirname, $new_apps_path_symlink)
  74. ), 'Setup of apps path failed');
  75. // Create an app within that path
  76. $this->assertTrue((
  77. mkdir($new_apps_path . '/' . $appName) && touch($new_apps_path . '/' . $appName . '/' . 'test-file.js')
  78. ), 'Setup of app within the new apps path failed');
  79. // Use the symlink as the app path
  80. $this->appManager->expects($this->once())
  81. ->method('getAppPath')
  82. ->with($appName)
  83. ->willReturn("$new_apps_path_symlink/$appName");
  84. $this->appManager->expects($this->once())
  85. ->method('getAppWebPath')
  86. ->with($appName)
  87. ->willReturn("/js-apps-test/$appName");
  88. // Run the tests
  89. $locator = $this->jsResourceLocator();
  90. $locator->find(["$appName/test-file"]);
  91. $resources = $locator->getResources();
  92. $this->assertCount(1, $resources);
  93. $resource = $resources[0];
  94. $this->assertCount(3, $resource);
  95. $root = $resource[0];
  96. $webRoot = $resource[1];
  97. $file = $resource[2];
  98. $expectedRoot = $new_apps_path;
  99. $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test';
  100. $expectedFile = $appName . '/test-file.js';
  101. $this->assertEquals($expectedRoot, $root,
  102. 'Ensure the app path symlink is resolved into the real path');
  103. $this->assertEquals($expectedWebRoot, $webRoot);
  104. $this->assertEquals($expectedFile, $file);
  105. unlink($new_apps_path_symlink);
  106. $this->rrmdir($new_apps_path);
  107. }
  108. public function testNotExistingTranslationHandledSilent() {
  109. $this->appManager->expects($this->once())
  110. ->method('getAppPath')
  111. ->with('core')
  112. ->willThrowException(new AppPathNotFoundException());
  113. $this->appManager->expects($this->atMost(1))
  114. ->method('getAppWebPath')
  115. ->with('core')
  116. ->willThrowException(new AppPathNotFoundException());
  117. // Assert logger is not called
  118. $this->logger->expects($this->never())
  119. ->method('error');
  120. // Run the tests
  121. $locator = $this->jsResourceLocator();
  122. $locator->find(["core/l10n/en.js"]);
  123. $resources = $locator->getResources();
  124. $this->assertCount(0, $resources);
  125. }
  126. public function testFindModuleJSWithFallback() {
  127. // First create new apps path, and a symlink to it
  128. $apps_dirname = $this->randomString();
  129. $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
  130. mkdir($new_apps_path);
  131. // Create an app within that path
  132. mkdir("$new_apps_path/test-js-app");
  133. touch("$new_apps_path/test-js-app/module.mjs");
  134. touch("$new_apps_path/test-js-app/both.mjs");
  135. touch("$new_apps_path/test-js-app/both.js");
  136. touch("$new_apps_path/test-js-app/plain.js");
  137. // Use the app path
  138. $this->appManager->expects($this->any())
  139. ->method('getAppPath')
  140. ->with('test-js-app')
  141. ->willReturn("$new_apps_path/test-js-app");
  142. $locator = $this->jsResourceLocator();
  143. $locator->find(['test-js-app/module', 'test-js-app/both', 'test-js-app/plain']);
  144. $resources = $locator->getResources();
  145. $this->assertCount(3, $resources);
  146. $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
  147. $expectedFiles = ['module.mjs', 'both.mjs', 'plain.js'];
  148. for ($idx = 0; $idx++; $idx < 3) {
  149. $this->assertEquals($expectedWebRoot, $resources[$idx][1]);
  150. $this->assertEquals($expectedFiles[$idx], $resources[$idx][2]);
  151. }
  152. $this->rrmdir($new_apps_path);
  153. }
  154. }