diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php
index e27b690..e2134d9 100644
--- a/core/modules/simpletest/src/WebTestBase.php
+++ b/core/modules/simpletest/src/WebTestBase.php
@@ -12,6 +12,7 @@
 use Drupal\Component\Utility\Crypt;
 use Drupal\Component\Utility\NestedArray;
 use Drupal\Component\Utility\String;
+use Drupal\Component\Utility\Unicode;
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\DependencyInjection\YamlFileLoader;
 use Drupal\Core\DrupalKernel;
@@ -28,6 +29,7 @@
 use Drupal\Core\Datetime\DrupalDateTime;
 use Drupal\block\Entity\Block;
 use Drupal\Core\Url;
+use Drupal\user\Plugin\Validation\Constraint\UserNameConstraintValidator;
 use Symfony\Component\HttpFoundation\Request;
 use Drupal\user\Entity\Role;
 
@@ -542,8 +544,14 @@ protected function drupalCreateUser(array $permissions = array(), $name = NULL)
 
     // Create a user assigned to that role.
     $edit = array();
-    $edit['name']   = !empty($name) ? $name : $this->randomMachineName();
+    $edit['name']   = !empty($name) ? $name : $this->getRandomGenerator()->string(8, TRUE, array($this, 'randomUsernameValidate'));
     $edit['mail']   = $edit['name'] . '@example.com';
+    // It is possible that name + @example.com is not a valid email address
+    // since user names can contain whitespace and @ characters and start with a
+    // dot.
+    if (!valid_email_address($edit['mail'])) {
+      $edit['mail'] = $this->getRandomGenerator()->name() . '@example.com';
+    }
     $edit['pass']   = user_password();
     $edit['status'] = 1;
     if ($rid) {
@@ -2561,6 +2569,10 @@ protected function assertNoResponse($code, $message = '', $group = 'Browser') {
    *   TRUE on pass, FALSE on fail.
    */
   protected function assertMail($name, $value = '', $message = '', $group = 'Email') {
+    // The mail subject is mime encoded.
+    if ($name == 'subject') {
+      $value = Unicode::mimeHeaderEncode($value);
+    }
     $captured_emails = \Drupal::state()->get('system.test_mail_collector') ?: array();
     $email = end($captured_emails);
     return $this->assertTrue($email && isset($email[$name]) && $email[$name] == $value, $message, $group);
@@ -2720,12 +2732,30 @@ protected function buildUrl($path, array $options = array()) {
     }
     // The URL generator service is not necessarily available yet; e.g., in
     // interactive installer tests.
-    else if ($this->container->has('url_generator')) {
-      $options['absolute'] = TRUE;
-      return $this->container->get('url_generator')->generateFromPath($path, $options);
-    }
     else {
-      return $this->getAbsoluteUrl($path);
+      if ($this->container->has('url_generator')) {
+        $options['absolute'] = TRUE;
+        return $this->container->get('url_generator')
+          ->generateFromPath($path, $options);
+      }
+      else {
+        return $this->getAbsoluteUrl($path);
+      }
     }
   }
+
+  /**
+   * Callback for random username validation.
+   *
+   * @see \Drupal\Component\Utility\Random::string()
+   *
+   * @param string $string
+   *   The random string to validate.
+   *
+   * @return bool
+   *   TRUE if the random string is valid, FALSE if not.
+   */
+  public function randomUsernameValidate($string) {
+    return $this->randomStringValidate($string) && !UserNameConstraintValidator::hasIllegalCharacters($string);
+  }
 }
