class PermissionAccessCheckTest

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

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

Hierarchy

Expanded class hierarchy of PermissionAccessCheckTest

File

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

Namespace

Drupal\Tests\user\Unit
View source
class PermissionAccessCheckTest extends UnitTestCase {
    
    /**
     * The tested access checker.
     *
     * @var \Drupal\user\Access\PermissionAccessCheck
     */
    public $accessCheck;
    
    /**
     * The dependency injection container.
     *
     * @var \Symfony\Component\DependencyInjection\ContainerBuilder
     */
    protected $container;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->container = new ContainerBuilder();
        $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
        $cache_contexts_manager->assertValidTokens()
            ->willReturn(TRUE);
        $cache_contexts_manager->reveal();
        $this->container
            ->set('cache_contexts_manager', $cache_contexts_manager);
        \Drupal::setContainer($this->container);
        $this->accessCheck = new PermissionAccessCheck();
    }
    
    /**
     * Provides data for the testAccess method.
     *
     * @return array
     */
    public static function providerTestAccess() {
        return [
            [
                [],
                FALSE,
            ],
            [
                [
                    '_permission' => 'allowed',
                ],
                TRUE,
                [
                    'user.permissions',
                ],
            ],
            [
                [
                    '_permission' => 'denied',
                ],
                FALSE,
                [
                    'user.permissions',
                ],
                "The 'denied' permission is required.",
            ],
            [
                [
                    '_permission' => 'allowed+denied',
                ],
                TRUE,
                [
                    'user.permissions',
                ],
            ],
            [
                [
                    '_permission' => 'allowed+denied+other',
                ],
                TRUE,
                [
                    'user.permissions',
                ],
            ],
            [
                [
                    '_permission' => 'allowed,denied',
                ],
                FALSE,
                [
                    'user.permissions',
                ],
                "The following permissions are required: 'allowed' AND 'denied'.",
            ],
        ];
    }
    
    /**
     * Tests the access check method.
     *
     * @dataProvider providerTestAccess
     * @covers ::access
     */
    public function testAccess($requirements, $access, array $contexts = [], $message = '') : void {
        $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')
            ->willReturnMap([
            [
                'allowed',
                TRUE,
            ],
            [
                'denied',
                FALSE,
            ],
            [
                'other',
                FALSE,
            ],
        ]);
        $route = new Route('', [], $requirements);
        $this->assertEquals($access_result, $this->accessCheck
            ->access($route, $user));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
PermissionAccessCheckTest::$accessCheck public property The tested access checker.
PermissionAccessCheckTest::$container protected property The dependency injection container.
PermissionAccessCheckTest::providerTestAccess public static function Provides data for the testAccess method.
PermissionAccessCheckTest::setUp protected function Overrides UnitTestCase::setUp
PermissionAccessCheckTest::testAccess public function Tests the access check method.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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