6060fcc30caf3b78a9060fcd641274b7ff70d7dc
[yaffs-website] / web / core / modules / system / src / Tests / Render / HtmlResponseAttachmentsTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Render;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Functional tests for HtmlResponseAttachmentsProcessor.
9  *
10  * @group Render
11  */
12 class HtmlResponseAttachmentsTest extends WebTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['render_attached_test'];
20
21   /**
22    * Test rendering of ['#attached'].
23    */
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');
33
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');
41
42     // Test ['#attached']['feed'].
43     $this->drupalGet('/render_attached_test/feed');
44     $this->assertHeader('X-Drupal-Cache', 'MISS');
45     $this->assertFeed();
46     // Repeat for the cache.
47     $this->drupalGet('/render_attached_test/feed');
48     $this->assertHeader('X-Drupal-Cache', 'HIT');
49
50     // Test ['#attached']['html_head'].
51     $this->drupalGet('/render_attached_test/head');
52     $this->assertHeader('X-Drupal-Cache', 'MISS');
53     $this->assertHead();
54     // Repeat for the cache.
55     $this->drupalGet('/render_attached_test/head');
56     $this->assertHeader('X-Drupal-Cache', 'HIT');
57
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=&lt;baz&gt;&amp;baz=false>; rel="alternate"',
62       '</foo/bar>; hreflang="nl"; rel="alternate"',
63     ];
64     $this->assertEqual($this->drupalGetHeader('link'), implode(',', $expected_link_headers));
65   }
66
67   /**
68    * Test caching of ['#attached'].
69    */
70   public function testRenderCachedBlock() {
71     // Make sure our test block is visible.
72     $this->drupalPlaceBlock('attached_rendering_block', ['region' => 'content']);
73
74     // Get the front page, which should now have our visible block.
75     $this->drupalGet('');
76     // Make sure our block is visible.
77     $this->assertText('Markup from attached_rendering_block.');
78     // Test that all our attached items are present.
79     $this->assertFeed();
80     $this->assertHead();
81     $this->assertResponse(418);
82     $this->assertTeapotHeaders();
83
84     // Reload the page, to test caching.
85     $this->drupalGet('');
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');
90   }
91
92   /**
93    * Helper function to make assertions about added HTTP headers.
94    */
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');
99   }
100
101   /**
102    * Helper function to make assertions about the presence of an RSS feed.
103    */
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.',
114     ];
115     $test_meta = reset($test_meta);
116     if (empty($test_meta)) {
117       $this->fail('Unable to find feed link.');
118     }
119     else {
120       foreach ($test_meta->attributes() as $attribute => $value) {
121         $this->assertEqual($value, $test_meta_attributes[$attribute]);
122       }
123     }
124   }
125
126   /**
127    * Helper function to make assertions about HTML head elements.
128    */
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.');
137     }
138     else {
139       $test_meta_attributes = $test_meta->attributes();
140       $this->assertEqual($test_meta_attributes['test-attribute'], 'testvalue');
141     }
142   }
143
144 }