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