public function ConfigDiffTest::testDiff in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Config/ConfigDiffTest.php \Drupal\KernelTests\Core\Config\ConfigDiffTest::testDiff()
- 9 core/tests/Drupal/KernelTests/Core/Config/ConfigDiffTest.php \Drupal\KernelTests\Core\Config\ConfigDiffTest::testDiff()
Tests calculating the difference between two sets of configuration.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Config/ ConfigDiffTest.php, line 24
Class
- ConfigDiffTest
- Calculating the difference between two sets of configuration.
Namespace
Drupal\KernelTests\Core\ConfigCode
public function testDiff() {
$active = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$config_name = 'config_test.system';
$change_key = 'foo';
$remove_key = '404';
$add_key = 'biff';
$add_data = 'bangpow';
$change_data = 'foobar';
// Install the default config.
$this
->installConfig([
'config_test',
]);
$original_data = \Drupal::config($config_name)
->get();
// Change a configuration value in sync.
$sync_data = $original_data;
$sync_data[$change_key] = $change_data;
$sync_data[$add_key] = $add_data;
$sync
->write($config_name, $sync_data);
// Verify that the diff reflects a change.
$diff = \Drupal::service('config.manager')
->diff($active, $sync, $config_name);
$edits = $diff
->getEdits();
$this
->assertYamlEdit($edits, $change_key, 'change', [
$change_key . ': ' . $original_data[$change_key],
], [
$change_key . ': ' . $change_data,
]);
// Reset data back to original, and remove a key
$sync_data = $original_data;
unset($sync_data[$remove_key]);
$sync
->write($config_name, $sync_data);
// Verify that the diff reflects a removed key.
$diff = \Drupal::service('config.manager')
->diff($active, $sync, $config_name);
$edits = $diff
->getEdits();
$this
->assertYamlEdit($edits, $change_key, 'copy');
$this
->assertYamlEdit($edits, $remove_key, 'delete', [
$remove_key . ': ' . $original_data[$remove_key],
], FALSE);
// Reset data back to original and add a key
$sync_data = $original_data;
$sync_data[$add_key] = $add_data;
$sync
->write($config_name, $sync_data);
// Verify that the diff reflects an added key.
$diff = \Drupal::service('config.manager')
->diff($active, $sync, $config_name);
$edits = $diff
->getEdits();
$this
->assertYamlEdit($edits, $change_key, 'copy');
$this
->assertYamlEdit($edits, $add_key, 'add', FALSE, [
$add_key . ': ' . $add_data,
]);
// Test diffing a renamed config entity.
$test_entity_id = $this
->randomMachineName();
$test_entity = \Drupal::entityTypeManager()
->getStorage('config_test')
->create([
'id' => $test_entity_id,
'label' => $this
->randomMachineName(),
]);
$test_entity
->save();
$data = $active
->read('config_test.dynamic.' . $test_entity_id);
$sync
->write('config_test.dynamic.' . $test_entity_id, $data);
$config_name = 'config_test.dynamic.' . $test_entity_id;
$diff = \Drupal::service('config.manager')
->diff($active, $sync, $config_name, $config_name);
// Prove the fields match.
$edits = $diff
->getEdits();
$this
->assertEquals('copy', $edits[0]->type, 'The first item in the diff is a copy.');
$this
->assertCount(1, $edits, 'There is one item in the diff');
// Rename the entity.
$new_test_entity_id = $this
->randomMachineName();
$test_entity
->set('id', $new_test_entity_id);
$test_entity
->save();
$diff = \Drupal::service('config.manager')
->diff($active, $sync, 'config_test.dynamic.' . $new_test_entity_id, $config_name);
$edits = $diff
->getEdits();
$this
->assertYamlEdit($edits, 'uuid', 'copy');
$this
->assertYamlEdit($edits, 'id', 'change', [
'id: ' . $new_test_entity_id,
], [
'id: ' . $test_entity_id,
]);
$this
->assertYamlEdit($edits, 'label', 'copy');
$this
->assertEquals('copy', $edits[2]->type, 'The third item in the diff is a copy.');
$this
->assertCount(3, $edits, 'There are three items in the diff.');
}