src/File/FileUrlGenerator.php | 10 ++++--- tests/src/Functional/CdnIntegrationTest.php | 41 +++++++++++++++++++---------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/File/FileUrlGenerator.php b/src/File/FileUrlGenerator.php index 7c71cff..b6dc0d1 100644 --- a/src/File/FileUrlGenerator.php +++ b/src/File/FileUrlGenerator.php @@ -139,9 +139,13 @@ class FileUrlGenerator implements FileUrlGeneratorInterface { if (!$scheme = StreamWrapperManager::getScheme($uri)) { $scheme = self::RELATIVE; - $relative_url = '/' . $uri; - $relative_file_path = $relative_url; - $absolute_file_path = $this->root . $relative_url; + $relative_url = !str_starts_with($uri, $this->getBasePath() . '/') + // Convert Drupal root-relative file URI to Drupal-root relative URL. + ? '/' . $uri + // Convert HTTP root-relative file URL to a Drupal root-relative URL. + : str_replace($this->getBasePath(), '', $uri); + $relative_file_path = parse_url($relative_url)['path']; + $absolute_file_path = $this->root . $relative_file_path; } else { $relative_url = str_replace($this->requestStack->getCurrentRequest()->getSchemeAndHttpHost() . $this->getBasePath(), '', $this->streamWrapperManager->getViaUri($uri)->getExternalUrl()); diff --git a/tests/src/Functional/CdnIntegrationTest.php b/tests/src/Functional/CdnIntegrationTest.php index 807feb8..0088958 100644 --- a/tests/src/Functional/CdnIntegrationTest.php +++ b/tests/src/Functional/CdnIntegrationTest.php @@ -106,27 +106,36 @@ class CdnIntegrationTest extends BrowserTestBase { $this->drupalGet(''); $this->assertSame('MISS', $session->getResponseHeader('X-Drupal-Cache'), 'Changing CDN settings causes Page Cache miss: setting changes have immediate effect.'); $href = $this->cssSelect('link[rel=stylesheet]')[0]->getAttribute('href'); - $regexp = '#/' . $this->siteDirectory . '/files/css/css_[a-zA-Z0-9_-]{43}\.css#'; - $this->assertSame(1, preg_match($regexp, $href)); - $this->assertCssFileUsesRootRelativeUrl($this->baseUrl . $href); + $regexp = '#^' . base_path() . $this->siteDirectory . '/files/css/css_[a-zA-Z0-9_-]{43}\.css\?delta=0&language=en&theme=stark&include=[a-zA-Z0-9_-]+$#'; + if (version_compare(\Drupal::VERSION, '10.1', '<')) { + $regexp = '#^' . base_path() . $this->siteDirectory . '/files/css/css_[a-zA-Z0-9_-]{43}\.css$#'; + } + $this->assertMatchesRegularExpression($regexp, $href); + $this->assertCssFileUsesRootRelativeUrl($href); // CDN enabled, "Forever cacheable files" disabled. $this->config('cdn.settings')->set('status', TRUE)->save(); $this->drupalGet(''); $this->assertSame('MISS', $session->getResponseHeader('X-Drupal-Cache'), 'Changing CDN settings causes Page Cache miss: setting changes have immediate effect.'); $href = $this->cssSelect('link[rel=stylesheet]')[0]->getAttribute('href'); - $regexp = '#//cdn.example.com' . base_path() . $this->siteDirectory . '/files/css/css_[a-zA-Z0-9_-]{43}\.css#'; - $this->assertSame(1, preg_match($regexp, $href)); - $this->assertCssFileUsesRootRelativeUrl($this->baseUrl . str_replace('//cdn.example.com', '', $href)); + $regexp = '#^//cdn.example.com' . base_path() . $this->siteDirectory . '/files/css/css_[a-zA-Z0-9_-]{43}\.css\?delta=0&language=en&theme=stark&include=[a-zA-Z0-9_-]+$#'; + if (version_compare(\Drupal::VERSION, '10.1', '<')) { + $regexp = '#^' . base_path() . $this->siteDirectory . '/files/css/css_[a-zA-Z0-9_-]{43}\.css$#'; + } + $this->assertMatchesRegularExpression($regexp, $href); + $this->assertCssFileUsesRootRelativeUrl(str_replace('//cdn.example.com', '', $href)); // CDN enabled, "Forever cacheable files" enabled. $this->config('cdn.settings')->set('farfuture.status', TRUE)->save(); $this->drupalGet(''); $this->assertSame('MISS', $session->getResponseHeader('X-Drupal-Cache'), 'Changing CDN settings causes Page Cache miss: setting changes have immediate effect.'); $href = $this->cssSelect('link[rel=stylesheet]')[0]->getAttribute('href'); - $regexp = '#//cdn.example.com' . base_path() . 'cdn/ff/[a-zA-Z0-9_-]{43}/[0-9]{10}/public/css/css_[a-zA-Z0-9_-]{43}\.css#'; - $this->assertSame(1, preg_match($regexp, $href)); - $this->assertCssFileUsesRootRelativeUrl($this->baseUrl . str_replace('//cdn.example.com', '', $href)); + $regexp = '#^//cdn.example.com' . base_path() . 'cdn/ff/[a-zA-Z0-9_-]{43}/[0-9]{10}/' . FileUrlGenerator::RELATIVE . '/' . $this->siteDirectory . '/files/css/css_[a-zA-Z0-9_-]{43}\.css$#'; + if (version_compare(\Drupal::VERSION, '10.1', '<')) { + $regexp = '#^//cdn.example.com' . base_path() . 'cdn/ff/[a-zA-Z0-9_-]{43}/[0-9]{10}/public/css/css_[a-zA-Z0-9_-]{43}\.css$#'; + } + $this->assertMatchesRegularExpression($regexp, $href); + $this->assertCssFileUsesRootRelativeUrl(str_replace('//cdn.example.com', '', $href)); } /** @@ -135,12 +144,13 @@ class CdnIntegrationTest extends BrowserTestBase { * @param string $css_file_url * The URL to a CSS file. */ - protected function assertCssFileUsesRootRelativeUrl($css_file_url) { - $this->drupalGet($css_file_url); + protected function assertCssFileUsesRootRelativeUrl(string $css_file_url): void { + $this->getSession()->visit($css_file_url); + $css = $this->getSession()->getPage()->getContent(); // CSS references other files. - $this->assertSession()->responseContains('url('); + $this->assertStringContainsString('url(', $css); // CSS references other files by root-relative URL, not CDN URL. - $this->assertSession()->responseContains('url(' . base_path() . 'core/misc/tree.png)'); + $this->assertStringContainsString('url(' . base_path() . 'core/misc/tree.png)', $css); } /** @@ -240,7 +250,10 @@ class CdnIntegrationTest extends BrowserTestBase { 'core/modules/system/js/system.modules.js', FileUrlGenerator::RELATIVE, 'core/modules/system/js/system.modules.js', - 'application/javascript', + // @see https://www.drupal.org/node/3312139 + version_compare(\Drupal::VERSION, '10.1', '>=') + ? 'text/javascript; charset=UTF-8' + : 'application/javascript', ], ]; }