You are here

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

Tests DownloadAction.

Throws

\Behat\Mink\Exception\ExpectationException

File

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

Class

HandlePdfControllerTest
@coversDefaultClass \Drupal\fillpdf\Controller\HandlePdfController

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testDownloadAction() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');
  $form_id = $this
    ->getLatestFillPdfForm();
  $fid_before = $this
    ->getLastFileId();
  $fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
    'query' => [
      'fid' => $form_id,
      'sample' => 1,
    ],
  ]);
  $this
    ->drupalGet($fillpdf_route);
  $fid_after = $this
    ->getLastFileId();

  // Make sure the PDF file has not been saved.
  $this
    ->assertEquals($fid_before, $fid_after);

  // Make sure we are seeing the downloaded PDF.
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $maybe_pdf = $this
    ->getSession()
    ->getPage()
    ->getContent();
  static::assertEquals('application/pdf', $this
    ->getMimeType($maybe_pdf), 'The file has the correct MIME type.');

  // Ensure the headers are set to make the PDF download.
  $this
    ->assertSession()
    ->responseHeaderContains('Content-Disposition', ResponseHeaderBag::DISPOSITION_ATTACHMENT);
  $this
    ->assertSession()
    ->responseHeaderContains('Content-Type', 'application/pdf');
  $this
    ->assertSession()
    ->responseHeaderContains('Content-Length', (string) strlen(file_get_contents($this
    ->getTestPdfPath('fillpdf_test_v3.pdf'))));
}