You are here

protected function FillPdfUploadTestBase::assertUploadPdfFile in FillPDF 8.4

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

Asserts that a PDF file may be properly uploaded as a template.

Parameters

string $op: (optional) Operation to perform. May be either of:

bool $filename_preexists: (optional) Whether the testfile has previously been uploaded, so a file with the same filename preexists. Defaults to FALSE.

\Drupal\fillpdf\FillPdfFormInterface $form: The FillPDF Form that is being updated. Needed so we can make some assertions against the fields.

Throws

\Behat\Mink\Exception\ExpectationException

\Behat\Mink\Exception\ResponseTextException

2 calls to FillPdfUploadTestBase::assertUploadPdfFile()
FillPdfFormFormTest::testFormFormUpload in tests/src/Functional/FillPdfFormFormTest.php
Tests the FillPdfForm entity's edit form.
FillPdfOverviewFormTest::testOverviewFormUpload in tests/src/Functional/FillPdfOverviewFormTest.php
Tests the overview form's PDF file upload functionality.

File

tests/src/Functional/FillPdfUploadTestBase.php, line 92

Class

FillPdfUploadTestBase
Allows testing everything around uploading PDF template files.

Namespace

Drupal\Tests\fillpdf\Functional

Code

protected function assertUploadPdfFile($op = self::OP_UPLOAD, $filename_preexists = FALSE, FillPdfFormInterface $form = NULL) {
  $previous_file_id = $this
    ->getLastFileId();
  if ($op === self::OP_SAVE) {

    // Record the mappings in the FillPdfFormFields before overwriting the
    // file. We may need to compare them later.
    $existing_fields = $form
      ->getFormFields();
    $existing_mappings = [];
    foreach ($existing_fields as $existing_field) {
      $existing_mappings[$existing_field->pdf_key->value] = $existing_field->value->value;
    }
  }

  // Upload PDF test file.
  $edit = [
    'files[upload_pdf]' => $this
      ->getTestPdfPath('fillpdf_test_v3.pdf'),
  ];
  $this
    ->drupalPostForm(NULL, $edit, $op);

  // Whether submitted or just uploaded, at least temporarily the file should
  // exist now both as an object and physically on the disk.

  /** @var \Drupal\file\FileInterface $new_file */
  $new_file = File::load($this
    ->getLastFileId());
  $new_filename = $new_file
    ->getFilename();
  if (version_compare(Drupal::VERSION, '8.8.0', '<')) {

    // @todo: REMOVE when Drupal 8.7.x is no longer supported.
    $this
      ->assertFileExists($new_file);
  }
  else {
    $this
      ->assertFileExists($new_file
      ->getFileUri());
  }
  $this
    ->assertLessThan((int) $new_file
    ->id(), $previous_file_id);

  // If the same file was previously uploaded, it should have a "_0" appendix.
  $this
    ->assertEquals($new_filename, $filename_preexists ? 'fillpdf_test_v3_0.pdf' : 'fillpdf_test_v3.pdf');
  switch ($op) {
    case self::OP_UPLOAD:

      // We only uploaded, so make sure FillPdf Forms were not affected.
      $this
        ->assertSession()
        ->pageTextNotContains('New FillPDF form has been created.');
      $this
        ->assertSession()
        ->pageTextNotContains('Your previous field mappings have been transferred to the new PDF template you uploaded.');

      // Make sure the file is temporary only.
      // @todo Simplify once there is an assertFileIsTemporary().
      //   See: https://www.drupal.org/project/drupal/issues/3043129.
      $this
        ->assertTrue($new_file
        ->isTemporary(), new FormattableMarkup('File %file is temporary.', [
        '%file' => $new_file
          ->getFileUri(),
      ]));

      // Now remove the PDF file again. The temporary file should now be
      // removed both from the disk and the database.
      $this
        ->drupalPostForm(NULL, NULL, self::OP_REMOVE);
      if (version_compare(Drupal::VERSION, '8.8.0', '<')) {

        // @todo: REMOVE when Drupal 8.7.x is no longer supported.
        $this
          ->assertFileNotExists($new_file);
      }
      else {
        $this
          ->assertFileNotExists($new_file
          ->getFileUri());
      }

      // @todo Simplify once Core bug gets fixed.
      //   See: https://www.drupal.org/project/drupal/issues/3043127.
      $this
        ->assertFileEntryNotExists($new_file, NULL);
      break;
    case self::OP_CREATE:

      // A new FillPdfForm should be created.
      $this
        ->assertSession()
        ->pageTextContains('New FillPDF form has been created.');
      $this
        ->assertSession()
        ->pageTextNotContains('Your previous field mappings have been transferred to the new PDF template you uploaded.');

      // There should be four fields in the correct order.
      // @todo: Add some CSS markup to the view so we can test the order.
      $this
        ->assertSession()
        ->pageTextContainsOnce('ImageField');
      $this
        ->assertSession()
        ->pageTextContainsOnce('TestButton');
      $this
        ->assertSession()
        ->pageTextContainsOnce('TextField1');
      $this
        ->assertSession()
        ->pageTextContainsOnce('TextField2');
      $this
        ->assertSession()
        ->elementsCount('css', 'tbody > tr', 4);

      // Make sure the file is permanent and correctly placed.
      $this
        ->assertFileIsPermanent($new_file);
      $expected_file_uri = FillPdf::buildFileUri($this
        ->config('fillpdf.settings')
        ->get('template_scheme'), "fillpdf/{$new_filename}");
      $this
        ->assertEquals($new_file
        ->getFileUri(), $expected_file_uri);
      break;
    case self::OP_SAVE:

      // The current FillPdfForm should be updated with the new file.
      $this
        ->assertSession()
        ->pageTextNotContains('New FillPDF form has been created.');
      $this
        ->assertSession()
        ->pageTextContains('Your previous field mappings have been transferred to the new PDF template you uploaded.');

      // Make sure the file is permanent and correctly placed.
      $this
        ->assertFileIsPermanent($new_file);
      $expected_file_uri = FillPdf::buildFileUri($this
        ->config('fillpdf.settings')
        ->get('template_scheme'), "fillpdf/{$new_filename}");
      $this
        ->assertEquals($new_file
        ->getFileUri(), $expected_file_uri);
      $new_fields = $form
        ->getFormFields();
      $new_mappings = [];
      foreach ($new_fields as $new_field) {
        $new_mappings[$new_field->pdf_key->value] = $new_field->value->value;
      }

      /** @var array $existing_mappings */
      foreach ($existing_mappings as $field_name => $existing_mapping) {
        $this
          ->assertEquals($existing_mapping, $new_mappings[$field_name], 'New mapping value matches old mapping value.');
      }
      break;
  }
}