You are here

protected function TokenBearerFunctionalTestBase::setUp in Simple OAuth (OAuth2) & OpenID Connect 8.2

Same name and namespace in other branches
  1. 8.4 tests/src/Functional/TokenBearerFunctionalTestBase.php \Drupal\Tests\simple_oauth\Functional\TokenBearerFunctionalTestBase::setUp()
  2. 8.3 tests/src/Functional/TokenBearerFunctionalTestBase.php \Drupal\Tests\simple_oauth\Functional\TokenBearerFunctionalTestBase::setUp()
  3. 5.x tests/src/Functional/TokenBearerFunctionalTestBase.php \Drupal\Tests\simple_oauth\Functional\TokenBearerFunctionalTestBase::setUp()

Overrides BrowserTestBase::setUp

3 calls to TokenBearerFunctionalTestBase::setUp()
AuthCodeFunctionalTest::setUp in simple_oauth_extras/tests/src/Functional/AuthCodeFunctionalTest.php
ImplicitFunctionalTest::setUp in simple_oauth_extras/tests/src/Functional/ImplicitFunctionalTest.php
RefreshFunctionalTest::setUp in simple_oauth_extras/tests/src/Functional/RefreshFunctionalTest.php
3 methods override TokenBearerFunctionalTestBase::setUp()
AuthCodeFunctionalTest::setUp in simple_oauth_extras/tests/src/Functional/AuthCodeFunctionalTest.php
ImplicitFunctionalTest::setUp in simple_oauth_extras/tests/src/Functional/ImplicitFunctionalTest.php
RefreshFunctionalTest::setUp in simple_oauth_extras/tests/src/Functional/RefreshFunctionalTest.php

File

tests/src/Functional/TokenBearerFunctionalTestBase.php, line 80

Class

TokenBearerFunctionalTestBase
Class TokenBearerFunctionalTestBase

Namespace

Drupal\Tests\simple_oauth\Functional

Code

protected function setUp() {
  parent::setUp();
  $this->htmlOutputEnabled = FALSE;
  $this->url = Url::fromRoute('oauth2_token.token');

  // Set up a HTTP client that accepts relative URLs.
  $this->httpClient = $this->container
    ->get('http_client_factory')
    ->fromOptions([
    'base_uri' => $this->baseUrl,
  ]);
  $client_role = Role::create([
    'id' => $this
      ->getRandomGenerator()
      ->name(8, TRUE),
    'label' => $this
      ->getRandomGenerator()
      ->word(5),
    'is_admin' => FALSE,
  ]);
  $client_role
    ->save();
  $this->additionalRoles = [];
  for ($i = 0; $i < mt_rand(1, 3); $i++) {
    $role = Role::create([
      'id' => $this
        ->getRandomGenerator()
        ->name(8, TRUE),
      'label' => $this
        ->getRandomGenerator()
        ->word(5),
      'is_admin' => FALSE,
    ]);
    $role
      ->save();
    $this->additionalRoles[] = $role;
  }
  $this->clientSecret = $this
    ->getRandomGenerator()
    ->string();
  $this->client = Oauth2Client::create([
    'owner_id' => '',
    'label' => $this
      ->getRandomGenerator()
      ->name(),
    'secret' => $this->clientSecret,
    'confidential' => TRUE,
    'roles' => [
      [
        'target_id' => $client_role
          ->id(),
      ],
    ],
  ]);
  $this->client
    ->save();
  $this->user = $this
    ->drupalCreateUser();
  $this
    ->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), [
    'access content',
  ]);
  $this
    ->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), [
    'access content',
  ]);

  // Use the public and private keys.
  $path = $this->container
    ->get('module_handler')
    ->getModule('simple_oauth')
    ->getPath();
  $temp_dir = sys_get_temp_dir();
  $public_path = '/' . $path . '/tests/certificates/public.key';
  $private_path = '/' . $path . '/tests/certificates/private.key';
  file_put_contents($temp_dir . '/public.key', file_get_contents(DRUPAL_ROOT . $public_path));
  file_put_contents($temp_dir . '/private.key', file_get_contents(DRUPAL_ROOT . $private_path));
  chmod($temp_dir . '/public.key', 0660);
  chmod($temp_dir . '/private.key', 0660);
  $this->publicKeyPath = $temp_dir . '/public.key';
  $this->privateKeyPath = $temp_dir . '/private.key';
  $settings = $this
    ->config('simple_oauth.settings');
  $settings
    ->set('public_key', $this->publicKeyPath);
  $settings
    ->set('private_key', $this->privateKeyPath);
  $settings
    ->save();
  $num_roles = mt_rand(1, count($this->additionalRoles));
  $requested_roles = array_slice($this->additionalRoles, 0, $num_roles);
  $scopes = array_map(function (RoleInterface $role) {
    return $role
      ->id();
  }, $requested_roles);
  $this->scope = implode(' ', $scopes);
  drupal_flush_all_caches();
}