You are here

protected function ProgrammaticTest::doSubmitForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php \Drupal\Tests\system\Kernel\Form\ProgrammaticTest::doSubmitForm()

Helper function used to programmatically submit the form defined in form_test.module 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 69

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 ? t('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),
      ]));
    }
  }
}