function SchemaTest::testFindTables

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php \Drupal\KernelTests\Core\Database\SchemaTest::testFindTables()

Tests the findTables() method.

File

core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php, line 1197

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\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');
    $test_schema = Database::getConnection()->schema();
    // Create the tables.
    $table_specification = [
        'description' => 'Test table.',
        'fields' => [
            'id' => [
                'type' => 'int',
                'default' => NULL,
            ],
        ],
    ];
    $test_schema->createTable('test_1_table', $table_specification);
    $test_schema->createTable('test_2_table', $table_specification);
    $test_schema->createTable('the_third_table', $table_specification);
    // Check the "all tables" syntax.
    $tables = $test_schema->findTables('%');
    sort($tables);
    $expected = [
        // The 'config' table is added by
        // \Drupal\KernelTests\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 = $test_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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.