From 79925d2c2979a233cc43998a5a3be1f7156edc61 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 19 Apr 2022 09:14:22 +0100 Subject: [PATCH] Issue #3269091 by gambry, yogeshmpawar, jonathanshaw, joachim, alexpott: Undocumented behaviour for Schema::findTables() when an underscore is used --- core/lib/Drupal/Core/Database/Schema.php | 7 ++- .../KernelTests/Core/Database/SchemaTest.php | 55 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Database/Schema.php b/core/lib/Drupal/Core/Database/Schema.php index ed3a04eecb7..9f14f359214 100644 --- a/core/lib/Drupal/Core/Database/Schema.php +++ b/core/lib/Drupal/Core/Database/Schema.php @@ -180,7 +180,12 @@ abstract class Schema implements PlaceholderInterface { * Finds all tables that are like the specified base table name. * * @param string $table_expression - * An SQL expression, for example "cache_%" (without the quotes). + * A case-insensitive pattern against which table names are compared. Both + * '_' and '%' are treated like wildcards in MySQL 'LIKE' expressions, where + * '_' matches any single character and '%' matches an arbitrary number of + * characters (including zero characters). So 'foo%bar' matches table names + * like 'foobar', 'fooXBar', 'fooXBaR', or 'fooXxBar'; whereas 'foo_bar' + * matches 'fooXBar' and 'fooXBaR' but not 'fooBar' or 'fooXxxBar'. * * @return array * Both the keys and the values are the matching tables. diff --git a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php index b0ffb5d58f7..a1ccad0efe1 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php @@ -1307,6 +1307,61 @@ class SchemaTest extends KernelTestBase { 'test_2_table', ]; $this->assertEquals($expected, $tables, 'Two tables were found.'); + + // Check '_' and '%' wildcards. + $test_schema->createTable('test3table', $table_specification); + $test_schema->createTable('test4', $table_specification); + $test_schema->createTable('testTable', $table_specification); + $test_schema->createTable('test', $table_specification); + + $tables = $test_schema->findTables('test%'); + sort($tables); + $expected = [ + 'test', + 'test3table', + 'test4', + 'testTable', + 'test_1_table', + 'test_2_table', + ]; + $this->assertEquals($expected, $tables, 'All "test" prefixed tables were found.'); + + $tables = $test_schema->findTables('test_%'); + sort($tables); + $expected = [ + 'test3table', + 'test4', + 'testTable', + 'test_1_table', + 'test_2_table', + ]; + $this->assertEquals($expected, $tables, 'All "/^test..*?/" tables were found.'); + + $tables = $test_schema->findTables('test%table'); + sort($tables); + $expected = [ + 'test3table', + 'testTable', + 'test_1_table', + 'test_2_table', + ]; + $this->assertEquals($expected, $tables, 'All "/^test.*?table/" tables were found.'); + + $tables = $test_schema->findTables('test_%table'); + sort($tables); + $expected = [ + 'test3table', + 'test_1_table', + 'test_2_table', + ]; + $this->assertEquals($expected, $tables, 'All "/^test..*?table/" tables were found.'); + + $tables = $test_schema->findTables('test_'); + sort($tables); + $expected = [ + 'test4', + ]; + $this->assertEquals($expected, $tables, 'All "/^test./" tables were found.'); } /**