You are here

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

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/Processor/DateRangeTest.php \Drupal\Tests\search_api_solr\Kernel\Processor\DateRangeTest::setUp()
  2. 8.2 tests/src/Kernel/Processor/DateRangeTest.php \Drupal\Tests\search_api_solr\Kernel\Processor\DateRangeTest::setUp()

File

tests/src/Kernel/Processor/DateRangeTest.php, line 49

Class

DateRangeTest
Tests the "Date range" processor.

Namespace

Drupal\Tests\search_api_solr\Kernel\Processor

Code

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

  // 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');
  $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());
}