You are here

public function PdfPopulationTest::testImageStamping in FillPDF 8.4

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

Tests Core image stamping.

File

tests/src/Functional/PdfPopulationTest.php, line 152

Class

PdfPopulationTest
Tests Core entity population and image stamping.

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testImageStamping() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');
  $this
    ->assertSession()
    ->pageTextContains('New FillPDF form has been created.');
  $fillpdf_form = FillPdfForm::load($this
    ->getLatestFillPdfForm());
  $testCases = [
    'node' => 'article',
    'taxonomy_term' => $this->testVocabulary
      ->id(),
    'user' => 'user',
  ];
  foreach ($testCases as $entity_type => $bundle) {
    $this
      ->createImageField('field_fillpdf_test_image', $entity_type, $bundle);
    $storage = \Drupal::entityTypeManager()
      ->getStorage($entity_type);
    $entity = $storage
      ->load($this
      ->createImageFieldEntity($this->testImage, 'field_fillpdf_test_image', $entity_type, $bundle, 'FillPDF Test image'));

    // Get the field definitions for the form that was created and configure
    // them.
    FillPdfTestBase::mapFillPdfFieldsToEntityFields($entity_type, $fillpdf_form
      ->getFormFields());

    // Hit the generation route, check results from the test backend plugin.
    $url = $this->linkManipulator
      ->generateLink([
      'fid' => $fillpdf_form
        ->id(),
      'entity_ids' => [
        $entity_type => [
          $entity
            ->id(),
        ],
      ],
    ]);
    $this
      ->drupalGet($url);

    // 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');
    $file = File::load($entity->field_fillpdf_test_image->target_id);
    self::assertArrayHasKey('ImageField', $populate_result['field_mapping']['images'], "{$entity_type} isn't populated with an image.");
    self::assertEquals($populate_result['field_mapping']['images']['ImageField']['data'], base64_encode(file_get_contents($file
      ->getFileUri())), 'Encoded image matches known image.');
    $path_info = pathinfo($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}{$file->getFileUri()}", 'URI in metadata matches expected URI.');
  }
}