You are here

public function BrowserTest::testCookies in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/Tests/BrowserTest.php \Drupal\simpletest\Tests\BrowserTest::testCookies()

Tests that cookies set during a request are available for testing.

File

core/modules/simpletest/src/Tests/BrowserTest.php, line 96
Contains \Drupal\simpletest\Tests\BrowserTest.

Class

BrowserTest
Tests the internal browser of the testing framework.

Namespace

Drupal\simpletest\Tests

Code

public function testCookies() {

  // Check that the $this->cookies property is populated when a user logs in.
  $user = $this
    ->drupalCreateUser();
  $edit = [
    'name' => $user
      ->getUsername(),
    '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;
}