public function AggregatedFieldsTest::testAggregation in Search API 8
Tests aggregated fields of the given type.
@dataProvider aggregationTestsDataProvider
Parameters
string $type: The aggregation type to test.
array $expected: The expected values for the two items.
bool $integer: (optional) TRUE if the items' normal fields should contain integers, FALSE otherwise.
File
- tests/
src/ Unit/ Processor/ AggregatedFieldsTest.php, line 147
Class
- AggregatedFieldsTest
- Tests the "Aggregated fields" processor.
Namespace
Drupal\Tests\search_api\Unit\ProcessorCode
public function testAggregation($type, array $expected, $integer = FALSE) {
// Add the field configuration.
$configuration = [
'type' => $type,
'fields' => [
'entity:test1/foo',
'entity:test1/foo:bar',
'entity:test2/foobaz:bla',
'entity:test3/always_empty',
],
];
$this->index
->getField($this->fieldId)
->setConfiguration($configuration);
if ($integer) {
$field_values = [
'foo' => [
2,
4,
],
'bar' => [
16,
],
'bla' => [
7,
],
];
}
else {
$field_values = [
'foo' => [
'foo',
'bar',
],
'bar' => [
'baz',
],
'bla' => [
'foobar',
],
];
}
$items = [];
$i = 0;
foreach ([
'entity:test1',
'entity:test2',
'entity:test3',
] as $datasource_id) {
$this->itemIds[$i++] = $item_id = Utility::createCombinedId($datasource_id, '1:en');
$item = \Drupal::getContainer()
->get('search_api.fields_helper')
->createItem($this->index, $item_id);
foreach ([
NULL,
$datasource_id,
] as $field_datasource_id) {
foreach ($this->index
->getFieldsByDatasource($field_datasource_id) as $field_id => $field) {
$field = clone $field;
if (!empty($field_values[$field_id])) {
$field
->setValues($field_values[$field_id]);
}
$item
->setField($field_id, $field);
}
}
$item
->setFieldsExtracted(TRUE);
$items[$item_id] = $item;
}
// Add the processor's field values to the items.
foreach ($items as $item) {
$this->processor
->addFieldValues($item);
}
$this
->assertEquals(array_map($this->valueCallback, $expected[0]), $items[$this->itemIds[0]]
->getField($this->fieldId)
->getValues(), 'Correct aggregation for item 1.');
$this
->assertEquals(array_map($this->valueCallback, $expected[1]), $items[$this->itemIds[1]]
->getField($this->fieldId)
->getValues(), 'Correct aggregation for item 2.');
$this
->assertEquals(array_map($this->valueCallback, $expected[2]), $items[$this->itemIds[2]]
->getField($this->fieldId)
->getValues(), 'Correct aggregation for item 3.');
}