CRM-15903 - Modify CRM_Utils_System_UnitTests to not extend Drupal
[civicrm-core.git] / CRM / Utils / System / Drupal.php
index 84455192536bdb292bf0b0a9a04c3cccb34cfc02..5b7c1842be4af45c82b5297a47b0eb33b86f1bc5 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
 class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
 
   /**
-   * Create a user in Drupal.
-   *
-   * @param array  $params associated array
-   * @param string $mail email id for cms user
-   *
-   * @return uid if user exists, false otherwise
-   *
-   *
+   * @inheritDoc
    */
   public function createUser(&$params, $mail) {
     $form_state = form_state_defaults();
@@ -59,10 +52,10 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
 
     $admin = user_access('administer users');
     if (!variable_get('user_email_verification', TRUE) || $admin) {
-      $form_state['input']['pass'] = array('pass1'=>$params['cms_pass'],'pass2'=>$params['cms_pass']);
+      $form_state['input']['pass'] = array('pass1' => $params['cms_pass'], 'pass2' => $params['cms_pass']);
     }
 
-    if(!empty($params['notify'])){
+    if (!empty($params['notify'])) {
       $form_state['input']['notify'] = $params['notify'];
     }
 
@@ -72,10 +65,10 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
     $form_state['method'] = 'post';
     $form_state['build_info']['args'] = array();
     /*
-    * if we want to submit this form more than once in a process (e.g. create more than one user)
-    * we must force it to validate each time for this form. Otherwise it will not validate
-    * subsequent submissions and the manner in which the password is passed in will be invalid
-    */
+     * if we want to submit this form more than once in a process (e.g. create more than one user)
+     * we must force it to validate each time for this form. Otherwise it will not validate
+     * subsequent submissions and the manner in which the password is passed in will be invalid
+     */
     $form_state['must_validate'] = TRUE;
     $config = CRM_Core_Config::singleton();
 
@@ -97,15 +90,8 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
     return $form_state['user']->uid;
   }
 
-  /*
-   *  Change user name in host CMS
-   *
-   *  @param integer $ufID User ID in CMS
-   *  @param string $ufName User name
-   */
   /**
-   * @param int $ufID
-   * @param string $ufName
+   * @inheritDoc
    */
   public function updateCMSName($ufID, $ufName) {
     // CRM-5555
@@ -121,18 +107,21 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   /**
    * Check if username and email exists in the drupal db
    *
-   * @param array $params array of name and mail values
-   * @param array $errors array of errors
-   * @param string $emailName  field label for the 'email'
+   * @param array $params
+   *   Array of name and mail values.
+   * @param array $errors
+   *   Array of errors.
+   * @param string $emailName
+   *   Field label for the 'email'.
    *
    * @return void
    */
   public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
     $config = CRM_Core_Config::singleton();
 
-    $dao    = new CRM_Core_DAO();
-    $name   = $dao->escape(CRM_Utils_Array::value('name', $params));
-    $email  = $dao->escape(CRM_Utils_Array::value('mail', $params));
+    $dao = new CRM_Core_DAO();
+    $name = $dao->escape(CRM_Utils_Array::value('name', $params));
+    $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
     $errors = form_get_errors();
     if ($errors) {
       // unset drupal messages to avoid twice display of errors
@@ -173,72 +162,16 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
     }
   }
 
-  /*
-   * Get the drupal destination string. When this is passed in the
-   * URL the user will be directed to it after filling in the drupal form
-   *
-   * @param CRM_Core_Form $form Form object representing the 'current' form - to which the user will be returned
-   * @return string $destination destination value for URL
-   *
-   */
   /**
-   * @param CRM_Core_Form $form
-   *
-   * @return null|string
-   */
-  public function getLoginDestination(&$form) {
-    $args = NULL;
-
-    $id = $form->get('id');
-    if ($id) {
-      $args .= "&id=$id";
-    }
-    else {
-      $gid = $form->get('gid');
-      if ($gid) {
-        $args .= "&gid=$gid";
-      }
-      else {
-        // Setup Personal Campaign Page link uses pageId
-        $pageId = $form->get('pageId');
-        if ($pageId) {
-          $component = $form->get('component');
-          $args .= "&pageId=$pageId&component=$component&action=add";
-        }
-      }
-    }
-
-    $destination = NULL;
-    if ($args) {
-      // append destination so user is returned to form they came from after login
-      $destination = CRM_Utils_System::currentPath() . '?reset=1' . $args;
-    }
-    return $destination;
-  }
-
-  /**
-   * Get user login URL for hosting CMS (method declared in each CMS system class)
-   *
-   * @param string $destination - if present, add destination to querystring (works for Drupal only)
-   *
-   * @return string - loginURL for the current CMS
-   * @static
+   * @inheritDoc
    */
   public function getLoginURL($destination = '') {
     $query = $destination ? array('destination' => $destination) : array();
     return url('user', array('query' => $query));
   }
 
-
   /**
-   * Sets the title of the page
-   *
-   * @param string $title
-   * @param null $pageTitle
-   *
-   * @paqram string $pageTitle
-   *
-   * @return void
+   * @inheritDoc
    */
   public function setTitle($title, $pageTitle = NULL) {
     if (arg(0) == 'civicrm') {
@@ -251,13 +184,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Append an additional breadcrumb tag to the existing breadcrumb
-   *
-   * @param array $breadCrumbs
-   * @internal param string $title
-   * @internal param string $url
-   *
-   * @return void
+   * @inheritDoc
    */
   public function appendBreadCrumb($breadCrumbs) {
     $breadCrumb = drupal_get_breadcrumb();
@@ -282,9 +209,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Reset an additional breadcrumb tag to the existing breadcrumb
-   *
-   * @return void
+   * @inheritDoc
    */
   public function resetBreadCrumb() {
     $bc = array();
@@ -292,11 +217,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Append a string to the head of the html file
-   *
-   * @param string $header the new string to be appended
-   *
-   * @return void
+   * @inheritDoc
    */
   public function addHTMLHead($header) {
     static $count = 0;
@@ -311,15 +232,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Add a script file
-   *
-   * @param $url: string, absolute path to file
-   * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
-   *
-   * Note: This function is not to be called directly
-   * @see CRM_Core_Region::render()
-   *
-   * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
+   * @inheritDoc
    */
   public function addScriptUrl($url, $region) {
     $params = array('group' => JS_LIBRARY, 'weight' => 10);
@@ -328,6 +241,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
       case 'page-footer':
         $params['scope'] = substr($region, 5);
         break;
+
       default:
         return FALSE;
     }
@@ -338,15 +252,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Add an inline script
-   *
-   * @param $code: string, javascript code
-   * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
-   *
-   * Note: This function is not to be called directly
-   * @see CRM_Core_Region::render()
-   *
-   * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
+   * @inheritDoc
    */
   public function addScript($code, $region) {
     $params = array('type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10);
@@ -355,6 +261,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
       case 'page-footer':
         $params['scope'] = substr($region, 5);
         break;
+
       default:
         return FALSE;
     }
@@ -363,15 +270,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Add a css file
-   *
-   * @param $url: string, absolute path to file
-   * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
-   *
-   * Note: This function is not to be called directly
-   * @see CRM_Core_Region::render()
-   *
-   * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
+   * @inheritDoc
    */
   public function addStyleUrl($url, $region) {
     if ($region != 'html-header') {
@@ -385,15 +284,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Add an inline style
-   *
-   * @param $code: string, css code
-   * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
-   *
-   * Note: This function is not to be called directly
-   * @see CRM_Core_Region::render()
-   *
-   * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
+   * @inheritDoc
    */
   public function addStyle($code, $region) {
     if ($region != 'html-header') {
@@ -405,11 +296,7 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Rewrite various system urls to https
-   *
-   * @param null
-   *
-   * @return void
+   * @inheritDoc
    */
   public function mapConfigToSSL() {
     global $base_url;
@@ -417,34 +304,9 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * Figure out the post url for the form
-   *
-   * @param mix $action the default action if one is pre-specified
-   *
-   * @return string the url to post the form
+   * @inheritDoc
    */
-  public function postURL($action) {
-    if (!empty($action)) {
-      return $action;
-    }
-
-    return $this->url($_GET['q']);
-  }
-
-
-  /**
-   * Authenticate the user against the drupal db
-   *
-   * @param string $name     the user name
-   * @param string $password the password for the above user name
-   * @param boolean $loadCMSBootstrap load cms bootstrap?
-   * @param NULL|string $realPath filename of script
-   *
-   * @return mixed false if no auth
-   *               array(
-   *  contactID, ufID, unique string ) if success
-   */
-   public static function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
+  public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
     require_once 'DB.php';
 
     $config = CRM_Core_Config::singleton();
@@ -477,12 +339,12 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
       // Contact CiviSMTP folks if we run into issues with this :)
       $cmsPath = $config->userSystem->cmsRootPath($realPath);
 
-      require_once ("$cmsPath/includes/bootstrap.inc");
-      require_once ("$cmsPath/includes/password.inc");
+      require_once "$cmsPath/includes/bootstrap.inc";
+      require_once "$cmsPath/includes/password.inc";
 
       $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
-      $name       = $dbDrupal->escapeSimple($strtolower($name));
-      $sql        = "
+      $name = $dbDrupal->escapeSimple($strtolower($name));
+      $sql = "
 SELECT u.*
 FROM   {$config->userFrameworkUsersTableName} u
 WHERE  LOWER(u.name) = '$name'
@@ -515,13 +377,8 @@ AND    u.status = 1
     return FALSE;
   }
 
-  /*
-   * Load user into session
-   */
   /**
-   * @param string $username
-   *
-   * @return bool
+   * @inheritDoc
    */
   public function loadUser($username) {
     global $user;
@@ -547,11 +404,11 @@ AND    u.status = 1
    * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
    * calls hook_user op 'login' and generates a new session.
    *
-   * @param array params
+   * @param array $params
    *
    * FIXME: Document values accepted/required by $params
    */
-  public function userLoginFinalize($params = array()){
+  public function userLoginFinalize($params = array()) {
     user_login_finalize($params);
   }
 
@@ -570,30 +427,13 @@ AND    u.status = 1
   }
 
   /**
-   * Set a message in the UF to display to a user
-   *
-   * @param string $message the message to set
-   *
-   */
-  public function setMessage($message) {
-    drupal_set_message($message);
-  }
-
-  /**
-   * @return mixed
+   * @inheritDoc
    */
   public function logout() {
     module_load_include('inc', 'user', 'user.pages');
     return user_logout();
   }
 
-  public function updateCategories() {
-    // copied this from profile.module. Seems a bit inefficient, but i dont know a better way
-    // CRM-3600
-    cache_clear_all();
-    menu_rebuild();
-  }
-
   /**
    * Get the default location for CiviCRM blocks
    *
@@ -603,50 +443,16 @@ AND    u.status = 1
     return 'sidebar_first';
   }
 
-  /**
-   * Get the locale set in the hosting CMS
-   *
-   * @return string  with the locale or null for none
-   */
-  public function getUFLocale() {
-    // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
-    // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
-    // sometimes for CLI based on order called, this might not be set and/or empty
-    global $language;
-
-    if (empty($language)) {
-      return NULL;
-    }
-
-    if ($language->language == 'zh-hans') {
-      return 'zh_CN';
-    }
-
-    if ($language->language == 'zh-hant') {
-      return 'zh_TW';
-    }
-
-    if (preg_match('/^.._..$/', $language->language)) {
-      return $language->language;
-    }
-
-    return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2));
-  }
-
-  /**
-   * @return string
-   */
-  public function getVersion() {
-    return defined('VERSION') ? VERSION : 'Unknown';
-  }
-
   /**
    * Load drupal bootstrap
    *
-   * @param array $params Either uid, or name & pass.
-   * @param boolean $loadUser boolean Require CMS user load.
-   * @param boolean $throwError If true, print error on failure and exit.
-   * @param boolean|string $realPath path to script
+   * @param array $params
+   *   Either uid, or name & pass.
+   * @param bool $loadUser
+   *   Boolean Require CMS user load.
+   * @param bool $throwError
+   *   If true, print error on failure and exit.
+   * @param bool|string $realPath path to script
    *
    * @return bool
    */
@@ -739,7 +545,7 @@ AND    u.status = 1
     // which means that define(CIVICRM_CLEANURL) was correctly set.
     // So we correct it
     $config = CRM_Core_Config::singleton();
-    $config->cleanURL = (int)variable_get('clean_url', '0');
+    $config->cleanURL = (int) variable_get('clean_url', '0');
 
     // CRM-8655: Drupal wasn't available during bootstrap, so hook_civicrm_config never executes
     CRM_Utils_Hook::config($config);
@@ -748,7 +554,6 @@ AND    u.status = 1
   }
 
   /**
-   *
    */
   public function cmsRootPath($scriptFilename = NULL) {
     $cmsRoot = $valid = NULL;
@@ -796,9 +601,7 @@ AND    u.status = 1
   }
 
   /**
-   * Check is user logged in.
-   *
-   * @return boolean true/false.
+   * @inheritDoc
    */
   public function isUserLoggedIn() {
     $isloggedIn = FALSE;
@@ -810,9 +613,7 @@ AND    u.status = 1
   }
 
   /**
-   * Get currently logged in user uf id.
-   *
-   * @return int $userID logged in user uf id.
+   * @inheritDoc
    */
   public function getLoggedInUfID() {
     $ufID = NULL;
@@ -827,15 +628,7 @@ AND    u.status = 1
   }
 
   /**
-   * Format the url as per language Negotiation.
-   *
-   * @param string $url
-   *
-   * @param bool $addLanguagePart
-   * @param bool $removeLanguagePart
-   *
-   * @return string $url, formatted url.
-   * @static
+   * @inheritDoc
    */
   public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
     if (empty($url)) {
@@ -884,11 +677,11 @@ AND    u.status = 1
               //kinda hackish but not sure how to do it right
               //hope http_build_url() will help at some point.
               if (is_array($parseUrl) && !empty($parseUrl)) {
-                $urlParts           = explode('/', $url);
-                $hostKey            = array_search($parseUrl['host'], $urlParts);
-                $ufUrlParts         = parse_url(CIVICRM_UF_BASEURL);
+                $urlParts = explode('/', $url);
+                $hostKey = array_search($parseUrl['host'], $urlParts);
+                $ufUrlParts = parse_url(CIVICRM_UF_BASEURL);
                 $urlParts[$hostKey] = $ufUrlParts['host'];
-                $url                = implode('/', $urlParts);
+                $url = implode('/', $urlParts);
               }
             }
           }
@@ -903,8 +696,9 @@ AND    u.status = 1
    * Find any users/roles/security-principals with the given permission
    * and replace it with one or more permissions.
    *
-   * @param $oldPerm string
-   * @param $newPerms array, strings
+   * @param string $oldPerm
+   * @param array $newPerms
+   *   Array, strings.
    *
    * @return void
    */
@@ -918,27 +712,15 @@ AND    u.status = 1
     }
   }
 
-  /**
-   * Get a list of all installed modules, including enabled and disabled ones
-   *
-   * @return array CRM_Core_Module
-   */
-  public function getModules() {
-    $result = array();
-    $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1');
-    foreach ($q as $row) {
-      $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE);
-    }
-    return $result;
-  }
-
   /**
    * Wrapper for og_membership creation
    *
-   * @param integer $ogID Organic Group ID
-   * @param integer $drupalID drupal User ID
+   * @param int $ogID
+   *   Organic Group ID.
+   * @param int $drupalID
+   *   Drupal User ID.
    */
-  public function og_membership_create($ogID, $drupalID){
+  public function og_membership_create($ogID, $drupalID) {
     if (function_exists('og_entity_query_alter')) {
       // sort-of-randomly chose a function that only exists in the // 7.x-2.x branch
       //
@@ -956,8 +738,10 @@ AND    u.status = 1
   /**
    * Wrapper for og_membership deletion
    *
-   * @param integer $ogID Organic Group ID
-   * @param integer $drupalID drupal User ID
+   * @param int $ogID
+   *   Organic Group ID.
+   * @param int $drupalID
+   *   Drupal User ID.
    */
   public function og_membership_delete($ogID, $drupalID) {
     if (function_exists('og_entity_query_alter')) {
@@ -965,33 +749,28 @@ AND    u.status = 1
       // TODO: Find a more solid way to make this test
       // Also, since we don't know how to get the entity type of the group, we'll assume it's 'node'
       og_ungroup('node', $ogID, 'user', user_load($drupalID));
-    } else {
+    }
+    else {
       // Works for the OG 7.x-1.x branch
       og_ungroup($ogID, 'user', user_load($drupalID));
     }
   }
 
   /**
-   * Over-ridable function to get timezone as a string eg.
-   * @return string Timezone e.g. 'America/Los_Angeles'
+   * @inheritDoc
    */
   public function getTimeZoneString() {
     global $user;
     if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
       $timezone = $user->timezone;
-    } else {
-      $timezone = variable_get('date_default_timezone', null);
+    }
+    else {
+      $timezone = variable_get('date_default_timezone', NULL);
     }
     if (!$timezone) {
       $timezone = parent::getTimeZoneString();
     }
     return $timezone;
   }
-  /**
-   * Reset any system caches that may be required for proper CiviCRM
-   * integration.
-   */
-  public function flush() {
-    drupal_flush_all_caches();
-  }
+
 }