You are here

class OpenIdConnectSessionTest in OpenID Connect / OAuth client 2.x

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

@coversDefaultClass \Drupal\openid_connect\OpenIDConnectSession @group openid_connect

Hierarchy

Expanded class hierarchy of OpenIdConnectSessionTest

File

tests/src/Unit/OpenIdConnectSessionTest.php, line 22

Namespace

Drupal\Tests\openid_connect\Unit
View 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

Namesort descending Modifiers Type Description Overrides
OpenIdConnectSessionTest::$configFactory protected property A mock of the config.factory service.
OpenIdConnectSessionTest::$languageManager protected property A mock of the language manager service.
OpenIdConnectSessionTest::$redirectDestination protected property A mock of the redirect.destination service.
OpenIdConnectSessionTest::$session protected property A mock of the session service.
OpenIdConnectSessionTest::setUp protected function Overrides UnitTestCase::setUp
OpenIdConnectSessionTest::testSaveDestination public function Test the saveDestination method.
OpenIdConnectSessionTest::testSaveDestinationUserPath public function Test the saveDestination() method with the /user/login path.
OpenIdConnectSessionTest::TEST_PATH constant Create a test path for testing.
OpenIdConnectSessionTest::TEST_QUERY constant A query string to test with.
OpenIdConnectSessionTest::TEST_USER_PATH constant The user login path for testing.
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