You are here

public function BoostMoreRecentTest::setUp in Search API Solr 4.x

File

tests/src/Kernel/Processor/BoostMoreRecentTest.php, line 45

Class

BoostMoreRecentTest
Tests the "Boost more recent" processor.

Namespace

Drupal\Tests\search_api_solr\Kernel\Processor

Code

public function setUp($processor = NULL) : void {
  parent::setUp('solr_boost_more_recent');
  $this
    ->enableSolrServer();

  // Create a node type for testing.
  $type = NodeType::create([
    'type' => 'page',
    'name' => 'page',
  ]);
  $type
    ->save();

  // Add a datetime field.
  $dateFieldStorage = FieldStorageConfig::create([
    'field_name' => 'field_date',
    'entity_type' => 'node',
    'type' => 'datetime',
    'settings' => [
      'datetime_type' => DateTimeItem::DATETIME_TYPE_DATE,
    ],
  ]);
  $dateFieldStorage
    ->save();
  $rangeField = FieldConfig::create([
    'field_storage' => $dateFieldStorage,
    'bundle' => 'page',
    'required' => TRUE,
  ]);
  $rangeField
    ->save();

  // Create a node.
  $values = [
    'status' => NodeInterface::PUBLISHED,
    'type' => 'page',
    'title' => 'test title',
    'field_date' => '2016-09-21',
  ];
  $this->nodes[0] = Node::create($values);
  $this->nodes[0]
    ->save();
  $values = [
    'status' => NodeInterface::PUBLISHED,
    'type' => 'page',
    'title' => 'some title',
    'field_date' => '2020-10-19',
  ];
  $this->nodes[1] = Node::create($values);
  $this->nodes[1]
    ->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',
  ];
  $date_info = [
    'datasource_id' => 'entity:node',
    'property_path' => 'field_date',
    'type' => 'date',
  ];
  $fieldsHelper = $this->container
    ->get('search_api.fields_helper');
  $this->index
    ->addField($fieldsHelper
    ->createField($this->index, 'nid', $nid_info));
  $this->index
    ->addField($fieldsHelper
    ->createField($this->index, 'field_date', $date_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());
}