You are here

class OpenIDConnectStateTokenTest 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

Test the OpenIDConnectStateToken class.

@coversDefaultClass \Drupal\openid_connect\OpenIDConnectStateToken @group openid_connect

Hierarchy

Expanded class hierarchy of OpenIDConnectStateTokenTest

File

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

Namespace

Drupal\Tests\openid_connect\Unit
View source
class OpenIDConnectStateTokenTest extends UnitTestCase {

  /**
   * Mock of the openid_connect.state_token service.
   *
   * @var \Drupal\openid_connect\OpenIDConnectStateToken
   */
  protected $stateTokenService;

  /**
   * The state token created for these tests.
   *
   * @var string
   */
  protected $stateToken;

  /**
   * {@inheritDoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->stateTokenService = new OpenIDConnectStateToken();

    // Set the state token and save the results.
    $this->stateToken = $this->stateTokenService
      ->create();
  }

  /**
   * Test the state tokens.
   *
   * @runInSeparateProcess
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OpenIDConnectStateTokenTest::$stateToken protected property The state token created for these tests.
OpenIDConnectStateTokenTest::$stateTokenService protected property Mock of the openid_connect.state_token service.
OpenIDConnectStateTokenTest::setUp protected function Overrides UnitTestCase::setUp
OpenIDConnectStateTokenTest::testConfirm public function Test the state tokens.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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.