I'm working on test cases and having issues with creating users. My setUp function looks like this:

  function setUp() {
    parent::setUp('my_module');
    $this->admin_user = $this->drupalCreateUser(array('administer users'));
    // ...
  }

On the line with $this->drupalCreateUser, I am getting a PDOException halting testing because, essentially, when user_save tries to save the new roles, it gets a primary key conflict. Apparently the new user already has the new role in the database?

Because this is a test case, only my module is installed on top of the base system. However my module doesn't do anything with roles. It does however have several user-related hook implementations to control certain information stored in $account->data. Does anybody know what's going on? Did I mess up role handling without explicitly dealing with roles at all?

Comments

meustrus’s picture

Status: Active » Closed (fixed)

Figured it out. My hook_user_insert implementation may result in another call to user_save (because $edit has already been written to the database) which for some reason hasn't been an issue, but even after unsetting $account->is_new it was breaking with roles because the nested call to user_save writes the roles before the parent call to user_save writes them. I'm modifying my code to use hook_user_presave instead now.