You are here

public function PdfPopulationTest::testPdfPopulation 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::testPdfPopulation()

Tests Core entity population and image stamping.

File

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

Class

PdfPopulationTest
Tests Core entity 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.');

  // Load the FillPdf Form and add a form-level replacement.
  $fillpdf_form = FillPdfForm::load($this
    ->getLatestFillPdfForm());
  $fillpdf_form->replacements = 'Hello & how are you?|Hello & how is it going?';
  $fillpdf_form
    ->save();

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

  // Create a node to populate the FillPdf Form.
  $node = $this
    ->createNode([
    'title' => 'Hello & how are you?',
    'type' => 'article',
    'body' => [
      [
        'value' => "<p>PDF form fields don't accept <em>any</em> HTML.</p>",
        'format' => 'restricted_html',
      ],
    ],
  ]);

  // Hit the generation route, check the results from the test backend plugin.
  $url = $this->linkManipulator
    ->generateLink([
    'fid' => $fillpdf_form
      ->id(),
    'entity_ids' => [
      'node' => [
        $node
          ->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');
  self::assertEquals('Hello & how are you doing?', $populate_result['field_mapping']['fields']['TextField1'], 'PDF is populated with the title of the node with all HTML stripped.');
  self::assertEquals("PDF form fields don't accept any HTML.\n", $populate_result['field_mapping']['fields']['TextField2'], 'PDF is populated with the node body. HTML is stripped but a newline
       replaces the <p> tags.');
}