Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / layout_discovery / tests / src / Kernel / LayoutTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_discovery\Kernel;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests Layout functionality.
10  *
11  * @group Layout
12  */
13 class LayoutTest extends KernelTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['system', 'layout_discovery', 'layout_test'];
19
20   /**
21    * The layout plugin manager.
22    *
23    * @var \Drupal\Core\Layout\LayoutPluginManagerInterface
24    */
25   protected $layoutPluginManager;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     $this->layoutPluginManager = $this->container->get('plugin.manager.core.layout');
34   }
35
36   /**
37    * Test rendering a layout.
38    *
39    * @dataProvider renderLayoutData
40    */
41   public function testRenderLayout($layout_id, $config, $regions, array $html) {
42     $layout = $this->layoutPluginManager->createInstance($layout_id, $config);
43     $built['layout'] = $layout->build($regions);
44     $built['layout']['#prefix'] = 'Test prefix' . "\n";
45     $built['layout']['#suffix'] = 'Test suffix' . "\n";
46
47     // Assume each layout is contained by a form, in order to ensure the
48     // building of the layout does not interfere with form processing.
49     $form_state = new FormState();
50     $form_builder = $this->container->get('form_builder');
51     $form_builder->prepareForm('the_form_id', $built, $form_state);
52     $form_builder->processForm('the_form_id', $built, $form_state);
53
54     $this->render($built);
55
56     // Add in the wrapping form elements and prefix/suffix.
57     array_unshift($html, 'Test prefix');
58     array_unshift($html, '<form data-drupal-selector="the-form-id" action="/" method="post" id="the-form-id" accept-charset="UTF-8">');
59     // Retrieve the build ID from the rendered HTML since the string is random.
60     $build_id_input = $this->cssSelect('input[name="form_build_id"]')[0]->asXML();
61     $form_id_input = '<input data-drupal-selector="edit-the-form-id" type="hidden" name="form_id" value="the_form_id"/>';
62     $html[] = 'Test suffix';
63     $html[] = $build_id_input . $form_id_input . '</form>';
64
65     // Match the HTML to the full form element.
66     $this->assertSame(implode("\n", $html), $this->cssSelect('#the-form-id')[0]->asXML());
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   protected function render(array &$elements) {
73     $content = parent::render($elements);
74     // Strip leading whitespace from every line.
75     $this->content = preg_replace('/^\s+/m', '', $content);
76     return $this->content;
77   }
78
79   /**
80    * Data provider for testRenderLayout().
81    */
82   public function renderLayoutData() {
83     $html = [];
84     $html[] = '<div data-drupal-selector="edit-layout" class="layout layout--onecol">';
85     $html[] = '<div data-drupal-selector="edit-content" class="layout__region layout__region--content">';
86     $html[] = 'This is the content';
87     $html[] = '</div>';
88     $html[] = '</div>';
89     $data['layout_onecol'] = [
90       'layout_onecol',
91       [],
92       [
93         'content' => [
94           '#markup' => 'This is the content',
95         ],
96       ],
97       $html,
98     ];
99
100     $html = [];
101     $html[] = '<div data-drupal-selector="edit-layout" class="layout-example-1col clearfix">';
102     $html[] = '<div data-drupal-selector="edit-top" class="region-top">';
103     $html[] = 'This string added by #process.';
104     $html[] = '</div>';
105     $html[] = '<div data-drupal-selector="edit-bottom" class="region-bottom">';
106     $html[] = 'This is the bottom';
107     $html[] = '</div>';
108     $html[] = '</div>';
109     $data['layout_test_1col_with_form'] = [
110       'layout_test_1col',
111       [],
112       [
113         'top' => [
114           '#process' => [[static::class, 'processCallback']],
115         ],
116         'bottom' => [
117           '#markup' => 'This is the bottom',
118         ],
119       ],
120       $html,
121     ];
122
123     $html = [];
124     $html[] = '<div data-drupal-selector="edit-layout" class="layout-example-1col clearfix">';
125     $html[] = '<div data-drupal-selector="edit-top" class="region-top">';
126     $html[] = 'This is the top';
127     $html[] = '</div>';
128     $html[] = '<div data-drupal-selector="edit-bottom" class="region-bottom">';
129     $html[] = 'This is the bottom';
130     $html[] = '</div>';
131     $html[] = '</div>';
132     $data['layout_test_1col'] = [
133       'layout_test_1col',
134       [],
135       [
136         'top' => [
137           '#markup' => 'This is the top',
138         ],
139         'bottom' => [
140           '#markup' => 'This is the bottom',
141         ],
142       ],
143       $html,
144     ];
145
146     $html = [];
147     $html[] = '<div data-drupal-selector="edit-layout" class="layout layout--layout-test-1col-no-template">';
148     $html[] = '<div data-drupal-selector="edit-top" class="layout__region layout__region--top">';
149     $html[] = 'This is the top';
150     $html[] = '</div>';
151     $html[] = '<div data-drupal-selector="edit-bottom" class="layout__region layout__region--bottom">';
152     $html[] = 'This is the bottom';
153     $html[] = '</div>';
154     $html[] = '</div>';
155     $data['layout_test_1col_no_template'] = [
156       'layout_test_1col_no_template',
157       [],
158       [
159         'top' => [
160           '#markup' => 'This is the top',
161         ],
162         'bottom' => [
163           '#markup' => 'This is the bottom',
164         ],
165       ],
166       $html,
167     ];
168
169     $html = [];
170     $html[] = '<div data-drupal-selector="edit-layout" class="layout-example-2col clearfix">';
171     $html[] = '<div data-drupal-selector="edit-left" class="class-added-by-preprocess region-left">';
172     $html[] = 'This is the left';
173     $html[] = '</div>';
174     $html[] = '<div data-drupal-selector="edit-right" class="region-right">';
175     $html[] = 'This is the right';
176     $html[] = '</div>';
177     $html[] = '</div>';
178     $data['layout_test_2col'] = [
179       'layout_test_2col',
180       [],
181       [
182         'left' => [
183           '#markup' => 'This is the left',
184         ],
185         'right' => [
186           '#markup' => 'This is the right',
187         ],
188       ],
189       $html,
190     ];
191
192     $html = [];
193     $html[] = '<div data-drupal-selector="edit-layout" class="layout-test-plugin clearfix">';
194     $html[] = '<div>';
195     $html[] = '<span class="setting-1-label">Blah: </span>';
196     $html[] = 'Config value';
197     $html[] = '</div>';
198     $html[] = '<div data-drupal-selector="edit-main" class="region-main">';
199     $html[] = 'Main region';
200     $html[] = '</div>';
201     $html[] = '</div>';
202     $data['layout_test_plugin'] = [
203       'layout_test_plugin',
204       [
205         'setting_1' => 'Config value',
206       ],
207       [
208         'main' => [
209           '#markup' => 'Main region',
210         ],
211       ],
212       $html,
213     ];
214
215     return $data;
216   }
217
218   /**
219    * Provides a test #process callback.
220    */
221   public static function processCallback($element) {
222     $element['#markup'] = 'This string added by #process.';
223     return $element;
224   }
225
226 }