You are here

protected function PdfParseTest::backendTest in FillPDF 8.4

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

Tests a backend.

@internal

@todo Consolidate duplicate code with PdfPopulationTest. @todo This may be significantly simplified once we're initializing a FillPdfForm with the parsed values.

Parameters

\Drupal\Core\Config\Config $fillpdf_config: FillPDF configuration object.

Return value

\Drupal\fillpdf\FillPdfFormInterface The created FillPdfForm.

Throws

\Behat\Mink\Exception\ResponseTextException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Core\Entity\EntityStorageException

See also

https://www.drupal.org/project/fillpdf/issues/3056400

2 calls to PdfParseTest::backendTest()
PdfParseTest::testParseLocalService in tests/src/Functional/PdfParseTest.php
Tests PDF population using local service.
PdfParseTest::testParsePdftk in tests/src/Functional/PdfParseTest.php
Tests PDF population using a local install of pdftk.

File

tests/src/Functional/PdfParseTest.php, line 73

Class

PdfParseTest
Tests PDF parsing.

Namespace

Drupal\Tests\fillpdf\Functional

Code

protected function backendTest(Config $fillpdf_config) {
  $this
    ->uploadTestPdf('fillpdf_Ŧäßð_v3â.pdf');
  $this
    ->assertSession()
    ->pageTextNotContains('No fields detected in PDF.');
  $fillpdf_form = FillPdfForm::load($this
    ->getLatestFillPdfForm());
  $fields = $fillpdf_form
    ->getFormFields();
  $this
    ->assertCount($this
    ->getExpectedFieldCount($fillpdf_config
    ->get('backend')), $fields);

  // Get the uploaded template's file ID.
  $previous_file_id = $fillpdf_form->file->target_id;

  // Set public scheme so populated files are saved to disk.
  $fillpdf_form->scheme = 'public';
  $fillpdf_form
    ->save();

  // Populate an unflattened sample PDF file and do some checks.
  $fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
    'query' => [
      'fid' => $fillpdf_form
        ->id(),
      'sample' => 1,
      'flatten' => 0,
    ],
  ]);
  $this
    ->drupalGet($fillpdf_route);
  $this
    ->assertSession()
    ->pageTextNotContains('Merging the FillPDF Form failed');

  // Retrieve the last file ID which should be the sample file.
  // @todo: When using pdftk, the saved xfdf file leads to the file counter
  // being increased by two instead of 1. We're therefore only comparing by
  // "greater than".
  $file_id = $this
    ->getLastFileId();
  static::assertTrue($file_id > $previous_file_id, 'Populated PDF was saved as a new managed file.');

  // Load the sample file and check it is a PDF.
  $file = File::load($file_id);
  static::assertEquals('application/pdf', $file
    ->getMimeType());

  // Create an instance of the backend plugin.
  $backend_manager = $this->container
    ->get('plugin.manager.fillpdf.pdf_backend');
  $backend = $backend_manager
    ->createInstance($fillpdf_config
    ->get('backend'), $fillpdf_config
    ->get());

  // Re-parse the sample PDF file and check for each text field that the
  // field value equals the field name (now in angle brackets, since the
  // sample functionality does that).
  foreach ($backend
    ->parseFile($file) as $field) {
    if ($field['type'] == 'Text') {
      $value = isset($field['value']) ? $field['value'] : NULL;
      static::assertEquals("<{$field['name']}>", $value);
    }
  }
  return $fillpdf_form;
}