You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSResourceLocatorTest.php 6.2KB

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