You are here

public function FillPdfFormFormTest::testStorageSettings in FillPDF 8.4

Same name and namespace in other branches
  1. 5.0.x tests/src/Functional/FillPdfFormFormTest.php \Drupal\Tests\fillpdf\Functional\FillPdfFormFormTest::testStorageSettings()

Tests the FillPdfForm entity's edit form.

File

tests/src/Functional/FillPdfFormFormTest.php, line 117

Class

FillPdfFormFormTest
@coversDefaultClass \Drupal\fillpdf\Form\FillPdfFormForm @group fillpdf

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testStorageSettings() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');
  $form_id = $this
    ->getLatestFillPdfForm();
  $previous_file_id = $this
    ->getLastFileId();
  $edit_form_url = Url::fromRoute('entity.fillpdf_form.edit_form', [
    'fillpdf_form' => $form_id,
  ]);
  $generate_url = Url::fromRoute('fillpdf.populate_pdf', [], [
    'query' => [
      'fid' => $form_id,
      'entity_id' => "node:{$this->testNode->id()}",
    ],
  ]);

  // Check the initial storage settings.
  $this
    ->assertSession()
    ->fieldValueEquals('scheme', '_none');
  foreach ([
    '- None -',
    'private://',
    'public://',
  ] as $option) {
    $this
      ->assertSession()
      ->optionExists('scheme', $option);
  }
  $this
    ->assertSession()
    ->fieldValueEquals('destination_path[0][value]', '');
  $this
    ->drupalGet($edit_form_url);

  // Now hit the generation route and make sure the PDF file is *not* stored.
  $this
    ->drupalGet($generate_url);
  $this
    ->assertEquals($previous_file_id, $this
    ->getLastFileId(), 'Generated file is not stored.');

  // Set the 'public' scheme and see if the 'destination_path' field appears.
  $this
    ->drupalPostForm($edit_form_url, [
    'scheme' => 'public',
  ], self::OP_SAVE);
  $this
    ->assertSession()
    ->fieldValueEquals('scheme', 'public');
  $this
    ->assertSession()
    ->pageTextContains('Destination path');

  // Hit the generation route again and make sure this time the PDF file is
  // stored in the public storage.
  $this
    ->drupalGet($generate_url);
  $this
    ->assertEquals(++$previous_file_id, $this
    ->getLastFileId(), 'Generated file was stored.');
  $this
    ->assertStringStartsWith('public://', File::load($this
    ->getLastFileId())
    ->getFileUri());

  // Now disallow the public scheme and reload.
  $this
    ->configureFillPdf([
    'allowed_schemes' => [
      'private',
    ],
  ]);

  // Reload and check if the public option has disappeared now.
  $this
    ->drupalGet($edit_form_url);
  $this
    ->assertSession()
    ->fieldValueEquals('scheme', '_none');
  foreach ([
    '- None -',
    'private://',
  ] as $option) {
    $this
      ->assertSession()
      ->optionExists('scheme', $option);
  }
  $this
    ->assertSession()
    ->optionNotExists('scheme', 'public://');

  // Hit the generation route once more and make sure the scheme has been
  // unset, so the PDF file is *not* stored.
  $this
    ->drupalGet($generate_url);
  $this
    ->assertEquals($previous_file_id, $this
    ->getLastFileId(), 'Generated file is not stored.');

  // Set the 'private' scheme.
  $this
    ->drupalPostForm($edit_form_url, [
    'scheme' => 'private',
  ], self::OP_SAVE);
  $this
    ->assertSession()
    ->fieldValueEquals('scheme', 'private');

  // Hit the generation route again and make sure this time the PDF file is
  // stored in the private storage.
  $this
    ->drupalGet($generate_url);
  $this
    ->assertEquals(++$previous_file_id, $this
    ->getLastFileId(), 'Generated file was stored.');
  $this
    ->assertStringStartsWith('private://', File::load($this
    ->getLastFileId())
    ->getFileUri());

  // Now disallow the private scheme as well and reload.
  $this
    ->configureFillPdf([
    'allowed_schemes' => [],
  ]);
  $this
    ->drupalGet($edit_form_url);

  // Check if the whole storage settings section has disappeared now.
  $this
    ->assertSession()
    ->pageTextNotContains('Storage and download');

  // Hit the generation route one last time and make sure the PDF file is
  // again *not* stored.
  $this
    ->drupalGet($generate_url);
  $this
    ->assertEquals($previous_file_id, $this
    ->getLastFileId(), 'Generated file is not stored.');
}