3 namespace Drupal\Tests\layout_discovery\Kernel;
5 use Drupal\Core\Form\FormState;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests Layout functionality.
13 class LayoutTest extends KernelTestBase {
18 public static $modules = ['system', 'layout_discovery', 'layout_test'];
21 * The layout plugin manager.
23 * @var \Drupal\Core\Layout\LayoutPluginManagerInterface
25 protected $layoutPluginManager;
30 protected function setUp() {
33 $this->layoutPluginManager = $this->container->get('plugin.manager.core.layout');
37 * Test rendering a layout.
39 * @dataProvider renderLayoutData
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";
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);
54 $this->render($built);
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>';
65 // Match the HTML to the full form element.
66 $this->assertSame(implode("\n", $html), $this->cssSelect('#the-form-id')[0]->asXML());
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;
80 * Data provider for testRenderLayout().
82 public function renderLayoutData() {
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';
89 $data['layout_onecol'] = [
94 '#markup' => 'This is the content',
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.';
105 $html[] = '<div data-drupal-selector="edit-bottom" class="region-bottom">';
106 $html[] = 'This is the bottom';
109 $data['layout_test_1col_with_form'] = [
114 '#process' => [[static::class, 'processCallback']],
117 '#markup' => 'This is the bottom',
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';
128 $html[] = '<div data-drupal-selector="edit-bottom" class="region-bottom">';
129 $html[] = 'This is the bottom';
132 $data['layout_test_1col'] = [
137 '#markup' => 'This is the top',
140 '#markup' => 'This is the bottom',
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';
151 $html[] = '<div data-drupal-selector="edit-bottom" class="layout__region layout__region--bottom">';
152 $html[] = 'This is the bottom';
155 $data['layout_test_1col_no_template'] = [
156 'layout_test_1col_no_template',
160 '#markup' => 'This is the top',
163 '#markup' => 'This is the bottom',
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';
174 $html[] = '<div data-drupal-selector="edit-right" class="region-right">';
175 $html[] = 'This is the right';
178 $data['layout_test_2col'] = [
183 '#markup' => 'This is the left',
186 '#markup' => 'This is the right',
193 $html[] = '<div data-drupal-selector="edit-layout" class="layout-test-plugin clearfix">';
195 $html[] = '<span class="setting-1-label">Blah: </span>';
196 $html[] = 'Config value';
198 $html[] = '<div data-drupal-selector="edit-main" class="region-main">';
199 $html[] = 'Main region';
202 $data['layout_test_plugin'] = [
203 'layout_test_plugin',
205 'setting_1' => 'Config value',
209 '#markup' => 'Main region',
219 * Provides a test #process callback.
221 public static function processCallback($element) {
222 $element['#markup'] = 'This string added by #process.';