diff --git a/src/Form/FillPdfSettingsForm.php b/src/Form/FillPdfSettingsForm.php index 5612889ac3c4c0634784733c6ca87483c9b0fe36..6b8757f4e06912ce61b08270107d1e623ebcff6f 100644 --- a/src/Form/FillPdfSettingsForm.php +++ b/src/Form/FillPdfSettingsForm.php @@ -286,15 +286,29 @@ class FillPdfSettingsForm extends ConfigFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { // Save form values. - $this->config('fillpdf.settings') - ->set('backend', $form_state->getValue('backend')) - ->set('remote_endpoint', $form_state->getValue('remote_endpoint')) - ->set('fillpdf_service_api_key', $form_state->getValue('fillpdf_service_api_key')) - ->set('remote_protocol', $form_state->getValue('remote_protocol')) - ->set('pdftk_path', $form_state->getValue('pdftk_path')) - ->set('scheme', $form_state->getValue('scheme')) - ->set('local_service_endpoint', $form_state->getValue('local_service_endpoint')) - ->save(); + $values = $form_state->getValues(); + $config = $this->config('fillpdf.settings'); + + $config->set('scheme', $values['scheme']) + ->set('backend', $values['backend']); + + switch ($values['backend']) { + case 'fillpdf_service': + $config->set('remote_endpoint', $values['remote_endpoint']) + ->set('fillpdf_service_api_key', $values['fillpdf_service_api_key']) + ->set('remote_protocol', $values['remote_protocol']); + break; + + case 'local_service': + $config->set('local_service_endpoint', $values['local_service_endpoint']); + break; + + case 'pdftk': + $config->set('pdftk_path', $form_state->getValue('pdftk_path')); + break; + } + + $config->save(); parent::submitForm($form, $form_state); } diff --git a/tests/src/Functional/FillPdfSettingsFormTest.php b/tests/src/Functional/FillPdfSettingsFormTest.php index 4026d3dd27d4ef24023a7abb8fdc66b09f8f4b72..2e238146b4e3d9b8e391ea5690c28528bd933547 100644 --- a/tests/src/Functional/FillPdfSettingsFormTest.php +++ b/tests/src/Functional/FillPdfSettingsFormTest.php @@ -120,6 +120,30 @@ class FillPdfSettingsFormTest extends BrowserTestBase { $this->assertEqual($this->config('fillpdf.settings')->get('scheme'), 'private'); } + /** + * Tests the backend settings with the 'fillpdf_service' backend. + */ + public function testSettingsFormBackendFillPdfService() { + // FillPDF is not yet configured. The settings form is however initialized + // with the 'fillpdf_service' backend. Save that configuration. + $this->drupalPostForm(Url::fromRoute('fillpdf.settings'), NULL, 'Save configuration'); + + // There's currently no validation, so the 'backend' setting should be + // both submitted and stored. + $this->assertSession()->pageTextContains('The configuration options have been saved.'); + $this->assertSession()->fieldValueEquals('backend', 'fillpdf_service'); + $this->assertEqual($this->config('fillpdf.settings')->get('backend'), 'fillpdf_service'); + + // Now add an API key and save again. + $this->drupalPostForm(NULL, ['fillpdf_service_api_key' => 'Invalid, just playing around.'], 'Save configuration'); + + // There's currently no validation, so the obviously invalid + // 'fillpdf_service_api_key' should be both submitted and stored. + $this->assertSession()->pageTextContains('The configuration options have been saved.'); + $this->assertSession()->fieldValueEquals('fillpdf_service_api_key', 'Invalid, just playing around.'); + $this->assertEqual($this->config('fillpdf.settings')->get('fillpdf_service_api_key'), 'Invalid, just playing around.'); + } + /** * Tests the backend settings with the 'pdftk' backend. */ @@ -155,12 +179,14 @@ class FillPdfSettingsFormTest extends BrowserTestBase { $this->assertSession()->pageTextContainsOnce('plugin for testing'); $this->assertSession()->pageTextContains('Form-altered pass-through plugin for testing'); - // Try configuring FillPDF with the 'test' backend, yet an invalid value - // for the form-altered configuration setting. + // Try configuring FillPDF with the 'test' backend, yet with invalid values + // for the form-altered 'example_setting' and the unrelated + // 'fillpdf_service_api_key'. $edit = [ 'scheme' => 'private', 'backend' => 'test', 'example_setting' => 'x', + 'fillpdf_service_api_key' => 'Invalid, just playing around.', ]; $this->drupalPostForm(NULL, $edit, 'Save configuration'); @@ -182,10 +208,12 @@ class FillPdfSettingsFormTest extends BrowserTestBase { // This time, our custom validation handler passes. $this->assertSession()->pageTextNotContains('Not a valid value.'); $this->assertSession()->pageTextContains('The configuration options have been saved.'); - // So the new values should be submitted *and* saved this time. - foreach ($edit as $field => $value) { - $this->assertEqual($this->config('fillpdf.settings')->get($field), $value); + // So the new values should be submitted *and* saved this time, except for + // the unrelated 'fillpdf_service_api_key' which should be dismissed. + $expected = ['fillpdf_service_api_key' => NULL] + $edit; + foreach ($expected as $field => $value) { $this->assertSession()->fieldValueEquals($field, $value); + $this->assertEqual($this->config('fillpdf.settings')->get($field), $value); } }