You are here

public function SchemaTest::testFindTables in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SchemaTest.php \Drupal\system\Tests\Database\SchemaTest::testFindTables()

Tests the findTables() method.

File

core/modules/system/src/Tests/Database/SchemaTest.php, line 741
Contains \Drupal\system\Tests\Database\SchemaTest.

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\system\Tests\Database

Code

public function testFindTables() {

  // We will be testing with three tables, two of them using the default
  // prefix and the third one with an individually specified prefix.
  // Set up a new connection with different connection info.
  $connection_info = Database::getConnectionInfo();

  // Add per-table prefix to the second table.
  $new_connection_info = $connection_info['default'];
  $new_connection_info['prefix']['test_2_table'] = $new_connection_info['prefix']['default'] . '_shared_';
  Database::addConnectionInfo('test', 'default', $new_connection_info);
  Database::setActiveConnection('test');

  // Create the tables.
  $table_specification = [
    'description' => 'Test table.',
    'fields' => [
      'id' => [
        'type' => 'int',
        'default' => NULL,
      ],
    ],
  ];
  Database::getConnection()
    ->schema()
    ->createTable('test_1_table', $table_specification);
  Database::getConnection()
    ->schema()
    ->createTable('test_2_table', $table_specification);
  Database::getConnection()
    ->schema()
    ->createTable('the_third_table', $table_specification);

  // Check the "all tables" syntax.
  $tables = Database::getConnection()
    ->schema()
    ->findTables('%');
  sort($tables);
  $expected = [
    // The 'config' table is added by
    // \Drupal\simpletest\KernelTestBase::containerBuild().
    'config',
    'test_1_table',
    // This table uses a per-table prefix, yet it is returned as un-prefixed.
    'test_2_table',
    'the_third_table',
  ];
  $this
    ->assertEqual($tables, $expected, 'All tables were found.');

  // Check the restrictive syntax.
  $tables = Database::getConnection()
    ->schema()
    ->findTables('test_%');
  sort($tables);
  $expected = [
    'test_1_table',
    'test_2_table',
  ];
  $this
    ->assertEqual($tables, $expected, 'Two tables were found.');

  // Go back to the initial connection.
  Database::setActiveConnection('default');
}