function ProgrammaticTest::doSubmitForm

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php \Drupal\Tests\system\Kernel\Form\ProgrammaticTest::doSubmitForm()
  2. 10 core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php \Drupal\Tests\system\Kernel\Form\ProgrammaticTest::doSubmitForm()
  3. 11.x core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php \Drupal\Tests\system\Kernel\Form\ProgrammaticTest::doSubmitForm()

Programmatically submits the form_test.module form with the given values.

Parameters

$values: An array of field values to be submitted.

$valid_input: A boolean indicating whether or not the form submission is expected to be valid.

1 call to ProgrammaticTest::doSubmitForm()
ProgrammaticTest::testSubmissionWorkflow in core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php
Tests the programmatic form submission workflow.

File

core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php, line 68

Class

ProgrammaticTest
Tests the programmatic form submission behavior.

Namespace

Drupal\Tests\system\Kernel\Form

Code

protected function doSubmitForm($values, $valid_input) {
    // Programmatically submit the given values.
    $form_state = (new FormState())->setValues($values);
    \Drupal::formBuilder()->submitForm('\\Drupal\\form_test\\Form\\FormTestProgrammaticForm', $form_state);
    // Check that the form returns an error when expected, and vice versa.
    $errors = $form_state->getErrors();
    $valid_form = empty($errors);
    $args = [
        '%values' => print_r($values, TRUE),
        '%errors' => $valid_form ? 'None' : implode(' ', $errors),
    ];
    $this->assertSame($valid_form, $valid_input, new FormattableMarkup('Input values: %values<br />Validation handler errors: %errors', $args));
    // We check submitted values only if we have a valid input.
    if ($valid_input) {
        // Fetching the values that were set in the submission handler.
        $stored_values = $form_state->get('programmatic_form_submit');
        foreach ($values as $key => $value) {
            $this->assertEquals($value, $stored_values[$key], new FormattableMarkup('Submission handler correctly executed: %stored_key is %stored_value', [
                '%stored_key' => $key,
                '%stored_value' => print_r($value, TRUE),
            ]));
        }
    }
}

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