summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-05-25 08:35:51 (GMT)
committerAlex Pott2015-05-25 08:35:51 (GMT)
commitca5fced8c463215355f166c332dafe581271a74d (patch)
tree17c44e55a7f404351e771fa542fc9a490725e456
parent74520b4777304adfa96e7cfd90d7372a2edfe35c (diff)
Issue #2491353 by pfrenssen, znerol: Cookies from previous tests are still present when a new test starts
-rw-r--r--core/modules/simpletest/src/Tests/BrowserTest.php48
-rw-r--r--core/modules/simpletest/src/WebTestBase.php3
2 files changed, 50 insertions, 1 deletions
diff --git a/core/modules/simpletest/src/Tests/BrowserTest.php b/core/modules/simpletest/src/Tests/BrowserTest.php
index e0ac326..d43f63b 100644
--- a/core/modules/simpletest/src/Tests/BrowserTest.php
+++ b/core/modules/simpletest/src/Tests/BrowserTest.php
@@ -15,6 +15,14 @@ use Drupal\simpletest\WebTestBase;
* @group simpletest
*/
class BrowserTest extends WebTestBase {
+
+ /**
+ * A flag indicating whether a cookie has been set in a test.
+ *
+ * @var bool
+ */
+ protected static $cookieSet = FALSE;
+
/**
* Test \Drupal\simpletest\WebTestBase::getAbsoluteUrl().
*/
@@ -65,4 +73,44 @@ EOF;
$urls = $this->xpath('//a[text()=:text]', array(':text' => 'A fourth link, containing alternative \\1 regex backreferences \\2'));
$this->assertEqual($urls[0]['href'], 'link4', 'Match with another regular expression back reference symbol (double backslash).');
}
+
+ /**
+ * Tests that cookies set during a request are available for testing.
+ */
+ 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;
+ }
+
+ /**
+ * Tests that the cookies from a previous test do not bleed into a new test.
+ *
+ * @see static::testCookies()
+ */
+ public function testCookieDoesNotBleed() {
+ // In order for this test to be effective it should always run after the
+ // testCookies() test.
+ $this->assertTrue(static::$cookieSet, 'Tests have been executed in the expected order.');
+ $this->assertEqual(count($this->cookies), 0, 'No cookies are present at the start of a new test.');
+ }
+
}
diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php
index 7abfbf3..2e8702b 100644
--- a/core/modules/simpletest/src/WebTestBase.php
+++ b/core/modules/simpletest/src/WebTestBase.php
@@ -78,7 +78,7 @@ abstract class WebTestBase extends TestBase {
*
* @var array
*/
- protected $cookies;
+ protected $cookies = array();
/**
* Indicates that headers should be dumped if verbose output is enabled.
@@ -1089,6 +1089,7 @@ abstract class WebTestBase extends TestBase {
// testing so test classes containing multiple tests are not polluted.
$this->curlClose();
$this->curlCookies = array();
+ $this->cookies = array();
}
/**