You are here

public function DependencyRemovalTest::testFieldDependency in Search API 8

Tests index with a field dependency that gets removed.

File

tests/src/Kernel/ConfigEntity/DependencyRemovalTest.php, line 86

Class

DependencyRemovalTest
Tests what happens when an index's or a server's dependencies are removed.

Namespace

Drupal\Tests\search_api\Kernel\ConfigEntity

Code

public function testFieldDependency() {

  // Add new field storage and field definitions. Use an indirect reference
  // for the field to test whether this can also be handled correctly.
  $this
    ->createEntityReferenceField('user', 'user', 'parent', 'Parent', 'user');
  $parent_field_storage = FieldStorageConfig::loadByName('user', 'parent');

  /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_search',
    'type' => 'string',
    'entity_type' => 'user',
  ]);
  $field_storage
    ->save();
  $field_search = FieldConfig::create([
    'field_name' => 'field_search',
    'field_type' => 'string',
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'Search Field',
  ]);
  $field_search
    ->save();

  // Add fields to the index.
  $fields_helper = \Drupal::getContainer()
    ->get('search_api.fields_helper');
  $field = $fields_helper
    ->createFieldFromProperty($this->index, $field_storage
    ->getPropertyDefinition('value'), 'entity:user', 'field_search', 'search', 'string');
  $this->index
    ->addField($field);
  $field = $fields_helper
    ->createFieldFromProperty($this->index, $field_storage
    ->getPropertyDefinition('value'), 'entity:user', 'parent:entity:field_search', 'parent_search', 'string');
  $this->index
    ->addField($field);
  $field = $fields_helper
    ->createFieldFromProperty($this->index, $parent_field_storage
    ->getPropertyDefinition('target_id'), 'entity:user', 'parent', 'parent', 'string');
  $this->index
    ->addField($field);
  $this->index
    ->save();

  // New field has been added to the list of dependencies.
  $config_dependencies = \Drupal::config('search_api.index.' . $this->index
    ->id())
    ->get('dependencies.config');
  $this
    ->assertContains($parent_field_storage
    ->getConfigDependencyName(), $config_dependencies);
  $this
    ->assertContains($field_storage
    ->getConfigDependencyName(), $config_dependencies);

  // Remove a dependent field.
  $parent_field_storage
    ->delete();

  // Index has not been deleted and index dependencies were updated.
  $this
    ->reloadIndex();
  $index = \Drupal::config('search_api.index.' . $this->index
    ->id());
  $dependencies = $index
    ->get('dependencies');
  $this
    ->assertFalse(isset($dependencies['config'][$parent_field_storage
    ->getConfigDependencyName()]));
  $this
    ->assertContains($field_storage
    ->getConfigDependencyName(), $config_dependencies);

  // Correct fields were removed.
  $this
    ->assertEquals([
    'search',
  ], array_keys($index
    ->get('field_settings')));

  // Remove a dependent field.
  $field_storage
    ->delete();

  // Index has not been deleted and index dependencies were updated.
  $this
    ->reloadIndex();
  $dependencies = \Drupal::config('search_api.index.' . $this->index
    ->id())
    ->get('dependencies');
  $this
    ->assertFalse(isset($dependencies['config'][$field_storage
    ->getConfigDependencyName()]));

  // Last field was removed.
  $this
    ->assertEquals([], array_keys($index
    ->get('field_settings')));
}