function UserLoginTest::testCookiesNotAccepted

Same name and namespace in other branches
  1. 10 core/modules/user/tests/src/Functional/UserLoginTest.php \Drupal\Tests\user\Functional\UserLoginTest::testCookiesNotAccepted()
  2. 11.x 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 239

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.