You are here

public function PdfWebformPopulationTest::testPdfPopulation in FillPDF 5.0.x

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

Tests Webform population and image stamping.

File

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

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']['TextField1']
    ->getData(), $this->testSubmission
    ->getWebform()
    ->label(), 'PDF is populated with the title of the Webform Submission.');
  $submission_file = File::load($submission_values['image'][0]);

  /** @var \Drupal\fillpdf\FieldMapping\ImageFieldMapping $image_field_mapping */
  $image_field_mapping = $populate_result['field_mapping']['ImageField'];
  self::assertEquals(base64_encode($image_field_mapping
    ->getData()), 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'];
  $actual_path_info = pathinfo($image_field_mapping
    ->getUri());
  $actual_file_hash = md5($actual_path_info['filename']) . '.' . $actual_path_info['extension'];
  self::assertEquals($actual_file_hash, $expected_file_hash, 'Hashed filename matches known hash.');
  self::assertEquals($image_field_mapping
    ->getUri(), $submission_file
    ->getFileUri(), 'URI in metadata matches expected URI.');
  $signature_image = TokenResolver::getSignatureImage($submission_values['test_signature']);
  self::assertEquals(base64_encode($signature_image), base64_encode($populate_result['field_mapping']['TestButton']
    ->getData()), 'Signature matches signature from Webform.');
}