You are here

protected function SchemaTest::assertFieldAdditionRemoval in Drupal 9

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

Asserts that a given field can be added and removed from a table.

The addition test covers both defining a field of a given specification when initially creating at table and extending an existing table.

Parameters

$field_spec: The schema specification of the field.

1 call to SchemaTest::assertFieldAdditionRemoval()
SchemaTest::testSchemaAddFieldDefaultInitial in core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
Tests adding columns to an existing table with default and initial value.

File

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

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function assertFieldAdditionRemoval($field_spec) {

  // Try creating the field on a new table.
  $table_name = 'test_table_' . $this->counter++;
  $table_spec = [
    'fields' => [
      'serial_column' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'test_nullable_field' => [
        'type' => 'int',
        'not null' => FALSE,
      ],
      'test_field' => $field_spec,
    ],
    'primary key' => [
      'serial_column',
    ],
  ];
  $this->schema
    ->createTable($table_name, $table_spec);

  // Check the characteristics of the field.
  $this
    ->assertFieldCharacteristics($table_name, 'test_field', $field_spec);

  // Clean-up.
  $this->schema
    ->dropTable($table_name);

  // Try adding a field to an existing table.
  $table_name = 'test_table_' . $this->counter++;
  $table_spec = [
    'fields' => [
      'serial_column' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'test_nullable_field' => [
        'type' => 'int',
        'not null' => FALSE,
      ],
    ],
    'primary key' => [
      'serial_column',
    ],
  ];
  $this->schema
    ->createTable($table_name, $table_spec);

  // Insert some rows to the table to test the handling of initial values.
  for ($i = 0; $i < 3; $i++) {
    $this->connection
      ->insert($table_name)
      ->useDefaults([
      'serial_column',
    ])
      ->fields([
      'test_nullable_field' => 100,
    ])
      ->execute();
  }

  // Add another row with no value for the 'test_nullable_field' column.
  $this->connection
    ->insert($table_name)
    ->useDefaults([
    'serial_column',
  ])
    ->execute();
  $this->schema
    ->addField($table_name, 'test_field', $field_spec);

  // Check the characteristics of the field.
  $this
    ->assertFieldCharacteristics($table_name, 'test_field', $field_spec);

  // Clean-up.
  $this->schema
    ->dropField($table_name, 'test_field');

  // Add back the field and then try to delete a field which is also a primary
  // key.
  $this->schema
    ->addField($table_name, 'test_field', $field_spec);
  $this->schema
    ->dropField($table_name, 'serial_column');
  $this->schema
    ->dropTable($table_name);
}