View source
<?php
namespace Drupal\Tests\webform_entity_print\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\Role;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformEntityPrintFunctionalTest extends WebformEntityPrintFunctionalTestBase {
public static $modules = [
'webform_entity_print_test',
];
public function testEntityPrint() {
global $base_path;
$this
->drupalLogin($this->rootUser);
$webform = Webform::load('test_entity_print');
$sid = $this
->postSubmissionTest($webform);
$submission = WebformSubmission::load($sid);
$this
->drupalGet("/admin/structure/webform/manage/test_entity_print/submission/{$sid}");
$this
->assertRaw('<div class="webform-entity-print-links"><a href="' . $base_path . 'print/pdf/webform_submission/' . $sid . '?view_mode=html" class="button webform-entity-print-link webform-entity-print-link-pdf">Download PDF</a></div>');
$this
->drupalGet("/admin/structure/webform/manage/test_entity_print/submission/{$sid}/table");
$this
->assertRaw('<div class="webform-entity-print-links"><a href="' . $base_path . 'print/pdf/webform_submission/' . $sid . '?view_mode=table" class="button webform-entity-print-link webform-entity-print-link-pdf">Download PDF</a></div>');
$this
->drupalGet("/print/pdf/webform_submission/{$sid}/debug", [
'query' => [
'view_mode' => 'html',
],
]);
$this
->assertRaw('<div class="webform-entity-print-header"><h1>' . Html::escape($submission
->label()) . '</h1></div>');
$this
->assertRaw('<label>textfield</label>');
$this
->assertRaw('<style type="text/css" media="all">
/** custom webform css **/
/* Remove page margins and padding and rely on the PDF generator\'s default margins. */
body {
margin: 0;
}
.page {
padding: 0;
}
</style>');
$this
->drupalGet("/print/pdf/webform_submission/{$sid}/debug", [
'query' => [
'view_mode' => 'table',
],
]);
$this
->assertRaw('<div class="webform-entity-print-header"><h1>' . Html::escape($submission
->label()) . '</h1></div>');
$this
->assertNoRaw('<label>textfield</label>');
$this
->assertRaw('<th>textfield</th>');
$this
->assertRaw('<table class="webform-submission-table" data-striping="1">');
$this
->drupalLogout();
$role_object = Role::load(AccountInterface::ANONYMOUS_ROLE);
$role_object
->grantPermission('entity print access type webform_submission');
$role_object
->save();
$token = $submission
->getToken();
$this
->drupalGet("/webform/test_entity_print/submissions/{$sid}", [
'query' => [
'token' => $token,
],
]);
$this
->assertLinkByHref("{$base_path}print/pdf/webform_submission/{$sid}?view_mode=html&token={$token}");
$this
->drupalLogin($this->rootUser);
$edit = [
'third_party_settings[webform_entity_print][export_types][pdf][link_text]' => 'Generate PDF',
];
$this
->drupalPostForm('/admin/structure/webform/config', $edit, 'Save configuration');
$this
->drupalGet("/admin/structure/webform/manage/test_entity_print/submission/{$sid}");
$this
->assertNoLink('Download PDF');
$this
->assertLink('Generate PDF');
$edit = [
'third_party_settings[webform_entity_print][export_types][pdf][enabled]' => FALSE,
];
$this
->drupalPostForm('/admin/structure/webform/config', $edit, 'Save configuration');
$this
->drupalGet("/admin/structure/webform/manage/test_entity_print/submission/{$sid}");
$this
->assertNoLink('Download PDF');
$submission_exporter = \Drupal::service('webform_submission.exporter');
$submission_exporter
->setWebform($webform);
$submission_exporter
->setExporter();
$edit = [
'exporter' => 'webform_entity_print:pdf',
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_entity_print/results/download', $edit, 'Download');
$files = $this
->getArchiveContents($submission_exporter
->getArchiveFilePath());
$this
->assertEquals([
"submission-{$sid}.pdf" => "submission-{$sid}.pdf",
], $files);
$webform = Webform::load('test_entity_print_custom');
$sid = $this
->postSubmissionTest($webform);
$this
->drupalGet("/admin/structure/webform/manage/test_entity_print_custom/submission/{$sid}");
$this
->assertRaw('<div class="webform-entity-print-links"><a href="' . $base_path . 'print/pdf/webform_submission/' . $sid . '?view_mode=html" style="color: red" class="custom-class webform-entity-print-link webform-entity-print-link-pdf">{custom link text}</a></div>');
$this
->drupalGet("/print/pdf/webform_submission/{$sid}/debug", [
'query' => [
'view_mode' => 'html',
],
]);
$this
->assertRaw('<div class="webform-entity-print-header"><div>{custom header}</div></div>');
$this
->assertRaw('<div class="webform-entity-print-footer"><div>{custom footer}</div></div>');
$this
->assertRaw('<style type="text/css" media="all">
/** custom webform css **/
/* Remove page margins and padding and rely on the PDF generator\'s default margins. */
body {
margin: 0;
}
.page {
padding: 0;
}
/** custom webform print css **/
</style>');
}
protected function getArchiveContents($filepath) {
if (strpos($filepath, '.zip') !== FALSE) {
$archive = new \ZipArchive();
$archive
->open($filepath);
$files = [];
for ($i = 0; $i < $archive->numFiles; $i++) {
$files[] = $archive
->getNameIndex($i);
}
}
else {
$archive = new \Archive_Tar($filepath, 'gz');
$files = [];
foreach ($archive
->listContent() as $file_data) {
$files[] = $file_data['filename'];
}
}
return array_combine($files, $files);
}
}