Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-14-13-42-39
authorkurund <kurund@civicrm.org>
Mon, 14 Jul 2014 09:29:02 +0000 (14:59 +0530)
committerkurund <kurund@civicrm.org>
Mon, 14 Jul 2014 09:29:02 +0000 (14:59 +0530)
Conflicts:
CRM/Contact/BAO/Contact.php
CRM/Contact/BAO/Contact/Utils.php

CRM/Contact/BAO/Contact.php
CRM/Contact/Selector.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/MembershipTest.php

index 54fe7206ce99869dc7f2b2261f643a2c4d4b1e9b..f545f04b7863fa18b4d3873053cef5ca3c2c52b1 100644 (file)
@@ -234,13 +234,14 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact {
     }
 
     if ($contact->contact_type == 'Individual' && (isset($params['current_employer']) || isset($params['employer_id']))) {
-      $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params);
       // create current employer
+      $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params);
+
+      $newContact = FALSE;
+      if (empty($params['contact_id'])) {
+        $newContact = TRUE;
+      }
       if ($newEmployer) {
-        $newContact = FALSE;
-        if (empty($params['contact_id'])) {
-          $newContact = TRUE;
-        }
         CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact);
       }
       else {
index 857868fee313c3e8b075ec712882490c481a10a3..7c9b3b6beca0955939143bffc2b8ba1670e94c49 100644 (file)
@@ -855,7 +855,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
       $this->fillupPrevNextCache($sort, $cacheKey);
     }
     elseif ($firstRecord >= $countRow) {
-      $this->fillupPrevNextCache($sort, $cacheKey, $countRow, $firstRecord + 500);
+      $this->fillupPrevNextCache($sort, $cacheKey, $countRow, 500);
     }
     return $cacheKey;
   }
@@ -997,7 +997,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
 
     $insertSQL = "
 INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
-SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
+SELECT DISTINCT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
 ";
 
     $sql = str_replace($replaceSQL, $insertSQL, $sql);
index e6ff0d9d6bc091f76e224411bec057b8cd4b8675..37c6b9cbe645c6f1570315f8fefdec0955f07afa 100644 (file)
@@ -58,7 +58,7 @@ define('API_LATEST_VERSION', 3);
 class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
 
   /**
-   * api version - easier to override than just a defin
+   * api version - easier to override than just a define
    */
   protected $_apiversion = API_LATEST_VERSION;
   /**
@@ -988,38 +988,28 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * Generic function to create Organisation, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple organizations
    *
    * @return int    id of Organisation created
    */
-  function organizationCreate($params = array()) {
+  function organizationCreate($params = array(), $seq = 0) {
     if (!$params) {
       $params = array();
     }
-    $orgParams = array(
-      'organization_name' => 'Unit Test Organization',
-      'contact_type' => 'Organization',
-    );
-    return $this->_contactCreate(array_merge($orgParams, $params));
+    $params = array_merge($this->sampleContact('Organization', $seq), $params);
+    return $this->_contactCreate($params);
   }
 
   /**
    * Generic function to create Individual, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple individuals
    *
    * @return int    id of Individual created
    */
-  function individualCreate($params = array()) {
-    $params = array_merge(array(
-        'first_name' => 'Anthony',
-        'middle_name' => 'J.',
-        'last_name' => 'Anderson',
-        'prefix_id' => 3,
-        'suffix_id' => 3,
-        'email' => 'anthony_anderson@civicrm.org',
-        'contact_type' => 'Individual',
-      ), $params);
-
+  function individualCreate($params = array(), $seq = 0) {
+    $params = array_merge($this->sampleContact('Individual', $seq), $params);
     return $this->_contactCreate($params);
   }
 
@@ -1027,17 +1017,53 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * Generic function to create Household, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple households
    *
    * @return int    id of Household created
    */
-  function householdCreate($params =  array()) {
-    $params = array_merge(array(
-        'household_name' => 'Unit Test household',
-        'contact_type' => 'Household',
-      ), $params);
+  function householdCreate($params = array(), $seq = 0) {
+    $params = array_merge($this->sampleContact('Household', $seq), $params);
     return $this->_contactCreate($params);
   }
 
+  /**
+   * Helper function for getting sample contact properties
+   *
+   * @param enum     contact type: Individual, Organization
+   * @param int      sequence number for the values of this type
+   *
+   * @return array   properties of sample contact (ie. $params for API call)
+   */
+  function sampleContact($contact_type, $seq = 0) {
+    $samples = array(
+      'Individual' => array(
+        // The number of values in each list need to be coprime numbers to not have duplicates
+        'first_name' => array('Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'),
+        'middle_name' => array('J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'),
+        'last_name' => array('Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'),
+      ),
+      'Organization' => array(
+        'organization_name' => array('Unit Test Organization', 'Acme', 'Roberts and Sons', 'Cryo Space Labs', 'Sharper Pens'),
+      ),
+      'Household' => array(
+        'household_name' => array('Unit Test household'),
+      ),
+    );
+    $params = array('contact_type' => $contact_type);
+    foreach ($samples[$contact_type] as $key => $values) {
+      $params[$key] = $values[$seq % sizeof($values)];
+    }
+    if ($contact_type == 'Individual' ) {
+        $employer = $this->sampleContact('Organization', $seq);
+        $params['email'] = strtolower(
+          $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
+        );
+        $params['prefix_id'] = 3;
+        $params['suffix_id'] = 3;
+    }
+    return $params;
+  }
+
   /**
    * Private helper function for calling civicrm_contact_add
    *
index b79df091b40853d094c4cd0147c00a115b3ca674..70f6346668e41507a2eb542f52da44d9a31aa4a3 100644 (file)
@@ -440,6 +440,132 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
     $this->contactDelete($memberContactId);
   }
 
+  /**
+   * Test civicrm_membership_create with relationships.
+   * create/get Memberships.
+   *
+   * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
+   * and max_related property for Membership_Type and Membership entities
+   */
+  function testCreateWithRelationship() {
+    // Create membership type: inherited through employment, max_related = 2
+    $params = array(
+      'name_a_b' => 'Employee of',
+    );
+    $result = $this->callAPISuccess('relationship_type', 'get', $params);
+    $relationshipTypeId = $result['id'];
+    $membershipOrgId = $this->organizationCreate();
+    $params = array(
+      'name' => 'Corporate Membership',
+      'duration_unit' => 'year',
+      'duration_interval' => 1,
+      'period_type' => 'rolling',
+      'member_of_contact_id' => $membershipOrgId,
+      'domain_id' => 1,
+      'financial_type_id' => 1,
+      'relationship_type_id' => $relationshipTypeId,
+      'relationship_direction' => 'b_a',
+      'max_related' => 2,
+      'is_active' => 1,
+    );
+    $result = $this->callAPISuccess('membership_type', 'create', $params);
+    $membershipTypeId = $result['id'];
+
+    // Create employer and first employee
+    $employerId[0] = $this->organizationCreate(array(), 1);
+    $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
+
+    // Create organization's membership
+    $params = array(
+      'contact_id' => $employerId[0],
+      'membership_type_id' => $membershipTypeId,
+      'source' => 'Test suite',
+      'start_date' => date('Y-m-d'),
+      'end_date' => "+1 year",
+    );
+    $OrganizationMembershipID = $this->contactMembershipCreate($params);
+
+    // Check that the employee inherited the membership
+    $params = array(
+      'contact_id' => $memberContactId[0],
+      'membership_type_id' => $membershipTypeId,
+    );
+
+    $result = $this->callAPISuccess('membership', 'get', $params);
+
+    $this->assertEquals(1, $result['count']);
+    $result = $result['values'][$result['id']];
+    $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
+
+    // Create second employee
+    $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
+
+    // Check that the employee inherited the membership
+    $params = array(
+      'contact_id' => $memberContactId[1],
+      'membership_type_id' => $membershipTypeId,
+    );
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    //exit;
+    // If it fails here CRM-14758 is not fixed
+    $this->assertEquals(1, $result['count']);
+    $result = $result['values'][$result['id']];
+    $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
+
+    // Create third employee
+    $memberContactId[2] = $this->individualCreate(array('current_employer' => $employerId[0]), 2);
+
+    // Check that employee does NOT inherit the membership (max_related = 2)
+    $params = array(
+      'contact_id' => $memberContactId[2],
+      'membership_type_id' => $membershipTypeId,
+    );
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(0, $result['count']);
+
+    // Increase max_related for the employer's membership
+    $params = array(
+      'id' => $OrganizationMembershipID,
+      'max_related' => 3,
+    );
+    $this->contactMembershipCreate($params);
+
+    // Check that the employee inherited the membership
+    $params = array(
+      'contact_id' => $memberContactId[2],
+      'membership_type_id' => $membershipTypeId,
+    );
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(1, $result['count']);
+    $result = $result['values'][$result['id']];
+    $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
+
+    // First employee moves to a new job
+    $employerId[1] = $this->organizationCreate(array(), 2);
+    $params = array(
+      'id' => $memberContactId[0],
+      'employer_id' => $employerId[1],
+    );
+    $this->callAPISuccess('contact', 'create', $params);
+
+    // Check that employee does NO LONGER inherit the membership
+    $params = array(
+      'contact_id' => $memberContactId[0],
+      'membership_type_id' => $membershipTypeId,
+    );
+    $result = $this->callAPISuccess('membership', 'get', $params);
+    $this->assertEquals(0, $result['count']);
+
+    // Tear down - reverse of creation to be safe
+    $this->contactDelete($memberContactId[2]);
+    $this->contactDelete($memberContactId[1]);
+    $this->contactDelete($memberContactId[0]);
+    $this->contactDelete($employerId[1]);
+    $this->contactDelete($employerId[0]);
+    $this->membershipTypeDelete(array('id' => $membershipTypeId));
+    $this->contactDelete($membershipOrgId);
+  }
+
   /**
    * We are checking for no enotices + only id & end_date returned
    */
@@ -869,4 +995,3 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
 
    }
 }
-