1 schema.test protected SchemaTestCase::assertFieldAdditionRemoval($field_spec)

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

File

core/modules/simpletest/tests/schema.test, line 276
Tests for the Database Schema API.

Class

SchemaTestCase
Unit tests for the Schema API.

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