You are here

public function RoleAccessTest::testRoleBasedAccess in Search API 8

Tests the role based access filtering.

File

tests/src/Kernel/Processor/RoleAccessTest.php, line 89

Class

RoleAccessTest
Tests the "Role-based access" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function testRoleBasedAccess() {
  $allowed_foo = $this
    ->createTestNode('allow for foo role');
  $allowed_bar = $this
    ->createTestNode('allow for bar role');
  $allowed_anonymous = $this
    ->createTestNode('allow for anonymous role');
  $allowed_authenticated = $this
    ->createTestNode('allow for authenticated role');
  $allowed_all = $this
    ->createTestNode('allow for foo, bar, baz, anonymous, authenticated role');
  $this->index
    ->reindex();
  $this
    ->indexItems();
  $query = \Drupal::getContainer()
    ->get('search_api.query_helper')
    ->createQuery($this->index);
  $expected = [
    'foo' => [
      'node' => [
        $allowed_all,
        $allowed_foo,
        $allowed_authenticated,
      ],
    ],
    'bar' => [
      'node' => [
        $allowed_all,
        $allowed_bar,
        $allowed_authenticated,
      ],
    ],
    'baz' => [
      'node' => [
        $allowed_all,
        $allowed_authenticated,
      ],
    ],
    'anonymous' => [
      'node' => [
        $allowed_anonymous,
        $allowed_all,
      ],
    ],
    'authenticated' => [
      'node' => [
        $allowed_authenticated,
        $allowed_all,
      ],
    ],
  ];
  foreach ($expected as $role_id => $expected_role_results) {
    $cloned_query = clone $query;
    $cloned_query
      ->setOption('search_api_access_account', $this->testUsers[$role_id]);
    $result = $cloned_query
      ->execute();
    $this
      ->assertResults($result, $expected_role_results);
  }
}