You are here

public function ContentAccessTest::testContentAccessAll in Search API 8

Tests comment indexing when all users have access to content.

File

tests/src/Kernel/Processor/ContentAccessTest.php, line 247

Class

ContentAccessTest
Tests the "Content access" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function testContentAccessAll() {

  // Deactivate our custom grant and re-save the grant records.
  \Drupal::state()
    ->set('search_api_test_add_node_access_grant', FALSE);

  /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
  $access_control_handler = \Drupal::entityTypeManager()
    ->getAccessControlHandler('node');
  $grants_storage = \Drupal::getContainer()
    ->get('node.grant_storage');
  foreach ($this->nodes as $node) {
    $grants = $access_control_handler
      ->acquireGrants($node);
    $grants_storage
      ->write($node, $grants);
  }
  user_role_grant_permissions('anonymous', [
    'access content',
    'access comments',
  ]);
  $items = [];
  foreach ($this->comments as $comment) {
    $items[] = [
      'datasource' => 'entity:comment',
      'item' => $comment
        ->getTypedData(),
      'item_id' => $comment
        ->id(),
      'text' => 'Comment: ' . $comment
        ->id(),
    ];
  }
  $items = $this
    ->generateItems($items);

  // Add the processor's field values to the items.
  foreach ($items as $item) {
    $this->processor
      ->addFieldValues($item);
  }

  // Verify all items were indexed with the same "all" realm grant.
  $all = [
    'node_access_all:0',
  ];
  foreach ($items as $item) {
    $this
      ->assertEquals($all, $item
      ->getField('node_grants')
      ->getValues());
  }

  // Verify that the anonymous user has exactly that grant.
  $grants = node_access_grants('view', new AnonymousUserSession());
  $this
    ->assertEquals([
    'all' => [
      0,
    ],
  ], $grants);
}