You are here

protected function BrowserTestBase::submitForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/BrowserTestBase.php \Drupal\simpletest\BrowserTestBase::submitForm()

Fills and submits a form.

Parameters

array $edit: Field data in an associative array. Changes the current input fields (where possible) to the values indicated.

A checkbox can be set to TRUE to be checked and should be set to FALSE to be unchecked.

string $submit: Value of the submit button whose click is to be emulated. For example, t('Save'). The processing of the request depends on this value. For example, a form may have one button with the value t('Save') and another button with the value t('Delete'), and execute different code depending on which one is clicked.

string $form_html_id: (optional) HTML ID of the form to be submitted. On some pages there are many identical forms, so just using the value of the submit button is not enough. For example: 'trigger-node-presave-assign-form'. Note that this is not the Drupal $form_id, but rather the HTML ID of the form, which is typically the same thing but with hyphens replacing the underscores.

2 calls to BrowserTestBase::submitForm()
BrowserTestBase::drupalLogin in core/modules/simpletest/src/BrowserTestBase.php
Logs in a user using the Mink controlled browser.
BrowserTestBaseTest::testForm in core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php
Tests basic form functionality.

File

core/modules/simpletest/src/BrowserTestBase.php, line 751
Contains \Drupal\simpletest\BrowserTestBase.

Class

BrowserTestBase
Provides a test case for functional Drupal tests.

Namespace

Drupal\simpletest

Code

protected function submitForm(array $edit, $submit, $form_html_id = NULL) {
  $assert_session = $this
    ->assertSession();

  // Get the form.
  if (isset($form_html_id)) {
    $form = $assert_session
      ->elementExists('xpath', "//form[@id='{$form_html_id}']");
    $submit_button = $assert_session
      ->buttonExists($submit, $form);
  }
  else {
    $submit_button = $assert_session
      ->buttonExists($submit);
    $form = $assert_session
      ->elementExists('xpath', './ancestor::form', $submit_button);
  }

  // Edit the form values.
  foreach ($edit as $name => $value) {
    $field = $assert_session
      ->fieldExists($name, $form);
    $field
      ->setValue($value);
  }

  // Submit form.
  $this
    ->prepareRequest();
  $submit_button
    ->press();

  // Ensure that any changes to variables in the other thread are picked up.
  $this
    ->refreshVariables();
}