View source
<?php
namespace Drupal\Tests\resource_hints\Functional;
use Drupal\Tests\BrowserTestBase;
class ResourceHintsAttachmentTest extends BrowserTestBase {
public $profile = 'minimal';
protected $user;
public static $modules = [
'resource_hints',
];
protected function setUp() {
parent::setUp();
$this->user = $this
->drupalCreateUser([
'administer resource hints',
]);
}
public function testResourceHints() {
$this
->drupalLogin($this->user);
$assert = $this
->assertSession();
$this
->drupalGet('admin/config/development/performance/resources-hints');
$this
->submitForm([
'dns_prefetch_resources' => '//dns-prefetch.com',
], t('Save configuration'));
$assert
->responseHeaderContains('X-DNS-Prefetch-Control', 'on');
$assert
->responseHeaderContains('Link', '<//dns-prefetch.com>; rel="dns-prefetch"');
$assert
->elementNotExists('css', 'link[rel="dns-prefetch"][href="//dns-prefetch.com"]');
$this
->submitForm([
'dns_prefetch_output' => '1',
], t('Save configuration'));
$assert
->elementExists('css', 'meta[http-equiv="x-dns-prefetch-control"][content="on"]');
$assert
->responseHeaderNotContains('Link', '<//dns-prefetch.com>; rel="dns-prefetch"');
$assert
->elementExists('css', 'link[rel="dns-prefetch"][href="//dns-prefetch.com"]');
$this
->submitForm([
'dns_prefetch_output' => '0',
'dns_prefetch_control' => 'off',
], t('Save configuration'));
$assert
->responseHeaderContains('X-DNS-Prefetch-Control', 'off');
$assert
->responseHeaderNotContains('Link', '<//dns-prefetch.com>; rel="dns-prefetch"');
$assert
->elementNotExists('css', 'link[rel="dns-prefetch"][href="//dns-prefetch.com"]');
$this
->submitForm([
'dns_prefetch_output' => '1',
], t('Save configuration'));
$assert
->elementExists('css', 'meta[http-equiv="x-dns-prefetch-control"][content="off"]');
$assert
->responseHeaderNotContains('Link', '<//dns-prefetch.com>; rel="dns-prefetch"');
$assert
->elementNotExists('css', 'link[rel="dns-prefetch"][href="//dns-prefetch.com"]');
$this
->submitForm([
'preconnect_resources' => '//preconnect.com',
], t('Save configuration'));
$assert
->responseHeaderContains('Link', '<//preconnect.com>; rel="preconnect"');
$assert
->elementNotExists('css', 'link[rel="preconnect"][href="//preconnect.com"]');
$this
->submitForm([
'preconnect_output' => '1',
], t('Save configuration'));
$assert
->responseHeaderNotContains('Link', '<//preconnect.com>; rel="preconnect"');
$assert
->elementExists('css', 'link[rel="preconnect"][href="//preconnect.com"]');
$this
->submitForm([
'prefetch_resources' => '//prefetch.com',
], t('Save configuration'));
$assert
->responseHeaderContains('Link', '<//prefetch.com>; rel="prefetch"');
$assert
->elementNotExists('css', 'link[rel="prefetch"][href="//prefetch.com"]');
$this
->submitForm([
'prefetch_output' => '1',
], t('Save configuration'));
$assert
->responseHeaderNotContains('Link', '<//prefetch.com>; rel="prefetch"');
$assert
->elementExists('css', 'link[rel="prefetch"][href="//prefetch.com"]');
$this
->submitForm([
'prerender_resources' => '//prerender.com',
], t('Save configuration'));
$assert
->responseHeaderContains('Link', '<//prerender.com>; rel="prerender"');
$assert
->elementNotExists('css', 'link[rel="prerender"][href="//prerender.com"]');
$this
->submitForm([
'prerender_output' => '1',
], t('Save configuration'));
$assert
->responseHeaderNotContains('Link', '<//prerender.com>; rel="prerender"');
$assert
->elementExists('css', 'link[rel="prerender"][href="//prerender.com"]');
}
}