View source
<?php
namespace Drupal\Tests\fillpdf\Functional;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\fillpdf\Entity\FillPdfForm;
use Drupal\user\Entity\Role;
class FillPdfFormFormTest extends FillPdfUploadTestBase {
protected function setUp() {
parent::setUp();
$this->testNode = $this
->createNode([
'title' => 'Hello',
'type' => 'article',
]);
}
public function testDefaultEntityId() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$this
->assertSession()
->hiddenFieldExists('default_entity_id');
for ($i = 0; $i < 25; $i++) {
$this
->createUser();
}
$testcases = [
'fillpdf_file_context' => [
0,
0,
'',
],
'node' => [
1,
$this->testNode
->id(),
$this->testNode
->label(),
],
'user' => [
26,
$this->adminUser
->id(),
$this->adminUser
->label(),
],
];
foreach ($testcases as $type => list($count, $id, $label)) {
$this
->drupalPostForm(NULL, [
'default_entity_type' => $type,
], self::OP_SAVE);
$this
->assertSession()
->pageTextContains("FillPDF Form has been updated.");
$this
->assertSession()
->fieldValueEquals('default_entity_type', $type);
$this
->assertSession()
->fieldValueEquals('default_entity_id', NULL);
if ($count == 0) {
$options = $this
->assertSession()
->selectExists('default_entity_id')
->findAll('xpath', 'option');
$this
->assertCount(1, $options);
$this
->assertEquals('', $options[0]
->getValue());
$this
->assertEquals('- None -', $options[0]
->getText());
continue;
}
elseif ($count <= 25) {
$this
->assertSession()
->pageTextContains("Choose a {$type} to test populating the PDF template.");
$this
->assertSession()
->optionExists('default_entity_id', $id);
$this
->drupalPostForm(NULL, [
'default_entity_id' => $id,
], self::OP_SAVE);
$expected_value = $id;
}
else {
$this
->assertSession()
->pageTextContains("Enter the title of a {$type} to test populating the PDF template.");
$this
->drupalPostForm(NULL, [
'default_entity_id' => $label,
], self::OP_SAVE);
$expected_value = "{$label} ({$id})";
}
$this
->assertSession()
->pageTextContains("FillPDF Form has been updated.");
$this
->assertSession()
->fieldValueEquals('default_entity_type', $type);
$this
->assertSession()
->fieldValueEquals('default_entity_id', $expected_value);
$this
->assertSession()
->linkExistsExact("Download this PDF template populated with data from the {$type} {$label} ({$id}).");
}
}
public function testFormFormUpload() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$latest_fid = $this
->getLatestFillPdfForm();
$latest_fillpdf_form = FillPdfForm::load($latest_fid);
$max_fid_after = $latest_fillpdf_form->fid->value;
$this
->drupalGet('admin/structure/fillpdf/' . $max_fid_after);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementAttributeContains('css', 'input#edit-upload-pdf-upload', 'accept', 'application/pdf');
$this
->assertNotUploadTextFile(self::OP_UPLOAD);
$this
->assertNotUploadTextFile(self::OP_SAVE);
$this
->assertUploadPdfFile(self::OP_UPLOAD, TRUE);
$pdf_fields = $latest_fillpdf_form
->getFormFields();
FillPdfTestBase::mapFillPdfFieldsToEntityFields('node', $pdf_fields);
$this
->assertUploadPdfFile(self::OP_SAVE, TRUE, $latest_fillpdf_form);
}
public function testStorageSettings() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$form_id = $this
->getLatestFillPdfForm();
$previous_file_id = $this
->getLastFileId();
$edit_form_url = Url::fromRoute('entity.fillpdf_form.edit_form', [
'fillpdf_form' => $form_id,
]);
$generate_url = Url::fromRoute('fillpdf.populate_pdf', [], [
'query' => [
'fid' => $form_id,
'entity_id' => "node:{$this->testNode->id()}",
],
]);
$this
->assertSession()
->fieldValueEquals('scheme', '_none');
foreach ([
'- None -',
'private://',
'public://',
] as $option) {
$this
->assertSession()
->optionExists('scheme', $option);
}
$this
->assertSession()
->fieldValueEquals('destination_path[0][value]', '');
$this
->drupalGet($edit_form_url);
$this
->drupalGet($generate_url);
$this
->assertEquals($previous_file_id, $this
->getLastFileId(), 'Generated file is not stored.');
$this
->drupalPostForm($edit_form_url, [
'scheme' => 'public',
], self::OP_SAVE);
$this
->assertSession()
->fieldValueEquals('scheme', 'public');
$this
->assertSession()
->pageTextContains('Destination path');
$this
->drupalGet($generate_url);
$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($edit_form_url);
$this
->assertSession()
->fieldValueEquals('scheme', '_none');
foreach ([
'- None -',
'private://',
] as $option) {
$this
->assertSession()
->optionExists('scheme', $option);
}
$this
->assertSession()
->optionNotExists('scheme', 'public://');
$this
->drupalGet($generate_url);
$this
->assertEquals($previous_file_id, $this
->getLastFileId(), 'Generated file is not stored.');
$this
->drupalPostForm($edit_form_url, [
'scheme' => 'private',
], self::OP_SAVE);
$this
->assertSession()
->fieldValueEquals('scheme', 'private');
$this
->drupalGet($generate_url);
$this
->assertEquals(++$previous_file_id, $this
->getLastFileId(), 'Generated file was stored.');
$this
->assertStringStartsWith('private://', File::load($this
->getLastFileId())
->getFileUri());
$this
->configureFillPdf([
'allowed_schemes' => [],
]);
$this
->drupalGet($edit_form_url);
$this
->assertSession()
->pageTextNotContains('Storage and download');
$this
->drupalGet($generate_url);
$this
->assertEquals($previous_file_id, $this
->getLastFileId(), 'Generated file is not stored.');
}
public function testFillPdfFileUsage() {
$this
->uploadTestPdf('fillpdf_test_v3.pdf');
$admin_title = 'Example form';
$this
->drupalPostForm(NULL, [
'admin_title[0][value]' => $admin_title,
], self::OP_SAVE);
$this
->assertSession()
->pageTextContains("FillPDF Form {$admin_title} has been updated.");
$this
->assertSession()
->fieldValueEquals('edit-admin-title-0-value', $admin_title);
$existing_user_roles = $this->loggedInUser
->getRoles(TRUE);
$role_to_modify = Role::load(end($existing_user_roles));
$this
->grantPermissions($role_to_modify, [
'access files overview',
]);
$fillpdf_form = FillPdfForm::load($this
->getLatestFillPdfForm());
$file_id = $fillpdf_form
->get('file')
->first()
->getValue()['target_id'];
$this
->drupalPostForm('admin/content/files', [
'edit-filename' => 'fillpdf_test_v3.pdf',
], 'Filter');
$this
->assertSession()
->elementsCount('css', 'table td.views-field.views-field-filename', 1);
$this
->assertSession()
->pageTextContains('Permanent');
$file_uri = File::load($file_id)
->getFileUri();
$this
->assertSession()
->linkByHrefExists(file_create_url($file_uri));
$this
->drupalGet("admin/content/files/usage/{$file_id}");
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkByHrefExists($fillpdf_form
->toUrl()
->toString());
}
}