View source
<?php
namespace Drupal\Tests\cdn\Functional;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Site\Settings;
use Drupal\file\Entity\File;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
class CdnIntegrationTest extends BrowserTestBase {
protected static $modules = [
'node',
'cdn',
'file',
'editor',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$format = $this
->randomMachineName();
FilterFormat::create([
'format' => $format,
'name' => $this
->randomString(),
'weight' => 0,
'filters' => [
'editor_file_reference' => [
'status' => 1,
'weight' => 0,
],
],
])
->save();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
file_put_contents('public://druplicon ❤️.png', $this
->randomMachineName());
$image = File::create([
'uri' => 'public://druplicon ❤️.png',
]);
$image
->save();
$uuid = $image
->uuid();
$this
->drupalCreateNode([
'type' => 'article',
'body' => [
0 => [
'value' => '<p>Do you also love Drupal?</p><img src="druplicon ❤️.png" data-caption="Druplicon" data-entity-type="file" data-entity-uuid="' . $uuid . '" />',
'format' => $format,
],
],
]);
$this
->config('cdn.settings')
->set('mapping', [
'type' => 'simple',
'domain' => 'cdn.example.com',
])
->set('status', TRUE)
->set('farfuture', [
'status' => FALSE,
])
->save();
$this
->config('system.performance')
->set('css.preprocess', TRUE)
->set('js.preprocess', TRUE)
->save();
}
public function testCss() {
$session = $this
->getSession();
$this
->drupalGet('<front>');
$this
->assertSame('MISS', $session
->getResponseHeader('X-Drupal-Cache'));
$this
->drupalGet('<front>');
$this
->assertSame('HIT', $session
->getResponseHeader('X-Drupal-Cache'));
$this
->config('cdn.settings')
->set('status', FALSE)
->save();
$this
->drupalGet('<front>');
$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);
$this
->config('cdn.settings')
->set('status', TRUE)
->save();
$this
->drupalGet('<front>');
$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));
$this
->config('cdn.settings')
->set('farfuture.status', TRUE)
->save();
$this
->drupalGet('<front>');
$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));
}
protected function assertCssFileUsesRootRelativeUrl($css_file_url) {
$this
->drupalGet($css_file_url);
$this
->assertSession()
->responseContains('url(');
$this
->assertSession()
->responseContains('url(' . base_path() . 'core/misc/tree.png)');
}
public function testUpdatePhp() {
$session = $this
->getSession();
$this
->writeSettings([
'settings' => [
'update_free_access' => (object) [
'value' => TRUE,
'required' => TRUE,
],
],
]);
$this
->drupalGet('update.php');
foreach ($session
->getPage()
->findAll('css', 'html > head > link[rel=stylesheet],link[rel="shortcut icon"]') as $node) {
$this
->assertStringStartsNotWith('//cdn.example.com', $node
->getAttribute('href'));
}
}
public function testUninstall() {
$session = $this
->getSession();
$this
->drupalGet('/node/1');
$this
->assertSame('MISS', $session
->getResponseHeader('X-Drupal-Cache'));
$this
->assertSession()
->responseContains('src="//cdn.example.com' . base_path() . $this->siteDirectory . '/files/' . UrlHelper::encodePath('druplicon ❤️.png') . '"');
$this
->drupalGet('/node/1');
$this
->assertSame('HIT', $session
->getResponseHeader('X-Drupal-Cache'));
\Drupal::service('module_installer')
->uninstall([
'cdn',
]);
$this
->assertTrue(TRUE, 'Uninstalled CDN module.');
$this
->drupalGet('/node/1');
$this
->assertSame('MISS', $session
->getResponseHeader('X-Drupal-Cache'));
$this
->assertSession()
->responseContains('src="' . base_path() . $this->siteDirectory . '/files/' . UrlHelper::encodePath('druplicon ❤️.png') . '"');
}
public function testOldFarfuture() {
$druplicon_png_mtime = filemtime('public://druplicon ❤️.png');
$druplicon_png_security_token = Crypt::hmacBase64($druplicon_png_mtime . '/' . $this->siteDirectory . '/files/' . UrlHelper::encodePath('druplicon ❤️.png'), \Drupal::service('private_key')
->get() . Settings::getHashSalt());
$this
->drupalGet('/cdn/farfuture/' . $druplicon_png_security_token . '/' . $druplicon_png_mtime . '/' . $this->siteDirectory . '/files/druplicon ❤️.png');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSame('Wed, 20 Jan 1988 04:20:42 GMT', $this
->getSession()
->getResponseHeader('Last-Modified'));
$this
->assertSame('bytes', $this
->getSession()
->getResponseHeader('Accept-Ranges'));
$this
->drupalGet('/cdn/farfuture/' . substr($druplicon_png_security_token, 1) . '/' . $druplicon_png_mtime . '/sites/default/files/druplicon ❤️.png');
$this
->assertSession()
->statusCodeEquals(403);
}
public function testFarfuture() {
$druplicon_png_mtime = filemtime('public://druplicon ❤️.png');
$druplicon_png_security_token = Crypt::hmacBase64($druplicon_png_mtime . 'public' . UrlHelper::encodePath('/druplicon ❤️.png'), \Drupal::service('private_key')
->get() . Settings::getHashSalt());
$druplicon_png_relative_security_token = Crypt::hmacBase64($druplicon_png_mtime . ':relative:' . UrlHelper::encodePath('/' . $this->siteDirectory . '/files/druplicon ❤️.png'), \Drupal::service('private_key')
->get() . Settings::getHashSalt());
$this
->drupalGet('/cdn/ff/' . $druplicon_png_security_token . '/' . $druplicon_png_mtime . '/public/druplicon ❤️.png');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('/cdn/ff/' . $druplicon_png_relative_security_token . '/' . $druplicon_png_mtime . '/:relative:/' . $this->siteDirectory . '/files/druplicon ❤️.png');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSame('Wed, 20 Jan 1988 04:20:42 GMT', $this
->getSession()
->getResponseHeader('Last-Modified'));
$this
->assertSame('bytes', $this
->getSession()
->getResponseHeader('Accept-Ranges'));
$this
->drupalGet('/cdn/ff/' . substr($druplicon_png_security_token, 1) . '/' . $druplicon_png_mtime . '/public/druplicon ❤️.png');
$this
->assertSession()
->statusCodeEquals(403);
}
}