}
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 {
$this->fillupPrevNextCache($sort, $cacheKey);
}
elseif ($firstRecord >= $countRow) {
- $this->fillupPrevNextCache($sort, $cacheKey, $countRow, $firstRecord + 500);
+ $this->fillupPrevNextCache($sort, $cacheKey, $countRow, 500);
}
return $cacheKey;
}
$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);
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;
/**
* 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);
}
* 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
*
$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
*/
}
}
-