summaryrefslogtreecommitdiffstats
path: root/tests/lib
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2023-03-22 09:17:28 +0100
committerGitHub <noreply@github.com>2023-03-22 09:17:28 +0100
commit8b4d49cf506d202c4b7ab58f453cdc84a30bca38 (patch)
treecfaaa6ee6ea356014349417788f0125dae3a4304 /tests/lib
parent9c7d7139b6d7487275c5ad18645a0770c23761d9 (diff)
parentd461da3b04dfb2d6b7e6bad8ebf9b592d3008f20 (diff)
downloadnextcloud-server-8b4d49cf506d202c4b7ab58f453cdc84a30bca38.tar.gz
nextcloud-server-8b4d49cf506d202c4b7ab58f453cdc84a30bca38.zip
Merge pull request #36057 from nextcloud/feat/esm-js-scripts
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/Template/JSResourceLocatorTest.php76
-rw-r--r--tests/lib/TemplateFunctionsTest.php29
2 files changed, 93 insertions, 12 deletions
diff --git a/tests/lib/Template/JSResourceLocatorTest.php b/tests/lib/Template/JSResourceLocatorTest.php
index 20fd79a91b5..627fe676680 100644
--- a/tests/lib/Template/JSResourceLocatorTest.php
+++ b/tests/lib/Template/JSResourceLocatorTest.php
@@ -26,6 +26,7 @@ namespace Test\Template;
use OC\SystemConfig;
use OC\Template\JSCombiner;
use OC\Template\JSResourceLocator;
+use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\ICacheFactory;
use OCP\IURLGenerator;
@@ -42,6 +43,8 @@ class JSResourceLocatorTest extends \Test\TestCase {
protected $cacheFactory;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
protected $logger;
+ /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
+ protected $appManager;
protected function setUp(): void {
parent::setUp();
@@ -51,6 +54,7 @@ class JSResourceLocatorTest extends \Test\TestCase {
$this->config = $this->createMock(SystemConfig::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
+ $this->appManager = $this->createMock(IAppManager::class);
}
private function jsResourceLocator() {
@@ -63,7 +67,8 @@ class JSResourceLocatorTest extends \Test\TestCase {
);
return new JSResourceLocator(
$this->logger,
- $jsCombiner
+ $jsCombiner,
+ $this->appManager,
);
}
@@ -84,25 +89,34 @@ class JSResourceLocatorTest extends \Test\TestCase {
}
public function testFindWithAppPathSymlink() {
+ $appName = 'test-js-app';
+
// First create new apps path, and a symlink to it
$apps_dirname = $this->randomString();
$new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
$new_apps_path_symlink = $new_apps_path . '_link';
- mkdir($new_apps_path);
- symlink($apps_dirname, $new_apps_path_symlink);
+ $this->assertTrue((
+ mkdir($new_apps_path) && symlink($apps_dirname, $new_apps_path_symlink)
+ ), 'Setup of apps path failed');
// Create an app within that path
- mkdir($new_apps_path . '/' . 'test-js-app');
+ $this->assertTrue((
+ mkdir($new_apps_path . '/' . $appName) && touch($new_apps_path . '/' . $appName . '/' . 'test-file.js')
+ ), 'Setup of app within the new apps path failed');
// Use the symlink as the app path
- \OC::$APPSROOTS[] = [
- 'path' => $new_apps_path_symlink,
- 'url' => '/js-apps-test',
- 'writable' => false,
- ];
-
+ $this->appManager->expects($this->once())
+ ->method('getAppPath')
+ ->with($appName)
+ ->willReturn("$new_apps_path_symlink/$appName");
+ $this->appManager->expects($this->once())
+ ->method('getAppWebPath')
+ ->with($appName)
+ ->willReturn("/js-apps-test/$appName");
+
+ // Run the tests
$locator = $this->jsResourceLocator();
- $locator->find(['test-js-app/test-file']);
+ $locator->find(["$appName/test-file"]);
$resources = $locator->getResources();
$this->assertCount(1, $resources);
@@ -122,7 +136,45 @@ class JSResourceLocatorTest extends \Test\TestCase {
$this->assertEquals($expectedFile, $file);
array_pop(\OC::$APPSROOTS);
- unlink($new_apps_path_symlink);
+ //unlink($new_apps_path_symlink);
+ //$this->rrmdir($new_apps_path);
+ }
+
+ public function testFindModuleJSWithFallback() {
+ // First create new apps path, and a symlink to it
+ $apps_dirname = $this->randomString();
+ $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
+ mkdir($new_apps_path);
+
+ // Create an app within that path
+ mkdir("$new_apps_path/test-js-app");
+ touch("$new_apps_path/test-js-app/module.mjs");
+ touch("$new_apps_path/test-js-app/both.mjs");
+ touch("$new_apps_path/test-js-app/both.js");
+ touch("$new_apps_path/test-js-app/plain.js");
+
+ // Use the app path
+ $this->appManager->expects($this->any())
+ ->method('getAppPath')
+ ->with('test-js-app')
+ ->willReturn("$new_apps_path/test-js-app");
+
+ $locator = $this->jsResourceLocator();
+ $locator->find(['test-js-app/module', 'test-js-app/both', 'test-js-app/plain']);
+
+ $resources = $locator->getResources();
+ $this->assertCount(3, $resources);
+
+ $expectedRoot = $new_apps_path . '/test-js-app';
+ $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
+ $expectedFiles = ['module.mjs', 'both.mjs', 'plain.js'];
+
+ for ($idx = 0; $idx++; $idx < 3) {
+ $this->assertEquals($expectedWebRoot, $resources[$idx][1]);
+ $this->assertEquals($expectedFiles[$idx], $resources[$idx][2]);
+ }
+
+ array_pop(\OC::$APPSROOTS);
$this->rrmdir($new_apps_path);
}
}
diff --git a/tests/lib/TemplateFunctionsTest.php b/tests/lib/TemplateFunctionsTest.php
index caecdfc76ac..b2b25ab654c 100644
--- a/tests/lib/TemplateFunctionsTest.php
+++ b/tests/lib/TemplateFunctionsTest.php
@@ -57,6 +57,35 @@ class TemplateFunctionsTest extends \Test\TestCase {
print_unescaped($string);
}
+ public function testEmitScriptTagWithContent() {
+ $this->expectOutputRegex('/<script nonce="[^"]+">\nalert\(\)\n<\/script>\n?/');
+ emit_script_tag('', 'alert()');
+ }
+
+ public function testEmitScriptTagWithSource() {
+ $this->expectOutputRegex('/<script nonce=".*" defer src="some.js"><\/script>/');
+ emit_script_tag('some.js');
+ }
+
+ public function testEmitScriptTagWithModuleSource() {
+ $this->expectOutputRegex('/<script nonce=".*" defer src="some.mjs" type="module"><\/script>/');
+ emit_script_tag('some.mjs', '', 'module');
+ }
+
+ public function testEmitScriptLoadingTags() {
+ // Test mjs js and inline content
+ $pattern = '/src="some\.mjs"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
+ $pattern .= '<script[^>]+src="other\.js"[^>]*>.+\n'; // other.js as plain javascript
+ $pattern .= '<script[^>]*>\n?.*inline.*\n?<\/script>'; // inline content
+ $pattern .= '/'; // no flags
+
+ $this->expectOutputRegex($pattern);
+ emit_script_loading_tags([
+ 'jsfiles' => ['some.mjs', 'other.js'],
+ 'inline_ocjs' => '// inline'
+ ]);
+ }
+
// ---------------------------------------------------------------------------
// Test relative_modified_date with dates only
// ---------------------------------------------------------------------------