You are here

protected function IntegrationTest::checkAccessChecks in Search API Saved Searches 8

Makes sure access checks work correctly.

1 call to IntegrationTest::checkAccessChecks()
IntegrationTest::testModule in tests/src/Functional/IntegrationTest.php
Tests overall functionality of the module.

File

tests/src/Functional/IntegrationTest.php, line 479

Class

IntegrationTest
Tests overall functionality of the module.

Namespace

Drupal\Tests\search_api_saved_searches\Functional

Code

protected function checkAccessChecks() {
  $assert_session = $this
    ->assertSession();

  // Make sure we really have all the expected saved searches present, to
  // avoid confusing assertion failures later.
  $total = \Drupal::entityQuery('search_api_saved_search')
    ->accessCheck(FALSE)
    ->count()
    ->execute();
  $this
    ->assertEquals(4, $total);
  $user1_uid = $this->registeredUser
    ->id();
  $tests = [
    'anonymous' => [
      'account' => NULL,
      'access' => [],
    ],
    'admin' => [
      'account' => $this->adminUser,
      'access' => [
        0,
        $user1_uid,
      ],
    ],
    '1st registered' => [
      'account' => $this->registeredUser,
      'access' => [
        $user1_uid,
      ],
    ],
    '2nd registered' => [
      'account' => $this->registeredUser2,
      'access' => [],
    ],
  ];
  foreach ($tests as $key => $info) {
    if ($this->loggedInUser) {
      $this
        ->drupalLogout();
    }
    if ($info['account']) {
      $this
        ->drupalLogin($info['account']);
    }
    foreach ([
      0,
      $user1_uid,
    ] as $uid) {

      // Unfortunately, web assertions don't let us pass a message, so we have
      // to use this to get any information on where this failed.
      $this
        ->verbose("Testing view for saved searches of user #{$uid} as {$key} user.");
      $this
        ->drupalGet("user/{$uid}/saved-searches");
      if (in_array($uid, $info['access'])) {
        $assert_session
          ->pageTextNotContains('The requested page could not be found.');
      }
      else {
        $assert_session
          ->pageTextContains('The requested page could not be found.');
      }
    }
  }
}