View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\openid_connect\Unit;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\openid_connect\OpenIDConnectClaims;
use Drupal\Tests\UnitTestCase;
class OpenIdConnectClaimsTest extends UnitTestCase {
const DEFAULT_CLAIMS_COUNT = 19;
const USERINFO_MAPPINGS = [
'timezone' => 'zoneinfo',
'user_picture' => 'picture',
];
protected $configFactory;
protected $moduleHandler;
protected $openIdConnectClaims;
protected $container;
protected function setUp() : void {
parent::setUp();
$this->configFactory = $this
->createMock(ConfigFactory::class);
$this->moduleHandler = $this
->createMock(ModuleHandler::class);
$this->container = new ContainerBuilder();
$this->container
->set('module_handler', $this->moduleHandler);
$this->container
->set('config.factory', $this->configFactory);
$this->container
->set('string_translation', self::getStringTranslationStub());
\Drupal::setContainer($this->container);
$this->openIdConnectClaims = OpenIDConnectClaims::create($this->container);
}
public function testGetClaimsAlter() : void {
$this->moduleHandler
->expects($this
->atLeast(1))
->method('alter')
->with('openid_connect_claims');
$this->openIdConnectClaims
->getClaims();
}
public function testGetDefaultClaims(string $key, string $scope, string $type) : void {
$claims = $this->openIdConnectClaims
->getClaims();
$this
->assertArrayHasKey($key, $claims);
$this
->assertEquals($scope, $claims[$key]['scope']);
$this
->assertEquals($type, $claims[$key]['type']);
}
public function testDefaultClaimsCount() : void {
$claims = $this->openIdConnectClaims
->getClaims();
$this
->assertCount(self::DEFAULT_CLAIMS_COUNT, $claims);
}
public function testGetOptions(string $key, string $scope, string $type) : void {
$options = $this->openIdConnectClaims
->getOptions();
$this
->assertArrayHasKey(ucfirst($scope), $options);
$this
->assertArrayHasKey($key, $options[ucfirst($scope)]);
}
public function testDefaultGetScopes() : void {
$userInfoMapping = $this
->createMock(ImmutableConfig::class);
$userInfoMapping
->expects($this
->once())
->method('get')
->willReturn(self::USERINFO_MAPPINGS);
$this->configFactory
->expects($this
->once())
->method('getEditable')
->willReturn($userInfoMapping);
$scopes = $this->openIdConnectClaims
->getScopes();
$this
->assertEquals('openid email profile', $scopes);
}
public function testUserInfoMappingScopes(string $key, string $scope, string $type) : void {
$mappings = self::USERINFO_MAPPINGS;
$mappings[$key] = $this
->randomMachineName();
$userInfoMapping = $this
->createMock(ImmutableConfig::class);
$userInfoMapping
->expects($this
->once())
->method('get')
->willReturn($mappings);
$this->configFactory
->expects($this
->once())
->method('getEditable')
->willReturn($userInfoMapping);
$actualScopes = $this->openIdConnectClaims
->getScopes();
switch ($scope) {
case 'email':
case 'address':
case 'phone':
$this
->assertEquals("openid email profile", $actualScopes);
break;
default:
$this
->assertEquals("openid email {$scope}", $actualScopes);
break;
}
}
public function defaultClaimsProvider() : array {
return [
[
'name',
'profile',
'string',
],
[
'given_name',
'profile',
'string',
],
[
'family_name',
'profile',
'string',
],
[
'middle_name',
'profile',
'string',
],
[
'nickname',
'profile',
'string',
],
[
'preferred_username',
'profile',
'string',
],
[
'profile',
'profile',
'string',
],
[
'picture',
'profile',
'string',
],
[
'website',
'profile',
'string',
],
[
'email',
'email',
'string',
],
[
'email_verified',
'email',
'boolean',
],
[
'gender',
'profile',
'string',
],
[
'birthdate',
'profile',
'string',
],
[
'zoneinfo',
'profile',
'string',
],
[
'locale',
'profile',
'string',
],
[
'phone_number',
'phone',
'string',
],
[
'phone_number_verified',
'phone',
'boolean',
],
[
'address',
'address',
'json',
],
[
'updated_at',
'profile',
'number',
],
];
}
}