1.20.x config.test | ConfigurationUITest::testFullImportValidation() |
Test full import validation.
File
- modules/
config/ tests/ config.test, line 429 - Tests for Configuration module.
Class
- ConfigurationUITest
- Tests the UI for syncing, importing, and exporting.
Code
function testFullImportValidation() {
// Enable all non-test core modules to populate all default config files.
$core_modules = db_query("SELECT name FROM {system} WHERE type = 'module' AND filename LIKE 'core/modules%' AND filename NOT LIKE '%/tests/%'")->fetchCol();
module_enable($core_modules);
// Submit the export form and verify response.
$this->backdropPost('admin/config/development/configuration/full/export', array(), t('Export'));
$this->assertResponse(200, 'User can access the download callback.');
// Get the archived binary file provided to user for download.
$archive_data = $this->backdropGetContent();
// Save the archive file.
$uri = 'temporary://config.tar.gz';
file_put_contents($uri, $archive_data);
$realpath = backdrop_realpath($uri);
// Expand the archive file to modify its contents.
$directory = 'temporary://' . basename($uri, '.tar.gz');
$archiver = new Archive_Tar($realpath);
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
$archiver->extract($directory);
// Delete the old archive.
unset($archiver);
unlink($uri);
// Modify each and every config file.
$files = file_scan_directory($directory, '/.*\.json/');
foreach ($files as $filepath => $file) {
$config_data = json_decode(file_get_contents($filepath), TRUE);
$config_data['config_test_addition'] = 'test';
file_put_contents($filepath, backdrop_json_encode($config_data, TRUE));
}
// Add back into the archive.
$archiver = new Archive_Tar($realpath);
$archiver->createModify(array_keys($files), '', $directory);
// Delete the modified files.
file_unmanaged_delete_recursive($directory);
// Upload the archive into the import form.
$edit = array(
'files[import_tarball]' => $uri,
);
$this->backdropPost('admin/config/development/configuration/full', $edit, t('Stage import'));
// Delete the new archive.
unlink($uri);
$this->backdropGet('admin/config/development/configuration');
// Check that every config file is changed.
foreach ($files as $file) {
$this->assertText(str_replace('.json', '', $file->filename), format_string('Config file "@file" staged for import.', array('@file' => $file->filename)));
}
$this->backdropPost(NULL, array(), t('Import all'));
// Confirm the import was successful.
$this->assertText(t('Configuration sync completed. @files configuration files synced.', array('@files' => count($files))));
// Display any errors directly as a failure.
$errors = $this->xpath('//div[@id="messages"]//div[contains(@class, "error")]');
if (count($errors)) {
$this->fail(strip_tags($errors[0]->asXML()));
}
}