You are here

public function HandlePdfControllerTest::testStorageSchemeUnavailable 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::testStorageSchemeUnavailable()

Tests handling of an unavailable storage scheme.

File

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

Class

HandlePdfControllerTest
@coversDefaultClass \Drupal\fillpdf\Controller\HandlePdfController

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testStorageSchemeUnavailable() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');
  $form_id = $this
    ->getLatestFillPdfForm();
  $previous_file_id = $this
    ->getLastFileId();
  $edit = [
    'admin_title[0][value]' => 'Scheme test',
    'scheme' => 'private',
    '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 private:// is unavailable');
  $this
    ->assertEquals(++$previous_file_id, $this
    ->getLastFileId(), 'Generated file was stored.');
  $this
    ->assertStringStartsWith('private://', File::load($this
    ->getLastFileId())
    ->getFileUri());

  // Now remove the private path from settings.php and rebuild the container.
  $this
    ->writeSettings([
    'settings' => [
      'file_private_path' => (object) [
        'value' => '',
        'required' => TRUE,
      ],
    ],
  ]);
  $this
    ->rebuildContainer();

  // 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 private:// 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.');
}