0

How can I programmatically create a new user for use during a SimpleTest? The user must have certain custom fields populated during the registration.

I'm using $this->drupalPost('user/register', $edit, 'Create new account'); to create the account currently as I do not see a way to populate custom profile fields with drupalCreateUser().

$edit = array(
      'name' => $this->randomName(17),
      'mail' => $this->randomName(17) . '@example.com',
      'field_owner_first_name[und][0][value]' => $this->randomName(17),
      'field_owner_last_name[und][0][value]' => $this->randomName(17),
      'field_owner_title[und][0][value]' => $this->randomName(8),
      'field_business_name[und][0][value]' => $this->randomName(17),
      'field_business_phone[und][0][value]' => mt_rand(200, 999) . '-' . mt_rand(200, 999) . '-' . mt_rand(1000, 9999),
      'field_business_email[und][0][value]' => $this->randomName(17) . '@example.com',
      'field_business_website[und][0][value]' => 'http://' . $this->randomName(17) . '.com',
      'field_business_address_1[und][0][value]' => mt_rand(1, 9999) . ' ' . $this->randomName(12) . ' ' . $this->randomName(2),
      'field_business_address_2[und][0][value]' => 'Apt ' . mt_rand(1, 999),
      'field_city[und][0][value]' => $this->randomName(8),
      'field_state[und]' => 'HI',
      'field_zip[und][0][value]' => mt_rand(10000, 99999),
      'field_request_website[und]' => 'Yes',
);
$this->drupalPost('user/register', $edit, 'Create new account');

The code above successfully completes and posts the registration form.

How can I automatically approve and login as this newly submitted user for testing? Do I need to write tests to login the admin account, find and then approve the user?


Additional Thoughts and Clarification:

Looking at other options, can I somehow use variable_set() to accomplish the automated user approval above when the setUp() method is called?

| improve this question | | | | |
1

While setting the user_register variable works by automatically logging you in, if you want to test the default mail confirmation, you can inspect the mail as described over in your other question: How to inspect outgoing email content during a SimpleTest?.

Extract the link with a regex, "click" it by using drupalGet() and confirm by submitting the form (this is pretty much exactly what I did in the simplenews tests linked in the other question). Note that if you also want to be able to log in through the normal user login form after that, you need to load the user object and then set the pass_raw property of the loaded user object to the password, because that is what is used in drupalLogin().

| improve this answer | | | | |
1

in the setUp() method I simply added: variable_set('user_register', 1);

This way I could drupalPost a new account and login immediately without approval.

$this->drupalPost('user/register', $edit, 'Create new account');

Account is successfully registered and the required additional fields were collected during registration.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.