You are here

public function ConfigStorageTestBase::testCRUD in Drupal 8

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. 10 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
    ->assertIdentical($this->storage
    ->exists($name), FALSE);

  // Reading a non-existing name returns FALSE.
  $data = $this->storage
    ->read($name);
  $this
    ->assertIdentical($data, FALSE);

  // Writing data returns TRUE and the data has been written.
  $data = [
    'foo' => 'bar',
  ];
  $result = $this->storage
    ->write($name, $data);
  $this
    ->assertIdentical($result, TRUE);
  $raw_data = $this
    ->read($name);
  $this
    ->assertIdentical($raw_data, $data);

  // Checking whether an existing name exists returns TRUE.
  $this
    ->assertIdentical($this->storage
    ->exists($name), TRUE);

  // Writing the identical data again still returns TRUE.
  $result = $this->storage
    ->write($name, $data);
  $this
    ->assertIdentical($result, TRUE);

  // 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
    ->assertIdentical($raw_data, $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
    ->assertIdentical($result, TRUE);

  // Deleting a non-existing name returns FALSE.
  $result = $this->storage
    ->delete($name);
  $this
    ->assertIdentical($result, FALSE);

  // 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
    ->assertIdentical($result, TRUE);
  $this
    ->assertIdentical($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));
}