public function DateRangeTest::setUp in Search API Solr 8.2
Same name and namespace in other branches
- 8.3 tests/src/Kernel/Processor/DateRangeTest.php \Drupal\Tests\search_api_solr\Kernel\Processor\DateRangeTest::setUp()
- 4.x tests/src/Kernel/Processor/DateRangeTest.php \Drupal\Tests\search_api_solr\Kernel\Processor\DateRangeTest::setUp()
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/ DateRangeTest.php, line 47
Class
- DateRangeTest
- Tests the "Date range" processor.
Namespace
Drupal\Tests\search_api_solr\Kernel\ProcessorCode
public function setUp($processor = NULL) {
parent::setUp('solr_date_range');
$this
->enableSolrServer('search_api_solr_test', '/config/install/search_api.server.solr_search_server.yml');
// Create a node type for testing.
$type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$type
->save();
// Add a datetime range field.
$rangeFieldStorage = FieldStorageConfig::create([
'field_name' => 'field_date_range',
'entity_type' => 'node',
'type' => 'daterange',
'settings' => [
'datetime_type' => DateRangeItem::DATETIME_TYPE_DATE,
],
]);
$rangeFieldStorage
->save();
$rangeField = FieldConfig::create([
'field_storage' => $rangeFieldStorage,
'bundle' => 'page',
'required' => TRUE,
]);
$rangeField
->save();
// Add a datetime ranges field.
$rangesFieldStorage = FieldStorageConfig::create([
'field_name' => 'field_date_ranges',
'entity_type' => 'node',
'type' => 'daterange',
'settings' => [
'datetime_type' => DateRangeItem::DATETIME_TYPE_DATE,
],
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
]);
$rangesFieldStorage
->save();
$rangesField = FieldConfig::create([
'field_storage' => $rangesFieldStorage,
'bundle' => 'page',
'required' => TRUE,
]);
$rangesField
->save();
// Create a node.
$values = [
'status' => NodeInterface::PUBLISHED,
'type' => 'page',
'title' => 'test title',
'field_date_range' => [
'value' => '2016-09-21',
'end_value' => '2016-10-21',
],
'field_date_ranges' => [
[
'value' => '2015-09-21',
'end_value' => '2015-10-21',
],
[
'value' => '2014-09-21',
'end_value' => '2014-10-21',
],
],
];
$this->nodes[0] = Node::create($values);
$this->nodes[0]
->save();
$values = [
'status' => NodeInterface::PUBLISHED,
'type' => 'page',
'title' => 'some title',
'field_date_range' => [
'value' => '2016-10-19',
'end_value' => '2016-11-21',
],
'field_date_ranges' => [
[
'value' => '2015-10-19',
'end_value' => '2015-11-21',
],
[
'value' => '2014-10-19',
'end_value' => '2014-11-21',
],
],
];
$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);
$date_range_info = [
'datasource_id' => 'entity:node',
'property_path' => 'field_date_range',
'type' => 'solr_date_range',
];
$date_ranges_info = [
'datasource_id' => 'entity:node',
'property_path' => 'field_date_ranges',
'type' => 'solr_date_range',
];
$fieldsHelper = $this->container
->get('search_api.fields_helper');
// Index location coordinates as location data type.
$this->index
->addField($fieldsHelper
->createField($this->index, 'field_date_range', $date_range_info));
$this->index
->addField($fieldsHelper
->createField($this->index, 'field_date_ranges', $date_ranges_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());
}