public function OpenIDConnectTest::testCreateUser in OpenID Connect / OAuth client 2.x
Same name and namespace in other branches
- 8 tests/src/Unit/OpenIDConnectTest.php \Drupal\Tests\openid_connect\Unit\OpenIDConnectTest::testCreateUser()
Test for the createUser method.
@dataProvider dataProviderForCreateUser
Parameters
string $sub: The sub to use.
array $userinfo: The userinfo array containing the email key.
string $client_name: The client name for the user.
int $status: The user status.
bool $duplicate: Whether to test a duplicate username.
File
- tests/
src/ Unit/ OpenIDConnectTest.php, line 383
Class
- OpenIDConnectTest
- Provides tests for the OpenID Connect module.
Namespace
Drupal\Tests\openid_connect\UnitCode
public function testCreateUser(string $sub, array $userinfo, string $client_name, int $status, bool $duplicate) : void {
// Mock the expected username.
$expectedUserName = 'oidc_' . $client_name . '_' . md5($sub);
// If the preferred username is defined, use it instead.
if (array_key_exists('preferred_username', $userinfo)) {
$expectedUserName = trim($userinfo['preferred_username']);
}
// If the name key exists, use it.
if (array_key_exists('name', $userinfo)) {
$expectedUserName = trim($userinfo['name']);
}
$expectedAccountArray = [
'name' => $duplicate ? "{$expectedUserName}_1" : $expectedUserName,
'mail' => $userinfo['email'],
'init' => $userinfo['email'],
'status' => $status,
];
// Mock the user account to be created.
$account = $this
->createMock(UserInterface::class);
$this->externalAuth
->expects($this
->once())
->method('register')
->with($sub, 'openid_connect.' . $client_name, $expectedAccountArray)
->willReturn($account);
if ($duplicate) {
$this->userStorage
->expects($this
->exactly(2))
->method('loadByProperties')
->withConsecutive([
[
'name' => $expectedUserName,
],
], [
[
'name' => "{$expectedUserName}_1",
],
])
->willReturnOnConsecutiveCalls([
1,
], []);
}
else {
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with([
'name' => $expectedUserName,
])
->willReturn([]);
}
$actualResult = $this->openIdConnect
->createUser($sub, $userinfo, $client_name, $status);
$this
->assertInstanceOf('\\Drupal\\user\\UserInterface', $actualResult);
}