From bf4469825e1f96c8e13c5d6e7f047b46dffdd53f Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 23 Jul 2014 01:29:32 -0700 Subject: [PATCH] INFRA-124 - CiviSeleniumTestCase - Load cookies from $this->settings->cookies For INFRA-124, we'll need a way to distinguish requests that correspond to different test-instances. We'll do that by passing along a cookie. --- .../CiviTest/CiviSeleniumSettings.php.txt | 12 ++++++ .../phpunit/CiviTest/CiviSeleniumTestCase.php | 43 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/tests/phpunit/CiviTest/CiviSeleniumSettings.php.txt b/tests/phpunit/CiviTest/CiviSeleniumSettings.php.txt index 316cdc0431..b64c315888 100644 --- a/tests/phpunit/CiviTest/CiviSeleniumSettings.php.txt +++ b/tests/phpunit/CiviTest/CiviSeleniumSettings.php.txt @@ -42,6 +42,12 @@ class CiviSeleniumSettings { */ var $timeout = 30; + /** + * @var array + * @see CiviSeleniumTestCase::setCookies + */ + var $cookies = array(); + /** * @var int|NULL seconds to wait for SeleniumRC to become available * @@ -54,6 +60,12 @@ class CiviSeleniumSettings { function __construct() { $this->fullSandboxPath = $this->sandboxURL . $this->sandboxPATH; + // $this->cookies[] = array( + // 'name' => 'mycookie', + // 'value' => 'myvalue', + // 'path' => '/', + // 'max_age' => 24*60*60, + // );; } } diff --git a/tests/phpunit/CiviTest/CiviSeleniumTestCase.php b/tests/phpunit/CiviTest/CiviSeleniumTestCase.php index 1a98695e40..1c1a6cd2d6 100644 --- a/tests/phpunit/CiviTest/CiviSeleniumTestCase.php +++ b/tests/phpunit/CiviTest/CiviSeleniumTestCase.php @@ -105,6 +105,49 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase { } } + protected function prepareTestSession() { + $result = parent::prepareTestSession(); + + // Set any cookies required by local installation + // Note: considered doing this in setUp(), but the Selenium session wasn't yet initialized. + if (property_exists($this->settings, 'cookies')) { + // We don't really care about this page, but it seems we need + // to open a page before setting a cookie. + $this->open($this->sboxPath); + $this->waitForPageToLoad($this->getTimeoutMsec()); + $this->setCookies($this->settings->cookies); + } + return $result; + } + + /** + * @param array $cookies each item is an array with keys: + * - name: string + * - value: string; note that RFC's don't define particular encoding scheme, so + * you must pick one yourself and pre-encode; does not allow values with + * commas, semicolons, or whitespace + * - path: string; default: '/' + * - max_age: int; default: 1 week (7*24*60*60) + */ + protected function setCookies($cookies) { + foreach ($cookies as $cookie) { + if (!isset($cookie['path'])) { + $cookie['path'] = '/'; + } + if (!isset($cookie['max_age'])) { + $cookie['max_age'] = 7*24*60*60; + } + $this->deleteCookie($cookie['name'], $cookie['path']); + $optionExprs = array(); + foreach ($cookie as $key => $value) { + if ($key != 'name' && $key != 'value') { + $optionExprs[] = "$key=$value"; + } + } + $this->createCookie("{$cookie['name']}={$cookie['value']}", implode(', ', $optionExprs)); + } + } + protected function tearDown() { } -- 2.25.1