CRM-15031 Event Webtest Fix
[civicrm-core.git] / tests / phpunit / CiviTest / CiviSeleniumTestCase.php
index dbf4add63ea6c070c406a1ef2db8cbf1cd929132..a8f3853ccb808620629d5c52d899e7d9fd662f1f 100644 (file)
@@ -32,6 +32,7 @@ require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
  */
 define('CIVICRM_SETTINGS_PATH', __DIR__ . '/civicrm.settings.dist.php');
 define('CIVICRM_SETTINGS_LOCAL_PATH', __DIR__ . '/civicrm.settings.local.php');
+define('CIVICRM_WEBTEST', 1);
 
 if (file_exists(CIVICRM_SETTINGS_LOCAL_PATH)) {
   require_once CIVICRM_SETTINGS_LOCAL_PATH;
@@ -104,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() {
   }
 
@@ -498,10 +542,13 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
    *
    * @return void
    */
-  function fillRichTextField($fieldName, $text = 'Typing this text into editor.', $editor = 'CKEditor') {
+  function fillRichTextField($fieldName, $text = 'Typing this text into editor.', $editor = 'CKEditor', $compressed = FALSE) {
     // make sure cursor focuses on the field
     $this->fireEvent($fieldName, 'focus');
     if ($editor == 'CKEditor') {
+      if ($compressed) {
+        $this->click("{$fieldName}-plain");        
+      }
       $this->waitForElementPresent("xpath=//div[@id='cke_{$fieldName}']//iframe");
       $this->runScript("CKEDITOR.instances['{$fieldName}'].setData('<p>{$text}</p>');");
     }
@@ -875,7 +922,7 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
    * @param bool $isPcpApprovalNeeded
    * @param bool $isSeparatePayment
    * @param bool $honoreeSection
-   * @param bool $allowOtherAmmount
+   * @param bool $allowOtherAmount
    * @param bool $isConfirmEnabled
    * @param string $financialType
    * @param bool $fixedAmount
@@ -905,7 +952,7 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
                                       $isPcpApprovalNeeded = FALSE,
                                       $isSeparatePayment = FALSE,
                                       $honoreeSection = TRUE,
-                                      $allowOtherAmmount = TRUE,
+                                      $allowOtherAmount = TRUE,
                                       $isConfirmEnabled = TRUE,
                                       $financialType = 'Donation',
                                       $fixedAmount = TRUE,
@@ -1009,7 +1056,7 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
         $this->click("is_recur_interval");
         $this->click("is_recur_installments");
       }
-      if ($allowOtherAmmount) {
+      if ($allowOtherAmount) {
 
         $this->click('is_allow_other_amount');
 
@@ -1017,7 +1064,7 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
         //$this->type('min_amount', $rand / 2);
         //$this->type('max_amount', $rand * 10);
       }
-      if ($fixedAmount || !$allowOtherAmmount) {
+      if ($fixedAmount || !$allowOtherAmount) {
         $this->type('label_1', "Label $hash");
         $this->type('value_1', "$rand");
       }
@@ -1527,7 +1574,7 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
    * @param $returnColumn
    * @param $searchColumn
    * @param $expectedValue
-   * @param $message
+   * @param string $message
    */
   function assertDBCompareValue($daoName, $searchValue, $returnColumn, $searchColumn,
                                 $expectedValue, $message
@@ -2036,7 +2083,7 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
 
       $fieldLabel = "custom_field_for_{$customSet['entity']}_{$customSet['subEntity']}" . substr(sha1(rand()), 0, 4);
       $this->type('label', $fieldLabel);
-      $this->click('_qf_Field_next-bottom');
+      $this->click('_qf_Field_next_new-bottom');
       $this->waitForPageToLoad($this->getTimeoutMsec());
       $customGroupTitle = preg_replace('/\s/', '_', trim($customGroupTitle));
 
@@ -2051,10 +2098,10 @@ class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
    */
   function select2($fieldName,$label, $multiple = FALSE, $xpath=FALSE) {
     if ($multiple) {
-      $this->clickAt("//*[@id='$fieldName']/../div/ul/li[1]");
-      $this->keyDown("//*[@id='$fieldName']/../div/ul/li[1]/input", " ");
-      $this->type("//*[@id='$fieldName']/../div/ul/li[1]/input", $label);
-      $this->typeKeys("//*[@id='$fieldName']/../div/ul/li[1]/input", $label);
+      $this->clickAt("//*[@id='$fieldName']/../div/ul/li");
+      $this->keyDown("//*[@id='$fieldName']/../div/ul/li//input", " ");
+      $this->type("//*[@id='$fieldName']/../div/ul/li//input", $label);
+      $this->typeKeys("//*[@id='$fieldName']/../div/ul/li//input", $label);
       $this->waitForElementPresent("//*[@class='select2-result-label']");
       $this->clickAt("//*[@class='select2-results']/li[1]/div");
     }