function DriverSpecificSchemaTestBase::assertFieldChange

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php \Drupal\KernelTests\Core\Database\DriverSpecificSchemaTestBase::assertFieldChange()

Asserts that a field can be changed from one spec to another.

@internal

Parameters

array $old_spec: The beginning field specification.

array $new_spec: The ending field specification.

mixed $test_data: (optional) A test value to insert and test, if specified.

1 call to DriverSpecificSchemaTestBase::assertFieldChange()
DriverSpecificSchemaTestBase::testSchemaChangeFieldDefaultInitial in core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php
Tests changing columns between types with default and initial values.

File

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php, line 845

Class

DriverSpecificSchemaTestBase
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function assertFieldChange(array $old_spec, array $new_spec, $test_data = NULL) : void {
    $table_name = 'test_table_' . $this->counter++;
    $table_spec = [
        'fields' => [
            'serial_column' => [
                'type' => 'serial',
                'unsigned' => TRUE,
                'not null' => TRUE,
            ],
            'test_field' => $old_spec,
        ],
        'primary key' => [
            'serial_column',
        ],
    ];
    $this->schema
        ->createTable($table_name, $table_spec);
    // Check the characteristics of the field.
    $this->assertFieldCharacteristics($table_name, 'test_field', $old_spec);
    // Remove inserted rows.
    $this->connection
        ->truncate($table_name)
        ->execute();
    if ($test_data) {
        $id = $this->connection
            ->insert($table_name)
            ->fields([
            'test_field',
        ], [
            $test_data,
        ])
            ->execute();
    }
    // Change the field.
    $this->schema
        ->changeField($table_name, 'test_field', 'test_field', $new_spec);
    if ($test_data) {
        $field_value = $this->connection
            ->select($table_name)
            ->fields($table_name, [
            'test_field',
        ])
            ->condition('serial_column', $id)
            ->execute()
            ->fetchField();
        $this->assertSame($test_data, $field_value);
    }
    // Check the field was changed.
    $this->assertFieldCharacteristics($table_name, 'test_field', $new_spec);
    // Clean-up.
    $this->schema
        ->dropTable($table_name);
}

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