You are here

public function IndexChangesTest::testPropertyBundleRemoved in Search API 8

Tests correct reaction when a bundle containing a property is removed.

File

tests/src/Kernel/Index/IndexChangesTest.php, line 341

Class

IndexChangesTest
Tests correct reactions to changes for the index.

Namespace

Drupal\Tests\search_api\Kernel\Index

Code

public function testPropertyBundleRemoved() {
  entity_test_create_bundle('bundle1', NULL, 'entity_test_mulrev_changed');
  entity_test_create_bundle('bundle2', NULL, 'entity_test_mulrev_changed');
  $this
    ->enableModules([
    'field',
    'text',
  ]);
  $this
    ->installEntitySchema('field_storage_config');
  $this
    ->installEntitySchema('field_config');
  $this
    ->installConfig('field');
  FieldStorageConfig::create([
    'field_name' => 'field1',
    'entity_type' => 'entity_test_mulrev_changed',
    'type' => 'text',
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field1',
    'entity_type' => 'entity_test_mulrev_changed',
    'bundle' => 'bundle1',
  ])
    ->save();
  FieldStorageConfig::create([
    'field_name' => 'field2',
    'entity_type' => 'entity_test_mulrev_changed',
    'type' => 'text',
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field2',
    'entity_type' => 'entity_test_mulrev_changed',
    'bundle' => 'bundle2',
  ])
    ->save();
  $datasource_id = 'entity:entity_test_mulrev_changed';
  $datasource = $this->container
    ->get('plugin.manager.search_api.datasource')
    ->createInstance($datasource_id, [
    '#index' => $this->index,
    'bundles' => [
      'default' => TRUE,
      'selected' => [],
    ],
  ]);
  $this->index
    ->setDatasources([
    $datasource_id => $datasource,
  ]);
  $fields_helper = \Drupal::getContainer()
    ->get('search_api.fields_helper');
  $info = [
    'datasource_id' => $datasource_id,
    'property_path' => 'field1',
  ];
  $this->index
    ->addField($fields_helper
    ->createField($this->index, 'field1', $info));
  $info = [
    'datasource_id' => $datasource_id,
    'property_path' => 'field2',
  ];
  $this->index
    ->addField($fields_helper
    ->createField($this->index, 'field2', $info));
  $this->index
    ->save();
  $fields = array_keys($this->index
    ->getFields());
  sort($fields);
  $this
    ->assertEquals([
    'field1',
    'field2',
  ], $fields);
  $this->index
    ->getDatasource($datasource_id)
    ->setConfiguration([
    'bundles' => [
      'default' => TRUE,
      'selected' => [
        'bundle2',
      ],
    ],
  ]);
  $this->index
    ->save();
  $fields = array_keys($this->index
    ->getFields());
  $this
    ->assertEquals([
    'field1',
  ], $fields);
}