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. 10 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheContexts()
  3. 11.x 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 403

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 = [
        '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->assertEquals($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')
        ->will($this->returnValue(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.