View source
<?php
namespace Drupal\Tests\search_api\Kernel\Processor;
use Drupal\Core\Form\FormState;
use Drupal\node\NodeInterface;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\search_api\Utility\Utility;
class TypeBoostTest extends ProcessorTestBase {
protected $processor;
public function setUp($processor = NULL) {
parent::setUp('type_boost');
if (!NodeType::load('article')) {
$article_node_type = NodeType::create([
'type' => 'article',
'name' => 'Article',
]);
$article_node_type
->save();
}
if (!NodeType::load('page')) {
$page_node_type = NodeType::create([
'type' => 'page',
'name' => 'Page',
]);
$page_node_type
->save();
}
$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());
}
public function testEntityBundleBoost() {
$processor = $this->index
->getProcessor('type_boost');
$configuration = [
'boosts' => [
'entity:node' => [
'datasource_boost' => Utility::formatBoostFactor(3),
'bundle_boosts' => [
'article' => Utility::formatBoostFactor(5),
],
],
],
];
$processor
->setConfiguration($configuration);
$this->index
->setProcessors([
'type_boost' => $processor,
]);
$this->index
->save();
$nodes = [];
foreach ([
'article',
'page',
'article',
] as $node_type) {
$node = Node::create([
'status' => NodeInterface::PUBLISHED,
'type' => $node_type,
'title' => $this
->randomString(),
]);
$node
->save();
$nodes[$node
->id()] = $node
->getTypedData();
}
$items = [];
foreach ($nodes as $nid => $node) {
$items[] = [
'datasource' => 'entity:node',
'item' => $node,
'item_id' => $nid,
];
}
$items = $this
->generateItems($items);
$items['entity:node/3']
->setBoost(2);
$this->index
->preprocessIndexItems($items);
$boost_expected = 5;
$boost_actual = $items['entity:node/1']
->getBoost();
$this
->assertEquals($boost_expected, $boost_actual);
$boost_expected = 3;
$boost_actual = $items['entity:node/2']
->getBoost();
$this
->assertEquals($boost_expected, $boost_actual);
$boost_expected = 10;
$boost_actual = $items['entity:node/3']
->getBoost();
$this
->assertEquals($boost_expected, $boost_actual);
}
public function testConfigFormBundleBoostDefaults() {
$form = $this->processor
->buildConfigurationForm([], new FormState());
$this
->assertEquals(Utility::formatBoostFactor(0), $form['boosts']['entity:node']['bundle_boosts']['article']['#default_value']);
$this
->assertEquals(Utility::formatBoostFactor(0), $form['boosts']['entity:node']['bundle_boosts']['page']['#default_value']);
$configuration = [
'boosts' => [
'entity:node' => [
'datasource_boost' => Utility::formatBoostFactor(3),
'bundle_boosts' => [
'article' => Utility::formatBoostFactor(0),
],
],
],
];
$this->processor
->setConfiguration($configuration);
$form = $this->processor
->buildConfigurationForm([], new FormState());
$this
->assertEquals(Utility::formatBoostFactor(0), $form['boosts']['entity:node']['bundle_boosts']['article']['#default_value']);
$this
->assertEquals(Utility::formatBoostFactor(0), $form['boosts']['entity:node']['bundle_boosts']['page']['#default_value']);
}
}