public function UserProtectionTest::testRolesProtection in User protect 8
Tests if the user's roles field has the expected protection.
File
- tests/src/ Functional/ UserProtectionTest.php, line 74 
Class
- UserProtectionTest
- Tests each UserProtection plugin in action.
Namespace
Drupal\Tests\userprotect\FunctionalCode
public function testRolesProtection() {
  $protected_account = $this
    ->createProtectedUser([
    'user_roles',
  ]);
  // Add a role to the protected account.
  $rid1 = $this
    ->drupalCreateRole([]);
  $protected_account
    ->addRole($rid1);
  $protected_account
    ->save();
  // Add another role. We try to add this role to the user form later.
  $rid2 = $this
    ->drupalCreateRole([]);
  // Reload the user and check its roles.
  $protected_account = $this
    ->reloadEntity($protected_account);
  // Assert the protected account's roles.
  $this
    ->assertTrue($protected_account
    ->hasRole($rid1));
  $this
    ->assertFalse($protected_account
    ->hasRole($rid2));
  // Ensure a checkbox for the second role is not available.
  $this
    ->assertSession()
    ->fieldNotExists(sprintf('roles[%s]', $rid2));
}