function SchemaTest::assertFieldChange

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

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

Parameters

$old_spec: The beginning field specification.

$new_spec: The ending field specification.

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

File

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

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function assertFieldChange($old_spec, $new_spec, $test_data = NULL) {
    $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->assertIdentical($field_value, $test_data);
    }
    // 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.