function ComponentInFormTest::testFormRenderingAndSubmission

Tests that fields validation messages are sorted in the fields order.

File

core/tests/Drupal/KernelTests/Components/ComponentInFormTest.php, line 118

Class

ComponentInFormTest
Tests the correct rendering of components in form.

Namespace

Drupal\KernelTests\Components

Code

public function testFormRenderingAndSubmission() : void {
    
    /** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */
    $form_builder = \Drupal::service('form_builder');
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $form = $form_builder->getForm($this);
    // Test form structure after being processed.
    $this->assertTrue($form['normal']['#processed'], 'The normal textfield should have been processed.');
    $this->assertTrue($form['banner']['banner_body']['card_body']['bar']['#processed'], 'The textfield inside component should have been processed.');
    $this->assertTrue($form['banner']['banner_body']['card_body']['foo']['#processed'], 'The select inside component should have been processed.');
    $this->assertTrue($form['actions']['submit']['#processed'], 'The submit button should have been processed.');
    // Test form rendering.
    $markup = $renderer->renderRoot($form);
    $this->setRawContent($markup);
    // Ensure form elements are rendered once.
    $this->assertCount(1, $this->cssSelect('input[name="normal"]'), 'The normal textfield should have been rendered once.');
    $this->assertCount(1, $this->cssSelect('input[name="foo"]'), 'The foo textfield should have been rendered once.');
    $this->assertCount(1, $this->cssSelect('select[name="bar"]'), 'The bar select should have been rendered once.');
    // Check the position of the form elements in the DOM.
    $paths = [
        '//form/div[1]/input[@name="normal"]',
        '//form/div[2][@data-component-id="sdc_test:my-banner"]/div[2][@class="component--my-banner--body"]/div[1][@data-component-id="sdc_theme_test:my-card"]/div[1][@class="component--my-card__body"]/div[1]/input[@name="foo"]',
        '//form/div[2][@data-component-id="sdc_test:my-banner"]/div[2][@class="component--my-banner--body"]/div[1][@data-component-id="sdc_theme_test:my-card"]/div[1][@class="component--my-card__body"]/div[2]/select[@name="bar"]',
    ];
    foreach ($paths as $path) {
        $this->assertNotEmpty($this->xpath($path), 'There should be a result with the path: ' . $path . '.');
    }
    // Test form submission. Assertions are in submitForm().
    $form_state = new FormState();
    $form_builder->submitForm($this, $form_state);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.