CRM-13294 fix membership to support IN format on contact_id
authoreileen <eileen@fuzion.co.nz>
Wed, 28 Aug 2013 04:13:28 +0000 (16:13 +1200)
committereileen <eileen@fuzion.co.nz>
Wed, 28 Aug 2013 04:14:46 +0000 (16:14 +1200)
Conflicts:

tests/phpunit/api/v3/MembershipTest.php

api/v3/Membership.php
tests/phpunit/api/v3/MembershipTest.php

index a04bf1b0d23690b5a97046b4b80599e4c37f63d6..3d4d54140b788d7cdd230fe8ae85d53ee9b3cf58 100644 (file)
@@ -191,8 +191,8 @@ function civicrm_api3_membership_get($params) {
   }
   $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
 
-  if (CRM_Utils_Array::value('contact_id', $params)) {
-    $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly );
+  if (CRM_Utils_Array::value('contact_id', $params) && !is_array($params['contact_id'])) {
+    $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $membershipTypeId, $activeOnly );
   }
   else {
     //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise
@@ -260,9 +260,9 @@ function civicrm_api3_membership_get($params) {
  * @param array $params parameters passed into get function
  * @return array result for calling function
  */
-function _civicrm_api3_membership_get_customv2behaviour(&$params, $contactID, $membershipTypeId, $activeOnly) {
+function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) {
   // get the membership for the given contact ID
-  $membershipParams = array('contact_id' => $contactID);
+  $membershipParams = array('contact_id' => $params['contact_id']);
   if ($membershipTypeId) {
     $membershipParams['membership_type_id'] = $membershipTypeId;
   }
index 4965eb5cbe89cc2c50d9c77dfbd4c8de89c77b63..ed58352fedd816a598f09192b82478563a2fb6d2 100644 (file)
@@ -185,6 +185,51 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
    * Test civicrm_membership_get with params not array.
    * Gets treated as contact_id, memberships expected.
    */
+  function testGetInSyntax() {
+    $this->_membershipID = $this->contactMembershipCreate($this->_params);
+    $this->_membershipID2 = $this->contactMembershipCreate($this->_params);
+    $this->_membershipID3 = $this->contactMembershipCreate($this->_params);
+    $params = array(
+      'id' => array('IN' => array($this->_membershipID, $this->_membershipID3)),
+    );
+    $membership = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(2, $membership['count']);
+    $this->assertEquals(array($this->_membershipID, $this->_membershipID3), array_keys($membership['values']));
+    $params = array(
+      'id' => array('NOT IN' => array($this->_membershipID, $this->_membershipID3)),
+    );
+    $membership = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(1, $membership['count']);
+    $this->assertEquals(array($this->_membershipID2), array_keys($membership['values']));
+  }
+  /**
+   * Test civicrm_membership_get with params not array.
+   * Gets treated as contact_id, memberships expected.
+   */
+  function testGetInSyntaxOnContactID() {
+    $this->_membershipID = $this->contactMembershipCreate($this->_params);
+    $contact2 = $this->individualCreate();
+    $contact3 = $this->individualCreate(array('first_name' => 'Scout', 'last_name' => 'Canine'));
+    $this->_membershipID2 = $this->contactMembershipCreate(array_merge($this->_params, array('contact_id' => $contact2)));
+    $this->_membershipID3 = $this->contactMembershipCreate(array_merge($this->_params, array('contact_id' => $contact3)));
+    $params = array(
+      'contact_id' => array('IN' => array($this->_contactID, $contact3)),
+    );
+    $membership = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(2, $membership['count']);
+    $this->assertEquals(array($this->_membershipID, $this->_membershipID3), array_keys($membership['values']));
+    $params = array(
+      'contact_id' => array('NOT IN' => array($this->_contactID, $contact3)),
+    );
+    $membership = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(1, $membership['count']);
+    $this->assertEquals(array($this->_membershipID2), array_keys($membership['values']));
+  }
+  /**
+   * Test civicrm_membership_get with params not array.
+   * Gets treated as contact_id, memberships expected.
+   */
+
   function testGetWithParamsMemberShipTypeId() {
     $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
     $params = array(