You are here

protected function SchemaTest::assertFieldAdditionRemoval 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::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 core/modules/system/src/Tests/Database/SchemaTest.php
Tests adding columns to an existing table.

File

core/modules/system/src/Tests/Database/SchemaTest.php, line 558
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 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');

  // Add back the field and then try to delete a field which is also a primary
  // key.
  db_add_field($table_name, 'test_field', $field_spec);
  db_drop_field($table_name, 'serial_column');
  db_drop_table($table_name);
}