From 5e031e2e7490623a4ec105a14e5ba3fb13c2fdd9 Mon Sep 17 00:00:00 2001 From: Bernd Oliver Suenderhauf <bos@suenderhauf.de> Date: Sat, 6 Apr 2019 15:56:37 +0200 Subject: [PATCH] Issue #3045717 by Pancho: Fix validation and add tests for subdirectory creation on FillPdfSettingsForm --- src/Form/FillPdfSettingsForm.php | 2 +- .../Functional/FillPdfSettingsFormTest.php | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Form/FillPdfSettingsForm.php b/src/Form/FillPdfSettingsForm.php index 6b8757f..79acbe3 100644 --- a/src/Form/FillPdfSettingsForm.php +++ b/src/Form/FillPdfSettingsForm.php @@ -277,7 +277,7 @@ class FillPdfSettingsForm extends ConfigFormBase { $directory = FillPdf::buildFileUri($form_state->getValue('scheme'), 'fillpdf'); if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY + FILE_MODIFY_PERMISSIONS)) { - $form_state->setErrorByName('upload_pdf', $this->t('Could not automatically create the <em>fillpdf</em> subdirectory. Please create this manually before uploading your PDF form.')); + $form_state->setErrorByName('scheme', $this->t('Could not automatically create the <em>fillpdf</em> subdirectory. Please create this manually before uploading your PDF form.')); } } diff --git a/tests/src/Functional/FillPdfSettingsFormTest.php b/tests/src/Functional/FillPdfSettingsFormTest.php index 2e23814..30c9500 100644 --- a/tests/src/Functional/FillPdfSettingsFormTest.php +++ b/tests/src/Functional/FillPdfSettingsFormTest.php @@ -120,6 +120,42 @@ class FillPdfSettingsFormTest extends BrowserTestBase { $this->assertEqual($this->config('fillpdf.settings')->get('scheme'), 'private'); } + /** + * Tests the scheme settings with the 'public' stream wrapper. + */ + public function testSettingsFormSchemePublic() { + // FillPDF is not yet configured. + // Configure FillPDF with the 'test' backend and the site default scheme, + // which at this point is 'public'. + $this->configureBackend(); + + // On the settings page, verify the 'public' scheme is set both in the form + // and in config. + $this->drupalGet(Url::fromRoute('fillpdf.settings')); + $this->assertSession()->fieldValueEquals('scheme', 'public'); + $this->assertEqual($this->config('fillpdf.settings')->get('scheme'), 'public'); + + // Verify the subdirectory doesn't exist yet. + $directory = 'public://fillpdf'; + $this->assertFalse(is_dir($directory), 'Directory does not exist prior to testing.'); + + // Now on the settings form, save the unchanged configuration to create the + // subdirectory. Verify it does exist now and is writeable. + $this->drupalPostForm(NULL, [], 'Save configuration'); + $this->assertSession()->pageTextContains('The configuration options have been saved.'); + $this->assertTrue(is_dir($directory), 'Directory exists now.'); + $this->assertTrue(file_prepare_directory($directory), 'Directory is writeable.'); + + // Now delete the directory and replace it with a file with the same name, + // so the directory can't be recreated. Try saving again and verify an error + // is set. + drupal_rmdir($directory); + file_unmanaged_copy('public://.htaccess', $directory); + $this->drupalPostForm(NULL, [], 'Save configuration'); + $this->assertSession()->pageTextNotContains('The configuration options have been saved.'); + $this->assertSession()->pageTextContains('Could not automatically create the fillpdf subdirectory.'); + } + /** * Tests the backend settings with the 'fillpdf_service' backend. */ -- GitLab