You are here

public function FieldStorageCrudTest::testIndexes in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php \Drupal\Tests\field\Kernel\FieldStorageCrudTest::testIndexes()

Tests creation of indexes on data column.

File

core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php, line 244

Class

FieldStorageCrudTest
Tests field storage create, read, update, and delete.

Namespace

Drupal\Tests\field\Kernel

Code

public function testIndexes() {

  // Check that indexes specified by the field type are used by default.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_1',
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ]);
  $field_storage
    ->save();
  $field_storage = FieldStorageConfig::load($field_storage
    ->id());
  $schema = $field_storage
    ->getSchema();
  $expected_indexes = [
    'value' => [
      'value',
    ],
  ];
  $this
    ->assertEquals($expected_indexes, $schema['indexes'], 'Field type indexes saved by default');

  // Check that indexes specified by the field definition override the field
  // type indexes.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_2',
    'entity_type' => 'entity_test',
    'type' => 'test_field',
    'indexes' => [
      'value' => [],
    ],
  ]);
  $field_storage
    ->save();
  $field_storage = FieldStorageConfig::load($field_storage
    ->id());
  $schema = $field_storage
    ->getSchema();
  $expected_indexes = [
    'value' => [],
  ];
  $this
    ->assertEquals($expected_indexes, $schema['indexes'], 'Field definition indexes override field type indexes');

  // Check that indexes specified by the field definition add to the field
  // type indexes.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_3',
    'entity_type' => 'entity_test',
    'type' => 'test_field',
    'indexes' => [
      'value_2' => [
        'value',
      ],
    ],
  ]);
  $field_storage
    ->save();
  $id = $field_storage
    ->id();
  $field_storage = FieldStorageConfig::load($id);
  $schema = $field_storage
    ->getSchema();
  $expected_indexes = [
    'value' => [
      'value',
    ],
    'value_2' => [
      'value',
    ],
  ];
  $this
    ->assertEquals($expected_indexes, $schema['indexes'], 'Field definition indexes are merged with field type indexes');
}