function DriverSpecificSchemaTestBase::assertFieldCharacteristics

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php \Drupal\KernelTests\Core\Database\DriverSpecificSchemaTestBase::assertFieldCharacteristics()

Asserts that a newly added field has the correct characteristics.

@internal

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

File

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php, line 509

Class

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.