You are here

public function AddHierarchyTest::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/AddHierarchyTest.php, line 80

Class

AddHierarchyTest
Tests the "Hierarchy" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function setUp($processor = NULL) {
  parent::setUp();
  $this
    ->installConfig([
    'filter',
  ]);
  $this
    ->installEntitySchema('taxonomy_term');
  $this
    ->createTaxonomyHierarchy();

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

  // Add the taxonomy field to page type.
  $this
    ->createEntityReferenceField('node', 'page', 'term_field', NULL, 'taxonomy_term', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

  // Add a generic entity reference field.
  $this
    ->createEntityReferenceField('node', 'page', 'parent_reference', NULL, 'node', 'default', []);

  // Index the taxonomy field.
  $term_field = new Field($this->index, 'term_field');
  $term_field
    ->setType('integer');
  $term_field
    ->setPropertyPath('term_field');
  $term_field
    ->setDatasourceId('entity:node');
  $term_field
    ->setLabel('Terms');
  $this->index
    ->addField($term_field);

  // Index the entity reference field.
  $reference_field = new Field($this->index, 'parent_reference');
  $reference_field
    ->setType('integer');
  $reference_field
    ->setPropertyPath('parent_reference');
  $reference_field
    ->setDatasourceId('entity:node');
  $reference_field
    ->setLabel('Parent page');
  $this->index
    ->addField($reference_field);

  // Add the "Index hierarchy" processor only now (not in parent method) since
  // it can only be enabled once there are actually hierarchical fields.
  $this->processor = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createProcessorPlugin($this->index, 'hierarchy');
  $this->index
    ->addProcessor($this->processor);

  // Add the node datasource to the index.
  $datasources = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugins($this->index, [
    'entity:node',
  ]);
  $this->index
    ->setDatasources($datasources);
  $this->index
    ->save();
  $this->container
    ->get('search_api.index_task_manager')
    ->addItemsAll($this->index);
  $index_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('search_api_index');
  $index_storage
    ->resetCache([
    $this->index
      ->id(),
  ]);
  $this->index = $index_storage
    ->load($this->index
    ->id());
}