View source
<?php
namespace Drupal\Tests\search_api\Kernel\Processor;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\node\Entity\NodeType;
use Drupal\search_api\Item\Field;
use Drupal\search_api\Query\Query;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\search_api\Kernel\ResultsTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class AddHierarchyTest extends ProcessorTestBase {
use NodeCreationTrait;
use EntityReferenceTestTrait;
use ResultsTrait;
use TaxonomyTestTrait;
public static $modules = [
'filter',
'taxonomy',
];
protected static $hierarchy = [
'fruit' => [
'apple',
'pear',
],
'vegetable' => [
'radish',
'turnip',
],
];
protected $nodes = [];
protected $terms = [];
protected $vocabulary;
public function setUp($processor = NULL) {
parent::setUp();
$this
->installConfig([
'filter',
]);
$this
->installEntitySchema('taxonomy_term');
$this
->createTaxonomyHierarchy();
$type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$type
->save();
$this
->createEntityReferenceField('node', 'page', 'term_field', NULL, 'taxonomy_term', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this
->createEntityReferenceField('node', 'page', 'parent_reference', NULL, 'node', 'default', []);
$term_field = new Field($this->index, 'term_field');
$term_field
->setType('integer');
$term_field
->setPropertyPath('term_field');
$term_field
->setDatasourceId('entity:node');
$term_field
->setLabel('Terms');
$this->index
->addField($term_field);
$reference_field = new Field($this->index, 'parent_reference');
$reference_field
->setType('integer');
$reference_field
->setPropertyPath('parent_reference');
$reference_field
->setDatasourceId('entity:node');
$reference_field
->setLabel('Parent page');
$this->index
->addField($reference_field);
$this->processor = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createProcessorPlugin($this->index, 'hierarchy');
$this->index
->addProcessor($this->processor);
$datasources = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createDatasourcePlugins($this->index, [
'entity:node',
]);
$this->index
->setDatasources($datasources);
$this->index
->save();
$this->container
->get('search_api.index_task_manager')
->addItemsAll($this->index);
$index_storage = $this->container
->get('entity_type.manager')
->getStorage('search_api_index');
$index_storage
->resetCache([
$this->index
->id(),
]);
$this->index = $index_storage
->load($this->index
->id());
}
protected function createTaxonomyHierarchy() {
$this->vocabulary = $this
->createVocabulary();
foreach (static::$hierarchy as $type => $items) {
$this->terms[$type] = $type_term = $this
->createTerm($this->vocabulary, [
'name' => $type,
]);
foreach ($items as $item) {
$this->terms["{$type}.{$item}"] = $this
->createTerm($this->vocabulary, [
'name' => $item,
'parent' => $type_term,
]);
}
}
}
public function testPreprocessIndexItemsTaxonomy() {
foreach ([
'vegetable.turnip',
'vegetable',
'fruit.pear',
] as $i => $term) {
$this->nodes[$i] = $this
->createNode([
'type' => 'page',
'term_field' => [
'target_id' => $this->terms[$term]
->id(),
],
]);
}
$this->index
->reindex();
$this
->indexItems();
$query = new Query($this->index);
$query
->addCondition('term_field', $this->terms['vegetable']
->id());
$result = $query
->execute();
$expected = [
'node' => [
1,
],
];
$this
->assertResults($result, $expected);
$processor = $this->index
->getProcessor('hierarchy');
$processor
->setConfiguration([
'fields' => [
'term_field' => 'taxonomy_term-parent',
],
]);
$this->index
->save();
$this
->indexItems();
$query = new Query($this->index);
$query
->addCondition('term_field', $this->terms['vegetable']
->id());
$result = $query
->execute();
$expected = [
'node' => [
0,
1,
],
];
$this
->assertResults($result, $expected);
$query = new Query($this->index);
$query
->addCondition('term_field', $this->terms['vegetable.turnip']
->id());
$result = $query
->execute();
$expected = [
'node' => [
0,
],
];
$this
->assertResults($result, $expected);
$this->terms['avocado'] = $this
->createTerm($this->vocabulary, [
'name' => 'Avocado',
'parent' => [
$this->terms['fruit']
->id(),
$this->terms['vegetable']
->id(),
],
]);
$this->nodes[3] = $this
->createNode([
'type' => 'page',
'term_field' => [
'target_id' => $this->terms['avocado']
->id(),
],
]);
$this->index
->reindex();
$this
->indexItems();
$query = new Query($this->index);
$query
->addCondition('term_field', $this->terms['fruit']
->id());
$result = $query
->execute();
$expected = [
'node' => [
2,
3,
],
];
$this
->assertResults($result, $expected);
$query = new Query($this->index);
$query
->addCondition('term_field', $this->terms['vegetable']
->id());
$result = $query
->execute();
$expected = [
'node' => [
0,
1,
3,
],
];
$this
->assertResults($result, $expected);
}
public function testRegression3059312() {
foreach ([
'vegetable.turnip',
'vegetable',
'fruit.pear',
] as $i => $term) {
$this->nodes[$i] = $this
->createNode([
'type' => 'page',
'term_field' => [
'target_id' => $this->terms[$term]
->id(),
],
]);
}
$this->terms['avocado'] = $this
->createTerm($this->vocabulary, [
'name' => 'Avocado',
'parent' => [
$this->terms['fruit']
->id(),
$this->terms['vegetable']
->id(),
],
]);
$this->nodes[3] = $this
->createNode([
'type' => 'page',
'term_field' => [
'target_id' => $this->terms['avocado']
->id(),
],
]);
$processor = $this->index
->getProcessor('hierarchy');
$processor
->setConfiguration([
'fields' => [
'term_field' => 'taxonomy_term-parent',
],
]);
$this->index
->getField('term_field')
->setType('text');
$this->index
->save();
$this
->indexItems();
$query = new Query($this->index);
$query
->addCondition('term_field', $this->terms['fruit']
->id());
$result = $query
->execute();
$expected = [
'node' => [
2,
3,
],
];
$this
->assertResults($result, $expected);
}
public function testPreprocessIndexItems() {
foreach (static::$hierarchy as $type => $items) {
$this->nodes[] = $type_node = $this
->createNode([
'title' => $type,
]);
foreach ($items as $item) {
$this->nodes[] = $this
->createNode([
'title' => $item,
'parent_reference' => [
'target_id' => $type_node
->id(),
],
]);
}
}
foreach ([
'Cherry Belle',
'Snow Belle',
'Daikon',
] as $item) {
$this->nodes[] = $this
->createNode([
'title' => $item,
'parent_reference' => [
'target_id' => $this->nodes[5]
->id(),
],
]);
}
$this->index
->reindex();
$this
->indexItems();
$query = new Query($this->index);
$query
->addCondition('parent_reference', $this->nodes[3]
->id());
$result = $query
->execute();
$expected = [
'node' => [
4,
5,
],
];
$this
->assertResults($result, $expected);
$processor = $this->index
->getProcessor('hierarchy');
$processor
->setConfiguration([
'fields' => [
'parent_reference' => 'node-parent_reference',
],
]);
$this->index
->save();
$this
->indexItems();
$query = new Query($this->index);
$query
->addCondition('parent_reference', $this->nodes[3]
->id());
$result = $query
->execute();
$expected = [
'node' => [
4,
5,
6,
7,
8,
],
];
$this
->assertResults($result, $expected);
}
}