You are here

public function ReferencedEntitiesReindexingTest::testUnrelatedDatasourceUnaffected in Search API 8

Tests whether relationships are correctly separated between datasources.

See also

https://www.drupal.org/node/3178941

File

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

Class

ReferencedEntitiesReindexingTest
Tests that changes in related entities are correctly tracked.

Namespace

Drupal\Tests\search_api\Kernel\Datasource

Code

public function testUnrelatedDatasourceUnaffected() {

  // First, check whether the tracking helper correctly includes "datasource"
  // keys with all foreign relationship entries.
  $tracking_helper = \Drupal::getContainer()
    ->get('search_api.tracking_helper');
  $method = new \ReflectionMethod(TrackingHelper::class, 'getForeignEntityRelationsMap');
  $method
    ->setAccessible(TRUE);

  /** @see \Drupal\search_api\Utility\TrackingHelper::getForeignEntityRelationsMap() */
  $map = $method
    ->invoke($tracking_helper, $this->index);
  usort($map, function (array $a, array $b) : int {
    $field = 'property_path_to_foreign_entity';
    return $a[$field] <=> $b[$field];
  });
  $expected = [
    [
      'datasource' => 'entity:node',
      'entity_type' => 'node',
      // Note: It's unspecified that this array has string keys, only its
      // values are important. Still, it's easier to just reflect the current
      // implementation, when checking for equality.
      'bundles' => [
        'child' => 'child',
      ],
      'property_path_to_foreign_entity' => 'entity_reference:entity',
      'field_name' => 'indexed',
    ],
    [
      'datasource' => 'entity:node',
      'entity_type' => 'node',
      'bundles' => [
        'parent' => 'parent',
      ],
      'property_path_to_foreign_entity' => 'parent_reference:entity',
      'field_name' => 'entity_reference',
    ],
    [
      'datasource' => 'entity:node',
      'entity_type' => 'node',
      'bundles' => [
        'child' => 'child',
      ],
      'property_path_to_foreign_entity' => 'parent_reference:entity:entity_reference:entity',
      'field_name' => 'indexed',
    ],
  ];
  $this
    ->assertEquals($expected, $map);

  // Then, check whether datasources correctly ignore relationships from other
  // datasources, or that they at least don't lead to an exception/error.
  $datasource = $this->index
    ->getDatasource('entity:user');
  $entities = $this
    ->createEntitiesFromMap([
    'child' => [
      'title' => 'Child',
      'indexed' => 'Indexed value',
      'not_indexed' => 'Not indexed value.',
    ],
  ], [], 'child');
  $child = reset($entities);
  $original_child = clone $child;
  $child
    ->get('indexed')
    ->setValue([
    'New value',
  ]);
  $result = $datasource
    ->getAffectedItemsForEntityChange($child, $map, $original_child);
  $this
    ->assertEquals([], $result);

  // Change foreign relationships map slightly to trigger #3178941 on purpose.
  $map[0]['property_path_to_foreign_entity'] = 'entity_reference:entity';
  $result = $datasource
    ->getAffectedItemsForEntityChange($child, $map, $original_child);
  $this
    ->assertEquals([], $result);
}