You are here

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

Class

ReverseEntityReferencesTest
Tests the "Reverse entity references" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

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

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

  // Insert the anonymous user into the database, as well as some other users.
  User::create([
    'uid' => 0,
    'name' => '',
  ])
    ->save();
  User::create([
    'uid' => 1,
    'name' => 'admin',
  ])
    ->save();
  User::create([
    'uid' => 2,
    'name' => 'user',
  ])
    ->save();
  User::create([
    'uid' => 3,
    'name' => 'other user',
  ])
    ->save();

  // Create nodes.
  foreach ($this->nodeUids as $i => $uid) {
    $values = [
      'type' => 'page',
      'title' => 'test title',
      'uid' => $uid,
    ];
    $this->nodes[$i] = Node::create($values);
    $this->nodes[$i]
      ->save();
  }

  // Switch the index to index users and add a reverse reference to the nodes
  // authored by the indexed user.
  $datasources = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugins($this->index, [
    'entity:user',
  ]);
  $this->index
    ->setDatasources($datasources);
  $field = new Field($this->index, 'nid');
  $field
    ->setType('integer');
  $field
    ->setPropertyPath('search_api_reverse_entity_references_node__uid:nid');
  $field
    ->setDatasourceId('entity:user');
  $field
    ->setLabel('Authored nodes');
  $this->index
    ->addField($field);
  $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());
}