Security update to Drupal 8.4.6
[yaffs-website] / web / modules / contrib / http2_server_push / tests / src / Functional / LinkHeaderTest.php
1 <?php
2
3 namespace Drupal\Tests\http2_server_push\Functional;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\RoleInterface;
9
10 /**
11  * @group http2_server_push
12  */
13 class LinkHeaderTest extends BrowserTestBase {
14
15   /**
16    * Modules to install.
17    *
18    * @var array
19    */
20   public static $modules = ['toolbar', 'http2_server_push'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
28     // Grant anonymous users access to the toolbar, so the anonymous user gets
29     // some JavaScript.
30     $this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), ['access toolbar']);
31
32     // \Drupal\Tests\BrowserTestBase::installDrupal() overrides some of the
33     // defaults for easier test debugging. But for a CDN integration test, we do
34     // want the defaults to be applied, because that is what we want to test.
35     $this->config('system.performance')
36       ->set('css.preprocess', TRUE)
37       ->set('js.preprocess', TRUE)
38       ->save();
39   }
40
41   public function testCssJsLinkHeaders() {
42     $session = $this->getSession();
43
44     $html = $this->drupalGet('<front>');
45
46     // Determine the expected link response headers.
47     $expected_link_response_headers = [];
48     $document = Html::load($html);
49     $xpath = new \DOMXPath($document);
50     $dom_nodes = $xpath->query('//link[@rel="stylesheet"]');
51     foreach ($dom_nodes as $dom_node) {
52       $expected_link_response_headers[] = '<' . $dom_node->getAttribute('href') . '>; rel=preload; as=style';
53     }
54     $dom_nodes = $xpath->query('//script[@src]');
55     foreach ($dom_nodes as $dom_node) {
56       $expected_link_response_headers[] = '<' . $dom_node->getAttribute('src') . '>; rel=preload; as=script';
57     }
58
59     // Assert that the expected Server Push Link response headers are present.
60     $this->assertSame($expected_link_response_headers, $session->getResponseHeaders()['Link']);
61
62     // @todo test more explicitly that browser-conditional CSS and JS does not get a Server Push Link header. This is a bit difficult because DOMXPath automatically ignores this HTML.
63     $conditional_script_html = <<<HTML
64 <!--[if lte IE 8]>
65 <script
66 HTML;
67
68     $this->assertSession()->responseContains($conditional_script_html);
69   }
70
71 }