PageRenderTest.php in Zircon Profile 8.0
File
core/modules/system/src/Tests/Common/PageRenderTest.php
View source
<?php
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\KernelTestBase;
class PageRenderTest extends KernelTestBase {
function testHookPageAttachmentsExceptions() {
$this
->enableModules([
'common_test',
'system',
]);
$this
->installSchema('system', 'router');
\Drupal::service('router.builder')
->rebuild();
$this
->assertPageRenderHookExceptions('common_test', 'hook_page_attachments');
}
function testHookPageAlter() {
$this
->enableModules([
'common_test',
'system',
]);
$this
->installSchema('system', 'router');
\Drupal::service('router.builder')
->rebuild();
$this
->assertPageRenderHookExceptions('common_test', 'hook_page_attachments_alter');
}
function assertPageRenderHookExceptions($module, $hook) {
$html_renderer = \Drupal::getContainer()
->get('main_content_renderer.html');
$page = [];
$html_renderer
->invokePageAttachmentHooks($page);
$this
->assertEqual($page['#cache']['tags'], [
'example',
]);
$this
->assertEqual($page['#cache']['contexts'], [
'user.permissions',
]);
\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
->error($assertion);
} catch (\LogicException $e) {
$this
->pass($assertion);
$this
->assertEqual($e
->getMessage(), 'Only #attached and #cache may be set in ' . $hook . '().');
}
\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
->error($assertion);
} catch (\LogicException $e) {
$this
->pass($assertion);
$this
->assertEqual($e
->getMessage(), 'Only #attached and #cache may be set in ' . $hook . '().');
}
\Drupal::state()
->set($module . '.' . $hook . '.render_array', FALSE);
}
}