You are here

public function NumberFieldBoostTest::setUp in Search API 8

Performs setup tasks before each individual test method is run.

Installs commonly used schemas and sets up a search server and an index, with the specified processor enabled.

Parameters

string|null $processor: (optional) The plugin ID of the processor that should be set up for testing.

Overrides ProcessorTestBase::setUp

File

tests/src/Kernel/Processor/NumberFieldBoostTest.php, line 33

Class

NumberFieldBoostTest
Tests the "Number field boost" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function setUp($processor = NULL) {
  parent::setUp('number_field_boost');

  // Create a page node type, if not already present.
  if (!NodeType::load('page')) {
    $page_node_type = NodeType::create([
      'type' => 'page',
      'name' => 'Page',
    ]);
    $page_node_type
      ->save();
  }

  // Add an integer field.
  $boost_field_storage = FieldStorageConfig::create([
    'field_name' => 'field_boost',
    'entity_type' => 'node',
    'type' => 'integer',
    'cardinality' => 3,
  ]);
  $boost_field_storage
    ->save();
  $boost_field = FieldConfig::create([
    'field_storage' => $boost_field_storage,
    'bundle' => 'page',
    'required' => FALSE,
  ]);
  $boost_field
    ->save();

  // Create some nodes.
  $node = Node::create([
    'status' => NodeInterface::PUBLISHED,
    'type' => 'page',
    'title' => 'node 1 title',
    'body' => 'node 1 body',
    'field_boost' => [
      8,
    ],
  ]);
  $node
    ->save();
  $node = Node::create([
    'status' => NodeInterface::PUBLISHED,
    'type' => 'page',
    'title' => 'node 2 title',
    'body' => 'node 2 body',
    'field_boost' => [
      3,
      10,
    ],
  ]);
  $node
    ->save();
  $node = Node::create([
    'status' => NodeInterface::PUBLISHED,
    'type' => 'page',
    'title' => 'node 3 title',
    'body' => 'node 3 body',
    'field_boost' => [
      1,
    ],
  ]);
  $node
    ->save();
  $datasources = $this->container
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugins($this->index, [
    'entity:node',
  ]);
  $this->index
    ->setDatasources($datasources);
  $nid_info = [
    'datasource_id' => 'entity:node',
    'property_path' => 'nid',
    'type' => 'integer',
  ];
  $boost_info = [
    'datasource_id' => 'entity:node',
    'property_path' => 'field_boost',
    'type' => 'integer',
  ];
  $fields_helper = $this->container
    ->get('search_api.fields_helper');
  $this->index
    ->addField($fields_helper
    ->createField($this->index, 'nid', $nid_info));
  $this->index
    ->addField($fields_helper
    ->createField($this->index, 'field_boost', $boost_info));
  $this->index
    ->save();
  \Drupal::getContainer()
    ->get('search_api.index_task_manager')
    ->addItemsAll($this->index);
  $index_storage = \Drupal::entityTypeManager()
    ->getStorage('search_api_index');
  $index_storage
    ->resetCache([
    $this->index
      ->id(),
  ]);
  $this->index = $index_storage
    ->load($this->index
    ->id());
}