function ConfigStorageTestBase::testCRUD

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

Tests storage CRUD operations.

@todo Coverage: Trigger PDOExceptions / Database exceptions.

File

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

Class

ConfigStorageTestBase
Base class for testing storage operations.

Namespace

Drupal\KernelTests\Core\Config\Storage

Code

public function testCRUD() : void {
  $name = 'config_test.storage';
  // Checking whether a non-existing name exists returns FALSE.
  $this->assertFalse($this->storage
    ->exists($name));
  // Checking whether readMultiple() works with empty storage.
  $this->assertEmpty($this->storage
    ->readMultiple([
    $name,
  ]));
  // readMultiple() accepts an empty array.
  $this->assertSame([], $this->storage
    ->readMultiple([]), 'Empty query should return empty array');
  // Reading a non-existing name returns FALSE.
  $data = $this->storage
    ->read($name);
  $this->assertFalse($data);
  // Writing data returns TRUE and the data has been written.
  $data = [
    'foo' => 'bar',
  ];
  $result = $this->storage
    ->write($name, $data);
  $this->assertTrue($result);
  $raw_data = $this->read($name);
  $this->assertSame($data, $raw_data);
  // Checking whether an existing name exists returns TRUE.
  $this->assertTrue($this->storage
    ->exists($name));
  // Writing the identical data again still returns TRUE.
  $result = $this->storage
    ->write($name, $data);
  $this->assertTrue($result);
  // Listing all names returns all.
  $this->storage
    ->write('system.performance', []);
  $names = $this->storage
    ->listAll();
  $this->assertContains('system.performance', $names);
  $this->assertContains($name, $names);
  // Listing all names with prefix returns names with that prefix only.
  $names = $this->storage
    ->listAll('config_test.');
  $this->assertNotContains('system.performance', $names);
  $this->assertContains($name, $names);
  // Rename the configuration storage object.
  $new_name = 'config_test.storage_rename';
  $this->storage
    ->rename($name, $new_name);
  $raw_data = $this->read($new_name);
  $this->assertSame($data, $raw_data);
  // Rename it back so further tests work.
  $this->storage
    ->rename($new_name, $name);
  // Deleting an existing name returns TRUE.
  $result = $this->storage
    ->delete($name);
  $this->assertTrue($result);
  // Deleting a non-existing name returns FALSE.
  $result = $this->storage
    ->delete($name);
  $this->assertFalse($result);
  // Deleting all names with prefix deletes the appropriate data and returns
  // TRUE.
  $files = [
    'config_test.test.biff',
    'config_test.test.bang',
    'config_test.test.pow',
  ];
  foreach ($files as $name) {
    $this->storage
      ->write($name, $data);
  }
  // Test that deleting a prefix that returns no configuration returns FALSE
  // because nothing is deleted.
  $this->assertFalse($this->storage
    ->deleteAll('some_thing_that_cannot_exist'));
  $result = $this->storage
    ->deleteAll('config_test.');
  $names = $this->storage
    ->listAll('config_test.');
  $this->assertTrue($result);
  $this->assertSame([], $names);
  // Test renaming an object that does not exist returns FALSE.
  $this->assertFalse($this->storage
    ->rename('config_test.storage_does_not_exist', 'config_test.storage_does_not_exist_rename'));
  // Test renaming to an object that already returns FALSE.
  $data = [
    'foo' => 'bar',
  ];
  $this->assertTrue($this->storage
    ->write($name, $data));
  $this->assertFalse($this->storage
    ->rename('config_test.storage_does_not_exist', $name));
}

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