function DrupalUnitTestBaseTest::testInstallSchema

Tests expected behavior of installSchema().

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php, line 126
Contains Drupal\simpletest\Tests\DrupalUnitTestBaseTest.

Class

DrupalUnitTestBaseTest
Tests DrupalUnitTestBase functionality.

Namespace

Drupal\simpletest\Tests

Code

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

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

  // Verify that the schema is known to Schema API.
  $schema = drupal_get_schema();
  $this
    ->assertTrue($schema[$table], "'{$table}' table found in schema.");
  $schema = drupal_get_schema($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(db_table_exists($table), "'{$table}' database table not found.");
  $schema = drupal_get_schema($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(db_table_exists($table), "'{$table}' database table not found.");
  $schema = drupal_get_schema($table);
  $this
    ->assertFalse($schema, "'{$table}' table schema not found.");

  // Verify that the same table can be installed after enabling the module.
  $this
    ->enableModules(array(
    $module,
  ));
  $this
    ->installSchema($module, $table);
  $this
    ->assertTrue(db_table_exists($table), "'{$table}' database table found.");
  $schema = drupal_get_schema($table);
  $this
    ->assertTrue($schema, "'{$table}' table schema found.");
}