You are here

public function IndexChangesTest::testDatasourceRemoved in Search API 8

Tests correct reactions when a datasource is removed.

File

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

Class

IndexChangesTest
Tests correct reactions to changes for the index.

Namespace

Drupal\Tests\search_api\Kernel\Index

Code

public function testDatasourceRemoved() {
  $info = [
    'datasource_id' => 'entity:entity_test_mulrev_changed',
    'property_path' => 'id',
  ];
  $field = \Drupal::getContainer()
    ->get('search_api.fields_helper')
    ->createField($this->index, 'id', $info);
  $this->index
    ->addField($field);
  $processor = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createProcessorPlugin($this->index, 'search_api_test');
  $this->index
    ->addProcessor($processor);
  $this
    ->setMethodOverride('processor', 'supportsIndex', function (IndexInterface $index) {
    return in_array('entity:entity_test_mulrev_changed', $index
      ->getDatasourceIds());
  });
  $this->index
    ->save();
  $this
    ->assertArrayHasKey('search_api_test', $this->index
    ->getProcessors());
  $tracker = $this->index
    ->getTrackerInstance();
  $expected = [
    Utility::createCombinedId('entity:entity_test_mulrev_changed', '1:en'),
    Utility::createCombinedId('entity:user', '1:en'),
  ];
  $remaining_items = $tracker
    ->getRemainingItems();
  sort($remaining_items);
  $this
    ->assertEquals($expected, $remaining_items);
  $this
    ->getCalledMethods('backend');
  $indexed = $this->index
    ->indexItems();
  $this
    ->assertEquals(2, $indexed);
  $this
    ->assertEquals([
    'indexItems',
  ], $this
    ->getCalledMethods('backend'));
  $indexed_items = array_keys($this
    ->getIndexedItems());
  sort($indexed_items);
  $this
    ->assertEquals($expected, $indexed_items);
  $this
    ->assertEquals(0, $tracker
    ->getRemainingItemsCount());
  $this->index
    ->removeDatasource('entity:entity_test_mulrev_changed')
    ->save();
  $this
    ->assertArrayNotHasKey('id', $this->index
    ->getFields());
  $this
    ->assertArrayNotHasKey('search_api_test', $this->index
    ->getProcessors());
  $this
    ->assertEquals(1, $tracker
    ->getTotalItemsCount());
  $expected = [
    Utility::createCombinedId('entity:user', '1:en'),
  ];
  $indexed_items = array_keys($this
    ->getIndexedItems());
  sort($indexed_items);
  $this
    ->assertEquals($expected, $indexed_items);
  $this
    ->assertEquals([
    'updateIndex',
    'deleteAllIndexItems',
  ], $this
    ->getCalledMethods('backend'));
  User::create([
    'uid' => 2,
    'name' => 'someone',
    'langcode' => 'en',
  ])
    ->save();
  EntityTestMulRevChanged::create([
    'id' => 2,
    'name' => 'test 2',
  ])
    ->save();
  $this
    ->assertEquals(2, $tracker
    ->getTotalItemsCount());
  $indexed = $this->index
    ->indexItems();
  $this
    ->assertGreaterThanOrEqual(1, $indexed);
  $this
    ->assertEquals([
    'indexItems',
  ], $this
    ->getCalledMethods('backend'));
  $expected = [
    Utility::createCombinedId('entity:user', '1:en'),
    Utility::createCombinedId('entity:user', '2:en'),
  ];
  $indexed_items = array_keys($this
    ->getIndexedItems());
  sort($indexed_items);
  $this
    ->assertEquals($expected, $indexed_items);
  $this
    ->assertEquals(0, $tracker
    ->getRemainingItemsCount());
}