diff --git a/core/tests/Drupal/KernelTests/Core/Database/AssertSchemaTrait.php b/core/tests/Drupal/KernelTests/Core/Database/AssertSchemaTrait.php new file mode 100644 index 0000000000..20f8b04cd6 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Database/AssertSchemaTrait.php @@ -0,0 +1,54 @@ +databaseType(); + + switch ($db_type) { + case 'mysql': + $result = Database::getConnection()->query("SHOW KEYS FROM {" . $table_name . "} WHERE Key_name = 'PRIMARY'")->fetchAllAssoc('Column_name'); + $this->assertSame($primary_key, array_keys($result)); + + break; + case 'pgsql': + $result = Database::getConnection()->query("SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type + FROM pg_index i + JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) + WHERE i.indrelid = '{" . $table_name . "}'::regclass AND i.indisprimary") + ->fetchAllAssoc('attname'); + $this->assertSame($primary_key, array_keys($result)); + + break; + case 'sqlite': + // For SQLite we need access to the protected + // \Drupal\Core\Database\Driver\sqlite\Schema::introspectSchema() method + // because we have no other way of getting the table prefixes needed for + // running a straight PRAGMA query. + $schema_object = Database::getConnection()->schema(); + $reflection = new \ReflectionMethod($schema_object, 'introspectSchema'); + $reflection->setAccessible(TRUE); + + $table_info = $reflection->invoke($schema_object, $table_name); + $this->assertSame($primary_key, $table_info['primary key']); + + break; + } + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php index e239098715..608c748606 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php @@ -16,6 +16,8 @@ */ class SchemaTest extends KernelTestBase { + use AssertSchemaTrait; + /** * A global counter for table and field creation. */ @@ -826,46 +828,4 @@ public function testFindTables() { Database::setActiveConnection('default'); } - /** - * Tests the primary keys of a table. - * - * @param string $table_name - * The name of the table to check. - * @param array $primary_key - * The expected key column specifier for a table's primary key. - */ - protected function assertPrimaryKeyColumns($table_name, array $primary_key = []) { - $db_type = Database::getConnection()->databaseType(); - - switch ($db_type) { - case 'mysql': - $result = Database::getConnection()->query("SHOW KEYS FROM {" . $table_name . "} WHERE Key_name = 'PRIMARY'")->fetchAllAssoc('Column_name'); - $this->assertSame($primary_key, array_keys($result)); - - break; - case 'pgsql': - $result = Database::getConnection()->query("SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type - FROM pg_index i - JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) - WHERE i.indrelid = '{" . $table_name . "}'::regclass AND i.indisprimary") - ->fetchAllAssoc('attname'); - $this->assertSame($primary_key, array_keys($result)); - - break; - case 'sqlite': - // For SQLite we need access to the protected - // \Drupal\Core\Database\Driver\sqlite\Schema::introspectSchema() method - // because we have no other way of getting the table prefixes needed for - // running a straight PRAGMA query. - $schema_object = Database::getConnection()->schema(); - $reflection = new \ReflectionMethod($schema_object, 'introspectSchema'); - $reflection->setAccessible(TRUE); - - $table_info = $reflection->invoke($schema_object, $table_name); - $this->assertSame($primary_key, $table_info['primary key']); - - break; - } - } - } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php index 3e285ff69b..98085eec92 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntitySchemaTest.php @@ -3,14 +3,18 @@ namespace Drupal\KernelTests\Core\Entity; use Drupal\Component\Utility\SafeMarkup; +use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\KernelTests\Core\Database\AssertSchemaTrait; /** - * Tests adding a custom bundle field. + * Tests the default entity storage schema handler. * - * @group system + * @group Entity */ class EntitySchemaTest extends EntityKernelTestBase { + use AssertSchemaTrait; + /** * The database connection used. * @@ -109,6 +113,32 @@ public function testEntitySchemaUpdate() { $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), SafeMarkup::format('Field schema correct for the @table table.', ['@table' => $table])); } + /** + * Tests deleting and creating a field that is part of a primary key. + */ + public function testPrimaryKeyUpdate() { + /* @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $update_manager */ + $update_manager = $this->container->get('entity.definition_update_manager'); + + $entity_type_id = 'entity_test'; + $field_name = 'id'; + $provider = 'entity_test'; + + $field = BaseFieldDefinition::create('string') + ->setLabel('ID') + ->setReadOnly(TRUE) + ->setSetting('is_ascii', TRUE) + ->setName($field_name) + ->setTargetEntityTypeId($entity_type_id) + ->setProvider($provider); + + $this->assertPrimaryKeyColumns($entity_type_id, [$field_name]); + $update_manager->uninstallFieldStorageDefinition($field); + $this->assertPrimaryKeyColumns($entity_type_id, []); + $update_manager->installFieldStorageDefinition($field_name, $entity_type_id, $provider, $field); + $this->assertPrimaryKeyColumns($entity_type_id, [$field_name]); + } + /** * {@inheritdoc} */