You are here

public function HandlePdfControllerTest::testTokenFilenameDestination in FillPDF 8.4

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

Tests filename and destination of a populated PDF file.

File

tests/src/Functional/HandlePdfControllerTest.php, line 134

Class

HandlePdfControllerTest
@coversDefaultClass \Drupal\fillpdf\Controller\HandlePdfController

Namespace

Drupal\Tests\fillpdf\Functional

Code

public function testTokenFilenameDestination() {
  $this
    ->uploadTestPdf('fillpdf_test_v3.pdf');
  $form_id = $this
    ->getLatestFillPdfForm();
  $edit = [
    'title[0][value]' => '[current-date:html_year]-[user:account-name]-[node:title].pdf',
    'scheme' => 'public',
    'destination_path[0][value]' => '[current-date:html_year]-[user:account-name]-[node:title]',
  ];
  $this
    ->drupalPostForm("admin/structure/fillpdf/{$form_id}", $edit, 'Save');
  $year = date('Y');
  $node1_id = $this->testNodes[1]
    ->id();
  $node1_title = $this->testNodes[1]
    ->getTitle();
  $node2_id = $this->testNodes[2]
    ->id();
  $node2_title = $this->testNodes[2]
    ->getTitle();
  $user_id = $this->adminUser
    ->id();
  $user_name = $this->adminUser
    ->getAccountName();
  $testcases = [];

  // Test case 0: no entity.
  $testcases[1]['entities'] = [];
  $testcases[1]['expected'] = "{$year}--";

  // Test case 1: existing node.
  $testcases[1]['entities'] = [
    "node:{$node1_id}",
  ];
  $testcases[1]['expected'] = "{$year}--{$node1_title}";

  // Test case 2: two existing nodes.
  $testcases[2]['entities'] = [
    "node:{$node1_id}",
    "node:{$node2_id}",
  ];
  $testcases[2]['expected'] = "{$year}--{$node2_title}";

  // Test case 3: twice the same node.
  $testcases[3]['entities'] = [
    "node:{$node1_id}",
    "node:{$node1_id}",
  ];
  $testcases[3]['expected'] = "{$year}--{$node1_title}";

  // Test case 4: existing user.
  $testcases[4]['entities'] = [
    "user:{$user_id}",
  ];
  $testcases[4]['expected'] = "{$year}-{$user_name}-";

  // Test case 5: existing node and existing user.
  $testcases[5]['entities'] = [
    "node:{$node1_id}",
    "user:{$user_id}",
  ];
  $testcases[5]['expected'] = "{$year}-{$user_name}-{$node1_title}";

  // Test case 6: non-existing node.
  $testcases[6]['entities'] = [
    "node:123",
  ];
  $testcases[6]['expected'] = "{$year}--";

  // Test case 7: existing node and non-existing user.
  $testcases[7]['entities'] = [
    "node:{$node1_id}",
    "user:456",
  ];
  $testcases[7]['expected'] = "{$year}--{$node1_title}";
  foreach ($testcases as $id => $case) {

    // Hit the generation route.
    $entities = $case['entities'];
    $fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
      'query' => [
        'fid' => $form_id,
        'entity_ids' => $entities,
      ],
    ]);
    $this
      ->drupalGet($fillpdf_route);

    // Get last file and check if filename and path are correct.
    $file = File::load($this
      ->getLastFileId());
    $filename = $file
      ->getFilename();
    $uri = $file
      ->getFileUri();
    $expected = $case['expected'];
    $this
      ->assertEquals("{$expected}.pdf", $filename, "Test case {$id}: The file has the filename {$filename}.");
    $this
      ->assertEquals("public://fillpdf/{$expected}/{$expected}.pdf", $uri, "Test case {$id}: The file has the expected URI.");

    // Check if file is permanent and has the right format.
    $this
      ->assertFileIsPermanent($file);
    $this
      ->drupalGet(file_create_url($uri));
    $maybe_pdf = $this
      ->getSession()
      ->getPage()
      ->getContent();
    static::assertEquals('application/pdf', $this
      ->getMimeType($maybe_pdf), "Test case {$id}: The file has the correct MIME type.");

    // Delete the file, so we don't run into conflicts with the next testcase.
    $file
      ->delete();
  }
}