Skip to content
Snippets Groups Projects
Commit 4924a6fa authored by Bernd Oliver Suenderhauf's avatar Bernd Oliver Suenderhauf
Browse files

Issue #3047303 by Pancho: Reliably replace tokens in filenames

parent dcaf8ff3
No related branches found
No related tags found
No related merge requests found
...@@ -253,9 +253,7 @@ class HandlePdfController extends ControllerBase { ...@@ -253,9 +253,7 @@ class HandlePdfController extends ControllerBase {
*/ */
protected function buildFilename($original, array $entities) { protected function buildFilename($original, array $entities) {
// Replace tokens *before* sanitization. // Replace tokens *before* sanitization.
if (count($entities)) { $original = $this->tokenResolver->replace($original, $entities);
$original = $this->tokenResolver->replace($original, $entities);
}
$output_name = str_replace(' ', '_', $original); $output_name = str_replace(' ', '_', $original);
$output_name = preg_replace('/\.pdf$/i', '', $output_name); $output_name = preg_replace('/\.pdf$/i', '', $output_name);
......
...@@ -12,15 +12,17 @@ use Drupal\file\Entity\File; ...@@ -12,15 +12,17 @@ use Drupal\file\Entity\File;
* and \Drupal\fillpdf\OutputHandler. * and \Drupal\fillpdf\OutputHandler.
* *
* @group fillpdf * @group fillpdf
*
* @todo Convert into a unit test.
*/ */
class HandlePdfControllerTest extends FillPdfUploadTestBase { class HandlePdfControllerTest extends FillPdfUploadTestBase {
/** /**
* A test node. * A test node.
* *
* @var \Drupal\node\NodeInterface * @var \Drupal\node\NodeInterface[]
*/ */
protected $testNode; protected $testNodes;
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -30,19 +32,20 @@ class HandlePdfControllerTest extends FillPdfUploadTestBase { ...@@ -30,19 +32,20 @@ class HandlePdfControllerTest extends FillPdfUploadTestBase {
$this->configureBackend(); $this->configureBackend();
$this->testNode = $this->createNode([ $this->testNode[1] = $this->createNode([
'title' => 'Hello', 'title' => 'Hello',
'type' => 'article', 'type' => 'article',
]); ]);
$this->testNode[2] = $this->createNode([
'title' => 'Goodbye',
'type' => 'article',
]);
} }
/** /**
* Tests filename and destination of a populated PDF file. * Tests filename and destination of a populated PDF file.
*/ */
public function testTokenFilenameDestination() { public function testTokenFilenameDestination() {
$node_id = $this->testNode->id();
$user_id = $this->adminUser->id();
$this->uploadTestPdf('fillpdf_test_v3.pdf'); $this->uploadTestPdf('fillpdf_test_v3.pdf');
$form_id = $this->getLatestFillPdfForm(); $form_id = $this->getLatestFillPdfForm();
$edit = [ $edit = [
...@@ -51,16 +54,54 @@ class HandlePdfControllerTest extends FillPdfUploadTestBase { ...@@ -51,16 +54,54 @@ class HandlePdfControllerTest extends FillPdfUploadTestBase {
]; ];
$this->drupalPostForm("admin/structure/fillpdf/{$form_id}", $edit, 'Save'); $this->drupalPostForm("admin/structure/fillpdf/{$form_id}", $edit, 'Save');
$entities = [ $year = date('Y');
'node' => $node_id, $node1_id = $this->testNode[1]->id();
'user' => $user_id, $node1_title = $this->testNode[1]->getTitle();
]; $node2_id = $this->testNode[2]->id();
foreach ($entities as $type => $id) { $node2_title = $this->testNode[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. // Hit the generation route.
$entities = $case['entities'];
$fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [ $fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [ 'query' => [
'fid' => $form_id, 'fid' => $form_id,
'entity_id' => "{$type}:{$id}", 'entity_ids' => $entities,
], ],
]); ]);
$this->drupalGet($fillpdf_route); $this->drupalGet($fillpdf_route);
...@@ -70,31 +111,19 @@ class HandlePdfControllerTest extends FillPdfUploadTestBase { ...@@ -70,31 +111,19 @@ class HandlePdfControllerTest extends FillPdfUploadTestBase {
$filename = $file->getFilename(); $filename = $file->getFilename();
$uri = $file->getFileUri(); $uri = $file->getFileUri();
$current_year = date('Y'); $expected = $case['expected'];
switch ($type) { $this->assertEquals("{$expected}.pdf", $filename, "Test case $id: The file has the filename $filename.");
case 'node': $this->assertEquals("public://fillpdf/{$expected}/{$expected}.pdf", $uri, "Test case $id: The file has the expected URI.");
$node_title = $this->testNode->getTitle();
$expected = "{$current_year}--{$node_title}";
break;
case 'user':
$account_name = $this->adminUser->getAccountName();
$expected = "{$current_year}-{$account_name}-";
break;
default:
$expected = "{$current_year}--";
break;
}
$this->assertEquals("{$expected}.pdf", $filename, "Populated with a $type, the file has the filename $filename.");
$this->assertEquals("public://fillpdf/{$expected}/{$expected}.pdf", $uri, "Populated with a $type, the file has the expected URI.");
// Check if file is permanent and has the right format. // Check if file is permanent and has the right format.
$this->assertFileIsPermanent($file); $this->assertFileIsPermanent($file);
$this->drupalGet(file_create_url($uri)); $this->drupalGet(file_create_url($uri));
$maybe_pdf = $this->getSession()->getPage()->getContent(); $maybe_pdf = $this->getSession()->getPage()->getContent();
$finfo = new \finfo(FILEINFO_MIME_TYPE); $finfo = new \finfo(FILEINFO_MIME_TYPE);
static::assertEquals('application/pdf', $finfo->buffer($maybe_pdf), 'The file has the correct application/pdf MIME type.'); static::assertEquals('application/pdf', $finfo->buffer($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();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment