protected function 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.

Parameters

$field_spec: The schema specification of the field.

1 call to SchemaTest::assertFieldAdditionRemoval()
SchemaTest::testSchemaAddField in drupal/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php
Tests adding columns to an existing table.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php, line 320
Contains Drupal\system\Tests\Database\SchemaTest.

Class

SchemaTest
Tests the Schema API.

Namespace

Drupal\system\Tests\Database

Code

protected function assertFieldAdditionRemoval($field_spec) {

  // Try creating the field on a new table.
  $table_name = 'test_table_' . $this->counter++;
  $table_spec = array(
    'fields' => array(
      'serial_column' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'test_field' => $field_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', $field_spec);

  // Clean-up.
  db_drop_table($table_name);

  // Try adding a field to an existing table.
  $table_name = 'test_table_' . $this->counter++;
  $table_spec = array(
    'fields' => array(
      'serial_column' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'serial_column',
    ),
  );
  db_create_table($table_name, $table_spec);
  $this
    ->pass(format_string('Table %table created.', array(
    '%table' => $table_name,
  )));

  // Insert some rows to the table to test the handling of initial values.
  for ($i = 0; $i < 3; $i++) {
    db_insert($table_name)
      ->useDefaults(array(
      'serial_column',
    ))
      ->execute();
  }
  db_add_field($table_name, 'test_field', $field_spec);
  $this
    ->pass(format_string('Column %column created.', array(
    '%column' => 'test_field',
  )));

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

  // Clean-up.
  db_drop_field($table_name, 'test_field');
  db_drop_table($table_name);
}