You are here

protected function SchemaTest::assertFieldChange 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::assertFieldChange()
  2. 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.

@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.

File

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

Class

SchemaTest
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);
}