ConfigTestTrait.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. }