function ConfigStorageTestBase::testCollection

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/Storage/ConfigStorageTestBase.php \Drupal\KernelTests\Core\Config\Storage\ConfigStorageTestBase::testCollection()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Config/Storage/ConfigStorageTestBase.php \Drupal\KernelTests\Core\Config\Storage\ConfigStorageTestBase::testCollection()
  3. 10 core/tests/Drupal/KernelTests/Core/Config/Storage/ConfigStorageTestBase.php \Drupal\KernelTests\Core\Config\Storage\ConfigStorageTestBase::testCollection()

Tests that the storage supports collections.

File

core/tests/Drupal/KernelTests/Core/Config/Storage/ConfigStorageTestBase.php, line 194

Class

ConfigStorageTestBase
Base class for testing storage operations.

Namespace

Drupal\KernelTests\Core\Config\Storage

Code

public function testCollection() : void {
    $name = 'config_test.storage';
    $data = [
        'foo' => 'bar',
    ];
    $result = $this->storage
        ->write($name, $data);
    $this->assertTrue($result);
    $this->assertSame($data, $this->storage
        ->read($name));
    // Create configuration in a new collection.
    $new_storage = $this->storage
        ->createCollection('collection.sub.new');
    $this->assertFalse($new_storage->exists($name));
    $this->assertEquals([], $new_storage->listAll());
    $this->assertFalse($new_storage->delete($name));
    $this->assertFalse($new_storage->deleteAll('config_test.'));
    $this->assertFalse($new_storage->deleteAll());
    $this->assertFalse($new_storage->rename($name, 'config_test.another_name'));
    $new_storage->write($name, $data);
    $this->assertTrue($result);
    $this->assertSame($data, $new_storage->read($name));
    $this->assertEquals([
        $name,
    ], $new_storage->listAll());
    $this->assertTrue($new_storage->exists($name));
    $new_data = [
        'foo' => 'baz',
    ];
    $new_storage->write($name, $new_data);
    $this->assertTrue($result);
    $this->assertSame($new_data, $new_storage->read($name));
    // Create configuration in another collection.
    $another_storage = $this->storage
        ->createCollection('collection.sub.another');
    $this->assertFalse($another_storage->exists($name));
    $this->assertEquals([], $another_storage->listAll());
    $another_storage->write($name, $new_data);
    $this->assertTrue($result);
    $this->assertSame($new_data, $another_storage->read($name));
    $this->assertEquals([
        $name,
    ], $another_storage->listAll());
    $this->assertTrue($another_storage->exists($name));
    // Create configuration in yet another collection.
    $alt_storage = $this->storage
        ->createCollection('alternate');
    $alt_storage->write($name, $new_data);
    $this->assertTrue($result);
    $this->assertSame($new_data, $alt_storage->read($name));
    // Switch back to the collection-less mode and check the data still exists
    // add has not been touched.
    $this->assertSame($data, $this->storage
        ->read($name));
    // Check that the getAllCollectionNames() method works.
    $this->assertSame([
        'alternate',
        'collection.sub.another',
        'collection.sub.new',
    ], $this->storage
        ->getAllCollectionNames());
    // Check that the collections are removed when they are empty.
    $alt_storage->delete($name);
    $this->assertSame([
        'collection.sub.another',
        'collection.sub.new',
    ], $this->storage
        ->getAllCollectionNames());
    // Create configuration in collection called 'collection'. This ensures that
    // FileStorage's collection storage works regardless of its use of
    // subdirectories.
    $parent_storage = $this->storage
        ->createCollection('collection');
    $this->assertFalse($parent_storage->exists($name));
    $this->assertEquals([], $parent_storage->listAll());
    $parent_storage->write($name, $new_data);
    $this->assertTrue($result);
    $this->assertSame($new_data, $parent_storage->read($name));
    $this->assertEquals([
        $name,
    ], $parent_storage->listAll());
    $this->assertTrue($parent_storage->exists($name));
    $this->assertSame([
        'collection',
        'collection.sub.another',
        'collection.sub.new',
    ], $this->storage
        ->getAllCollectionNames());
    $parent_storage->deleteAll();
    $this->assertSame([
        'collection.sub.another',
        'collection.sub.new',
    ], $this->storage
        ->getAllCollectionNames());
    // Test operations on a collection emptied through deletion.
    $this->assertFalse($parent_storage->exists($name));
    $this->assertEquals([], $parent_storage->listAll());
    $this->assertFalse($parent_storage->delete($name));
    $this->assertFalse($parent_storage->deleteAll('config_test.'));
    $this->assertFalse($parent_storage->deleteAll());
    $this->assertFalse($parent_storage->rename($name, 'config_test.another_name'));
    // Check that the having an empty collection-less storage does not break
    // anything. Before deleting check that the previous delete did not affect
    // data in another collection.
    $this->assertSame($data, $this->storage
        ->read($name));
    $this->storage
        ->delete($name);
    $this->assertSame([
        'collection.sub.another',
        'collection.sub.new',
    ], $this->storage
        ->getAllCollectionNames());
}

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