Same name in this branch
  1. 10 core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageTest.php \Drupal\KernelTests\Core\KeyValueStore\DatabaseStorageTest
  2. 10 core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php \Drupal\KernelTests\Core\Config\Storage\DatabaseStorageTest
Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php \Drupal\KernelTests\Core\Config\Storage\DatabaseStorageTest
  2. 9 core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php \Drupal\KernelTests\Core\Config\Storage\DatabaseStorageTest

Tests DatabaseStorage operations.

@group config

Hierarchy

Expanded class hierarchy of DatabaseStorageTest

File

core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php, line 16

Namespace

Drupal\KernelTests\Core\Config\Storage
View source
class DatabaseStorageTest extends ConfigStorageTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->storage = new DatabaseStorage($this->container
      ->get('database'), 'config');
    $this->invalidStorage = new DatabaseStorage($this->container
      ->get('database'), 'invalid');
  }
  protected function read($name) {
    $data = Database::getConnection()
      ->select('config', 'c')
      ->fields('c', [
      'data',
    ])
      ->condition('name', $name)
      ->execute()
      ->fetchField();
    return unserialize($data);
  }
  protected function insert($name, $data) {
    Database::getConnection()
      ->insert('config')
      ->fields([
      'name' => $name,
      'data' => $data,
    ])
      ->execute();
  }
  protected function update($name, $data) {
    Database::getConnection()
      ->update('config')
      ->fields([
      'data' => $data,
    ])
      ->condition('name', $name)
      ->execute();
  }
  protected function delete($name) {
    Database::getConnection()
      ->delete('config')
      ->condition('name', $name)
      ->execute();
  }

  /**
   * Tests that operations throw exceptions if the query fails.
   */
  public function testExceptionIsThrownIfQueryFails() {
    $connection = Database::getConnection();
    if ($connection
      ->databaseType() === 'sqlite') {

      // See: https://www.drupal.org/project/drupal/issues/3349286
      $this
        ->markTestSkipped('SQLite cannot allow detection of exceptions due to double quoting.');
      return;
    }
    Database::getConnection()
      ->schema()
      ->dropTable('config');

    // In order to simulate database issue create a table with an incorrect
    // specification.
    $table_specification = [
      'fields' => [
        'id' => [
          'type' => 'int',
          'default' => NULL,
        ],
      ],
    ];
    Database::getConnection()
      ->schema()
      ->createTable('config', $table_specification);
    try {
      $this->storage
        ->exists('config.settings');
      $this
        ->fail('Expected exception not thrown from exists()');
    } catch (DatabaseExceptionWrapper $e) {

      // Exception was expected
    }
    try {
      $this->storage
        ->read('config.settings');
      $this
        ->fail('Expected exception not thrown from read()');
    } catch (DatabaseExceptionWrapper $e) {

      // Exception was expected
    }
    try {
      $this->storage
        ->readMultiple([
        'config.settings',
        'config.settings2',
      ]);
      $this
        ->fail('Expected exception not thrown from readMultiple()');
    } catch (DatabaseExceptionWrapper $e) {

      // Exception was expected
    }
    try {
      $this->storage
        ->write('config.settings', [
        'data' => '',
      ]);
      $this
        ->fail('Expected exception not thrown from deleteAll()');
    } catch (DatabaseExceptionWrapper $e) {

      // Exception was expected
    }
    try {
      $this->storage
        ->listAll();
      $this
        ->fail('Expected exception not thrown from listAll()');
    } catch (DatabaseExceptionWrapper $e) {

      // Exception was expected
    }
    try {
      $this->storage
        ->deleteAll();
      $this
        ->fail('Expected exception not thrown from deleteAll()');
    } catch (DatabaseExceptionWrapper $e) {

      // Exception was expected
    }
    try {
      $this->storage
        ->getAllCollectionNames();
      $this
        ->fail('Expected exception not thrown from getAllCollectionNames()');
    } catch (DatabaseExceptionWrapper $e) {

      // Exception was expected
    }
    $this
      ->assertTrue(TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigStorageTestBase::$invalidStorage protected property
ConfigStorageTestBase::$storage protected property
ConfigStorageTestBase::testCollection public function Tests that the storage supports collections.
ConfigStorageTestBase::testCRUD public function Tests storage CRUD operations.
ConfigStorageTestBase::testDataTypes public function Tests storage writing and reading data preserving data type.
ConfigStorageTestBase::testInvalidStorage public function Tests an invalid storage. 4
DatabaseStorageTest::delete protected function Overrides ConfigStorageTestBase::delete
DatabaseStorageTest::insert protected function Overrides ConfigStorageTestBase::insert
DatabaseStorageTest::read protected function Overrides ConfigStorageTestBase::read
DatabaseStorageTest::setUp protected function
DatabaseStorageTest::testExceptionIsThrownIfQueryFails public function Tests that operations throw exceptions if the query fails.
DatabaseStorageTest::update protected function Overrides ConfigStorageTestBase::update