diff --git a/core/lib/Drupal/Core/Config/StorageComparer.php b/core/lib/Drupal/Core/Config/StorageComparer.php index c7ccc6b0fa38eb44b5aff1de6b5272f0d9c7955a..51f7bec937dda7c74f9440d1a447c1f096a5f686 100644 --- a/core/lib/Drupal/Core/Config/StorageComparer.php +++ b/core/lib/Drupal/Core/Config/StorageComparer.php @@ -90,6 +90,13 @@ class StorageComparer implements StorageComparerInterface { * Storage object used to write configuration. */ public function __construct(StorageInterface $source_storage, StorageInterface $target_storage) { + if ($source_storage->getCollectionName() !== StorageInterface::DEFAULT_COLLECTION) { + $source_storage = $source_storage->createCollection(StorageInterface::DEFAULT_COLLECTION); + } + if ($target_storage->getCollectionName() !== StorageInterface::DEFAULT_COLLECTION) { + $target_storage = $target_storage->createCollection(StorageInterface::DEFAULT_COLLECTION); + } + // Wrap the storages in a static cache so that multiple reads of the same // raw configuration object are not costly. $this->sourceCacheStorage = new MemoryBackend(); diff --git a/core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php b/core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php index 6d85e64e3743b87d815dd87b2b09fe15c06a0f44..f20d9f89b1095b823c6beba9c204a97d9ec33170 100644 --- a/core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php +++ b/core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php @@ -3,7 +3,9 @@ namespace Drupal\Tests\Core\Config; use Drupal\Component\Uuid\Php; +use Drupal\Core\Config\MemoryStorage; use Drupal\Core\Config\StorageComparer; +use Drupal\Core\Config\StorageInterface; use Drupal\Tests\UnitTestCase; /** @@ -42,6 +44,14 @@ class StorageComparerTest extends UnitTestCase { protected function setUp(): void { $this->sourceStorage = $this->createMock('Drupal\Core\Config\StorageInterface'); $this->targetStorage = $this->createMock('Drupal\Core\Config\StorageInterface'); + + $this->sourceStorage->expects($this->atLeastOnce()) + ->method('getCollectionName') + ->will($this->returnValue(StorageInterface::DEFAULT_COLLECTION)); + $this->targetStorage->expects($this->atLeastOnce()) + ->method('getCollectionName') + ->will($this->returnValue(StorageInterface::DEFAULT_COLLECTION)); + $this->storageComparer = new StorageComparer($this->sourceStorage, $this->targetStorage); } @@ -241,4 +251,56 @@ public function testCreateChangelistUpdate() { $this->assertEmpty($this->storageComparer->getChangelist('delete')); } + /** + * @covers ::createChangelist + */ + public function testDifferentCollections() { + $source = new MemoryStorage(); + $target = new MemoryStorage(); + + $this->generateRandomData($source, 's'); + $this->generateRandomData($target, 't'); + + // Use random collections for source and target. + $collections = $source->getAllCollectionNames(); + $source = $source->createCollection($collections[array_rand($collections)]); + $collections = $target->getAllCollectionNames(); + $target = $target->createCollection($collections[array_rand($collections)]); + + $comparer = new StorageComparer($source, $target); + $comparer->createChangelist(); + + foreach (array_merge([StorageInterface::DEFAULT_COLLECTION], $source->getAllCollectionNames(), $target->getAllCollectionNames()) as $collection) { + $expected = [ + 'create' => $source->createCollection($collection)->listAll(), + 'update' => [], + 'delete' => $target->createCollection($collection)->listAll(), + 'rename' => [], + ]; + + $this->assertEqualsCanonicalizing($expected, $comparer->getChangelist(NULL, $collection)); + } + } + + /** + * Generate random data in a config storage. + * + * @param \Drupal\Core\Config\StorageInterface $storage + * The storage to populate with random data. + * @param string $prefix + * The prefix for random names to make sure they are unique. + */ + protected function generateRandomData(StorageInterface $storage, string $prefix = '') { + $generator = $this->getRandomGenerator(); + for ($i = 0; $i < rand(2, 10); $i++) { + $storage->write($prefix . $this->randomMachineName(), (array) $generator->object()); + } + for ($i = 0; $i < rand(1, 5); $i++) { + $collection = $storage->createCollection($prefix . $this->randomMachineName()); + for ($i = 0; $i < rand(2, 10); $i++) { + $collection->write($prefix . $this->randomMachineName(), (array) $generator->object()); + } + } + } + }