ExportStorageManagerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Core\Config\ExportStorageManager;
  4. use Drupal\Core\Config\StorageTransformerException;
  5. use Drupal\Core\Lock\NullLockBackend;
  6. use Drupal\KernelTests\KernelTestBase;
  7. /**
  8. * Tests the export storage manager.
  9. *
  10. * @group config
  11. */
  12. class ExportStorageManagerTest extends KernelTestBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected static $modules = [
  17. 'system',
  18. 'config_transformer_test',
  19. ];
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function setUp() {
  24. parent::setUp();
  25. $this->installConfig(['system']);
  26. }
  27. /**
  28. * Test getting the export storage.
  29. */
  30. public function testGetStorage() {
  31. // Get the raw system.site config and set it in the sync storage.
  32. $rawConfig = $this->config('system.site')->getRawData();
  33. $this->container->get('config.storage.sync')->write('system.site', $rawConfig);
  34. // The export storage manager under test.
  35. $manager = new ExportStorageManager(
  36. $this->container->get('config.storage'),
  37. $this->container->get('database'),
  38. $this->container->get('event_dispatcher'),
  39. new NullLockBackend()
  40. );
  41. $storage = $manager->getStorage();
  42. $exported = $storage->read('system.site');
  43. // The test subscriber adds "Arrr" to the slogan of the sync config.
  44. $this->assertEquals($rawConfig['name'], $exported['name']);
  45. $this->assertEquals($rawConfig['slogan'] . ' Arrr', $exported['slogan']);
  46. // Save the config to active storage so that the transformer can alter it.
  47. $this->config('system.site')
  48. ->set('name', 'New name')
  49. ->set('slogan', 'New slogan')
  50. ->save();
  51. // Get the storage again.
  52. $storage = $manager->getStorage();
  53. $exported = $storage->read('system.site');
  54. // The test subscriber adds "Arrr" to the slogan of the sync config.
  55. $this->assertEquals('New name', $exported['name']);
  56. $this->assertEquals($rawConfig['slogan'] . ' Arrr', $exported['slogan']);
  57. // Change what the transformer does without changing anything else to assert
  58. // that the event is dispatched every time the storage is needed.
  59. $this->container->get('state')->set('config_transform_test_mail', 'config@drupal.example');
  60. $storage = $manager->getStorage();
  61. $exported = $storage->read('system.site');
  62. // The mail is still set to the value from the beginning.
  63. $this->assertEquals('config@drupal.example', $exported['mail']);
  64. }
  65. /**
  66. * Test the export storage when it is locked.
  67. */
  68. public function testGetStorageLock() {
  69. $lock = $this->createMock('Drupal\Core\Lock\LockBackendInterface');
  70. $lock->expects($this->at(0))
  71. ->method('acquire')
  72. ->with(ExportStorageManager::LOCK_NAME)
  73. ->will($this->returnValue(FALSE));
  74. $lock->expects($this->at(1))
  75. ->method('wait')
  76. ->with(ExportStorageManager::LOCK_NAME);
  77. $lock->expects($this->at(2))
  78. ->method('acquire')
  79. ->with(ExportStorageManager::LOCK_NAME)
  80. ->will($this->returnValue(FALSE));
  81. // The export storage manager under test.
  82. $manager = new ExportStorageManager(
  83. $this->container->get('config.storage'),
  84. $this->container->get('database'),
  85. $this->container->get('event_dispatcher'),
  86. $lock
  87. );
  88. $this->expectException(StorageTransformerException::class);
  89. $this->expectExceptionMessage("Cannot acquire config export transformer lock.");
  90. $manager->getStorage();
  91. }
  92. }