You are here

public function IndexChangesTest::testDatasourceAdded in Search API 8

Tests correct reactions when a new datasource is added.

File

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

Class

IndexChangesTest
Tests correct reactions to changes for the index.

Namespace

Drupal\Tests\search_api\Kernel\Index

Code

public function testDatasourceAdded() {
  $this->index
    ->set('datasource_settings', [
    'entity:user' => [],
  ]);
  $this->index
    ->save();
  $tracker = $this->index
    ->getTrackerInstance();
  $expected = [
    Utility::createCombinedId('entity:user', '1:en'),
  ];
  $this
    ->assertEquals($expected, $tracker
    ->getRemainingItems());

  /** @var \Drupal\search_api\Datasource\DatasourceInterface $datasource */
  $datasource = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugin($this->index, 'entity:entity_test_mulrev_changed');
  $this->index
    ->addDatasource($datasource)
    ->save();
  $this->taskManager
    ->executeAllTasks();
  $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);
  User::create([
    'uid' => 2,
    'name' => 'someone',
    'langcode' => 'en',
  ])
    ->save();
  EntityTestMulRevChanged::create([
    'id' => 2,
    'name' => 'test 2',
  ])
    ->save();
  $expected = [
    Utility::createCombinedId('entity:entity_test_mulrev_changed', '1:en'),
    Utility::createCombinedId('entity:entity_test_mulrev_changed', '2:en'),
    Utility::createCombinedId('entity:user', '1:en'),
    Utility::createCombinedId('entity:user', '2:en'),
  ];
  $remaining_items = $tracker
    ->getRemainingItems();
  sort($remaining_items);
  $this
    ->assertEquals($expected, $remaining_items);
  $this
    ->getCalledMethods('backend');
  $indexed = $this->index
    ->indexItems();
  $this
    ->assertEquals(4, $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());
}