class OpenIdConnectSessionTest in OpenID Connect / OAuth client 2.x
Same name and namespace in other branches
- 8 tests/src/Unit/OpenIdConnectSessionTest.php \Drupal\Tests\openid_connect\Unit\OpenIdConnectSessionTest
@coversDefaultClass \Drupal\openid_connect\OpenIDConnectSession @group openid_connect
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\openid_connect\Unit\OpenIdConnectSessionTest
Expanded class hierarchy of OpenIdConnectSessionTest
File
- tests/
src/ Unit/ OpenIdConnectSessionTest.php, line 22
Namespace
Drupal\Tests\openid_connect\UnitView source
class OpenIdConnectSessionTest extends UnitTestCase {
/**
* Create a test path for testing.
*/
const TEST_PATH = '/test/path/1';
/**
* The user login path for testing.
*/
const TEST_USER_PATH = '/user/login';
/**
* A query string to test with.
*/
const TEST_QUERY = 'sport=baseball&team=reds';
/**
* A mock of the config.factory service.
*
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $configFactory;
/**
* A mock of the redirect.destination service.
*
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $redirectDestination;
/**
* A mock of the session service.
*
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $session;
/**
* A mock of the language manager service.
*
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $languageManager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Mock the configuration factory service.
$this->configFactory = $this
->createMock(ConfigFactoryInterface::class);
// Mock the 'redirect.destination' service.
$this->redirectDestination = $this
->createMock(RedirectDestinationInterface::class);
// Mock the 'session' service.
$this->session = $this
->createMock(SessionInterface::class);
// Mock the 'language_manager' service.
$this->languageManager = $this
->createMock(LanguageManagerInterface::class);
// Mock the url generator service.
$urlGenerator = $this
->createMock(UrlGeneratorInterface::class);
$urlGenerator
->expects($this
->once())
->method('generateFromRoute')
->with('user.login', [], [], FALSE)
->willReturn('/user/login');
$container = new ContainerBuilder();
$container
->set('url_generator', $urlGenerator);
\Drupal::setContainer($container);
}
/**
* Test the saveDestination method.
*/
public function testSaveDestination() : void {
// Get the expected destination.
$expectedDestination = self::TEST_PATH . '?' . self::TEST_QUERY;
// Mock the get method for the 'redirect.destination' service.
$this->redirectDestination
->expects($this
->once())
->method('get')
->willReturn($expectedDestination);
// Mock the get method for the 'session' service.
$this->session
->expects($this
->exactly(2))
->method('get')
->willReturnOnConsecutiveCalls($expectedDestination, 'und');
$language = $this
->createMock(LanguageInterface::class);
$language
->expects($this
->once())
->method('getId')
->willReturn('und');
$this->languageManager
->expects($this
->once())
->method('getCurrentLanguage')
->willReturn($language);
// Create a new OpenIDConnectSession class.
$session = new OpenIDConnectSession($this->configFactory, $this->redirectDestination, $this->session, $this->languageManager);
// Call the saveDestination() method.
$session
->saveDestination();
// Call the retrieveDestination method.
$destination = $session
->retrieveDestination();
// Assert the destination matches our expectation.
$this
->assertEquals($destination, [
'destination' => $expectedDestination,
'langcode' => 'und',
]);
}
/**
* Test the saveDestination() method with the /user/login path.
*/
public function testSaveDestinationUserPath() : void {
// Setup our expected results.
$expectedDestination = 'user';
$immutableConfig = $this
->createMock(ImmutableConfig::class);
$this->configFactory
->expects($this
->once())
->method('get')
->with('openid_connect.settings')
->willReturn($immutableConfig);
// Mock the get method with the user login path.
$this->redirectDestination
->expects($this
->once())
->method('get')
->willReturn(self::TEST_USER_PATH);
// Mock the get method for the 'session' service.
$this->session
->expects($this
->exactly(2))
->method('get')
->willReturnOnConsecutiveCalls($expectedDestination, 'und');
$language = $this
->createMock(LanguageInterface::class);
$language
->expects($this
->once())
->method('getId')
->willReturn('und');
$this->languageManager
->expects($this
->once())
->method('getCurrentLanguage')
->willReturn($language);
// Create a class to test with.
$session = new OpenIDConnectSession($this->configFactory, $this->redirectDestination, $this->session, $this->languageManager);
// Call the saveDestination method.
$session
->saveDestination();
// Call the retrieveDestination method.
$destination = $session
->retrieveDestination();
// Assert the destination matches our expectations.
$this
->assertEquals($destination, [
'destination' => $expectedDestination,
'langcode' => 'und',
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OpenIdConnectSessionTest:: |
protected | property | A mock of the config.factory service. | |
OpenIdConnectSessionTest:: |
protected | property | A mock of the language manager service. | |
OpenIdConnectSessionTest:: |
protected | property | A mock of the redirect.destination service. | |
OpenIdConnectSessionTest:: |
protected | property | A mock of the session service. | |
OpenIdConnectSessionTest:: |
protected | function |
Overrides UnitTestCase:: |
|
OpenIdConnectSessionTest:: |
public | function | Test the saveDestination method. | |
OpenIdConnectSessionTest:: |
public | function | Test the saveDestination() method with the /user/login path. | |
OpenIdConnectSessionTest:: |
constant | Create a test path for testing. | ||
OpenIdConnectSessionTest:: |
constant | A query string to test with. | ||
OpenIdConnectSessionTest:: |
constant | The user login path for testing. | ||
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |