View source
<?php
namespace Drupal\Tests\facets\Functional;
use Drupal\entity_test\Entity\EntityTestMulRevChanged;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Plugin\search_api\processor\Property\AggregatedFieldProperty;
use Drupal\search_api\Utility\Utility;
use Drupal\user\Entity\User;
class AggregatedFieldTest extends FacetsTestBase {
protected $users;
public function setUp() {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this
->setUpExampleStructure();
$this
->insertExampleContent();
foreach ([
7 => 'Owl',
8 => 'Robin',
9 => 'Hawk',
] as $i => $value) {
$this->users[$i] = User::create([
'uid' => $i,
'name' => "User {$value}",
]);
$this->users[$i]
->save();
$this->entities[$i] = EntityTestMulRevChanged::create([
'id' => $i,
'user_id' => $i,
'name' => "Test entity {$value} name",
'body' => "Test entity {$value} body",
]);
$this->entities[$i]
->save();
}
$plugin_creation_helper = \Drupal::getContainer()
->get('search_api.plugin_helper');
$fields_helper = \Drupal::getContainer()
->get('search_api.fields_helper');
$index = Index::load($this->indexId);
$index
->addDatasource($plugin_creation_helper
->createDatasourcePlugin($index, 'entity:user'));
$property = AggregatedFieldProperty::create('string');
$field = $fields_helper
->createFieldFromProperty($index, $property, NULL, 'aggregated_field', 'aggregated_field', 'string');
$field
->setLabel('Aggregated field');
$field
->setConfiguration([
'type' => 'union',
'fields' => [
Utility::createCombinedId('entity:entity_test_mulrev_changed', 'name'),
Utility::createCombinedId('entity:user', 'name'),
],
]);
$index
->addField($field);
$index
->save();
$this
->assertEquals(16, $this
->indexItems($this->indexId));
}
public function testAggregatedField() {
$facet_id = 'test_agg';
$facet_add_page = '/admin/config/search/facets/add-facet';
$this
->drupalGet($facet_add_page);
$this
->assertSession()
->statusCodeEquals(200);
$form_values = [
'name' => 'Test agg',
'id' => $facet_id,
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
'facet_source_configs[search_api:views_page__search_api_test_view__page_1][field_identifier]' => 'aggregated_field',
];
$this
->drupalPostForm(NULL, [
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
], 'Configure facet source');
$this
->drupalPostForm(NULL, $form_values, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
}
}