You are here

private function ProgrammaticTest::submitForm in Zircon Profile 8.0

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

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::submitForm()
ProgrammaticTest::testSubmissionWorkflow in core/modules/system/src/Tests/Form/ProgrammaticTest.php
Test the programmatic form submission workflow.

File

core/modules/system/src/Tests/Form/ProgrammaticTest.php, line 73
Contains \Drupal\system\Tests\Form\ProgrammaticTest.

Class

ProgrammaticTest
Tests the programmatic form submission behavior.

Namespace

Drupal\system\Tests\Form

Code

private function submitForm($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 = array(
    '%values' => print_r($values, TRUE),
    '%errors' => $valid_form ? t('None') : implode(' ', $errors),
  );
  $this
    ->assertTrue($valid_input == $valid_form, format_string('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
        ->assertEqual($stored_values[$key], $value, format_string('Submission handler correctly executed: %stored_key is %stored_value', array(
        '%stored_key' => $key,
        '%stored_value' => print_r($value, TRUE),
      )));
    }
  }
}