Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\KernelTests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Diff\Diff;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Trait to help with diffing config.
|
Chris@0
|
9 */
|
Chris@0
|
10 trait AssertConfigTrait {
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Ensures that a specific config diff does not contain unwanted changes.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @param \Drupal\Component\Diff\Diff $result
|
Chris@0
|
16 * The diff result for the passed in config name.
|
Chris@0
|
17 * @param string $config_name
|
Chris@0
|
18 * The config name to check.
|
Chris@0
|
19 * @param array $skipped_config
|
Chris@0
|
20 * An array of skipped config, keyed by string. If the value is TRUE, the
|
Chris@0
|
21 * entire file will be ignored, otherwise it's an array of strings which are
|
Chris@0
|
22 * ignored.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @throws \Exception
|
Chris@0
|
25 * Thrown when a configuration is different.
|
Chris@0
|
26 */
|
Chris@0
|
27 protected function assertConfigDiff(Diff $result, $config_name, array $skipped_config) {
|
Chris@0
|
28 foreach ($result->getEdits() as $op) {
|
Chris@0
|
29 switch (get_class($op)) {
|
Chris@0
|
30 case 'Drupal\Component\Diff\Engine\DiffOpCopy':
|
Chris@0
|
31 // Nothing to do, a copy is what we expect.
|
Chris@0
|
32 break;
|
Chris@0
|
33 case 'Drupal\Component\Diff\Engine\DiffOpDelete':
|
Chris@0
|
34 case 'Drupal\Component\Diff\Engine\DiffOpChange':
|
Chris@0
|
35 // It is not part of the skipped config, so we can directly throw the
|
Chris@0
|
36 // exception.
|
Chris@0
|
37 if (!in_array($config_name, array_keys($skipped_config))) {
|
Chris@0
|
38 throw new \Exception($config_name . ': ' . var_export($op, TRUE));
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 // Allow to skip entire config files.
|
Chris@0
|
42 if ($skipped_config[$config_name] === TRUE) {
|
Chris@17
|
43 break;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 // Allow to skip some specific lines of imported config files.
|
Chris@0
|
47 // Ensure that the only changed lines are the ones we marked as
|
Chris@0
|
48 // skipped.
|
Chris@0
|
49 $all_skipped = TRUE;
|
Chris@0
|
50
|
Chris@0
|
51 $changes = get_class($op) == 'Drupal\Component\Diff\Engine\DiffOpDelete' ? $op->orig : $op->closing;
|
Chris@0
|
52 foreach ($changes as $closing) {
|
Chris@0
|
53 // Skip some of the changes, as they are caused by module install
|
Chris@0
|
54 // code.
|
Chris@0
|
55 $found = FALSE;
|
Chris@0
|
56 if (!empty($skipped_config[$config_name])) {
|
Chris@0
|
57 foreach ($skipped_config[$config_name] as $line) {
|
Chris@0
|
58 if (strpos($closing, $line) !== FALSE) {
|
Chris@0
|
59 $found = TRUE;
|
Chris@0
|
60 break;
|
Chris@0
|
61 }
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|
Chris@0
|
64 $all_skipped = $all_skipped && $found;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 if (!$all_skipped) {
|
Chris@0
|
68 throw new \Exception($config_name . ': ' . var_export($op, TRUE));
|
Chris@0
|
69 }
|
Chris@0
|
70 break;
|
Chris@0
|
71 case 'Drupal\Component\Diff\Engine\DiffOpAdd':
|
Chris@0
|
72 // The _core property does not exist in the default config.
|
Chris@0
|
73 if ($op->closing[0] === '_core:') {
|
Chris@17
|
74 break;
|
Chris@0
|
75 }
|
Chris@0
|
76 foreach ($op->closing as $closing) {
|
Chris@0
|
77 // The UUIDs don't exist in the default config.
|
Chris@0
|
78 if (strpos($closing, 'uuid: ') === 0) {
|
Chris@17
|
79 break;
|
Chris@0
|
80 }
|
Chris@0
|
81 throw new \Exception($config_name . ': ' . var_export($op, TRUE));
|
Chris@0
|
82 }
|
Chris@0
|
83 break;
|
Chris@0
|
84 default:
|
Chris@0
|
85 throw new \Exception($config_name . ': ' . var_export($op, TRUE));
|
Chris@0
|
86 }
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 }
|