2acaefb873b62b80272d495704b5297c99234b36
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Render / RenderTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Render;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Performs functional tests on drupal_render().
9  *
10  * @group Common
11  */
12 class RenderTest extends KernelTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['system', 'common_test', 'theme_test'];
20
21   /**
22    * Tests theme preprocess functions being able to attach assets.
23    */
24   public function testDrupalRenderThemePreprocessAttached() {
25     \Drupal::state()->set('theme_preprocess_attached_test', TRUE);
26
27     $test_element = [
28       '#theme' => 'common_test_render_element',
29       'foo' => [
30         '#markup' => 'Kittens!',
31       ],
32     ];
33     \Drupal::service('renderer')->renderRoot($test_element);
34
35     $expected_attached = [
36       'library' => [
37         'test/generic_preprocess',
38         'test/specific_preprocess',
39       ]
40     ];
41     $this->assertEqual($expected_attached, $test_element['#attached'], 'All expected assets from theme preprocess hooks attached.');
42
43     \Drupal::state()->set('theme_preprocess_attached_test', FALSE);
44   }
45
46   /**
47    * Ensures that render array children are processed correctly.
48    */
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.
52     $build = [
53       '#type' => 'container',
54       '#theme' => 'theme_test_render_element_children',
55       '#prefix' => 'kangaroo',
56       '#suffix' => 'kitten',
57     ];
58     $this->render($build);
59     $this->removeWhiteSpace();
60     $this->assertNoRaw('<div>kangarookitten</div>');
61   }
62
63   /**
64    * Tests that we get an exception when we try to attach an illegal type.
65    */
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');
71     try {
72       $renderer->renderBarePage($build, '', 'maintenance_page');
73       $this->fail("Invalid #attachment 'drupal_process_states' allowed");
74     }
75     catch (\LogicException $e) {
76       $this->pass("Invalid #attachment 'drupal_process_states' not allowed");
77     }
78   }
79
80 }