Commit 27a650cd authored by tlyngej's avatar tlyngej Committed by tlyngej

Issue #2845022 by tlyngej: Add support for wildcards in 'Configuration entity names to ignore'

parent d0fe3dd5
......@@ -2,6 +2,7 @@
namespace Drupal\config_ignore;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigImporter;
use Drupal\user\SharedTempStore;
......@@ -30,8 +31,25 @@ class ConfigImporterIgnore {
// For now, we only support updates.
if ($op == 'update') {
foreach ($config_importer->getUnprocessedConfiguration($op) as $config) {
if (in_array($config, $config_ignore_settings)) {
$config_to_ignore[$op][$config] = \Drupal::config($config)->getRawData();
foreach ($config_ignore_settings as $config_ignore_setting) {
// Check if the last character in the string is an asterisk.
// If so, it means that it is a wildcard.
if (Unicode::substr($config_ignore_setting, -1) == '*') {
// Remove the asterisk character from the end of the string.
$config_ignore_setting = rtrim($config_ignore_setting, '*');
// Test if the start of the config, we are checking, are matching
// the $config_ignore_setting string. If it is a match, mark
// that config name to be ignored.
if (Unicode::substr($config, 0, strlen($config_ignore_setting)) == $config_ignore_setting) {
$config_to_ignore[$op][$config] = \Drupal::config($config)->getRawData();
}
}
// If string does not contain an asterisk in the end, just compare
// the two strings, and if they match, mark that config name to be
// ignored.
elseif ($config == $config_ignore_setting) {
$config_to_ignore[$op][$config] = \Drupal::config($config)->getRawData();
}
}
}
}
......
......@@ -39,7 +39,7 @@ class Settings extends ConfigFormBase {
'#type' => 'textarea',
'#rows' => 25,
'#title' => $this->t('Configuration entity names to ignore'),
'#description' => $this->t('One configuration name per line.<br />Examples: <ul><li>user.settings</li><li>views.settings</li><li>contact.settings</li></ul>'),
'#description' => $this->t('One configuration name per line.<br />Examples: <ul><li>user.settings</li><li>views.settings</li><li>contact.settings</li><li>webform.webform.* (will ignore all config entities that starts with <em>webform.webform</em>)</li><li>* (will ignore everything)</li></ul>'),
'#default_value' => implode(PHP_EOL, $config_ignore_settings->get('ignored_config_entities')),
'#size' => 60,
];
......
......@@ -63,4 +63,35 @@ class ConfigIgnoreTest extends WebTestBase {
}
/**
* Verify all wildcard asterisk is working.
*/
public function testValidateIgnoringWithWildcard() {
// Login with a user that has permission to import config.
$this->drupalLogin($this->drupalCreateUser(['import configuration']));
// Set the site name to a known value that we later will try and overwrite.
$this->config('system.site')->set('name', 'Test import')->save();
// Set the system.site:name to be ignored upon config import.
$this->config('config_ignore.settings')->set('ignored_config_entities', ['system.*'])->save();
// Assemble a change that will try and override the current value.
$config = $this->config('system.site')->set('name', 'Import has changed title');
$edit = [
'config_type' => 'system.simple',
'config_name' => $config->getName(),
'import' => Yaml::encode($config->get()),
];
// Submit a new single item config, with the changes.
$this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import'));
$this->drupalPostForm(NULL, array(), t('Confirm'));
// Validate if the title from the imported config was rejected.
$this->assertText('Test import');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment