You are here

public function OAuth2ServerStorageTestCase::testAccessToken in OAuth2 Server 7

File

tests/oauth2_server.test, line 979
OAuth2 tests.

Class

OAuth2ServerStorageTestCase
Test \Drupal\oauth2_server\Storage.

Code

public function testAccessToken() {
  $user = $this
    ->drupalCreateUser(array(
    'use oauth2 server',
  ));
  $token = $this->storage
    ->getAccessToken('newtoken');
  $this
    ->assertFalse($token, 'Trying to load a nonexistent token is unsuccessful.');
  $expires = time() + 20;
  $success = $this->storage
    ->setAccessToken('newtoken', $this->client_key, $user->uid, $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($token, 'An access token was successfully returned.');
  $this
    ->assertTrue(array_key_exists('access_token', $token), 'The "access_token" value is present in the token array.');
  $this
    ->assertTrue(array_key_exists('client_id', $token), 'The "client_id" value is present in the token array.');
  $this
    ->assertTrue(array_key_exists('user_id', $token), 'The "user_id" value is present in the token array.');
  $this
    ->assertTrue(array_key_exists('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->client_key, 'The "client_id" key has the expected value.');
  $this
    ->assertEqual($token['user_id'], $user->uid, '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 = $this->storage
    ->setAccessToken('newtoken', $this->client_key, $user->uid, $expires);
  $this
    ->assertTrue($success, 'The access token was successfully updated.');
  $token = $this->storage
    ->getAccessToken('newtoken');
  $this
    ->assertTrue($token, 'An access token was successfully returned.');
  $this
    ->assertEqual($token['expires'], $expires, 'The expires timestamp matches the new value.');
}