function AccessResultTest::testCacheContexts

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheContexts()
  2. 8.9.x core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheContexts()
  3. 10 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 401

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testCacheContexts() : void {
    $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->assertEqualsCanonicalizing($contexts, $access->getCacheContexts());
        $this->assertEqualsCanonicalizing([], $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 = [
        '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 = [
        'user',
    ];
    $a = AccessResult::neutral()->addCacheContexts($contexts);
    $verify($a, $contexts);
    $b = AccessResult::neutral()->cachePerUser();
    $verify($b, $contexts);
    $this->assertEquals($a, $b);
    // Both.
    $contexts = [
        '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->assertEqualsCanonicalizing($a, $b);
    $this->assertEquals($a, $c);
    // ::allowIfHasPermission and ::allowedIfHasPermission convenience methods.
    $account = $this->createMock('\\Drupal\\Core\\Session\\AccountInterface');
    $account->expects($this->any())
        ->method('hasPermission')
        ->with('may herd llamas')
        ->willReturn(FALSE);
    $contexts = [
        'user.permissions',
    ];
    // Verify the object when using the ::allowedIfHasPermission() convenience
    // static method.
    $b = AccessResult::allowedIfHasPermission($account, 'may herd llamas');
    $verify($b, $contexts);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.