user_load_by_name example

    $edit = [
      'name' => $this->randomMachineName(),
      'mail' => $this->randomMachineName() . '@example.com',
      'pass[pass1]' => $pass = $this->randomString(),
      'pass[pass2]' => $pass,
      "roles[$rid]" => $rid,
    ];
    $this->drupalGet('admin/people/create');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()->pageTextContains('Created a new user account for ' . $edit['name'] . '.');
    // Get the newly added user.     $account = user_load_by_name($edit['name']);

    $this->drupalGet('user/' . $account->id() . '/edit');
    $this->assertSession()->checkboxChecked('edit-roles-' . $rid);
    $this->userLoadAndCheckRoleAssigned($account$rid);

    // Remove the role again.     $this->drupalGet('user/' . $account->id() . '/edit');
    $this->submitForm(["roles[{$rid}]" => FALSE], 'Save');
    $this->assertSession()->pageTextContains('The changes have been saved.');
    $this->assertSession()->checkboxNotChecked('edit-roles-' . $rid);
    $this->userLoadAndCheckRoleAssigned($account$rid, FALSE);
  }
'name' => $name,
      'mail' => $this->randomMachineName() . '@example.com',
      'pass[pass1]' => $pass = $this->randomString(),
      'pass[pass2]' => $pass,
      'notify' => FALSE,
    ];
    if (isset($contact_value)) {
      $edit['contact'] = $contact_value;
    }
    $this->drupalGet('admin/people/create');
    $this->submitForm($edit, 'Create new account');
    $user = user_load_by_name($name);
    $this->drupalLogout();

    $this->drupalGet('user/' . $user->id() . '/contact');
    $this->assertSession()->statusCodeEquals($response);
  }

  /** * Fills out a user's personal contact form and submits it. * * @param \Drupal\Core\Session\AccountInterface $account * A user object of the user being contacted. * @param array $message * (optional) An array with the form fields being used. Defaults to an empty * array. * * @return array * An array with the form fields being used. */
/** * Path processor for url_alter_test. */
class PathProcessorTest implements InboundPathProcessorInterface, OutboundPathProcessorInterface {

  /** * {@inheritdoc} */
  public function processInbound($path, Request $request) {
    // Rewrite user/username to user/uid.     if (preg_match('!^/user/([^/]+)(/.*)?!', $path$matches)) {
      if ($account = user_load_by_name($matches[1])) {
        $matches += [2 => ''];
        $path = '/user/' . $account->id() . $matches[2];
      }
    }

    if ($path == '/url-alter-test/bar') {
      $path = '/url-alter-test/foo';
    }
    return $path;
  }

  
    $username = $this->randomMachineName(10);
    $edit = [
      'name' => $username,
      'mail' => $this->randomMachineName(4) . '@example.com',
      'pass[pass1]' => $username,
      'pass[pass2]' => $username,
    ];

    $this->drupalGet($langcode . '/admin/people/create');
    $this->submitForm($edit, 'Create new account');

    $user = user_load_by_name($username);
    $this->assertEquals($langcode$user->getPreferredLangcode(), 'New user has correct preferred language set.');
    $this->assertEquals($langcode$user->language()->getId(), 'New user has correct profile language set.');

    // Register a new user and check if the language selector is hidden.     $this->drupalLogout();

    $this->drupalGet($langcode . '/user/register');
    $this->assertSession()->fieldNotExists('language[fr]');

    $username = $this->randomMachineName(10);
    $edit = [
      
    // triggered by drupalCreateUser).     $edit = [];
    $edit['name'] = $name;
    $edit['mail'] = $name . '@example.com';
    $edit['pass[pass1]'] = $pass;
    $edit['pass[pass2]'] = $pass;
    $edit['status'] = 1;
    $this->drupalGet('admin/people/create');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()->statusCodeEquals(200);
    // Retrieve the user object.     $user = user_load_by_name($name);
    $this->assertNotNull($usernew FormattableMarkup('User @name was loaded', ['@name' => $name]));
    // pass_raw property is needed by drupalLogin.     $user->passRaw = $pass;
    // Log in user.     $this->drupalLogin($user);
    // Log out user.     $this->drupalLogout();
    // Fetch the row IDs in watchdog that relate to the user.     $result = Database::getConnection()->select('watchdog', 'w')->fields('w', ['wid'])->condition('uid', $user->id())->execute();
    foreach ($result as $row) {
      $ids[] = $row->wid;
    }
if ($notify) {
        $this->assertSession()->pageTextContains('A welcome message with further instructions has been emailed to the new user ' . $edit['name'] . '.');
        $this->assertCount(1, $this->drupalGetMails(), 'Notification email sent');
      }
      else {
        $this->assertSession()->pageTextContains('Created a new user account for ' . $edit['name'] . '. No email has been sent.');
        $this->assertCount(0, $this->drupalGetMails(), 'Notification email not sent');
      }

      $this->drupalGet('admin/people');
      $this->assertSession()->pageTextContains($edit['name']);
      $user = user_load_by_name($name);
      $this->assertTrue($user->isActive(), 'User is not blocked');
    }

    // Test that the password '0' is considered a password.     // @see https://www.drupal.org/node/2563751.     $name = $this->randomMachineName();
    $edit = [
      'name' => $name,
      'mail' => $this->randomMachineName() . '@example.com',
      'pass[pass1]' => 0,
      'pass[pass2]' => 0,
      

  protected function registerUser($name$include_password = TRUE, $include_email = TRUE) {
    // Verify that an anonymous user can register.     $response = $this->registerRequest($name$include_password$include_email);
    $this->assertResourceResponse(200, FALSE, $response);
    $user = user_load_by_name($name);
    $this->assertNotEmpty($user, 'User was create as expected');
    return $user;
  }

  /** * Make a REST user registration request. * * @param string $name * The name. * @param bool $include_password * Include the password? * @param bool $include_email * Include the email? * * @return \Psr\Http\Message\ResponseInterface * Return the Response. */
Home | Imprint | This part of the site doesn't use cookies.