You are here

public function AccessTest::testAccess in Search API Autocomplete 8

Tests access to the autocomplete path under a given set of conditions.

@covers ::access

@dataProvider accessTestDataProvider

Parameters

array $options: Associative array of options, containing one or more of the following:

  • status: Whether the search should be enabled.
  • index: Whether the search's index should exist.
  • index_status: Whether the search's index should be enabled.
  • permission: Whether the user should have the necessary permission to access the search.
  • admin: Whether the user should have the "administer search_api_autocomplete" permission.

All options default to TRUE.

bool $should_be_allowed: Whether access should be allowed.

File

tests/src/Unit/AccessTest.php, line 82

Class

AccessTest
Tests access to the autocomplete path.

Namespace

Drupal\Tests\search_api_autocomplete\Unit

Code

public function testAccess(array $options, $should_be_allowed) {
  $options += [
    'status' => TRUE,
    'index' => TRUE,
    'index_status' => TRUE,
    'permission' => TRUE,
    'admin' => TRUE,
  ];
  $this->search
    ->method('status')
    ->willReturn($options['status']);
  $this->search
    ->method('hasValidIndex')
    ->willReturn($options['index']);
  if ($options['index']) {
    $index = $this
      ->createMock(IndexInterface::class);
    $index
      ->method('status')
      ->willReturn($options['index_status']);
    $this->search
      ->method('getIndex')
      ->willReturn($index);
  }

  /** @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject $account */
  $account = $this
    ->createMock(AccountInterface::class);
  $permission = 'use search_api_autocomplete for ' . $this->search
    ->id();
  $account
    ->method('hasPermission')
    ->willReturnMap([
    [
      $permission,
      $options['permission'],
    ],
    [
      'administer search_api_autocomplete',
      $options['admin'],
    ],
  ]);

  // Needn't really be AccessResultNeutral, of course, but this is the easiest
  // way to get all the possible interfaces.

  /** @var \Drupal\Core\Access\AccessResultNeutral $result */
  $result = $this->autocompleteHelper
    ->access($this->search, $account);
  $this
    ->assertEquals($should_be_allowed, $result
    ->isAllowed());
  $this
    ->assertEquals(FALSE, $result
    ->isForbidden());
  $this
    ->assertEquals(!$should_be_allowed, $result
    ->isNeutral());
  $this
    ->assertInstanceOf(CacheableDependencyInterface::class, $result);
  $this
    ->assertContains('test', $result
    ->getCacheContexts());
  $this
    ->assertContains('test', $result
    ->getCacheTags());
  $this
    ->assertEquals(1337, $result
    ->getCacheMaxAge());
  if (!$should_be_allowed) {
    $this
      ->assertInstanceOf(AccessResultReasonInterface::class, $result);
    $this
      ->assertEquals("The \"{$permission}\" permission is required and autocomplete for this search must be enabled.", $result
      ->getReason());
  }
}