You are here

public function OAuth2ServerStorageTest::testAccessToken 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::testAccessToken()

Access token.

File

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

Class

OAuth2ServerStorageTest
The OAuth2 Server admin test case.

Namespace

Drupal\Tests\oauth2_server\Functional

Code

public function testAccessToken() {
  $user = $this
    ->drupalCreateUser([
    'use oauth2 server',
  ]);
  $token = (bool) $this->storage
    ->getAccessToken('newtoken');
  $this
    ->assertFalse($token, 'Trying to load a nonexistent token is unsuccessful.');
  $expires = time() + 20;
  $success = (bool) $this->storage
    ->setAccessToken('newtoken', $this->clientId, $user
    ->id(), $expires);
  $this
    ->assertTrue($success, 'A new access token has been successfully created.');

  // Verify the return format of getAccessToken().
  $token = $this->storage
    ->getAccessToken('newtoken');
  $this
    ->assertTrue((bool) $token, 'An access token was successfully returned.');
  $this
    ->assertArrayHasKey('access_token', $token, 'The "access_token" value is present in the token array.');
  $this
    ->assertArrayHasKey('client_id', $token, 'The "client_id" value is present in the token array.');
  $this
    ->assertArrayHasKey('user_id', $token, 'The "user_id" value is present in the token array.');
  $this
    ->assertArrayHasKey('expires', $token, 'The "expires" value is present in the token array.');
  $this
    ->assertEqual($token['access_token'], 'newtoken', 'The "access_token" key has the expected value.');
  $this
    ->assertEqual($token['client_id'], $this->clientId, 'The "client_id" key has the expected value.');
  $this
    ->assertEqual($token['user_id'], $user
    ->id(), 'The "user_id" key has the expected value.');
  $this
    ->assertEqual($token['expires'], $expires, 'The "expires" key has the expected value.');

  // Update the token.
  $expires = time() + 42;
  $success = (bool) $this->storage
    ->setAccessToken('newtoken', $this->clientId, $user
    ->id(), $expires);
  $this
    ->assertTrue($success, 'The access token was successfully updated.');
  $token = $this->storage
    ->getAccessToken('newtoken');
  $this
    ->assertTrue((bool) $token, 'An access token was successfully returned.');
  $this
    ->assertEqual($token['expires'], $expires, 'The expires timestamp matches the new value.');
}