function FieldSqlStorageTest::testFieldUpdateIndexesWithData

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldUpdateIndexesWithData()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldUpdateIndexesWithData()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldUpdateIndexesWithData()

Tests adding and removing indexes while data is present.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php, line 392

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldUpdateIndexesWithData() {
    // Create a decimal field.
    $field_name = 'testfield';
    $entity_type = 'entity_test_rev';
    $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => $entity_type,
        'type' => 'text',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => $entity_type,
    ]);
    $field->save();
    $tables = [
        $this->tableMapping
            ->getDedicatedDataTableName($field_storage),
        $this->tableMapping
            ->getDedicatedRevisionTableName($field_storage),
    ];
    // Verify the indexes we will create do not exist yet.
    foreach ($tables as $table) {
        $this->assertFalse(Database::getConnection()->schema()
            ->indexExists($table, 'value'), 'No index named value exists in $table');
        $this->assertFalse(Database::getConnection()->schema()
            ->indexExists($table, 'value_format'), 'No index named value_format exists in $table');
    }
    // Add data so the table cannot be dropped.
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        'id' => 1,
        'revision_id' => 1,
    ]);
    $entity->{$field_name}->value = 'field data';
    $entity->enforceIsNew();
    $entity->save();
    // Add an index.
    $field_storage->setIndexes([
        'value' => [
            [
                'value',
                255,
            ],
        ],
    ]);
    $field_storage->save();
    foreach ($tables as $table) {
        $this->assertTrue(Database::getConnection()->schema()
            ->indexExists($table, "{$field_name}_value"), "Index on value created in @table", [
            '@table' => $table,
        ]);
    }
    // Add a different index, removing the existing custom one.
    $field_storage->setIndexes([
        'value_format' => [
            [
                'value',
                127,
            ],
            [
                'format',
                127,
            ],
        ],
    ]);
    $field_storage->save();
    foreach ($tables as $table) {
        $this->assertTrue(Database::getConnection()->schema()
            ->indexExists($table, "{$field_name}_value_format"), "Index on value_format created in @table", [
            '@table' => $table,
        ]);
        $this->assertFalse(Database::getConnection()->schema()
            ->indexExists($table, "{$field_name}_value"), "Index on value removed in @table", [
            '@table' => $table,
        ]);
    }
    // Verify that the tables were not dropped in the process.
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->load(1);
    $this->assertEquals('field data', $entity->{$field_name}->value);
}

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