Allow passing custom $account properties to drupalCreateUser()

Created on 24 June 2010, over 14 years ago
Updated 10 October 2024, about 1 month ago

Hi there,

I have a module which create new fields for users and stores in a specific database. Basically I am creating the row in the

<?php
HOOK_useruser($op, &$edit, &$account, $category = NULL) {
  case 'insert':
    // do stuff
    break;
}
<?

I then created a new class test which extends the DrupalWebTestCase and overriden the drupalCreateUser method.

But, instead of calling parrent::drupalCreateUser($permission) and then doing stuff with the $account parameter (adding the fields), I have to recopy all the method and add my custom fields in the $edit array.

It would be great to pass the $edit array so we can reuse code and get the real power of php classes.

Let's see concretely what will be the benefits:

Instead of override the full method:

<?php
protected function drupalCreateUser($permissions = array('access comments', 'access content', 'post comments', 'post comments without approval')) {
    // Create a role with the given permission set.
    if (!($rid = $this->drupalCreateRole($permissions))) {
      return FALSE;
    }

    // Create a user assigned to that role.
    $edit = array();
    $edit['name']   = $this->randomName();
    $edit['mail']   = $edit['name'] . '@example.com';
    $edit['roles']  = array($rid => $rid);
    $edit['pass']   = user_password();
    $edit['status'] = 1;
    
    // ---------------------------
    // ADD HERE YOUR STUFF.
    $edit['age'] = ..
    $edit['given_name'] = ...
    $edit['family_name'] = ...
    // ---------------------------
    
    $account = user_save('', $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;
  }
?>

We change a bit the signature: <?php protected function drupalCreateUser($permissions, <strong>$edit</strong>) { ?>

And then better reuse the code:

<?php
  protected function drupalCreateUser($permissions = array('access comments', 'access content', 'post comments', 'post comments without approval')) {
    /* Add here your custom Module fields */
    $edit['given_name'] = 'Sylvain';
    $edit['family_name'] = 'Lecoy';
    $edit['age'] = 0;

    /* Then call the parent method. */
    $account = parent::drupalCreateUser($permissions, $edit);
}
?>
Feature request
Status

Closed: outdated

Version

11.0 🔥

Component

simpletest.module

Created by

🇫🇷France Sylvain Lecoy

Live updates comments and jobs are added and updated live.
  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024