3 namespace Drupal\KernelTests;
5 use Drupal\Component\Diff\Diff;
8 * Trait to help with diffing config.
10 trait AssertConfigTrait {
13 * Ensures that a specific config diff does not contain unwanted changes.
15 * @param \Drupal\Component\Diff\Diff $result
16 * The diff result for the passed in config name.
17 * @param string $config_name
18 * The config name to check.
19 * @param array $skipped_config
20 * An array of skipped config, keyed by string. If the value is TRUE, the
21 * entire file will be ignored, otherwise it's an array of strings which are
25 * Thrown when a configuration is different.
27 protected function assertConfigDiff(Diff $result, $config_name, array $skipped_config) {
28 foreach ($result->getEdits() as $op) {
29 switch (get_class($op)) {
30 case 'Drupal\Component\Diff\Engine\DiffOpCopy':
31 // Nothing to do, a copy is what we expect.
33 case 'Drupal\Component\Diff\Engine\DiffOpDelete':
34 case 'Drupal\Component\Diff\Engine\DiffOpChange':
35 // It is not part of the skipped config, so we can directly throw the
37 if (!in_array($config_name, array_keys($skipped_config))) {
38 throw new \Exception($config_name . ': ' . var_export($op, TRUE));
41 // Allow to skip entire config files.
42 if ($skipped_config[$config_name] === TRUE) {
46 // Allow to skip some specific lines of imported config files.
47 // Ensure that the only changed lines are the ones we marked as
51 $changes = get_class($op) == 'Drupal\Component\Diff\Engine\DiffOpDelete' ? $op->orig : $op->closing;
52 foreach ($changes as $closing) {
53 // Skip some of the changes, as they are caused by module install
56 if (!empty($skipped_config[$config_name])) {
57 foreach ($skipped_config[$config_name] as $line) {
58 if (strpos($closing, $line) !== FALSE) {
64 $all_skipped = $all_skipped && $found;
68 throw new \Exception($config_name . ': ' . var_export($op, TRUE));
71 case 'Drupal\Component\Diff\Engine\DiffOpAdd':
72 // The _core property does not exist in the default config.
73 if ($op->closing[0] === '_core:') {
76 foreach ($op->closing as $closing) {
77 // The UUIDs don't exist in the default config.
78 if (strpos($closing, 'uuid: ') === 0) {
81 throw new \Exception($config_name . ': ' . var_export($op, TRUE));
85 throw new \Exception($config_name . ': ' . var_export($op, TRUE));