Verified Commit 2120171a authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3224239 by bircher, rpayanm, smustgrave: StorageComparer does not work...

Issue #3224239 by bircher, rpayanm, smustgrave: StorageComparer does not work when storages are not in the default collection

(cherry picked from commit c48da9e7)
parent 6a14cc6f
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -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();
+62 −0
Original line number Diff line number Diff line
@@ -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());
      }
    }
  }

}