Skip to content
Snippets Groups Projects
Unverified Commit 1f559ea8 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3358586 by godotislate, kala4ek, jaswinsingh, benjifisher, creact,...

Issue #3358586 by godotislate, kala4ek, jaswinsingh, benjifisher, creact, catch, simohell, alexpott, alfthecat, aaronbauman, rupertj, poker10: RuntimeException: Adding non-existent permissions to a role is not allowed

(cherry picked from commit 0348fc51)
parent b5ccec75
No related branches found
No related tags found
16 merge requests!11887Issue #3520065: The migrate Row class API is incomplete,!11636Draft: Issue #3515643 by macsim: fieldNameExists method is inconsistent,!11515Issue #3480419 by mondrake, smustgrave, catch: Method...,!11380Issue #3490698 by catch, spokje: Bump MINIMUM_STABILITY back to 'stable' when...,!11281Use Drupal Core Leadership terminology in MAINTAINERS.txt,!11239Issue #3507548: Allow workspace changes listing to show all items, without a pager,!11238Fix issue #3051797,!11213Issue #3506743 by tomislav.matokovic: Increasing the color contrast for the navigation block title against the background of the navigation sidebar to at least 4.5:1,!11147Draft: Try to avoid manually setting required cache contexts,!11108Issue #3490298 by nicxvan: Profiles can be missed in OOP hooks,!11093Drupal on MongoDB 11.1.x,!11017Issue #3502540: Add date filter for moderated content.,!11009Issue #3486972 migrate feed icon,!10999Cleaning up Taxonomy hooks and updating baseline.,!10977Issue #3501457: Fix path used in a A11y Test Admin,!10881Issue #3489329 by mfb, casey: symfony/http-foundation commit 32310ff breaks PathValidator
Pipeline #389819 passed
Pipeline: drupal

#389821

    ......@@ -211,10 +211,15 @@ public function calculateDependencies() {
    $valid_permissions = array_intersect($this->permissions, array_keys($permission_definitions));
    $invalid_permissions = array_diff($this->permissions, $valid_permissions);
    if (!empty($invalid_permissions)) {
    throw new \RuntimeException('Adding non-existent permissions to a role is not allowed. The incorrect permissions are "' . implode('", "', $invalid_permissions) . '".');
    \Drupal::logger('user')->error('Non-existent permission(s) assigned to role "@label" (@id) were removed. Invalid permission(s): @permissions.', [
    '@label' => $this->label(),
    '@id' => $this->id(),
    '@permissions' => implode(', ', $invalid_permissions),
    ]);
    $this->permissions = $valid_permissions;
    }
    foreach ($valid_permissions as $permission) {
    // Depend on the module that is providing this permissions.
    // Depend on the module that is providing this permission.
    $this->addDependency('module', $permission_definitions[$permission]['provider']);
    // Depend on any other dependencies defined by permissions granted to
    // this role.
    ......
    ......@@ -4,8 +4,11 @@
    namespace Drupal\Tests\user\Kernel;
    use Drupal\Core\DependencyInjection\ContainerBuilder;
    use Drupal\Core\Logger\RfcLogLevel;
    use Drupal\KernelTests\KernelTestBase;
    use Drupal\user\Entity\Role;
    use Symfony\Component\ErrorHandler\BufferingLogger;
    /**
    * @group user
    ......@@ -18,6 +21,16 @@ class UserRoleEntityTest extends KernelTestBase {
    */
    protected static $modules = ['system', 'user', 'user_permissions_test'];
    /**
    * {@inheritdoc}
    */
    public function register(ContainerBuilder $container): void {
    parent::register($container);
    $container
    ->register(BufferingLogger::class)
    ->addTag('logger');
    }
    public function testOrderOfPermissions(): void {
    $role = Role::create(['id' => 'test_role', 'label' => 'Test role']);
    $role->grantPermission('b')
    ......@@ -37,17 +50,27 @@ public function testGrantingNonExistentPermission(): void {
    $role = Role::create(['id' => 'test_role', 'label' => 'Test role']);
    // A single permission that does not exist.
    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage('Adding non-existent permissions to a role is not allowed. The incorrect permissions are "does not exist".');
    $role->grantPermission('does not exist')
    ->save();
    $log_message = \Drupal::service(BufferingLogger::class)->cleanLogs()[0];
    $this->assertSame(RfcLogLevel::ERROR, $log_message[0]);
    $this->assertSame('Non-existent permission(s) assigned to role "@label" (@id) were removed. Invalid permission(s): @permissions.', $log_message[1]);
    $this->assertSame('Test role', $log_message[2]['@label']);
    $this->assertSame('test_role', $log_message[2]['@id']);
    $this->assertSame('does not exist', $log_message[2]['@permissions']);
    // A multiple permissions that do not exist.
    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage('Adding non-existent permissions to a role is not allowed. The incorrect permissions are "does not exist, also does not exist".');
    // Multiple permissions that do not exist.
    $role->grantPermission('does not exist')
    ->grantPermission('also does not exist')
    ->save();
    $log_message = \Drupal::service(BufferingLogger::class)->cleanLogs()[0];
    $this->assertSame(RfcLogLevel::ERROR, $log_message[0]);
    $this->assertSame('Non-existent permission(s) assigned to role "@label" (@id) were removed. Invalid permission(s): @permissions.', $log_message[1]);
    $this->assertSame('Test role', $log_message[2]['@label']);
    $this->assertSame('test_role', $log_message[2]['@id']);
    $this->assertSame('does not exist, also does not exist', $log_message[2]['@permissions']);
    $permissions = $role->getPermissions();
    $this->assertEmpty(array_intersect(['does not exist', 'also does not exist'], $permissions));
    }
    public function testPermissionRevokeAndConfigSync(): void {
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment