You are here

protected function CookieResourceTestTrait::initAuthentication in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/CookieResourceTestTrait.php \Drupal\Tests\rest\Functional\CookieResourceTestTrait::initAuthentication()
  2. 9 core/modules/rest/tests/src/Functional/CookieResourceTestTrait.php \Drupal\Tests\rest\Functional\CookieResourceTestTrait::initAuthentication()

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'];
}