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

Issue #3039489 by Pancho: Fix TypeError when no PDF file is selected for upload

parent 0ae48fcc
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\fillpdf\FillPdfBackendManager;
......@@ -125,7 +126,7 @@ class FillPdfOverviewForm extends FillPdfAdminFormBase {
public function validateForm(array &$form, FormStateInterface $form_state) {
$files = $this->getRequest()->files->get('files');
$file_upload = array_key_exists('upload_pdf', $files) ? $files['upload_pdf'] : NULL;
$file_upload = !empty($files) && array_key_exists('upload_pdf', $files) ? $files['upload_pdf'] : NULL;
if ($file_upload) {
$this->validatePdfUpload($form, $form_state, $file_upload);
}
......@@ -142,17 +143,24 @@ class FillPdfOverviewForm extends FillPdfAdminFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\file\FileInterface $file */
$file = $form_state->getValue('upload_pdf');
$added = $this->inputHelper->attachPdfToForm($file);
if ($file) {
$added = $this->inputHelper->attachPdfToForm($file);
$fillpdf_form = $added['form'];
$form_fields = $added['fields'];
$fillpdf_form = $added['form'];
$fid = $fillpdf_form->id();
if (count($form_fields) === 0) {
drupal_set_message($this->t('No fields detected in PDF. Are you sure it contains editable fields?'), 'warning');
}
$this->logger('fillpdf')->notice('Added FillPDF form %id.', ['%id' => $fid]);
$this->messenger()->addStatus($this->t('New FillPDF form has been created.'));
$form_fields = $added['fields'];
if (count($form_fields) === 0) {
$this->messenger()->addWarning($this->t('No fields detected in PDF. Are you sure it contains editable fields?'));
} else {
$this->messenger()->addStatus($this->t("You may now create mappings between the fields of the PDF form and an entity type."));
}
$fid = $fillpdf_form->id();
$form_state->setRedirect('entity.fillpdf_form.edit_form', ['fillpdf_form' => $fid]);
$form_state->setRedirect('entity.fillpdf_form.edit_form', ['fillpdf_form' => $fid]);
}
}
}
......@@ -18,8 +18,28 @@ class AdminIdTest extends BrowserTestBase {
static public $modules = ['fillpdf_test'];
protected $profile = 'minimal';
protected function setUp() {
parent::setUp();
$this->configureBackend();
$this->initializeUser();
}
/**
* Tests the overview form's PDF file upload functionality.
*/
public function testOverviewFormUpload() {
// Without a PDF file being supplied, no FillPdf form should be created.
$this->uploadTestPdf(NULL);
$this->assertSession()->pageTextNotContains('New FillPDF form has been created.');
// With a PDF file being supplied, a new FillPdf form should be created.
$this->uploadTestPdf('fillpdf_test_v3.pdf');
$this->assertSession()->pageTextContains('New FillPDF form has been created.');
}
public function testEditLink() {
$this->uploadTestPdf();
$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;
......@@ -27,11 +47,4 @@ class AdminIdTest extends BrowserTestBase {
$this->assertSession()->statusCodeEquals(200);
}
protected function setUp() {
parent::setUp();
$this->configureBackend();
$this->initializeUser();
}
}
......@@ -53,7 +53,8 @@ class PdfPopulationTest extends FillPdfTestBase {
public function testPdfPopulation() {
// Test with a node.
$this->uploadTestPdf();
$this->uploadTestPdf('fillpdf_test_v3.pdf');
$this->assertSession()->pageTextContains('New FillPDF form has been created.');
$fillpdf_form = FillPdfForm::load($this->getLatestFillPdfForm());
// Get the field definitions for the form that was created and configure
......@@ -104,7 +105,7 @@ class PdfPopulationTest extends FillPdfTestBase {
);
// Test with a Webform.
$this->uploadTestPdf();
$this->uploadTestPdf('fillpdf_test_v3.pdf');
$fillpdf_form2 = FillPdfForm::load($this->getLatestFillPdfForm());
// Create a test submission for our Contact form.
......@@ -198,7 +199,7 @@ class PdfPopulationTest extends FillPdfTestBase {
$backend_service = $this->backendServiceManager->createInstance('test');
// Test the parse method.
$original_pdf = file_get_contents($this->getTestPdfPath());
$original_pdf = file_get_contents($this->getTestPdfPath('fillpdf_test_v3.pdf'));
$parsed_fields = $backend_service->parse($original_pdf);
$actual_keys = [];
foreach ($parsed_fields as $parsed_field) {
......@@ -332,7 +333,7 @@ class PdfPopulationTest extends FillPdfTestBase {
protected function backendTest() {
// If we can upload a PDF, parsing is working.
// Test with a node.
$this->uploadTestPdf();
$this->uploadTestPdf('fillpdf_test_v3.pdf');
$fillpdf_form = FillPdfForm::load($this->getLatestFillPdfForm());
// Get the field definitions for the form that was created and configure
......
......@@ -48,11 +48,21 @@ trait TestFillPdfTrait {
$this->drupalLogin($account);
}
protected function uploadTestPdf() {
// Upload a test file.
$edit = array(
'files[upload_pdf]' => $this->getTestPdfPath(),
);
/**
* Uploads a specified PDF testfile, if given.
*
* @param string|null $filename
* (optional) Filename of the PDF testfile. Defaults to NULL.
*/
protected function uploadTestPdf($filename = NULL) {
if ($filename) {
$path = $this->getTestPdfPath($filename);
$this->assertNotFalse($path);
};
$edit = [
'files[upload_pdf]' => isset($path) ? $path : NULL,
];
$this->drupalPostForm('admin/structure/fillpdf', $edit, 'Upload');
$this->assertSession()->statusCodeEquals(200);
}
......@@ -73,13 +83,16 @@ trait TestFillPdfTrait {
}
/**
* @param $file_system
* @return mixed
* @param string $filename
* Filename of the PDF testfile.
*
* @return string|false
* The absolute locale filepath or FALSE on failure.
*/
protected function getTestPdfPath() {
protected function getTestPdfPath($filename) {
/** @var \Drupal\Core\File\FileSystem $file_system */
$file_system = $this->container->get('file_system');
return $file_system->realpath(drupal_get_path('module', 'fillpdf') . '/tests/modules/fillpdf_test/files/fillpdf_test_v3.pdf');
return $file_system->realpath(drupal_get_path('module', 'fillpdf') . '/tests/modules/fillpdf_test/files/' . $filename);
}
/**
......
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