You are here

protected function OAuth2ServerTest::setUp in OAuth2 Server 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/OAuth2ServerTest.php \Drupal\Tests\oauth2_server\Functional\OAuth2ServerTest::setUp()

Overrides BrowserTestBase::setUp

File

tests/src/Functional/OAuth2ServerTest.php, line 102

Class

OAuth2ServerTest
The OAuth2 Server admin test case.

Namespace

Drupal\Tests\oauth2_server\Functional

Code

protected function setUp() {
  parent::setUp();
  $this->redirectUri = $this
    ->buildUrl('/user', [
    'absolute' => TRUE,
  ]);

  // Set the keys so that the module can see them.
  $keys = [
    'public_key' => $this->publicKey,
    'private_key' => $this->privateKey,
  ];
  \Drupal::state()
    ->set('oauth2_server.keys', $keys);
  \Drupal::state()
    ->set('oauth2_server.last_generated', \Drupal::time()
    ->getRequestTime());

  /** @var \Drupal\oauth2_server\ServerInterface $server */
  $server = $this->container
    ->get('entity_type.manager')
    ->getStorage('oauth2_server')
    ->create([
    'server_id' => 'test_server',
    'name' => 'Test Server',
    'settings' => [
      'default_scope' => 'test_server_basic',
      'enforce_state' => TRUE,
      'allow_implicit' => TRUE,
      'use_openid_connect' => TRUE,
      'use_crypto_tokens' => FALSE,
      'store_encrypted_token_string' => FALSE,
      'grant_types' => [
        'authorization_code' => 'authorization_code',
        'client_credentials' => 'client_credentials',
        'urn:ietf:params:oauth:grant-type:jwt-bearer' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        'refresh_token' => 'refresh_token',
        'password' => 'password',
      ],
      'always_issue_new_refresh_token' => TRUE,
      'advanced_settings' => [
        'require_exact_redirect_uri' => TRUE,
        'access_lifetime' => 3600,
        'id_lifetime' => 3600,
        'refresh_token_lifetime' => 1209600,
      ],
    ],
  ]);
  $server
    ->save();

  /** @var \Drupal\oauth2_server\ClientInterface $client */
  $client = $this->container
    ->get('entity_type.manager')
    ->getStorage('oauth2_server_client')
    ->create([
    'client_id' => $this->clientId,
    'server_id' => $server
      ->id(),
    'name' => 'Test client',
    'unhashed_client_secret' => $this->clientSecret,
    'public_key' => $this->publicKey,
    'redirect_uri' => 'https://google.com' . "\n" . $this->redirectUri,
    'automatic_authorization' => TRUE,
  ]);
  $client
    ->save();
  $scopes = [
    'basic' => 'Basic',
    'admin' => 'Admin',
    'forbidden' => 'Forbidden',
    'phone' => 'phone',
  ];
  foreach ($scopes as $scope_name => $scope_label) {
    $scope = $this->container
      ->get('entity_type.manager')
      ->getStorage('oauth2_server_scope')
      ->create([
      'scope_id' => $scope_name,
      'server_id' => $server
        ->id(),
      'description' => $scope_label,
    ]);
    $scope
      ->save();
  }
}