You are here

public function FieldValuesExtractionTest::testPropertyValuesExtraction in Search API 8

Tests extraction of properties, as used in processors or for result lists.

@covers ::extractItemValues

File

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

Class

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

Namespace

Drupal\Tests\search_api\Kernel\System

Code

public function testPropertyValuesExtraction() {
  $items['foobar'] = $this->fieldsHelper
    ->createItemFromObject($this->index, $this->entities[0]
    ->getTypedData(), Utility::createCombinedId('entity:entity_test_mulrev_changed', '0:en'));
  $properties = [
    NULL => [
      'rendered_item' => 'a',
      // Since there is no field defined on "aggregated_field" for the index,
      // we won't be able to extract it.
      'aggregated_field' => 'b',
      'search_api_url' => 'c',
    ],
    'entity:entity_test_mulrev_changed' => [
      'name' => 'd',
      'type' => 'e',
      'soul_mate:name' => 'f',
    ],
    'unknown_datasource' => [
      'name' => 'x',
    ],
  ];
  $expected = [
    'foobar' => [
      'a' => [],
      'b' => [],
      'c' => [],
      'd' => [],
      'e' => [],
      'f' => [],
    ],
  ];
  $values = $this->fieldsHelper
    ->extractItemValues($items, $properties, FALSE);
  ksort($values['foobar']);
  $this
    ->assertEquals($expected, $values);
  $expected = [
    'foobar' => [
      // 'a' => 'Tested separately.',
      'b' => [],
      'c' => [
        '/entity_test_mulrev_changed/manage/1',
      ],
      'd' => [
        'Article 1',
      ],
      'e' => [
        'article',
      ],
      'f' => [
        'Test user',
      ],
    ],
  ];
  $values = $this->fieldsHelper
    ->extractItemValues($items, $properties);
  ksort($values['foobar']);
  $this
    ->assertArrayHasKey('a', $values['foobar']);
  $this
    ->assertNotEmpty($values['foobar']['a']);
  $this
    ->assertStringContainsString('Article 1', $values['foobar']['a'][0]);
  unset($values['foobar']['a']);
  $this
    ->assertEquals($expected, $values);
  $items['foobar']
    ->setFields([
    'aa' => $this->fieldsHelper
      ->createField($this->index, 'aa_foo', [
      'property_path' => 'aggregated_field',
      'values' => [
        1,
        2,
      ],
    ]),
    'bb' => $this->fieldsHelper
      ->createField($this->index, 'bb_foo', [
      'property_path' => 'rendered_item',
      'values' => [
        3,
      ],
    ]),
    'cc' => $this->fieldsHelper
      ->createField($this->index, 'cc_foo', [
      'datasource_id' => 'entity:entity_test_mulrev_changed',
      'property_path' => 'type',
      'values' => [
        4,
      ],
    ]),
    'dd' => $this->fieldsHelper
      ->createField($this->index, 'dd_foo', [
      'datasource_id' => 'entity:entity_test_mulrev_changed',
      'property_path' => 'soul_mate:name',
      'values' => [
        5,
      ],
    ]),
  ]);
  $expected = [
    'foobar' => [
      'a' => [
        3,
      ],
      'b' => [
        1,
        2,
      ],
      'c' => [],
      'd' => [],
      'e' => [
        4,
      ],
      'f' => [
        5,
      ],
    ],
  ];
  $values = $this->fieldsHelper
    ->extractItemValues($items, $properties, FALSE);
  ksort($values['foobar']);
  $this
    ->assertEquals($expected, $values);
  $expected = [
    'foobar' => [
      'a' => [
        3,
      ],
      'b' => [
        1,
        2,
      ],
      'c' => [
        '/entity_test_mulrev_changed/manage/1',
      ],
      'd' => [
        'Article 1',
      ],
      'e' => [
        4,
      ],
      'f' => [
        5,
      ],
    ],
  ];
  $values = $this->fieldsHelper
    ->extractItemValues($items, $properties);
  ksort($values['foobar']);
  $this
    ->assertEquals($expected, $values);
}