function PermissionHandlerTest::testBuildPermissionsYamlCallback

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

Tests dynamic callback permissions provided by YML files.

@covers ::__construct @covers ::getPermissions @covers ::buildPermissionsYaml

File

core/modules/user/tests/src/Unit/PermissionHandlerTest.php, line 212

Class

PermissionHandlerTest
Tests the permission handler.

Namespace

Drupal\Tests\user\Unit

Code

public function testBuildPermissionsYamlCallback() : void {
    vfsStreamWrapper::register();
    $root = new vfsStreamDirectory('modules');
    vfsStreamWrapper::setRoot($root);
    $this->moduleHandler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $this->moduleHandler
        ->expects($this->once())
        ->method('getModuleDirectories')
        ->willReturn([
        'module_a' => vfsStream::url('modules/module_a'),
        'module_b' => vfsStream::url('modules/module_b'),
        'module_c' => vfsStream::url('modules/module_c'),
    ]);
    $url = vfsStream::url('modules');
    mkdir($url . '/module_a');
    file_put_contents($url . '/module_a/module_a.permissions.yml', <<<EOF
permission_callbacks:
  - 'Drupal\\user\\Tests\\TestPermissionCallbacks::singleDescription'
EOF
);
    mkdir($url . '/module_b');
    file_put_contents($url . '/module_b/module_b.permissions.yml', <<<EOF
permission_callbacks:
  - 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription'
  - 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleProvider'
EOF
);
    mkdir($url . '/module_c');
    file_put_contents($url . '/module_c/module_c.permissions.yml', <<<EOF
permission_callbacks:
  - 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescriptionRestrictAccess'
EOF
);
    $modules = [
        'module_a',
        'module_b',
        'module_c',
    ];
    $this->moduleHandler
        ->expects($this->any())
        ->method('getModuleList')
        ->willReturn(array_flip($modules));
    $this->callableResolver
        ->expects($this->exactly(4))
        ->method('getCallableFromDefinition')
        ->willReturnMap([
        [
            'Drupal\\user\\Tests\\TestPermissionCallbacks::singleDescription',
            [
                new TestPermissionCallbacks(),
                'singleDescription',
            ],
        ],
        [
            'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription',
            [
                new TestPermissionCallbacks(),
                'titleDescription',
            ],
        ],
        [
            'Drupal\\user\\Tests\\TestPermissionCallbacks::titleProvider',
            [
                new TestPermissionCallbacks(),
                'titleProvider',
            ],
        ],
        [
            'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescriptionRestrictAccess',
            [
                new TestPermissionCallbacks(),
                'titleDescriptionRestrictAccess',
            ],
        ],
    ]);
    $module_extension_list = $this->createMock(ModuleExtensionList::class);
    $this->permissionHandler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->callableResolver, $module_extension_list);
    $actual_permissions = $this->permissionHandler
        ->getPermissions();
    $this->assertPermissions($actual_permissions);
}

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