function CheckpointStorageTest::testCollections

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Config/Checkpoint/CheckpointStorageTest.php \Drupal\Tests\Core\Config\Checkpoint\CheckpointStorageTest::testCollections()

@covers ::getAllCollectionNames @covers ::getCollectionName @covers ::createCollection

File

core/tests/Drupal/Tests/Core/Config/Checkpoint/CheckpointStorageTest.php, line 213

Class

CheckpointStorageTest
@coversDefaultClass \Drupal\Core\Config\Checkpoint\CheckpointStorage @group Config

Namespace

Drupal\Tests\Core\Config\Checkpoint

Code

public function testCollections() : void {
    $ref_readFromCheckpoint = new \ReflectionProperty($this->storage, 'readFromCheckpoint');
    // Create some checkpoints so the checkpoint storage can be read from.
    $checkpoint1 = $this->storage
        ->checkpoint('1');
    $config = $this->prophesize(Config::class);
    $config->getName()
        ->willReturn('test.config');
    $config->getOriginal('', FALSE)
        ->willReturn([]);
    $config->getRawData()
        ->willReturn([
        'foo' => 'bar',
    ]);
    $config->getStorage()
        ->willReturn($this->storage);
    $event = new ConfigCrudEvent($config->reveal());
    $this->storage
        ->onConfigSaveAndDelete($event);
    $checkpoint2 = $this->storage
        ->checkpoint('2');
    $fixture = [
        StorageInterface::DEFAULT_COLLECTION => [
            $this->randomMachineName(),
        ],
        'A' => [
            $this->randomMachineName(),
        ],
        'B' => [
            $this->randomMachineName(),
        ],
        'C' => [
            $this->randomMachineName(),
        ],
    ];
    $this->setRandomFixtureConfig($fixture);
    $this->assertEquals([
        'A',
        'B',
        'C',
    ], $this->storage
        ->getAllCollectionNames());
    foreach (array_keys($fixture) as $collection) {
        $storage = $this->storage
            ->createCollection($collection);
        // Assert that the collection storage is still a checkpoint storage.
        $this->assertInstanceOf(CheckpointStorage::class, $storage);
        $this->assertEquals($collection, $storage->getCollectionName());
        // Ensure that the
        // \Drupal\Core\Config\Checkpoint\CheckpointStorage::$readFromCheckpoint
        // property is kept in sync.
        $this->storage
            ->setCheckpointToReadFrom($checkpoint2);
        $this->assertSame($checkpoint2->id, $ref_readFromCheckpoint->getValue($storage->createCollection($collection))?->id);
        if (isset($previous_collection)) {
            $previous_collection->setCheckpointToReadFrom($checkpoint1);
            $this->assertSame($checkpoint1->id, $ref_readFromCheckpoint->getValue($storage->createCollection($collection))?->id);
            $this->assertSame($checkpoint1->id, $ref_readFromCheckpoint->getValue($this->storage
                ->createCollection($collection))?->id);
        }
        // Save the storage in a variable so we can test use
        // setCheckpointToReadFrom() on it.
        $previous_collection = $storage;
    }
}

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