You are here

protected function SchemaTest::assertFieldCharacteristics in Drupal 10

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

Asserts that a newly added field has the correct characteristics.

@internal

File

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

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function assertFieldCharacteristics(string $table_name, string $field_name, array $field_spec) : void {

  // 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.');
  }
}