You are here

public function FieldValuesExtractionTest::testFieldValuesExtraction in Search API 8

Tests extraction of field values, as used during indexing.

@covers ::extractFields @covers ::extractField @covers ::extractFieldValues

File

tests/src/Kernel/System/FieldValuesExtractionTest.php, line 146

Class

FieldValuesExtractionTest
Tests extraction of field values, as used during indexing.

Namespace

Drupal\Tests\search_api\Kernel\System

Code

public function testFieldValuesExtraction() {
  $object = $this->entities[3]
    ->getTypedData();

  /** @var \Drupal\search_api\Item\FieldInterface[][] $fields */
  $fields = [
    'type' => [
      $this->fieldsHelper
        ->createField($this->index, 'type'),
    ],
    'name' => [
      $this->fieldsHelper
        ->createField($this->index, 'name'),
    ],
    'links:entity:name' => [
      $this->fieldsHelper
        ->createField($this->index, 'links'),
      $this->fieldsHelper
        ->createField($this->index, 'links_1'),
    ],
    'links:entity:links:entity:name' => [
      $this->fieldsHelper
        ->createField($this->index, 'links_links'),
    ],
  ];
  $this->fieldsHelper
    ->extractFields($object, $fields);
  $values = [];
  foreach ($fields as $property_path => $property_fields) {
    foreach ($property_fields as $field) {
      $field_values = $field
        ->getValues();
      sort($field_values);
      if (!isset($values[$property_path])) {
        $values[$property_path] = $field_values;
      }
      else {
        $this
          ->assertEquals($field_values, $values[$property_path], 'Second extraction provided the same results as the first.');
      }
    }
  }
  $expected = [
    'type' => [
      'article',
    ],
    'name' => [
      'Article 4',
    ],
    'links:entity:name' => [
      'Article 1',
      'Article 3',
    ],
    'links:entity:links:entity:name' => [
      'Article 1',
      'Article 2',
    ],
  ];
  $this
    ->assertEquals($expected, $values, 'Field values were correctly extracted');
}