You are here

public function ReferencedEntitiesReindexingTest::testReferencedEntityChanged in Search API 8

Tests correct tracking of changes in referenced entities.

@dataProvider referencedEntityChangedDataProvider

Parameters

array $child_map: Map of the child nodes to create. It should be compatible with the ::createEntitiesFromMap().

array $updates: A list of updates to child entities to execute. It should be keyed by the machine-name of the child entities. Value can be either FALSE (to remove an entity) or a list of the new raw values to apply to the entity.

array $expected: A list of search items that should be marked for reindexing.

File

tests/src/Kernel/Datasource/ReferencedEntitiesReindexingTest.php, line 117

Class

ReferencedEntitiesReindexingTest
Tests that changes in related entities are correctly tracked.

Namespace

Drupal\Tests\search_api\Kernel\Datasource

Code

public function testReferencedEntityChanged(array $child_map, array $updates, array $expected) {
  $children = $this
    ->createEntitiesFromMap($child_map, [], 'child');
  $parent_map = [
    'parent' => [
      'title' => 'Parent',
      'entity_reference' => 'child',
    ],
  ];
  $parents = $this
    ->createEntitiesFromMap($parent_map, $children, 'parent');
  $grandparent_map = [
    'grandparent' => [
      'title' => 'Grandparent',
      'parent_reference' => 'parent',
    ],
  ];
  $this
    ->createEntitiesFromMap($grandparent_map, $parents, 'grandparent');
  $this->index
    ->indexItems();
  $tracker = $this->index
    ->getTrackerInstance();
  $this
    ->assertEquals([], $tracker
    ->getRemainingItems());

  // Now let's execute updates.
  foreach ($updates as $i => $field_updates) {
    if ($field_updates === FALSE) {
      $children[$i]
        ->delete();
    }
    else {
      foreach ($field_updates as $field => $items) {
        $children[$i]
          ->get($field)
          ->setValue($items);
      }
      $children[$i]
        ->save();
    }
  }
  $this
    ->assertEquals($expected, $tracker
    ->getRemainingItems());
}