function BrowserTest::testCookies
Tests that cookies set during a request are available for testing.
File
-
core/
modules/ simpletest/ src/ Tests/ BrowserTest.php, line 93
Class
- BrowserTest
- Tests the internal browser of the testing framework.
Namespace
Drupal\simpletest\TestsCode
public function testCookies() {
// Check that the $this->cookies property is populated when a user logs in.
$user = $this->drupalCreateUser();
$edit = [
'name' => $user->getAccountName(),
'pass' => $user->pass_raw,
];
$this->drupalPostForm('<front>', $edit, t('Log in'));
$this->assertEqual(count($this->cookies), 1, 'A cookie is set when the user logs in.');
// Check that the name and value of the cookie match the request data.
$cookie_header = $this->drupalGetHeader('set-cookie', TRUE);
// The name and value are located at the start of the string, separated by
// an equals sign and ending in a semicolon.
preg_match('/^([^=]+)=([^;]+)/', $cookie_header, $matches);
$name = $matches[1];
$value = $matches[2];
$this->assertTrue(array_key_exists($name, $this->cookies), 'The cookie name is correct.');
$this->assertEqual($value, $this->cookies[$name]['value'], 'The cookie value is correct.');
// Set a flag indicating that a cookie has been set in this test.
// @see testCookieDoesNotBleed()
static::$cookieSet = TRUE;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.