You are here

public function SchemaTest::testFindPrimaryKeyColumns in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php \Drupal\KernelTests\Core\Database\SchemaTest::testFindPrimaryKeyColumns()
  2. 9 core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php \Drupal\KernelTests\Core\Database\SchemaTest::testFindPrimaryKeyColumns()

@covers ::findPrimaryKeyColumns

File

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

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testFindPrimaryKeyColumns() {
  $method = new \ReflectionMethod(get_class($this->schema), 'findPrimaryKeyColumns');
  $method
    ->setAccessible(TRUE);

  // Test with single column primary key.
  $this->schema
    ->createTable('table_with_pk_0', [
    'description' => 'Table with primary key.',
    'fields' => [
      'id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'test_field' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'id',
    ],
  ]);
  $this
    ->assertSame([
    'id',
  ], $method
    ->invoke($this->schema, 'table_with_pk_0'));

  // Test with multiple column primary key.
  $this->schema
    ->createTable('table_with_pk_1', [
    'description' => 'Table with primary key with multiple columns.',
    'fields' => [
      'id0' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'id1' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'test_field' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'id0',
      'id1',
    ],
  ]);
  $this
    ->assertSame([
    'id0',
    'id1',
  ], $method
    ->invoke($this->schema, 'table_with_pk_1'));

  // Test with multiple column primary key and not being the first column of
  // the table definition.
  $this->schema
    ->createTable('table_with_pk_2', [
    'description' => 'Table with primary key with multiple columns at the end and in reverted sequence.',
    'fields' => [
      'test_field_1' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'test_field_2' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'id3' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'id4' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'id4',
      'id3',
    ],
  ]);
  $this
    ->assertSame([
    'id4',
    'id3',
  ], $method
    ->invoke($this->schema, 'table_with_pk_2'));

  // Test with multiple column primary key in a different order. For the
  // PostgreSQL and the SQLite drivers is sorting used to get the primary key
  // columns in the right order.
  $this->schema
    ->createTable('table_with_pk_3', [
    'description' => 'Table with primary key with multiple columns at the end and in reverted sequence.',
    'fields' => [
      'test_field_1' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'test_field_2' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'id3' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'id4' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'primary key' => [
      'id3',
      'test_field_2',
      'id4',
    ],
  ]);
  $this
    ->assertSame([
    'id3',
    'test_field_2',
    'id4',
  ], $method
    ->invoke($this->schema, 'table_with_pk_3'));

  // Test with table without a primary key.
  $this->schema
    ->createTable('table_without_pk_1', [
    'description' => 'Table without primary key.',
    'fields' => [
      'id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'test_field' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
  ]);
  $this
    ->assertSame([], $method
    ->invoke($this->schema, 'table_without_pk_1'));

  // Test with table with an empty primary key.
  $this->schema
    ->createTable('table_without_pk_2', [
    'description' => 'Table without primary key.',
    'fields' => [
      'id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'test_field' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'primary key' => [],
  ]);
  $this
    ->assertSame([], $method
    ->invoke($this->schema, 'table_without_pk_2'));

  // Test with non existing table.
  $this
    ->assertFalse($method
    ->invoke($this->schema, 'non_existing_table'));
}