How to log in a user and inspect their role in a functional test? 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-&gt;drupalGet('user/login');
    $edit = ['name' =&gt; [redacted], 'pass' =&gt; [redacted]];

    $this-&gt;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?
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-&gt;assertEquals($expected_roles, $account-&gt;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.