layoutPluginManager = $this->container->get('plugin.manager.core.layout'); } /** * Test rendering a layout. * * @dataProvider renderLayoutData */ public function testRenderLayout($layout_id, $config, $regions, array $html) { $layout = $this->layoutPluginManager->createInstance($layout_id, $config); $built['layout'] = $layout->build($regions); $built['layout']['#prefix'] = 'Test prefix' . "\n"; $built['layout']['#suffix'] = 'Test suffix' . "\n"; // Assume each layout is contained by a form, in order to ensure the // building of the layout does not interfere with form processing. $form_state = new FormState(); $form_builder = $this->container->get('form_builder'); $form_builder->prepareForm('the_form_id', $built, $form_state); $form_builder->processForm('the_form_id', $built, $form_state); $this->render($built); // Add in the wrapping form elements and prefix/suffix. array_unshift($html, 'Test prefix'); array_unshift($html, '
'); // Retrieve the build ID from the rendered HTML since the string is random. $build_id_input = $this->cssSelect('input[name="form_build_id"]')[0]->asXML(); $form_id_input = ''; $html[] = 'Test suffix'; $html[] = $build_id_input . $form_id_input . '
'; // Match the HTML to the full form element. $this->assertSame(implode("\n", $html), $this->cssSelect('#the-form-id')[0]->asXML()); } /** * {@inheritdoc} */ protected function render(array &$elements) { $content = parent::render($elements); // Strip leading whitespace from every line. $this->content = preg_replace('/^\s+/m', '', $content); return $this->content; } /** * Data provider for testRenderLayout(). */ public function renderLayoutData() { $html = []; $html[] = '
'; $html[] = '
'; $html[] = 'This is the content'; $html[] = '
'; $html[] = '
'; $data['layout_onecol'] = [ 'layout_onecol', [], [ 'content' => [ '#markup' => 'This is the content', ], ], $html, ]; $html = []; $html[] = '
'; $html[] = '
'; $html[] = 'This string added by #process.'; $html[] = '
'; $html[] = '
'; $html[] = 'This is the bottom'; $html[] = '
'; $html[] = '
'; $data['layout_test_1col_with_form'] = [ 'layout_test_1col', [], [ 'top' => [ '#process' => [[static::class, 'processCallback']], ], 'bottom' => [ '#markup' => 'This is the bottom', ], ], $html, ]; $html = []; $html[] = '
'; $html[] = '
'; $html[] = 'This is the top'; $html[] = '
'; $html[] = '
'; $html[] = 'This is the bottom'; $html[] = '
'; $html[] = '
'; $data['layout_test_1col'] = [ 'layout_test_1col', [], [ 'top' => [ '#markup' => 'This is the top', ], 'bottom' => [ '#markup' => 'This is the bottom', ], ], $html, ]; $html = []; $html[] = '
'; $html[] = '
'; $html[] = 'This is the top'; $html[] = '
'; $html[] = '
'; $html[] = 'This is the bottom'; $html[] = '
'; $html[] = '
'; $data['layout_test_1col_no_template'] = [ 'layout_test_1col_no_template', [], [ 'top' => [ '#markup' => 'This is the top', ], 'bottom' => [ '#markup' => 'This is the bottom', ], ], $html, ]; $html = []; $html[] = '
'; $html[] = '
'; $html[] = 'This is the left'; $html[] = '
'; $html[] = '
'; $html[] = 'This is the right'; $html[] = '
'; $html[] = '
'; $data['layout_test_2col'] = [ 'layout_test_2col', [], [ 'left' => [ '#markup' => 'This is the left', ], 'right' => [ '#markup' => 'This is the right', ], ], $html, ]; $html = []; $html[] = '
'; $html[] = '
'; $html[] = 'Blah: '; $html[] = 'Config value'; $html[] = '
'; $html[] = '
'; $html[] = 'Main region'; $html[] = '
'; $html[] = '
'; $data['layout_test_plugin'] = [ 'layout_test_plugin', [ 'setting_1' => 'Config value', ], [ 'main' => [ '#markup' => 'Main region', ], ], $html, ]; return $data; } /** * Provides a test #process callback. */ public static function processCallback($element) { $element['#markup'] = 'This string added by #process.'; return $element; } }