function PermissionAccessCheckTest::testAccess

Same name in other branches
  1. 9 core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php \Drupal\Tests\user\Unit\PermissionAccessCheckTest::testAccess()
  2. 10 core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php \Drupal\Tests\user\Unit\PermissionAccessCheckTest::testAccess()
  3. 11.x core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php \Drupal\Tests\user\Unit\PermissionAccessCheckTest::testAccess()

Tests the access check method.

@dataProvider providerTestAccess @covers ::access

File

core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php, line 71

Class

PermissionAccessCheckTest
@coversDefaultClass \Drupal\user\Access\PermissionAccessCheck @group Routing @group Access

Namespace

Drupal\Tests\user\Unit

Code

public function testAccess($requirements, $access, array $contexts = [], $message = '') {
    $access_result = AccessResult::allowedIf($access)->addCacheContexts($contexts);
    if (!empty($message)) {
        $access_result->setReason($message);
    }
    $user = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
    $user->expects($this->any())
        ->method('hasPermission')
        ->will($this->returnValueMap([
        [
            'allowed',
            TRUE,
        ],
        [
            'denied',
            FALSE,
        ],
        [
            'other',
            FALSE,
        ],
    ]));
    $route = new Route('', [], $requirements);
    $this->assertEquals($access_result, $this->accessCheck
        ->access($route, $user));
}

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