View source
<?php
namespace Drupal\Tests\system\Functional\Render;
use Drupal\Tests\BrowserTestBase;
class HtmlResponseAttachmentsTest extends BrowserTestBase {
protected static $modules = [
'render_attached_test',
];
protected $defaultTheme = 'stark';
public function testAttachments() {
$this
->drupalGet('/render_attached_test/teapot');
$this
->assertSession()
->statusCodeEquals(418);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->drupalGet('/render_attached_test/teapot');
$this
->assertSession()
->statusCodeEquals(418);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this
->drupalGet('/render_attached_test/header');
$this
->assertTeapotHeaders();
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->drupalGet('/render_attached_test/header');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this
->drupalGet('/render_attached_test/feed');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->assertFeed();
$this
->drupalGet('/render_attached_test/feed');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this
->drupalGet('/render_attached_test/head');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->assertHead();
$this
->drupalGet('/render_attached_test/head');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this
->drupalGet('/render_attached_test/html_header_link');
$expected_link_headers = [
'</foo?bar=<baz>&baz=false>; rel="alternate"',
'</foo/bar>; hreflang="nl"; rel="alternate"',
];
$this
->assertEquals($expected_link_headers, $this
->getSession()
->getResponseHeaders()['Link']);
}
public function testRenderCachedBlock() {
$this
->drupalPlaceBlock('attached_rendering_block', [
'region' => 'content',
]);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains('Markup from attached_rendering_block.');
$this
->assertFeed();
$this
->assertHead();
$this
->assertSession()
->statusCodeEquals(418);
$this
->assertTeapotHeaders();
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains('Markup from attached_rendering_block.');
$this
->assertSession()
->responseHeaderEquals('X-Test-Teapot', 'Teapot Mode Active');
}
protected function assertTeapotHeaders() {
$this
->assertSession()
->responseHeaderEquals('X-Test-Teapot', 'Teapot Mode Active');
$this
->assertSession()
->responseHeaderEquals('X-Test-Teapot-Replace', 'Teapot replaced');
$this
->assertSession()
->responseHeaderEquals('X-Test-Teapot-No-Replace', 'This value is not replaced');
}
protected function assertFeed() {
$test_meta = $this
->xpath('//head/link[@href="test://url"]');
$this
->assertCount(1, $test_meta, 'Link has URL.');
$test_meta_attributes = [
'href' => 'test://url',
'rel' => 'alternate',
'type' => 'application/rss+xml',
'title' => 'Your RSS feed.',
];
$test_meta = reset($test_meta);
if (empty($test_meta)) {
$this
->fail('Unable to find feed link.');
}
else {
foreach ($test_meta_attributes as $attribute => $value) {
$this
->assertEquals($value, $test_meta
->getAttribute($attribute));
}
}
}
protected function assertHead() {
$test_meta = $this
->xpath('//head/meta[@test-attribute="testvalue"]');
$this
->assertCount(1, $test_meta, 'There\'s only one test attribute.');
$test_meta = reset($test_meta);
if (empty($test_meta)) {
$this
->fail('Unable to find the head meta.');
}
else {
$this
->assertEquals('testvalue', $test_meta
->getAttribute('test-attribute'));
}
}
}