You are here

protected function SchemaTest::assertFieldCharacteristics in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php \Drupal\KernelTests\Core\Database\SchemaTest::assertFieldCharacteristics()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php \Drupal\KernelTests\Core\Database\SchemaTest::assertFieldCharacteristics()

Asserts that a newly added field has the correct characteristics.

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

File

core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php, line 741

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\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 = $this->connection
      ->select($table_name)
      ->fields($table_name, [
      'serial_column',
    ])
      ->condition($field_name, $field_spec['initial'], '<>')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertEquals(0, $count, 'Initial values filled out.');
  }

  // Check that the initial value from another field has been registered.
  if (isset($field_spec['initial_from_field']) && !isset($field_spec['initial'])) {

    // There should be no row with a value different than
    // $field_spec['initial_from_field'].
    $count = $this->connection
      ->select($table_name)
      ->fields($table_name, [
      'serial_column',
    ])
      ->where("[{$table_name}].[{$field_spec['initial_from_field']}] <> [{$table_name}].[{$field_name}]")
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertEquals(0, $count, 'Initial values from another field filled out.');
  }
  elseif (isset($field_spec['initial_from_field']) && isset($field_spec['initial'])) {

    // There should be no row with a value different than '100'.
    $count = $this->connection
      ->select($table_name)
      ->fields($table_name, [
      'serial_column',
    ])
      ->condition($field_name, 100, '<>')
      ->countQuery()
      ->execute()
      ->fetchField();
    $this
      ->assertEquals(0, $count, 'Initial values from another field or a default value 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 = $this->connection
      ->insert($table_name)
      ->useDefaults([
      'serial_column',
    ])
      ->execute();
    $field_value = $this->connection
      ->select($table_name)
      ->fields($table_name, [
      $field_name,
    ])
      ->condition('serial_column', $id)
      ->execute()
      ->fetchField();
    $this
      ->assertEquals($field_spec['default'], $field_value, 'Default value registered.');
  }
}