You are here

public function AccessResultTest::testCacheContexts in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheContexts()

@covers ::addCacheContexts @covers ::resetCacheContexts @covers ::getCacheContexts @covers ::cachePerPermissions @covers ::cachePerUser @covers ::allowedIfHasPermission

File

core/tests/Drupal/Tests/Core/Access/AccessResultTest.php, line 336
Contains \Drupal\Tests\Core\Access\AccessResultTest.

Class

AccessResultTest
@coversDefaultClass \Drupal\Core\Access\AccessResult @group Access

Namespace

Drupal\Tests\Core\Access

Code

public function testCacheContexts() {
  $verify = function (AccessResult $access, array $contexts) {
    $this
      ->assertFalse($access
      ->isAllowed());
    $this
      ->assertFalse($access
      ->isForbidden());
    $this
      ->assertTrue($access
      ->isNeutral());
    $this
      ->assertSame(Cache::PERMANENT, $access
      ->getCacheMaxAge());
    $this
      ->assertSame($contexts, $access
      ->getCacheContexts());
    $this
      ->assertSame([], $access
      ->getCacheTags());
  };
  $access = AccessResult::neutral()
    ->addCacheContexts([
    'foo',
  ]);
  $verify($access, [
    'foo',
  ]);

  // Verify resetting works.
  $access
    ->resetCacheContexts();
  $verify($access, []);

  // Verify idempotency.
  $access
    ->addCacheContexts([
    'foo',
  ])
    ->addCacheContexts([
    'foo',
  ]);
  $verify($access, [
    'foo',
  ]);

  // Verify same values in different call order yields the same result.
  $access
    ->resetCacheContexts()
    ->addCacheContexts([
    'foo',
  ])
    ->addCacheContexts([
    'bar',
  ]);
  $verify($access, [
    'bar',
    'foo',
  ]);
  $access
    ->resetCacheContexts()
    ->addCacheContexts([
    'bar',
  ])
    ->addCacheContexts([
    'foo',
  ]);
  $verify($access, [
    'bar',
    'foo',
  ]);

  // ::cachePerPermissions() convenience method.
  $contexts = array(
    'user.permissions',
  );
  $a = AccessResult::neutral()
    ->addCacheContexts($contexts);
  $verify($a, $contexts);
  $b = AccessResult::neutral()
    ->cachePerPermissions();
  $verify($b, $contexts);
  $this
    ->assertEquals($a, $b);

  // ::cachePerUser() convenience method.
  $contexts = array(
    'user',
  );
  $a = AccessResult::neutral()
    ->addCacheContexts($contexts);
  $verify($a, $contexts);
  $b = AccessResult::neutral()
    ->cachePerUser();
  $verify($b, $contexts);
  $this
    ->assertEquals($a, $b);

  // Both.
  $contexts = array(
    'user',
    'user.permissions',
  );
  $a = AccessResult::neutral()
    ->addCacheContexts($contexts);
  $verify($a, $contexts);
  $b = AccessResult::neutral()
    ->cachePerPermissions()
    ->cachePerUser();
  $verify($b, $contexts);
  $c = AccessResult::neutral()
    ->cachePerUser()
    ->cachePerPermissions();
  $verify($c, $contexts);
  $this
    ->assertEquals($a, $b);
  $this
    ->assertEquals($a, $c);

  // ::allowIfHasPermission and ::allowedIfHasPermission convenience methods.
  $account = $this
    ->getMock('\\Drupal\\Core\\Session\\AccountInterface');
  $account
    ->expects($this
    ->any())
    ->method('hasPermission')
    ->with('may herd llamas')
    ->will($this
    ->returnValue(FALSE));
  $contexts = array(
    'user.permissions',
  );

  // Verify the object when using the ::allowedIfHasPermission() convenience
  // static method.
  $b = AccessResult::allowedIfHasPermission($account, 'may herd llamas');
  $verify($b, $contexts);
}