3 namespace Drupal\system\Tests\Common;
6 use Drupal\simpletest\WebTestBase;
9 * Verifies that bubbleable metadata of early rendering is not lost.
13 class EarlyRenderingControllerTest extends WebTestBase {
18 protected $dumpHeaders = TRUE;
23 public static $modules = ['system', 'early_rendering_controller_test'];
26 * Tests theme preprocess functions being able to attach assets.
28 public function testEarlyRendering() {
29 // Render array: non-early & early.
30 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.render_array'));
31 $this->assertResponse(200);
32 $this->assertRaw('Hello world!');
33 $this->assertCacheTag('foo');
34 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.render_array.early'));
35 $this->assertResponse(200);
36 $this->assertRaw('Hello world!');
37 $this->assertCacheTag('foo');
39 // AjaxResponse: non-early & early.
40 // @todo Add cache tags assertion when AjaxResponse is made cacheable in
41 // https://www.drupal.org/node/956186.
42 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.ajax_response'));
43 $this->assertResponse(200);
44 $this->assertRaw('Hello world!');
45 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.ajax_response.early'));
46 $this->assertResponse(200);
47 $this->assertRaw('Hello world!');
49 // Basic Response object: non-early & early.
50 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.response'));
51 $this->assertResponse(200);
52 $this->assertRaw('Hello world!');
53 $this->assertNoCacheTag('foo');
54 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.response.early'));
55 $this->assertResponse(200);
56 $this->assertRaw('Hello world!');
57 $this->assertNoCacheTag('foo');
59 // Response object with attachments: non-early & early.
60 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.response-with-attachments'));
61 $this->assertResponse(200);
62 $this->assertRaw('Hello world!');
63 $this->assertNoCacheTag('foo');
64 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.response-with-attachments.early'));
65 $this->assertResponse(500);
66 $this->assertRaw('The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\early_rendering_controller_test\AttachmentsTestResponse.');
68 // Cacheable Response object: non-early & early.
69 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.cacheable-response'));
70 $this->assertResponse(200);
71 $this->assertRaw('Hello world!');
72 $this->assertNoCacheTag('foo');
73 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.cacheable-response.early'));
74 $this->assertResponse(500);
75 $this->assertRaw('The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\early_rendering_controller_test\CacheableTestResponse.');
77 // Basic domain object: non-early & early.
78 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.domain-object'));
79 $this->assertResponse(200);
80 $this->assertRaw('TestDomainObject');
81 $this->assertNoCacheTag('foo');
82 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.domain-object.early'));
83 $this->assertResponse(200);
84 $this->assertRaw('TestDomainObject');
85 $this->assertNoCacheTag('foo');
87 // Basic domain object with attachments: non-early & early.
88 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.domain-object-with-attachments'));
89 $this->assertResponse(200);
90 $this->assertRaw('AttachmentsTestDomainObject');
91 $this->assertNoCacheTag('foo');
92 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.domain-object-with-attachments.early'));
93 $this->assertResponse(500);
94 $this->assertRaw('The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\early_rendering_controller_test\AttachmentsTestDomainObject.');
96 // Cacheable Response object: non-early & early.
97 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.cacheable-domain-object'));
98 $this->assertResponse(200);
99 $this->assertRaw('CacheableTestDomainObject');
100 $this->assertNoCacheTag('foo');
101 $this->drupalGet(Url::fromRoute('early_rendering_controller_test.cacheable-domain-object.early'));
102 $this->assertResponse(500);
103 $this->assertRaw('The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\early_rendering_controller_test\CacheableTestDomainObject.');
105 // The exceptions are expected. Do not interpret them as a test failure.
106 // Not using File API; a potential error must trigger a PHP warning.
107 unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');