From 03fb185210beb27d7df0b0c8aa24837a5255f8f6 Mon Sep 17 00:00:00 2001
From: Brian Canini <canini.16@osu.edu>
Date: Wed, 4 Mar 2020 10:58:33 -0500
Subject: [PATCH] Updating drupal/administerusersbyrole (2.0.0-alpha6 =>
 2.0.0-beta1)

---
 composer.json                                 |   4 +-
 composer.lock                                 |  20 ++--
 vendor/composer/installed.json                |  20 ++--
 .../administerusersbyrole.info.yml            |   6 +-
 .../administerusersbyrole.module              |  26 ++++-
 .../Constraint/OverrideUserMailRequired.php   |  16 +++
 .../OverrideUserMailRequiredValidator.php     |  40 +++++++
 .../src/Routing/RouteSubscriber.php           |  11 +-
 .../Functional/AdministerusersbyroleTest.php} | 104 ++++++------------
 9 files changed, 138 insertions(+), 109 deletions(-)
 create mode 100644 web/modules/administerusersbyrole/src/Constraint/OverrideUserMailRequired.php
 create mode 100644 web/modules/administerusersbyrole/src/Constraint/OverrideUserMailRequiredValidator.php
 rename web/modules/administerusersbyrole/{src/Tests/AdministerusersbyroleTestCaseTest.php => tests/src/Functional/AdministerusersbyroleTest.php} (79%)

diff --git a/composer.json b/composer.json
index 8de26d8ad5..9747fe9c50 100644
--- a/composer.json
+++ b/composer.json
@@ -90,7 +90,7 @@
         "drupal/address": "1.1",
         "drupal/addtocalendar": "3.1",
         "drupal/admin_toolbar": "2.0",
-        "drupal/administerusersbyrole": "2.0-alpha6",
+        "drupal/administerusersbyrole": "2.0-beta1",
         "drupal/allowed_formats": "1.1",
         "drupal/anchor_link": "^1.6",
         "drupal/better_exposed_filters": "3.0-alpha6",
@@ -316,4 +316,4 @@
             "php": "7.0.8"
         }
     }
-}
\ No newline at end of file
+}
diff --git a/composer.lock b/composer.lock
index e304ef53bb..95585aa272 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "9cebdfa2dca9a89bb18005ed1f9db499",
+    "content-hash": "5faa9d0a6256c2306fa89a2c82ecb881",
     "packages": [
         {
             "name": "alchemy/zippy",
@@ -2191,17 +2191,17 @@
         },
         {
             "name": "drupal/administerusersbyrole",
-            "version": "2.0.0-alpha6",
+            "version": "2.0.0-beta1",
             "source": {
                 "type": "git",
                 "url": "https://git.drupalcode.org/project/administerusersbyrole.git",
-                "reference": "8.x-2.0-alpha6"
+                "reference": "8.x-2.0-beta1"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://ftp.drupal.org/files/projects/administerusersbyrole-8.x-2.0-alpha6.zip",
-                "reference": "8.x-2.0-alpha6",
-                "shasum": "08c6bb6ac8aa6e7f9e089b134da636fa8b03c338"
+                "url": "https://ftp.drupal.org/files/projects/administerusersbyrole-8.x-2.0-beta1.zip",
+                "reference": "8.x-2.0-beta1",
+                "shasum": "735162d6d1a5f393598bf97a420de354abef3218"
             },
             "require": {
                 "drupal/core": "~8.0"
@@ -2212,11 +2212,11 @@
                     "dev-2.x": "2.x-dev"
                 },
                 "drupal": {
-                    "version": "8.x-2.0-alpha6",
-                    "datestamp": "1527757985",
+                    "version": "8.x-2.0-beta1",
+                    "datestamp": "1553362685",
                     "security-coverage": {
                         "status": "not-covered",
-                        "message": "Alpha releases are not covered by Drupal security advisories."
+                        "message": "Beta releases are not covered by Drupal security advisories."
                     }
                 }
             },
@@ -2241,7 +2241,7 @@
             "description": "Allows users with 'administer users' permission and a role (specified in 'Permissions') to edit/delete other users with a specified role.  Also provides control over user creation.",
             "homepage": "https://www.drupal.org/project/administerusersbyrole",
             "support": {
-                "source": "http://cgit.drupalcode.org/administerusersbyrole"
+                "source": "https://git.drupalcode.org/project/administerusersbyrole"
             }
         },
         {
diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json
index 899a4506a1..134a538002 100644
--- a/vendor/composer/installed.json
+++ b/vendor/composer/installed.json
@@ -2258,18 +2258,18 @@
     },
     {
         "name": "drupal/administerusersbyrole",
-        "version": "2.0.0-alpha6",
-        "version_normalized": "2.0.0.0-alpha6",
+        "version": "2.0.0-beta1",
+        "version_normalized": "2.0.0.0-beta1",
         "source": {
             "type": "git",
             "url": "https://git.drupalcode.org/project/administerusersbyrole.git",
-            "reference": "8.x-2.0-alpha6"
+            "reference": "8.x-2.0-beta1"
         },
         "dist": {
             "type": "zip",
-            "url": "https://ftp.drupal.org/files/projects/administerusersbyrole-8.x-2.0-alpha6.zip",
-            "reference": "8.x-2.0-alpha6",
-            "shasum": "08c6bb6ac8aa6e7f9e089b134da636fa8b03c338"
+            "url": "https://ftp.drupal.org/files/projects/administerusersbyrole-8.x-2.0-beta1.zip",
+            "reference": "8.x-2.0-beta1",
+            "shasum": "735162d6d1a5f393598bf97a420de354abef3218"
         },
         "require": {
             "drupal/core": "~8.0"
@@ -2280,11 +2280,11 @@
                 "dev-2.x": "2.x-dev"
             },
             "drupal": {
-                "version": "8.x-2.0-alpha6",
-                "datestamp": "1527757985",
+                "version": "8.x-2.0-beta1",
+                "datestamp": "1553362685",
                 "security-coverage": {
                     "status": "not-covered",
-                    "message": "Alpha releases are not covered by Drupal security advisories."
+                    "message": "Beta releases are not covered by Drupal security advisories."
                 }
             }
         },
@@ -2310,7 +2310,7 @@
         "description": "Allows users with 'administer users' permission and a role (specified in 'Permissions') to edit/delete other users with a specified role.  Also provides control over user creation.",
         "homepage": "https://www.drupal.org/project/administerusersbyrole",
         "support": {
-            "source": "http://cgit.drupalcode.org/administerusersbyrole"
+            "source": "https://git.drupalcode.org/project/administerusersbyrole"
         }
     },
     {
diff --git a/web/modules/administerusersbyrole/administerusersbyrole.info.yml b/web/modules/administerusersbyrole/administerusersbyrole.info.yml
index f9972b50e0..a8f53cd413 100644
--- a/web/modules/administerusersbyrole/administerusersbyrole.info.yml
+++ b/web/modules/administerusersbyrole/administerusersbyrole.info.yml
@@ -3,8 +3,8 @@ description: "Allows users with 'administer users' permission and a role (specif
 # core: 8.x
 type: module
 
-# Information added by Drupal.org packaging script on 2018-05-31
-version: '8.x-2.0-alpha6'
+# Information added by Drupal.org packaging script on 2019-03-23
+version: '8.x-2.0-beta1'
 core: '8.x'
 project: 'administerusersbyrole'
-datestamp: 1527757992
+datestamp: 1553362692
diff --git a/web/modules/administerusersbyrole/administerusersbyrole.module b/web/modules/administerusersbyrole/administerusersbyrole.module
index b5060acd5d..5b72d18575 100644
--- a/web/modules/administerusersbyrole/administerusersbyrole.module
+++ b/web/modules/administerusersbyrole/administerusersbyrole.module
@@ -120,12 +120,12 @@ function administerusersbyrole_entity_field_access($operation, FieldDefinitionIn
 }
 
 /**
- * Implements hook_user_mail_required_alter()
+ * Implements hook_validation_constraint_alter().
+ *
+ * @todo Remove when https://www.drupal.org/node/2992848 is fixed.
  */
-function administerusersbyrole_user_mail_required_alter(&$required, AccountInterface $user) {
-  if ($user->hasPermission('allow empty user mail')) {
-    $required = FALSE;
-  }
+function administerusersbyrole_validation_constraint_alter(array &$definitions) {
+  $definitions['UserMailRequired']['class'] = '\Drupal\administerusersbyrole\Constraint\OverrideUserMailRequired';
 }
 
 /**
@@ -159,7 +159,7 @@ function administerusersbyrole_query_administerusersbyrole_edit_access_alter(Alt
     // Hide any user accounts that the user does not have permission to edit.
     // If an account has multiple roles, we make sure the current user has
     // permission to edit all assigned roles.
-    if (isset($exclude)) {
+    if (!empty($exclude)) {
       // This code was changed from D7 to workaround D8 core bug https://www.drupal.org/node/2744069.
 
       // Get a list of uids with roles that the user does not have permission
@@ -174,6 +174,20 @@ function administerusersbyrole_query_administerusersbyrole_edit_access_alter(Alt
   }
 }
 
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function administerusersbyrole_form_user_form_alter(&$form, &$form_state) {
+  $user = $form_state->getFormObject()->getEntity();
+  $account = Drupal::currentUser();
+
+  // Allow empty email.
+  // @todo Remove when https://www.drupal.org/node/2992848 is fixed.
+  if (!$user->getEmail() && $account->hasPermission('allow empty user mail')) {
+    $form['account']['mail']['#required'] = FALSE;
+  }
+}
+
 /**
  * Implements hook_form_FORM_ID_alter().
  *
diff --git a/web/modules/administerusersbyrole/src/Constraint/OverrideUserMailRequired.php b/web/modules/administerusersbyrole/src/Constraint/OverrideUserMailRequired.php
new file mode 100644
index 0000000000..cb92a6efb7
--- /dev/null
+++ b/web/modules/administerusersbyrole/src/Constraint/OverrideUserMailRequired.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Drupal\administerusersbyrole\Constraint;
+
+use Drupal\user\Plugin\Validation\Constraint\UserMailRequired;
+
+/**
+ * Checks if the user's email address is provided if required.
+ *
+ * The user mail field is NOT required if account originally had no mail set
+ * and the user performing the edit has permission to set empty user mail.
+ * This allows users without email address to be edited and deleted.
+ */
+class OverrideUserMailRequired extends UserMailRequired {
+
+}
diff --git a/web/modules/administerusersbyrole/src/Constraint/OverrideUserMailRequiredValidator.php b/web/modules/administerusersbyrole/src/Constraint/OverrideUserMailRequiredValidator.php
new file mode 100644
index 0000000000..069e26eb8a
--- /dev/null
+++ b/web/modules/administerusersbyrole/src/Constraint/OverrideUserMailRequiredValidator.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\administerusersbyrole\Constraint;
+
+use Drupal\user\Plugin\Validation\Constraint\UserMailRequiredValidator;
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * Checks if the user's email address is provided if required.
+ *
+ * The user mail field is NOT required if account originally had no mail set
+ * and the user performing the edit has permission to set empty user mail.
+ * This allows users without email address to be edited and deleted.
+ */
+class OverrideUserMailRequiredValidator extends UserMailRequiredValidator {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validate($items, Constraint $constraint) {
+    /** @var \Drupal\Core\Field\FieldItemListInterface $items */
+    /** @var \Drupal\user\UserInterface $account */
+    $account = $items->getEntity();
+    $existing_value = NULL;
+    if ($account->id()) {
+      $account_unchanged = \Drupal::entityManager()
+        ->getStorage('user')
+        ->loadUnchanged($account->id());
+      $existing_value = $account_unchanged->getEmail();
+    }
+
+    $has_permission = \Drupal::currentUser()->hasPermission('administer users') || \Drupal::currentUser()->hasPermission('allow empty user mail');
+    $required = !(!$existing_value && $has_permission);
+
+    if ($required && (!isset($items) || $items->isEmpty())) {
+      $this->context->addViolation($constraint->message, ['@name' => $account->getFieldDefinition('mail')->getLabel()]);
+    }
+  }
+
+}
diff --git a/web/modules/administerusersbyrole/src/Routing/RouteSubscriber.php b/web/modules/administerusersbyrole/src/Routing/RouteSubscriber.php
index fb8dc70c57..5cafd3bb60 100644
--- a/web/modules/administerusersbyrole/src/Routing/RouteSubscriber.php
+++ b/web/modules/administerusersbyrole/src/Routing/RouteSubscriber.php
@@ -21,16 +21,13 @@ public function alterRoutes(RouteCollection $collection) {
       $route->setRequirement('_permission', $perm);
     }
 
-    // @todo: Remove depending on outcome of https://www.drupal.org/project/drupal/issues/2854252
+    // The route 'user.multiple_cancel_confirm' specifically checks for access
+    // to each user.  Hence in theory we could remove the requirement entirely,
+    // but it's safer to set a basic permission.
     if ($route = $collection->get('user.multiple_cancel_confirm')) {
       $perm = $route->getRequirement('_permission') . '+access users overview';
       $route->setRequirement('_permission', $perm);
     }
-
-    // @todo: Currently in patch for https://www.drupal.org/project/drupal/issues/2854252
-    //if ($route = $collection->get('user.admin_create')) {
-    //  $perm = $route->getRequirement('_permission') . '+create users';
-    //  $route->setRequirement('_permission', $perm);
-    //}
   }
+
 }
diff --git a/web/modules/administerusersbyrole/src/Tests/AdministerusersbyroleTestCaseTest.php b/web/modules/administerusersbyrole/tests/src/Functional/AdministerusersbyroleTest.php
similarity index 79%
rename from web/modules/administerusersbyrole/src/Tests/AdministerusersbyroleTestCaseTest.php
rename to web/modules/administerusersbyrole/tests/src/Functional/AdministerusersbyroleTest.php
index 30a00108d4..c4e836f90f 100644
--- a/web/modules/administerusersbyrole/src/Tests/AdministerusersbyroleTestCaseTest.php
+++ b/web/modules/administerusersbyrole/tests/src/Functional/AdministerusersbyroleTest.php
@@ -1,30 +1,23 @@
 <?php
 
-namespace Drupal\administerusersbyrole\Tests;
+namespace Drupal\Tests\administerusersbyrole\Functional;
 
-use Drupal\simpletest\WebTestBase;
-use Drupal\Component\Utility\String;
+use Drupal\Tests\BrowserTestBase;
+use Drupal\Component\Utility\SafeMarkup;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Testing for administerusersbyrole module
  *
  * @group administerusersbyrole
  */
-class AdministerusersbyroleTestCaseTest extends WebTestBase {
+class AdministerusersbyroleTest extends BrowserTestBase {
 
-  public static $modules = array('administerusersbyrole', 'user', 'node');
+  public static $modules = array('administerusersbyrole', 'user');
 
   protected $roles = array();
   protected $users = array();
 
-  public static function getInfo() {
-    return array(
-      'name' => 'administerusersbyrole',
-      'description' => 'Ensure that Administer Users by Role functions properly.',
-      'group' => 'Administer Users by Role',
-    );
-  }
-
   public function setUp() {
     parent::setUp();
 
@@ -39,31 +32,28 @@ public function setUp() {
       _administerusersbyrole_build_perm_string($this->roles['alpha'], 'edit'),
       _administerusersbyrole_build_perm_string($this->roles['beta'], 'edit'),
     );
-    $this->resetAll();
     $this->roles['alphabeta_ed'] = $this->drupalCreateRole($perms, 'alphabeta_ed');
     $this->createUserWithRole('alphabeta_ed', array('alphabeta_ed'));
 
     // all_editor
     $perms = array(
       'access content',
-      'edit users with no custom roles',
+      _administerusersbyrole_build_perm_string(AccountInterface::AUTHENTICATED_ROLE, 'edit'),
     );
     foreach ($this->roles as $roleName => $roleID) {
       $perms[] = _administerusersbyrole_build_perm_string($this->roles[$roleName], 'edit');
     }
-    $this->resetAll();
     $this->roles['all_editor'] = $this->drupalCreateRole($perms, 'all_editor');
     $this->createUserWithRole('all_editor', array('all_editor'));
 
     // all_deletor
     $perms = array(
       'access content',
-      'cancel users with no custom roles',
+      _administerusersbyrole_build_perm_string(AccountInterface::AUTHENTICATED_ROLE, 'cancel'),
     );
     foreach ($this->roles as $roleName => $roleID) {
       $perms[] = _administerusersbyrole_build_perm_string($roleID, 'cancel');
     }
-    $this->resetAll();
     $this->roles['all_deletor'] = $this->drupalCreateRole($perms, 'all_deletor');
     $this->createUserWithRole('all_deletor', array('all_deletor'));
 
@@ -72,7 +62,6 @@ public function setUp() {
       'access content',
       'create users',
     );
-    $this->resetAll();
     $this->roles['creator'] = $this->drupalCreateRole($perms, 'creator');
     $this->createUserWithRole('creator', array('creator'));
   }
@@ -95,7 +84,7 @@ public function testPermissions() {
         'create users' => FALSE,
       ),
       'noroles' => array(
-        'noroles'      => array('edit' => TRUE,  'cancel' => TRUE),
+        'noroles'      => array('edit' => TRUE,  'cancel' => FALSE),
         'alpha'        => array('edit' => FALSE, 'cancel' => FALSE),
         'alpha_editor' => array('edit' => FALSE, 'cancel' => FALSE),
         'beta'         => array('edit' => FALSE, 'cancel' => FALSE),
@@ -109,7 +98,7 @@ public function testPermissions() {
       ),
       'alpha' => array(
         'noroles'      => array('edit' => FALSE, 'cancel' => FALSE),
-        'alpha'        => array('edit' => TRUE,  'cancel' => TRUE),
+        'alpha'        => array('edit' => TRUE,  'cancel' => FALSE),
         'alpha_editor' => array('edit' => FALSE, 'cancel' => FALSE),
         'beta'         => array('edit' => FALSE, 'cancel' => FALSE),
         'beta_editor'  => array('edit' => FALSE, 'cancel' => FALSE),
@@ -123,10 +112,10 @@ public function testPermissions() {
       'alpha_editor' => array(
         'noroles'      => array('edit' => TRUE,  'cancel' => FALSE),
         'alpha'        => array('edit' => TRUE,  'cancel' => FALSE),
-        'alpha_editor' => array('edit' => TRUE,  'cancel' => TRUE),
+        'alpha_editor' => array('edit' => TRUE,  'cancel' => FALSE),
         'beta'         => array('edit' => FALSE, 'cancel' => FALSE),
         'beta_editor'  => array('edit' => FALSE, 'cancel' => FALSE),
-        'alphabeta'    => array('edit' => TRUE, 'cancel' => FALSE),
+        'alphabeta'    => array('edit' => FALSE, 'cancel' => FALSE),
         'alphabeta_ed' => array('edit' => FALSE, 'cancel' => FALSE),
         'creator'      => array('edit' => FALSE, 'cancel' => FALSE),
         'all_editor'   => array('edit' => FALSE, 'cancel' => FALSE),
@@ -137,7 +126,7 @@ public function testPermissions() {
         'noroles'      => array('edit' => FALSE, 'cancel' => FALSE),
         'alpha'        => array('edit' => FALSE, 'cancel' => FALSE),
         'alpha_editor' => array('edit' => FALSE, 'cancel' => FALSE),
-        'beta'         => array('edit' => TRUE,  'cancel' => TRUE),
+        'beta'         => array('edit' => TRUE,  'cancel' => FALSE),
         'beta_editor'  => array('edit' => FALSE, 'cancel' => FALSE),
         'alphabeta'    => array('edit' => FALSE, 'cancel' => FALSE),
         'alphabeta_ed' => array('edit' => FALSE, 'cancel' => FALSE),
@@ -151,8 +140,8 @@ public function testPermissions() {
         'alpha'        => array('edit' => FALSE, 'cancel' => FALSE),
         'alpha_editor' => array('edit' => FALSE, 'cancel' => FALSE),
         'beta'         => array('edit' => TRUE,  'cancel' => TRUE),
-        'beta_editor'  => array('edit' => TRUE,  'cancel' => TRUE),
-        'alphabeta'    => array('edit' => TRUE, 'cancel' => TRUE),
+        'beta_editor'  => array('edit' => TRUE,  'cancel' => FALSE),
+        'alphabeta'    => array('edit' => FALSE, 'cancel' => FALSE),
         'alphabeta_ed' => array('edit' => FALSE, 'cancel' => FALSE),
         'creator'      => array('edit' => FALSE, 'cancel' => FALSE),
         'all_editor'   => array('edit' => FALSE, 'cancel' => FALSE),
@@ -165,7 +154,7 @@ public function testPermissions() {
         'alpha_editor' => array('edit' => FALSE, 'cancel' => FALSE),
         'beta'         => array('edit' => FALSE, 'cancel' => FALSE),
         'beta_editor'  => array('edit' => FALSE, 'cancel' => FALSE),
-        'alphabeta'    => array('edit' => TRUE,  'cancel' => TRUE),
+        'alphabeta'    => array('edit' => TRUE,  'cancel' => FALSE),
         'alphabeta_ed' => array('edit' => FALSE, 'cancel' => FALSE),
         'creator'      => array('edit' => FALSE, 'cancel' => FALSE),
         'all_editor'   => array('edit' => FALSE, 'cancel' => FALSE),
@@ -179,7 +168,7 @@ public function testPermissions() {
         'beta'         => array('edit' => TRUE,  'cancel' => FALSE),
         'beta_editor'  => array('edit' => FALSE, 'cancel' => FALSE),
         'alphabeta'    => array('edit' => TRUE,  'cancel' => FALSE),
-        'alphabeta_ed' => array('edit' => TRUE,  'cancel' => TRUE),
+        'alphabeta_ed' => array('edit' => TRUE,  'cancel' => FALSE),
         'creator'      => array('edit' => FALSE, 'cancel' => FALSE),
         'all_editor'   => array('edit' => FALSE, 'cancel' => FALSE),
         'all_deletor'  => array('edit' => FALSE, 'cancel' => FALSE),
@@ -194,7 +183,7 @@ public function testPermissions() {
         'alphabeta'    => array('edit' => TRUE,  'cancel' => FALSE),
         'alphabeta_ed' => array('edit' => TRUE,  'cancel' => FALSE),
         'creator'      => array('edit' => FALSE, 'cancel' => FALSE),
-        'all_editor'   => array('edit' => TRUE,  'cancel' => TRUE),
+        'all_editor'   => array('edit' => TRUE,  'cancel' => FALSE),
         'all_deletor'  => array('edit' => FALSE, 'cancel' => FALSE),
         'create users' => FALSE,
       ),
@@ -208,7 +197,7 @@ public function testPermissions() {
         'alphabeta_ed' => array('edit' => FALSE, 'cancel' => TRUE),
         'creator'      => array('edit' => FALSE, 'cancel' => FALSE),
         'all_editor'   => array('edit' => FALSE, 'cancel' => TRUE),
-        'all_deletor'  => array('edit' => TRUE,  'cancel' => TRUE),
+        'all_deletor'  => array('edit' => TRUE,  'cancel' => FALSE),
         'create users' => FALSE,
       ),
       'creator' => array(
@@ -219,7 +208,7 @@ public function testPermissions() {
         'beta_editor'  => array('edit' => FALSE, 'cancel' => FALSE),
         'alphabeta'    => array('edit' => FALSE, 'cancel' => FALSE),
         'alphabeta_ed' => array('edit' => FALSE, 'cancel' => FALSE),
-        'creator'      => array('edit' => TRUE,  'cancel' => TRUE),
+        'creator'      => array('edit' => TRUE,  'cancel' => FALSE),
         'all_editor'   => array('edit' => FALSE, 'cancel' => FALSE),
         'all_deletor'  => array('edit' => FALSE, 'cancel' => FALSE),
         'create users' => TRUE,
@@ -236,10 +225,10 @@ public function testPermissions() {
           $this->drupalGet("admin/people/create");
           $expectedResult = $v;
           if ($expectedResult) {
-            $this->assertRaw('All emails from the system will be sent to this address', "My expectation is that $loginUsername should be able to create users, but it can't.");
+            $this->assertRaw('<h1 class="page-title">Add user</h1>');
           }
           else {
-            $this->assertRaw('You are not authorized to access this page.', "My expectation is that $loginUsername shouldn't be able to create users, but it can.");
+            $this->assertRaw('You are not authorized to access this page.');
           }
         }
         else {
@@ -248,12 +237,13 @@ public function testPermissions() {
           $editUid = $this->users[$editUsername]->id();
           foreach ($operations as $operation => $expectedResult) {
             $this->drupalGet("user/$editUid/$operation");
+            // $loginUsername perform $operation on $editUsername
             if ($expectedResult) {
               if ($operation === 'edit') {
-                $this->assertRaw("All emails from the system will be sent to this address.", "My expectation is that $loginUsername should be able to $operation $editUsername, but it can't.");
+                $this->assertRaw("All emails from the system will be sent to this address.");
               }
               elseif ($operation === 'cancel') {
-                $this->assertRaw("Are you sure you want to cancel", "My expectation is that $loginUsername should be able to $operation $editUsername, but it can't.");
+                $this->assertRaw("Are you sure you want to cancel the account <em class=\"placeholder\">$editUsername</em>?");
               }
             }
             else {
@@ -273,13 +263,13 @@ public function testPermissions() {
   }
 
   protected function createUserWithRole($userName, $roleNames) {
-
-    $this->users[$userName] = $this->createUser($roleNames, $userName);
-
-    //$id = $account->id();
-    //$this->drupalGet("user/$id/edit");
-
-    $this->assertTrue($this->users[$userName]->id() > 0, "Unable to create user $userName.");
+    $user = $this->drupalCreateUser([], $userName);
+    $this->assertTrue($user, "Unable to create user $userName.");
+    foreach ($roleNames as $role) {
+      $user->addRole($role);
+    }
+    $user->save();
+    $this->users[$userName] = $user;
   }
 
   protected function createRolesAndUsers($roleName, $allowEditorToCancel) {
@@ -287,12 +277,10 @@ protected function createRolesAndUsers($roleName, $allowEditorToCancel) {
     $this->roles[$roleName] = $this->drupalCreateRole(array('access content'), $roleName);
     $this->createUserWithRole($roleName, array($roleName));
 
-    // clear permissions cache, so we can use the 'edit users with ...' permission for this role
-    $this->resetAll();
     // create role to edit above role and also anyone with no custom roles.
     $perms = array(
       'access content',
-      'edit users with no custom roles',
+      _administerusersbyrole_build_perm_string(AccountInterface::AUTHENTICATED_ROLE, 'edit'),
       _administerusersbyrole_build_perm_string($this->roles[$roleName], 'edit'),
     );
     if ($allowEditorToCancel) {
@@ -303,30 +291,4 @@ protected function createRolesAndUsers($roleName, $allowEditorToCancel) {
     $this->createUserWithRole("{$roleName}_editor", array("{$roleName}_editor"));
   }
 
-  /**
-  * Allows create users with determined roles
-  */
-  protected function createUser(array $roles = array(), $name = NULL) {
-     // Create a user assigned to that role.
-    $edit = array();
-    $edit['name'] = !empty($name) ? $name : $this->randomMachineName();
-    $edit['mail'] = $edit['name'] . '@example.com';
-    $edit['pass'] = 'cheese';
-    $edit['status'] = 1;
-    if (sizeof($roles) > 0) {
-      $edit['roles'] = $roles;
-    }
-
-    $account = entity_create('user', $edit);
-    $account->save();
-
-    $this->assertTrue($account->id(), String::format('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), 'User login');
-    if (!$account->id()) {
-      return FALSE;
-    }
-
-    // Add the raw password so that we can log in as this user.
-    $account->pass_raw = $edit['pass'];
-    return $account;
-  }
 }
-- 
GitLab