You are here

protected function UiHelperTrait::drupalPostForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/UiHelperTrait.php \Drupal\Tests\UiHelperTrait::drupalPostForm()

Executes a form submission.

It will be done as usual submit form with Mink.

Parameters

\Drupal\Core\Url|string $path: Location of the post form. Either a Drupal path or an absolute path or NULL to post to the current page. For multi-stage forms you can set the path to NULL and have it post to the last received page. Example:


  // First step in form.
  $edit = array(...);
  $this->drupalGet('some_url');
  $this->submitForm($edit, 'Save');

  // Second step in form.
  $edit = array(...);
  $this->submitForm($edit, 'Save');
  

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

When working with form tests, the keys for an $edit element should match the 'name' parameter of the HTML of the form. For example, the 'body' field for a node has the following HTML:


  <textarea id="edit-body-und-0-value" class="text-full form-textarea
   resize-vertical" placeholder="" cols="60" rows="9"
   name="body[0][value]"></textarea>
  

When testing this field using an $edit parameter, the code becomes:

$edit["body[0][value]"] = 'My test value';

A checkbox can be set to TRUE to be checked and should be set to FALSE to be unchecked. Multiple select fields can be tested using 'name[]' and setting each of the desired values in an array:

$edit = array();
$edit['name[]'] = array(
  'value1',
  'value2',
);

string $submit: The id, name, label or value of the submit button which is to be clicked. For example, 'Save'. The first element matched by \Drupal\Tests\WebAssert::buttonExists() will be used. The processing of the request depends on this value. For example, a form may have one button with the value 'Save' and another button with the value 'Delete', and execute different code depending on which one is clicked.

array $options: Options to be forwarded to the url generator.

string|null $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.

Return value

string (deprecated) The response content after submit form. It is necessary for backwards compatibility and will be removed before Drupal 9.0. You should just use the webAssert object for your assertions.

Deprecated

in drupal:9.1.0 and is removed from drupal:10.0.0. Use $this->submitForm() instead.

See also

\Drupal\Tests\WebAssert::buttonExists()

https://www.drupal.org/node/3168858

1 call to UiHelperTrait::drupalPostForm()
BrowserTestBaseTest::testLegacyDrupalPostForm in core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
Tests deprecation of drupalPostForm().

File

core/tests/Drupal/Tests/UiHelperTrait.php, line 197

Class

UiHelperTrait
Provides UI helper methods.

Namespace

Drupal\Tests

Code

protected function drupalPostForm($path, $edit, $submit, array $options = [], $form_html_id = NULL) {
  @trigger_error('UiHelperTrait::drupalPostForm() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use $this->submitForm() instead. See https://www.drupal.org/node/3168858', E_USER_DEPRECATED);
  if (is_object($submit)) {
    @trigger_error('Calling ' . __METHOD__ . '() with $submit as an object is deprecated in drupal:9.2.0 and the method is removed in drupal:10.0.0. Use $this->submitForm() instead. See https://www.drupal.org/node/3168858', E_USER_DEPRECATED);

    // Cast MarkupInterface objects to string.
    $submit = (string) $submit;
  }
  if ($edit === NULL) {
    @trigger_error('Calling ' . __METHOD__ . '() with $edit set to NULL is deprecated in drupal:9.1.0 and the method is removed in drupal:10.0.0. Use $this->submitForm() instead. See https://www.drupal.org/node/3168858', E_USER_DEPRECATED);
    $edit = [];
  }
  if ($path === NULL) {
    @trigger_error('Calling ' . __METHOD__ . '() with $path set to NULL is deprecated in drupal:9.2.0 and the method is removed in drupal:10.0.0. Use $this->submitForm() instead. See https://www.drupal.org/node/3168858', E_USER_DEPRECATED);
  }
  if (isset($path)) {
    $this
      ->drupalGet($path, $options);
  }
  $this
    ->submitForm($edit, $submit, $form_html_id);
  return $this
    ->getSession()
    ->getPage()
    ->getContent();
}