function SchemaTest::assertFieldAdditionRemoval

Same name in other branches
  1. 8.9.x 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.

@internal

Parameters

array $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 546

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function assertFieldAdditionRemoval(array $field_spec) : void {
    // 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);
}

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