You are here

public function OpenIDConnectStateTokenTest::testConfirm in OpenID Connect / OAuth client 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/OpenIDConnectStateTokenTest.php \Drupal\Tests\openid_connect\Unit\OpenIDConnectStateTokenTest::testConfirm()

Test the state tokens.

@runInSeparateProcess

File

tests/src/Unit/OpenIDConnectStateTokenTest.php, line 48

Class

OpenIDConnectStateTokenTest
Test the OpenIDConnectStateToken class.

Namespace

Drupal\Tests\openid_connect\Unit

Code

public function testConfirm() : void {

  // Confirm the session matches the state token variable.
  $confirmResultTrue = $this->stateTokenService
    ->confirm($this->stateToken);
  $this
    ->assertEquals(TRUE, $confirmResultTrue);

  // Assert the state token key in the session global.
  $this
    ->assertArrayHasKey('openid_connect_state', $_SESSION);

  // Change the session variable.
  $_SESSION['openid_connect_state'] = $this
    ->randomMachineName();
  $confirmResultFalse = $this->stateTokenService
    ->confirm($this->stateToken);

  // Assert the expected value no longer matches the session.
  $this
    ->assertEquals(FALSE, $confirmResultFalse);

  // Remove the session variable altogether.
  unset($_SESSION['openid_connect_state']);

  // Check the state token.
  $confirmResultEmpty = $this->stateTokenService
    ->confirm($this->stateToken);

  // Assert the session global does not contain the state token.
  $this
    ->assertEquals(FALSE, $confirmResultEmpty);
}