You are here

public function LayoutTest::testRenderLayout in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_discovery/tests/src/Kernel/LayoutTest.php \Drupal\Tests\layout_discovery\Kernel\LayoutTest::testRenderLayout()
  2. 10 core/modules/layout_discovery/tests/src/Kernel/LayoutTest.php \Drupal\Tests\layout_discovery\Kernel\LayoutTest::testRenderLayout()

Tests rendering a layout.

@dataProvider renderLayoutData

File

core/modules/layout_discovery/tests/src/Kernel/LayoutTest.php, line 52

Class

LayoutTest
Tests Layout functionality.

Namespace

Drupal\Tests\layout_discovery\Kernel

Code

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, '<form data-drupal-selector="the-form-id" action="/" method="post" id="the-form-id" accept-charset="UTF-8">');

  // 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 = '<input data-drupal-selector="edit-the-form-id" type="hidden" name="form_id" value="the_form_id"/>';
  $html[] = 'Test suffix';
  $html[] = $build_id_input . $form_id_input . '</form>';

  // Match the HTML to the full form element.
  $this
    ->assertSame(implode("\n", $html), $this
    ->cssSelect('#the-form-id')[0]
    ->asXML());
}