class StorageComparerTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php \Drupal\Tests\Core\Config\StorageComparerTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php \Drupal\Tests\Core\Config\StorageComparerTest
  3. 10 core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php \Drupal\Tests\Core\Config\StorageComparerTest

@coversDefaultClass \Drupal\Core\Config\StorageComparer @group Config

Hierarchy

Expanded class hierarchy of StorageComparerTest

File

core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php, line 17

Namespace

Drupal\Tests\Core\Config
View source
class StorageComparerTest extends UnitTestCase {
    
    /**
     * @var \Drupal\Core\Config\StorageInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $sourceStorage;
    
    /**
     * @var \Drupal\Core\Config\StorageInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $targetStorage;
    
    /**
     * The storage comparer to test.
     *
     * @var \Drupal\Core\Config\StorageComparer
     */
    protected $storageComparer;
    
    /**
     * An array of test configuration data keyed by configuration name.
     *
     * @var array
     */
    protected $configData;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->sourceStorage = $this->createMock('Drupal\\Core\\Config\\StorageInterface');
        $this->targetStorage = $this->createMock('Drupal\\Core\\Config\\StorageInterface');
        $this->sourceStorage
            ->expects($this->atLeastOnce())
            ->method('getCollectionName')
            ->willReturn(StorageInterface::DEFAULT_COLLECTION);
        $this->targetStorage
            ->expects($this->atLeastOnce())
            ->method('getCollectionName')
            ->willReturn(StorageInterface::DEFAULT_COLLECTION);
        $this->storageComparer = new StorageComparer($this->sourceStorage, $this->targetStorage);
    }
    protected function getConfigData() {
        $uuid = new Php();
        // Mock data using minimal data to use ConfigDependencyManger.
        $this->configData = [
            // Simple config that controls configuration sync.
'system.site' => [
                'title' => 'Drupal',
                'uuid' => $uuid->generate(),
            ],
            // Config entity which requires another config entity.
'field.field.node.article.body' => [
                'id' => 'node.article.body',
                'uuid' => $uuid->generate(),
                'dependencies' => [
                    'config' => [
                        'field.storage.node.body',
                    ],
                ],
            ],
            // Config entity which is required by another config entity.
'field.storage.node.body' => [
                'id' => 'node.body',
                'uuid' => $uuid->generate(),
                'dependencies' => [
                    'module' => [
                        'text',
                    ],
                ],
            ],
            // Config entity not which has no dependencies on configuration.
'views.view.test_view' => [
                'id' => 'test_view',
                'uuid' => $uuid->generate(),
                'dependencies' => [
                    'module' => [
                        'node',
                    ],
                ],
            ],
            // Simple config.
'system.logging' => [
                'error_level' => 'hide',
            ],
        ];
        return $this->configData;
    }
    
    /**
     * @covers ::createChangelist
     */
    public function testCreateChangelistNoChange() : void {
        $config_data = $this->getConfigData();
        $config_files = array_keys($config_data);
        $this->sourceStorage
            ->expects($this->once())
            ->method('listAll')
            ->willReturn($config_files);
        $this->targetStorage
            ->expects($this->once())
            ->method('listAll')
            ->willReturn($config_files);
        $this->sourceStorage
            ->expects($this->once())
            ->method('readMultiple')
            ->willReturn($config_data);
        $this->targetStorage
            ->expects($this->once())
            ->method('readMultiple')
            ->willReturn($config_data);
        $this->sourceStorage
            ->expects($this->once())
            ->method('getAllCollectionNames')
            ->willReturn([]);
        $this->targetStorage
            ->expects($this->once())
            ->method('getAllCollectionNames')
            ->willReturn([]);
        $this->storageComparer
            ->createChangelist();
        $this->assertEmpty($this->storageComparer
            ->getChangelist('create'));
        $this->assertEmpty($this->storageComparer
            ->getChangelist('delete'));
        $this->assertEmpty($this->storageComparer
            ->getChangelist('update'));
    }
    
    /**
     * @covers ::createChangelist
     */
    public function testCreateChangelistCreate() : void {
        $target_data = $source_data = $this->getConfigData();
        unset($target_data['field.storage.node.body']);
        unset($target_data['field.field.node.article.body']);
        unset($target_data['views.view.test_view']);
        $this->sourceStorage
            ->expects($this->once())
            ->method('listAll')
            ->willReturn(array_keys($source_data));
        $this->targetStorage
            ->expects($this->once())
            ->method('listAll')
            ->willReturn(array_keys($target_data));
        $this->sourceStorage
            ->expects($this->once())
            ->method('readMultiple')
            ->willReturn($source_data);
        $this->targetStorage
            ->expects($this->once())
            ->method('readMultiple')
            ->willReturn($target_data);
        $this->sourceStorage
            ->expects($this->once())
            ->method('getAllCollectionNames')
            ->willReturn([]);
        $this->targetStorage
            ->expects($this->once())
            ->method('getAllCollectionNames')
            ->willReturn([]);
        $this->storageComparer
            ->createChangelist();
        $expected = [
            'field.storage.node.body',
            'field.field.node.article.body',
            'views.view.test_view',
        ];
        $this->assertEquals($expected, $this->storageComparer
            ->getChangelist('create'));
        $this->assertEmpty($this->storageComparer
            ->getChangelist('delete'));
        $this->assertEmpty($this->storageComparer
            ->getChangelist('update'));
    }
    
    /**
     * @covers ::createChangelist
     */
    public function testCreateChangelistDelete() : void {
        $target_data = $source_data = $this->getConfigData();
        unset($source_data['field.storage.node.body']);
        unset($source_data['field.field.node.article.body']);
        unset($source_data['views.view.test_view']);
        $this->sourceStorage
            ->expects($this->once())
            ->method('listAll')
            ->willReturn(array_keys($source_data));
        $this->targetStorage
            ->expects($this->once())
            ->method('listAll')
            ->willReturn(array_keys($target_data));
        $this->sourceStorage
            ->expects($this->once())
            ->method('readMultiple')
            ->willReturn($source_data);
        $this->targetStorage
            ->expects($this->once())
            ->method('readMultiple')
            ->willReturn($target_data);
        $this->sourceStorage
            ->expects($this->once())
            ->method('getAllCollectionNames')
            ->willReturn([]);
        $this->targetStorage
            ->expects($this->once())
            ->method('getAllCollectionNames')
            ->willReturn([]);
        $this->storageComparer
            ->createChangelist();
        $expected = [
            'views.view.test_view',
            'field.field.node.article.body',
            'field.storage.node.body',
        ];
        $this->assertEquals($expected, $this->storageComparer
            ->getChangelist('delete'));
        $this->assertEmpty($this->storageComparer
            ->getChangelist('create'));
        $this->assertEmpty($this->storageComparer
            ->getChangelist('update'));
    }
    
    /**
     * @covers ::createChangelist
     */
    public function testCreateChangelistUpdate() : void {
        $target_data = $source_data = $this->getConfigData();
        $source_data['system.site']['title'] = 'Drupal New!';
        $source_data['field.field.node.article.body']['new_config_key'] = 'new data';
        $source_data['field.storage.node.body']['new_config_key'] = 'new data';
        $this->sourceStorage
            ->expects($this->once())
            ->method('listAll')
            ->willReturn(array_keys($source_data));
        $this->targetStorage
            ->expects($this->once())
            ->method('listAll')
            ->willReturn(array_keys($target_data));
        $this->sourceStorage
            ->expects($this->once())
            ->method('readMultiple')
            ->willReturn($source_data);
        $this->targetStorage
            ->expects($this->once())
            ->method('readMultiple')
            ->willReturn($target_data);
        $this->sourceStorage
            ->expects($this->once())
            ->method('getAllCollectionNames')
            ->willReturn([]);
        $this->targetStorage
            ->expects($this->once())
            ->method('getAllCollectionNames')
            ->willReturn([]);
        $this->storageComparer
            ->createChangelist();
        $expected = [
            'field.storage.node.body',
            'field.field.node.article.body',
            'system.site',
        ];
        $this->assertEquals($expected, $this->storageComparer
            ->getChangelist('update'));
        $this->assertEmpty($this->storageComparer
            ->getChangelist('create'));
        $this->assertEmpty($this->storageComparer
            ->getChangelist('delete'));
    }
    
    /**
     * @covers ::createChangelist
     */
    public function testDifferentCollections() : void {
        $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 = '') : void {
        $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());
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
StorageComparerTest::$configData protected property An array of test configuration data keyed by configuration name.
StorageComparerTest::$sourceStorage protected property
StorageComparerTest::$storageComparer protected property The storage comparer to test.
StorageComparerTest::$targetStorage protected property
StorageComparerTest::generateRandomData protected function Generate random data in a config storage.
StorageComparerTest::getConfigData protected function
StorageComparerTest::setUp protected function Overrides UnitTestCase::setUp
StorageComparerTest::testCreateChangelistCreate public function @covers ::createChangelist
StorageComparerTest::testCreateChangelistDelete public function @covers ::createChangelist
StorageComparerTest::testCreateChangelistNoChange public function @covers ::createChangelist
StorageComparerTest::testCreateChangelistUpdate public function @covers ::createChangelist
StorageComparerTest::testDifferentCollections public function @covers ::createChangelist
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.