3 namespace Drupal\KernelTests\Core\Render;
5 use Drupal\KernelTests\KernelTestBase;
8 * Performs functional tests on drupal_render().
12 class RenderTest extends KernelTestBase {
19 public static $modules = ['system', 'common_test', 'theme_test'];
22 * Tests theme preprocess functions being able to attach assets.
24 public function testDrupalRenderThemePreprocessAttached() {
25 \Drupal::state()->set('theme_preprocess_attached_test', TRUE);
28 '#theme' => 'common_test_render_element',
30 '#markup' => 'Kittens!',
33 \Drupal::service('renderer')->renderRoot($test_element);
35 $expected_attached = [
37 'test/generic_preprocess',
38 'test/specific_preprocess',
41 $this->assertEqual($expected_attached, $test_element['#attached'], 'All expected assets from theme preprocess hooks attached.');
43 \Drupal::state()->set('theme_preprocess_attached_test', FALSE);
47 * Ensures that render array children are processed correctly.
49 public function testRenderChildren() {
50 // Ensure that #prefix and #suffix is only being printed once since that is
51 // the behaviour the caller code expects.
53 '#type' => 'container',
54 '#theme' => 'theme_test_render_element_children',
55 '#prefix' => 'kangaroo',
56 '#suffix' => 'kitten',
58 $this->render($build);
59 $this->removeWhiteSpace();
60 $this->assertNoRaw('<div>kangarookitten</div>');
64 * Tests that we get an exception when we try to attach an illegal type.
66 public function testProcessAttached() {
67 // Specify invalid attachments in a render array.
68 $build['#attached']['library'][] = 'core/drupal.states';
69 $build['#attached']['drupal_process_states'][] = [];
70 $renderer = $this->container->get('bare_html_page_renderer');
72 $renderer->renderBarePage($build, '', 'maintenance_page');
73 $this->fail("Invalid #attachment 'drupal_process_states' allowed");
75 catch (\LogicException $e) {
76 $this->pass("Invalid #attachment 'drupal_process_states' not allowed");