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

Issue #3045691 by Pancho: Properly test scheme options on the settings form

parent beda3e8b
No related branches found
No related tags found
No related merge requests found
...@@ -37,8 +37,12 @@ function fillpdf_test_form_fillpdf_settings_alter(array &$form, FormStateInterfa ...@@ -37,8 +37,12 @@ function fillpdf_test_form_fillpdf_settings_alter(array &$form, FormStateInterfa
* Custom validation for fillpdf_test_form_fillpdf_settings_alter(). * Custom validation for fillpdf_test_form_fillpdf_settings_alter().
*/ */
function fillpdf_test_form_fillpdf_settings_validate($form, FormStateInterface &$form_state) { function fillpdf_test_form_fillpdf_settings_validate($form, FormStateInterface &$form_state) {
if ($form_state->getValue('backend') == 'test' && strlen($form_state->getValue('example_setting')) < 2) { if ($form_state->getValue('backend') == 'test') {
$form_state->setErrorByName('example_setting', t('Not a valid value.')); // Only validate if a value is set.
$value = $form_state->getValue('example_setting');
if (!empty($value) && strlen($value) < 2) {
$form_state->setErrorByName('example_setting', t('Not a valid value.'));
}
} }
} }
......
...@@ -33,11 +33,13 @@ class FillPdfSettingsFormTest extends BrowserTestBase { ...@@ -33,11 +33,13 @@ class FillPdfSettingsFormTest extends BrowserTestBase {
} }
/** /**
* Tests the settings form. * Tests the scheme settings with the site default.
*/ */
public function testSettingsForm() { public function testSettingsFormDefaults() {
// FillPDF is not yet configured. Scheme should be initialized with the site // FillPDF is not yet configured.
// default. Backend should be set to 'fillpdf_service'. // Verify the settings form is however initialized with the site default
// scheme, which at this point should be 'public', and with the
// 'fillpdf_service' backend.
$this->drupalGet(Url::fromRoute('fillpdf.settings')); $this->drupalGet(Url::fromRoute('fillpdf.settings'));
$this->assertSession()->pageTextContains('Public files (site default)'); $this->assertSession()->pageTextContains('Public files (site default)');
$this->assertSession()->checkboxChecked('edit-scheme-public'); $this->assertSession()->checkboxChecked('edit-scheme-public');
...@@ -49,21 +51,27 @@ class FillPdfSettingsFormTest extends BrowserTestBase { ...@@ -49,21 +51,27 @@ class FillPdfSettingsFormTest extends BrowserTestBase {
->set('default_scheme', 'private'); ->set('default_scheme', 'private');
$config->save(); $config->save();
// Scheme should now be initialized with the new site default. Backend // The form should now be initialized with the new site default scheme,
// should still be 'fillpdf_service'. // while the backend should remain unchanged.
$this->drupalGet(Url::fromRoute('fillpdf.settings')); $this->drupalGet(Url::fromRoute('fillpdf.settings'));
$this->assertSession()->pageTextContains('Private files (site default)'); $this->assertSession()->pageTextContains('Private files (site default)');
$this->assertSession()->checkboxChecked('edit-scheme-private'); $this->assertSession()->checkboxChecked('edit-scheme-private');
$this->assertSession()->checkboxChecked('edit-backend-fillpdf-service'); $this->assertSession()->checkboxChecked('edit-backend-fillpdf-service');
}
// Select 'dummy_remote://' as new default scheme. /**
$this->configureBackend('dummy_remote'); * Tests the scheme settings with the 'dummy_remote' stream wrapper.
*/
// Verify that the default 'dummy_remote' stream wrapper is present. public function testSettingsFormSchemeDummyRemote() {
// FillPDF is not yet configured.
// Verify the 'dummy_remote' stream wrapper is present on the form.
$this->drupalGet(Url::fromRoute('fillpdf.settings')); $this->drupalGet(Url::fromRoute('fillpdf.settings'));
$this->assertSession()->elementExists('css', '#edit-scheme-dummy-remote'); $this->assertSession()->elementExists('css', '#edit-scheme-dummy-remote');
// Now uninstall the test module with the dummy stream wrappers. // Programmatically configure 'dummy_remote' as new default scheme.
$this->configureBackend('dummy_remote');
// Now uninstall the file_test module with the dummy stream wrappers.
$this->assertTrue(\Drupal::service('module_installer')->uninstall(['file_test']), "Module 'file_test' has been uninstalled."); $this->assertTrue(\Drupal::service('module_installer')->uninstall(['file_test']), "Module 'file_test' has been uninstalled.");
$this->assertFalse(\Drupal::moduleHandler()->moduleExists('file_test'), "Module 'file_test' is no longer present."); $this->assertFalse(\Drupal::moduleHandler()->moduleExists('file_test'), "Module 'file_test' is no longer present.");
...@@ -71,27 +79,45 @@ class FillPdfSettingsFormTest extends BrowserTestBase { ...@@ -71,27 +79,45 @@ class FillPdfSettingsFormTest extends BrowserTestBase {
$this->drupalGet(Url::fromRoute('fillpdf.settings')); $this->drupalGet(Url::fromRoute('fillpdf.settings'));
$this->assertSession()->elementNotExists('css', '#edit-scheme-dummy-remote'); $this->assertSession()->elementNotExists('css', '#edit-scheme-dummy-remote');
$this->assertSession()->pageTextContains('Your previously used file scheme dummy_remote:// is no longer available'); $this->assertSession()->pageTextContains('Your previously used file scheme dummy_remote:// is no longer available');
}
// Select 'private://' as new default scheme. /**
// @todo Once https://www.drupal.org/project/fillpdf/issues/3040901 is in, * Tests the scheme settings with the 'private' stream wrapper.
// this should be set via the UI: */
// $this->drupalPostForm(NULL, ['scheme' => 'private'], 'Save configuration'); public function testSettingsFormSchemePrivate() {
// $this->assertSession()->pageTextNotContains('An illegal choice has been detected.'); // FillPDF is not yet configured.
$this->configureBackend('private'); // Configure FillPDF with the 'test' backend and the site default scheme,
// which at this point is 'public'.
// Now remove the private path and rebuild the container. $this->configureBackend();
$settings['settings']['file_private_path'] = (object) [
'value' => '', // Now on the settings form, switch to the 'private' scheme.
'required' => TRUE, $this->drupalPostForm(Url::fromRoute('fillpdf.settings'), ['scheme' => 'private'], 'Save configuration');
];
$this->writeSettings($settings); // Verify the new values have been submitted *and* successfully saved.
$this->assertSession()->pageTextContains('The configuration options have been saved.');
$this->assertSession()->fieldValueEquals('scheme', 'private');
$this->assertEqual($this->config('fillpdf.settings')->get('scheme'), 'private');
// Now remove the private path from settings.php and rebuild the container.
$this->writeSettings([
'settings' => [
'file_private_path' => (object) [
'value' => '',
'required' => TRUE,
],
],
]);
$this->rebuildContainer(); $this->rebuildContainer();
// Reload the page and and verify that 'private' is gone, too. // Reload the page to verify the 'private' scheme is gone.
$this->drupalGet(Url::fromRoute('fillpdf.settings')); $this->drupalGet(Url::fromRoute('fillpdf.settings'));
$this->assertSession()->elementNotExists('css', '#edit-scheme-private'); $this->assertSession()->elementNotExists('css', '#edit-scheme-private');
$this->assertSession()->pageTextContains('Your previously used file scheme private:// is no longer available'); $this->assertSession()->pageTextContains('Your previously used file scheme private:// is no longer available');
// Verify that the site default scheme, which at this point is 'public', is
// preselected but not yet saved in config.
$this->assertSession()->fieldValueEquals('scheme', \Drupal::config('system.file')->get('default_scheme'));
$this->assertEqual($this->config('fillpdf.settings')->get('scheme'), 'private');
} }
/** /**
......
...@@ -10,19 +10,25 @@ namespace Drupal\Tests\fillpdf\Traits; ...@@ -10,19 +10,25 @@ namespace Drupal\Tests\fillpdf\Traits;
trait TestFillPdfTrait { trait TestFillPdfTrait {
/** /**
* Configures the FillPdf test backend. * Configures the FillPdf test scheme and backend.
* *
* @param string $scheme * @param string $scheme
* (optional) The file system scheme to use for PDF templates. Defaults * (optional) The file system scheme to use for PDF templates. Defaults
* to 'public'. * to the site default, which initially is 'public'.
* @param string $backend * @param string $backend
* (optional) The backend to use. Defaults to 'test'. * (optional) The backend to use. Defaults to 'test'.
*/ */
protected function configureBackend($scheme = 'public', $backend = 'test') { protected function configureBackend($scheme = 'default', $backend = 'test') {
// FillPDF needs to be configured. /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
/** @var \Drupal\Core\Config\Config $fillpdf_settings */ $config_factory = $this->container->get('config.factory');
$fillpdf_settings = $this->container->get('config.factory')
->getEditable('fillpdf.settings') // Get the site default scheme.
if ($scheme == 'default') {
$scheme = $config_factory->get('system.file')->get('default_scheme');
}
// Set FillPDF backend and scheme.
$fillpdf_settings = $config_factory->getEditable('fillpdf.settings')
->set('scheme', $scheme) ->set('scheme', $scheme)
->set('backend', $backend); ->set('backend', $backend);
$fillpdf_settings->save(); $fillpdf_settings->save();
......
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