INFRA-124 - CiviSeleniumTestCase - Load cookies from $this->settings->cookies
authorTim Otten <totten@civicrm.org>
Wed, 23 Jul 2014 08:29:32 +0000 (01:29 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 23 Jul 2014 08:29:32 +0000 (01:29 -0700)
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.

tests/phpunit/CiviTest/CiviSeleniumSettings.php.txt
tests/phpunit/CiviTest/CiviSeleniumTestCase.php

index 316cdc0431e1b52bd28cc1c45289cfd312087a23..b64c315888aac8bccf1d8b041ec5f2a0ab38c7ce 100644 (file)
@@ -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,
+    // );;
   }
 
 }
index 1a98695e407941cf00b438464aea80c0cd9dffb5..1c1a6cd2d6b49ab4e066c309d8aaf034c24b3fd7 100644 (file)
@@ -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() {
   }