You are here

public function HandlePdfControllerTest::testStorageSchemeDisallowed in FillPDF 8.4

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

Tests handling of an no longer allowed storage scheme.

File

tests/src/Functional/HandlePdfControllerTest.php, line 219

Class

HandlePdfControllerTest
@coversDefaultClass \Drupal\fillpdf\Controller\HandlePdfController

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testStorageSchemeDisallowed() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');
  $form_id = $this
    ->getLatestFillPdfForm();
  $previous_file_id = $this
    ->getLastFileId();
  $edit = [
    'admin_title[0][value]' => 'Scheme test',
    'scheme' => 'public',
    'destination_path[0][value]' => 'test',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
    'query' => [
      'fid' => $form_id,
    ],
  ]);

  // Hit the generation route. Make sure we are redirected to the front page.
  $this
    ->drupalGet($fillpdf_route);
  $this
    ->assertSession()
    ->addressNotEquals('/fillpdf');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Get back to the front page and make sure the file was stored in the
  // private storage.
  $this
    ->drupalGet('<front>');
  $this
    ->assertSession()
    ->pageTextNotContains('File storage scheme public:// is unavailable');
  $this
    ->assertEquals(++$previous_file_id, $this
    ->getLastFileId(), 'Generated file was stored.');
  $this
    ->assertStringStartsWith('public://', File::load($this
    ->getLastFileId())
    ->getFileUri());

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

  // Hit the generation route again. This time we should be redirected to the
  // PDF file. Make sure no PHP error occured.
  $this
    ->drupalGet($fillpdf_route);
  $this
    ->assertSession()
    ->addressEquals('/fillpdf');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Get back to the front page and check if an error was set, and we didn't
  // try to store the file.
  $this
    ->drupalGet('<front>');
  $this
    ->assertSession()
    ->pageTextContains("File storage scheme public:// is unavailable, so a PDF file generated from FillPDF form Scheme test ({$form_id}) could not be stored.");
  $this
    ->assertEquals($previous_file_id, $this
    ->getLastFileId(), 'Generated file was not stored.');
}