View source
<?php
namespace Drupal\Tests\fillpdf\Functional;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\fillpdf\Entity\FillPdfForm;
use Drupal\fillpdf\TokenResolver;
use Drupal\user\Entity\Role;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\WebformInterface;
class PdfWebformPopulationTest extends FillPdfTestBase {
public static $modules = [
'webform',
'fillpdf_webform_test',
];
protected $testSubmission;
protected function setUp() {
parent::setUp();
$existing_user_roles = $this->adminUser
->getRoles(TRUE);
$role_to_modify = Role::load(end($existing_user_roles));
$this
->grantPermissions($role_to_modify, [
'administer webform',
'access webform submission log',
'create webform',
]);
$contact_form = Webform::load('fillpdf_contact');
$contact_form_test_route = Url::fromRoute('entity.webform.test_form', [
'webform' => $contact_form
->id(),
]);
$this
->drupalPostForm($contact_form_test_route, [], t('Send message'));
$this->testSubmission = WebformSubmission::load($this
->getLastSubmissionId($contact_form));
}
public function testPdfPopulation() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$this
->assertSession()
->pageTextContains('New FillPDF form has been created.');
$fillpdf_form = FillPdfForm::load($this
->getLatestFillPdfForm());
$this
->mapFillPdfFieldsToWebformFields($fillpdf_form
->getFormFields());
$fillpdf_route = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [
'fid' => $fillpdf_form
->id(),
'entity_id' => "webform_submission:{$this->testSubmission->id()}",
],
]);
$this
->drupalGet($fillpdf_route);
$populate_result = $this->container
->get('state')
->get('fillpdf_test.last_populated_metadata');
$submission_values = $this->testSubmission
->getData();
self::assertEquals($populate_result['field_mapping']['fields']['TextField1'], $this->testSubmission
->getWebform()
->label(), 'PDF is populated with the title of the Webform Submission.');
$submission_file = File::load($submission_values['image'][0]);
self::assertEquals($populate_result['field_mapping']['images']['ImageField']['data'], base64_encode(file_get_contents($submission_file
->getFileUri())), 'Encoded image matches known image.');
$path_info = pathinfo($submission_file
->getFileUri());
$expected_file_hash = md5($path_info['filename']) . '.' . $path_info['extension'];
self::assertEquals($populate_result['field_mapping']['images']['ImageField']['filenamehash'], $expected_file_hash, 'Hashed filename matches known hash.');
self::assertEquals($populate_result['field_mapping']['fields']['ImageField'], "{image}{$submission_file->getFileUri()}", 'URI in metadata matches expected URI.');
self::assertEquals('{image}webform_signature.png', $populate_result['field_mapping']['fields']['TestButton'], 'Signature matches signature from Webform.');
$signature_image = TokenResolver::getSignatureImage($submission_values['test_signature']);
self::assertEquals(base64_encode($signature_image), $populate_result['field_mapping']['images']['TestButton']['data'], 'Signature matches signature from Webform.');
}
protected function getLastSubmissionId(WebformInterface $webform) {
$url = UrlHelper::parse($this
->getUrl());
if (isset($url['query']['sid'])) {
return $url['query']['sid'];
}
$entity_ids = $this->container
->get('entity_type.manager')
->getStorage('webform_submission')
->getQuery()
->sort('sid', 'DESC')
->condition('webform_id', $webform
->id())
->execute();
return reset($entity_ids);
}
protected function mapFillPdfFieldsToWebformFields(array $fields) {
foreach ($fields as $pdf_key => $field) {
switch ($pdf_key) {
case 'ImageField':
$field->value = '[webform_submission:values:image]';
break;
case 'TextField1':
$field->value = '[webform_submission:webform:title]';
break;
case 'TestButton':
$field->value = '[webform_submission:values:test_signature]';
break;
}
$field
->save();
}
}
}