You are here

protected function SmartlingTestBase::submitForm in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/SmartlingTestBase.php \Drupal\Tests\tmgmt_smartling\Functional\SmartlingTestBase::submitForm()

Same as UiHelperTrait::submitForm() but is able to fill in hidden fields.

Overrides UiHelperTrait::submitForm

File

tests/src/Functional/SmartlingTestBase.php, line 356

Class

SmartlingTestBase
Basic tests for the Smartling translator.

Namespace

Drupal\Tests\tmgmt_smartling\Functional

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);
    $action = $form
      ->getAttribute('action');
  }
  else {
    $submit_button = $assert_session
      ->buttonExists($submit);
    $form = $assert_session
      ->elementExists('xpath', './ancestor::form', $submit_button);
    $action = $form
      ->getAttribute('action');
  }

  // Edit the form values.
  foreach ($edit as $name => $value) {

    // If field is not found then probably it's a hidden field.
    try {
      $field = $assert_session
        ->fieldExists($name, $form);
    } catch (ElementNotFoundException $e) {
      $field = $assert_session
        ->hiddenFieldExists($name, $form);
    }

    // Provide support for the values '1' and '0' for checkboxes instead of
    // TRUE and FALSE.
    // @todo Get rid of supporting 1/0 by converting all tests cases using
    // this to boolean values.
    $field_type = $field
      ->getAttribute('type');
    if ($field_type === 'checkbox') {
      $value = (bool) $value;
    }
    $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();

  // Check if there are any meta refresh redirects (like Batch API pages).
  if ($this
    ->checkForMetaRefresh()) {

    // We are finished with all meta refresh redirects, so reset the counter.
    $this->metaRefreshCount = 0;
  }

  // Log only for JavascriptTestBase tests because for Goutte we log with
  // ::getResponseLogHandler.
  if ($this->htmlOutputEnabled && !$this
    ->getSession()
    ->getDriver() instanceof GoutteDriver) {
    $out = $this
      ->getSession()
      ->getPage()
      ->getContent();
    $html_output = 'POST request to: ' . $action . '<hr />Ending URL: ' . $this
      ->getSession()
      ->getCurrentUrl();
    $html_output .= '<hr />' . $out;
    $html_output .= $this
      ->getHtmlOutputHeaders();
    $this
      ->htmlOutput($html_output);
  }
}