You are here

public function FillPdfFormFormTest::testDefaultEntityId in FillPDF 8.4

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

Tests the FillPdfForm entity's edit form.

File

tests/src/Functional/FillPdfFormFormTest.php, line 31

Class

FillPdfFormFormTest
@coversDefaultClass \Drupal\fillpdf\Form\FillPdfFormForm @group fillpdf

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testDefaultEntityId() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');

  // Default entity type is not yet given, so the ID element should be hidden.
  $this
    ->assertSession()
    ->hiddenFieldExists('default_entity_id');

  // Create 25 more users to check the threshold.
  for ($i = 0; $i < 25; $i++) {
    $this
      ->createUser();
  }
  $testcases = [
    // Test case: no referenceable entity.
    'fillpdf_file_context' => [
      0,
      0,
      '',
    ],
    // Test case: 1 referenceable entity.
    'node' => [
      1,
      $this->testNode
        ->id(),
      $this->testNode
        ->label(),
    ],
    // Test case: 26 referenceable entities.
    'user' => [
      26,
      $this->adminUser
        ->id(),
      $this->adminUser
        ->label(),
    ],
  ];
  foreach ($testcases as $type => list($count, $id, $label)) {

    // Set a default entity type and check if it's properly saved.
    $this
      ->drupalPostForm(NULL, [
      'default_entity_type' => $type,
    ], self::OP_SAVE);
    $this
      ->assertSession()
      ->pageTextContains("FillPDF Form has been updated.");
    $this
      ->assertSession()
      ->fieldValueEquals('default_entity_type', $type);

    // Check the default entity ID field is present but empty.
    $this
      ->assertSession()
      ->fieldValueEquals('default_entity_id', NULL);
    if ($count == 0) {
      $options = $this
        ->assertSession()
        ->selectExists('default_entity_id')
        ->findAll('xpath', 'option');
      $this
        ->assertCount(1, $options);
      $this
        ->assertEquals('', $options[0]
        ->getValue());
      $this
        ->assertEquals('- None -', $options[0]
        ->getText());

      // Skip the rest and continue with the next test case.
      continue;
    }
    elseif ($count <= 25) {
      $this
        ->assertSession()
        ->pageTextContains("Choose a {$type} to test populating the PDF template.");

      // Now enter an entity title.
      $this
        ->assertSession()
        ->optionExists('default_entity_id', $id);
      $this
        ->drupalPostForm(NULL, [
        'default_entity_id' => $id,
      ], self::OP_SAVE);
      $expected_value = $id;
    }
    else {
      $this
        ->assertSession()
        ->pageTextContains("Enter the title of a {$type} to test populating the PDF template.");

      // Now choose an entity and check the entity type is unchanged.
      $this
        ->drupalPostForm(NULL, [
        'default_entity_id' => $label,
      ], self::OP_SAVE);
      $expected_value = "{$label} ({$id})";
    }

    // Check entity type, entity ID and description.
    $this
      ->assertSession()
      ->pageTextContains("FillPDF Form has been updated.");
    $this
      ->assertSession()
      ->fieldValueEquals('default_entity_type', $type);
    $this
      ->assertSession()
      ->fieldValueEquals('default_entity_id', $expected_value);
    $this
      ->assertSession()
      ->linkExistsExact("Download this PDF template populated with data from the {$type} {$label} ({$id}).");
  }
}