1

I want to write a functional test that takes some test user credentials in a remote system, and posts them to Drupal login.

So far, I have

  /**
   * These users should authetnicate via Netforum.
   */
  public function testNetforumUsersCanAuthenticate() {
    $this->drupalGet('user/login');
    $edit = ['name' => [redacted], 'pass' => [redacted]];

    $this->drupalPostForm(NULL, $edit, t('Log in'));
  }

The authentication always goes through the external system even if the local account exists in Drupal. Therefore, I cannot just create these accounts beforehand.

The account should get created when authentication passes - and then I want to look at the users roles to check if they match what I expect for the different test accounts.

What can I use to see that this was successful and then look at the roles and check them?

1 Answer 1

1

You should be able to load the user object using user_load_by_name(), and then just make your assertions:

$account = user_load_by_name('foo');
$expected_roles = [
  'foo',
  'bar',
];
$this->assertEquals($expected_roles, $account->getRoles());

As an aside, it may be worth pursuing a mocked/testing authentication service for your tests. This way they don't rely on an external service, and will be more stable and reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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