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

Issue #3044868 by Pancho: Don't save unvalidated backend configuration if another backend is chosen

parent 63f2c1dc
No related branches found
No related tags found
No related merge requests found
...@@ -286,15 +286,29 @@ class FillPdfSettingsForm extends ConfigFormBase { ...@@ -286,15 +286,29 @@ class FillPdfSettingsForm extends ConfigFormBase {
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
// Save form values. // Save form values.
$this->config('fillpdf.settings') $values = $form_state->getValues();
->set('backend', $form_state->getValue('backend')) $config = $this->config('fillpdf.settings');
->set('remote_endpoint', $form_state->getValue('remote_endpoint'))
->set('fillpdf_service_api_key', $form_state->getValue('fillpdf_service_api_key')) $config->set('scheme', $values['scheme'])
->set('remote_protocol', $form_state->getValue('remote_protocol')) ->set('backend', $values['backend']);
->set('pdftk_path', $form_state->getValue('pdftk_path'))
->set('scheme', $form_state->getValue('scheme')) switch ($values['backend']) {
->set('local_service_endpoint', $form_state->getValue('local_service_endpoint')) case 'fillpdf_service':
->save(); $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); parent::submitForm($form, $form_state);
} }
......
...@@ -120,6 +120,30 @@ class FillPdfSettingsFormTest extends BrowserTestBase { ...@@ -120,6 +120,30 @@ class FillPdfSettingsFormTest extends BrowserTestBase {
$this->assertEqual($this->config('fillpdf.settings')->get('scheme'), 'private'); $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. * Tests the backend settings with the 'pdftk' backend.
*/ */
...@@ -155,12 +179,14 @@ class FillPdfSettingsFormTest extends BrowserTestBase { ...@@ -155,12 +179,14 @@ class FillPdfSettingsFormTest extends BrowserTestBase {
$this->assertSession()->pageTextContainsOnce('plugin for testing'); $this->assertSession()->pageTextContainsOnce('plugin for testing');
$this->assertSession()->pageTextContains('Form-altered pass-through plugin for testing'); $this->assertSession()->pageTextContains('Form-altered pass-through plugin for testing');
// Try configuring FillPDF with the 'test' backend, yet an invalid value // Try configuring FillPDF with the 'test' backend, yet with invalid values
// for the form-altered configuration setting. // for the form-altered 'example_setting' and the unrelated
// 'fillpdf_service_api_key'.
$edit = [ $edit = [
'scheme' => 'private', 'scheme' => 'private',
'backend' => 'test', 'backend' => 'test',
'example_setting' => 'x', 'example_setting' => 'x',
'fillpdf_service_api_key' => 'Invalid, just playing around.',
]; ];
$this->drupalPostForm(NULL, $edit, 'Save configuration'); $this->drupalPostForm(NULL, $edit, 'Save configuration');
...@@ -182,10 +208,12 @@ class FillPdfSettingsFormTest extends BrowserTestBase { ...@@ -182,10 +208,12 @@ class FillPdfSettingsFormTest extends BrowserTestBase {
// This time, our custom validation handler passes. // This time, our custom validation handler passes.
$this->assertSession()->pageTextNotContains('Not a valid value.'); $this->assertSession()->pageTextNotContains('Not a valid value.');
$this->assertSession()->pageTextContains('The configuration options have been saved.'); $this->assertSession()->pageTextContains('The configuration options have been saved.');
// So the new values should be submitted *and* saved this time. // So the new values should be submitted *and* saved this time, except for
foreach ($edit as $field => $value) { // the unrelated 'fillpdf_service_api_key' which should be dismissed.
$this->assertEqual($this->config('fillpdf.settings')->get($field), $value); $expected = ['fillpdf_service_api_key' => NULL] + $edit;
foreach ($expected as $field => $value) {
$this->assertSession()->fieldValueEquals($field, $value); $this->assertSession()->fieldValueEquals($field, $value);
$this->assertEqual($this->config('fillpdf.settings')->get($field), $value);
} }
} }
......
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