Commit a98b454c authored by pfrilling's avatar pfrilling
Browse files

Squashed commit of the following:

commit 38e164e96d711c2a6da0da3e6c468bcd125a6be2
Author: Philip Frilling <philip@frilling.com>
Date:   Fri May 1 13:50:00 2020 -0400

    Added tests for the getConnectedAccounts method.

commit b06df02cfff42b09f2e569cba533781a7985822a
Author: Philip Frilling <philip@frilling.com>
Date:   Fri May 1 08:36:58 2020 -0400

    Added test coverage for the userLoadBySub method

commit 63b0800294dda1bab832bb2f842610d7ff4c1294
Author: Philip Frilling <philip@frilling.com>
Date:   Thu Apr 30 08:39:43 2020 -0400

    Added test covereage for the delete association method

commit b8d87a433b30017323252ddf9f7e51d0b22859bf
Author: Philip Frilling <philip@frilling.com>
Date:   Wed Apr 29 08:17:11 2020 -0400

    Added unit test for the createAssociation method.
parent 1bf49f85
......@@ -6,9 +6,10 @@ The format is based on [Keep a Changelog][].
[Keep a Changelog]: http://keepachangelog.com/
## [Unreleased]
### Added
- Added unit tests for the OpenIdConnectSession class.
- Added unit tests for the OpenIdConnectClaims class.
- Added unit tests for the OpenIDConnectSession class.
- Added unit tests for the OpenIDConnectClaims class.
- Added unit tests for the OpenIDConnectStateToken class.
- Added unit tests for the OpenIDConnectAuthmap class.
### Changed
### Deprecated
......
<?php
declare(strict_types = 1);
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Database\Query\Insert;
use Drupal\openid_connect\OpenIDConnectAuthmap;
use Drupal\Core\Database\Query\Delete;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\user\Entity\User;
/**
* Test the OpenIdConnectAuthmap class.
*
* @coversDefaultClass \Drupal\openid_connect\OpenIDConnectAuthmap
* @group openid_connect
*/
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 {
$expectdFieldsArray = [
'uid' => self::USER_ID,
'client_name' => 'generic',
'sub' => 'subject',
];
$this->account->expects($this->once())
->method('id')
->willReturn(self::USER_ID);
$queryInsert = $this->createMock(Insert::class);
$queryInsert->expects($this->once())
->method('fields')
->with($expectdFieldsArray)
->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|string[]
* 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 = new stdClass();
$test->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 = new stdClass();
$record->client_name = $this->randomMachineName();
$record->sub = $this->randomMachineName();
return [
[[]],
[[$record]],
];
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment