1 config.test | ConfigurationUITest::testImport() |
Tests importing configuration.
File
- core/
modules/ config/ tests/ config.test, line 281 - Tests for Configuration module.
Class
- ConfigurationUITest
- Tests the UI for syncing, importing, and exporting.
Code
function testImport() {
$name = 'config_test.simple';
$dynamic_name = 'config_test.dynamic.new';
$staging_storage = new ConfigFileStorage(config_get_config_directory('staging'));
$this->backdropGet('admin/config/development/configuration');
$this->assertText('There are no configuration changes currently staged.');
$this->assertNoFieldById('edit-submit', t('Import all'));
// Copy all configuration to staging before modification.
$this->copyConfig('active', 'staging');
// Create updated configuration object.
$favorite_animal = 'Animal ' . $this->randomString();
$staging_config = config($name, 'staging');
$staging_config->set('favorite_animal', $favorite_animal);
$staging_config->save();
$this->assertIdentical($staging_storage->exists($name), TRUE, $name . ' found.');
// Create new config.
$original_dynamic_data = array(
'id' => 'new',
'label' => 'New',
'weight' => 0,
'status' => TRUE,
);
$staging_storage->write($dynamic_name, $original_dynamic_data);
$this->assertIdentical($staging_storage->exists($dynamic_name), TRUE, $dynamic_name . ' found in staging.');
// Verify that both appear as ready to import.
$this->backdropGet('admin/config/development/configuration');
$this->assertText($name);
$this->assertText($dynamic_name);
$this->assertFieldById('edit-submit', t('Import all'));
// Check that the locking message works when an import is in-progress.
state_set('config_sync', REQUEST_TIME);
$this->backdropPost(NULL, array(), t('Import all'));
$this->assertText('Another request may be synchronizing configuration already or a sync failed unexpectedly.');
state_del('config_sync');
// Import and verify that both do not appear anymore.
$this->backdropPost(NULL, array(), t('Import all'));
$this->assertNoText($name);
$this->assertNoText($dynamic_name);
$this->assertNoFieldById('edit-submit', t('Import all'));
// Verify that there are no further changes to import.
$this->assertText('There are no configuration changes currently staged.');
// Verify configuration has changed.
$this->assertIdentical($favorite_animal, config_get($name, 'favorite_animal'));
// Verify that new config exists.
$this->assertIdentical($original_dynamic_data, config_get($dynamic_name));
// Create a config that is not declared in hook_config_info().
$this->copyConfig('active', 'staging');
$dynamic_name2 = $dynamic_name . '2';
$staging_storage->write($dynamic_name2, $original_dynamic_data);
$this->assertIdentical($staging_storage->exists($dynamic_name2), TRUE, $dynamic_name2 . ' found in staging.');
// Attempt an import which should fail.
$this->backdropPost('admin/config/development/configuration', array(), t('Import all'));
// Validation should fail, as this config file is not owned by a module.
$missing_module_message = 'The configuration "@name" is not owned by any module. Try enabling the module that provides this configuration, then importing again.';
$error_message = t($missing_module_message, array('@name' => $dynamic_name2));
$this->assertText($error_message, 'Validation properly prevents import when a config is not declared by a module.');
// Create a second config that is not declared in hook_config_info().
$dynamic_name3 = $dynamic_name . '3';
$staging_storage->write($dynamic_name3, $original_dynamic_data);
$this->assertIdentical($staging_storage->exists($dynamic_name3), TRUE, $dynamic_name3 . ' found in staging.');
// Attempt an import which should fail on two missing modules.
$this->backdropPost('admin/config/development/configuration', array(), t('Import all'));
// Confirm the error message is shown once for each module that is missing.
$error_message = t($missing_module_message, array('@name' => $dynamic_name2));
$this->assertText($error_message, 'Validation prevents import when first module of two missing modules is missing.');
$error_message = t($missing_module_message, array('@name' => $dynamic_name3));
$this->assertText($error_message, 'Validation prevents import when second module of two missing modules is missing.');
// Depend on a real module that is not enabled. This configCopy() includes
// the config from "config_test" module.
$this->copyConfig('active', 'staging');
// Fully uninstall config_test module, which will delete associated config.
module_disable(array('config_test'));
backdrop_uninstall_modules(array('config_test'));
module_list(TRUE);
$this->assertFalse(module_exists('config_test'), 'The config_test module has been disabled.');
// Do not include the config_test module being enabled in the first import.
$system_extensions_data = $staging_storage->read('system.extensions');
$system_extensions_data['modules']['config_test'] = FALSE;
$staging_storage->write('system.extensions', $system_extensions_data);
// Attempt an import which should fail on the disabled module.
$this->backdropPost('admin/config/development/configuration', array(), t('Import all'));
$error_message = t($missing_module_message, array('@name' => 'config_test.simple'));
$this->assertText($error_message, 'Validation prevents import when depending on a disabled module.');
// Intentionally create an error in the configuration file so that it will
// be flagged as invalid by config_test module, even though it is disabled
// before the start of the sync process.
$config_test_data = $staging_storage->read('config_test.simple');
$config_test_data['fail_validation'] = TRUE;
$staging_storage->write('config_test.simple', $config_test_data);
// Include the dependent module in system.extensions so it is enabled as
// part of the config sync.
$system_extensions_data['modules']['config_test'] = TRUE;
$staging_storage->write('system.extensions', $system_extensions_data);
// Validation should now fail from hook_config_data_validate().
$this->backdropPost('admin/config/development/configuration', array(), t('Import all'));
$this->assertText('Configuration intentionally flagged to fail validation.', 'Config sync correctly failed when a disabled module was enabled via system.extensions, then dependent configuration failed validation.');
// Allow validation to pass by removing the intentional fail flag.
$config_test_data['fail_validation'] = FALSE;
$staging_storage->write('config_test.simple', $config_test_data);
// Now the import should succeed with the dependent module enabled.
$this->backdropPost('admin/config/development/configuration', array(), t('Import all'));
$error_message = t($missing_module_message, array('@name' => 'config_test'));
$this->assertNoText($error_message, 'No validation error when config that has a dependent module is enabled in the same config sync.');
$this->assertText('Configuration sync completed.', 'Sync successful when enabling a module and its dependent configuration.');
module_list(TRUE);
$this->assertTrue(module_exists('config_test'), 'The config_test module was enabled as part of the config sync.');
// Empty out the staging config.
$staging_storage->deleteAll();
}