You are here

public function RolesNegotiationFunctionalTest::setUp in Simple OAuth (OAuth2) & OpenID Connect 8.2

Same name and namespace in other branches
  1. 8.3 simple_oauth_extras/tests/src/Functional/RolesNegotiationFunctionalTest.php \Drupal\Tests\simple_oauth_extras\Functional\RolesNegotiationFunctionalTest::setUp()

Overrides BrowserTestBase::setUp

File

simple_oauth_extras/tests/src/Functional/RolesNegotiationFunctionalTest.php, line 71

Class

RolesNegotiationFunctionalTest
@group simple_oauth_extras

Namespace

Drupal\Tests\simple_oauth_extras\Functional

Code

public function setUp() {
  parent::setUp();
  $this->htmlOutputEnabled = FALSE;
  $this->tokenTestUrl = Url::fromRoute('oauth2_token.user_debug');
  $this->url = Url::fromRoute('oauth2_token.token');
  $this->user = $this
    ->drupalCreateUser();

  // Set up a HTTP client that accepts relative URLs.
  $this->httpClient = $this->container
    ->get('http_client_factory')
    ->fromOptions([
    'base_uri' => $this->baseUrl,
  ]);
  $this->clientSecret = $this
    ->getRandomGenerator()
    ->string();

  // Create a role 'foo' and add two permissions to it.
  $role = Role::create([
    'id' => 'foo',
    'label' => 'Foo',
    'is_admin' => FALSE,
  ]);
  $role
    ->grantPermission('view own simple_oauth entities');
  $role
    ->save();
  $role = Role::create([
    'id' => 'bar',
    'label' => 'Bar',
    'is_admin' => FALSE,
  ]);
  $role
    ->grantPermission('administer simple_oauth entities');
  $role
    ->save();
  $role = Role::create([
    'id' => 'oof',
    'label' => 'Oof',
    'is_admin' => FALSE,
  ]);
  $role
    ->grantPermission('delete own simple_oauth entities');
  $role
    ->save();
  $this->user
    ->addRole('foo');
  $this->user
    ->addRole('bar');
  $this->user
    ->save();

  // Create a Oauth2Client.
  $this->client = Oauth2Client::create([
    'owner_id' => 1,
    'user_id' => $this->user
      ->id(),
    'label' => $this
      ->getRandomGenerator()
      ->name(),
    'secret' => $this->clientSecret,
    'confidential' => TRUE,
    'roles' => [
      [
        'target_id' => 'oof',
      ],
    ],
  ]);
  $this->client
    ->save();

  // Configure 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();
}