You are here

protected function SchemaTest::assertFieldCharacteristics 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::assertFieldCharacteristics()

Asserts that a newly added field has the correct characteristics.

2 calls to SchemaTest::assertFieldCharacteristics()
SchemaTest::assertFieldAdditionRemoval in core/modules/system/src/Tests/Database/SchemaTest.php
Asserts that a given field can be added and removed from a table.
SchemaTest::assertFieldChange in core/modules/system/src/Tests/Database/SchemaTest.php
Asserts that a field can be changed from one spec to another.

File

core/modules/system/src/Tests/Database/SchemaTest.php, line 614
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 assertFieldCharacteristics($table_name, $field_name, $field_spec) {

  // Check that the initial value has been registered.
  if (isset($field_spec['initial'])) {

    // There should be no row with a value different then $field_spec['initial'].
    $count = db_select($table_name)
      ->fields($table_name, array(
      'serial_column',
    ))
      ->condition($field_name, $field_spec['initial'], '<>')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertEqual($count, 0, 'Initial values filled out.');
  }

  // Check that the default value has been registered.
  if (isset($field_spec['default'])) {

    // Try inserting a row, and check the resulting value of the new column.
    $id = db_insert($table_name)
      ->useDefaults(array(
      'serial_column',
    ))
      ->execute();
    $field_value = db_select($table_name)
      ->fields($table_name, array(
      $field_name,
    ))
      ->condition('serial_column', $id)
      ->execute()
      ->fetchField();
    $this
      ->assertEqual($field_value, $field_spec['default'], 'Default value registered.');
  }
}