View source
<?php
namespace Drupal\system\Tests\Render;
use Drupal\simpletest\WebTestBase;
class HtmlResponseAttachmentsTest extends WebTestBase {
public static $modules = [
'render_attached_test',
];
public function testAttachments() {
$this
->drupalGet('/render_attached_test/teapot');
$this
->assertResponse(418);
$this
->assertHeader('X-Drupal-Cache', 'MISS');
$this
->drupalGet('/render_attached_test/teapot');
$this
->assertResponse(418);
$this
->assertHeader('X-Drupal-Cache', 'HIT');
$this
->drupalGet('/render_attached_test/header');
$this
->assertTeapotHeaders();
$this
->assertHeader('X-Drupal-Cache', 'MISS');
$this
->drupalGet('/render_attached_test/header');
$this
->assertHeader('X-Drupal-Cache', 'HIT');
$this
->drupalGet('/render_attached_test/feed');
$this
->assertHeader('X-Drupal-Cache', 'MISS');
$this
->assertFeed();
$this
->drupalGet('/render_attached_test/feed');
$this
->assertHeader('X-Drupal-Cache', 'HIT');
$this
->drupalGet('/render_attached_test/head');
$this
->assertHeader('X-Drupal-Cache', 'MISS');
$this
->assertHead();
$this
->drupalGet('/render_attached_test/head');
$this
->assertHeader('X-Drupal-Cache', 'HIT');
}
public function testRenderCachedBlock() {
$this
->drupalPlaceBlock('attached_rendering_block', [
'region' => 'content',
]);
$this
->drupalGet('');
$this
->assertText('Markup from attached_rendering_block.');
$this
->assertFeed();
$this
->assertHead();
$this
->assertResponse(418);
$this
->assertTeapotHeaders();
$this
->drupalGet('');
$this
->assertText('Markup from attached_rendering_block.');
$this
->assertHeader('X-Test-Teapot', 'Teapot Mode Active');
}
protected function assertTeapotHeaders() {
$this
->assertHeader('X-Test-Teapot', 'Teapot Mode Active');
$this
->assertHeader('X-Test-Teapot-Replace', 'Teapot replaced');
$this
->assertHeader('X-Test-Teapot-No-Replace', 'This value is not replaced,This one is added');
}
protected function assertFeed() {
$test_meta = $this
->xpath('//head/link[@href="test://url"]');
$this
->assertEqual(1, count($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
->assertEqual($value, $test_meta_attributes[$attribute]);
}
}
}
protected function assertHead() {
$test_meta = $this
->xpath('//head/meta[@test-attribute="testvalue"]');
$this
->assertEqual(1, count($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 {
$test_meta_attributes = $test_meta
->attributes();
$this
->assertEqual($test_meta_attributes['test-attribute'], 'testvalue');
}
}
}