You are here

public function FillPdfSettingsFormTest::testBackendTest in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 tests/src/Functional/FillPdfSettingsFormTest.php \Drupal\Tests\fillpdf\Functional\FillPdfSettingsFormTest::testBackendTest()

Tests the backend settings with the 'test' backend.

File

tests/src/Functional/FillPdfSettingsFormTest.php, line 221

Class

FillPdfSettingsFormTest
@coversDefaultClass \Drupal\fillpdf\Form\FillPdfSettingsForm @group fillpdf

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testBackendTest() {

  // FillPDF is not yet configured.
  // Go to the settings page and verify the autodetected 'test' backend is
  // present only once and with the form-altered label.
  $this
    ->drupalGet(Url::fromRoute('fillpdf.settings'));
  $this
    ->assertSession()
    ->pageTextContainsOnce('plugin for testing');
  $this
    ->assertSession()
    ->pageTextContains('Form-altered pass-through plugin for testing');

  // Try configuring FillPDF with the 'test' backend, yet with invalid values
  // for the form-altered 'example_setting' and the unrelated
  // 'fillpdf_service_api_key'.
  $edit = [
    'template_scheme' => 'private',
    'backend' => 'test',
    'example_setting' => 'x',
    'fillpdf_service_api_key' => 'Invalid, just playing around.',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save configuration');

  // The 'test' radio option is present and working.
  $this
    ->assertSession()
    ->pageTextNotContains('An illegal choice has been detected.');

  // However, our custom validation handler kicked in.
  $this
    ->assertSession()
    ->pageTextNotContains('The configuration options have been saved.');
  $this
    ->assertSession()
    ->pageTextContains('Not a valid value.');

  // Therefore, the new values should be submitted, but *not* saved.
  foreach ($edit as $field => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals($field, $value);
    $this
      ->assertEqual($this
      ->config('fillpdf.settings')
      ->get($field), NULL);
  }

  // Try again with a valid value.
  $edit['example_setting'] = 'xyz';
  $this
    ->drupalPostForm(NULL, $edit, 'Save configuration');

  // This time, our custom validation handler passes.
  $this
    ->assertSession()
    ->pageTextNotContains('Not a valid value.');
  $this
    ->assertSession()
    ->pageTextContains('The configuration options have been saved.');

  // So the new values should be submitted *and* saved this time, except for
  // the unrelated 'fillpdf_service_api_key' which should be dismissed.
  $expected = [
    'fillpdf_service_api_key' => NULL,
  ] + $edit;
  foreach ($expected as $field => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals($field, $value);
    $this
      ->assertEqual($this
      ->config('fillpdf.settings')
      ->get($field), $value);
  }
}