You are here

public function TypeBoostTest::testEntityBundleBoost in Search API 8

Tests that the correct boost is set on items.

@covers ::preprocessIndexItems

File

tests/src/Kernel/Processor/TypeBoostTest.php, line 72

Class

TypeBoostTest
Tests the "Type-specific boosting" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function testEntityBundleBoost() {

  // Enable the processor indexing.
  $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();

  // Create nodes for both node types.
  $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();
  }

  // Prepare and generate Search API items.
  $items = [];
  foreach ($nodes as $nid => $node) {
    $items[] = [
      'datasource' => 'entity:node',
      'item' => $node,
      'item_id' => $nid,
    ];
  }
  $items = $this
    ->generateItems($items);

  // Set a boost on one of the items to check whether it gets overwritten or
  // (correctly) multiplied.
  $items['entity:node/3']
    ->setBoost(2);

  // Preprocess items.
  $this->index
    ->preprocessIndexItems($items);

  // Check boost value on article node.
  $boost_expected = 5;
  $boost_actual = $items['entity:node/1']
    ->getBoost();
  $this
    ->assertEquals($boost_expected, $boost_actual);

  // Check boost value on page node.
  $boost_expected = 3;
  $boost_actual = $items['entity:node/2']
    ->getBoost();
  $this
    ->assertEquals($boost_expected, $boost_actual);

  // Check boost value on article node with pre-existing boost.
  $boost_expected = 10;
  $boost_actual = $items['entity:node/3']
    ->getBoost();
  $this
    ->assertEquals($boost_expected, $boost_actual);
}