You are here

public function ConfigStorageTestBase::testCRUD in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Config/Storage/ConfigStorageTestBase.php \Drupal\KernelTests\Core\Config\Storage\ConfigStorageTestBase::testCRUD()
  2. 9 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 36

Class

ConfigStorageTestBase
Base class for testing storage operations.

Namespace

Drupal\KernelTests\Core\Config\Storage

Code

public function testCRUD() {
  $name = 'config_test.storage';

  // Checking whether a non-existing name exists returns FALSE.
  $this
    ->assertFalse($this->storage
    ->exists($name));

  // 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.
  $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));
}