OpenIdConnectSessionTest.php in OpenID Connect / OAuth client 2.x
File
tests/src/Unit/OpenIdConnectSessionTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\openid_connect\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\openid_connect\OpenIDConnectSession;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class OpenIdConnectSessionTest extends UnitTestCase {
const TEST_PATH = '/test/path/1';
const TEST_USER_PATH = '/user/login';
const TEST_QUERY = 'sport=baseball&team=reds';
protected $configFactory;
protected $redirectDestination;
protected $session;
protected $languageManager;
protected function setUp() : void {
parent::setUp();
$this->configFactory = $this
->createMock(ConfigFactoryInterface::class);
$this->redirectDestination = $this
->createMock(RedirectDestinationInterface::class);
$this->session = $this
->createMock(SessionInterface::class);
$this->languageManager = $this
->createMock(LanguageManagerInterface::class);
$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);
}
public function testSaveDestination() : void {
$expectedDestination = self::TEST_PATH . '?' . self::TEST_QUERY;
$this->redirectDestination
->expects($this
->once())
->method('get')
->willReturn($expectedDestination);
$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);
$session = new OpenIDConnectSession($this->configFactory, $this->redirectDestination, $this->session, $this->languageManager);
$session
->saveDestination();
$destination = $session
->retrieveDestination();
$this
->assertEquals($destination, [
'destination' => $expectedDestination,
'langcode' => 'und',
]);
}
public function testSaveDestinationUserPath() : void {
$expectedDestination = 'user';
$immutableConfig = $this
->createMock(ImmutableConfig::class);
$this->configFactory
->expects($this
->once())
->method('get')
->with('openid_connect.settings')
->willReturn($immutableConfig);
$this->redirectDestination
->expects($this
->once())
->method('get')
->willReturn(self::TEST_USER_PATH);
$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);
$session = new OpenIDConnectSession($this->configFactory, $this->redirectDestination, $this->session, $this->languageManager);
$session
->saveDestination();
$destination = $session
->retrieveDestination();
$this
->assertEquals($destination, [
'destination' => $expectedDestination,
'langcode' => 'und',
]);
}
}