Merge pull request #1127 from eileenmcnaughton/4.3
authorTim Otten <to-git@think.hm>
Sat, 6 Jul 2013 02:30:30 +0000 (19:30 -0700)
committerTim Otten <to-git@think.hm>
Sat, 6 Jul 2013 02:30:30 +0000 (19:30 -0700)
4.3 developer support, apiv3 wrapper, uf level functions to support civimobile/other extensions

CRM/Utils/System/Base.php
CRM/Utils/System/Drupal.php
CRM/Utils/System/Drupal6.php
api/Exception.php
api/api.php
tests/phpunit/api/v3/APITest.php

index 5f7f1b7859000d94c263dd845e7c9eb65ded4bcd..153c7a260719bb6e418faa49fdddf8ec0d821d65 100644 (file)
@@ -124,5 +124,13 @@ abstract class CRM_Utils_System_Base {
   function flush() {
     // nullop by default
   }
+
+  /**
+   * Perform an post login activities required by the UF -
+   * 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 $edit: The array of form values submitted by the user.
+   */
+  function userLoginFinalize($edit = array()){
+  }
 }
 
index c1652527aa2f562b0191cb36a5cf0a00e1070695..90108352324ca47c816ff8a99ce750823205d63a 100644 (file)
@@ -652,6 +652,15 @@ AND    u.status = 1
     return TRUE;
   }
 
+  /**
+   * Perform an post login activities required by the UF -
+   * 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 $edit: The array of form values submitted by the user.
+   */
+  function userLoginFinalize($edit = array()){
+    user_login_finalize(&$edit);
+  }
+
   /**
    * Set a message in the UF to display to a user
    *
index e7a3dfe5e71d1ca2e8c058a9b85e9c4e46dd4b39..832a61b93496686a601210193c07e94e7336c69c 100644 (file)
@@ -583,6 +583,15 @@ SELECT name, mail
     return TRUE;
   }
 
+  /**
+   * Perform an post login activities required by the UF -
+   * 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 $edit: The array of form values submitted by the user.
+   */
+  function userLoginFinalize($edit = array()){
+    user_authenticate_finalize(&$edit);
+  }
+
   /**
    * Set a message in the UF to display to a user
    *
index e8f24e4e0157ac9bd745a24ed23eaee64719f10d..ea4705820e010eeb8997a1ef0eb8cd6996d4dfdb 100644 (file)
@@ -48,3 +48,31 @@ class API_Exception extends Exception
         );
   }
 }
+/**
+ * This api exception returns more information than the default one. We are using it rather than
+ * API_Exception from the api wrapper as the namespace is more generic
+ * @param string $message the human friendly error message
+ * @param string $error_code a computer friendly error code. By convention, no space (but underscore allowed)
+ *  ex: mandatory_missing, duplicate, invalid_format
+ * @param array $data extra params to return. eg an extra array of ids. It is not mandatory, but can help the computer using the api. Keep in mind the api consumer isn't to be trusted. eg. the database password is NOT a good extra data
+ */
+class CiviCRM_API3_Exception extends Exception
+{
+  private $extraParams = array();
+  public function __construct($message, $error_code, $extraParams = array(),Exception $previous = null) {
+    parent::__construct(ts($message));
+    $this->extraParams = $extraParams + array('error_code' => $error_code);
+  }
+
+  // custom string representation of object
+  public function __toString() {
+    return __CLASS__ . ": [{$this->extraParams['error_code']}: {$this->message}\n";
+  }
+
+  public function getErrorCode() {
+    return $this->extraParams['error_code'];
+  }
+  public function getExtraParams() {
+    return $this->extraParams;
+  }
+}
index 84f0a99330271d1c1d65c8400e1695c153354f52..3ade1a5b39e1809f0177ccbd26b371665316d5e8 100644 (file)
@@ -238,6 +238,22 @@ function _civicrm_api_resolve($apiRequest) {
   $cache[$cachekey] = array('function' => FALSE, 'is_generic' => FALSE);
   return $cache[$cachekey];
 }
+/**
+ * Version 3 wrapper for civicrm_api. Throws exception
+ * @param string $entity type of entities to deal with
+ * @param string $action create, get, delete or some special action name.
+ * @param array $params array to be passed to function
+ *
+ * @return array
+ *
+ */
+function civicrm_api3($entity, $action, $params){
+  $params['version'] = 3;
+  $result = civicrm_api($entity, $action, $params);
+  if($result['is_error']){
+    throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result);
+  }
+}
 
 /**
  * Load/require all files related to an entity.
index 7cb1b6f2a91113f8947d2e3ee6852c0d4d63df14..5b7f8af0423a8cb695ca7e36ab090fc79ecb04af 100644 (file)
@@ -168,5 +168,35 @@ class api_v3_APITest extends CiviUnitTestCase {
       $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual));
     }
   }
+/**
+ * Test that calling via wrapper works
+ */
+  function testv3Wrapper() {
+    try{
+      $result = civicrm_api3('contact', 'get', array());
+    }
+    catch (CRM_Exception $e){
+      $this->fail("This should have been a success test");
+    }
+    $this->assertAPISuccess($result);
+  }
+
+  /**
+   * test exception is thrown
+   */
+  function testv3WrapperException(){
+    try{
+      $result = civicrm_api3('contact', 'create', array('debug' => 1));
+    }
+    catch (CiviCRM_API3_Exception $e){
+      $this->assertEquals('undefined', $e->getErrorCode());
+      $this->assertEquals('Mandatory key(s) missing from params array: contact_type', $e->getMessage());
+      $extra = $e->getExtraParams();
+      $this->assertArrayHasKey('trace', $extra);
+      return;
+    }
+    $this->fail('Exception was expected');
+  }
+
 }