You are here

class OpenIDConnectStateTokenTest in OpenID Connect / OAuth client 2.x

Same name and namespace in other branches
  1. 8 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 17

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;

  /**
   * A mock of the openid_connect.session service.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject
   */
  protected $session;

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

  /**
   * {@inheritDoc}
   */
  protected function setUp() : void {
    parent::setUp();

    // Mock the 'openid_connect.session' service.
    $this->session = $this
      ->createMock(OpenIDConnectSessionInterface::class);
    $this->stateTokenService = new OpenIDConnectStateToken($this->session);

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

  /**
   * Test the state tokens.
   *
   * @runInSeparateProcess
   */
  public function testConfirm() : void {
    $random = $this
      ->randomMachineName();
    $this->session
      ->expects($this
      ->atLeast(2))
      ->method('retrieveStateToken')
      ->willReturnOnConsecutiveCalls($this->stateToken, $random, '');

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

    // Change the session variable.
    $this->session
      ->saveStateToken($random);
    $confirmResultFalse = $this->stateTokenService
      ->confirm($this->stateToken);

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

    // 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::$session protected property A mock of the openid_connect.session service.
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.
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