You are here

public function UserLoginTest::testCookiesNotAccepted in Drupal 9

Same name and namespace in other branches
  1. 10 core/modules/user/tests/src/Functional/UserLoginTest.php \Drupal\Tests\user\Functional\UserLoginTest::testCookiesNotAccepted()

Tests with a browser that denies cookies.

File

core/modules/user/tests/src/Functional/UserLoginTest.php, line 158

Class

UserLoginTest
Ensure that login works as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testCookiesNotAccepted() {
  $this
    ->drupalGet('user/login');
  $form_build_id = $this
    ->getSession()
    ->getPage()
    ->findField('form_build_id');
  $account = $this
    ->drupalCreateUser([]);
  $post = [
    'form_id' => 'user_login_form',
    'form_build_id' => $form_build_id,
    'name' => $account
      ->getAccountName(),
    'pass' => $account->passRaw,
    'op' => 'Log in',
  ];
  $url = $this
    ->buildUrl(Url::fromRoute('user.login'));

  /** @var \Psr\Http\Message\ResponseInterface $response */
  $response = $this
    ->getHttpClient()
    ->post($url, [
    'form_params' => $post,
    'http_errors' => FALSE,
    'cookies' => FALSE,
    'allow_redirects' => FALSE,
  ]);

  // Follow the location header.
  $this
    ->drupalGet($response
    ->getHeader('location')[0]);
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->assertSession()
    ->pageTextContains('To log in to this site, your browser must accept cookies from the domain');
}