Same filename and directory in other branches
- 8.9.x core/modules/system/tests/src/Kernel/Common/PageRenderTest.php
- 9 core/modules/system/tests/src/Kernel/Common/PageRenderTest.php
Namespace
Drupal\Tests\system\Kernel\Common
File
core/modules/system/tests/src/Kernel/Common/PageRenderTest.php
View source
<?php
namespace Drupal\Tests\system\Kernel\Common;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class PageRenderTest extends KernelTestBase {
public function testHookPageAttachmentsExceptions() {
$this
->enableModules([
'common_test',
'system',
]);
$this
->assertPageRenderHookExceptions('common_test', 'hook_page_attachments');
}
public function testHookPageAlter() {
$this
->enableModules([
'common_test',
'system',
]);
$this
->assertPageRenderHookExceptions('common_test', 'hook_page_attachments_alter');
}
public function assertPageRenderHookExceptions(string $module, string $hook) : void {
$html_renderer = \Drupal::getContainer()
->get('main_content_renderer.html');
$page = [];
$html_renderer
->invokePageAttachmentHooks($page);
$this
->assertEquals([
'example',
], $page['#cache']['tags']);
$this
->assertEquals([
'user.permissions',
], $page['#cache']['contexts']);
\Drupal::state()
->set($module . '.' . $hook . '.descendant_attached', TRUE);
$assertion = $hook . '() implementation that sets #attached on a descendant triggers an exception';
$page = [];
try {
$html_renderer
->invokePageAttachmentHooks($page);
$this
->fail($assertion);
} catch (\LogicException $e) {
$this
->assertEquals('Only #attached and #cache may be set in ' . $hook . '().', $e
->getMessage());
}
\Drupal::state()
->set('bc_test.' . $hook . '.descendant_attached', FALSE);
\Drupal::state()
->set('bc_test.' . $hook . '.render_array', TRUE);
$assertion = $hook . '() implementation that sets a child render array triggers an exception';
$page = [];
try {
$html_renderer
->invokePageAttachmentHooks($page);
$this
->fail($assertion);
} catch (\LogicException $e) {
$this
->assertEquals('Only #attached and #cache may be set in ' . $hook . '().', $e
->getMessage());
}
\Drupal::state()
->set($module . '.' . $hook . '.render_array', FALSE);
}
public function testHtmlRendererAttachmentsRenderContext() : void {
$this
->enableModules([
'common_test',
'system',
]);
\Drupal::state()
->set('common_test.hook_page_attachments.render_url', TRUE);
$uri = '/common/attachments-test';
$request = new Request([], [], [], [], [], [
'REQUEST_URI' => $uri,
'SCRIPT_NAME' => $uri,
]);
$response = \Drupal::service('http_kernel')
->handle($request);
$this
->assertEquals(Response::HTTP_OK, $response
->getStatusCode());
}
}
Classes