You are here

public function OAuth2ServerStorageTest::testAuthorizationCode in OAuth2 Server 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Functional/OAuth2ServerStorageTest.php \Drupal\Tests\oauth2_server\Functional\OAuth2ServerStorageTest::testAuthorizationCode()

Authorization code.

File

tests/src/Functional/OAuth2ServerStorageTest.php, line 213

Class

OAuth2ServerStorageTest
The OAuth2 Server admin test case.

Namespace

Drupal\Tests\oauth2_server\Functional

Code

public function testAuthorizationCode() {
  $user = $this
    ->drupalCreateUser([
    'use oauth2 server',
  ]);
  $code = (bool) $this->storage
    ->getAuthorizationCode('newcode');
  $this
    ->assertFalse($code, 'Trying to load a nonexistent authorization code is unsuccessful.');
  $expires = time() + 20;
  $success = (bool) $this->storage
    ->setAuthorizationCode('newcode', $this->clientId, $user
    ->id(), 'http://example.com', $expires);
  $this
    ->assertTrue($success, 'A new authorization code was successfully created.');

  // Verify the return format of getAuthorizationCode().
  $code = $this->storage
    ->getAuthorizationCode('newcode');
  $this
    ->assertTrue((bool) $code, 'An authorization code was successfully returned.');
  $this
    ->assertArrayHasKey('authorization_code', $code, 'The "authorization_code" value is present in the code array.');
  $this
    ->assertArrayHasKey('client_id', $code, 'The "client_id" value is present in the code array.');
  $this
    ->assertArrayHasKey('user_id', $code, 'The "user_id" value is present in the code array.');
  $this
    ->assertArrayHasKey('redirect_uri', $code, 'The "redirect_uri" value is present in the code array.');
  $this
    ->assertArrayHasKey('expires', $code, 'The "expires" value is present in the code array.');
  $this
    ->assertEqual($code['authorization_code'], 'newcode', 'The "authorization_code" key has the expected value.');
  $this
    ->assertEqual($code['client_id'], $this->clientId, 'The "client_id" key has the expected value.');
  $this
    ->assertEqual($code['user_id'], $user
    ->id(), 'The "user_id" key has the expected value.');
  $this
    ->assertEqual($code['redirect_uri'], 'http://example.com', 'The "redirect_uri" key has the expected value.');
  $this
    ->assertEqual($code['expires'], $expires, 'The "expires" key has the expected value.');

  // Change an existing code.
  $expires = time() + 42;
  $success = (bool) $this->storage
    ->setAuthorizationCode('newcode', $this->clientId, $user
    ->id(), 'http://example.org', $expires);
  $this
    ->assertTrue($success, 'The authorization code was successfully updated.');
  $code = $this->storage
    ->getAuthorizationCode('newcode');
  $this
    ->assertTrue((bool) $code, 'An authorization code was successfully returned.');
  $this
    ->assertEqual($code['expires'], $expires, 'The expires timestamp matches the new value.');
}