You are here

public function SimpleTestBrowserTestCase::testCookies in Drupal 7

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

File

modules/simpletest/simpletest.test, line 400
Tests for simpletest.module.

Class

SimpleTestBrowserTestCase
Test internal testing framework browser.

Code

public function testCookies() {

  // Check that the $this->cookies property is populated when a user logs in.
  $user = $this
    ->drupalCreateUser();
  $edit = array(
    'name' => $user->name,
    'pass' => $user->pass_raw,
  );
  $this
    ->drupalPost('<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 SimpleTestBrowserTestCase::testCookieDoesNotBleed().
  self::$cookieSet = TRUE;
}