You are here

public function KernelTestBaseTest::testInstallSchema in Drupal 8

Tests expected behavior of installSchema().

File

core/modules/simpletest/src/Tests/KernelTestBaseTest.php, line 154

Class

KernelTestBaseTest
Tests KernelTestBase functionality.

Namespace

Drupal\simpletest\Tests

Code

public function testInstallSchema() {
  $module = 'entity_test';
  $table = 'entity_test_example';

  // Verify that we can install a table from the module schema.
  $this
    ->installSchema($module, $table);
  $this
    ->assertTrue(Database::getConnection()
    ->schema()
    ->tableExists($table), "'{$table}' database table found.");

  // Verify that the schema is known to Schema API.
  $schema = drupal_get_module_schema($module, $table);
  $this
    ->assertTrue($schema, "'{$table}' table schema found.");

  // Verify that a unknown table from an enabled module throws an error.
  $table = 'unknown_entity_test_table';
  try {
    $this
      ->installSchema($module, $table);
    $this
      ->fail('Exception for non-retrievable schema found.');
  } catch (\Exception $e) {
    $this
      ->pass('Exception for non-retrievable schema found.');
  }
  $this
    ->assertFalse(Database::getConnection()
    ->schema()
    ->tableExists($table), "'{$table}' database table not found.");
  $schema = drupal_get_module_schema($module, $table);
  $this
    ->assertFalse($schema, "'{$table}' table schema not found.");

  // Verify that a table from a unknown module cannot be installed.
  $module = 'database_test';
  $table = 'test';
  try {
    $this
      ->installSchema($module, $table);
    $this
      ->fail('Exception for non-retrievable schema found.');
  } catch (\Exception $e) {
    $this
      ->pass('Exception for non-retrievable schema found.');
  }
  $this
    ->assertFalse(Database::getConnection()
    ->schema()
    ->tableExists($table), "'{$table}' database table not found.");
  $schema = drupal_get_module_schema($module, $table);
  $this
    ->assertTrue($schema, "'{$table}' table schema found.");

  // Verify that the same table can be installed after enabling the module.
  $this
    ->enableModules([
    $module,
  ]);
  $this
    ->installSchema($module, $table);
  $this
    ->assertTrue(Database::getConnection()
    ->schema()
    ->tableExists($table), "'{$table}' database table found.");
  $schema = drupal_get_module_schema($module, $table);
  $this
    ->assertTrue($schema, "'{$table}' table schema found.");
}