function PageRenderTest::assertPageRenderHookExceptions
Same name in other branches
- 9 core/modules/system/tests/src/Kernel/Common/PageRenderTest.php \Drupal\Tests\system\Kernel\Common\PageRenderTest::assertPageRenderHookExceptions()
- 8.9.x core/modules/system/tests/src/Kernel/Common/PageRenderTest.php \Drupal\Tests\system\Kernel\Common\PageRenderTest::assertPageRenderHookExceptions()
- 11.x core/modules/system/tests/src/Kernel/Common/PageRenderTest.php \Drupal\Tests\system\Kernel\Common\PageRenderTest::assertPageRenderHookExceptions()
Asserts whether expected exceptions are thrown for invalid hook implementations.
@internal
Parameters
string $module: The module whose invalid logic in its hooks to enable.
string $hook: The page render hook to assert expected exceptions for.
2 calls to PageRenderTest::assertPageRenderHookExceptions()
- PageRenderTest::testHookPageAlter in core/
modules/ system/ tests/ src/ Kernel/ Common/ PageRenderTest.php - Tests hook_page_attachments_alter() exceptions.
- PageRenderTest::testHookPageAttachmentsExceptions in core/
modules/ system/ tests/ src/ Kernel/ Common/ PageRenderTest.php - Tests hook_page_attachments() exceptions.
File
-
core/
modules/ system/ tests/ src/ Kernel/ Common/ PageRenderTest.php, line 46
Class
- PageRenderTest
- Test page rendering hooks.
Namespace
Drupal\Tests\system\Kernel\CommonCode
public function assertPageRenderHookExceptions(string $module, string $hook) : void {
$html_renderer = \Drupal::getContainer()->get('main_content_renderer.html');
// Assert a valid hook implementation doesn't trigger an exception.
$page = [];
$html_renderer->invokePageAttachmentHooks($page);
// Assert that hooks can set cache tags.
$this->assertEquals([
'example',
], $page['#cache']['tags']);
$this->assertEquals([
'user.permissions',
], $page['#cache']['contexts']);
// Assert an invalid hook implementation doesn't trigger an exception.
\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);
// Assert an invalid hook implementation doesn't trigger an exception.
\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);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.