Drupal investigation

ConfigTestTrait.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Drupal\Tests;
  3. use Drupal\Core\Config\ConfigImporter;
  4. use Drupal\Core\Config\StorageComparer;
  5. use Drupal\Core\Config\StorageInterface;
  6. /**
  7. * Provides helper methods to deal with config system objects in tests.
  8. */
  9. trait ConfigTestTrait {
  10. /**
  11. * Returns a ConfigImporter object to import test configuration.
  12. *
  13. * @return \Drupal\Core\Config\ConfigImporter
  14. * The config importer object.
  15. */
  16. protected function configImporter() {
  17. if (!$this->configImporter) {
  18. // Set up the ConfigImporter object for testing.
  19. $storage_comparer = new StorageComparer(
  20. $this->container->get('config.storage.sync'),
  21. $this->container->get('config.storage'),
  22. $this->container->get('config.manager')
  23. );
  24. $this->configImporter = new ConfigImporter(
  25. $storage_comparer,
  26. $this->container->get('event_dispatcher'),
  27. $this->container->get('config.manager'),
  28. $this->container->get('lock'),
  29. $this->container->get('config.typed'),
  30. $this->container->get('module_handler'),
  31. $this->container->get('module_installer'),
  32. $this->container->get('theme_handler'),
  33. $this->container->get('string_translation')
  34. );
  35. }
  36. // Always recalculate the changelist when called.
  37. return $this->configImporter->reset();
  38. }
  39. /**
  40. * Copies configuration objects from source storage to target storage.
  41. *
  42. * @param \Drupal\Core\Config\StorageInterface $source_storage
  43. * The source config storage service.
  44. * @param \Drupal\Core\Config\StorageInterface $target_storage
  45. * The target config storage service.
  46. */
  47. protected function copyConfig(StorageInterface $source_storage, StorageInterface $target_storage) {
  48. $target_storage->deleteAll();
  49. foreach ($source_storage->listAll() as $name) {
  50. $target_storage->write($name, $source_storage->read($name));
  51. }
  52. }
  53. }