You are here

class OpenIdConnectClaimsTest in OpenID Connect / OAuth client 2.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/OpenIdConnectClaimsTest.php \Drupal\Tests\openid_connect\Unit\OpenIdConnectClaimsTest

Test the OpenIdConnectClaims class.

@coversDefaultClass \Drupal\openid_connect\OpenIDConnectClaims @group openid_connect

Hierarchy

Expanded class hierarchy of OpenIdConnectClaimsTest

File

tests/src/Unit/OpenIdConnectClaimsTest.php, line 20

Namespace

Drupal\Tests\openid_connect\Unit
View source
class OpenIdConnectClaimsTest extends UnitTestCase {

  /**
   * The default count of the available claims.
   */
  const DEFAULT_CLAIMS_COUNT = 19;

  /**
   * The default userinfo_mappings array.
   */
  const USERINFO_MAPPINGS = [
    'timezone' => 'zoneinfo',
    'user_picture' => 'picture',
  ];

  /**
   * A mock of the config.factory service.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject
   */
  protected $configFactory;

  /**
   * A mock of the module_handler service.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject
   */
  protected $moduleHandler;

  /**
   * The OpenIdConnectClaims class being tested.
   *
   * @var \Drupal\openid_connect\OpenIDConnectClaims
   */
  protected $openIdConnectClaims;

  /**
   * Mock of the container for service calls.
   *
   * @var \Drupal\Core\DependencyInjection\ContainerBuilder
   */
  protected $container;

  /**
   * {@inheritDoc}
   */
  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);
  }

  /**
   * Test the getClaims method and ensure the alter method is invoked.
   */
  public function testGetClaimsAlter() : void {
    $this->moduleHandler
      ->expects($this
      ->atLeast(1))
      ->method('alter')
      ->with('openid_connect_claims');
    $this->openIdConnectClaims
      ->getClaims();
  }

  /**
   * Test the default claims array.
   *
   * @param string $key
   *   The key for the claim.
   * @param string $scope
   *   The scope for the claim.
   * @param string $type
   *   The data type for the claim.
   *
   * @dataProvider defaultClaimsProvider
   *   The data profiled for the claims test.
   */
  public function testGetDefaultClaims(string $key, string $scope, string $type) : void {
    $claims = $this->openIdConnectClaims
      ->getClaims();

    // Ensure the default key exists.
    $this
      ->assertArrayHasKey($key, $claims);

    // Assert the scope is correct.
    $this
      ->assertEquals($scope, $claims[$key]['scope']);

    // Assert the type is correct.
    $this
      ->assertEquals($type, $claims[$key]['type']);
  }

  /**
   * Test the default count of the default claims.
   */
  public function testDefaultClaimsCount() : void {
    $claims = $this->openIdConnectClaims
      ->getClaims();
    $this
      ->assertCount(self::DEFAULT_CLAIMS_COUNT, $claims);
  }

  /**
   * Test the options array for the form api.
   *
   * @param string $key
   *   The key for the claim.
   * @param string $scope
   *   The scope for the claim.
   * @param string $type
   *   The data type for the claim.
   *
   * @dataProvider defaultClaimsProvider
   *   The data profiled for the claims test.
   */
  public function testGetOptions(string $key, string $scope, string $type) : void {

    // Get the options.
    $options = $this->openIdConnectClaims
      ->getOptions();
    $this
      ->assertArrayHasKey(ucfirst($scope), $options);
    $this
      ->assertArrayHasKey($key, $options[ucfirst($scope)]);
  }

  /**
   * Test the default getScopes() method.
   */
  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);
  }

  /**
   * Test the scopes based on the user mappings.
   *
   * @param string $key
   *   The key for the claim.
   * @param string $scope
   *   The scope for the claim.
   * @param string $type
   *   The data type for the claim.
   *
   * @dataProvider defaultClaimsProvider
   *   The data profiled for the claims test.
   */
  public function testUserInfoMappingScopes(string $key, string $scope, string $type) : void {
    $mappings = self::USERINFO_MAPPINGS;

    // Append the provided key to the 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;
    }
  }

  /**
   * Get the expected default claims.
   *
   * @return array
   *   The default key, scope, type for the claims.
   */
  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',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OpenIdConnectClaimsTest::$configFactory protected property A mock of the config.factory service.
OpenIdConnectClaimsTest::$container protected property Mock of the container for service calls.
OpenIdConnectClaimsTest::$moduleHandler protected property A mock of the module_handler service.
OpenIdConnectClaimsTest::$openIdConnectClaims protected property The OpenIdConnectClaims class being tested.
OpenIdConnectClaimsTest::defaultClaimsProvider public function Get the expected default claims.
OpenIdConnectClaimsTest::DEFAULT_CLAIMS_COUNT constant The default count of the available claims.
OpenIdConnectClaimsTest::setUp protected function Overrides UnitTestCase::setUp
OpenIdConnectClaimsTest::testDefaultClaimsCount public function Test the default count of the default claims.
OpenIdConnectClaimsTest::testDefaultGetScopes public function Test the default getScopes() method.
OpenIdConnectClaimsTest::testGetClaimsAlter public function Test the getClaims method and ensure the alter method is invoked.
OpenIdConnectClaimsTest::testGetDefaultClaims public function Test the default claims array.
OpenIdConnectClaimsTest::testGetOptions public function Test the options array for the form api.
OpenIdConnectClaimsTest::testUserInfoMappingScopes public function Test the scopes based on the user mappings.
OpenIdConnectClaimsTest::USERINFO_MAPPINGS constant The default userinfo_mappings array.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function