You are here

public function PdfPopulationTest::testPluginApi in FillPDF 8.4

Test plugin APIs directly to make sure third-party consumers can use them.

File

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

Class

PdfPopulationTest
Tests Core entity population and image stamping.

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testPluginApi() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');
  $fillpdf_form = FillPdfForm::load($this
    ->getLatestFillPdfForm());

  // Get the field definitions from the actually created form and sort.
  $actual_keys = [];
  foreach (array_keys($fillpdf_form
    ->getFormFields()) as $pdf_key) {
    $actual_keys[] = $pdf_key;
  }
  sort($actual_keys);

  // Get the fields from the fixture and sort.
  $expected_keys = [];
  foreach (TestFillPdfBackend::getParseResult() as $expected_field) {
    $expected_keys[] = $expected_field['name'];
  }
  sort($expected_keys);

  // Now compare. InputHelper::attachPdfToForm() filtered out the duplicate,
  // so the count differs, but not the actual values.
  $this
    ->assertCount(5, $expected_keys);
  $this
    ->assertCount(4, $actual_keys);
  $differences = array_diff($expected_keys, $actual_keys);
  self::assertEmpty($differences, 'Parsed fields are in fixture match.');

  // Now create an instance of the backend service and test directly.

  /** @var \Drupal\fillpdf_test\Plugin\BackendService\Test $backend_service */
  $backend_service = $this->backendServiceManager
    ->createInstance('test');
  $original_pdf = file_get_contents($this
    ->getTestPdfPath('fillpdf_test_v3.pdf'));

  // Get the fields from the backend service and sort.
  $actual_keys = [];
  foreach ($backend_service
    ->parse($original_pdf) as $parsed_field) {
    $actual_keys[] = $parsed_field['name'];
  }
  sort($actual_keys);

  // Compare the values.
  $this
    ->assertCount(5, $expected_keys);
  $this
    ->assertCount(5, $actual_keys);
  $differences = array_diff($expected_keys, $actual_keys);
  self::assertEmpty($differences, 'Parsed fields from plugin are in fixture match.');

  // Test the merge method. We'd normally pass in values for $webform_fields
  // and $options, but since this is a stub anyway, there isn't much point.
  // @todo: Test deeper using the State API.
  $merged_pdf = $backend_service
    ->merge($original_pdf, [
    'Foo' => new TextFieldMapping('bar'),
    'Foo2' => new TextFieldMapping('bar2'),
    'Image1' => new ImageFieldMapping(file_get_contents($this->testImage
      ->getFileUri()), 'png'),
  ], []);
  self::assertEquals($original_pdf, $merged_pdf);
  $merge_state = $this->container
    ->get('state')
    ->get('fillpdf_test.last_populated_metadata');

  // Check that fields are set as expected.
  self::assertInstanceOf(TextFieldMapping::class, $merge_state['field_mapping']['Foo'], 'Field "Foo" was mapped to a TextFieldMapping object.');
  self::assertInstanceOf(TextFieldMapping::class, $merge_state['field_mapping']['Foo2'], 'Field "Foo2" was mapped to a TextFieldMapping object.');
  self::assertInstanceOf(ImageFieldMapping::class, $merge_state['field_mapping']['Image1'], 'Field "Image1" was mapped to an ImageFieldMapping object.');
}