View source
<?php
namespace Drupal\Tests\fillpdf\Functional;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class HandlePdfControllerTest extends FillPdfUploadTestBase {
protected function setUp() {
parent::setUp();
$this->testNodes[1] = $this
->createNode([
'title' => 'Hello',
'type' => 'article',
]);
$this->testNodes[2] = $this
->createNode([
'title' => 'Goodbye',
'type' => 'article',
]);
}
public function testDownloadAction() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$form_id = $this
->getLatestFillPdfForm();
$fid_before = $this
->getLastFileId();
$fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [
'fid' => $form_id,
'sample' => 1,
],
]);
$this
->drupalGet($fillpdf_route);
$fid_after = $this
->getLastFileId();
$this
->assertEquals($fid_before, $fid_after);
$this
->assertSession()
->statusCodeEquals(200);
$maybe_pdf = $this
->getSession()
->getPage()
->getContent();
static::assertEquals('application/pdf', $this
->getMimeType($maybe_pdf), 'The file has the correct MIME type.');
$this
->assertSession()
->responseHeaderContains('Content-Disposition', ResponseHeaderBag::DISPOSITION_ATTACHMENT);
$this
->assertSession()
->responseHeaderContains('Content-Type', 'application/pdf');
$this
->assertSession()
->responseHeaderContains('Content-Length', (string) strlen(file_get_contents($this
->getTestPdfPath('fillpdf_test_v3.pdf'))));
}
public function testSaveAction() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$form_id = $this
->getLatestFillPdfForm();
$edit = [
'scheme' => 'public',
];
$this
->drupalPostForm("admin/structure/fillpdf/{$form_id}", $edit, 'Save');
$fid_before = $this
->getLastFileId();
$fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [
'fid' => $form_id,
'sample' => 1,
],
]);
$this
->drupalGet($fillpdf_route);
$fid_after = $this
->getLastFileId();
$this
->assertEquals($fid_before + 1, $fid_after);
$this
->assertSession()
->statusCodeEquals(200);
$maybe_pdf = $this
->getSession()
->getPage()
->getContent();
static::assertNotEquals('application/pdf', $this
->getMimeType($maybe_pdf), "The file has the correct MIME type.");
}
public function testRedirectAction() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$form_id = $this
->getLatestFillPdfForm();
$edit = [
'scheme' => 'public',
'destination_redirect[value]' => TRUE,
];
$this
->drupalPostForm("admin/structure/fillpdf/{$form_id}", $edit, 'Save');
$fid_before = $this
->getLastFileId();
$fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [
'fid' => $form_id,
'sample' => 1,
],
]);
$this
->drupalGet($fillpdf_route);
$fid_after = $this
->getLastFileId();
$this
->assertEquals($fid_before + 1, $fid_after);
$this
->assertSession()
->statusCodeEquals(200);
$maybe_pdf = $this
->getSession()
->getPage()
->getContent();
static::assertEquals('application/pdf', $this
->getMimeType($maybe_pdf), "The file has the correct MIME type.");
}
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 = [];
$testcases[1]['entities'] = [];
$testcases[1]['expected'] = "{$year}--";
$testcases[1]['entities'] = [
"node:{$node1_id}",
];
$testcases[1]['expected'] = "{$year}--{$node1_title}";
$testcases[2]['entities'] = [
"node:{$node1_id}",
"node:{$node2_id}",
];
$testcases[2]['expected'] = "{$year}--{$node2_title}";
$testcases[3]['entities'] = [
"node:{$node1_id}",
"node:{$node1_id}",
];
$testcases[3]['expected'] = "{$year}--{$node1_title}";
$testcases[4]['entities'] = [
"user:{$user_id}",
];
$testcases[4]['expected'] = "{$year}-{$user_name}-";
$testcases[5]['entities'] = [
"node:{$node1_id}",
"user:{$user_id}",
];
$testcases[5]['expected'] = "{$year}-{$user_name}-{$node1_title}";
$testcases[6]['entities'] = [
"node:123",
];
$testcases[6]['expected'] = "{$year}--";
$testcases[7]['entities'] = [
"node:{$node1_id}",
"user:456",
];
$testcases[7]['expected'] = "{$year}--{$node1_title}";
foreach ($testcases as $id => $case) {
$entities = $case['entities'];
$fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [
'fid' => $form_id,
'entity_ids' => $entities,
],
]);
$this
->drupalGet($fillpdf_route);
$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.");
$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.");
$file
->delete();
}
}
public function testStorageSchemeDisallowed() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$form_id = $this
->getLatestFillPdfForm();
$previous_file_id = $this
->getLastFileId();
$edit = [
'admin_title[0][value]' => 'Scheme test',
'scheme' => 'public',
'destination_path[0][value]' => 'test',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [
'fid' => $form_id,
],
]);
$this
->drupalGet($fillpdf_route);
$this
->assertSession()
->addressNotEquals('/fillpdf');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextNotContains('File storage scheme public:// is unavailable');
$this
->assertEquals(++$previous_file_id, $this
->getLastFileId(), 'Generated file was stored.');
$this
->assertStringStartsWith('public://', File::load($this
->getLastFileId())
->getFileUri());
$this
->configureFillPdf([
'allowed_schemes' => [
'private',
],
]);
$this
->drupalGet($fillpdf_route);
$this
->assertSession()
->addressEquals('/fillpdf');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains("File storage scheme public:// is unavailable, so a PDF file generated from FillPDF form Scheme test ({$form_id}) could not be stored.");
$this
->assertEquals($previous_file_id, $this
->getLastFileId(), 'Generated file was not stored.');
}
public function testStorageSchemeUnavailable() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$form_id = $this
->getLatestFillPdfForm();
$previous_file_id = $this
->getLastFileId();
$edit = [
'admin_title[0][value]' => 'Scheme test',
'scheme' => 'private',
'destination_path[0][value]' => 'test',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [
'fid' => $form_id,
],
]);
$this
->drupalGet($fillpdf_route);
$this
->assertSession()
->addressNotEquals('/fillpdf');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextNotContains('File storage scheme private:// is unavailable');
$this
->assertEquals(++$previous_file_id, $this
->getLastFileId(), 'Generated file was stored.');
$this
->assertStringStartsWith('private://', File::load($this
->getLastFileId())
->getFileUri());
$this
->writeSettings([
'settings' => [
'file_private_path' => (object) [
'value' => '',
'required' => TRUE,
],
],
]);
$this
->rebuildContainer();
$this
->drupalGet($fillpdf_route);
$this
->assertSession()
->addressEquals('/fillpdf');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains("File storage scheme private:// is unavailable, so a PDF file generated from FillPDF form Scheme test ({$form_id}) could not be stored.");
$this
->assertEquals($previous_file_id, $this
->getLastFileId(), 'Generated file was not stored.');
}
}