You are here

protected function CookieResourceTestTrait::initAuthentication in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/CookieResourceTestTrait.php \Drupal\Tests\rest\Functional\CookieResourceTestTrait::initAuthentication()
2 calls to CookieResourceTestTrait::initAuthentication()
DbLogResourceTest::testWatchdog in core/modules/dblog/tests/src/Functional/DbLogResourceTest.php
Writes a log messages and retrieves it via the REST API.
RestRegisterUserTest::testRegisterUser in core/modules/user/tests/src/Functional/RestRegisterUserTest.php
Tests that only anonymous users can register users.

File

core/modules/rest/tests/src/Functional/CookieResourceTestTrait.php, line 55

Class

CookieResourceTestTrait
Trait for ResourceTestBase subclasses testing $auth=cookie.

Namespace

Drupal\Tests\rest\Functional

Code

protected function initAuthentication() {
  $user_login_url = Url::fromRoute('user.login.http')
    ->setRouteParameter('_format', static::$format);
  $request_body = [
    'name' => $this->account->name->value,
    'pass' => $this->account->passRaw,
  ];
  $request_options[RequestOptions::BODY] = $this->serializer
    ->encode($request_body, static::$format);
  $request_options[RequestOptions::HEADERS] = [
    'Content-Type' => static::$mimeType,
  ];
  $response = $this
    ->request('POST', $user_login_url, $request_options);

  // Parse and store the session cookie.
  $this->sessionCookie = explode(';', $response
    ->getHeader('Set-Cookie')[0], 2)[0];

  // Parse and store the CSRF token and logout token.
  $data = $this->serializer
    ->decode((string) $response
    ->getBody(), static::$format);
  $this->csrfToken = $data['csrf_token'];
  $this->logoutToken = $data['logout_token'];
}