You are here

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

Class

ContentAccessTest
Tests the "Content access" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

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

  // Activate our custom grant.
  \Drupal::state()
    ->set('search_api_test_add_node_access_grant', TRUE);

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

  // Create anonymous user role.
  $role = Role::create([
    'id' => 'anonymous',
    'label' => 'anonymous',
  ]);
  $role
    ->save();

  // Insert the anonymous user into the database, as the user table is inner
  // joined by \Drupal\comment\CommentStorage.
  User::create([
    'uid' => 0,
    'name' => '',
  ])
    ->save();

  // Create a node with attached comment.
  $values = [
    'status' => NodeInterface::PUBLISHED,
    'type' => 'page',
    'title' => 'test title',
  ];
  $this->nodes[0] = Node::create($values);
  $this->nodes[0]
    ->save();
  $comment_type = CommentType::create([
    'id' => 'comment',
    'target_entity_type_id' => 'node',
  ]);
  $comment_type
    ->save();
  $this
    ->installConfig([
    'comment',
  ]);
  $this
    ->addDefaultCommentField('node', 'page');
  $comment = Comment::create([
    'status' => CommentInterface::PUBLISHED,
    'entity_type' => 'node',
    'entity_id' => $this->nodes[0]
      ->id(),
    'field_name' => 'comment',
    'body' => 'test body',
    'comment_type' => $comment_type
      ->id(),
  ]);
  $comment
    ->save();
  $this->comments[] = $comment;
  $values = [
    'status' => NodeInterface::PUBLISHED,
    'type' => 'page',
    'title' => 'some title',
  ];
  $this->nodes[1] = Node::create($values);
  $this->nodes[1]
    ->save();
  $values = [
    'status' => NodeInterface::NOT_PUBLISHED,
    'type' => 'page',
    'title' => 'other title',
  ];
  $this->nodes[2] = Node::create($values);
  $this->nodes[2]
    ->save();

  // Also index users, to verify that they are unaffected by the processor.
  $datasources = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugins($this->index, [
    'entity:comment',
    'entity:node',
    'entity:user',
  ]);
  $this->index
    ->setDatasources($datasources);
  $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());
}