3 namespace Drupal\system\Tests\Render;
5 use Drupal\simpletest\WebTestBase;
8 * Functional tests for HtmlResponseAttachmentsProcessor.
12 class HtmlResponseAttachmentsTest extends WebTestBase {
19 public static $modules = ['render_attached_test'];
22 * Test rendering of ['#attached'].
24 public function testAttachments() {
25 // Test ['#attached']['http_header] = ['Status', $code].
26 $this->drupalGet('/render_attached_test/teapot');
27 $this->assertResponse(418);
28 $this->assertHeader('X-Drupal-Cache', 'MISS');
29 // Repeat for the cache.
30 $this->drupalGet('/render_attached_test/teapot');
31 $this->assertResponse(418);
32 $this->assertHeader('X-Drupal-Cache', 'HIT');
34 // Test ['#attached']['http_header'] with various replacement rules.
35 $this->drupalGet('/render_attached_test/header');
36 $this->assertTeapotHeaders();
37 $this->assertHeader('X-Drupal-Cache', 'MISS');
38 // Repeat for the cache.
39 $this->drupalGet('/render_attached_test/header');
40 $this->assertHeader('X-Drupal-Cache', 'HIT');
42 // Test ['#attached']['feed'].
43 $this->drupalGet('/render_attached_test/feed');
44 $this->assertHeader('X-Drupal-Cache', 'MISS');
46 // Repeat for the cache.
47 $this->drupalGet('/render_attached_test/feed');
48 $this->assertHeader('X-Drupal-Cache', 'HIT');
50 // Test ['#attached']['html_head'].
51 $this->drupalGet('/render_attached_test/head');
52 $this->assertHeader('X-Drupal-Cache', 'MISS');
54 // Repeat for the cache.
55 $this->drupalGet('/render_attached_test/head');
56 $this->assertHeader('X-Drupal-Cache', 'HIT');
58 // Test ['#attached']['html_head_link'] when outputted as HTTP header.
59 $this->drupalGet('/render_attached_test/html_header_link');
60 $expected_link_headers = [
61 '</foo?bar=<baz>&baz=false>; rel="alternate"',
62 '</foo/bar>; hreflang="nl"; rel="alternate"',
64 $this->assertEqual($this->drupalGetHeader('link'), implode(',', $expected_link_headers));
68 * Test caching of ['#attached'].
70 public function testRenderCachedBlock() {
71 // Make sure our test block is visible.
72 $this->drupalPlaceBlock('attached_rendering_block', ['region' => 'content']);
74 // Get the front page, which should now have our visible block.
76 // Make sure our block is visible.
77 $this->assertText('Markup from attached_rendering_block.');
78 // Test that all our attached items are present.
81 $this->assertResponse(418);
82 $this->assertTeapotHeaders();
84 // Reload the page, to test caching.
86 // Make sure our block is visible.
87 $this->assertText('Markup from attached_rendering_block.');
88 // The header should be present again.
89 $this->assertHeader('X-Test-Teapot', 'Teapot Mode Active');
93 * Helper function to make assertions about added HTTP headers.
95 protected function assertTeapotHeaders() {
96 $this->assertHeader('X-Test-Teapot', 'Teapot Mode Active');
97 $this->assertHeader('X-Test-Teapot-Replace', 'Teapot replaced');
98 $this->assertHeader('X-Test-Teapot-No-Replace', 'This value is not replaced,This one is added');
102 * Helper function to make assertions about the presence of an RSS feed.
104 protected function assertFeed() {
105 // Discover the DOM element for the feed link.
106 $test_meta = $this->xpath('//head/link[@href="test://url"]');
107 $this->assertEqual(1, count($test_meta), 'Link has URL.');
108 // Reconcile the other attributes.
109 $test_meta_attributes = [
110 'href' => 'test://url',
111 'rel' => 'alternate',
112 'type' => 'application/rss+xml',
113 'title' => 'Your RSS feed.',
115 $test_meta = reset($test_meta);
116 if (empty($test_meta)) {
117 $this->fail('Unable to find feed link.');
120 foreach ($test_meta->attributes() as $attribute => $value) {
121 $this->assertEqual($value, $test_meta_attributes[$attribute]);
127 * Helper function to make assertions about HTML head elements.
129 protected function assertHead() {
130 // Discover the DOM element for the meta link.
131 $test_meta = $this->xpath('//head/meta[@test-attribute="testvalue"]');
132 $this->assertEqual(1, count($test_meta), 'There\'s only one test attribute.');
133 // Grab the only DOM element.
134 $test_meta = reset($test_meta);
135 if (empty($test_meta)) {
136 $this->fail('Unable to find the head meta.');
139 $test_meta_attributes = $test_meta->attributes();
140 $this->assertEqual($test_meta_attributes['test-attribute'], 'testvalue');