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