function HtmlResponseAttachmentsTest::testAttachments

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Render/HtmlResponseAttachmentsTest.php \Drupal\Tests\system\Functional\Render\HtmlResponseAttachmentsTest::testAttachments()
  2. 8.9.x core/modules/system/tests/src/Functional/Render/HtmlResponseAttachmentsTest.php \Drupal\Tests\system\Functional\Render\HtmlResponseAttachmentsTest::testAttachments()
  3. 10 core/modules/system/tests/src/Functional/Render/HtmlResponseAttachmentsTest.php \Drupal\Tests\system\Functional\Render\HtmlResponseAttachmentsTest::testAttachments()

Tests rendering of ['#attached'].

File

core/modules/system/tests/src/Functional/Render/HtmlResponseAttachmentsTest.php, line 29

Class

HtmlResponseAttachmentsTest
Functional tests for HtmlResponseAttachmentsProcessor.

Namespace

Drupal\Tests\system\Functional\Render

Code

public function testAttachments() : void {
    // Test ['#attached']['http_header] = ['Status', $code].
    $this->drupalGet('/render_attached_test/teapot');
    $this->assertSession()
        ->statusCodeEquals(418);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    // Repeat for the cache.
    $this->drupalGet('/render_attached_test/teapot');
    $this->assertSession()
        ->statusCodeEquals(418);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    // Test ['#attached']['http_header'] with various replacement rules.
    $this->drupalGet('/render_attached_test/header');
    $this->assertTeapotHeaders();
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    // Repeat for the cache.
    $this->drupalGet('/render_attached_test/header');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    // Test ['#attached']['feed'].
    $this->drupalGet('/render_attached_test/feed');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertFeed();
    // Repeat for the cache.
    $this->drupalGet('/render_attached_test/feed');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    // Test ['#attached']['html_head'].
    $this->drupalGet('/render_attached_test/head');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertHead();
    // Repeat for the cache.
    $this->drupalGet('/render_attached_test/head');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    // Test ['#attached']['html_head_link'] when outputted as HTTP header.
    $this->drupalGet('/render_attached_test/html_header_link');
    $expected_link_headers = [
        '</foo?bar=<baz>&baz=false>; rel="alternate"',
        '</foo/bar>; hreflang="nl"; rel="alternate"',
        '</foo/bar>; hreflang="de"; rel="alternate"',
    ];
    $this->assertEquals($expected_link_headers, $this->getSession()
        ->getResponseHeaders()['Link']);
    // Check that duplicate alternate URLs with different hreflang attributes
    // are allowed.
    $this->assertSession()
        ->elementsCount('xpath', '//head/link[@rel="alternate"][@href="/foo/bar"]', 2);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.