Mercurial > hg > cmmr2012-drupal-site
comparison core/tests/Drupal/KernelTests/AssertConfigTrait.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\KernelTests; | |
4 | |
5 use Drupal\Component\Diff\Diff; | |
6 | |
7 /** | |
8 * Trait to help with diffing config. | |
9 */ | |
10 trait AssertConfigTrait { | |
11 | |
12 /** | |
13 * Ensures that a specific config diff does not contain unwanted changes. | |
14 * | |
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 | |
22 * ignored. | |
23 * | |
24 * @throws \Exception | |
25 * Thrown when a configuration is different. | |
26 */ | |
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. | |
32 break; | |
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 | |
36 // exception. | |
37 if (!in_array($config_name, array_keys($skipped_config))) { | |
38 throw new \Exception($config_name . ': ' . var_export($op, TRUE)); | |
39 } | |
40 | |
41 // Allow to skip entire config files. | |
42 if ($skipped_config[$config_name] === TRUE) { | |
43 continue; | |
44 } | |
45 | |
46 // Allow to skip some specific lines of imported config files. | |
47 // Ensure that the only changed lines are the ones we marked as | |
48 // skipped. | |
49 $all_skipped = TRUE; | |
50 | |
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 | |
54 // code. | |
55 $found = FALSE; | |
56 if (!empty($skipped_config[$config_name])) { | |
57 foreach ($skipped_config[$config_name] as $line) { | |
58 if (strpos($closing, $line) !== FALSE) { | |
59 $found = TRUE; | |
60 break; | |
61 } | |
62 } | |
63 } | |
64 $all_skipped = $all_skipped && $found; | |
65 } | |
66 | |
67 if (!$all_skipped) { | |
68 throw new \Exception($config_name . ': ' . var_export($op, TRUE)); | |
69 } | |
70 break; | |
71 case 'Drupal\Component\Diff\Engine\DiffOpAdd': | |
72 // The _core property does not exist in the default config. | |
73 if ($op->closing[0] === '_core:') { | |
74 continue; | |
75 } | |
76 foreach ($op->closing as $closing) { | |
77 // The UUIDs don't exist in the default config. | |
78 if (strpos($closing, 'uuid: ') === 0) { | |
79 continue; | |
80 } | |
81 throw new \Exception($config_name . ': ' . var_export($op, TRUE)); | |
82 } | |
83 break; | |
84 default: | |
85 throw new \Exception($config_name . ': ' . var_export($op, TRUE)); | |
86 } | |
87 } | |
88 } | |
89 | |
90 } |