You are here

public function PdfWebformPopulationTest::testPdfPopulation in FillPDF 8.4

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

Tests Webform population and image stamping.

File

tests/src/Functional/PdfWebformPopulationTest.php, line 71

Class

PdfWebformPopulationTest
Tests Webform population and image stamping.

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testPdfPopulation() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');
  $this
    ->assertSession()
    ->pageTextContains('New FillPDF form has been created.');
  $fillpdf_form = FillPdfForm::load($this
    ->getLatestFillPdfForm());

  // Get the field definitions for the form that was created and configure
  // them.
  $this
    ->mapFillPdfFieldsToWebformFields($fillpdf_form
    ->getFormFields());

  // Hit the generation route, check the results from the test backend plugin.
  $fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
    'query' => [
      'fid' => $fillpdf_form
        ->id(),
      'entity_id' => "webform_submission:{$this->testSubmission->id()}",
    ],
  ]);
  $this
    ->drupalGet($fillpdf_route);

  // We don't actually care about downloading the fake PDF. We just want to
  // check what happened in the backend.
  $populate_result = $this->container
    ->get('state')
    ->get('fillpdf_test.last_populated_metadata');
  $submission_values = $this->testSubmission
    ->getData();
  self::assertEquals($populate_result['field_mapping']['fields']['TextField1'], $this->testSubmission
    ->getWebform()
    ->label(), 'PDF is populated with the title of the Webform Submission.');
  $submission_file = File::load($submission_values['image'][0]);
  self::assertEquals($populate_result['field_mapping']['images']['ImageField']['data'], base64_encode(file_get_contents($submission_file
    ->getFileUri())), 'Encoded image matches known image.');
  $path_info = pathinfo($submission_file
    ->getFileUri());
  $expected_file_hash = md5($path_info['filename']) . '.' . $path_info['extension'];
  self::assertEquals($populate_result['field_mapping']['images']['ImageField']['filenamehash'], $expected_file_hash, 'Hashed filename matches known hash.');
  self::assertEquals($populate_result['field_mapping']['fields']['ImageField'], "{image}{$submission_file->getFileUri()}", 'URI in metadata matches expected URI.');
  self::assertEquals('{image}webform_signature.png', $populate_result['field_mapping']['fields']['TestButton'], 'Signature matches signature from Webform.');
  $signature_image = TokenResolver::getSignatureImage($submission_values['test_signature']);
  self::assertEquals(base64_encode($signature_image), $populate_result['field_mapping']['images']['TestButton']['data'], 'Signature matches signature from Webform.');
}