You are here

protected function ParagraphsWebTestCase::drupalCreateUserWithRole in Paragraphs 7

Helper to create a user with a given role.

Parameters

string $role_name: The name of the role to give the user.

Return value

bool|object A user account object.

Throws

\Exception

See also

DrupalWebTestCase::drupalCreateUser()

1 call to ParagraphsWebTestCase::drupalCreateUserWithRole()
ParagraphsWebTestCase::setUp in tests/paragraphs.test
Sets up a Drupal site for running functional and integration tests.

File

tests/paragraphs.test, line 154

Class

ParagraphsWebTestCase
Defines tests for paragraphs.

Code

protected function drupalCreateUserWithRole($role_name) {
  $role = user_role_load_by_name($role_name);
  if (!$role) {
    return FALSE;
  }

  // Create a user assigned to that role.
  $edit = array();
  $edit['name'] = $this
    ->randomName();
  $edit['mail'] = $edit['name'] . '@example.com';
  $edit['pass'] = user_password();
  $edit['status'] = 1;
  $edit['roles'] = array(
    $role->rid => $role->rid,
  );
  $account = user_save(drupal_anonymous_user(), $edit);
  $this
    ->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array(
    '%name' => $edit['name'],
    '%pass' => $edit['pass'],
  )), t('User login'));
  if (empty($account->uid)) {
    return FALSE;
  }

  // Add the raw password so that we can log in as this user.
  $account->pass_raw = $edit['pass'];
  return $account;
}