You are here

protected function SchemaTest::assertFieldChange in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SchemaTest.php \Drupal\system\Tests\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::testSchemaChangeField in core/modules/system/src/Tests/Database/SchemaTest.php
Tests changing columns between types.

File

core/modules/system/src/Tests/Database/SchemaTest.php, line 695
Contains \Drupal\system\Tests\Database\SchemaTest.

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\system\Tests\Database

Code

protected function assertFieldChange($old_spec, $new_spec, $test_data = NULL) {
  $table_name = 'test_table_' . $this->counter++;
  $table_spec = array(
    'fields' => array(
      'serial_column' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'test_field' => $old_spec,
    ),
    'primary key' => array(
      'serial_column',
    ),
  );
  db_create_table($table_name, $table_spec);
  $this
    ->pass(format_string('Table %table created.', array(
    '%table' => $table_name,
  )));

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

  // Remove inserted rows.
  db_truncate($table_name)
    ->execute();
  if ($test_data) {
    $id = db_insert($table_name)
      ->fields([
      'test_field',
    ], [
      $test_data,
    ])
      ->execute();
  }

  // Change the field.
  db_change_field($table_name, 'test_field', 'test_field', $new_spec);
  if ($test_data) {
    $field_value = db_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.
  db_drop_table($table_name);
}