AssertConfigTrait.php 3.0 KB

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