function SqlContentEntityStorageSchemaTest::testonEntityTypeUpdateWithNewIndex

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageSchemaTest::testonEntityTypeUpdateWithNewIndex()
  2. 10 core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageSchemaTest::testonEntityTypeUpdateWithNewIndex()
  3. 11.x core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageSchemaTest::testonEntityTypeUpdateWithNewIndex()

::onEntityTypeUpdate.

File

core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php, line 1476

Class

SqlContentEntityStorageSchemaTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Sql%21SqlContentEntityStorageSchema.php/class/SqlContentEntityStorageSchema/9" title="Defines a schema handler that supports revisionable, translatable entities." class="local">\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema</a> @group Entity

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testonEntityTypeUpdateWithNewIndex() {
    $this->entityType = $original_entity_type = new ContentEntityType([
        'id' => 'entity_test',
        'entity_keys' => [
            'id' => 'id',
        ],
    ]);
    // Add a field with a really long index.
    $this->setUpStorageDefinition('long_index_name', [
        'columns' => [
            'long_index_name' => [
                'type' => 'int',
            ],
        ],
        'indexes' => [
            'long_index_name_really_long_long_name' => [
                [
                    'long_index_name',
                    10,
                ],
            ],
        ],
    ]);
    $expected = [
        'entity_test' => [
            'description' => 'The base table for entity_test entities.',
            'fields' => [
                'id' => [
                    'type' => 'serial',
                    'not null' => TRUE,
                ],
                'long_index_name' => [
                    'type' => 'int',
                    'not null' => FALSE,
                ],
            ],
            'indexes' => [
                'entity_test__b588603cb9' => [
                    [
                        'long_index_name',
                        10,
                    ],
                ],
            ],
        ],
    ];
    $this->setUpStorageSchema($expected);
    $table_mapping = new TestSqlContentDefaultTableMapping($this->entityType, $this->storageDefinitions);
    $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
    $table_mapping->setExtraColumns('entity_test', [
        'default_langcode',
    ]);
    $this->storageSchema
        ->expects($this->any())
        ->method('getTableMapping')
        ->willReturn($table_mapping);
    $this->storageSchema
        ->expects($this->any())
        ->method('loadEntitySchemaData')
        ->willReturn([
        'entity_test' => [
            'indexes' => [
                // A changed index definition.
'entity_test__b588603cb9' => [
                    'longer_index_name',
                ],
                // An index that has been removed.
'entity_test__removed_field' => [
                    'removed_field',
                ],
            ],
        ],
    ]);
    // The original indexes should be dropped before the new one is added.
    $this->dbSchemaHandler
        ->expects($this->exactly(3))
        ->method('dropIndex')
        ->withConsecutive([
        'entity_test',
        'entity_test__b588603cb9',
    ], [
        'entity_test',
        'entity_test__removed_field',
    ]);
    $this->dbSchemaHandler
        ->expects($this->atLeastOnce())
        ->method('fieldExists')
        ->willReturn(TRUE);
    $this->dbSchemaHandler
        ->expects($this->atLeastOnce())
        ->method('addIndex')
        ->with('entity_test', 'entity_test__b588603cb9', [
        [
            'long_index_name',
            10,
        ],
    ], $this->callback(function ($actual_value) use ($expected) {
        $this->assertEquals($expected['entity_test']['indexes'], $actual_value['indexes']);
        $this->assertEquals($expected['entity_test']['fields'], $actual_value['fields']);
        // If the parameters don't match, the assertions above will throw an
        // exception.
        return TRUE;
    }));
    $this->assertNull($this->storageSchema
        ->onEntityTypeUpdate($this->entityType, $original_entity_type));
}

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