View source
<?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\KernelTests\KernelTestBase;
class ConfigDiffTest extends KernelTestBase {
protected static $modules = [
'config_test',
'system',
];
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';
$this
->installConfig([
'config_test',
]);
$original_data = \Drupal::config($config_name)
->get();
$sync_data = $original_data;
$sync_data[$change_key] = $change_data;
$sync_data[$add_key] = $add_data;
$sync
->write($config_name, $sync_data);
$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,
]);
$sync_data = $original_data;
unset($sync_data[$remove_key]);
$sync
->write($config_name, $sync_data);
$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);
$sync_data = $original_data;
$sync_data[$add_key] = $add_data;
$sync
->write($config_name, $sync_data);
$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_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);
$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');
$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.');
}
public function testCollectionDiff() {
$active = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$active_test_collection = $active
->createCollection('test');
$sync_test_collection = $sync
->createCollection('test');
$config_name = 'config_test.test';
$data = [
'foo' => 'bar',
];
$active
->write($config_name, $data);
$sync
->write($config_name, $data);
$active_test_collection
->write($config_name, $data);
$sync_test_collection
->write($config_name, [
'foo' => 'baz',
]);
$diff = \Drupal::service('config.manager')
->diff($active, $sync, $config_name);
$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');
$diff = \Drupal::service('config.manager')
->diff($active, $sync, $config_name, NULL, 'test');
$edits = $diff
->getEdits();
$this
->assertYamlEdit($edits, 'foo', 'change', [
'foo: bar',
], [
'foo: baz',
]);
}
protected function assertYamlEdit(array $edits, $field, $type, $orig = NULL, $closing = NULL) {
$match = FALSE;
foreach ($edits as $edit) {
$haystack = $type == 'add' ? $edit->closing : $edit->orig;
if (is_array($haystack)) {
foreach ($haystack as $item) {
if (strpos($item, $field . ':') === 0) {
$match = TRUE;
$this
->assertEquals($type, $edit->type, "The {$field} item in the diff is a {$type}");
if (isset($orig)) {
$this
->assertSame($orig, $edit->orig, "The original value for key '{$field}' is correct.");
}
if (isset($closing)) {
$this
->assertSame($closing, $edit->closing, "The closing value for key '{$field}' is correct.");
}
break 2;
}
}
}
}
if (!$match) {
$this
->fail("{$field} edit was not matched");
}
}
}