function ComponentAsFormElementTest::testFormRenderingAndSubmission
Tests that fields validation messages are sorted in the fields order.
File
-
core/
tests/ Drupal/ KernelTests/ Components/ ComponentAsFormElementTest.php, line 197
Class
- ComponentAsFormElementTest
- Tests the correct rendering of components in form.
Namespace
Drupal\KernelTests\ComponentsCode
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 rendering.
$markup = $renderer->renderRoot($form);
$this->setRawContent($markup);
// Ensure form elements are rendered once.
$this->assertCount(1, $this->cssSelect('input[name="sdc_input"]'), 'The sdc_input textfield should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name="sdc_input_basic"]'), 'The sdc_input_basic textfield should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name="sdc_input_with_label"]'), 'The sdc_input_with_label textfield should have been rendered once.');
$this->assertCount(1, $this->cssSelect('span[id="test_data_label_container"]'), 'The span with id "test_data_label_container" should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name="sdc_input_with_default_value"]'), 'The sdc_input_with_default_value textfield should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name="sdc_input_with_value"]'), 'The sdc_input_with_value textfield should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name="sdc_input_with_value_and_default_value"]'), 'The sdc_input_with_value_and_default_value textfield should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name="sdc_input_with_required"]'), 'The sdc_input_with_required textfield should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name="sdc_input_with_id_as_prop"]'), 'The sdc_input_with_id_as_prop textfield should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[id=test_sdc_input_prop_id]'), 'A textfield with id "test_sdc_input_prop_id" should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name=sdc_input_with_id_as_prop]'), 'A sdc_input with id "sdc_input_with_id_as_prop" should have been rendered once.');
$this->assertCount(1, $this->cssSelect('input[name=sdc_input_with_id_as_prop_attributes]'), 'A sdc_input with id "sdc_input_with_id_as_prop_attributes" should have been rendered once.');
$this->assertCount(1, $this->cssSelect('div[id=test_sdc_input_prop_attributes_id]'), 'A div wrapper with id "test_sdc_input_prop_attributes_id" should have been rendered once.');
// Check the position of the form elements in the DOM.
$paths = [
'//form/div[1]/input[@name="sdc_input"]',
'//form/div[2]/input[@name="sdc_input_basic"]',
'//form/div[3]/input[@name="sdc_input_with_label"]',
'//form/div[4]/input[@name="sdc_input_with_default_value"]',
'//form/div[5]/input[@name="sdc_input_with_value"]',
'//form/div[6]/input[@name="sdc_input_with_value_and_default_value"]',
'//form/div[7]/input[@name="sdc_input_with_required"]',
'//form/div[8]/input[@name="sdc_input_with_id_as_prop"]',
'//form/div[9]/input[@name="sdc_input_with_id_as_prop_attributes"]',
];
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.