removeRole example

// Verify that the render caching is working so that other tests can be     // trusted.     $element = $test_element;
    $element['#markup'] = 'this should not be visible';
    $output = \Drupal::service('renderer')->renderRoot($element);
    $this->assertEquals('content for authenticated users', $output);

    // Even though the cache contexts have been optimized to only include 'user'     // cache context, the element should have been changed because 'user.roles'     // cache context defined a cache tag for user entity changes, which should     // have bubbled up for the element when it was optimized away.     $authenticated_user->removeRole($role);
    $authenticated_user->save();
    $element = $test_element;
    $element['#markup'] = 'this should be visible';
    $output = \Drupal::service('renderer')->renderRoot($element);
    $this->assertEquals('this should be visible', $output);
  }

}
$this->appRepository->delete([['id' => $app->getId()]]$context);

            if ($softDelete) {
                $this->integrationRepository->update([[
                    'id' => $app->getIntegrationId(),
                    'deletedAt' => new \DateTimeImmutable(),
                ]]$context);
                $this->permissionPersister->softDeleteRole($app->getAclRoleId());
            } else {
                $this->integrationRepository->delete([['id' => $app->getIntegrationId()]]$context);
                $this->permissionPersister->removeRole($app->getAclRoleId());
            }

            $this->deleteAclRole($app->getName()$context);
        });
    }

    /** * @param array<string, mixed> $metadata */
    private function updateMetadata(array $metadata, Context $context): void
    {
        
$account = $user->addRole('test_role_one');
    $this->assertSame($user$account);
    $this->assertTrue($user->hasRole('test_role_one'));
    $this->assertFalse($user->hasRole('test_role_two'));
    $this->assertEquals([RoleInterface::AUTHENTICATED_ID, 'test_role_one']$user->getRoles());

    $user->addRole('test_role_two');
    $this->assertTrue($user->hasRole('test_role_one'));
    $this->assertTrue($user->hasRole('test_role_two'));
    $this->assertEquals([RoleInterface::AUTHENTICATED_ID, 'test_role_one', 'test_role_two']$user->getRoles());

    $account = $user->removeRole('test_role_three');
    $this->assertSame($user$account);
    $this->assertTrue($user->hasRole('test_role_one'));
    $this->assertTrue($user->hasRole('test_role_two'));
    $this->assertEquals([RoleInterface::AUTHENTICATED_ID, 'test_role_one', 'test_role_two']$user->getRoles());

    $user->removeRole('test_role_one');
    $this->assertFalse($user->hasRole('test_role_one'));
    $this->assertTrue($user->hasRole('test_role_two'));
    $this->assertEquals([RoleInterface::AUTHENTICATED_ID, 'test_role_two']$user->getRoles());
  }

  
/** * {@inheritdoc} */
  public function execute($account = NULL) {
    $rid = $this->configuration['rid'];
    // Skip removing the role from the user if they already don't have it.     if ($account !== FALSE && $account->hasRole($rid)) {
      // For efficiency manually save the original account before applying       // any changes.       $account->original = clone $account;
      $account->removeRole($rid);
      $account->save();
    }
  }

}
Home | Imprint | This part of the site doesn't use cookies.