You are here

public function FieldValuesExtractionTest::testNestedComplexFieldValuesExtraction in Search API 8

Tests extraction of field values from nested complex data structures.

@covers ::extractFieldValues

File

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

Class

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

Namespace

Drupal\Tests\search_api\Kernel\System

Code

public function testNestedComplexFieldValuesExtraction() {

  // Complex data definition structure.
  // phpcs:disable Drupal.Commenting.InlineComment.NotCapital
  // data => ListDataDefinition (list) [
  //   itemDefinition => ComplexDataDefinition (map) [
  //     propertyDefinitions => [
  //       id => DataDefinition (string),
  //       values (main property) => ListDataDefinition (list) [
  //         itemDefinition => ComplexDataDefinition (map) [
  //           propertyDefinitions => [
  //             property1 => DataDefinition (string),
  //             property2 (main property) => DataDefinition (string),
  //           ]
  //         ]
  //       ]
  //     ]
  //   ]
  // ]
  // phpcs:enable
  $properties_def = MapDataDefinition::create();
  $properties_def
    ->setPropertyDefinition('property1', DataDefinition::create('string'));
  $properties_def
    ->setPropertyDefinition('property2', DataDefinition::create('string'));
  $properties_def
    ->setMainPropertyName('property2');
  $values_def = ListDataDefinition::create('map');
  $values_def
    ->setItemDefinition($properties_def);
  $data_item_def = MapDataDefinition::create();
  $data_item_def
    ->setPropertyDefinition('id', DataDefinition::create('string'));
  $data_item_def
    ->setPropertyDefinition('values', $values_def);
  $data_item_def
    ->setMainPropertyName('values');
  $data_def = ListDataDefinition::create('map');
  $data_def
    ->setItemDefinition($data_item_def);

  // Creates an instance of the structure with test source data.
  $target_value = 'target value';
  $source_data = [
    'id' => 'test_id',
    'values' => [
      [
        'property1' => 'wrong value',
        'property2' => $target_value,
      ],
    ],
  ];
  $data = ItemList::createInstance($data_def, 'data');
  $data
    ->appendItem($source_data);
  $extracted_values = $this->fieldsHelper
    ->extractFieldValues($data);
  $this
    ->assertEquals([
    $target_value,
  ], $extracted_values);
}