You are here

class OpenIDConnectAuthmapTest in OpenID Connect / OAuth client 8

Test the OpenIdConnectAuthmap class.

@coversDefaultClass \Drupal\openid_connect\OpenIDConnectAuthmap @group openid_connect

Hierarchy

Expanded class hierarchy of OpenIDConnectAuthmapTest

File

tests/src/Unit/OpenIDConnectAuthmapTest.php, line 24

Namespace

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

  /**
   * The user_id to test.
   */
  const USER_ID = 1999;

  /**
   * Mock of the EntityStorageInterface for User objects.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject
   */
  protected $userStorage;

  /**
   * Mock the database connection service.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject
   */
  protected $connection;

  /**
   * Mock of the current_user service.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject
   */
  protected $account;

  /**
   * Mock of the entity_type.manager service.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;

  /**
   * {@inheritDoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->account = $this
      ->createMock(AccountInterface::class);
    $this->connection = $this
      ->createMock(Connection::class);
    $this->userStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->entityTypeManager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->willReturn($this->userStorage);
  }

  /**
   * Test the createAssociation method.
   */
  public function testCreateAssociationMethod() : void {
    $client_name = 'generic';
    $sub = 'subject';
    $this->account
      ->expects($this
      ->exactly(2))
      ->method('id')
      ->willReturn(self::USER_ID);
    $selectInterface = $this
      ->createMock(SelectInterface::class);
    $selectInterface
      ->expects($this
      ->once())
      ->method('fields')
      ->with('a', [
      'client_name',
      'sub',
    ])
      ->willReturnSelf();
    $selectInterface
      ->expects($this
      ->at(1))
      ->method('condition')
      ->with('uid', self::USER_ID)
      ->willReturnSelf();
    $selectInterface
      ->expects($this
      ->at(2))
      ->method('condition')
      ->with('client_name', $client_name)
      ->willReturnSelf();
    $selectInterface
      ->expects($this
      ->once())
      ->method('execute')
      ->willReturn([]);
    $this->connection
      ->expects($this
      ->once())
      ->method('select')
      ->willReturn($selectInterface);
    $queryInsert = $this
      ->createMock(Insert::class);
    $queryInsert
      ->expects($this
      ->once())
      ->method('fields')
      ->with([
      'uid' => self::USER_ID,
      'client_name' => $client_name,
      'sub' => $sub,
    ])
      ->willReturnSelf();
    $queryInsert
      ->expects($this
      ->once())
      ->method('execute')
      ->willReturn(1);
    $this->connection
      ->expects($this
      ->once())
      ->method('insert')
      ->with('openid_connect_authmap')
      ->willReturn($queryInsert);
    $authMapClass = new OpenIDConnectAuthmap($this->connection, $this->entityTypeManager);
    $authMapClass
      ->createAssociation($this->account, 'generic', 'subject');
  }

  /**
   * Test the deleteAssociationMethod.
   *
   * @param string|null $client
   *   The client name to test or an empty client.
   *
   * @dataProvider getDeleteAssociationParameters
   */
  public function testDeleteAssociationMethod(?string $client) : void {
    $deleteQuery = $this
      ->createMock(Delete::class);
    if (!empty($client)) {
      $deleteQuery
        ->expects($this
        ->exactly(2))
        ->method('condition')
        ->withConsecutive([
        'uid',
        self::USER_ID,
        '=',
      ], [
        'client_name',
        $client,
        '=',
      ])
        ->willReturnSelf();
    }
    else {
      $deleteQuery
        ->expects($this
        ->once())
        ->method('condition')
        ->with('uid', self::USER_ID, '=')
        ->willReturnSelf();
    }
    $deleteQuery
      ->expects($this
      ->once())
      ->method('execute')
      ->willReturn(1);
    $this->connection
      ->expects($this
      ->once())
      ->method('delete')
      ->with('openid_connect_authmap')
      ->willReturn($deleteQuery);
    $authMapClass = new OpenIDConnectAuthmap($this->connection, $this->entityTypeManager);
    $authMapClass
      ->deleteAssociation(self::USER_ID, $client);
  }

  /**
   * Provide data to the testDeleteAssociationMethod test.
   *
   * @return array
   *   Return the client names to test.
   */
  public function getDeleteAssociationParameters() : array {
    return [
      [
        '',
      ],
      [
        'test_client',
      ],
    ];
  }

  /**
   * Test for the userLoadBySub method.
   *
   * @param string $sub
   *   The sub to test.
   * @param string $client
   *   The client to test.
   * @param array $results
   *   The results to return.
   *
   * @dataProvider getUserLoadBySubParameters
   */
  public function testUserLoadBySubMethod(string $sub, string $client, array $results) : void {
    if (!empty($results)) {
      $account = $this
        ->createMock(User::class);
      $this->userStorage
        ->expects($this
        ->once())
        ->method('load')
        ->willReturn($account);
    }
    $selectMock = $this
      ->createMock(SelectInterface::class);
    $selectMock
      ->expects($this
      ->once())
      ->method('fields')
      ->with('a', [
      'uid',
    ])
      ->willReturnSelf();
    $selectMock
      ->expects($this
      ->exactly(2))
      ->method('condition')
      ->withConsecutive([
      'client_name',
      $client,
      '=',
    ], [
      'sub',
      $sub,
      '=',
    ])
      ->willReturnSelf();
    $selectMock
      ->expects($this
      ->once())
      ->method('execute')
      ->willReturn($results);
    $this->connection
      ->expects($this
      ->once())
      ->method('select')
      ->willReturn($selectMock);
    $authMapClass = new OpenIDConnectAuthmap($this->connection, $this->entityTypeManager);
    $actualResult = $authMapClass
      ->userLoadBySub($sub, $client);
    if (empty($results)) {
      $this
        ->assertEquals(FALSE, $actualResult);
    }
    else {
      $this
        ->assertInstanceOf('\\Drupal\\Core\\Entity\\EntityInterface', $actualResult);
    }
  }

  /**
   * Data provider for the userLoadBySubMethod().
   *
   * @return array|array[]
   *   The parameters to pass to the userLoadBySubMethod.
   */
  public function getUserLoadBySubParameters() : array {
    $test = (object) [
      'uid' => self::USER_ID,
    ];
    $results = [
      $test,
    ];
    return [
      [
        '',
        '',
        [],
      ],
      [
        'sub',
        '',
        [],
      ],
      [
        '',
        'client',
        [],
      ],
      [
        'sub',
        'client',
        [],
      ],
      [
        '',
        '',
        $results,
      ],
      [
        'sub',
        '',
        $results,
      ],
      [
        '',
        'client',
        $results,
      ],
      [
        'sub',
        'client',
        $results,
      ],
    ];
  }

  /**
   * Test the getConnectedAccounts method.
   *
   * @param array $results
   *   The results returned from the database query.
   *
   * @dataProvider getConnectedAccountsParameters
   */
  public function testGetConnectedAccounts(array $results) : void {
    $account = $this
      ->createMock(User::class);
    $account
      ->expects($this
      ->once())
      ->method('id')
      ->willReturn(self::USER_ID);
    $selectInterface = $this
      ->createMock(SelectInterface::class);
    $selectInterface
      ->expects($this
      ->once())
      ->method('fields')
      ->with('a', [
      'client_name',
      'sub',
    ])
      ->willReturnSelf();
    $selectInterface
      ->expects($this
      ->once())
      ->method('condition')
      ->with('uid', self::USER_ID)
      ->willReturnSelf();
    $selectInterface
      ->expects($this
      ->once())
      ->method('execute')
      ->willReturn($results);
    $this->connection
      ->expects($this
      ->once())
      ->method('select')
      ->willReturn($selectInterface);
    $authMapClass = new OpenIDConnectAuthmap($this->connection, $this->entityTypeManager);
    $actualResult = $authMapClass
      ->getConnectedAccounts($account);
    if (!empty($results)) {
      $record = array_shift($results);
      $expected = [
        $record->client_name => $record->sub,
      ];
      $this
        ->assertArrayEquals($expected, $actualResult);
    }
    else {
      $this
        ->assertEmpty($actualResult);
    }
  }

  /**
   * Data provider for the getConnectedAccounts method.
   *
   * @return array
   *   Data to test the getConnectedAccounts method.
   */
  public function getConnectedAccountsParameters() : array {
    $record = (object) [
      'client_name' => $this
        ->randomMachineName(),
      'sub' => $this
        ->randomMachineName(),
    ];
    return [
      [
        [],
      ],
      [
        [
          $record,
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OpenIDConnectAuthmapTest::$account protected property Mock of the current_user service.
OpenIDConnectAuthmapTest::$connection protected property Mock the database connection service.
OpenIDConnectAuthmapTest::$entityTypeManager protected property Mock of the entity_type.manager service.
OpenIDConnectAuthmapTest::$userStorage protected property Mock of the EntityStorageInterface for User objects.
OpenIDConnectAuthmapTest::getConnectedAccountsParameters public function Data provider for the getConnectedAccounts method.
OpenIDConnectAuthmapTest::getDeleteAssociationParameters public function Provide data to the testDeleteAssociationMethod test.
OpenIDConnectAuthmapTest::getUserLoadBySubParameters public function Data provider for the userLoadBySubMethod().
OpenIDConnectAuthmapTest::setUp protected function Overrides UnitTestCase::setUp
OpenIDConnectAuthmapTest::testCreateAssociationMethod public function Test the createAssociation method.
OpenIDConnectAuthmapTest::testDeleteAssociationMethod public function Test the deleteAssociationMethod.
OpenIDConnectAuthmapTest::testGetConnectedAccounts public function Test the getConnectedAccounts method.
OpenIDConnectAuthmapTest::testUserLoadBySubMethod public function Test for the userLoadBySub method.
OpenIDConnectAuthmapTest::USER_ID constant The user_id to test.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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.